Revert of Revert of Revert of Revert of Revert of Enable ServiceWorkerPerfTest (servi...
[chromium-blink-merge.git] / ash / display / mirror_window_controller_unittest.cc
blob15bdefaf5871fa086d2ac579464c12ded8976377
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_controller.h"
9 #include "ash/display/display_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->SetSecondDisplayMode(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->SetSecondDisplayMode(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->SetSecondDisplayMode(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("8,9", 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 DisplayController* display_controller = shell->display_controller();
221 DisplayManager* display_manager = shell->display_manager();
223 UpdateDisplay("400x400*2/r,400x400");
224 int64 primary_display_id = display_controller->GetPrimaryDisplayId();
225 int64 secondary_display_id = ScreenUtil::GetSecondaryDisplay().id();
226 test::DisplayManagerTestApi(display_manager)
227 .SetInternalDisplayId(primary_display_id);
229 // Chrome uses the internal display as the source display for software mirror
230 // mode. Move the cursor to the external display.
231 aura::Window* secondary_root_window =
232 display_controller->GetRootWindowForDisplayId(secondary_display_id);
233 secondary_root_window->MoveCursorTo(gfx::Point(100, 200));
234 EXPECT_EQ("300,200", env->last_mouse_location().ToString());
235 test::CursorManagerTestApi cursor_test_api(shell->cursor_manager());
236 EXPECT_EQ(1.0f, cursor_test_api.GetCurrentCursor().device_scale_factor());
237 EXPECT_EQ(gfx::Display::ROTATE_0, cursor_test_api.GetCurrentCursorRotation());
239 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
240 UpdateDisplay("400x400*2/r,400x400");
242 // Entering mirror mode should have centered the cursor on the primary display
243 // because the cursor's previous position is out of bounds.
244 // Check real cursor's position and properties.
245 EXPECT_EQ("100,100", env->last_mouse_location().ToString());
246 EXPECT_EQ(2.0f, cursor_test_api.GetCurrentCursor().device_scale_factor());
247 EXPECT_EQ(gfx::Display::ROTATE_90,
248 cursor_test_api.GetCurrentCursorRotation());
250 // Check mirrored cursor's location.
251 test::MirrorWindowTestApi test_api;
252 gfx::Point hot_point = test_api.GetCursorHotPoint();
253 // Rotated hot point must be (25-9, 8).
254 EXPECT_EQ("16,8", test_api.GetCursorHotPoint().ToString());
255 // New coordinates are not (200,200) because (200,200) is not the center of
256 // the display.
257 EXPECT_EQ("199,200",
258 test_api.GetCursorHotPointLocationInRootWindow().ToString());
261 // Make sure that the compositor based mirroring can switch
262 // from/to dock mode.
263 TEST_F(MirrorWindowControllerTest, MAYBE_DockMode) {
264 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
265 const int64 internal_id = 1;
266 const int64 external_id = 2;
268 const DisplayInfo internal_display_info =
269 CreateDisplayInfo(internal_id, gfx::Rect(0, 0, 500, 500));
270 const DisplayInfo external_display_info =
271 CreateDisplayInfo(external_id, gfx::Rect(1, 1, 100, 100));
272 std::vector<DisplayInfo> display_info_list;
274 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
276 // software mirroring.
277 display_info_list.push_back(internal_display_info);
278 display_info_list.push_back(external_display_info);
279 display_manager->OnNativeDisplaysChanged(display_info_list);
280 const int64 internal_display_id =
281 test::DisplayManagerTestApi(display_manager).
282 SetFirstDisplayAsInternalDisplay();
283 EXPECT_EQ(internal_id, internal_display_id);
285 EXPECT_EQ(1U, display_manager->GetNumDisplays());
286 EXPECT_TRUE(display_manager->IsMirrored());
287 EXPECT_EQ(external_id, display_manager->mirrored_display_id());
289 // dock mode.
290 display_info_list.clear();
291 display_info_list.push_back(external_display_info);
292 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
293 display_manager->OnNativeDisplaysChanged(display_info_list);
294 EXPECT_EQ(1U, display_manager->GetNumDisplays());
295 EXPECT_FALSE(display_manager->IsMirrored());
297 // back to software mirroring.
298 display_info_list.clear();
299 display_info_list.push_back(internal_display_info);
300 display_info_list.push_back(external_display_info);
301 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
302 display_manager->OnNativeDisplaysChanged(display_info_list);
303 EXPECT_EQ(1U, display_manager->GetNumDisplays());
304 EXPECT_TRUE(display_manager->IsMirrored());
305 EXPECT_EQ(external_id, display_manager->mirrored_display_id());
308 TEST_F(MirrorOnBootTest, MAYBE_MirrorOnBoot) {
309 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
310 EXPECT_TRUE(display_manager->IsMirrored());
311 RunAllPendingInMessageLoop();
312 test::MirrorWindowTestApi test_api;
313 EXPECT_TRUE(test_api.GetHost());
316 } // namespace ash