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 virtual ~MockDisplayObserver() { }
24 virtual void OnDisplayAdded(const Display
& display
) OVERRIDE
{
28 virtual void OnDisplayRemoved(const Display
& display
) OVERRIDE
{
32 virtual void OnDisplayMetricsChanged(const Display
& display
,
33 uint32_t metrics
) OVERRIDE
{
35 latest_metrics_change_
= metrics
;
38 int display_added() const {
39 return display_added_
;
42 int display_removed() const {
43 return display_removed_
;
46 int display_changed() const {
47 return display_changed_
;
50 uint32_t latest_metrics_change() const {
51 return latest_metrics_change_
;
58 uint32_t latest_metrics_change_
;
60 DISALLOW_COPY_AND_ASSIGN(MockDisplayObserver
);
63 TEST(DisplayChangeNotifierTest
, AddObserver_Smoke
) {
64 DisplayChangeNotifier change_notifier
;
65 MockDisplayObserver observer
;
67 change_notifier
.NotifyDisplaysChanged(
68 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
69 EXPECT_EQ(0, observer
.display_added());
71 change_notifier
.AddObserver(&observer
);
72 change_notifier
.NotifyDisplaysChanged(
73 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
74 EXPECT_EQ(1, observer
.display_added());
77 TEST(DisplayChangeNotifierTest
, AddObserver_Null
) {
78 DisplayChangeNotifier change_notifier
;
80 change_notifier
.AddObserver(NULL
);
84 TEST(DisplayChangeNotifier
, RemoveObserver_Smoke
) {
85 DisplayChangeNotifier change_notifier
;
86 MockDisplayObserver observer
;
88 change_notifier
.NotifyDisplaysChanged(
89 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
90 EXPECT_EQ(0, observer
.display_added());
92 change_notifier
.AddObserver(&observer
);
93 change_notifier
.RemoveObserver(&observer
);
95 change_notifier
.NotifyDisplaysChanged(
96 std::vector
<Display
>(), std::vector
<Display
>(1, Display()));
97 EXPECT_EQ(0, observer
.display_added());
100 TEST(DisplayChangeNotifierTest
, RemoveObserver_Null
) {
101 DisplayChangeNotifier change_notifier
;
103 change_notifier
.RemoveObserver(NULL
);
107 TEST(DisplayChangeNotifierTest
, RemoveObserver_Unknown
) {
108 DisplayChangeNotifier change_notifier
;
109 MockDisplayObserver observer
;
111 change_notifier
.RemoveObserver(&observer
);
115 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Removed
) {
116 DisplayChangeNotifier change_notifier
;
118 // If the previous display array is empty, no removal.
120 MockDisplayObserver observer
;
121 change_notifier
.AddObserver(&observer
);
123 std::vector
<Display
> old_displays
, new_displays
;
124 new_displays
.push_back(Display());
126 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
127 EXPECT_EQ(0, observer
.display_removed());
129 change_notifier
.RemoveObserver(&observer
);
132 // If the previous and new display array are empty, no removal.
134 MockDisplayObserver observer
;
135 change_notifier
.AddObserver(&observer
);
137 std::vector
<Display
> old_displays
, new_displays
;
139 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
140 EXPECT_EQ(0, observer
.display_removed());
142 change_notifier
.RemoveObserver(&observer
);
145 // If the new display array is empty, there are as many removal as old
148 MockDisplayObserver observer
;
149 change_notifier
.AddObserver(&observer
);
151 std::vector
<Display
> old_displays
, new_displays
;
152 old_displays
.push_back(Display());
153 old_displays
.push_back(Display());
154 old_displays
.push_back(Display());
156 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
157 EXPECT_EQ(3, observer
.display_removed());
159 change_notifier
.RemoveObserver(&observer
);
162 // If displays don't use ids, as long as the new display array has one
163 // element, there are no removals.
165 MockDisplayObserver observer
;
166 change_notifier
.AddObserver(&observer
);
168 std::vector
<Display
> old_displays
, new_displays
;
169 old_displays
.push_back(Display());
170 old_displays
.push_back(Display());
171 old_displays
.push_back(Display());
172 new_displays
.push_back(Display());
174 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
175 EXPECT_EQ(0, observer
.display_removed());
177 change_notifier
.RemoveObserver(&observer
);
180 // If displays use ids (and they are unique), ids not present in the new
181 // display array will be marked as removed.
183 MockDisplayObserver observer
;
184 change_notifier
.AddObserver(&observer
);
186 std::vector
<Display
> old_displays
, new_displays
;
187 old_displays
.push_back(Display(1));
188 old_displays
.push_back(Display(2));
189 old_displays
.push_back(Display(3));
190 new_displays
.push_back(Display(2));
192 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
193 EXPECT_EQ(2, observer
.display_removed());
195 change_notifier
.RemoveObserver(&observer
);
199 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Added
) {
200 DisplayChangeNotifier change_notifier
;
202 // If the new display array is empty, no addition.
204 MockDisplayObserver observer
;
205 change_notifier
.AddObserver(&observer
);
207 std::vector
<Display
> old_displays
, new_displays
;
208 old_displays
.push_back(Display());
210 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
211 EXPECT_EQ(0, observer
.display_added());
213 change_notifier
.RemoveObserver(&observer
);
216 // If the old and new display arrays are empty, no addition.
218 MockDisplayObserver observer
;
219 change_notifier
.AddObserver(&observer
);
221 std::vector
<Display
> old_displays
, new_displays
;
223 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
224 EXPECT_EQ(0, observer
.display_added());
226 change_notifier
.RemoveObserver(&observer
);
229 // If the old display array is empty, there are as many addition as new
232 MockDisplayObserver observer
;
233 change_notifier
.AddObserver(&observer
);
235 std::vector
<Display
> old_displays
, new_displays
;
236 new_displays
.push_back(Display());
237 new_displays
.push_back(Display());
238 new_displays
.push_back(Display());
240 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
241 EXPECT_EQ(3, observer
.display_added());
243 change_notifier
.RemoveObserver(&observer
);
246 // If displays don't use ids, as long as the old display array has one
247 // element, there are no additions.
249 MockDisplayObserver observer
;
250 change_notifier
.AddObserver(&observer
);
252 std::vector
<Display
> old_displays
, new_displays
;
253 old_displays
.push_back(Display());
254 new_displays
.push_back(Display());
255 new_displays
.push_back(Display());
256 new_displays
.push_back(Display());
258 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
259 EXPECT_EQ(0, observer
.display_added());
261 change_notifier
.RemoveObserver(&observer
);
264 // If displays use ids (and they are unique), ids not present in the old
265 // display array will be marked as added.
267 MockDisplayObserver observer
;
268 change_notifier
.AddObserver(&observer
);
270 std::vector
<Display
> old_displays
, new_displays
;
271 old_displays
.push_back(Display(1));
272 new_displays
.push_back(Display(1));
273 new_displays
.push_back(Display(2));
274 new_displays
.push_back(Display(3));
276 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
277 EXPECT_EQ(2, observer
.display_added());
279 change_notifier
.RemoveObserver(&observer
);
283 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Smoke
) {
284 DisplayChangeNotifier change_notifier
;
286 // If the old display array is empty, no change.
288 MockDisplayObserver observer
;
289 change_notifier
.AddObserver(&observer
);
291 std::vector
<Display
> old_displays
, new_displays
;
292 new_displays
.push_back(Display());
294 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
295 EXPECT_EQ(0, observer
.display_changed());
297 change_notifier
.RemoveObserver(&observer
);
300 // If the new display array is empty, no change.
302 MockDisplayObserver observer
;
303 change_notifier
.AddObserver(&observer
);
305 std::vector
<Display
> old_displays
, new_displays
;
306 old_displays
.push_back(Display());
308 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
309 EXPECT_EQ(0, observer
.display_changed());
311 change_notifier
.RemoveObserver(&observer
);
314 // If the old and new display arrays are empty, no change.
316 MockDisplayObserver observer
;
317 change_notifier
.AddObserver(&observer
);
319 std::vector
<Display
> old_displays
, new_displays
;
321 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
322 EXPECT_EQ(0, observer
.display_changed());
324 change_notifier
.RemoveObserver(&observer
);
327 // If there is an intersection between old and new displays but there are no
328 // metrics changes, there is no display change.
330 MockDisplayObserver observer
;
331 change_notifier
.AddObserver(&observer
);
333 std::vector
<Display
> old_displays
, new_displays
;
334 old_displays
.push_back(Display(1));
335 new_displays
.push_back(Display(1));
336 new_displays
.push_back(Display(2));
337 new_displays
.push_back(Display(3));
339 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
340 EXPECT_EQ(0, observer
.display_changed());
342 change_notifier
.RemoveObserver(&observer
);
346 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Bounds
) {
347 DisplayChangeNotifier change_notifier
;
350 MockDisplayObserver observer
;
351 change_notifier
.AddObserver(&observer
);
353 std::vector
<Display
> old_displays
, new_displays
;
354 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
355 new_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
357 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
358 EXPECT_EQ(0, observer
.display_changed());
360 change_notifier
.RemoveObserver(&observer
);
364 MockDisplayObserver observer
;
365 change_notifier
.AddObserver(&observer
);
367 std::vector
<Display
> old_displays
, new_displays
;
368 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
369 new_displays
.push_back(Display(1, Rect(10, 10, 300, 300)));
371 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
372 EXPECT_EQ(1, observer
.display_changed());
373 uint32_t metrics_change
= DisplayObserver::DISPLAY_METRIC_BOUNDS
|
374 DisplayObserver::DISPLAY_METRIC_WORK_AREA
;
375 EXPECT_EQ(metrics_change
, observer
.latest_metrics_change());
377 change_notifier
.RemoveObserver(&observer
);
381 MockDisplayObserver observer
;
382 change_notifier
.AddObserver(&observer
);
384 std::vector
<Display
> old_displays
, new_displays
;
385 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
386 new_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
387 new_displays
[0].set_bounds(Rect(10, 10, 300, 300));
389 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
390 EXPECT_EQ(1, observer
.display_changed());
391 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS
,
392 observer
.latest_metrics_change());
394 change_notifier
.RemoveObserver(&observer
);
398 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Rotation
) {
399 DisplayChangeNotifier change_notifier
;
400 MockDisplayObserver observer
;
401 change_notifier
.AddObserver(&observer
);
403 std::vector
<Display
> old_displays
, new_displays
;
404 old_displays
.push_back(Display(1));
405 old_displays
[0].SetRotationAsDegree(0);
406 new_displays
.push_back(Display(1));
407 new_displays
[0].SetRotationAsDegree(180);
409 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
410 EXPECT_EQ(1, observer
.display_changed());
411 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION
,
412 observer
.latest_metrics_change());
415 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_WorkArea
) {
416 DisplayChangeNotifier change_notifier
;
417 MockDisplayObserver observer
;
418 change_notifier
.AddObserver(&observer
);
420 std::vector
<Display
> old_displays
, new_displays
;
421 old_displays
.push_back(Display(1));
422 old_displays
[0].set_work_area(Rect(0, 0, 200, 200));
423 new_displays
.push_back(Display(1));
424 new_displays
[0].set_work_area(Rect(20, 20, 300, 300));
426 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
427 EXPECT_EQ(1, observer
.display_changed());
428 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA
,
429 observer
.latest_metrics_change());
432 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_DSF
) {
433 DisplayChangeNotifier change_notifier
;
434 MockDisplayObserver observer
;
435 change_notifier
.AddObserver(&observer
);
437 std::vector
<Display
> old_displays
, new_displays
;
438 old_displays
.push_back(Display(1));
439 old_displays
[0].set_device_scale_factor(1.f
);
440 new_displays
.push_back(Display(1));
441 new_displays
[0].set_device_scale_factor(2.f
);
443 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
444 EXPECT_EQ(1, observer
.display_changed());
445 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR
,
446 observer
.latest_metrics_change());
449 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Multi_Displays
) {
450 DisplayChangeNotifier change_notifier
;
451 MockDisplayObserver observer
;
452 change_notifier
.AddObserver(&observer
);
454 std::vector
<Display
> old_displays
, new_displays
;
455 old_displays
.push_back(Display(1));
456 old_displays
.push_back(Display(2));
457 old_displays
.push_back(Display(3));
458 new_displays
.push_back(Display(1));
459 new_displays
.push_back(Display(2));
460 new_displays
.push_back(Display(3));
462 old_displays
[0].set_device_scale_factor(1.f
);
463 new_displays
[0].set_device_scale_factor(2.f
);
465 old_displays
[1].set_bounds(Rect(0, 0, 200, 200));
466 new_displays
[1].set_bounds(Rect(0, 0, 400, 400));
468 old_displays
[2].SetRotationAsDegree(0);
469 new_displays
[2].SetRotationAsDegree(90);
471 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
472 EXPECT_EQ(3, observer
.display_changed());
475 TEST(DisplayChangeNotifierTest
, NotifyDisplaysChanged_Changed_Multi_Metrics
) {
476 DisplayChangeNotifier change_notifier
;
477 MockDisplayObserver observer
;
478 change_notifier
.AddObserver(&observer
);
480 std::vector
<Display
> old_displays
, new_displays
;
481 old_displays
.push_back(Display(1, Rect(0, 0, 200, 200)));
482 old_displays
[0].set_device_scale_factor(1.f
);
483 old_displays
[0].SetRotationAsDegree(0);
485 new_displays
.push_back(Display(1, Rect(100, 100, 200, 200)));
486 new_displays
[0].set_device_scale_factor(2.f
);
487 new_displays
[0].SetRotationAsDegree(90);
489 change_notifier
.NotifyDisplaysChanged(old_displays
, new_displays
);
490 EXPECT_EQ(1, observer
.display_changed());
491 uint32_t metrics
= DisplayObserver::DISPLAY_METRIC_BOUNDS
|
492 DisplayObserver::DISPLAY_METRIC_ROTATION
|
493 DisplayObserver::DISPLAY_METRIC_WORK_AREA
|
494 DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR
;
495 EXPECT_EQ(metrics
, observer
.latest_metrics_change());