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_manager.h"
12 #include "ash/display/display_util.h"
13 #include "ash/display/extended_mouse_warp_controller.h"
14 #include "ash/display/mouse_cursor_event_filter.h"
15 #include "ash/display/unified_mouse_warp_controller.h"
16 #include "ash/shell.h"
17 #include "base/command_line.h"
18 #include "base/strings/string_split.h"
19 #include "ui/aura/env.h"
20 #include "ui/aura/window_event_dispatcher.h"
21 #include "ui/events/test/event_generator.h"
22 #include "ui/gfx/display.h"
26 typedef std::vector
<gfx::Display
> DisplayList
;
27 typedef DisplayInfo DisplayInfo
;
28 typedef std::vector
<DisplayInfo
> DisplayInfoList
;
32 std::vector
<DisplayInfo
> CreateDisplayInfoListFromString(
33 const std::string specs
,
34 DisplayManager
* display_manager
) {
35 std::vector
<DisplayInfo
> display_info_list
;
36 std::vector
<std::string
> parts
;
37 base::SplitString(specs
, ',', &parts
);
40 DisplayManager::DisplayList list
=
41 display_manager
->IsInUnifiedMode()
42 ? display_manager
->software_mirroring_display_list()
43 : display_manager
->active_display_list();
45 for (std::vector
<std::string
>::const_iterator iter
= parts
.begin();
46 iter
!= parts
.end(); ++iter
, ++index
) {
47 int64 id
= (index
< list
.size()) ? list
[index
].id()
48 : gfx::Display::kInvalidDisplayID
;
49 display_info_list
.push_back(
50 DisplayInfo::CreateFromSpecWithID(*iter
, id
));
52 return display_info_list
;
58 bool DisplayManagerTestApi::TestIfMouseWarpsAt(
59 ui::test::EventGenerator
& event_generator
,
60 const gfx::Point
& point_in_screen
) {
61 aura::Window
* context
= Shell::GetAllRootWindows()[0];
62 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
63 if (display_manager
->IsInUnifiedMode()) {
64 static_cast<UnifiedMouseWarpController
*>(
66 ->mouse_cursor_filter()
67 ->mouse_warp_controller_for_test())
68 ->allow_non_native_event_for_test();
69 int orig_index
= FindDisplayIndexContainingPoint(
70 display_manager
->software_mirroring_display_list(), point_in_screen
);
73 event_generator
.MoveMouseTo(point_in_screen
);
75 int new_index
= FindDisplayIndexContainingPoint(
76 display_manager
->software_mirroring_display_list(),
77 aura::Env::GetInstance()->last_mouse_location());
80 return orig_index
!= new_index
;
82 static_cast<ExtendedMouseWarpController
*>(
84 ->mouse_cursor_filter()
85 ->mouse_warp_controller_for_test())
86 ->allow_non_native_event_for_test();
87 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(context
);
88 gfx::Display original_display
=
89 screen
->GetDisplayNearestPoint(point_in_screen
);
90 event_generator
.MoveMouseTo(point_in_screen
);
91 return original_display
.id() !=
92 screen
->GetDisplayNearestPoint(
93 aura::Env::GetInstance()->last_mouse_location()).id();
98 void DisplayManagerTestApi::EnableUnifiedDesktopForTest() {
99 #if defined(OS_CHROMEOS)
100 base::CommandLine::ForCurrentProcess()->AppendSwitch(
101 switches::kAshEnableUnifiedDesktop
);
105 DisplayManagerTestApi::DisplayManagerTestApi(DisplayManager
* display_manager
)
106 : display_manager_(display_manager
) {}
108 DisplayManagerTestApi::~DisplayManagerTestApi() {}
110 void DisplayManagerTestApi::UpdateDisplay(
111 const std::string
& display_specs
) {
112 std::vector
<DisplayInfo
> display_info_list
=
113 CreateDisplayInfoListFromString(display_specs
, display_manager_
);
114 bool is_host_origin_set
= false;
115 for (size_t i
= 0; i
< display_info_list
.size(); ++i
) {
116 const DisplayInfo
& display_info
= display_info_list
[i
];
117 if (display_info
.bounds_in_native().origin() != gfx::Point(0, 0)) {
118 is_host_origin_set
= true;
123 // On non-testing environment, when a secondary display is connected, a new
124 // native (i.e. X) window for the display is always created below the
125 // previous one for GPU performance reasons. Try to emulate the behavior
126 // unless host origins are explicitly set.
127 if (!is_host_origin_set
) {
128 // Sart from (1,1) so that windows won't overlap with native mouse cursor.
129 // See |AshTestBase::SetUp()|.
131 for (std::vector
<DisplayInfo
>::iterator iter
= display_info_list
.begin();
132 iter
!= display_info_list
.end(); ++iter
) {
133 gfx::Rect
bounds(iter
->bounds_in_native().size());
135 bounds
.set_y(next_y
);
136 next_y
+= bounds
.height();
137 iter
->SetBounds(bounds
);
141 display_manager_
->OnNativeDisplaysChanged(display_info_list
);
142 display_manager_
->UpdateInternalDisplayModeListForTest();
145 int64
DisplayManagerTestApi::SetFirstDisplayAsInternalDisplay() {
146 const gfx::Display
& internal
= display_manager_
->active_display_list_
[0];
147 SetInternalDisplayId(internal
.id());
148 return gfx::Display::InternalDisplayId();
151 void DisplayManagerTestApi::SetInternalDisplayId(int64 id
) {
152 gfx::Display::SetInternalDisplayId(id
);
153 display_manager_
->UpdateInternalDisplayModeListForTest();
156 void DisplayManagerTestApi::DisableChangeDisplayUponHostResize() {
157 display_manager_
->set_change_display_upon_host_resize(false);
160 void DisplayManagerTestApi::SetAvailableColorProfiles(
162 const std::vector
<ui::ColorCalibrationProfile
>& profiles
) {
163 display_manager_
->display_info_
[display_id
].set_available_color_profiles(