Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(936)

Side by Side Diff: ui/gfx/display_change_notifier_unittest.cc

Issue 341983008: Listen to Display reconfiguration and notify DisplayObservers on Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mac_display
Patch Set: review comments + tests Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ui/gfx/display_change_notifier.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/gfx/display.h"
9 #include "ui/gfx/display_observer.h"
10
11 using gfx::Display;
12 using gfx::DisplayObserver;
13
14 #if 0
15 class DisplayChangeNotifierTest : public testing::Test {
16 protected:
17 virtual void SetUp() OVERRIDE {}
18 virtual void TearDown() OVERRIDE {}
19 };
20 #endif // 0
oshima 2014/07/11 19:13:25 remove this?
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
21
22 class MockDisplayObserver : public DisplayObserver {
23 public:
24 MockDisplayObserver()
25 : display_added_(0)
26 , display_removed_(0)
oshima 2014/07/11 19:13:25 move ","s to previous line. Or you can just run "g
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
27 , display_changed_(0)
28 , latest_metrics_change_(DisplayObserver::DISPLAY_METRIC_NONE)
29 {}
30
31 virtual void OnDisplayAdded(const Display& display) OVERRIDE {
32 display_added_++;
33 }
34
35 virtual void OnDisplayRemoved(const Display& display) OVERRIDE {
36 display_removed_++;
37 }
38
39 virtual void OnDisplayMetricsChanged(const Display& display,
40 uint32_t metrics) OVERRIDE {
41 display_changed_++;
42 latest_metrics_change_ = metrics;
43 }
44
45 int display_added() const {
46 return display_added_;
47 }
48
49 int display_removed() const {
50 return display_removed_;
51 }
52
53 int display_changed() const {
54 return display_changed_;
55 }
56
57 uint32_t latest_metrics_change() const {
58 return latest_metrics_change_;
59 }
60
61 protected:
62 int display_added_;
63 int display_removed_;
64 int display_changed_;
65 uint32_t latest_metrics_change_;
66 };
oshima 2014/07/11 19:13:25 DISALLOW_COPY_AND_ASSIGN
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
67
68 namespace {
oshima 2014/07/11 19:13:25 Don't put tests in anonymous namespace because tes
mlamouri (slow - plz ping) 2014/07/13 15:20:40 Done.
69
70 TEST(DisplayChangeNotifierTest, AddObserver_Smoke) {
71 gfx::DisplayChangeNotifier change_notifier;
72 MockDisplayObserver observer;
73
74 change_notifier.NotifyDisplaysChanged(
75 std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
76 EXPECT_EQ(0, observer.display_added());
77
78 change_notifier.AddObserver(&observer);
79 change_notifier.NotifyDisplaysChanged(
80 std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
81 EXPECT_EQ(1, observer.display_added());
82 }
83
84 TEST(DisplayChangeNotifierTest, AddObserver_Null) {
85 gfx::DisplayChangeNotifier change_notifier;
86
87 change_notifier.AddObserver(NULL);
88 // Should not crash.
89 }
90
91 TEST(DisplayChangeNotifierTest, AddObserver_Twice) {
92 gfx::DisplayChangeNotifier change_notifier;
93 MockDisplayObserver observer;
94
95 change_notifier.AddObserver(&observer);
96 ASSERT_DEATH(change_notifier.AddObserver(&observer),
97 "Observers can only be added once!");
oshima 2014/07/11 19:13:25 Null/Twice/Unknowns are covered by ObserverList's
mlamouri (slow - plz ping) 2014/07/13 15:20:40 ObserverList's tests should indeed cover that but
98 }
99
100 TEST(DisplayChangeNotifier, RemoveObserver_Smoke) {
101 gfx::DisplayChangeNotifier change_notifier;
102 MockDisplayObserver observer;
103
104 change_notifier.NotifyDisplaysChanged(
105 std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
106 EXPECT_EQ(0, observer.display_added());
107
108 change_notifier.AddObserver(&observer);
109 change_notifier.RemoveObserver(&observer);
110
111 change_notifier.NotifyDisplaysChanged(
112 std::vector<gfx::Display>(), std::vector<Display>(1, gfx::Display()));
113 EXPECT_EQ(0, observer.display_added());
114 }
115
116 TEST(DisplayChangeNotifierTest, RemoveObserver_Null) {
117 gfx::DisplayChangeNotifier change_notifier;
118
119 change_notifier.RemoveObserver(NULL);
120 // Should not crash.
121 }
122
123 TEST(DisplayChangeNotifierTest, RemoveObserver_Unknown) {
124 gfx::DisplayChangeNotifier change_notifier;
125 MockDisplayObserver observer;
126
127 change_notifier.RemoveObserver(&observer);
128 // Should not crash.
129 }
130
131 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Removed) {
132 gfx::DisplayChangeNotifier change_notifier;
133
134 // If the previous display array is empty, no removal.
135 {
136 MockDisplayObserver observer;
137 change_notifier.AddObserver(&observer);
138
139 std::vector<Display> old_displays, new_displays;
140 new_displays.push_back(gfx::Display());
141
142 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
143 EXPECT_EQ(0, observer.display_removed());
144
145 change_notifier.RemoveObserver(&observer);
146 }
147
148 // If the previous and new display array are empty, no removal.
149 {
150 MockDisplayObserver observer;
151 change_notifier.AddObserver(&observer);
152
153 std::vector<Display> old_displays, new_displays;
154
155 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
156 EXPECT_EQ(0, observer.display_removed());
157
158 change_notifier.RemoveObserver(&observer);
159 }
160
161 // If the new display array is empty, there are as many removal as old
162 // displays.
163 {
164 MockDisplayObserver observer;
165 change_notifier.AddObserver(&observer);
166
167 std::vector<Display> old_displays, new_displays;
168 old_displays.push_back(Display());
169 old_displays.push_back(Display());
170 old_displays.push_back(Display());
171
172 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
173 EXPECT_EQ(3, observer.display_removed());
174
175 change_notifier.RemoveObserver(&observer);
176 }
177
178 // If displays don't use ids, as long as the new display array has one
179 // element, there are no removals.
180 {
181 MockDisplayObserver observer;
182 change_notifier.AddObserver(&observer);
183
184 std::vector<Display> old_displays, new_displays;
185 old_displays.push_back(Display());
186 old_displays.push_back(Display());
187 old_displays.push_back(Display());
188 new_displays.push_back(Display());
189
190 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
191 EXPECT_EQ(0, observer.display_removed());
192
193 change_notifier.RemoveObserver(&observer);
194 }
195
196 // If displays use ids (and they are unique), ids not present in the new
197 // display array will be marked as removed.
198 {
199 MockDisplayObserver observer;
200 change_notifier.AddObserver(&observer);
201
202 std::vector<Display> old_displays, new_displays;
203 old_displays.push_back(Display(1));
204 old_displays.push_back(Display(2));
205 old_displays.push_back(Display(3));
206 new_displays.push_back(Display(2));
207
208 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
209 EXPECT_EQ(2, observer.display_removed());
210
211 change_notifier.RemoveObserver(&observer);
212 }
213 }
214
215 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Added) {
216 gfx::DisplayChangeNotifier change_notifier;
217
218 // If the new display array is empty, no addition.
219 {
220 MockDisplayObserver observer;
221 change_notifier.AddObserver(&observer);
222
223 std::vector<Display> old_displays, new_displays;
224 old_displays.push_back(gfx::Display());
225
226 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
227 EXPECT_EQ(0, observer.display_added());
228
229 change_notifier.RemoveObserver(&observer);
230 }
231
232 // If the old and new display arrays are empty, no addition.
233 {
234 MockDisplayObserver observer;
235 change_notifier.AddObserver(&observer);
236
237 std::vector<Display> old_displays, new_displays;
238
239 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
240 EXPECT_EQ(0, observer.display_added());
241
242 change_notifier.RemoveObserver(&observer);
243 }
244
245 // If the old display array is empty, there are as many addition as new
246 // displays.
247 {
248 MockDisplayObserver observer;
249 change_notifier.AddObserver(&observer);
250
251 std::vector<Display> old_displays, new_displays;
252 new_displays.push_back(Display());
253 new_displays.push_back(Display());
254 new_displays.push_back(Display());
255
256 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
257 EXPECT_EQ(3, observer.display_added());
258
259 change_notifier.RemoveObserver(&observer);
260 }
261
262 // If displays don't use ids, as long as the old display array has one
263 // element, there are no additions.
264 {
265 MockDisplayObserver observer;
266 change_notifier.AddObserver(&observer);
267
268 std::vector<Display> old_displays, new_displays;
269 old_displays.push_back(Display());
270 new_displays.push_back(Display());
271 new_displays.push_back(Display());
272 new_displays.push_back(Display());
273
274 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
275 EXPECT_EQ(0, observer.display_added());
276
277 change_notifier.RemoveObserver(&observer);
278 }
279
280 // If displays use ids (and they are unique), ids not present in the old
281 // display array will be marked as added.
282 {
283 MockDisplayObserver observer;
284 change_notifier.AddObserver(&observer);
285
286 std::vector<Display> old_displays, new_displays;
287 old_displays.push_back(Display(1));
288 new_displays.push_back(Display(1));
289 new_displays.push_back(Display(2));
290 new_displays.push_back(Display(3));
291
292 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
293 EXPECT_EQ(2, observer.display_added());
294
295 change_notifier.RemoveObserver(&observer);
296 }
297 }
298
299 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Smoke) {
300 gfx::DisplayChangeNotifier change_notifier;
301
302 // If the old display array is empty, no change.
303 {
304 MockDisplayObserver observer;
305 change_notifier.AddObserver(&observer);
306
307 std::vector<Display> old_displays, new_displays;
308 new_displays.push_back(gfx::Display());
309
310 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
311 EXPECT_EQ(0, observer.display_changed());
312
313 change_notifier.RemoveObserver(&observer);
314 }
315
316 // If the new display array is empty, no change.
317 {
318 MockDisplayObserver observer;
319 change_notifier.AddObserver(&observer);
320
321 std::vector<Display> old_displays, new_displays;
322 old_displays.push_back(gfx::Display());
323
324 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
325 EXPECT_EQ(0, observer.display_changed());
326
327 change_notifier.RemoveObserver(&observer);
328 }
329
330 // If the old and new display arrays are empty, no change.
331 {
332 MockDisplayObserver observer;
333 change_notifier.AddObserver(&observer);
334
335 std::vector<Display> old_displays, new_displays;
336
337 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
338 EXPECT_EQ(0, observer.display_changed());
339
340 change_notifier.RemoveObserver(&observer);
341 }
342
343 // If there is an intersection between old and new displays but there are no
344 // metrics changes, there is no display change.
345 {
346 MockDisplayObserver observer;
347 change_notifier.AddObserver(&observer);
348
349 std::vector<Display> old_displays, new_displays;
350 old_displays.push_back(Display(1));
351 new_displays.push_back(Display(1));
352 new_displays.push_back(Display(2));
353 new_displays.push_back(Display(3));
354
355 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
356 EXPECT_EQ(0, observer.display_changed());
357
358 change_notifier.RemoveObserver(&observer);
359 }
360 }
361
362 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Bounds) {
363 gfx::DisplayChangeNotifier change_notifier;
364
365 {
366 MockDisplayObserver observer;
367 change_notifier.AddObserver(&observer);
368
369 std::vector<Display> old_displays, new_displays;
370 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
371 new_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
372
373 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
374 EXPECT_EQ(0, observer.display_changed());
375
376 change_notifier.RemoveObserver(&observer);
377 }
378
379 {
380 MockDisplayObserver observer;
381 change_notifier.AddObserver(&observer);
382
383 std::vector<Display> old_displays, new_displays;
384 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
385 new_displays.push_back(Display(1, gfx::Rect(10, 10, 300, 300)));
386
387 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
388 EXPECT_EQ(1, observer.display_changed());
389 uint32_t metrics_change = DisplayObserver::DISPLAY_METRIC_BOUNDS |
390 DisplayObserver::DISPLAY_METRIC_WORK_AREA;
391 EXPECT_EQ(metrics_change, observer.latest_metrics_change());
392
393 change_notifier.RemoveObserver(&observer);
394 }
395
396 {
397 MockDisplayObserver observer;
398 change_notifier.AddObserver(&observer);
399
400 std::vector<Display> old_displays, new_displays;
401 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
402 new_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
403 new_displays[0].set_bounds(gfx::Rect(10, 10, 300, 300));
404
405 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
406 EXPECT_EQ(1, observer.display_changed());
407 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS,
408 observer.latest_metrics_change());
409
410 change_notifier.RemoveObserver(&observer);
411 }
412 }
413
414 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Rotation) {
415 gfx::DisplayChangeNotifier change_notifier;
416 MockDisplayObserver observer;
417 change_notifier.AddObserver(&observer);
418
419 std::vector<Display> old_displays, new_displays;
420 old_displays.push_back(Display(1));
421 old_displays[0].SetRotationAsDegree(0);
422 new_displays.push_back(Display(1));
423 new_displays[0].SetRotationAsDegree(180);
424
425 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
426 EXPECT_EQ(1, observer.display_changed());
427 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION,
428 observer.latest_metrics_change());
429 }
430
431 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_WorkArea) {
432 gfx::DisplayChangeNotifier change_notifier;
433 MockDisplayObserver observer;
434 change_notifier.AddObserver(&observer);
435
436 std::vector<Display> old_displays, new_displays;
437 old_displays.push_back(Display(1));
438 old_displays[0].set_work_area(gfx::Rect(0, 0, 200, 200));
439 new_displays.push_back(Display(1));
440 new_displays[0].set_work_area(gfx::Rect(20, 20, 300, 300));
441
442 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
443 EXPECT_EQ(1, observer.display_changed());
444 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA,
445 observer.latest_metrics_change());
446 }
447
448 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_DSF) {
449 gfx::DisplayChangeNotifier change_notifier;
450 MockDisplayObserver observer;
451 change_notifier.AddObserver(&observer);
452
453 std::vector<Display> old_displays, new_displays;
454 old_displays.push_back(Display(1));
455 old_displays[0].set_device_scale_factor(1.f);
456 new_displays.push_back(Display(1));
457 new_displays[0].set_device_scale_factor(2.f);
458
459 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
460 EXPECT_EQ(1, observer.display_changed());
461 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR,
462 observer.latest_metrics_change());
463 }
464
465 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Displays) {
466 gfx::DisplayChangeNotifier change_notifier;
467 MockDisplayObserver observer;
468 change_notifier.AddObserver(&observer);
469
470 std::vector<Display> old_displays, new_displays;
471 old_displays.push_back(Display(1));
472 old_displays.push_back(Display(2));
473 old_displays.push_back(Display(3));
474 new_displays.push_back(Display(1));
475 new_displays.push_back(Display(2));
476 new_displays.push_back(Display(3));
477
478 old_displays[0].set_device_scale_factor(1.f);
479 new_displays[0].set_device_scale_factor(2.f);
480
481 old_displays[1].set_bounds(gfx::Rect(0, 0, 200, 200));
482 new_displays[1].set_bounds(gfx::Rect(0, 0, 400, 400));
483
484 old_displays[2].SetRotationAsDegree(0);
485 new_displays[2].SetRotationAsDegree(90);
486
487 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
488 EXPECT_EQ(3, observer.display_changed());
489 }
490
491 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Metrics) {
492 gfx::DisplayChangeNotifier change_notifier;
493 MockDisplayObserver observer;
494 change_notifier.AddObserver(&observer);
495
496 std::vector<Display> old_displays, new_displays;
497 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
498 old_displays[0].set_device_scale_factor(1.f);
499 old_displays[0].SetRotationAsDegree(0);
500
501 new_displays.push_back(Display(1, gfx::Rect(100, 100, 200, 200)));
502 new_displays[0].set_device_scale_factor(2.f);
503 new_displays[0].SetRotationAsDegree(90);
504
505 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
506 EXPECT_EQ(1, observer.display_changed());
507 uint32_t metrics = DisplayObserver::DISPLAY_METRIC_BOUNDS |
508 DisplayObserver::DISPLAY_METRIC_ROTATION |
509 DisplayObserver::DISPLAY_METRIC_WORK_AREA |
510 DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
511 EXPECT_EQ(metrics, observer.latest_metrics_change());
512 }
513
514 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698