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 "ui/touch_selection/touch_selection_controller_aura.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/aura/test/aura_test_base.h" |
| 9 #include "ui/aura/window.h" |
| 10 #include "ui/events/test/event_generator.h" |
| 11 #include "ui/touch_selection/touch_handle_drawable_aura.h" |
| 12 #include "ui/touch_selection/touch_selection_menu_runner.h" |
| 13 |
| 14 namespace ui { |
| 15 namespace { |
| 16 |
| 17 // ... |
| 18 class TestTouchSelectionMenuRunner : public TouchSelectionMenuRunner { |
| 19 public: |
| 20 TestTouchSelectionMenuRunner() { |
| 21 SetInstance(this); |
| 22 } |
| 23 |
| 24 ~TestTouchSelectionMenuRunner() override { |
| 25 SetInstance(nullptr); |
| 26 } |
| 27 |
| 28 private: |
| 29 // TouchSelectionMenuRunner: |
| 30 void RunMenu(TouchSelectionMenuClient* client, |
| 31 const gfx::Rect& anchor_rect, |
| 32 const gfx::Size& handle_image_size, |
| 33 aura::Window* context) override { |
| 34 is_running_ = true; |
| 35 } |
| 36 |
| 37 void CloseMenu() override { |
| 38 is_running_ = false; |
| 39 } |
| 40 |
| 41 bool IsRunning() const override { |
| 42 return is_running_; |
| 43 } |
| 44 |
| 45 bool is_running_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(TestTouchSelectionMenuRunner); |
| 48 }; |
| 49 |
| 50 // Convenience to make constructing a GestureEvent simpler. |
| 51 class GestureEventForTest : public ui::GestureEvent { |
| 52 public: |
| 53 GestureEventForTest(ui::EventType type, int x, int y) |
| 54 : GestureEvent(x, |
| 55 y, |
| 56 0, |
| 57 base::TimeDelta(), |
| 58 ui::GestureEventDetails(type)) {} |
| 59 }; |
| 60 |
| 61 // ... |
| 62 class TouchEventForTest : public ui::TouchEvent { |
| 63 public: |
| 64 TouchEventForTest(ui::EventType type, gfx::Point location) |
| 65 : TouchEvent(type, location, 0, base::TimeDelta()) {} |
| 66 }; |
| 67 |
| 68 } // namespace |
| 69 |
| 70 class TouchSelectionControllerAuraTest |
| 71 : public aura::test::AuraTestBase, |
| 72 public TouchSelectionControllerAuraClient { |
| 73 public: |
| 74 TouchSelectionControllerAuraTest() : caret_moved_(false), |
| 75 selection_moved_(false), |
| 76 selection_points_swapped_(false), |
| 77 last_command_id_(0), |
| 78 context_menu_opened_(false) { |
| 79 } |
| 80 |
| 81 ~TouchSelectionControllerAuraTest() override {} |
| 82 |
| 83 void ChangeSelection(const gfx::PointF& start_top, |
| 84 const gfx::PointF& start_bottom, |
| 85 const gfx::PointF& end_top, |
| 86 const gfx::PointF& end_bottom) { |
| 87 SelectionBound start_bound, end_bound; |
| 88 start_bound.set_type(SelectionBound::LEFT); |
| 89 end_bound.set_type(SelectionBound::RIGHT); |
| 90 start_bound.SetEdge(start_top, start_bottom); |
| 91 end_bound.SetEdge(end_top, end_bottom); |
| 92 start_bound.set_visible(true); |
| 93 end_bound.set_visible(true); |
| 94 controller_->OnSelectionBoundsUpdated(start_bound, end_bound); |
| 95 } |
| 96 |
| 97 void ChangeInsertion(const gfx::Point& top, |
| 98 const gfx::Point& bottom) { |
| 99 SelectionBound bound; |
| 100 bound.set_type(SelectionBound::CENTER); |
| 101 bound.SetEdge(top, bottom); |
| 102 bound.set_visible(true); |
| 103 controller_->OnSelectionBoundsUpdated(bound, bound); |
| 104 } |
| 105 |
| 106 void ClearSelection() { |
| 107 controller_->OnSelectionBoundsUpdated(SelectionBound(), |
| 108 SelectionBound()); |
| 109 } |
| 110 |
| 111 void ClearInsertion() { ClearSelection(); } |
| 112 |
| 113 bool GetAndResetCaretMoved() { |
| 114 bool moved = caret_moved_; |
| 115 caret_moved_ = false; |
| 116 return moved; |
| 117 } |
| 118 |
| 119 bool GetAndResetSelectionMoved() { |
| 120 bool moved = selection_moved_; |
| 121 selection_moved_ = false; |
| 122 return moved; |
| 123 } |
| 124 |
| 125 bool GetAndResetSelectionPointsSwapped() { |
| 126 bool swapped = selection_points_swapped_; |
| 127 selection_points_swapped_ = false; |
| 128 return swapped; |
| 129 } |
| 130 |
| 131 const gfx::PointF& GetLastCaretPosition() const { return caret_position_; } |
| 132 const gfx::PointF& GetLastSelectionStart() const { return selection_start_; } |
| 133 const gfx::PointF& GetLastSelectionEnd() const { return selection_end_; } |
| 134 |
| 135 TouchSelectionControllerAura& controller() const { return *controller_; } |
| 136 |
| 137 TouchHandleDrawableAura* GetInsertionHandleDrawable() const { |
| 138 return static_cast<TouchHandleDrawableAura*>( |
| 139 controller_->controller_for_testing()->insertion_handle_for_testing() |
| 140 ->drawable_for_testing()); |
| 141 } |
| 142 |
| 143 aura::Window* GetInsertionHandleWindow() const { |
| 144 return GetInsertionHandleDrawable()->window_for_testing(); |
| 145 } |
| 146 |
| 147 bool IsInsertionActive() const { |
| 148 return controller_->controller_for_testing()->is_insertion_active(); |
| 149 } |
| 150 |
| 151 bool IsSelectionActive() const { |
| 152 return controller_->controller_for_testing()->is_selection_active(); |
| 153 } |
| 154 |
| 155 bool IsMenuRunning() const { |
| 156 return menu_runner_->IsRunning(); |
| 157 } |
| 158 |
| 159 private: |
| 160 // aura::test::AuraTestBase: |
| 161 void SetUp() override { |
| 162 AuraTestBase::SetUp(); |
| 163 |
| 164 parent_window_.reset(CreateNormalWindow(0, root_window(), NULL)); |
| 165 parent_window_->SetBounds(gfx::Rect(0, 0, 400, 400)); |
| 166 |
| 167 controller_.reset(new TouchSelectionControllerAura(this)); |
| 168 controller_->set_immediate_quick_menu_for_testing(true); |
| 169 |
| 170 menu_runner_.reset(new TestTouchSelectionMenuRunner()); |
| 171 } |
| 172 |
| 173 void TearDown() override { |
| 174 menu_runner_.reset(); |
| 175 controller_.reset(); |
| 176 parent_window_.reset(); |
| 177 |
| 178 AuraTestBase::TearDown(); |
| 179 } |
| 180 |
| 181 // TouchSelectionControllerAuraClient: |
| 182 void MoveCaret(const gfx::PointF& position) override { |
| 183 caret_moved_ = true; |
| 184 caret_position_ = position; |
| 185 } |
| 186 |
| 187 void MoveRangeSelectionExtent(const gfx::PointF& extent) override { |
| 188 selection_moved_ = true; |
| 189 selection_end_ = extent; |
| 190 } |
| 191 |
| 192 void SelectBetweenCoordinates(const gfx::PointF& base, |
| 193 const gfx::PointF& extent) override { |
| 194 if (base == selection_end_ && extent == selection_start_) |
| 195 selection_points_swapped_ = true; |
| 196 |
| 197 selection_start_ = base; |
| 198 selection_end_ = extent; |
| 199 } |
| 200 |
| 201 aura::Window* GetParentWindow() const override { |
| 202 return parent_window_.get(); |
| 203 } |
| 204 |
| 205 bool IsCommandIdEnabled(int command_id) const override { |
| 206 return true; |
| 207 } |
| 208 |
| 209 void ExecuteCommand(int command_id, int event_flags) override { |
| 210 last_command_id_ = command_id; |
| 211 } |
| 212 |
| 213 void OpenContextMenu(const gfx::PointF& point) override { |
| 214 context_menu_opened_ = true; |
| 215 } |
| 216 |
| 217 gfx::Rect ConvertRectToScreen(const gfx::Rect& rect) const override { |
| 218 return rect; |
| 219 } |
| 220 |
| 221 scoped_ptr<aura::Window> parent_window_; |
| 222 gfx::PointF caret_position_; |
| 223 gfx::PointF selection_start_; |
| 224 gfx::PointF selection_end_; |
| 225 bool caret_moved_; |
| 226 bool selection_moved_; |
| 227 bool selection_points_swapped_; |
| 228 int last_command_id_; |
| 229 bool context_menu_opened_; |
| 230 scoped_ptr<TouchSelectionControllerAura> controller_; |
| 231 scoped_ptr<TouchSelectionMenuRunner> menu_runner_; |
| 232 |
| 233 DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerAuraTest); |
| 234 }; |
| 235 |
| 236 TEST_F(TouchSelectionControllerAuraTest, QuickMenuShowHide) { |
| 237 gfx::Point top(5, 5); |
| 238 gfx::Point bottom(5, 15); |
| 239 |
| 240 controller().OnSelectionEditable(true); |
| 241 ui::GestureEventForTest tap(ui::ET_GESTURE_TAP, top.x(), top.y()); |
| 242 controller().HandleGestureEvent(&tap); |
| 243 ChangeInsertion(top, bottom); |
| 244 EXPECT_TRUE(IsInsertionActive()); |
| 245 EXPECT_FALSE(IsSelectionActive()); |
| 246 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 247 EXPECT_TRUE(IsMenuRunning()); |
| 248 |
| 249 ClearInsertion(); |
| 250 EXPECT_FALSE(IsInsertionActive()); |
| 251 EXPECT_FALSE(IsSelectionActive()); |
| 252 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 253 EXPECT_FALSE(IsMenuRunning()); |
| 254 } |
| 255 |
| 256 TEST_F(TouchSelectionControllerAuraTest, QuickMenuHiddenOnScroll) { |
| 257 gfx::Point top(5, 5); |
| 258 gfx::Point bottom(5, 15); |
| 259 |
| 260 controller().OnSelectionEditable(true); |
| 261 ui::GestureEventForTest tap(ui::ET_GESTURE_TAP, top.x(), top.y()); |
| 262 controller().HandleGestureEvent(&tap); |
| 263 ChangeInsertion(top, bottom); |
| 264 EXPECT_TRUE(IsInsertionActive()); |
| 265 EXPECT_FALSE(IsSelectionActive()); |
| 266 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 267 EXPECT_TRUE(IsMenuRunning()); |
| 268 |
| 269 ui::GestureEventForTest scroll_begin(ui::ET_GESTURE_SCROLL_BEGIN, top.x(), |
| 270 top.y()); |
| 271 controller().HandleGestureEvent(&scroll_begin); |
| 272 EXPECT_TRUE(IsInsertionActive()); |
| 273 EXPECT_FALSE(IsSelectionActive()); |
| 274 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 275 EXPECT_FALSE(IsMenuRunning()); |
| 276 |
| 277 ui::GestureEventForTest scroll_end(ui::ET_GESTURE_SCROLL_END, top.x(), |
| 278 top.y()); |
| 279 controller().HandleGestureEvent(&scroll_end); |
| 280 EXPECT_TRUE(IsInsertionActive()); |
| 281 EXPECT_FALSE(IsSelectionActive()); |
| 282 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 283 EXPECT_TRUE(IsMenuRunning()); |
| 284 |
| 285 ClearInsertion(); |
| 286 EXPECT_FALSE(IsInsertionActive()); |
| 287 EXPECT_FALSE(IsSelectionActive()); |
| 288 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 289 EXPECT_FALSE(IsMenuRunning()); |
| 290 } |
| 291 |
| 292 TEST_F(TouchSelectionControllerAuraTest, QuickMenuHiddenOnHandleDrag) { |
| 293 gfx::Point top(5, 5); |
| 294 gfx::Point bottom(5, 15); |
| 295 |
| 296 controller().OnSelectionEditable(true); |
| 297 ui::GestureEventForTest tap(ui::ET_GESTURE_TAP, top.x(), top.y()); |
| 298 controller().HandleGestureEvent(&tap); |
| 299 ChangeInsertion(top, bottom); |
| 300 EXPECT_TRUE(IsInsertionActive()); |
| 301 EXPECT_FALSE(IsSelectionActive()); |
| 302 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 303 EXPECT_TRUE(IsMenuRunning()); |
| 304 |
| 305 gfx::Point drag_point = |
| 306 GetInsertionHandleWindow()->GetBoundsInScreen().CenterPoint(); |
| 307 |
| 308 ui::TouchEventForTest touch_pressed(ui::ET_TOUCH_PRESSED, drag_point); |
| 309 controller().HandleTouchEvent(&touch_pressed); |
| 310 EXPECT_TRUE(IsInsertionActive()); |
| 311 EXPECT_FALSE(IsSelectionActive()); |
| 312 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 313 EXPECT_FALSE(IsMenuRunning()); |
| 314 |
| 315 ui::TouchEventForTest touch_released(ui::ET_TOUCH_RELEASED, drag_point); |
| 316 controller().HandleTouchEvent(&touch_released); |
| 317 EXPECT_TRUE(IsInsertionActive()); |
| 318 EXPECT_FALSE(IsSelectionActive()); |
| 319 EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 320 EXPECT_TRUE(IsMenuRunning()); |
| 321 |
| 322 ClearInsertion(); |
| 323 EXPECT_FALSE(IsInsertionActive()); |
| 324 EXPECT_FALSE(IsSelectionActive()); |
| 325 EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning()); |
| 326 EXPECT_FALSE(IsMenuRunning()); |
| 327 } |
| 328 |
| 329 TEST_F(TouchSelectionControllerAuraTest, TapOverSelection) { |
| 330 gfx::Point start_top(5, 5); |
| 331 gfx::Point start_bottom(5, 15); |
| 332 gfx::Point middle(15, 10); |
| 333 gfx::Point end_top(25, 5); |
| 334 gfx::Point end_bottom(25, 15); |
| 335 |
| 336 controller().OnSelectionEditable(false); |
| 337 ChangeSelection(start_top, start_bottom, end_top, end_bottom); |
| 338 EXPECT_FALSE(IsInsertionActive()); |
| 339 EXPECT_FALSE(IsSelectionActive()); |
| 340 |
| 341 ui::GestureEventForTest tap(ui::ET_GESTURE_TAP, middle.x(), middle.y()); |
| 342 controller().HandleGestureEvent(&tap); |
| 343 EXPECT_FALSE(IsInsertionActive()); |
| 344 EXPECT_TRUE(IsSelectionActive()); |
| 345 |
| 346 ClearSelection(); |
| 347 EXPECT_FALSE(IsInsertionActive()); |
| 348 EXPECT_FALSE(IsSelectionActive()); |
| 349 } |
| 350 |
| 351 TEST_F(TouchSelectionControllerAuraTest, DeactivatedOnKeyEvent) { |
| 352 gfx::Point top(5, 5); |
| 353 gfx::Point bottom(5, 15); |
| 354 |
| 355 RunAllPendingInMessageLoop(); |
| 356 controller().OnSelectionEditable(true); |
| 357 ui::GestureEventForTest tap(ui::ET_GESTURE_TAP, top.x(), top.y()); |
| 358 controller().HandleGestureEvent(&tap); |
| 359 ChangeInsertion(top, bottom); |
| 360 EXPECT_TRUE(IsInsertionActive()); |
| 361 EXPECT_FALSE(IsSelectionActive()); |
| 362 |
| 363 test::EventGenerator generator(root_window()); |
| 364 generator.PressKey(VKEY_A, 0); |
| 365 RunAllPendingInMessageLoop(); |
| 366 EXPECT_FALSE(IsInsertionActive()); |
| 367 EXPECT_FALSE(IsSelectionActive()); |
| 368 } |
| 369 |
| 370 TEST_F(TouchSelectionControllerAuraTest, DeactivatedOnMouseEvent) { |
| 371 gfx::Point top(5, 5); |
| 372 gfx::Point bottom(5, 15); |
| 373 |
| 374 RunAllPendingInMessageLoop(); |
| 375 controller().OnSelectionEditable(true); |
| 376 ui::GestureEventForTest tap(ui::ET_GESTURE_TAP, top.x(), top.y()); |
| 377 controller().HandleGestureEvent(&tap); |
| 378 ChangeInsertion(top, bottom); |
| 379 EXPECT_TRUE(IsInsertionActive()); |
| 380 EXPECT_FALSE(IsSelectionActive()); |
| 381 |
| 382 test::EventGenerator generator(root_window()); |
| 383 generator.set_current_location(top); |
| 384 RunAllPendingInMessageLoop(); |
| 385 generator.MoveMouseTo(bottom); |
| 386 RunAllPendingInMessageLoop(); |
| 387 EXPECT_FALSE(IsInsertionActive()); |
| 388 EXPECT_FALSE(IsSelectionActive()); |
| 389 } |
| 390 |
| 391 } // namespace ui |
OLD | NEW |