| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/utility/partial_screenshot_controller.h" | |
| 6 | |
| 7 #include "ash/display/cursor_window_controller.h" | |
| 8 #include "ash/display/mouse_cursor_event_filter.h" | |
| 9 #include "ash/display/window_tree_host_manager.h" | |
| 10 #include "ash/screenshot_delegate.h" | |
| 11 #include "ash/shell.h" | |
| 12 #include "ash/test/ash_test_base.h" | |
| 13 #include "ash/test/display_manager_test_api.h" | |
| 14 #include "ash/test/mirror_window_test_api.h" | |
| 15 #include "ash/test/test_screenshot_delegate.h" | |
| 16 #include "ui/aura/env.h" | |
| 17 #include "ui/aura/window_event_dispatcher.h" | |
| 18 #include "ui/base/cursor/cursor.h" | |
| 19 #include "ui/events/test/event_generator.h" | |
| 20 #include "ui/wm/core/cursor_manager.h" | |
| 21 | |
| 22 namespace ash { | |
| 23 | |
| 24 class PartialScreenshotControllerTest : public test::AshTestBase { | |
| 25 public: | |
| 26 PartialScreenshotControllerTest() {} | |
| 27 ~PartialScreenshotControllerTest() override {} | |
| 28 | |
| 29 protected: | |
| 30 PartialScreenshotController* partial_screenshot_controller() { | |
| 31 return Shell::GetInstance()->partial_screenshot_controller(); | |
| 32 } | |
| 33 | |
| 34 bool TestIfMouseWarpsAt(const gfx::Point& point_in_screen) { | |
| 35 return test::DisplayManagerTestApi::TestIfMouseWarpsAt(GetEventGenerator(), | |
| 36 point_in_screen); | |
| 37 } | |
| 38 | |
| 39 void StartPartialScreenshotSession() { | |
| 40 partial_screenshot_controller()->StartPartialScreenshotSession( | |
| 41 GetScreenshotDelegate()); | |
| 42 } | |
| 43 | |
| 44 void Cancel() { partial_screenshot_controller()->Cancel(); } | |
| 45 | |
| 46 bool IsActive() { | |
| 47 return partial_screenshot_controller()->screenshot_delegate_ != nullptr; | |
| 48 } | |
| 49 | |
| 50 const gfx::Point& GetStartPosition() const { | |
| 51 return Shell::GetInstance() | |
| 52 ->partial_screenshot_controller() | |
| 53 ->start_position_; | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 DISALLOW_COPY_AND_ASSIGN(PartialScreenshotControllerTest); | |
| 58 }; | |
| 59 | |
| 60 TEST_F(PartialScreenshotControllerTest, BasicMouse) { | |
| 61 StartPartialScreenshotSession(); | |
| 62 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); | |
| 63 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
| 64 | |
| 65 generator.MoveMouseTo(100, 100); | |
| 66 generator.PressLeftButton(); | |
| 67 EXPECT_EQ("100,100", GetStartPosition().ToString()); | |
| 68 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 69 | |
| 70 generator.MoveMouseTo(200, 200); | |
| 71 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 72 | |
| 73 generator.ReleaseLeftButton(); | |
| 74 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString()); | |
| 75 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); | |
| 76 | |
| 77 RunAllPendingInMessageLoop(); | |
| 78 EXPECT_FALSE(IsActive()); | |
| 79 } | |
| 80 | |
| 81 // Starting the screenshot session while mouse is pressed, releasing the mouse | |
| 82 // without moving it used to cause a crash. Make sure this doesn't happen again. | |
| 83 // crbug.com/581432. | |
| 84 TEST_F(PartialScreenshotControllerTest, StartSessionWhileMousePressed) { | |
| 85 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
| 86 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); | |
| 87 | |
| 88 generator.MoveMouseTo(100, 100); | |
| 89 generator.PressLeftButton(); | |
| 90 | |
| 91 // The following used to cause a crash. Now it should remain in the | |
| 92 // screenshot session. | |
| 93 StartPartialScreenshotSession(); | |
| 94 generator.ReleaseLeftButton(); | |
| 95 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 96 EXPECT_TRUE(IsActive()); | |
| 97 | |
| 98 // Pressing again, moving, and releasing should take a screenshot. | |
| 99 generator.PressLeftButton(); | |
| 100 generator.MoveMouseTo(200, 200); | |
| 101 generator.ReleaseLeftButton(); | |
| 102 EXPECT_EQ(1, test_delegate->handle_take_partial_screenshot_count()); | |
| 103 EXPECT_FALSE(IsActive()); | |
| 104 | |
| 105 // Starting the screenshot session while mouse is pressed, moving the mouse | |
| 106 // and releasing should take a screenshot normally. | |
| 107 generator.MoveMouseTo(100, 100); | |
| 108 generator.PressLeftButton(); | |
| 109 StartPartialScreenshotSession(); | |
| 110 generator.MoveMouseTo(150, 150); | |
| 111 generator.MoveMouseTo(200, 200); | |
| 112 EXPECT_TRUE(IsActive()); | |
| 113 generator.ReleaseLeftButton(); | |
| 114 EXPECT_EQ(2, test_delegate->handle_take_partial_screenshot_count()); | |
| 115 EXPECT_FALSE(IsActive()); | |
| 116 } | |
| 117 | |
| 118 TEST_F(PartialScreenshotControllerTest, JustClick) { | |
| 119 StartPartialScreenshotSession(); | |
| 120 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); | |
| 121 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
| 122 | |
| 123 generator.MoveMouseTo(100, 100); | |
| 124 | |
| 125 // No moves, just clicking at the same position. | |
| 126 generator.ClickLeftButton(); | |
| 127 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 128 | |
| 129 RunAllPendingInMessageLoop(); | |
| 130 EXPECT_FALSE(IsActive()); | |
| 131 } | |
| 132 | |
| 133 TEST_F(PartialScreenshotControllerTest, BasicTouch) { | |
| 134 StartPartialScreenshotSession(); | |
| 135 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); | |
| 136 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
| 137 | |
| 138 generator.set_current_location(gfx::Point(100, 100)); | |
| 139 generator.PressTouch(); | |
| 140 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 141 EXPECT_EQ("100,100", GetStartPosition().ToString()); | |
| 142 | |
| 143 generator.MoveTouch(gfx::Point(200, 200)); | |
| 144 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 145 | |
| 146 generator.ReleaseTouch(); | |
| 147 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString()); | |
| 148 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); | |
| 149 | |
| 150 RunAllPendingInMessageLoop(); | |
| 151 EXPECT_FALSE(IsActive()); | |
| 152 } | |
| 153 | |
| 154 TEST_F(PartialScreenshotControllerTest, TwoFingerTouch) { | |
| 155 StartPartialScreenshotSession(); | |
| 156 test::TestScreenshotDelegate* test_delegate = GetScreenshotDelegate(); | |
| 157 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); | |
| 158 | |
| 159 generator.set_current_location(gfx::Point(100, 100)); | |
| 160 generator.PressTouch(); | |
| 161 EXPECT_EQ(0, test_delegate->handle_take_partial_screenshot_count()); | |
| 162 EXPECT_EQ("100,100", GetStartPosition().ToString()); | |
| 163 | |
| 164 generator.set_current_location(gfx::Point(200, 200)); | |
| 165 generator.PressTouchId(1); | |
| 166 EXPECT_EQ("100,100 100x100", GetScreenshotDelegate()->last_rect().ToString()); | |
| 167 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); | |
| 168 | |
| 169 RunAllPendingInMessageLoop(); | |
| 170 EXPECT_FALSE(IsActive()); | |
| 171 } | |
| 172 | |
| 173 TEST_F(PartialScreenshotControllerTest, MultipleDisplays) { | |
| 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 | |
| 192 // cursor. See http://crbug.com/462229 | |
| 193 #if defined(OS_CHROMEOS) | |
| 194 TEST_F(PartialScreenshotControllerTest, MouseWarpTest) { | |
| 195 if (!SupportsMultipleDisplays()) | |
| 196 return; | |
| 197 | |
| 198 // Create two displays. | |
| 199 Shell* shell = Shell::GetInstance(); | |
| 200 UpdateDisplay("500x500,500x500"); | |
| 201 EXPECT_EQ(2U, shell->display_manager()->GetNumDisplays()); | |
| 202 | |
| 203 StartPartialScreenshotSession(); | |
| 204 EXPECT_FALSE(TestIfMouseWarpsAt(gfx::Point(499, 11))); | |
| 205 EXPECT_EQ("499,11", | |
| 206 aura::Env::GetInstance()->last_mouse_location().ToString()); | |
| 207 | |
| 208 Cancel(); | |
| 209 EXPECT_TRUE(TestIfMouseWarpsAt(gfx::Point(499, 11))); | |
| 210 EXPECT_EQ("501,11", | |
| 211 aura::Env::GetInstance()->last_mouse_location().ToString()); | |
| 212 } | |
| 213 | |
| 214 TEST_F(PartialScreenshotControllerTest, VisibilityTest) { | |
| 215 aura::client::CursorClient* client = Shell::GetInstance()->cursor_manager(); | |
| 216 | |
| 217 GetEventGenerator().PressKey(ui::VKEY_A, 0); | |
| 218 GetEventGenerator().ReleaseKey(ui::VKEY_A, 0); | |
| 219 | |
| 220 EXPECT_FALSE(client->IsCursorVisible()); | |
| 221 | |
| 222 StartPartialScreenshotSession(); | |
| 223 EXPECT_TRUE(IsActive()); | |
| 224 EXPECT_TRUE(client->IsCursorVisible()); | |
| 225 | |
| 226 Cancel(); | |
| 227 EXPECT_TRUE(client->IsCursorVisible()); | |
| 228 } | |
| 229 | |
| 230 // Make sure PartialScreenshotController doesn't prevent handling of large | |
| 231 // cursor. See http://crbug.com/459214 | |
| 232 TEST_F(PartialScreenshotControllerTest, LargeCursor) { | |
| 233 Shell::GetInstance()->cursor_manager()->SetCursorSet(ui::CURSOR_SET_LARGE); | |
| 234 Shell::GetInstance() | |
| 235 ->window_tree_host_manager() | |
| 236 ->cursor_window_controller() | |
| 237 ->SetCursorCompositingEnabled(true); | |
| 238 | |
| 239 // Large cursor is represented as cursor window. | |
| 240 test::MirrorWindowTestApi test_api; | |
| 241 ASSERT_NE(nullptr, test_api.GetCursorWindow()); | |
| 242 | |
| 243 ui::test::EventGenerator event_generator(Shell::GetPrimaryRootWindow()); | |
| 244 gfx::Point cursor_location; | |
| 245 event_generator.MoveMouseTo(cursor_location); | |
| 246 EXPECT_EQ(cursor_location.ToString(), | |
| 247 test_api.GetCursorLocation().ToString()); | |
| 248 | |
| 249 StartPartialScreenshotSession(); | |
| 250 EXPECT_TRUE(IsActive()); | |
| 251 | |
| 252 cursor_location += gfx::Vector2d(1, 1); | |
| 253 event_generator.MoveMouseTo(cursor_location); | |
| 254 EXPECT_EQ(cursor_location.ToString(), | |
| 255 test_api.GetCursorLocation().ToString()); | |
| 256 | |
| 257 event_generator.PressLeftButton(); | |
| 258 cursor_location += gfx::Vector2d(5, 5); | |
| 259 event_generator.MoveMouseTo(cursor_location); | |
| 260 EXPECT_EQ(cursor_location.ToString(), | |
| 261 test_api.GetCursorLocation().ToString()); | |
| 262 | |
| 263 event_generator.ReleaseLeftButton(); | |
| 264 | |
| 265 EXPECT_EQ(1, GetScreenshotDelegate()->handle_take_partial_screenshot_count()); | |
| 266 EXPECT_EQ("1,1 5x5", GetScreenshotDelegate()->last_rect().ToString()); | |
| 267 RunAllPendingInMessageLoop(); | |
| 268 EXPECT_FALSE(IsActive()); | |
| 269 } | |
| 270 #endif | |
| 271 | |
| 272 } // namespace ash | |
| OLD | NEW |