1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/display_change_notifier.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/display.h"
9 #include "ui/gfx/display_observer.h"
13 class MockDisplayObserver
: public DisplayObserver
{
19 latest_metrics_change_(DisplayObserver::DISPLAY_METRIC_NONE
)
22 ~MockDisplayObserver() override
{}
24 void OnDisplayAdded(const Display
& display
) override
{ display_added_
++; }
26 void OnDisplayRemoved(const Display
& display
) override
{ display_removed_
++; }
28 void OnDisplayMetricsChanged(const Display
& display
,
29 uint32_t metrics
) override
{
31 latest_metrics_change_
= metrics
;
34 int display_added() const {
35 return display_added_
;
38 int display_removed() const {
39 return display_removed_
;
42 int display_changed() const {
43 return display_changed_
;
46 uint32_t latest_metrics_change() const {
47 return latest_metrics_change_
;
54 uint32_t latest_metrics_change_
;
56 DISALLOW_COPY_AND_ASSIGN(MockDisplayObserver
);
59 TEST(DisplayChangeNotifierTest
, AddObserver_Smoke
) {
60 DisplayChangeNotifier change_notifier
;
61 MockDisplayObserver observer
;
63 change_notifier
.NotifyDisplaysChanged(
64 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
65 EXPECT_EQ(0, observer
.display_added());
67 change_notifier
.AddObserver(&observer
);
68 change_notifier
.NotifyDisplaysChanged(
69 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
70 EXPECT_EQ(1, observer
.display_added());
73 TEST(DisplayChangeNotifierTest
, AddObserver_Null
) {
74 DisplayChangeNotifier change_notifier
;
76 change_notifier
.AddObserver(NULL
);
80 TEST(DisplayChangeNotifier
, RemoveObserver_Smoke
) {
81 DisplayChangeNotifier change_notifier
;
82 MockDisplayObserver observer
;
84 change_notifier
.NotifyDisplaysChanged(
85 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
86 EXPECT_EQ(0, observer
.display_added());
88 change_notifier
.AddObserver(&observer
);
89 change_notifier
.RemoveObserver(&observer
);
91 change_notifier
.NotifyDisplaysChanged(
92 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
93 EXPECT_EQ(0, observer
.display_added());
96 TEST(DisplayChangeNotifierTest
, RemoveObserver_Null
) {
97 DisplayChangeNotifier change_notifier
;
99 change_notifier
.RemoveObserver(NULL
);
103 TEST(DisplayChangeNotifierTest
, RemoveObserver_Unknown
) {
104 DisplayChangeNotifier change_notifier
;
105 MockDisplayObserver observer
;
107 change_notifier
.RemoveObserver(&observer
);
111 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Removed
) {
112 DisplayChangeNotifier change_notifier
;
114 // If the previous display array is empty, no removal.
116 MockDisplayObserver observer
;
117 change_notifier
.AddObserver(&observer
);
119 std::vector
<Display
> old_displays
, new_displays
;
120 new_displays
.push_back(Display());
122 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
123 EXPECT_EQ(0, observer
.display_removed());
125 change_notifier
.RemoveObserver(&observer
);
128 // If the previous and new display array are empty, no removal.
130 MockDisplayObserver observer
;
131 change_notifier
.AddObserver(&observer
);
133 std::vector
<Display
> old_displays
, new_displays
;
135 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
136 EXPECT_EQ(0, observer
.display_removed());
138 change_notifier
.RemoveObserver(&observer
);
141 // If the new display array is empty, there are as many removal as old
144 MockDisplayObserver observer
;
145 change_notifier
.AddObserver(&observer
);
147 std::vector
<Display
> old_displays
, new_displays
;
148 old_displays
.push_back(Display());
149 old_displays
.push_back(Display());
150 old_displays
.push_back(Display());
152 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
153 EXPECT_EQ(3, observer
.display_removed());
155 change_notifier
.RemoveObserver(&observer
);
158 // If displays don't use ids, as long as the new display array has one
159 // element, there are no removals.
161 MockDisplayObserver observer
;
162 change_notifier
.AddObserver(&observer
);
164 std::vector
<Display
> old_displays
, new_displays
;
165 old_displays
.push_back(Display());
166 old_displays
.push_back(Display());
167 old_displays
.push_back(Display());
168 new_displays
.push_back(Display());
170 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
171 EXPECT_EQ(0, observer
.display_removed());
173 change_notifier
.RemoveObserver(&observer
);
176 // If displays use ids (and they are unique), ids not present in the new
177 // display array will be marked as removed.
179 MockDisplayObserver observer
;
180 change_notifier
.AddObserver(&observer
);
182 std::vector
<Display
> old_displays
, new_displays
;
183 old_displays
.push_back(Display(1));
184 old_displays
.push_back(Display(2));
185 old_displays
.push_back(Display(3));
186 new_displays
.push_back(Display(2));
188 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
189 EXPECT_EQ(2, observer
.display_removed());
191 change_notifier
.RemoveObserver(&observer
);
195 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Added
) {
196 DisplayChangeNotifier change_notifier
;
198 // If the new display array is empty, no addition.
200 MockDisplayObserver observer
;
201 change_notifier
.AddObserver(&observer
);
203 std::vector
<Display
> old_displays
, new_displays
;
204 old_displays
.push_back(Display());
206 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
207 EXPECT_EQ(0, observer
.display_added());
209 change_notifier
.RemoveObserver(&observer
);
212 // If the old and new display arrays are empty, no addition.
214 MockDisplayObserver observer
;
215 change_notifier
.AddObserver(&observer
);
217 std::vector
<Display
> old_displays
, new_displays
;
219 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
220 EXPECT_EQ(0, observer
.display_added());
222 change_notifier
.RemoveObserver(&observer
);
225 // If the old display array is empty, there are as many addition as new
228 MockDisplayObserver observer
;
229 change_notifier
.AddObserver(&observer
);
231 std::vector
<Display
> old_displays
, new_displays
;
232 new_displays
.push_back(Display());
233 new_displays
.push_back(Display());
234 new_displays
.push_back(Display());
236 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
237 EXPECT_EQ(3, observer
.display_added());
239 change_notifier
.RemoveObserver(&observer
);
242 // If displays don't use ids, as long as the old display array has one
243 // element, there are no additions.
245 MockDisplayObserver observer
;
246 change_notifier
.AddObserver(&observer
);
248 std::vector
<Display
> old_displays
, new_displays
;
249 old_displays
.push_back(Display());
250 new_displays
.push_back(Display());
251 new_displays
.push_back(Display());
252 new_displays
.push_back(Display());
254 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
255 EXPECT_EQ(0, observer
.display_added());
257 change_notifier
.RemoveObserver(&observer
);
260 // If displays use ids (and they are unique), ids not present in the old
261 // display array will be marked as added.
263 MockDisplayObserver observer
;
264 change_notifier
.AddObserver(&observer
);
266 std::vector
<Display
> old_displays
, new_displays
;
267 old_displays
.push_back(Display(1));
268 new_displays
.push_back(Display(1));
269 new_displays
.push_back(Display(2));
270 new_displays
.push_back(Display(3));
272 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
273 EXPECT_EQ(2, observer
.display_added());
275 change_notifier
.RemoveObserver(&observer
);
279 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Smoke
) {
280 DisplayChangeNotifier change_notifier
;
282 // If the old display array is empty, no change.
284 MockDisplayObserver observer
;
285 change_notifier
.AddObserver(&observer
);
287 std::vector
<Display
> old_displays
, new_displays
;
288 new_displays
.push_back(Display());
290 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
291 EXPECT_EQ(0, observer
.display_changed());
293 change_notifier
.RemoveObserver(&observer
);
296 // If the new display array is empty, no change.
298 MockDisplayObserver observer
;
299 change_notifier
.AddObserver(&observer
);
301 std::vector
<Display
> old_displays
, new_displays
;
302 old_displays
.push_back(Display());
304 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
305 EXPECT_EQ(0, observer
.display_changed());
307 change_notifier
.RemoveObserver(&observer
);
310 // If the old and new display arrays are empty, no change.
312 MockDisplayObserver observer
;
313 change_notifier
.AddObserver(&observer
);
315 std::vector
<Display
> old_displays
, new_displays
;
317 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
318 EXPECT_EQ(0, observer
.display_changed());
320 change_notifier
.RemoveObserver(&observer
);
323 // If there is an intersection between old and new displays but there are no
324 // metrics changes, there is no display change.
326 MockDisplayObserver observer
;
327 change_notifier
.AddObserver(&observer
);
329 std::vector
<Display
> old_displays
, new_displays
;
330 old_displays
.push_back(Display(1));
331 new_displays
.push_back(Display(1));
332 new_displays
.push_back(Display(2));
333 new_displays
.push_back(Display(3));
335 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
336 EXPECT_EQ(0, observer
.display_changed());
338 change_notifier
.RemoveObserver(&observer
);
342 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Bounds
) {
343 DisplayChangeNotifier change_notifier
;
346 MockDisplayObserver observer
;
347 change_notifier
.AddObserver(&observer
);
349 std::vector
<Display
> old_displays
, new_displays
;
350 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
351 new_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
353 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
354 EXPECT_EQ(0, observer
.display_changed());
356 change_notifier
.RemoveObserver(&observer
);
360 MockDisplayObserver observer
;
361 change_notifier
.AddObserver(&observer
);
363 std::vector
<Display
> old_displays
, new_displays
;
364 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
365 new_displays
.push_back(Display(1, Rect(10, 10, 300, 300)));
367 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
368 EXPECT_EQ(1, observer
.display_changed());
369 uint32_t metrics_change
= DisplayObserver::DISPLAY_METRIC_BOUNDS
|
370 DisplayObserver::DISPLAY_METRIC_WORK_AREA
;
371 EXPECT_EQ(metrics_change
, observer
.latest_metrics_change());
373 change_notifier
.RemoveObserver(&observer
);
377 MockDisplayObserver observer
;
378 change_notifier
.AddObserver(&observer
);
380 std::vector
<Display
> old_displays
, new_displays
;
381 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
382 new_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
383 new_displays
[0].set_bounds(Rect(10, 10, 300, 300));
385 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
386 EXPECT_EQ(1, observer
.display_changed());
387 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS
,
388 observer
.latest_metrics_change());
390 change_notifier
.RemoveObserver(&observer
);
394 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Rotation
) {
395 DisplayChangeNotifier change_notifier
;
396 MockDisplayObserver observer
;
397 change_notifier
.AddObserver(&observer
);
399 std::vector
<Display
> old_displays
, new_displays
;
400 old_displays
.push_back(Display(1));
401 old_displays
[0].SetRotationAsDegree(0);
402 new_displays
.push_back(Display(1));
403 new_displays
[0].SetRotationAsDegree(180);
405 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
406 EXPECT_EQ(1, observer
.display_changed());
407 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION
,
408 observer
.latest_metrics_change());
411 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_WorkArea
) {
412 DisplayChangeNotifier change_notifier
;
413 MockDisplayObserver observer
;
414 change_notifier
.AddObserver(&observer
);
416 std::vector
<Display
> old_displays
, new_displays
;
417 old_displays
.push_back(Display(1));
418 old_displays
[0].set_work_area(Rect(0, 0, 200, 200));
419 new_displays
.push_back(Display(1));
420 new_displays
[0].set_work_area(Rect(20, 20, 300, 300));
422 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
423 EXPECT_EQ(1, observer
.display_changed());
424 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA
,
425 observer
.latest_metrics_change());
428 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_DSF
) {
429 DisplayChangeNotifier change_notifier
;
430 MockDisplayObserver observer
;
431 change_notifier
.AddObserver(&observer
);
433 std::vector
<Display
> old_displays
, new_displays
;
434 old_displays
.push_back(Display(1));
435 old_displays
[0].set_device_scale_factor(1.f
);
436 new_displays
.push_back(Display(1));
437 new_displays
[0].set_device_scale_factor(2.f
);
439 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
440 EXPECT_EQ(1, observer
.display_changed());
441 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR
,
442 observer
.latest_metrics_change());
445 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Multi_Displays
) {
446 DisplayChangeNotifier change_notifier
;
447 MockDisplayObserver observer
;
448 change_notifier
.AddObserver(&observer
);
450 std::vector
<Display
> old_displays
, new_displays
;
451 old_displays
.push_back(Display(1));
452 old_displays
.push_back(Display(2));
453 old_displays
.push_back(Display(3));
454 new_displays
.push_back(Display(1));
455 new_displays
.push_back(Display(2));
456 new_displays
.push_back(Display(3));
458 old_displays
[0].set_device_scale_factor(1.f
);
459 new_displays
[0].set_device_scale_factor(2.f
);
461 old_displays
[1].set_bounds(Rect(0, 0, 200, 200));
462 new_displays
[1].set_bounds(Rect(0, 0, 400, 400));
464 old_displays
[2].SetRotationAsDegree(0);
465 new_displays
[2].SetRotationAsDegree(90);
467 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
468 EXPECT_EQ(3, observer
.display_changed());
471 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Multi_Metrics
) {
472 DisplayChangeNotifier change_notifier
;
473 MockDisplayObserver observer
;
474 change_notifier
.AddObserver(&observer
);
476 std::vector
<Display
> old_displays
, new_displays
;
477 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
478 old_displays
[0].set_device_scale_factor(1.f
);
479 old_displays
[0].SetRotationAsDegree(0);
481 new_displays
.push_back(Display(1, Rect(100, 100, 200, 200)));
482 new_displays
[0].set_device_scale_factor(2.f
);
483 new_displays
[0].SetRotationAsDegree(90);
485 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
486 EXPECT_EQ(1, observer
.display_changed());
487 uint32_t metrics
= DisplayObserver::DISPLAY_METRIC_BOUNDS
|
488 DisplayObserver::DISPLAY_METRIC_ROTATION
|
489 DisplayObserver::DISPLAY_METRIC_WORK_AREA
|
490 DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR
;
491 EXPECT_EQ(metrics
, observer
.latest_metrics_change());