Implement FromDict/IntoDict for BackgroundTracingConfig.
[chromium-blink-merge.git] / ui / gfx / display_change_notifier_unittest.cc
blob9c1dda2fdcae1fe0672b564a1b3e9161f6e6b295
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"
11 namespace gfx {
13 class MockDisplayObserver : public DisplayObserver {
14 public:
15 MockDisplayObserver()
16 : display_added_(0),
17 display_removed_(0),
18 display_changed_(0),
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 {
30 display_changed_++;
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_;
50 protected:
51 int display_added_;
52 int display_removed_;
53 int display_changed_;
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(DisplayChangeNotifier, RemoveObserver_Smoke) {
74 DisplayChangeNotifier change_notifier;
75 MockDisplayObserver observer;
77 change_notifier.NotifyDisplaysChanged(
78 std::vector<Display>(), std::vector<Display>(1, Display()));
79 EXPECT_EQ(0, observer.display_added());
81 change_notifier.AddObserver(&observer);
82 change_notifier.RemoveObserver(&observer);
84 change_notifier.NotifyDisplaysChanged(
85 std::vector<Display>(), std::vector<Display>(1, Display()));
86 EXPECT_EQ(0, observer.display_added());
89 TEST(DisplayChangeNotifierTest, RemoveObserver_Unknown) {
90 DisplayChangeNotifier change_notifier;
91 MockDisplayObserver observer;
93 change_notifier.RemoveObserver(&observer);
94 // Should not crash.
97 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Removed) {
98 DisplayChangeNotifier change_notifier;
100 // If the previous display array is empty, no removal.
102 MockDisplayObserver observer;
103 change_notifier.AddObserver(&observer);
105 std::vector<Display> old_displays, new_displays;
106 new_displays.push_back(Display());
108 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
109 EXPECT_EQ(0, observer.display_removed());
111 change_notifier.RemoveObserver(&observer);
114 // If the previous and new display array are empty, no removal.
116 MockDisplayObserver observer;
117 change_notifier.AddObserver(&observer);
119 std::vector<Display> old_displays, new_displays;
121 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
122 EXPECT_EQ(0, observer.display_removed());
124 change_notifier.RemoveObserver(&observer);
127 // If the new display array is empty, there are as many removal as old
128 // displays.
130 MockDisplayObserver observer;
131 change_notifier.AddObserver(&observer);
133 std::vector<Display> old_displays, new_displays;
134 old_displays.push_back(Display());
135 old_displays.push_back(Display());
136 old_displays.push_back(Display());
138 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
139 EXPECT_EQ(3, observer.display_removed());
141 change_notifier.RemoveObserver(&observer);
144 // If displays don't use ids, as long as the new display array has one
145 // element, there are no removals.
147 MockDisplayObserver observer;
148 change_notifier.AddObserver(&observer);
150 std::vector<Display> old_displays, new_displays;
151 old_displays.push_back(Display());
152 old_displays.push_back(Display());
153 old_displays.push_back(Display());
154 new_displays.push_back(Display());
156 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
157 EXPECT_EQ(0, observer.display_removed());
159 change_notifier.RemoveObserver(&observer);
162 // If displays use ids (and they are unique), ids not present in the new
163 // display array will be marked as removed.
165 MockDisplayObserver observer;
166 change_notifier.AddObserver(&observer);
168 std::vector<Display> old_displays, new_displays;
169 old_displays.push_back(Display(1));
170 old_displays.push_back(Display(2));
171 old_displays.push_back(Display(3));
172 new_displays.push_back(Display(2));
174 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
175 EXPECT_EQ(2, observer.display_removed());
177 change_notifier.RemoveObserver(&observer);
181 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Added) {
182 DisplayChangeNotifier change_notifier;
184 // If the new display array is empty, no addition.
186 MockDisplayObserver observer;
187 change_notifier.AddObserver(&observer);
189 std::vector<Display> old_displays, new_displays;
190 old_displays.push_back(Display());
192 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
193 EXPECT_EQ(0, observer.display_added());
195 change_notifier.RemoveObserver(&observer);
198 // If the old and new display arrays are empty, no addition.
200 MockDisplayObserver observer;
201 change_notifier.AddObserver(&observer);
203 std::vector<Display> old_displays, new_displays;
205 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
206 EXPECT_EQ(0, observer.display_added());
208 change_notifier.RemoveObserver(&observer);
211 // If the old display array is empty, there are as many addition as new
212 // displays.
214 MockDisplayObserver observer;
215 change_notifier.AddObserver(&observer);
217 std::vector<Display> old_displays, new_displays;
218 new_displays.push_back(Display());
219 new_displays.push_back(Display());
220 new_displays.push_back(Display());
222 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
223 EXPECT_EQ(3, observer.display_added());
225 change_notifier.RemoveObserver(&observer);
228 // If displays don't use ids, as long as the old display array has one
229 // element, there are no additions.
231 MockDisplayObserver observer;
232 change_notifier.AddObserver(&observer);
234 std::vector<Display> old_displays, new_displays;
235 old_displays.push_back(Display());
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(0, observer.display_added());
243 change_notifier.RemoveObserver(&observer);
246 // If displays use ids (and they are unique), ids not present in the old
247 // display array will be marked as added.
249 MockDisplayObserver observer;
250 change_notifier.AddObserver(&observer);
252 std::vector<Display> old_displays, new_displays;
253 old_displays.push_back(Display(1));
254 new_displays.push_back(Display(1));
255 new_displays.push_back(Display(2));
256 new_displays.push_back(Display(3));
258 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
259 EXPECT_EQ(2, observer.display_added());
261 change_notifier.RemoveObserver(&observer);
265 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Smoke) {
266 DisplayChangeNotifier change_notifier;
268 // If the old display array is empty, no change.
270 MockDisplayObserver observer;
271 change_notifier.AddObserver(&observer);
273 std::vector<Display> old_displays, new_displays;
274 new_displays.push_back(Display());
276 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
277 EXPECT_EQ(0, observer.display_changed());
279 change_notifier.RemoveObserver(&observer);
282 // If the new display array is empty, no change.
284 MockDisplayObserver observer;
285 change_notifier.AddObserver(&observer);
287 std::vector<Display> old_displays, new_displays;
288 old_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 old and new display arrays are empty, no change.
298 MockDisplayObserver observer;
299 change_notifier.AddObserver(&observer);
301 std::vector<Display> old_displays, new_displays;
303 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
304 EXPECT_EQ(0, observer.display_changed());
306 change_notifier.RemoveObserver(&observer);
309 // If there is an intersection between old and new displays but there are no
310 // metrics changes, there is no display change.
312 MockDisplayObserver observer;
313 change_notifier.AddObserver(&observer);
315 std::vector<Display> old_displays, new_displays;
316 old_displays.push_back(Display(1));
317 new_displays.push_back(Display(1));
318 new_displays.push_back(Display(2));
319 new_displays.push_back(Display(3));
321 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
322 EXPECT_EQ(0, observer.display_changed());
324 change_notifier.RemoveObserver(&observer);
328 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Bounds) {
329 DisplayChangeNotifier change_notifier;
332 MockDisplayObserver observer;
333 change_notifier.AddObserver(&observer);
335 std::vector<Display> old_displays, new_displays;
336 old_displays.push_back(Display(1, Rect(0, 0, 200, 200)));
337 new_displays.push_back(Display(1, Rect(0, 0, 200, 200)));
339 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
340 EXPECT_EQ(0, observer.display_changed());
342 change_notifier.RemoveObserver(&observer);
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(10, 10, 300, 300)));
353 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
354 EXPECT_EQ(1, observer.display_changed());
355 uint32_t metrics_change = DisplayObserver::DISPLAY_METRIC_BOUNDS |
356 DisplayObserver::DISPLAY_METRIC_WORK_AREA;
357 EXPECT_EQ(metrics_change, observer.latest_metrics_change());
359 change_notifier.RemoveObserver(&observer);
363 MockDisplayObserver observer;
364 change_notifier.AddObserver(&observer);
366 std::vector<Display> old_displays, new_displays;
367 old_displays.push_back(Display(1, Rect(0, 0, 200, 200)));
368 new_displays.push_back(Display(1, Rect(0, 0, 200, 200)));
369 new_displays[0].set_bounds(Rect(10, 10, 300, 300));
371 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
372 EXPECT_EQ(1, observer.display_changed());
373 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS,
374 observer.latest_metrics_change());
376 change_notifier.RemoveObserver(&observer);
380 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Rotation) {
381 DisplayChangeNotifier change_notifier;
382 MockDisplayObserver observer;
383 change_notifier.AddObserver(&observer);
385 std::vector<Display> old_displays, new_displays;
386 old_displays.push_back(Display(1));
387 old_displays[0].SetRotationAsDegree(0);
388 new_displays.push_back(Display(1));
389 new_displays[0].SetRotationAsDegree(180);
391 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
392 EXPECT_EQ(1, observer.display_changed());
393 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION,
394 observer.latest_metrics_change());
397 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_WorkArea) {
398 DisplayChangeNotifier change_notifier;
399 MockDisplayObserver observer;
400 change_notifier.AddObserver(&observer);
402 std::vector<Display> old_displays, new_displays;
403 old_displays.push_back(Display(1));
404 old_displays[0].set_work_area(Rect(0, 0, 200, 200));
405 new_displays.push_back(Display(1));
406 new_displays[0].set_work_area(Rect(20, 20, 300, 300));
408 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
409 EXPECT_EQ(1, observer.display_changed());
410 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA,
411 observer.latest_metrics_change());
414 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_DSF) {
415 DisplayChangeNotifier change_notifier;
416 MockDisplayObserver observer;
417 change_notifier.AddObserver(&observer);
419 std::vector<Display> old_displays, new_displays;
420 old_displays.push_back(Display(1));
421 old_displays[0].set_device_scale_factor(1.f);
422 new_displays.push_back(Display(1));
423 new_displays[0].set_device_scale_factor(2.f);
425 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
426 EXPECT_EQ(1, observer.display_changed());
427 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR,
428 observer.latest_metrics_change());
431 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Displays) {
432 DisplayChangeNotifier change_notifier;
433 MockDisplayObserver observer;
434 change_notifier.AddObserver(&observer);
436 std::vector<Display> old_displays, new_displays;
437 old_displays.push_back(Display(1));
438 old_displays.push_back(Display(2));
439 old_displays.push_back(Display(3));
440 new_displays.push_back(Display(1));
441 new_displays.push_back(Display(2));
442 new_displays.push_back(Display(3));
444 old_displays[0].set_device_scale_factor(1.f);
445 new_displays[0].set_device_scale_factor(2.f);
447 old_displays[1].set_bounds(Rect(0, 0, 200, 200));
448 new_displays[1].set_bounds(Rect(0, 0, 400, 400));
450 old_displays[2].SetRotationAsDegree(0);
451 new_displays[2].SetRotationAsDegree(90);
453 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
454 EXPECT_EQ(3, observer.display_changed());
457 TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Metrics) {
458 DisplayChangeNotifier change_notifier;
459 MockDisplayObserver observer;
460 change_notifier.AddObserver(&observer);
462 std::vector<Display> old_displays, new_displays;
463 old_displays.push_back(Display(1, Rect(0, 0, 200, 200)));
464 old_displays[0].set_device_scale_factor(1.f);
465 old_displays[0].SetRotationAsDegree(0);
467 new_displays.push_back(Display(1, Rect(100, 100, 200, 200)));
468 new_displays[0].set_device_scale_factor(2.f);
469 new_displays[0].SetRotationAsDegree(90);
471 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
472 EXPECT_EQ(1, observer.display_changed());
473 uint32_t metrics = DisplayObserver::DISPLAY_METRIC_BOUNDS |
474 DisplayObserver::DISPLAY_METRIC_ROTATION |
475 DisplayObserver::DISPLAY_METRIC_WORK_AREA |
476 DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
477 EXPECT_EQ(metrics, observer.latest_metrics_change());
480 } // namespace gfx