forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_manager_unittest.cc
4657 lines (4020 loc) · 192 KB
/
display_manager_unittest.cc
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 "ui/display/manager/display_manager.h"
#include "ash/accelerators/accelerator_commands.h"
#include "ash/accelerometer/accelerometer_reader.h"
#include "ash/accelerometer/accelerometer_types.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/constants/app_types.h"
#include "ash/constants/ash_switches.h"
#include "ash/display/cursor_window_controller.h"
#include "ash/display/display_configuration_controller.h"
#include "ash/display/display_util.h"
#include "ash/display/mirror_window_controller.h"
#include "ash/display/mirror_window_test_api.h"
#include "ash/display/screen_orientation_controller.h"
#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/root_window_controller.h"
#include "ash/screen_util.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/shelf.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/command_line.h"
#include "base/format_macros.h"
#include "base/numerics/math_constants.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/env.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/display/display.h"
#include "ui/display/display_layout.h"
#include "ui/display/display_layout_builder.h"
#include "ui/display/display_observer.h"
#include "ui/display/display_switches.h"
#include "ui/display/fake/fake_display_snapshot.h"
#include "ui/display/manager/display_change_observer.h"
#include "ui/display/manager/display_layout_store.h"
#include "ui/display/manager/display_manager_utilities.h"
#include "ui/display/manager/display_util.h"
#include "ui/display/manager/managed_display_info.h"
#include "ui/display/manager/test/touch_device_manager_test_api.h"
#include "ui/display/screen.h"
#include "ui/display/test/display_manager_test_api.h"
#include "ui/display/types/display_constants.h"
#include "ui/events/devices/touchscreen_device.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/font_render_params.h"
namespace ash {
using std::string;
using std::vector;
using base::StringPrintf;
namespace {
std::string ToDisplayName(int64_t id) {
return "x-" + base::NumberToString(id);
}
} // namespace
class DisplayManagerTest : public AshTestBase,
public display::DisplayObserver,
public aura::WindowObserver {
public:
DisplayManagerTest() = default;
~DisplayManagerTest() override = default;
void SetUp() override {
AshTestBase::SetUp();
display_observer_.emplace(this);
Shell::GetPrimaryRootWindow()->AddObserver(this);
}
void TearDown() override {
Shell::GetPrimaryRootWindow()->RemoveObserver(this);
display_observer_.reset();
AshTestBase::TearDown();
}
const vector<display::Display>& changed() const { return changed_; }
const vector<display::Display>& added() const { return added_; }
uint32_t changed_metrics() const { return changed_metrics_; }
string GetCountSummary() const {
return StringPrintf("%" PRIuS " %" PRIuS " %" PRIuS " %" PRIuS " %" PRIuS,
changed_.size(), added_.size(), removed_count_,
will_process_count_, did_process_count_);
}
void reset() {
changed_.clear();
added_.clear();
removed_count_ = will_process_count_ = did_process_count_ = 0U;
changed_metrics_ = 0U;
root_window_destroyed_ = false;
}
bool root_window_destroyed() const { return root_window_destroyed_; }
const display::ManagedDisplayInfo& GetDisplayInfo(
const display::Display& display) {
return display_manager()->GetDisplayInfo(display.id());
}
const display::ManagedDisplayInfo& GetDisplayInfoAt(int index) {
return GetDisplayInfo(display_manager()->GetDisplayAt(index));
}
const display::Display& GetDisplayForId(int64_t id) {
return display_manager()->GetDisplayForId(id);
}
const display::ManagedDisplayInfo& GetDisplayInfoForId(int64_t id) {
return GetDisplayInfo(display_manager()->GetDisplayForId(id));
}
// aura::DisplayObserver overrides:
void OnWillProcessDisplayChanges() override { ++will_process_count_; }
void OnDidProcessDisplayChanges() override { ++did_process_count_; }
void OnDisplayMetricsChanged(const display::Display& display,
uint32_t changed_metrics) override {
changed_.push_back(display);
changed_metrics_ |= changed_metrics;
}
void OnDisplayAdded(const display::Display& new_display) override {
added_.push_back(new_display);
}
void OnDisplayRemoved(const display::Display& old_display) override {
++removed_count_;
}
// aura::WindowObserver overrides:
void OnWindowDestroying(aura::Window* window) override {
ASSERT_EQ(Shell::GetPrimaryRootWindow(), window);
root_window_destroyed_ = true;
}
// Returns true if there exists any overlapping mirroring displays.
bool OverlappingMirroringDisplaysExist() {
const auto& mirroring_displays =
display_manager()->software_mirroring_display_list();
for (size_t i = 0; i < mirroring_displays.size() - 1; ++i) {
for (size_t j = i + 1; j < mirroring_displays.size(); ++j) {
const gfx::Rect& bounds_1 = mirroring_displays[i].bounds();
const gfx::Rect& bounds_2 = mirroring_displays[j].bounds();
if (bounds_1.Intersects(bounds_2))
return true;
}
}
return false;
}
void SetSoftwareMirrorMode(bool active) {
display_manager()->SetMirrorMode(
active ? display::MirrorMode::kNormal : display::MirrorMode::kOff,
absl::nullopt);
base::RunLoop().RunUntilIdle();
}
private:
vector<display::Display> changed_;
vector<display::Display> added_;
size_t removed_count_ = 0u;
size_t will_process_count_ = 0u;
size_t did_process_count_ = 0u;
bool root_window_destroyed_ = false;
uint32_t changed_metrics_ = 0u;
absl::optional<display::ScopedDisplayObserver> display_observer_;
DISALLOW_COPY_AND_ASSIGN(DisplayManagerTest);
};
TEST_F(DisplayManagerTest, UpdateDisplayTest) {
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
// Update primary and add seconary.
UpdateDisplay("100+0-500x400,0+501-400x300");
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 500x400",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("2 1 0 1 1", GetCountSummary());
// Metrics change immediately when new displays set shelf work area insets.
// After that, DisplayManager::OnNativeDisplaysChanged trigger changes of the
// primary display's metrics. So the observed order of changes is [1, 0].
EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[1].id());
EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), changed()[0].id());
EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
EXPECT_EQ("0,0 500x400", changed()[1].bounds().ToString());
EXPECT_EQ("500,0 400x300", changed()[0].bounds().ToString());
// Secondary display is on right.
EXPECT_EQ("500,0 400x300", added()[0].bounds().ToString());
EXPECT_EQ("0,501 400x300",
GetDisplayInfo(added()[0]).bounds_in_native().ToString());
reset();
// Delete secondary.
UpdateDisplay("100+0-500x400");
EXPECT_EQ("0 0 1 1 1", GetCountSummary());
reset();
// Change primary.
UpdateDisplay("1+1-1000x600");
EXPECT_EQ("1 0 0 1 1", 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("1 1 0 1 1", GetCountSummary());
EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), changed()[0].id());
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_native().ToString());
reset();
// Secondary removed, primary changed.
UpdateDisplay("1+1-800x300");
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("1 0 1 1 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<display::ManagedDisplayInfo> empty;
display_manager()->OnNativeDisplaysChanged(empty);
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
// Going to 0 displays doesn't actually change the list and is effectively
// ignored.
EXPECT_EQ("0 0 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 1 1", 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_native().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_native().ToString());
reset();
// Changing primary will update secondary as well.
UpdateDisplay("0+0-800x600,1000+1000-600x400");
EXPECT_EQ("2 0 0 1 1", GetCountSummary());
reset();
EXPECT_EQ("0,0 800x600",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("800,0 600x400",
display_manager()->GetDisplayAt(1).bounds().ToString());
}
// Test in emulation mode (use_fullscreen_host_window=false)
TEST_F(DisplayManagerTest, EmulatorTest) {
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
display_manager()->AddRemoveDisplay();
// Update primary and add seconary.
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("1 1 0 1 1", GetCountSummary());
reset();
display_manager()->AddRemoveDisplay();
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
EXPECT_EQ("0 0 1 1 1", GetCountSummary());
reset();
display_manager()->AddRemoveDisplay();
EXPECT_EQ(2U, display_manager()->GetNumDisplays());
EXPECT_EQ("1 1 0 1 1", GetCountSummary());
}
// Tests support for 3 displays.
TEST_F(DisplayManagerTest, UpdateThreeDisplaysWithDefaultLayout) {
EXPECT_EQ(1U, display_manager()->GetNumDisplays());
// Test with three displays. Native origin will not affect ash
// display layout.
UpdateDisplay("0+0-640x480,1000+0-320x200,2000+0-400x300");
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("640,0 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
EXPECT_EQ("960,0 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
EXPECT_EQ("3 2 0 1 1", GetCountSummary());
// Metrics change immediately when new displays set shelf work area insets.
// After that, DisplayManager::OnNativeDisplaysChanged trigger changes of the
// primary display's metrics. So the observed order of changes is [1, 2, 0].
EXPECT_EQ(display_manager()->GetDisplayAt(0).id(), changed()[2].id());
EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), changed()[0].id());
EXPECT_EQ(display_manager()->GetDisplayAt(2).id(), changed()[1].id());
EXPECT_EQ(display_manager()->GetDisplayAt(1).id(), added()[0].id());
EXPECT_EQ(display_manager()->GetDisplayAt(2).id(), added()[1].id());
EXPECT_EQ("0,0 640x480", changed()[2].bounds().ToString());
EXPECT_EQ("640,0 320x200", changed()[0].bounds().ToString());
EXPECT_EQ("960,0 400x300", changed()[1].bounds().ToString());
// Secondary and terniary displays are on right.
EXPECT_EQ("640,0 320x200", added()[0].bounds().ToString());
EXPECT_EQ("1000,0 320x200",
GetDisplayInfo(added()[0]).bounds_in_native().ToString());
EXPECT_EQ("960,0 400x300", added()[1].bounds().ToString());
EXPECT_EQ("2000,0 400x300",
GetDisplayInfo(added()[1]).bounds_in_native().ToString());
// Verify calling ReconfigureDisplays doesn't change anything.
display_manager()->ReconfigureDisplays();
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("640,0 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
EXPECT_EQ("960,0 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
display::DisplayPlacement default_placement(display::DisplayPlacement::BOTTOM,
10);
display_manager()->layout_store()->SetDefaultDisplayPlacement(
default_placement);
// Test with new displays.
UpdateDisplay("640x480");
UpdateDisplay("640x480,320x200,400x300");
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("10,480 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
EXPECT_EQ("20,680 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
}
TEST_F(DisplayManagerTest, LayoutMorethanThreeDisplaysTest) {
int64_t primary_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 3);
{
// Layout: [2]
// [1][P]
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::LEFT, 10);
builder.AddDisplayPlacement(list[2], list[1],
display::DisplayPlacement::TOP, 10);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("640x480,320x200,400x300");
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("-320,10 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
// The above layout causes an overlap between [P] and [2], making [2]'s
// bounds be "-310,-290 400x300" if the overlap is not fixed. The overlap
// must be detected and fixed and [2] is shifted up to remove the overlap.
EXPECT_EQ("-310,-300 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
}
{
// Layout: [1]
// [P][2]
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::TOP, 10);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::RIGHT, 10);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("640x480,320x200,400x300");
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("10,-200 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
EXPECT_EQ("640,10 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
}
{
// Layout: [P]
// [2]
// [1]
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], list[2],
display::DisplayPlacement::BOTTOM, 10);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::BOTTOM, 10);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("640x480,320x200,400x300");
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
EXPECT_EQ("20,780 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
EXPECT_EQ("10,480 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
}
{
list = display::test::CreateDisplayIdListN(primary_id, 5);
// Layout: [P][2]
// [3][4]
// [1]
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::RIGHT, 10);
builder.AddDisplayPlacement(list[1], list[3],
display::DisplayPlacement::BOTTOM, 10);
builder.AddDisplayPlacement(list[3], list[4],
display::DisplayPlacement::LEFT, 10);
builder.AddDisplayPlacement(list[4], primary_id,
display::DisplayPlacement::BOTTOM, 10);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("640x480,320x200,400x300,300x200,200x100");
EXPECT_EQ(5U, display_manager()->GetNumDisplays());
EXPECT_EQ("0,0 640x480",
display_manager()->GetDisplayAt(0).bounds().ToString());
// 2nd is right of the primary.
EXPECT_EQ("640,10 400x300",
display_manager()->GetDisplayAt(2).bounds().ToString());
// 4th is bottom of the primary.
EXPECT_EQ("10,480 200x100",
display_manager()->GetDisplayAt(4).bounds().ToString());
// 3rd is the left of 4th.
EXPECT_EQ("-290,480 300x200",
display_manager()->GetDisplayAt(3).bounds().ToString());
// 1st is the bottom of 3rd.
EXPECT_EQ("-280,680 320x200",
display_manager()->GetDisplayAt(1).bounds().ToString());
}
}
// Makes sure that layouts with overlapped displays are detected and fixed when
// applied.
TEST_F(DisplayManagerTest, NoOverlappedDisplays) {
int64_t primary_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
{
// Layout with multiple overlaps and special cases:
//
// +-----+
// +----+-+6 |
// | 5 | | |
// +----+----+ | |
// | 7 | | | |
// +----+----+-+---+---+-+---------+
// | | P | | 2 |
// +------+ | | +----------+
// | | | | 3 |
// | | | | |
// +--+----+-+-+-+-----+--+ |
// | 1 | | 4 | | |
// | | | +--+-------+
// | | | |
// +--------+ +--------+
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 8);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::BOTTOM, 50);
builder.AddDisplayPlacement(list[2], list[1],
display::DisplayPlacement::TOP, 300);
builder.AddDisplayPlacement(list[3], list[2],
display::DisplayPlacement::RIGHT, 30);
builder.AddDisplayPlacement(list[4], list[2],
display::DisplayPlacement::BOTTOM, 400);
builder.AddDisplayPlacement(list[5], primary_id,
display::DisplayPlacement::LEFT, -300);
builder.AddDisplayPlacement(list[6], primary_id,
display::DisplayPlacement::TOP, -250);
builder.AddDisplayPlacement(list[7], list[6],
display::DisplayPlacement::LEFT, 250);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay(
"480x400,480x400,480x400,480x400,480x400,480x400,480x400,530x150");
// The resulting layout after overlaps had been removed:
//
//
// +---------+
// | 7 +-----+
// +-+-------+ 6 |
// | 5 | |
// | | |
// | | |
// | |-+---+----+---------+
// | | | P | 2 |
// +-------+ | | +----------+
// | | | 3 |
// | | | |
// +--+-----+-+-------+ |
// | 1 | | |
// | | +----+---+------+
// | | | 4 |
// +-------+ | |
// | |
// +--------+
EXPECT_EQ(8U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 480, 400),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(50, 400, 480, 400),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(480, 0, 480, 400),
display_manager()->GetDisplayAt(2).bounds());
EXPECT_EQ(gfx::Rect(960, 30, 480, 400),
display_manager()->GetDisplayAt(3).bounds());
EXPECT_EQ(gfx::Rect(730, 430, 480, 400),
display_manager()->GetDisplayAt(4).bounds());
EXPECT_EQ(gfx::Rect(-730, -300, 480, 400),
display_manager()->GetDisplayAt(5).bounds());
EXPECT_EQ(gfx::Rect(-250, -400, 480, 400),
display_manager()->GetDisplayAt(6).bounds());
EXPECT_EQ(gfx::Rect(-780, -450, 530, 150),
display_manager()->GetDisplayAt(7).bounds());
// Expect that the displays have been reparented correctly, such that a
// child is always touching its parent.
display::DisplayLayoutBuilder expected_layout_builder(primary_id);
expected_layout_builder.AddDisplayPlacement(
list[1], primary_id, display::DisplayPlacement::BOTTOM, 50);
expected_layout_builder.AddDisplayPlacement(
list[2], list[1], display::DisplayPlacement::TOP, 430);
expected_layout_builder.AddDisplayPlacement(
list[3], list[2], display::DisplayPlacement::RIGHT, 30);
// [4] became a child of [3] instead of [2] as they no longer touch.
expected_layout_builder.AddDisplayPlacement(
list[4], list[3], display::DisplayPlacement::BOTTOM, -230);
// [5] became a child of [6] instead of [P] as they no longer touch.
expected_layout_builder.AddDisplayPlacement(
list[5], list[6], display::DisplayPlacement::LEFT, 100);
expected_layout_builder.AddDisplayPlacement(
list[6], primary_id, display::DisplayPlacement::TOP, -250);
expected_layout_builder.AddDisplayPlacement(
list[7], list[6], display::DisplayPlacement::LEFT, -50);
const display::DisplayLayout& layout =
display_manager()->GetCurrentResolvedDisplayLayout();
EXPECT_TRUE(
layout.HasSamePlacementList(*(expected_layout_builder.Build())));
}
{
// The following is a special case where a child display is closer to the
// origin than its parent. Test that we can handle it successfully without
// introducing a circular dependency.
//
// +---------+
// | P | +---------+
// | | | 3 |
// | | | |
// | | | |
// +------+--+----+ |
// | 1 | | 2 +---------+
// | | | |
// | | | |
// | | | |
// +------+--+----+
//
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 4);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::BOTTOM, 0);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::BOTTOM, 464);
builder.AddDisplayPlacement(list[3], list[2],
display::DisplayPlacement::RIGHT, -700);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("696x800,696x800,300x800,696x800");
// The expected layout should be:
//
// +---------+
// | P | +---------+
// | | | 3 |
// | | | |
// | | | |
// +---------+-------+ |
// | 1 | 2 +---------+
// | | |
// | | |
// | | |
// +---------+-------+
//
//
EXPECT_EQ(4U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 696, 800),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(0, 800, 696, 800),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(696, 800, 300, 800),
display_manager()->GetDisplayAt(2).bounds());
EXPECT_EQ(gfx::Rect(996, 100, 696, 800),
display_manager()->GetDisplayAt(3).bounds());
// This case if not handled correctly might lead to a cyclic dependency.
// Make sure this doesn't happen.
display::DisplayLayoutBuilder expected_layout_builder(primary_id);
expected_layout_builder.AddDisplayPlacement(
list[1], primary_id, display::DisplayPlacement::BOTTOM, 0);
expected_layout_builder.AddDisplayPlacement(
list[2], primary_id, display::DisplayPlacement::BOTTOM, 696);
expected_layout_builder.AddDisplayPlacement(
list[3], list[2], display::DisplayPlacement::RIGHT, -700);
const display::DisplayLayout& layout =
display_manager()->GetCurrentResolvedDisplayLayout();
EXPECT_TRUE(
layout.HasSamePlacementList(*(expected_layout_builder.Build())));
}
{
// The following is a layout with an overlap to the left of the primary
// display.
//
// +---------+---------+
// | 1 | P |
// | | |
// +---------+ |
// | | |
// +---------+---------+
// | 2 |
// | |
// +---------+
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 3);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::LEFT, 0);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::LEFT, 250);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("696x500,696x500,696x500");
// The expected layout should be:
//
// +---------+---------+
// | 1 | P |
// | | |
// | | |
// | | |
// +---------+---------+
// | 2 |
// | |
// | |
// | |
// +---------+
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 696, 500),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(-696, 0, 696, 500),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(-696, 500, 696, 500),
display_manager()->GetDisplayAt(2).bounds());
}
{
// The following is a layout with an overlap occurring above the primary
// display.
//
// +------+--+------+
// | 2 | | 1 |
// | | | |
// | | | |
// | | | |
// +------+--+------+
// | P |
// | |
// | |
// | |
// +---------+
//
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 3);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::TOP, 0);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::TOP, -348);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("696x500,696x500,696x500");
// The expected layout should be:
//
// +---------+---------+
// | 2 | 1 |
// | | |
// | | |
// | | |
// +---------+---------+
// | P |
// | |
// | |
// | |
// +---------+
//
EXPECT_EQ(3U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 696, 500),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(0, -500, 696, 500),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(-696, -500, 696, 500),
display_manager()->GetDisplayAt(2).bounds());
}
}
TEST_F(DisplayManagerTest, NoOverlappedDisplaysNotFitBetweenTwo) {
// +------+--+----+--+------+
// | 1 | | 2 | | 3 |
// | | | | | |
// | | | | | |
// | | | | | |
// +-+----+--+----+--+---+--+
// | P |
// | |
// | |
// | |
// +-------------------+
//
int64_t primary_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 4);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::TOP, -110);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::TOP, 300);
builder.AddDisplayPlacement(list[3], primary_id,
display::DisplayPlacement::TOP, 600);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("1200x500,600x500,600x500,600x500");
// The expected layout should be:
//
// +---------+---------+---------+
// | 1 | 2 | 3 |
// | | | |
// | | | |
// | | | |
// +-+-------+---------+-+-------+
// | P |
// | |
// | |
// | |
// +-------------------+
//
EXPECT_EQ(4U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 1200, 500),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(-110, -500, 600, 500),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(490, -500, 600, 500),
display_manager()->GetDisplayAt(2).bounds());
EXPECT_EQ(gfx::Rect(1090, -500, 600, 500),
display_manager()->GetDisplayAt(3).bounds());
}
TEST_F(DisplayManagerTest, NoOverlappedDisplaysAfterResolutionChange) {
// Starting with a good layout with no overlaps, test that if the resolution
// of one of the displays is changed, it won't result in any overlaps.
//
// +-------------------+
// | 4 |
// | |
// | |
// | |
// +----+----+---------+----+----+
// | 1 | 2 | 3 |
// | | | |
// | | | |
// | | | |
// +----+----+---------+----+----+
// | p |
// | |
// | |
// | |
// +-------------------+
//
int64_t primary_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 5);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::TOP, -250);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::TOP, 250);
builder.AddDisplayPlacement(list[3], primary_id,
display::DisplayPlacement::TOP, 750);
builder.AddDisplayPlacement(list[4], list[1], display::DisplayPlacement::TOP,
250);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("1000x500,600x500,600x500,600x500,1000x500");
// There should be no overlap at all.
EXPECT_EQ(5U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 1000, 500),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(-250, -500, 600, 500),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(350, -500, 600, 500),
display_manager()->GetDisplayAt(2).bounds());
EXPECT_EQ(gfx::Rect(950, -500, 600, 500),
display_manager()->GetDisplayAt(3).bounds());
EXPECT_EQ(gfx::Rect(0, -1000, 1000, 500),
display_manager()->GetDisplayAt(4).bounds());
// Change the resolution of display (2) and expect the following layout.
//
// +-------------------+
// | 4 |
// | |
// | |
// | |
// +----+-------------++
// | 2 |
// +---------+ +---------+
// | 1 | | 3 |
// | | | |
// | | | |
// | | | |
// +----+----+-------------++--------+
// | p |
// | |
// | |
// | |
// +-------------------+
//
UpdateDisplay("1000x500,600x500,600x700,600x500,1000x500");
EXPECT_EQ(5U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 1000, 500),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(-250, -500, 600, 500),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(350, -700, 600, 700),
display_manager()->GetDisplayAt(2).bounds());
EXPECT_EQ(gfx::Rect(950, -500, 600, 500),
display_manager()->GetDisplayAt(3).bounds());
EXPECT_EQ(gfx::Rect(0, -1200, 1000, 500),
display_manager()->GetDisplayAt(4).bounds());
}
TEST_F(DisplayManagerTest, NoOverlappedDisplaysWithDetachedDisplays) {
// Detached displays that intersect other non-detached displays.
//
// +---------+---------+---------+
// | 1 | 2 | 3 |
// | | | |
// | | | |
// | | | |
// +----+----+-----+---+----+----+
// | 4, 5 | P |
// | detached | |
// | | |
// +----------+ |
// +-------------------+
//
int64_t primary_id = display::Screen::GetScreen()->GetPrimaryDisplay().id();
display::DisplayIdList list =
display::test::CreateDisplayIdListN(primary_id, 6);
display::DisplayLayoutBuilder builder(primary_id);
builder.AddDisplayPlacement(list[1], primary_id,
display::DisplayPlacement::TOP, -250);
builder.AddDisplayPlacement(list[2], primary_id,
display::DisplayPlacement::TOP, 250);
builder.AddDisplayPlacement(list[3], primary_id,
display::DisplayPlacement::TOP, 750);
display_manager()->layout_store()->RegisterLayoutForDisplayIdList(
list, builder.Build());
UpdateDisplay("1000x500,600x500,600x500,600x500,500x400,500x400");
// Detached displays will be de-intersected and reparented appropriately.
//
// +---------+---------+---------+
// | 1 | 2 | 3 |
// | | | |
// | | | |
// | | | |
// +----+----+---------+----+----+
// | P |
// | |
// | |
// | |
// +----------+--------+
// | 4 |
// | |
// | |
// +----------+
// | 5 |
// | |
// | |
// +----------+
//
EXPECT_EQ(6U, display_manager()->GetNumDisplays());
EXPECT_EQ(gfx::Rect(0, 0, 1000, 500),
display_manager()->GetDisplayAt(0).bounds());
EXPECT_EQ(gfx::Rect(-250, -500, 600, 500),
display_manager()->GetDisplayAt(1).bounds());
EXPECT_EQ(gfx::Rect(350, -500, 600, 500),
display_manager()->GetDisplayAt(2).bounds());
EXPECT_EQ(gfx::Rect(950, -500, 600, 500),
display_manager()->GetDisplayAt(3).bounds());
EXPECT_EQ(gfx::Rect(0, 500, 500, 400),
display_manager()->GetDisplayAt(4).bounds());
EXPECT_EQ(gfx::Rect(0, 900, 500, 400),
display_manager()->GetDisplayAt(5).bounds());
// This case if not handled correctly might lead to a cyclic dependency.
// Make sure this doesn't happen.
display::DisplayLayoutBuilder expected_layout_builder(primary_id);
expected_layout_builder.AddDisplayPlacement(
list[1], primary_id, display::DisplayPlacement::TOP, -250);
expected_layout_builder.AddDisplayPlacement(
list[2], primary_id, display::DisplayPlacement::TOP, 350);
expected_layout_builder.AddDisplayPlacement(
list[3], primary_id, display::DisplayPlacement::TOP, 950);
expected_layout_builder.AddDisplayPlacement(