Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / display / mirror_window_controller_unittest.cc
blob5c2fb01b843555ff43097e56a68c4dbfeb5ce291
1 // Copyright (c) 2013 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/display/mirror_window_controller.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/display/window_tree_host_manager.h"
10 #include "ash/screen_util.h"
11 #include "ash/shell.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/cursor_manager_test_api.h"
14 #include "ash/test/display_manager_test_api.h"
15 #include "ash/test/mirror_window_test_api.h"
16 #include "base/command_line.h"
17 #include "base/strings/stringprintf.h"
18 #include "ui/aura/env.h"
19 #include "ui/aura/test/test_window_delegate.h"
20 #include "ui/aura/test/test_windows.h"
21 #include "ui/aura/window.h"
22 #include "ui/aura/window_event_dispatcher.h"
23 #include "ui/base/hit_test.h"
24 #include "ui/events/test/event_generator.h"
26 namespace ash {
28 namespace {
29 DisplayInfo CreateDisplayInfo(int64 id, const gfx::Rect& bounds) {
30 DisplayInfo info(id, base::StringPrintf("x-%d", static_cast<int>(id)), false);
31 info.SetBounds(bounds);
32 return info;
35 class MirrorOnBootTest : public test::AshTestBase {
36 public:
37 MirrorOnBootTest() {}
38 ~MirrorOnBootTest() override {}
40 void SetUp() override {
41 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
42 switches::kAshHostWindowBounds, "1+1-300x300,1+301-300x300");
43 base::CommandLine::ForCurrentProcess()->AppendSwitch(
44 switches::kAshEnableSoftwareMirroring);
45 test::AshTestBase::SetUp();
47 void TearDown() override { test::AshTestBase::TearDown(); }
49 private:
50 DISALLOW_COPY_AND_ASSIGN(MirrorOnBootTest);
55 typedef test::AshTestBase MirrorWindowControllerTest;
57 #if defined(OS_WIN)
58 // Software mirroring does not work on win.
59 #define MAYBE_MirrorCursorBasic DISABLED_MirrorCursorBasic
60 #define MAYBE_MirrorCursorLocations DISABLED_MirrorCursorLocations
61 #define MAYBE_MirrorCursorMoveOnEnter DISABLED_MirrorCursorMoveOnEnter
62 #define MAYBE_MirrorCursorRotate DISABLED_MirrorCursorRotate
63 #define MAYBE_DockMode DISABLED_DockMode
64 #define MAYBE_MirrorOnBoot DISABLED_MirrorOnBoot
65 #else
66 #define MAYBE_MirrorCursorBasic MirrorCursorBasic
67 #define MAYBE_MirrorCursorLocations MirrorCursorLocations
68 #define MAYBE_MirrorCursorMoveOnEnter MirrorCursorMoveOnEnter
69 #define MAYBE_MirrorCursorRotate MirrorCursorRotate
70 #define MAYBE_DockMode DockMode
71 #define MAYBE_MirrorOnBoot MirrorOnBoot
72 #endif
74 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorBasic) {
75 test::MirrorWindowTestApi test_api;
76 aura::test::TestWindowDelegate test_window_delegate;
77 test_window_delegate.set_window_component(HTTOP);
79 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
80 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
81 UpdateDisplay("400x400,400x400");
82 aura::Window* root = Shell::GetInstance()->GetPrimaryRootWindow();
83 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
84 &test_window_delegate,
86 gfx::Rect(50, 50, 100, 100),
87 root));
88 window->Show();
89 window->SetName("foo");
91 EXPECT_TRUE(test_api.GetCursorWindow());
92 EXPECT_EQ("50,50 100x100", window->bounds().ToString());
94 ui::test::EventGenerator generator(root);
95 generator.MoveMouseTo(10, 10);
97 // Test if cursor movement is propertly reflected in mirror window.
98 EXPECT_EQ("4,4", test_api.GetCursorHotPoint().ToString());
99 EXPECT_EQ("10,10",
100 test_api.GetCursorHotPointLocationInRootWindow().ToString());
101 EXPECT_EQ(ui::kCursorNull, test_api.GetCurrentCursorType());
102 EXPECT_TRUE(test_api.GetCursorWindow()->IsVisible());
104 // Test if cursor type change is propertly reflected in mirror window.
105 generator.MoveMouseTo(100, 100);
106 EXPECT_EQ("100,100",
107 test_api.GetCursorHotPointLocationInRootWindow().ToString());
108 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
110 // Test if visibility change is propertly reflected in mirror window.
111 // A key event hides cursor.
112 generator.PressKey(ui::VKEY_A, 0);
113 generator.ReleaseKey(ui::VKEY_A, 0);
114 EXPECT_FALSE(test_api.GetCursorWindow()->IsVisible());
116 // Mouse event makes it visible again.
117 generator.MoveMouseTo(300, 300);
118 EXPECT_EQ("300,300",
119 test_api.GetCursorHotPointLocationInRootWindow().ToString());
120 EXPECT_EQ(ui::kCursorNull, test_api.GetCurrentCursorType());
121 EXPECT_TRUE(test_api.GetCursorWindow()->IsVisible());
124 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorRotate) {
125 test::MirrorWindowTestApi test_api;
126 aura::test::TestWindowDelegate test_window_delegate;
127 test_window_delegate.set_window_component(HTTOP);
129 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
130 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
131 UpdateDisplay("400x400,400x400");
132 aura::Window* root = Shell::GetInstance()->GetPrimaryRootWindow();
133 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
134 &test_window_delegate,
136 gfx::Rect(50, 50, 100, 100),
137 root));
138 window->Show();
139 window->SetName("foo");
141 EXPECT_TRUE(test_api.GetCursorWindow());
142 EXPECT_EQ("50,50 100x100", window->bounds().ToString());
144 ui::test::EventGenerator generator(root);
145 generator.MoveMouseToInHost(100, 100);
147 // Test if cursor movement is propertly reflected in mirror window.
148 EXPECT_EQ("11,12", test_api.GetCursorHotPoint().ToString());
149 EXPECT_EQ("100,100",
150 test_api.GetCursorHotPointLocationInRootWindow().ToString());
151 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
153 UpdateDisplay("400x400/r,400x400"); // 90 degrees.
154 generator.MoveMouseToInHost(300, 100);
155 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
156 // The size of cursor image is 25x25, so the rotated hot point must
157 // be (25-12, 11).
158 EXPECT_EQ("13,11", test_api.GetCursorHotPoint().ToString());
159 EXPECT_EQ("300,100",
160 test_api.GetCursorHotPointLocationInRootWindow().ToString());
162 UpdateDisplay("400x400/u,400x400"); // 180 degrees.
163 generator.MoveMouseToInHost(300, 300);
164 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
165 // Rotated hot point must be (25-11, 25-12).
166 EXPECT_EQ("14,13", test_api.GetCursorHotPoint().ToString());
167 EXPECT_EQ("300,300",
168 test_api.GetCursorHotPointLocationInRootWindow().ToString());
170 UpdateDisplay("400x400/l,400x400"); // 270 degrees.
171 generator.MoveMouseToInHost(100, 300);
172 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
173 // Rotated hot point must be (12, 25-11).
174 EXPECT_EQ("12,14", test_api.GetCursorHotPoint().ToString());
175 EXPECT_EQ("100,300",
176 test_api.GetCursorHotPointLocationInRootWindow().ToString());
179 // Make sure that the mirror cursor's location is same as
180 // the source display's host location in the mirror root window's
181 // coordinates.
182 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorLocations) {
183 test::MirrorWindowTestApi test_api;
184 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
185 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
187 // Test with device scale factor.
188 UpdateDisplay("400x600*2,400x600");
190 aura::Window* root = Shell::GetInstance()->GetPrimaryRootWindow();
191 ui::test::EventGenerator generator(root);
192 generator.MoveMouseToInHost(10, 20);
194 EXPECT_EQ("7,7", test_api.GetCursorHotPoint().ToString());
195 EXPECT_EQ("10,20",
196 test_api.GetCursorHotPointLocationInRootWindow().ToString());
198 // Test with ui scale
199 UpdateDisplay("400x600*0.5,400x600");
200 generator.MoveMouseToInHost(20, 30);
202 EXPECT_EQ("4,4", test_api.GetCursorHotPoint().ToString());
203 EXPECT_EQ("20,30",
204 test_api.GetCursorHotPointLocationInRootWindow().ToString());
206 // Test with rotation
207 UpdateDisplay("400x600/r,400x600");
208 generator.MoveMouseToInHost(30, 40);
210 EXPECT_EQ("21,4", test_api.GetCursorHotPoint().ToString());
211 EXPECT_EQ("30,40",
212 test_api.GetCursorHotPointLocationInRootWindow().ToString());
215 // Test the behavior of the cursor when entering software mirror mode swaps the
216 // cursor's display.
217 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorMoveOnEnter) {
218 aura::Env* env = aura::Env::GetInstance();
219 Shell* shell = Shell::GetInstance();
220 WindowTreeHostManager* window_tree_host_manager =
221 shell->window_tree_host_manager();
223 UpdateDisplay("400x400*2/r,400x400");
224 int64 primary_display_id = window_tree_host_manager->GetPrimaryDisplayId();
225 int64 secondary_display_id = ScreenUtil::GetSecondaryDisplay().id();
226 test::ScopedSetInternalDisplayId set_internal(primary_display_id);
228 // Chrome uses the internal display as the source display for software mirror
229 // mode. Move the cursor to the external display.
230 aura::Window* secondary_root_window =
231 window_tree_host_manager->GetRootWindowForDisplayId(secondary_display_id);
232 secondary_root_window->MoveCursorTo(gfx::Point(100, 200));
233 EXPECT_EQ("300,200", env->last_mouse_location().ToString());
234 test::CursorManagerTestApi cursor_test_api(shell->cursor_manager());
235 EXPECT_EQ(1.0f, cursor_test_api.GetCurrentCursor().device_scale_factor());
236 EXPECT_EQ(gfx::Display::ROTATE_0, cursor_test_api.GetCurrentCursorRotation());
238 shell->display_manager()->SetMultiDisplayMode(DisplayManager::MIRRORING);
239 UpdateDisplay("400x400*2/r,400x400");
241 // Entering mirror mode should have centered the cursor on the primary display
242 // because the cursor's previous position is out of bounds.
243 // Check real cursor's position and properties.
244 EXPECT_EQ("100,100", env->last_mouse_location().ToString());
245 EXPECT_EQ(2.0f, cursor_test_api.GetCurrentCursor().device_scale_factor());
246 EXPECT_EQ(gfx::Display::ROTATE_90,
247 cursor_test_api.GetCurrentCursorRotation());
249 // Check mirrored cursor's location.
250 test::MirrorWindowTestApi test_api;
251 gfx::Point hot_point = test_api.GetCursorHotPoint();
252 // Rotated hot point must be (25-7, 7).
253 EXPECT_EQ("18,7", test_api.GetCursorHotPoint().ToString());
254 // New coordinates are not (200,200) because (200,200) is not the center of
255 // the display.
256 EXPECT_EQ("199,200",
257 test_api.GetCursorHotPointLocationInRootWindow().ToString());
260 // Make sure that the compositor based mirroring can switch
261 // from/to dock mode.
262 TEST_F(MirrorWindowControllerTest, MAYBE_DockMode) {
263 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
264 const int64 internal_id = 1;
265 const int64 external_id = 2;
267 const DisplayInfo internal_display_info =
268 CreateDisplayInfo(internal_id, gfx::Rect(0, 0, 500, 500));
269 const DisplayInfo external_display_info =
270 CreateDisplayInfo(external_id, gfx::Rect(1, 1, 100, 100));
271 std::vector<DisplayInfo> display_info_list;
273 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
275 // software mirroring.
276 display_info_list.push_back(internal_display_info);
277 display_info_list.push_back(external_display_info);
278 display_manager->OnNativeDisplaysChanged(display_info_list);
279 const int64 internal_display_id =
280 test::DisplayManagerTestApi().SetFirstDisplayAsInternalDisplay();
281 EXPECT_EQ(internal_id, internal_display_id);
283 EXPECT_EQ(1U, display_manager->GetNumDisplays());
284 EXPECT_TRUE(display_manager->IsInMirrorMode());
285 EXPECT_EQ(external_id, display_manager->mirroring_display_id());
287 // dock mode.
288 display_info_list.clear();
289 display_info_list.push_back(external_display_info);
290 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
291 display_manager->OnNativeDisplaysChanged(display_info_list);
292 EXPECT_EQ(1U, display_manager->GetNumDisplays());
293 EXPECT_FALSE(display_manager->IsInMirrorMode());
295 // back to software mirroring.
296 display_info_list.clear();
297 display_info_list.push_back(internal_display_info);
298 display_info_list.push_back(external_display_info);
299 display_manager->SetMultiDisplayMode(DisplayManager::MIRRORING);
300 display_manager->OnNativeDisplaysChanged(display_info_list);
301 EXPECT_EQ(1U, display_manager->GetNumDisplays());
302 EXPECT_TRUE(display_manager->IsInMirrorMode());
303 EXPECT_EQ(external_id, display_manager->mirroring_display_id());
306 TEST_F(MirrorOnBootTest, MAYBE_MirrorOnBoot) {
307 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
308 EXPECT_TRUE(display_manager->IsInMirrorMode());
309 RunAllPendingInMessageLoop();
310 test::MirrorWindowTestApi test_api;
311 EXPECT_TRUE(test_api.GetHost());
314 } // namespace ash