OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/video_detector.h" | 5 #include "ash/wm/video_detector.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
9 #include "ash/wm/window_util.h" | 9 #include "ash/wm/window_util.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "third_party/skia/include/core/SkColor.h" | 13 #include "third_party/skia/include/core/SkColor.h" |
14 #include "ui/aura/client/aura_constants.h" | |
15 #include "ui/aura/client/window_types.h" | 14 #include "ui/aura/client/window_types.h" |
16 #include "ui/aura/root_window.h" | 15 #include "ui/aura/root_window.h" |
17 #include "ui/aura/test/test_windows.h" | 16 #include "ui/aura/test/test_windows.h" |
18 #include "ui/aura/window.h" | 17 #include "ui/aura/window.h" |
19 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
20 | 19 |
21 namespace ash { | 20 namespace ash { |
22 namespace test { | 21 namespace test { |
23 | 22 |
24 // Implementation that just counts the number of times we've been told that a | 23 // Implementation that just counts the number of times we've been told that a |
25 // video is playing. | 24 // video is playing. |
26 class TestVideoDetectorObserver : public VideoDetectorObserver { | 25 class TestVideoDetectorObserver : public VideoDetectorObserver { |
27 public: | 26 public: |
28 TestVideoDetectorObserver() : num_invocations_(0), | 27 TestVideoDetectorObserver() : num_invocations_(0) {} |
29 num_fullscreens_(0), | |
30 num_not_fullscreens_(0) {} | |
31 | 28 |
32 int num_invocations() const { return num_invocations_; } | 29 int num_invocations() const { return num_invocations_; } |
33 int num_fullscreens() const { return num_fullscreens_; } | 30 void reset_stats() { num_invocations_ = 0; } |
34 int num_not_fullscreens() const { return num_not_fullscreens_; } | |
35 void reset_stats() { | |
36 num_invocations_ = 0; | |
37 num_fullscreens_ = 0; | |
38 num_not_fullscreens_ = 0; | |
39 } | |
40 | 31 |
41 // VideoDetectorObserver implementation. | 32 // VideoDetectorObserver implementation. |
42 virtual void OnVideoDetected(bool is_fullscreen) OVERRIDE { | 33 virtual void OnVideoDetected() OVERRIDE { num_invocations_++; } |
43 num_invocations_++; | |
44 if (is_fullscreen) | |
45 num_fullscreens_++; | |
46 else | |
47 num_not_fullscreens_++; | |
48 } | |
49 | 34 |
50 private: | 35 private: |
51 // Number of times that OnVideoDetected() has been called. | 36 // Number of times that OnVideoDetected() has been called. |
52 int num_invocations_; | 37 int num_invocations_; |
53 // Number of times that OnVideoDetected() has been called with is_fullscreen | |
54 // == true. | |
55 int num_fullscreens_; | |
56 // Number of times that OnVideoDetected() has been called with is_fullscreen | |
57 // == false. | |
58 int num_not_fullscreens_; | |
59 | 38 |
60 DISALLOW_COPY_AND_ASSIGN(TestVideoDetectorObserver); | 39 DISALLOW_COPY_AND_ASSIGN(TestVideoDetectorObserver); |
61 }; | 40 }; |
62 | 41 |
63 class VideoDetectorTest : public AshTestBase { | 42 class VideoDetectorTest : public AshTestBase { |
64 public: | 43 public: |
65 VideoDetectorTest() {} | 44 VideoDetectorTest() {} |
66 virtual ~VideoDetectorTest() {} | 45 virtual ~VideoDetectorTest() {} |
67 | 46 |
68 virtual void SetUp() OVERRIDE { | 47 virtual void SetUp() OVERRIDE { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 gfx::Size(VideoDetector::kMinUpdateWidth, | 97 gfx::Size(VideoDetector::kMinUpdateWidth, |
119 VideoDetector::kMinUpdateHeight)); | 98 VideoDetector::kMinUpdateHeight)); |
120 for (int i = 0; i < VideoDetector::kMinFramesPerSecond - 1; ++i) | 99 for (int i = 0; i < VideoDetector::kMinFramesPerSecond - 1; ++i) |
121 detector_->OnWindowPaintScheduled(window.get(), update_region); | 100 detector_->OnWindowPaintScheduled(window.get(), update_region); |
122 EXPECT_EQ(0, observer_->num_invocations()); | 101 EXPECT_EQ(0, observer_->num_invocations()); |
123 | 102 |
124 // We should get notified after the next update, but not in response to | 103 // We should get notified after the next update, but not in response to |
125 // additional updates. | 104 // additional updates. |
126 detector_->OnWindowPaintScheduled(window.get(), update_region); | 105 detector_->OnWindowPaintScheduled(window.get(), update_region); |
127 EXPECT_EQ(1, observer_->num_invocations()); | 106 EXPECT_EQ(1, observer_->num_invocations()); |
128 EXPECT_EQ(0, observer_->num_fullscreens()); | |
129 EXPECT_EQ(1, observer_->num_not_fullscreens()); | |
130 detector_->OnWindowPaintScheduled(window.get(), update_region); | 107 detector_->OnWindowPaintScheduled(window.get(), update_region); |
131 EXPECT_EQ(1, observer_->num_invocations()); | 108 EXPECT_EQ(1, observer_->num_invocations()); |
132 EXPECT_EQ(0, observer_->num_fullscreens()); | |
133 EXPECT_EQ(1, observer_->num_not_fullscreens()); | |
134 | 109 |
135 // Spread out the frames over two seconds; we shouldn't detect video. | 110 // Spread out the frames over two seconds; we shouldn't detect video. |
136 observer_->reset_stats(); | 111 observer_->reset_stats(); |
137 AdvanceTime(base::TimeDelta::FromSeconds(2)); | 112 AdvanceTime(base::TimeDelta::FromSeconds(2)); |
138 for (int i = 0; i < VideoDetector::kMinFramesPerSecond - 1; ++i) | 113 for (int i = 0; i < VideoDetector::kMinFramesPerSecond - 1; ++i) |
139 detector_->OnWindowPaintScheduled(window.get(), update_region); | 114 detector_->OnWindowPaintScheduled(window.get(), update_region); |
140 AdvanceTime(base::TimeDelta::FromSeconds(1)); | 115 AdvanceTime(base::TimeDelta::FromSeconds(1)); |
141 for (int i = 0; i < VideoDetector::kMinFramesPerSecond - 1; ++i) | 116 for (int i = 0; i < VideoDetector::kMinFramesPerSecond - 1; ++i) |
142 detector_->OnWindowPaintScheduled(window.get(), update_region); | 117 detector_->OnWindowPaintScheduled(window.get(), update_region); |
143 EXPECT_EQ(0, observer_->num_invocations()); | 118 EXPECT_EQ(0, observer_->num_invocations()); |
(...skipping 18 matching lines...) Expand all Loading... |
162 detector_->OnWindowPaintScheduled(window.get(), update_region); | 137 detector_->OnWindowPaintScheduled(window.get(), update_region); |
163 EXPECT_EQ(0, observer_->num_invocations()); | 138 EXPECT_EQ(0, observer_->num_invocations()); |
164 | 139 |
165 // Make the window visible and send more updates. | 140 // Make the window visible and send more updates. |
166 observer_->reset_stats(); | 141 observer_->reset_stats(); |
167 AdvanceTime(base::TimeDelta::FromSeconds(2)); | 142 AdvanceTime(base::TimeDelta::FromSeconds(2)); |
168 window->Show(); | 143 window->Show(); |
169 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | 144 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) |
170 detector_->OnWindowPaintScheduled(window.get(), update_region); | 145 detector_->OnWindowPaintScheduled(window.get(), update_region); |
171 EXPECT_EQ(1, observer_->num_invocations()); | 146 EXPECT_EQ(1, observer_->num_invocations()); |
172 EXPECT_EQ(0, observer_->num_fullscreens()); | |
173 EXPECT_EQ(1, observer_->num_not_fullscreens()); | |
174 | 147 |
175 // We also shouldn't report video in a window that's fully offscreen. | 148 // We also shouldn't report video in a window that's fully offscreen. |
176 observer_->reset_stats(); | 149 observer_->reset_stats(); |
177 AdvanceTime(base::TimeDelta::FromSeconds(2)); | 150 AdvanceTime(base::TimeDelta::FromSeconds(2)); |
178 gfx::Rect offscreen_bounds( | 151 gfx::Rect offscreen_bounds( |
179 gfx::Point(Shell::GetPrimaryRootWindow()->bounds().width(), 0), | 152 gfx::Point(Shell::GetPrimaryRootWindow()->bounds().width(), 0), |
180 window_bounds.size()); | 153 window_bounds.size()); |
181 window->SetBounds(offscreen_bounds); | 154 window->SetBounds(offscreen_bounds); |
182 ASSERT_EQ(offscreen_bounds, window->bounds()); | 155 ASSERT_EQ(offscreen_bounds, window->bounds()); |
183 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | 156 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) |
(...skipping 13 matching lines...) Expand all Loading... |
197 // single notification. | 170 // single notification. |
198 gfx::Rect update_region( | 171 gfx::Rect update_region( |
199 gfx::Point(), | 172 gfx::Point(), |
200 gfx::Size(VideoDetector::kMinUpdateWidth, | 173 gfx::Size(VideoDetector::kMinUpdateWidth, |
201 VideoDetector::kMinUpdateHeight)); | 174 VideoDetector::kMinUpdateHeight)); |
202 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | 175 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) |
203 detector_->OnWindowPaintScheduled(window1.get(), update_region); | 176 detector_->OnWindowPaintScheduled(window1.get(), update_region); |
204 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | 177 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) |
205 detector_->OnWindowPaintScheduled(window2.get(), update_region); | 178 detector_->OnWindowPaintScheduled(window2.get(), update_region); |
206 EXPECT_EQ(1, observer_->num_invocations()); | 179 EXPECT_EQ(1, observer_->num_invocations()); |
207 EXPECT_EQ(0, observer_->num_fullscreens()); | |
208 EXPECT_EQ(1, observer_->num_not_fullscreens()); | |
209 } | 180 } |
210 | 181 |
211 // Test that the observer receives repeated notifications. | 182 // Test that the observer receives repeated notifications. |
212 TEST_F(VideoDetectorTest, RepeatedNotifications) { | 183 TEST_F(VideoDetectorTest, RepeatedNotifications) { |
213 gfx::Rect window_bounds(gfx::Point(), gfx::Size(1024, 768)); | 184 gfx::Rect window_bounds(gfx::Point(), gfx::Size(1024, 768)); |
214 scoped_ptr<aura::Window> window( | 185 scoped_ptr<aura::Window> window( |
215 aura::test::CreateTestWindow(SK_ColorRED, 12345, window_bounds, NULL)); | 186 aura::test::CreateTestWindow(SK_ColorRED, 12345, window_bounds, NULL)); |
216 | 187 |
217 gfx::Rect update_region( | 188 gfx::Rect update_region( |
218 gfx::Point(), | 189 gfx::Point(), |
219 gfx::Size(VideoDetector::kMinUpdateWidth, | 190 gfx::Size(VideoDetector::kMinUpdateWidth, |
220 VideoDetector::kMinUpdateHeight)); | 191 VideoDetector::kMinUpdateHeight)); |
221 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | 192 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) |
222 detector_->OnWindowPaintScheduled(window.get(), update_region); | 193 detector_->OnWindowPaintScheduled(window.get(), update_region); |
223 EXPECT_EQ(1, observer_->num_invocations()); | 194 EXPECT_EQ(1, observer_->num_invocations()); |
224 EXPECT_EQ(0, observer_->num_fullscreens()); | 195 |
225 EXPECT_EQ(1, observer_->num_not_fullscreens()); | |
226 // Let enough time pass that a second notification should be sent. | 196 // Let enough time pass that a second notification should be sent. |
227 observer_->reset_stats(); | 197 observer_->reset_stats(); |
228 AdvanceTime(base::TimeDelta::FromSeconds( | 198 AdvanceTime(base::TimeDelta::FromSeconds( |
229 static_cast<int64>(VideoDetector::kNotifyIntervalSec + 1))); | 199 static_cast<int64>(VideoDetector::kNotifyIntervalSec + 1))); |
230 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | 200 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) |
231 detector_->OnWindowPaintScheduled(window.get(), update_region); | 201 detector_->OnWindowPaintScheduled(window.get(), update_region); |
232 EXPECT_EQ(1, observer_->num_invocations()); | 202 EXPECT_EQ(1, observer_->num_invocations()); |
233 EXPECT_EQ(0, observer_->num_fullscreens()); | |
234 EXPECT_EQ(1, observer_->num_not_fullscreens()); | |
235 } | |
236 | |
237 // Test that the observer receives a true value when the window is fullscreen. | |
238 TEST_F(VideoDetectorTest, FullscreenWindow) { | |
239 gfx::Rect window_bounds(gfx::Point(), gfx::Size(1024, 768)); | |
240 scoped_ptr<aura::Window> window( | |
241 aura::test::CreateTestWindow(SK_ColorRED, 12345, window_bounds, NULL)); | |
242 window->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN); | |
243 | |
244 gfx::Rect update_region( | |
245 gfx::Point(), | |
246 gfx::Size(VideoDetector::kMinUpdateWidth, | |
247 VideoDetector::kMinUpdateHeight)); | |
248 for (int i = 0; i < VideoDetector::kMinFramesPerSecond; ++i) | |
249 detector_->OnWindowPaintScheduled(window.get(), update_region); | |
250 EXPECT_EQ(1, observer_->num_invocations()); | |
251 EXPECT_EQ(1, observer_->num_fullscreens()); | |
252 EXPECT_EQ(0, observer_->num_not_fullscreens()); | |
253 } | 203 } |
254 | 204 |
255 } // namespace test | 205 } // namespace test |
256 } // namespace ash | 206 } // namespace ash |
OLD | NEW |