| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/utility/partial_screenshot_controller.h" | 5 #include "ash/utility/screenshot_controller.h" |
| 6 | 6 |
| 7 #include "ash/display/cursor_window_controller.h" | 7 #include "ash/display/cursor_window_controller.h" |
| 8 #include "ash/display/mouse_cursor_event_filter.h" | 8 #include "ash/display/mouse_cursor_event_filter.h" |
| 9 #include "ash/display/window_tree_host_manager.h" | 9 #include "ash/display/window_tree_host_manager.h" |
| 10 #include "ash/screenshot_delegate.h" | 10 #include "ash/screenshot_delegate.h" |
| 11 #include "ash/shell.h" | 11 #include "ash/shell.h" |
| 12 #include "ash/test/ash_test_base.h" | 12 #include "ash/test/ash_test_base.h" |
| 13 #include "ash/test/display_manager_test_api.h" | 13 #include "ash/test/display_manager_test_api.h" |
| 14 #include "ash/test/mirror_window_test_api.h" | 14 #include "ash/test/mirror_window_test_api.h" |
| 15 #include "ash/test/test_screenshot_delegate.h" | 15 #include "ash/test/test_screenshot_delegate.h" |
| 16 #include "ash/wm/window_util.h" |
| 16 #include "ui/aura/env.h" | 17 #include "ui/aura/env.h" |
| 17 #include "ui/aura/window_event_dispatcher.h" | 18 #include "ui/aura/window_event_dispatcher.h" |
| 18 #include "ui/base/cursor/cursor.h" | 19 #include "ui/base/cursor/cursor.h" |
| 19 #include "ui/events/test/event_generator.h" | 20 #include "ui/events/test/event_generator.h" |
| 20 #include "ui/wm/core/cursor_manager.h" | 21 #include "ui/wm/core/cursor_manager.h" |
| 21 | 22 |
| 22 namespace ash { | 23 namespace ash { |
| 23 | 24 |
| 24 class PartialScreenshotControllerTest : public test::AshTestBase { | 25 class ScreenshotControllerTest : public test::AshTestBase { |
| 25 public: | 26 public: |
| 26 PartialScreenshotControllerTest() {} | 27 ScreenshotControllerTest() {} |
| 27 ~PartialScreenshotControllerTest() override {} | 28 ~ScreenshotControllerTest() override {} |
| 28 | 29 |
| 29 protected: | 30 protected: |
| 30 PartialScreenshotController* partial_screenshot_controller() { | 31 ScreenshotController* screenshot_controller() { |
| 31 return Shell::GetInstance()->partial_screenshot_controller(); | 32 return Shell::GetInstance()->screenshot_controller(); |
| 32 } | 33 } |
| 33 | 34 |
| 34 bool TestIfMouseWarpsAt(const gfx::Point& point_in_screen) { | 35 bool TestIfMouseWarpsAt(const gfx::Point& point_in_screen) { |
| 35 return test::DisplayManagerTestApi::TestIfMouseWarpsAt(GetEventGenerator(), | 36 return test::DisplayManagerTestApi::TestIfMouseWarpsAt(GetEventGenerator(), |
| 36 point_in_screen); | 37 point_in_screen); |
| 37 } | 38 } |
| 38 | 39 |
| 39 void StartPartialScreenshotSession() { | 40 void StartPartialScreenshotSession() { |
| 40 partial_screenshot_controller()->StartPartialScreenshotSession( | 41 screenshot_controller()->StartPartialScreenshotSession( |
| 41 GetScreenshotDelegate()); | 42 GetScreenshotDelegate()); |
| 42 } | 43 } |
| 43 | 44 |
| 44 void Cancel() { partial_screenshot_controller()->Cancel(); } | 45 void StartWindowScreenshotSession() { |
| 46 screenshot_controller()->StartWindowScreenshotSession( |
| 47 GetScreenshotDelegate()); |
| 48 } |
| 49 |
| 50 void Cancel() { screenshot_controller()->Cancel(); } |
| 45 | 51 |
| 46 bool IsActive() { | 52 bool IsActive() { |
| 47 return partial_screenshot_controller()->screenshot_delegate_ != nullptr; | 53 return screenshot_controller()->screenshot_delegate_ != nullptr; |
| 48 } | 54 } |
| 49 | 55 |
| 50 const gfx::Point& GetStartPosition() const { | 56 const gfx::Point& GetStartPosition() const { |
| 51 return Shell::GetInstance() | 57 return Shell::GetInstance()->screenshot_controller()->start_position_; |
| 52 ->partial_screenshot_controller() | 58 } |
| 53 ->start_position_; | 59 |
| 60 const aura::Window* GetCurrentSelectedWindow() const { |
| 61 return Shell::GetInstance()->screenshot_controller()->selected_; |
| 62 } |
| 63 |
| 64 aura::Window* CreateSelectableWindow(const gfx::Rect& rect) { |
| 65 return CreateTestWindowInShell(SK_ColorGRAY, 0, rect); |
| 54 } | 66 } |
| 55 | 67 |
| 56 private: | 68 private: |
| 57 DISALLOW_COPY_AND_ASSIGN(PartialScreenshotControllerTest); | 69 DISALLOW_COPY_AND_ASSIGN(ScreenshotControllerTest); |
| 58 }; | 70 }; |
| 59 | 71 |
| 72 using WindowScreenshotControllerTest = ScreenshotControllerTest; |
| 73 using PartialScreenshotControllerTest = ScreenshotControllerTest; |
| 74 |
| 60 TEST_F(PartialScreenshotControllerTest, BasicMouse) { | 75 TEST_F(PartialScreenshotControllerTest, BasicMouse) { |
| 61 StartPartialScreenshotSession(); | 76 StartPartialScreenshotSession(); |
| 62 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); | 77 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); |
| 63 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | 78 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
| 64 | 79 |
| 65 generator.MoveMouseTo(100, 100); | 80 generator.MoveMouseTo(100, 100); |
| 66 generator.PressLeftButton(); | 81 generator.PressLeftButton(); |
| 67 EXPECT_EQ("100,100", GetStartPosition().ToString()); | 82 EXPECT_EQ("100,100", GetStartPosition().ToString()); |
| 68 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | 83 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); |
| 69 | 84 |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 | 178 |
| 164 generator.set_current_location(gfx::Point(200, 200)); | 179 generator.set_current_location(gfx::Point(200, 200)); |
| 165 generator.PressTouchId(1); | 180 generator.PressTouchId(1); |
| 166 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString()); | 181 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString()); |
| 167 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); | 182 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); |
| 168 | 183 |
| 169 RunAllPendingInMessageLoop(); | 184 RunAllPendingInMessageLoop(); |
| 170 EXPECT_FALSE(IsActive()); | 185 EXPECT_FALSE(IsActive()); |
| 171 } | 186 } |
| 172 | 187 |
| 173 TEST_F(PartialScreenshotControllerTest, MultipleDisplays) { | 188 // Make sure ScreenshotController doesn't allow taking screenshot |
| 174 if (!SupportsMultipleDisplays()) | |
| 175 return; | |
| 176 | |
| 177 StartPartialScreenshotSession(); | |
| 178 EXPECT_TRUE(IsActive()); | |
| 179 UpdateDisplay("400x400,500x500"); | |
| 180 RunAllPendingInMessageLoop(); | |
| 181 EXPECT_FALSE(IsActive()); | |
| 182 | |
| 183 StartPartialScreenshotSession(); | |
| 184 EXPECT_TRUE(IsActive()); | |
| 185 UpdateDisplay("400x400"); | |
| 186 RunAllPendingInMessageLoop(); | |
| 187 EXPECT_FALSE(IsActive()); | |
| 188 } | |
| 189 | |
| 190 // Make sure PartialScreenshotController doesn't allow taking screenshot | |
| 191 // across multiple monitors | 189 // across multiple monitors |
| 192 // cursor. See http://crbug.com/462229 | 190 // cursor. See http://crbug.com/462229 |
| 193 #if defined(OS_CHROMEOS) | |
| 194 TEST_F(PartialScreenshotControllerTest, MouseWarpTest) { | 191 TEST_F(PartialScreenshotControllerTest, MouseWarpTest) { |
| 195 if (!SupportsMultipleDisplays()) | 192 if (!SupportsMultipleDisplays()) |
| 196 return; | 193 return; |
| 197 | 194 |
| 198 // Create two displays. | 195 // Create two displays. |
| 199 Shell* shell = Shell::GetInstance(); | 196 Shell* shell = Shell::GetInstance(); |
| 200 UpdateDisplay("500x500,500x500"); | 197 UpdateDisplay("500x500,500x500"); |
| 201 EXPECT_EQ(2U, shell->display_manager()->GetNumDisplays()); | 198 EXPECT_EQ(2U, shell->display_manager()->GetNumDisplays()); |
| 202 | 199 |
| 203 StartPartialScreenshotSession(); | 200 StartPartialScreenshotSession(); |
| 204 EXPECT_FALSE(TestIfMouseWarpsAt(gfx::Point(499, 11))); | 201 EXPECT_FALSE(TestIfMouseWarpsAt(gfx::Point(499, 11))); |
| 205 EXPECT_EQ("499,11", | 202 EXPECT_EQ("499,11", |
| 206 aura::Env::GetInstance()->last_mouse_location().ToString()); | 203 aura::Env::GetInstance()->last_mouse_location().ToString()); |
| 207 | 204 |
| 208 Cancel(); | 205 Cancel(); |
| 209 EXPECT_TRUE(TestIfMouseWarpsAt(gfx::Point(499, 11))); | 206 EXPECT_TRUE(TestIfMouseWarpsAt(gfx::Point(499, 11))); |
| 210 EXPECT_EQ("501,11", | 207 EXPECT_EQ("501,11", |
| 211 aura::Env::GetInstance()->last_mouse_location().ToString()); | 208 aura::Env::GetInstance()->last_mouse_location().ToString()); |
| 212 } | 209 } |
| 213 | 210 |
| 211 #if defined(OS_CHROMEOS) |
| 214 TEST_F(PartialScreenshotControllerTest, VisibilityTest) { | 212 TEST_F(PartialScreenshotControllerTest, VisibilityTest) { |
| 215 aura::client::CursorClient* client = Shell::GetInstance()->cursor_manager(); | 213 aura::client::CursorClient* client = Shell::GetInstance()->cursor_manager(); |
| 216 | 214 |
| 217 GetEventGenerator().PressKey(ui::VKEY_A, 0); | 215 GetEventGenerator().PressKey(ui::VKEY_A, 0); |
| 218 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0); | 216 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0); |
| 219 | 217 |
| 220 EXPECT_FALSE(client->IsCursorVisible()); | 218 EXPECT_FALSE(client->IsCursorVisible()); |
| 221 | 219 |
| 222 StartPartialScreenshotSession(); | 220 StartPartialScreenshotSession(); |
| 223 EXPECT_TRUE(IsActive()); | 221 EXPECT_TRUE(IsActive()); |
| 224 EXPECT_TRUE(client->IsCursorVisible()); | 222 EXPECT_TRUE(client->IsCursorVisible()); |
| 225 | 223 |
| 226 Cancel(); | 224 Cancel(); |
| 227 EXPECT_TRUE(client->IsCursorVisible()); | 225 EXPECT_TRUE(client->IsCursorVisible()); |
| 228 } | 226 } |
| 229 | 227 |
| 230 // Make sure PartialScreenshotController doesn't prevent handling of large | 228 // Make sure ScreenshotController doesn't prevent handling of large |
| 231 // cursor. See http://crbug.com/459214 | 229 // cursor. See http://crbug.com/459214 |
| 232 TEST_F(PartialScreenshotControllerTest, LargeCursor) { | 230 TEST_F(PartialScreenshotControllerTest, LargeCursor) { |
| 233 Shell::GetInstance()->cursor_manager()->SetCursorSet(ui::CURSOR_SET_LARGE); | 231 Shell::GetInstance()->cursor_manager()->SetCursorSet(ui::CURSOR_SET_LARGE); |
| 234 Shell::GetInstance() | 232 Shell::GetInstance() |
| 235 ->window_tree_host_manager() | 233 ->window_tree_host_manager() |
| 236 ->cursor_window_controller() | 234 ->cursor_window_controller() |
| 237 ->SetCursorCompositingEnabled(true); | 235 ->SetCursorCompositingEnabled(true); |
| 238 | 236 |
| 239 // Large cursor is represented as cursor window. | 237 // Large cursor is represented as cursor window. |
| 240 test::MirrorWindowTestApi test_api; | 238 test::MirrorWindowTestApi test_api; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 262 | 260 |
| 263 event_generator.ReleaseLeftButton(); | 261 event_generator.ReleaseLeftButton(); |
| 264 | 262 |
| 265 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); | 263 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); |
| 266 EXPECT_EQ("1,1 5x5", GetScreenshotDelegate()->last_rect().ToString()); | 264 EXPECT_EQ("1,1 5x5", GetScreenshotDelegate()->last_rect().ToString()); |
| 267 RunAllPendingInMessageLoop(); | 265 RunAllPendingInMessageLoop(); |
| 268 EXPECT_FALSE(IsActive()); | 266 EXPECT_FALSE(IsActive()); |
| 269 } | 267 } |
| 270 #endif | 268 #endif |
| 271 | 269 |
| 270 TEST_F(WindowScreenshotControllerTest, KeyboardOperation) { |
| 271 ui::test::EventGenerator& generator(GetEventGenerator()); |
| 272 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); |
| 273 |
| 274 StartWindowScreenshotSession(); |
| 275 generator.PressKey(ui::VKEY_ESCAPE, 0); |
| 276 generator.ReleaseKey(ui::VKEY_ESCAPE, 0); |
| 277 EXPECT_FALSE(IsActive()); |
| 278 EXPECT_FALSE(test_delegate->GetSelectedWindowAndReset()); |
| 279 |
| 280 StartWindowScreenshotSession(); |
| 281 generator.PressKey(ui::VKEY_RETURN, 0); |
| 282 generator.ReleaseKey(ui::VKEY_RETURN, 0); |
| 283 EXPECT_FALSE(IsActive()); |
| 284 EXPECT_FALSE(test_delegate->GetSelectedWindowAndReset()); |
| 285 |
| 286 std::unique_ptr<aura::Window> window1( |
| 287 CreateSelectableWindow(gfx::Rect(5, 5, 20, 20))); |
| 288 wm::ActivateWindow(window1.get()); |
| 289 StartWindowScreenshotSession(); |
| 290 generator.PressKey(ui::VKEY_ESCAPE, 0); |
| 291 generator.ReleaseKey(ui::VKEY_ESCAPE, 0); |
| 292 EXPECT_FALSE(IsActive()); |
| 293 EXPECT_FALSE(test_delegate->GetSelectedWindowAndReset()); |
| 294 |
| 295 StartWindowScreenshotSession(); |
| 296 generator.PressKey(ui::VKEY_RETURN, 0); |
| 297 generator.ReleaseKey(ui::VKEY_RETURN, 0); |
| 298 EXPECT_FALSE(IsActive()); |
| 299 EXPECT_EQ(window1.get(), test_delegate->GetSelectedWindowAndReset()); |
| 300 // Make sure it's properly reset. |
| 301 EXPECT_FALSE(test_delegate->GetSelectedWindowAndReset()); |
| 302 } |
| 303 |
| 304 TEST_F(WindowScreenshotControllerTest, MouseOperation) { |
| 305 ui::test::EventGenerator& generator(GetEventGenerator()); |
| 306 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); |
| 307 StartWindowScreenshotSession(); |
| 308 EXPECT_TRUE(IsActive()); |
| 309 generator.ClickLeftButton(); |
| 310 EXPECT_FALSE(IsActive()); |
| 311 EXPECT_FALSE(test_delegate->GetSelectedWindowAndReset()); |
| 312 |
| 313 std::unique_ptr<aura::Window> window1( |
| 314 CreateSelectableWindow(gfx::Rect(5, 5, 20, 20))); |
| 315 std::unique_ptr<aura::Window> window2( |
| 316 CreateSelectableWindow(gfx::Rect(100, 100, 100, 100))); |
| 317 wm::ActivateWindow(window1.get()); |
| 318 StartWindowScreenshotSession(); |
| 319 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 320 generator.MoveMouseTo(150, 150); |
| 321 EXPECT_EQ(window2.get(), GetCurrentSelectedWindow()); |
| 322 generator.MoveMouseTo(400, 0); |
| 323 EXPECT_FALSE(GetCurrentSelectedWindow()); |
| 324 generator.MoveMouseTo(10, 10); |
| 325 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 326 generator.ClickLeftButton(); |
| 327 EXPECT_EQ(window1.get(), test_delegate->GetSelectedWindowAndReset()); |
| 328 |
| 329 // Window selection should work even with Capture. |
| 330 window2->SetCapture(); |
| 331 wm::ActivateWindow(window2.get()); |
| 332 StartWindowScreenshotSession(); |
| 333 EXPECT_EQ(window2.get(), GetCurrentSelectedWindow()); |
| 334 generator.MoveMouseTo(10, 10); |
| 335 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 336 generator.MoveMouseTo(400, 0); |
| 337 EXPECT_FALSE(GetCurrentSelectedWindow()); |
| 338 generator.MoveMouseTo(10, 10); |
| 339 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 340 generator.ClickLeftButton(); |
| 341 EXPECT_EQ(window1.get(), test_delegate->GetSelectedWindowAndReset()); |
| 342 |
| 343 // Remove window. |
| 344 StartWindowScreenshotSession(); |
| 345 generator.MoveMouseTo(10, 10); |
| 346 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 347 window1.reset(); |
| 348 EXPECT_FALSE(GetCurrentSelectedWindow()); |
| 349 generator.ClickLeftButton(); |
| 350 EXPECT_FALSE(test_delegate->GetSelectedWindowAndReset()); |
| 351 } |
| 352 |
| 353 TEST_F(WindowScreenshotControllerTest, MultiDisplays) { |
| 354 if (!SupportsMultipleDisplays()) |
| 355 return; |
| 356 |
| 357 UpdateDisplay("400x400,500x500"); |
| 358 |
| 359 ui::test::EventGenerator& generator(GetEventGenerator()); |
| 360 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); |
| 361 |
| 362 std::unique_ptr<aura::Window> window1( |
| 363 CreateSelectableWindow(gfx::Rect(100, 100, 100, 100))); |
| 364 std::unique_ptr<aura::Window> window2( |
| 365 CreateSelectableWindow(gfx::Rect(600, 200, 100, 100))); |
| 366 EXPECT_NE(window1.get()->GetRootWindow(), window2.get()->GetRootWindow()); |
| 367 |
| 368 StartWindowScreenshotSession(); |
| 369 generator.MoveMouseTo(150, 150); |
| 370 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 371 generator.MoveMouseTo(650, 250); |
| 372 EXPECT_EQ(window2.get(), GetCurrentSelectedWindow()); |
| 373 generator.ClickLeftButton(); |
| 374 EXPECT_EQ(window2.get(), test_delegate->GetSelectedWindowAndReset()); |
| 375 |
| 376 window2->SetCapture(); |
| 377 wm::ActivateWindow(window2.get()); |
| 378 StartWindowScreenshotSession(); |
| 379 generator.MoveMouseTo(150, 150); |
| 380 EXPECT_EQ(window1.get(), GetCurrentSelectedWindow()); |
| 381 generator.ClickLeftButton(); |
| 382 EXPECT_EQ(window1.get(), test_delegate->GetSelectedWindowAndReset()); |
| 383 } |
| 384 |
| 385 TEST_F(ScreenshotControllerTest, MultipleDisplays) { |
| 386 if (!SupportsMultipleDisplays()) |
| 387 return; |
| 388 |
| 389 StartPartialScreenshotSession(); |
| 390 EXPECT_TRUE(IsActive()); |
| 391 UpdateDisplay("400x400,500x500"); |
| 392 RunAllPendingInMessageLoop(); |
| 393 EXPECT_FALSE(IsActive()); |
| 394 |
| 395 StartPartialScreenshotSession(); |
| 396 EXPECT_TRUE(IsActive()); |
| 397 UpdateDisplay("400x400"); |
| 398 RunAllPendingInMessageLoop(); |
| 399 EXPECT_FALSE(IsActive()); |
| 400 |
| 401 StartWindowScreenshotSession(); |
| 402 EXPECT_TRUE(IsActive()); |
| 403 UpdateDisplay("400x400,500x500"); |
| 404 RunAllPendingInMessageLoop(); |
| 405 EXPECT_FALSE(IsActive()); |
| 406 |
| 407 StartWindowScreenshotSession(); |
| 408 EXPECT_TRUE(IsActive()); |
| 409 UpdateDisplay("400x400"); |
| 410 RunAllPendingInMessageLoop(); |
| 411 EXPECT_FALSE(IsActive()); |
| 412 } |
| 413 |
| 272 } // namespace ash | 414 } // namespace ash |
| OLD | NEW |