1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/display/display_manager.h"
#include "ash/display/display_controller.h"
#include "ash/screen_ash.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/display_manager_test_api.h"
#include "base/format_macros.h"
#include "base/stringprintf.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window_observer.h"
#include "ui/gfx/display_observer.h"
#include "ui/gfx/display.h"
namespace ash {
namespace internal {
using std::vector;
using std::string;
using base::StringPrintf;
class DisplayManagerTest : public test::AshTestBase,
public gfx::DisplayObserver,
public aura::WindowObserver {
public:
DisplayManagerTest()
: removed_count_(0U),
root_window_destroyed_(false) {
}
virtual ~DisplayManagerTest() {}
virtual void SetUp() OVERRIDE {
AshTestBase::SetUp();
Shell::GetScreen()->AddObserver(this);
Shell::GetPrimaryRootWindow()->AddObserver(this);
}
virtual void TearDown() OVERRIDE {
Shell::GetPrimaryRootWindow()->RemoveObserver(this);
Shell::GetScreen()->RemoveObserver(this);
AshTestBase::TearDown();
}
DisplayManager* display_manager() {
return Shell::GetInstance()->display_manager();
}
const vector<gfx::Display>& changed() const { return changed_; }
const vector<gfx::Display>& added() const { return added_; }
string GetCountSummary() const {
return StringPrintf("%"PRIuS" %"PRIuS" %"PRIuS,
changed_.size(), added_.size(), removed_count_);
}
void reset() {
changed_.clear();
added_.clear();
removed_count_ = 0U;
root_window_destroyed_ = false;
}
bool root_window_destroyed() const {
return root_window_destroyed_;
}
const DisplayInfo& GetDisplayInfo(const gfx::Display& display) {
return display_manager()->GetDisplayInfo(display.id());
}
const DisplayInfo& GetDisplayInfoAt(int index) {
return GetDisplayInfo(*display_manager()->GetDisplayAt(index));
}
const gfx::Display& FindDisplayForId(int64 id) {
return display_manager()->FindDisplayForId(id);
}
const DisplayInfo& FindDisplayInfoForId(int64 id) {
return GetDisplayInfo(display_manager()->FindDisplayForId(id));
}
// aura::DisplayObserver overrides:
virtual void OnDisplayBoundsChanged(const gfx::Display& display) OVERRIDE {
changed_.push_back(display);
}
virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE {
added_.push_back(new_display);
}
virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE {
++removed_count_;
}
// aura::WindowObserver overrides:
virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
ASSERT_EQ(Shell::GetPrimaryRootWindow(), window);
root_window_destroyed_ = true;
}
private:
vector<gfx::Display> changed_;
vector<gfx::Display> added_;
size_t removed_count_;
bool root_window_destroyed_;
DISALLOW_COPY_AND_ASSIGN(DisplayManagerTest);
};
TEST_F(DisplayManagerTest, NativeDisplayTest) {
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
// Update primary and add seconary.
UpdateDisplay("100+0-500x500,0+501-400x400");
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 500x500",
display_manager()->GetDisplayAt(0)->bounds().ToString());
EXPECT_EQ("1 1 0", GetCountSummary());
EXPECT_EQ(display_manager()->GetDisplayAt(0)->id(), changed()[0].id());
EXPECT_EQ(display_manager()->GetDisplayAt(1)->id(), added()[0].id());
EXPECT_EQ("0,0 500x500", changed()[0].bounds().ToString());
// Secondary display is on right.
EXPECT_EQ("500,0 400x400", added()[0].bounds().ToString());
EXPECT_EQ("0,501 400x400",
GetDisplayInfo(added()[0]).bounds_in_pixel().ToString());
reset();
// Delete secondary.
UpdateDisplay("100+0-500x500");
EXPECT_EQ("0 0 1", GetCountSummary());
reset();
// Change primary.
UpdateDisplay("1+1-1000x600");
EXPECT_EQ("1 0 0", GetCountSummary());
EXPECT_EQ(display_manager()->GetDisplayAt(0)->id(), changed()[0].id());
EXPECT_EQ("0,0 1000x600", changed()[0].bounds().ToString());
reset();
// Add secondary.
UpdateDisplay("1+1-1000x600,1002+0-600x400");
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0 1 0", GetCountSummary());
EXPECT_EQ(display_manager()->GetDisplayAt(1)->id(), added()[0].id());
// Secondary display is on right.
EXPECT_EQ("1000,0 600x400", added()[0].bounds().ToString());
EXPECT_EQ("1002,0 600x400",
GetDisplayInfo(added()[0]).bounds_in_pixel().ToString());
reset();
// Secondary removed, primary changed.
UpdateDisplay("1+1-800x300");
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("1 0 1", GetCountSummary());
EXPECT_EQ(display_manager()->GetDisplayAt(0)->id(), changed()[0].id());
EXPECT_EQ("0,0 800x300", changed()[0].bounds().ToString());
reset();
// # of display can go to zero when screen is off.
const vector<DisplayInfo> empty;
display_manager()->OnNativeDisplaysChanged(empty);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("0 0 0", GetCountSummary());
EXPECT_FALSE(root_window_destroyed());
// Display configuration stays the same
EXPECT_EQ("0,0 800x300",
display_manager()->GetDisplayAt(0)->bounds().ToString());
reset();
// Connect to display again
UpdateDisplay("100+100-500x400");
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("1 0 0", GetCountSummary());
EXPECT_FALSE(root_window_destroyed());
EXPECT_EQ("0,0 500x400", changed()[0].bounds().ToString());
EXPECT_EQ("100,100 500x400",
GetDisplayInfo(changed()[0]).bounds_in_pixel().ToString());
reset();
// Go back to zero and wake up with multiple displays.
display_manager()->OnNativeDisplaysChanged(empty);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_FALSE(root_window_destroyed());
reset();
// Add secondary.
UpdateDisplay("0+0-1000x600,1000+1000-600x400");
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 1000x600",
display_manager()->GetDisplayAt(0)->bounds().ToString());
// Secondary display is on right.
EXPECT_EQ("1000,0 600x400",
display_manager()->GetDisplayAt(1)->bounds().ToString());
EXPECT_EQ("1000,1000 600x400",
GetDisplayInfoAt(1).bounds_in_pixel().ToString());
reset();
}
// Test in emulation mode (use_fullscreen_host_window=false)
TEST_F(DisplayManagerTest, EmulatorTest) {
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
DisplayManager::CycleDisplay();
// Update primary and add seconary.
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0 1 0", GetCountSummary());
reset();
DisplayManager::CycleDisplay();
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("0 0 1", GetCountSummary());
reset();
DisplayManager::CycleDisplay();
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0 1 0", GetCountSummary());
reset();
}
TEST_F(DisplayManagerTest, OverscanInsetsTest) {
UpdateDisplay("0+0-500x500,0+501-400x400");
reset();
ASSERT_EQ(2u, display_manager()->GetNumDisplays());
const DisplayInfo& display_info1 = GetDisplayInfoAt(0);
const DisplayInfo& display_info2 = GetDisplayInfoAt(1);
display_manager()->SetOverscanInsets(
display_info2.id(), gfx::Insets(13, 12, 11, 10));
std::vector<gfx::Display> changed_displays = changed();
EXPECT_EQ(1u, changed_displays.size());
EXPECT_EQ(display_info2.id(), changed_displays[0].id());
EXPECT_EQ("0,0 500x500",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
DisplayInfo updated_display_info2 = GetDisplayInfoAt(1);
EXPECT_EQ("0,501 400x400",
updated_display_info2.bounds_in_pixel().ToString());
EXPECT_EQ("378x376",
updated_display_info2.size_in_pixel().ToString());
EXPECT_EQ("13,12,11,10",
updated_display_info2.overscan_insets_in_dip().ToString());
EXPECT_EQ("500,0 378x376",
ScreenAsh::GetSecondaryDisplay().bounds().ToString());
// Make sure that SetOverscanInsets() is idempotent.
display_manager()->SetOverscanInsets(display_info1.id(), gfx::Insets());
display_manager()->SetOverscanInsets(
display_info2.id(), gfx::Insets(13, 12, 11, 10));
EXPECT_EQ("0,0 500x500",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
updated_display_info2 = GetDisplayInfoAt(1);
EXPECT_EQ("0,501 400x400",
updated_display_info2.bounds_in_pixel().ToString());
EXPECT_EQ("378x376",
updated_display_info2.size_in_pixel().ToString());
EXPECT_EQ("13,12,11,10",
updated_display_info2.overscan_insets_in_dip().ToString());
display_manager()->SetOverscanInsets(
display_info2.id(), gfx::Insets(10, 11, 12, 13));
EXPECT_EQ("0,0 500x500",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
EXPECT_EQ("376x378",
GetDisplayInfoAt(1).size_in_pixel().ToString());
EXPECT_EQ("10,11,12,13",
GetDisplayInfoAt(1).overscan_insets_in_dip().ToString());
// Recreate a new 2nd display. It won't apply the overscan inset because the
// new display has a different ID.
UpdateDisplay("0+0-500x500");
UpdateDisplay("0+0-500x500,0+501-400x400");
EXPECT_EQ("0,0 500x500",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
EXPECT_EQ("0,501 400x400",
GetDisplayInfoAt(1).bounds_in_pixel().ToString());
// Recreate the displays with the same ID. It should apply the overscan
// inset.
UpdateDisplay("0+0-500x500");
std::vector<DisplayInfo> display_info_list;
display_info_list.push_back(display_info1);
display_info_list.push_back(display_info2);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ("1,1 500x500",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
updated_display_info2 = GetDisplayInfoAt(1);
EXPECT_EQ("376x378",
updated_display_info2.size_in_pixel().ToString());
EXPECT_EQ("10,11,12,13",
updated_display_info2.overscan_insets_in_dip().ToString());
// HiDPI but overscan display. The specified insets size should be doubled.
UpdateDisplay("0+0-500x500,0+501-400x400*2");
display_manager()->SetOverscanInsets(
display_manager()->GetDisplayAt(1)->id(), gfx::Insets(4, 5, 6, 7));
EXPECT_EQ("0,0 500x500",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
updated_display_info2 = GetDisplayInfoAt(1);
EXPECT_EQ("0,501 400x400",
updated_display_info2.bounds_in_pixel().ToString());
EXPECT_EQ("376x380",
updated_display_info2.size_in_pixel().ToString());
EXPECT_EQ("4,5,6,7",
updated_display_info2.overscan_insets_in_dip().ToString());
EXPECT_EQ("8,10,12,14",
updated_display_info2.GetOverscanInsetsInPixel().ToString());
// Make sure switching primary display applies the overscan offset only once.
ash::Shell::GetInstance()->display_controller()->SetPrimaryDisplay(
ScreenAsh::GetSecondaryDisplay());
EXPECT_EQ("-500,0 500x500",
ScreenAsh::GetSecondaryDisplay().bounds().ToString());
EXPECT_EQ("0,0 500x500",
GetDisplayInfo(ScreenAsh::GetSecondaryDisplay()).
bounds_in_pixel().ToString());
EXPECT_EQ("0,501 400x400",
GetDisplayInfo(Shell::GetScreen()->GetPrimaryDisplay()).
bounds_in_pixel().ToString());
EXPECT_EQ("0,0 188x190",
Shell::GetScreen()->GetPrimaryDisplay().bounds().ToString());
}
TEST_F(DisplayManagerTest, ZeroOverscanInsets) {
// Make sure the display change events is emitted for overscan inset changes.
UpdateDisplay("0+0-500x500,0+501-400x400");
ASSERT_EQ(2u, display_manager()->GetNumDisplays());
int64 display2_id = display_manager()->GetDisplayAt(1)->id();
reset();
display_manager()->SetOverscanInsets(display2_id, gfx::Insets(0, 0, 0, 0));
EXPECT_EQ(0u, changed().size());
reset();
display_manager()->SetOverscanInsets(display2_id, gfx::Insets(1, 0, 0, 0));
EXPECT_EQ(1u, changed().size());
EXPECT_EQ(display2_id, changed()[0].id());
reset();
display_manager()->SetOverscanInsets(display2_id, gfx::Insets(0, 0, 0, 0));
EXPECT_EQ(1u, changed().size());
EXPECT_EQ(display2_id, changed()[0].id());
}
TEST_F(DisplayManagerTest, TestDeviceScaleOnlyChange) {
UpdateDisplay("1000x600");
EXPECT_EQ(1,
Shell::GetPrimaryRootWindow()->compositor()->device_scale_factor());
EXPECT_EQ("1000x600",
Shell::GetPrimaryRootWindow()->bounds().size().ToString());
UpdateDisplay("1000x600*2");
EXPECT_EQ(2,
Shell::GetPrimaryRootWindow()->compositor()->device_scale_factor());
EXPECT_EQ("500x300",
Shell::GetPrimaryRootWindow()->bounds().size().ToString());
}
DisplayInfo CreateDisplayInfo(int64 id, const gfx::Rect& bounds) {
DisplayInfo info(id, StringPrintf("x-%d", static_cast<int>(id)), false);
info.SetBounds(bounds);
return info;
}
TEST_F(DisplayManagerTest, TestNativeDisplaysChanged) {
const int internal_display_id =
test::DisplayManagerTestApi(display_manager()).
SetFirstDisplayAsInternalDisplay();
const int external_id = 10;
const int mirror_id = 11;
const int64 invalid_id = gfx::Display::kInvalidDisplayID;
const DisplayInfo internal_display_info =
CreateDisplayInfo(internal_display_id, gfx::Rect(0, 0, 500, 500));
const DisplayInfo external_display_info =
CreateDisplayInfo(external_id, gfx::Rect(1, 1, 100, 100));
const DisplayInfo mirrored_display_info =
CreateDisplayInfo(mirror_id, gfx::Rect(0, 0, 500, 500));
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ(1U, display_manager()->num_connected_displays());
std::string default_bounds =
display_manager()->GetDisplayAt(0)->bounds().ToString();
std::vector<DisplayInfo> display_info_list;
// Primary disconnected.
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ(default_bounds,
display_manager()->GetDisplayAt(0)->bounds().ToString());
EXPECT_EQ(1U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
// External connected while primary was disconnected.
display_info_list.push_back(external_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ(invalid_id, FindDisplayForId(internal_display_id).id());
EXPECT_EQ("1,1 100x100",
FindDisplayInfoForId(external_id).bounds_in_pixel().ToString());
EXPECT_EQ(1U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
EXPECT_EQ(external_id, Shell::GetScreen()->GetPrimaryDisplay().id());
// Primary connected, with different bounds.
display_info_list.clear();
display_info_list.push_back(internal_display_info);
display_info_list.push_back(external_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
// need to remember which is primary
EXPECT_EQ("0,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ("1,1 100x100",
FindDisplayInfoForId(10).bounds_in_pixel().ToString());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
EXPECT_EQ(StringPrintf("x-%d", internal_display_id),
display_manager()->GetDisplayNameForId(internal_display_id));
// Emulate suspend.
display_info_list.clear();
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ("1,1 100x100",
FindDisplayInfoForId(10).bounds_in_pixel().ToString());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
EXPECT_EQ(StringPrintf("x-%d", internal_display_id),
display_manager()->GetDisplayNameForId(internal_display_id));
// External display has disconnected then resumed.
display_info_list.push_back(internal_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ(1U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
// External display was changed during suspend.
display_info_list.push_back(external_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
// suspend...
display_info_list.clear();
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
// and resume with different external display.
display_info_list.push_back(internal_display_info);
display_info_list.push_back(CreateDisplayInfo(12, gfx::Rect(1, 1, 100, 100)));
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
EXPECT_FALSE(display_manager()->IsMirrored());
// mirrored...
display_info_list.clear();
display_info_list.push_back(internal_display_info);
display_info_list.push_back(mirrored_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_EQ(11U, display_manager()->mirrored_display_id());
EXPECT_TRUE(display_manager()->IsMirrored());
// Test display name.
EXPECT_EQ(StringPrintf("x-%d", internal_display_id),
display_manager()->GetDisplayNameForId(internal_display_id));
EXPECT_EQ("x-10", display_manager()->GetDisplayNameForId(10));
EXPECT_EQ("x-11", display_manager()->GetDisplayNameForId(11));
EXPECT_EQ("x-12", display_manager()->GetDisplayNameForId(12));
// Default name for the id that doesn't exist.
EXPECT_EQ("Display 100", display_manager()->GetDisplayNameForId(100));
// and exit mirroring.
display_info_list.clear();
display_info_list.push_back(internal_display_info);
display_info_list.push_back(external_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ(2U, display_manager()->num_connected_displays());
EXPECT_FALSE(display_manager()->IsMirrored());
EXPECT_EQ("0,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ("500,0 100x100",
FindDisplayForId(10).bounds().ToString());
// Turn off internal
display_info_list.clear();
display_info_list.push_back(external_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ(invalid_id, FindDisplayForId(internal_display_id).id());
EXPECT_EQ("1,1 100x100",
FindDisplayInfoForId(external_id).bounds_in_pixel().ToString());
EXPECT_EQ(1U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
// Switched to another display
display_info_list.clear();
display_info_list.push_back(internal_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ(
"0,0 500x500",
FindDisplayInfoForId(internal_display_id).bounds_in_pixel().ToString());
EXPECT_EQ(1U, display_manager()->num_connected_displays());
EXPECT_EQ(invalid_id, display_manager()->mirrored_display_id());
}
#if defined(OS_WIN)
// This test currently fails on Win8/Metro as it picks up the actual
// display size. http://crbug.com/154081
#define MAYBE_TestNativeDisplaysChangedNoInternal \
DISABLED_TestNativeDisplaysChangedNoInternal
#else
#define MAYBE_TestNativeDisplaysChangedNoInternal \
TestNativeDisplaysChangedNoInternal
#endif
TEST_F(DisplayManagerTest, MAYBE_TestNativeDisplaysChangedNoInternal) {
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
// Don't change the display info if all displays are disconnected.
std::vector<DisplayInfo> display_info_list;
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
// Connect another display which will become primary.
const DisplayInfo external_display_info =
CreateDisplayInfo(10, gfx::Rect(1, 1, 100, 100));
display_info_list.push_back(external_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("1,1 100x100",
FindDisplayInfoForId(10).bounds_in_pixel().ToString());
EXPECT_EQ("100x100",
ash::Shell::GetPrimaryRootWindow()->GetHostSize().ToString());
}
TEST_F(DisplayManagerTest, EnsurePointerInDisplays) {
UpdateDisplay("200x200,300x300");
Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
aura::Env* env = aura::Env::GetInstance();
// Set the initial position.
root_windows[0]->MoveCursorTo(gfx::Point(350, 150));
EXPECT_EQ("350,150", env->last_mouse_location().ToString());
// A mouse pointer will be inside 2nd display.
UpdateDisplay("300x300,200x200");
EXPECT_EQ("350,150", env->last_mouse_location().ToString());
// A mouse pointer will be outside of displays and move to the
// center of 2nd display.
UpdateDisplay("300x300,100x100");
EXPECT_EQ("350,50", env->last_mouse_location().ToString());
// 2nd display was disconnected, but the mouse pointer says in the
// 1st display.
UpdateDisplay("400x400");
EXPECT_EQ("350,50", env->last_mouse_location().ToString());
// 1st display's resolution has changed, and the mouse pointer is
// now outside. Move the mouse pointer to the center of 1st display.
UpdateDisplay("300x300");
EXPECT_EQ("150,150", env->last_mouse_location().ToString());
// Move the mouse pointer to the bottom of 1st display.
root_windows[0]->MoveCursorTo(gfx::Point(150, 290));
EXPECT_EQ("150,290", env->last_mouse_location().ToString());
// The mouse pointer is outside and closest display is 1st one.
UpdateDisplay("300x280,200x200");
EXPECT_EQ("150,140", env->last_mouse_location().ToString());
}
TEST_F(DisplayManagerTest, EnsurePointerInDisplays_2ndOnLeft) {
// Set the 2nd display on the left.
DisplayController* display_controller =
Shell::GetInstance()->display_controller();
DisplayLayout layout = display_controller->default_display_layout();
layout.position = DisplayLayout::LEFT;
display_controller->SetDefaultDisplayLayout(layout);
UpdateDisplay("200x200,300x300");
Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
EXPECT_EQ("-300,0 300x300",
ScreenAsh::GetSecondaryDisplay().bounds().ToString());
aura::Env* env = aura::Env::GetInstance();
// Set the initial position.
root_windows[0]->MoveCursorTo(gfx::Point(-150, 150));
EXPECT_EQ("-150,150", env->last_mouse_location().ToString());
// A mouse pointer will be in 2nd display.
UpdateDisplay("300x300,200x200");
EXPECT_EQ("-150,150", env->last_mouse_location().ToString());
// A mouse pointer will be outside of displays and move to the
// center of 2nd display.
UpdateDisplay("300x300,200x100");
EXPECT_EQ("-100,50", env->last_mouse_location().ToString());
// 2nd display was disconnected. Mouse pointer should move to
// 1st display.
UpdateDisplay("300x300");
EXPECT_EQ("150,150", env->last_mouse_location().ToString());
}
TEST_F(DisplayManagerTest, NativeDisplaysChangedAfterPrimaryChange) {
const int64 internal_display_id =
test::DisplayManagerTestApi(display_manager()).
SetFirstDisplayAsInternalDisplay();
const DisplayInfo native_display_info =
CreateDisplayInfo(internal_display_id, gfx::Rect(0, 0, 500, 500));
const DisplayInfo secondary_display_info =
CreateDisplayInfo(10, gfx::Rect(1, 1, 100, 100));
std::vector<DisplayInfo> display_info_list;
display_info_list.push_back(native_display_info);
display_info_list.push_back(secondary_display_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ("500,0 100x100", FindDisplayForId(10).bounds().ToString());
ash::Shell::GetInstance()->display_controller()->SetPrimaryDisplay(
FindDisplayForId(secondary_display_info.id()));
EXPECT_EQ("-500,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ("0,0 100x100", FindDisplayForId(10).bounds().ToString());
// OnNativeDisplaysChanged may change the display bounds. Here makes sure
// nothing changed if the exactly same displays are specified.
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ("-500,0 500x500",
FindDisplayForId(internal_display_id).bounds().ToString());
EXPECT_EQ("0,0 100x100", FindDisplayForId(10).bounds().ToString());
}
TEST_F(DisplayManagerTest, AutomaticOverscanInsets) {
UpdateDisplay("200x200,400x400");
std::vector<DisplayInfo> display_info_list;
display_info_list.push_back(GetDisplayInfoAt(0));
display_info_list.push_back(GetDisplayInfoAt(1));
display_info_list[1].set_has_overscan_for_test(true);
int64 id = display_info_list[1].id();
// SetDefaultOverscanInsets(&display_info_list[1]);
display_manager()->OnNativeDisplaysChanged(display_info_list);
// It has overscan insets, although SetOverscanInsets() isn't called.
EXPECT_EQ("380x380",
GetDisplayInfoAt(1).size_in_pixel().ToString());
// If custom overscan insets is specified, the specified value is used.
display_manager()->SetOverscanInsets(id, gfx::Insets(5, 6, 7, 8));
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ("386x388",
GetDisplayInfoAt(1).size_in_pixel().ToString());
// Do not overscan even though it has 'has_overscan' flag, if the custom
// insets is empty.
display_manager()->SetOverscanInsets(id, gfx::Insets());
display_manager()->OnNativeDisplaysChanged(display_info_list);
EXPECT_EQ("400x400",
GetDisplayInfoAt(1).size_in_pixel().ToString());
// Clearing the custom overscan should set the bounds to
// original.
display_manager()->ClearCustomOverscanInsets(id);
EXPECT_EQ("380x380",
GetDisplayInfoAt(1).size_in_pixel().ToString());
}
TEST_F(DisplayManagerTest, Rotate) {
UpdateDisplay("100x200/r,300x400/l");
EXPECT_EQ("1,1 100x200",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
EXPECT_EQ("200x100",
GetDisplayInfoAt(0).size_in_pixel().ToString());
EXPECT_EQ("1,201 300x400",
GetDisplayInfoAt(1).bounds_in_pixel().ToString());
EXPECT_EQ("400x300",
GetDisplayInfoAt(1).size_in_pixel().ToString());
UpdateDisplay("100x200/b,300x400");
EXPECT_EQ("1,1 100x200",
GetDisplayInfoAt(0).bounds_in_pixel().ToString());
EXPECT_EQ("100x200",
GetDisplayInfoAt(0).size_in_pixel().ToString());
EXPECT_EQ("1,201 300x400",
GetDisplayInfoAt(1).bounds_in_pixel().ToString());
EXPECT_EQ("300x400",
GetDisplayInfoAt(1).size_in_pixel().ToString());
}
TEST_F(DisplayManagerTest, UIScale) {
// 1x display does not support scaling.
UpdateDisplay("100x200");
int64 display_id = Shell::GetScreen()->GetPrimaryDisplay().id();
display_manager()->SetDisplayUIScale(display_id, 1.5f);
EXPECT_EQ(1.0f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 1.25f);
EXPECT_EQ(1.0f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 1.125f);
EXPECT_EQ(1.0, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 0.8f);
EXPECT_EQ(1.0f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 0.625f);
EXPECT_EQ(1.0f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 0.5f);
EXPECT_EQ(1.0f, GetDisplayInfoAt(0).ui_scale());
// 2x display supports all scales.
UpdateDisplay("100x200*2");
EXPECT_EQ(1.0f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 1.5f);
EXPECT_EQ(1.5f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 1.25f);
EXPECT_EQ(1.25f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 1.125f);
EXPECT_EQ(1.125f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 0.8f);
EXPECT_EQ(0.8f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 0.625f);
EXPECT_EQ(0.625f, GetDisplayInfoAt(0).ui_scale());
display_manager()->SetDisplayUIScale(display_id, 0.5f);
EXPECT_EQ(0.5f, GetDisplayInfoAt(0).ui_scale());
}
} // namespace internal
} // namespace ash
|