Rename initial_pos to initial_rect in ShowWidget and ShowView IPCs.
[chromium-blink-merge.git] / ash / display / mirror_window_controller_unittest.cc
blob06e233eb86eb692c20b17ab575e20d7c3c51f466
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/shell.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/test/display_manager_test_api.h"
12 #include "ash/test/mirror_window_test_api.h"
13 #include "base/command_line.h"
14 #include "base/strings/stringprintf.h"
15 #include "ui/aura/test/test_window_delegate.h"
16 #include "ui/aura/test/test_windows.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_event_dispatcher.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/events/test/event_generator.h"
22 namespace ash {
24 namespace {
25 DisplayInfo CreateDisplayInfo(int64 id, const gfx::Rect& bounds) {
26 DisplayInfo info(id, base::StringPrintf("x-%d", static_cast<int>(id)), false);
27 info.SetBounds(bounds);
28 return info;
31 class MirrorOnBootTest : public test::AshTestBase {
32 public:
33 MirrorOnBootTest() {}
34 ~MirrorOnBootTest() override {}
36 void SetUp() override {
37 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
38 switches::kAshHostWindowBounds, "1+1-300x300,1+301-300x300");
39 base::CommandLine::ForCurrentProcess()->AppendSwitch(
40 switches::kAshEnableSoftwareMirroring);
41 test::AshTestBase::SetUp();
43 void TearDown() override { test::AshTestBase::TearDown(); }
45 private:
46 DISALLOW_COPY_AND_ASSIGN(MirrorOnBootTest);
51 typedef test::AshTestBase MirrorWindowControllerTest;
53 #if defined(OS_WIN)
54 // Software mirroring does not work on win.
55 #define MAYBE_MirrorCursorBasic DISABLED_MirrorCursorBasic
56 #define MAYBE_MirrorCursorLocations DISABLED_MirrorCursorLocations
57 #define MAYBE_MirrorCursorRotate DISABLED_MirrorCursorRotate
58 #define MAYBE_DockMode DISABLED_DockMode
59 #define MAYBE_MirrorOnBoot DISABLED_MirrorOnBoot
60 #else
61 #define MAYBE_MirrorCursorBasic MirrorCursorBasic
62 #define MAYBE_MirrorCursorLocations MirrorCursorLocations
63 #define MAYBE_MirrorCursorRotate MirrorCursorRotate
64 #define MAYBE_DockMode DockMode
65 #define MAYBE_MirrorOnBoot MirrorOnBoot
66 #endif
68 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorBasic) {
69 test::MirrorWindowTestApi test_api;
70 aura::test::TestWindowDelegate test_window_delegate;
71 test_window_delegate.set_window_component(HTTOP);
73 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
74 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
75 UpdateDisplay("400x400,400x400");
76 aura::Window* root = Shell::GetInstance()->GetPrimaryRootWindow();
77 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
78 &test_window_delegate,
80 gfx::Rect(50, 50, 100, 100),
81 root));
82 window->Show();
83 window->SetName("foo");
85 EXPECT_TRUE(test_api.GetCursorWindow());
86 EXPECT_EQ("50,50 100x100", window->bounds().ToString());
88 ui::test::EventGenerator generator(root);
89 generator.MoveMouseTo(10, 10);
91 // Test if cursor movement is propertly reflected in mirror window.
92 gfx::Point hot_point = test_api.GetCursorHotPoint();
93 gfx::Point cursor_window_origin =
94 test_api.GetCursorWindow()->bounds().origin();
95 EXPECT_EQ("4,4", hot_point.ToString());
96 EXPECT_EQ(10 - hot_point.x(), cursor_window_origin.x());
97 EXPECT_EQ(10 - hot_point.y(), cursor_window_origin.y());
98 EXPECT_EQ(ui::kCursorNull, test_api.GetCurrentCursorType());
99 EXPECT_TRUE(test_api.GetCursorWindow()->IsVisible());
101 // Test if cursor type change is propertly reflected in mirror window.
102 generator.MoveMouseTo(100, 100);
103 hot_point = test_api.GetCursorHotPoint();
104 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
105 EXPECT_EQ(100 - hot_point.x(), cursor_window_origin.x());
106 EXPECT_EQ(100 - hot_point.y(), cursor_window_origin.y());
107 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
109 // Test if visibility change is propertly reflected in mirror window.
110 // A key event hides cursor.
111 generator.PressKey(ui::VKEY_A, 0);
112 generator.ReleaseKey(ui::VKEY_A, 0);
113 EXPECT_FALSE(test_api.GetCursorWindow()->IsVisible());
115 // Mouse event makes it visible again.
116 generator.MoveMouseTo(300, 300);
117 hot_point = test_api.GetCursorHotPoint();
118 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
119 EXPECT_EQ(300 - hot_point.x(), cursor_window_origin.x());
120 EXPECT_EQ(300 - hot_point.y(), cursor_window_origin.y());
121 EXPECT_EQ(ui::kCursorNull, test_api.GetCurrentCursorType());
122 EXPECT_TRUE(test_api.GetCursorWindow()->IsVisible());
125 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorRotate) {
126 test::MirrorWindowTestApi test_api;
127 aura::test::TestWindowDelegate test_window_delegate;
128 test_window_delegate.set_window_component(HTTOP);
130 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
131 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
132 UpdateDisplay("400x400,400x400");
133 aura::Window* root = Shell::GetInstance()->GetPrimaryRootWindow();
134 scoped_ptr<aura::Window> window(aura::test::CreateTestWindowWithDelegate(
135 &test_window_delegate,
137 gfx::Rect(50, 50, 100, 100),
138 root));
139 window->Show();
140 window->SetName("foo");
142 EXPECT_TRUE(test_api.GetCursorWindow());
143 EXPECT_EQ("50,50 100x100", window->bounds().ToString());
145 ui::test::EventGenerator generator(root);
146 generator.MoveMouseToInHost(100, 100);
148 // Test if cursor movement is propertly reflected in mirror window.
149 gfx::Point hot_point = test_api.GetCursorHotPoint();
150 gfx::Point cursor_window_origin =
151 test_api.GetCursorWindow()->bounds().origin();
152 EXPECT_EQ("11,12", hot_point.ToString());
153 EXPECT_EQ(100 - hot_point.x(), cursor_window_origin.x());
154 EXPECT_EQ(100 - hot_point.y(), cursor_window_origin.y());
155 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
157 UpdateDisplay("400x400/r,400x400"); // 90 degrees.
158 generator.MoveMouseToInHost(300, 100);
159 hot_point = test_api.GetCursorHotPoint();
160 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
161 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
162 // The size of cursor image is 25x25, so the rotated hot point must
163 // be (25-12, 11).
164 EXPECT_EQ("13,11", hot_point.ToString());
165 EXPECT_EQ(300 - hot_point.x(), cursor_window_origin.x());
166 EXPECT_EQ(100 - hot_point.y(), cursor_window_origin.y());
168 UpdateDisplay("400x400/u,400x400"); // 180 degrees.
169 generator.MoveMouseToInHost(300, 300);
170 hot_point = test_api.GetCursorHotPoint();
171 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
172 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
173 // Rotated hot point must be (25-11, 25-12).
174 EXPECT_EQ("14,13", hot_point.ToString());
175 EXPECT_EQ(300 - hot_point.x(), cursor_window_origin.x());
176 EXPECT_EQ(300 - hot_point.y(), cursor_window_origin.y());
178 UpdateDisplay("400x400/l,400x400"); // 270 degrees.
179 generator.MoveMouseToInHost(100, 300);
180 hot_point = test_api.GetCursorHotPoint();
181 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
182 EXPECT_EQ(ui::kCursorNorthResize, test_api.GetCurrentCursorType());
183 // Rotated hot point must be (12, 25-11).
184 EXPECT_EQ("12,14", hot_point.ToString());
185 EXPECT_EQ(100 - hot_point.x(), cursor_window_origin.x());
186 EXPECT_EQ(300 - hot_point.y(), cursor_window_origin.y());
189 // Make sure that the mirror cursor's location is same as
190 // the source display's host location in the mirror root window's
191 // coordinates.
192 TEST_F(MirrorWindowControllerTest, MAYBE_MirrorCursorLocations) {
193 test::MirrorWindowTestApi test_api;
194 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
195 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
197 // Test with device scale factor.
198 UpdateDisplay("400x600*2,400x600");
200 aura::Window* root = Shell::GetInstance()->GetPrimaryRootWindow();
201 ui::test::EventGenerator generator(root);
202 generator.MoveMouseToInHost(10, 20);
204 gfx::Point hot_point = test_api.GetCursorHotPoint();
205 EXPECT_EQ("8,9", hot_point.ToString());
206 gfx::Point cursor_window_origin =
207 test_api.GetCursorWindow()->bounds().origin();
208 EXPECT_EQ(10 - hot_point.x(), cursor_window_origin.x());
209 EXPECT_EQ(20 - hot_point.y(), cursor_window_origin.y());
211 // Test with ui scale
212 UpdateDisplay("400x600*0.5,400x600");
213 generator.MoveMouseToInHost(20, 30);
215 hot_point = test_api.GetCursorHotPoint();
216 EXPECT_EQ("4,4", hot_point.ToString());
217 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
218 EXPECT_EQ(20 - hot_point.x(), cursor_window_origin.x());
219 EXPECT_EQ(30 - hot_point.y(), cursor_window_origin.y());
221 // Test with rotation
222 UpdateDisplay("400x600/r,400x600");
223 generator.MoveMouseToInHost(30, 40);
225 hot_point = test_api.GetCursorHotPoint();
226 EXPECT_EQ("21,4", hot_point.ToString());
227 cursor_window_origin = test_api.GetCursorWindow()->bounds().origin();
228 EXPECT_EQ(30 - hot_point.x(), cursor_window_origin.x());
229 EXPECT_EQ(40 - hot_point.y(), cursor_window_origin.y());
232 // Make sure that the compositor based mirroring can switch
233 // from/to dock mode.
234 TEST_F(MirrorWindowControllerTest, MAYBE_DockMode) {
235 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
236 const int64 internal_id = 1;
237 const int64 external_id = 2;
239 const DisplayInfo internal_display_info =
240 CreateDisplayInfo(internal_id, gfx::Rect(0, 0, 500, 500));
241 const DisplayInfo external_display_info =
242 CreateDisplayInfo(external_id, gfx::Rect(1, 1, 100, 100));
243 std::vector<DisplayInfo> display_info_list;
245 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
247 // software mirroring.
248 display_info_list.push_back(internal_display_info);
249 display_info_list.push_back(external_display_info);
250 display_manager->OnNativeDisplaysChanged(display_info_list);
251 const int64 internal_display_id =
252 test::DisplayManagerTestApi(display_manager).
253 SetFirstDisplayAsInternalDisplay();
254 EXPECT_EQ(internal_id, internal_display_id);
256 EXPECT_EQ(1U, display_manager->GetNumDisplays());
257 EXPECT_TRUE(display_manager->IsMirrored());
258 EXPECT_EQ(external_id, display_manager->mirrored_display_id());
260 // dock mode.
261 display_info_list.clear();
262 display_info_list.push_back(external_display_info);
263 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
264 display_manager->OnNativeDisplaysChanged(display_info_list);
265 EXPECT_EQ(1U, display_manager->GetNumDisplays());
266 EXPECT_FALSE(display_manager->IsMirrored());
268 // back to software mirroring.
269 display_info_list.clear();
270 display_info_list.push_back(internal_display_info);
271 display_info_list.push_back(external_display_info);
272 display_manager->SetSecondDisplayMode(DisplayManager::MIRRORING);
273 display_manager->OnNativeDisplaysChanged(display_info_list);
274 EXPECT_EQ(1U, display_manager->GetNumDisplays());
275 EXPECT_TRUE(display_manager->IsMirrored());
276 EXPECT_EQ(external_id, display_manager->mirrored_display_id());
279 TEST_F(MirrorOnBootTest, MAYBE_MirrorOnBoot) {
280 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
281 EXPECT_TRUE(display_manager->IsMirrored());
282 RunAllPendingInMessageLoop();
283 test::MirrorWindowTestApi test_api;
284 EXPECT_TRUE(test_api.GetHost());
287 } // namespace ash