1 // Copyright (c) 2012 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 "ash/test/display_manager_test_api.h"
9 #include "ash/ash_switches.h"
10 #include "ash/display/display_info.h"
11 #include "ash/display/display_layout_store.h"
12 #include "ash/display/display_manager.h"
13 #include "ash/display/display_util.h"
14 #include "ash/display/extended_mouse_warp_controller.h"
15 #include "ash/display/mouse_cursor_event_filter.h"
16 #include "ash/display/unified_mouse_warp_controller.h"
17 #include "ash/shell.h"
18 #include "base/command_line.h"
19 #include "base/strings/string_split.h"
20 #include "ui/aura/env.h"
21 #include "ui/aura/window_event_dispatcher.h"
22 #include "ui/events/test/event_generator.h"
23 #include "ui/gfx/display.h"
27 typedef std::vector
<gfx::Display
> DisplayList
;
28 typedef DisplayInfo DisplayInfo
;
29 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
33 std::vector
<DisplayInfo
> CreateDisplayInfoListFromString(
34 const std::string specs
,
35 DisplayManager
* display_manager
) {
36 std::vector
<DisplayInfo
> display_info_list
;
37 std::vector
<std::string
> parts
;
38 base::SplitString(specs
, ',', &parts
);
41 DisplayManager::DisplayList list
=
42 display_manager
->IsInUnifiedMode()
43 ? display_manager
->software_mirroring_display_list()
44 : display_manager
->active_display_list();
46 for (std::vector
<std::string
>::const_iterator iter
= parts
.begin();
47 iter
!= parts
.end(); ++iter
, ++index
) {
48 int64 id
= (index
< list
.size()) ? list
[index
].id()
49 : gfx::Display::kInvalidDisplayID
;
50 display_info_list
.push_back(
51 DisplayInfo::CreateFromSpecWithID(*iter
, id
));
53 return display_info_list
;
59 bool DisplayManagerTestApi::TestIfMouseWarpsAt(
60 ui::test::EventGenerator
& event_generator
,
61 const gfx::Point
& point_in_screen
) {
62 aura::Window
* context
= Shell::GetAllRootWindows()[0];
63 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
64 if (display_manager
->IsInUnifiedMode()) {
65 static_cast<UnifiedMouseWarpController
*>(
67 ->mouse_cursor_filter()
68 ->mouse_warp_controller_for_test())
69 ->allow_non_native_event_for_test();
70 int orig_index
= FindDisplayIndexContainingPoint(
71 display_manager
->software_mirroring_display_list(), point_in_screen
);
74 event_generator
.MoveMouseTo(point_in_screen
);
76 int new_index
= FindDisplayIndexContainingPoint(
77 display_manager
->software_mirroring_display_list(),
78 aura::Env::GetInstance()->last_mouse_location());
81 return orig_index
!= new_index
;
83 static_cast<ExtendedMouseWarpController
*>(
85 ->mouse_cursor_filter()
86 ->mouse_warp_controller_for_test())
87 ->allow_non_native_event_for_test();
88 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(context
);
89 gfx::Display original_display
=
90 screen
->GetDisplayNearestPoint(point_in_screen
);
91 event_generator
.MoveMouseTo(point_in_screen
);
92 return original_display
.id() !=
93 screen
->GetDisplayNearestPoint(
94 aura::Env::GetInstance()->last_mouse_location()).id();
99 void DisplayManagerTestApi::EnableUnifiedDesktopForTest() {
100 #if defined(OS_CHROMEOS)
101 base::CommandLine::ForCurrentProcess()->AppendSwitch(
102 switches::kAshEnableUnifiedDesktop
);
106 ->SetDefaultDisplayLayout(DisplayLayout());
110 DisplayManagerTestApi::DisplayManagerTestApi(DisplayManager
* display_manager
)
111 : display_manager_(display_manager
) {}
113 DisplayManagerTestApi::~DisplayManagerTestApi() {}
115 void DisplayManagerTestApi::UpdateDisplay(
116 const std::string
& display_specs
) {
117 std::vector
<DisplayInfo
> display_info_list
=
118 CreateDisplayInfoListFromString(display_specs
, display_manager_
);
119 bool is_host_origin_set
= false;
120 for (size_t i
= 0; i
< display_info_list
.size(); ++i
) {
121 const DisplayInfo
& display_info
= display_info_list
[i
];
122 if (display_info
.bounds_in_native().origin() != gfx::Point(0, 0)) {
123 is_host_origin_set
= true;
128 // On non-testing environment, when a secondary display is connected, a new
129 // native (i.e. X) window for the display is always created below the
130 // previous one for GPU performance reasons. Try to emulate the behavior
131 // unless host origins are explicitly set.
132 if (!is_host_origin_set
) {
133 // Sart from (1,1) so that windows won't overlap with native mouse cursor.
134 // See |AshTestBase::SetUp()|.
136 for (std::vector
<DisplayInfo
>::iterator iter
= display_info_list
.begin();
137 iter
!= display_info_list
.end(); ++iter
) {
138 gfx::Rect
bounds(iter
->bounds_in_native().size());
140 bounds
.set_y(next_y
);
141 next_y
+= bounds
.height();
142 iter
->SetBounds(bounds
);
146 display_manager_
->OnNativeDisplaysChanged(display_info_list
);
147 display_manager_
->UpdateInternalDisplayModeListForTest();
150 int64
DisplayManagerTestApi::SetFirstDisplayAsInternalDisplay() {
151 const gfx::Display
& internal
= display_manager_
->active_display_list_
[0];
152 SetInternalDisplayId(internal
.id());
153 return gfx::Display::InternalDisplayId();
156 void DisplayManagerTestApi::SetInternalDisplayId(int64 id
) {
157 gfx::Display::SetInternalDisplayId(id
);
158 display_manager_
->UpdateInternalDisplayModeListForTest();
161 void DisplayManagerTestApi::DisableChangeDisplayUponHostResize() {
162 display_manager_
->set_change_display_upon_host_resize(false);
165 void DisplayManagerTestApi::SetAvailableColorProfiles(
167 const std::vector
<ui::ColorCalibrationProfile
>& profiles
) {
168 display_manager_
->display_info_
[display_id
].set_available_color_profiles(
172 ScopedDisable125DSFForUIScaling::ScopedDisable125DSFForUIScaling() {
173 DisplayInfo::SetUse125DSFForUIScalingForTest(false);
176 ScopedDisable125DSFForUIScaling::~ScopedDisable125DSFForUIScaling() {
177 DisplayInfo::SetUse125DSFForUIScalingForTest(true);