blob: 69e82b4de350ea62acda49102b78d085de1bb09a [file] [log] [blame]
[email protected]472de00a2014-07-18 22:36:131// 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
oshimaa15deb02016-05-11 00:19:335#include "ui/display/display_change_notifier.h"
[email protected]472de00a2014-07-18 22:36:136
avic89eb8d42015-12-23 08:08:187#include <stdint.h>
8
9#include "base/macros.h"
[email protected]472de00a2014-07-18 22:36:1310#include "testing/gtest/include/gtest/gtest.h"
oshimaa15deb02016-05-11 00:19:3311#include "ui/display/display.h"
12#include "ui/display/display_observer.h"
[email protected]472de00a2014-07-18 22:36:1313
oshimaa15deb02016-05-11 00:19:3314namespace display {
[email protected]472de00a2014-07-18 22:36:1315
16class MockDisplayObserver : public DisplayObserver {
17 public:
18 MockDisplayObserver()
oshimaa15deb02016-05-11 00:19:3319 : display_added_(0),
20 display_removed_(0),
21 display_changed_(0),
22 latest_metrics_change_(DisplayObserver::DISPLAY_METRIC_NONE) {}
[email protected]472de00a2014-07-18 22:36:1323
dcheng08038792014-10-21 10:53:2624 ~MockDisplayObserver() override {}
[email protected]472de00a2014-07-18 22:36:1325
dcheng08038792014-10-21 10:53:2626 void OnDisplayAdded(const Display& display) override { display_added_++; }
[email protected]472de00a2014-07-18 22:36:1327
dcheng08038792014-10-21 10:53:2628 void OnDisplayRemoved(const Display& display) override { display_removed_++; }
[email protected]472de00a2014-07-18 22:36:1329
dcheng08038792014-10-21 10:53:2630 void OnDisplayMetricsChanged(const Display& display,
31 uint32_t metrics) override {
[email protected]472de00a2014-07-18 22:36:1332 display_changed_++;
33 latest_metrics_change_ = metrics;
34 }
35
oshimaa15deb02016-05-11 00:19:3336 int display_added() const { return display_added_; }
[email protected]472de00a2014-07-18 22:36:1337
oshimaa15deb02016-05-11 00:19:3338 int display_removed() const { return display_removed_; }
[email protected]472de00a2014-07-18 22:36:1339
oshimaa15deb02016-05-11 00:19:3340 int display_changed() const { return display_changed_; }
[email protected]472de00a2014-07-18 22:36:1341
oshimaa15deb02016-05-11 00:19:3342 uint32_t latest_metrics_change() const { return latest_metrics_change_; }
[email protected]472de00a2014-07-18 22:36:1343
44 protected:
45 int display_added_;
46 int display_removed_;
47 int display_changed_;
48 uint32_t latest_metrics_change_;
49
50 DISALLOW_COPY_AND_ASSIGN(MockDisplayObserver);
51};
52
53TEST(DisplayChangeNotifierTest, AddObserver_Smoke) {
54 DisplayChangeNotifier change_notifier;
55 MockDisplayObserver observer;
56
oshimaa15deb02016-05-11 00:19:3357 change_notifier.NotifyDisplaysChanged(std::vector<Display>(),
58 std::vector<Display>(1, Display()));
[email protected]472de00a2014-07-18 22:36:1359 EXPECT_EQ(0, observer.display_added());
60
61 change_notifier.AddObserver(&observer);
oshimaa15deb02016-05-11 00:19:3362 change_notifier.NotifyDisplaysChanged(std::vector<Display>(),
63 std::vector<Display>(1, Display()));
[email protected]472de00a2014-07-18 22:36:1364 EXPECT_EQ(1, observer.display_added());
65}
66
[email protected]472de00a2014-07-18 22:36:1367TEST(DisplayChangeNotifier, RemoveObserver_Smoke) {
68 DisplayChangeNotifier change_notifier;
69 MockDisplayObserver observer;
70
oshimaa15deb02016-05-11 00:19:3371 change_notifier.NotifyDisplaysChanged(std::vector<Display>(),
72 std::vector<Display>(1, Display()));
[email protected]472de00a2014-07-18 22:36:1373 EXPECT_EQ(0, observer.display_added());
74
75 change_notifier.AddObserver(&observer);
76 change_notifier.RemoveObserver(&observer);
77
oshimaa15deb02016-05-11 00:19:3378 change_notifier.NotifyDisplaysChanged(std::vector<Display>(),
79 std::vector<Display>(1, Display()));
[email protected]472de00a2014-07-18 22:36:1380 EXPECT_EQ(0, observer.display_added());
81}
82
[email protected]472de00a2014-07-18 22:36:1383TEST(DisplayChangeNotifierTest, RemoveObserver_Unknown) {
84 DisplayChangeNotifier change_notifier;
85 MockDisplayObserver observer;
86
87 change_notifier.RemoveObserver(&observer);
88 // Should not crash.
89}
90
91TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Removed) {
92 DisplayChangeNotifier change_notifier;
93
94 // If the previous display array is empty, no removal.
95 {
96 MockDisplayObserver observer;
97 change_notifier.AddObserver(&observer);
98
99 std::vector<Display> old_displays, new_displays;
100 new_displays.push_back(Display());
101
102 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
103 EXPECT_EQ(0, observer.display_removed());
104
105 change_notifier.RemoveObserver(&observer);
106 }
107
108 // If the previous and new display array are empty, no removal.
109 {
110 MockDisplayObserver observer;
111 change_notifier.AddObserver(&observer);
112
113 std::vector<Display> old_displays, new_displays;
114
115 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
116 EXPECT_EQ(0, observer.display_removed());
117
118 change_notifier.RemoveObserver(&observer);
119 }
120
121 // If the new display array is empty, there are as many removal as old
122 // displays.
123 {
124 MockDisplayObserver observer;
125 change_notifier.AddObserver(&observer);
126
127 std::vector<Display> old_displays, new_displays;
128 old_displays.push_back(Display());
129 old_displays.push_back(Display());
130 old_displays.push_back(Display());
131
132 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
133 EXPECT_EQ(3, observer.display_removed());
134
135 change_notifier.RemoveObserver(&observer);
136 }
137
138 // If displays don't use ids, as long as the new display array has one
139 // element, there are no removals.
140 {
141 MockDisplayObserver observer;
142 change_notifier.AddObserver(&observer);
143
144 std::vector<Display> old_displays, new_displays;
145 old_displays.push_back(Display());
146 old_displays.push_back(Display());
147 old_displays.push_back(Display());
148 new_displays.push_back(Display());
149
150 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
151 EXPECT_EQ(0, observer.display_removed());
152
153 change_notifier.RemoveObserver(&observer);
154 }
155
156 // If displays use ids (and they are unique), ids not present in the new
157 // display array will be marked as removed.
158 {
159 MockDisplayObserver observer;
160 change_notifier.AddObserver(&observer);
161
162 std::vector<Display> old_displays, new_displays;
163 old_displays.push_back(Display(1));
164 old_displays.push_back(Display(2));
165 old_displays.push_back(Display(3));
166 new_displays.push_back(Display(2));
167
168 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
169 EXPECT_EQ(2, observer.display_removed());
170
171 change_notifier.RemoveObserver(&observer);
172 }
173}
174
175TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Added) {
176 DisplayChangeNotifier change_notifier;
177
178 // If the new display array is empty, no addition.
179 {
180 MockDisplayObserver observer;
181 change_notifier.AddObserver(&observer);
182
183 std::vector<Display> old_displays, new_displays;
184 old_displays.push_back(Display());
185
186 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
187 EXPECT_EQ(0, observer.display_added());
188
189 change_notifier.RemoveObserver(&observer);
190 }
191
192 // If the old and new display arrays are empty, no addition.
193 {
194 MockDisplayObserver observer;
195 change_notifier.AddObserver(&observer);
196
197 std::vector<Display> old_displays, new_displays;
198
199 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
200 EXPECT_EQ(0, observer.display_added());
201
202 change_notifier.RemoveObserver(&observer);
203 }
204
205 // If the old display array is empty, there are as many addition as new
206 // displays.
207 {
208 MockDisplayObserver observer;
209 change_notifier.AddObserver(&observer);
210
211 std::vector<Display> old_displays, new_displays;
212 new_displays.push_back(Display());
213 new_displays.push_back(Display());
214 new_displays.push_back(Display());
215
216 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
217 EXPECT_EQ(3, observer.display_added());
218
219 change_notifier.RemoveObserver(&observer);
220 }
221
222 // If displays don't use ids, as long as the old display array has one
223 // element, there are no additions.
224 {
225 MockDisplayObserver observer;
226 change_notifier.AddObserver(&observer);
227
228 std::vector<Display> old_displays, new_displays;
229 old_displays.push_back(Display());
230 new_displays.push_back(Display());
231 new_displays.push_back(Display());
232 new_displays.push_back(Display());
233
234 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
235 EXPECT_EQ(0, observer.display_added());
236
237 change_notifier.RemoveObserver(&observer);
238 }
239
240 // If displays use ids (and they are unique), ids not present in the old
241 // display array will be marked as added.
242 {
243 MockDisplayObserver observer;
244 change_notifier.AddObserver(&observer);
245
246 std::vector<Display> old_displays, new_displays;
247 old_displays.push_back(Display(1));
248 new_displays.push_back(Display(1));
249 new_displays.push_back(Display(2));
250 new_displays.push_back(Display(3));
251
252 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
253 EXPECT_EQ(2, observer.display_added());
254
255 change_notifier.RemoveObserver(&observer);
256 }
257}
258
259TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Smoke) {
260 DisplayChangeNotifier change_notifier;
261
262 // If the old display array is empty, no change.
263 {
264 MockDisplayObserver observer;
265 change_notifier.AddObserver(&observer);
266
267 std::vector<Display> old_displays, new_displays;
268 new_displays.push_back(Display());
269
270 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
271 EXPECT_EQ(0, observer.display_changed());
272
273 change_notifier.RemoveObserver(&observer);
274 }
275
276 // If the new display array is empty, no change.
277 {
278 MockDisplayObserver observer;
279 change_notifier.AddObserver(&observer);
280
281 std::vector<Display> old_displays, new_displays;
282 old_displays.push_back(Display());
283
284 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
285 EXPECT_EQ(0, observer.display_changed());
286
287 change_notifier.RemoveObserver(&observer);
288 }
289
290 // If the old and new display arrays are empty, no change.
291 {
292 MockDisplayObserver observer;
293 change_notifier.AddObserver(&observer);
294
295 std::vector<Display> old_displays, new_displays;
296
297 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
298 EXPECT_EQ(0, observer.display_changed());
299
300 change_notifier.RemoveObserver(&observer);
301 }
302
303 // If there is an intersection between old and new displays but there are no
304 // metrics changes, there is no display change.
305 {
306 MockDisplayObserver observer;
307 change_notifier.AddObserver(&observer);
308
309 std::vector<Display> old_displays, new_displays;
310 old_displays.push_back(Display(1));
311 new_displays.push_back(Display(1));
312 new_displays.push_back(Display(2));
313 new_displays.push_back(Display(3));
314
315 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
316 EXPECT_EQ(0, observer.display_changed());
317
318 change_notifier.RemoveObserver(&observer);
319 }
320}
321
322TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Bounds) {
323 DisplayChangeNotifier change_notifier;
324
325 {
326 MockDisplayObserver observer;
327 change_notifier.AddObserver(&observer);
328
329 std::vector<Display> old_displays, new_displays;
oshimaa15deb02016-05-11 00:19:33330 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
331 new_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
[email protected]472de00a2014-07-18 22:36:13332
333 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
334 EXPECT_EQ(0, observer.display_changed());
335
336 change_notifier.RemoveObserver(&observer);
337 }
338
339 {
340 MockDisplayObserver observer;
341 change_notifier.AddObserver(&observer);
342
343 std::vector<Display> old_displays, new_displays;
oshimaa15deb02016-05-11 00:19:33344 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
345 new_displays.push_back(Display(1, gfx::Rect(10, 10, 300, 300)));
[email protected]472de00a2014-07-18 22:36:13346
347 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
348 EXPECT_EQ(1, observer.display_changed());
349 uint32_t metrics_change = DisplayObserver::DISPLAY_METRIC_BOUNDS |
oshimaa15deb02016-05-11 00:19:33350 DisplayObserver::DISPLAY_METRIC_WORK_AREA;
[email protected]472de00a2014-07-18 22:36:13351 EXPECT_EQ(metrics_change, observer.latest_metrics_change());
352
353 change_notifier.RemoveObserver(&observer);
354 }
355
356 {
357 MockDisplayObserver observer;
358 change_notifier.AddObserver(&observer);
359
360 std::vector<Display> old_displays, new_displays;
oshimaa15deb02016-05-11 00:19:33361 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
362 new_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
363 new_displays[0].set_bounds(gfx::Rect(10, 10, 300, 300));
[email protected]472de00a2014-07-18 22:36:13364
365 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
366 EXPECT_EQ(1, observer.display_changed());
367 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_BOUNDS,
368 observer.latest_metrics_change());
369
370 change_notifier.RemoveObserver(&observer);
371 }
372}
373
374TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Rotation) {
375 DisplayChangeNotifier change_notifier;
376 MockDisplayObserver observer;
377 change_notifier.AddObserver(&observer);
378
379 std::vector<Display> old_displays, new_displays;
380 old_displays.push_back(Display(1));
381 old_displays[0].SetRotationAsDegree(0);
382 new_displays.push_back(Display(1));
383 new_displays[0].SetRotationAsDegree(180);
384
385 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
386 EXPECT_EQ(1, observer.display_changed());
387 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_ROTATION,
388 observer.latest_metrics_change());
389}
390
391TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_WorkArea) {
392 DisplayChangeNotifier change_notifier;
393 MockDisplayObserver observer;
394 change_notifier.AddObserver(&observer);
395
396 std::vector<Display> old_displays, new_displays;
397 old_displays.push_back(Display(1));
oshimaa15deb02016-05-11 00:19:33398 old_displays[0].set_work_area(gfx::Rect(0, 0, 200, 200));
[email protected]472de00a2014-07-18 22:36:13399 new_displays.push_back(Display(1));
oshimaa15deb02016-05-11 00:19:33400 new_displays[0].set_work_area(gfx::Rect(20, 20, 300, 300));
[email protected]472de00a2014-07-18 22:36:13401
402 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
403 EXPECT_EQ(1, observer.display_changed());
404 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_WORK_AREA,
405 observer.latest_metrics_change());
406}
407
408TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_DSF) {
409 DisplayChangeNotifier change_notifier;
410 MockDisplayObserver observer;
411 change_notifier.AddObserver(&observer);
412
413 std::vector<Display> old_displays, new_displays;
414 old_displays.push_back(Display(1));
415 old_displays[0].set_device_scale_factor(1.f);
416 new_displays.push_back(Display(1));
417 new_displays[0].set_device_scale_factor(2.f);
418
419 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
420 EXPECT_EQ(1, observer.display_changed());
421 EXPECT_EQ(DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR,
422 observer.latest_metrics_change());
423}
424
425TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Displays) {
426 DisplayChangeNotifier change_notifier;
427 MockDisplayObserver observer;
428 change_notifier.AddObserver(&observer);
429
430 std::vector<Display> old_displays, new_displays;
431 old_displays.push_back(Display(1));
432 old_displays.push_back(Display(2));
433 old_displays.push_back(Display(3));
434 new_displays.push_back(Display(1));
435 new_displays.push_back(Display(2));
436 new_displays.push_back(Display(3));
437
438 old_displays[0].set_device_scale_factor(1.f);
439 new_displays[0].set_device_scale_factor(2.f);
440
oshimaa15deb02016-05-11 00:19:33441 old_displays[1].set_bounds(gfx::Rect(0, 0, 200, 200));
442 new_displays[1].set_bounds(gfx::Rect(0, 0, 400, 400));
[email protected]472de00a2014-07-18 22:36:13443
444 old_displays[2].SetRotationAsDegree(0);
445 new_displays[2].SetRotationAsDegree(90);
446
447 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
448 EXPECT_EQ(3, observer.display_changed());
449}
450
451TEST(DisplayChangeNotifierTest, NotifyDisplaysChanged_Changed_Multi_Metrics) {
452 DisplayChangeNotifier change_notifier;
453 MockDisplayObserver observer;
454 change_notifier.AddObserver(&observer);
455
456 std::vector<Display> old_displays, new_displays;
oshimaa15deb02016-05-11 00:19:33457 old_displays.push_back(Display(1, gfx::Rect(0, 0, 200, 200)));
[email protected]472de00a2014-07-18 22:36:13458 old_displays[0].set_device_scale_factor(1.f);
459 old_displays[0].SetRotationAsDegree(0);
460
oshimaa15deb02016-05-11 00:19:33461 new_displays.push_back(Display(1, gfx::Rect(100, 100, 200, 200)));
[email protected]472de00a2014-07-18 22:36:13462 new_displays[0].set_device_scale_factor(2.f);
463 new_displays[0].SetRotationAsDegree(90);
464
465 change_notifier.NotifyDisplaysChanged(old_displays, new_displays);
466 EXPECT_EQ(1, observer.display_changed());
467 uint32_t metrics = DisplayObserver::DISPLAY_METRIC_BOUNDS |
oshimaa15deb02016-05-11 00:19:33468 DisplayObserver::DISPLAY_METRIC_ROTATION |
469 DisplayObserver::DISPLAY_METRIC_WORK_AREA |
470 DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR;
[email protected]472de00a2014-07-18 22:36:13471 EXPECT_EQ(metrics, observer.latest_metrics_change());
472}
473
oshimaa15deb02016-05-11 00:19:33474} // namespace display