| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 event->StopPropagation(); | 76 event->StopPropagation(); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(GestureCaptureView); | 80 DISALLOW_COPY_AND_ASSIGN(GestureCaptureView); |
| 81 }; | 81 }; |
| 82 | 82 |
| 83 // A view that always processes all mouse events. | 83 // A view that always processes all mouse events. |
| 84 class MouseView : public View { | 84 class MouseView : public View { |
| 85 public: | 85 public: |
| 86 MouseView() {} | 86 MouseView() |
| 87 : View(), |
| 88 entered_(0), |
| 89 exited_(0), |
| 90 pressed_(0) { |
| 91 } |
| 87 ~MouseView() override {} | 92 ~MouseView() override {} |
| 88 | 93 |
| 89 bool OnMousePressed(const ui::MouseEvent& event) override { | 94 bool OnMousePressed(const ui::MouseEvent& event) override { |
| 90 pressed_++; | 95 pressed_++; |
| 91 return true; | 96 return true; |
| 92 } | 97 } |
| 93 | 98 |
| 94 void OnMouseReleased(const ui::MouseEvent& event) override { released_++; } | |
| 95 | |
| 96 void OnMouseEntered(const ui::MouseEvent& event) override { entered_++; } | 99 void OnMouseEntered(const ui::MouseEvent& event) override { entered_++; } |
| 97 | 100 |
| 98 void OnMouseExited(const ui::MouseEvent& event) override { exited_++; } | 101 void OnMouseExited(const ui::MouseEvent& event) override { exited_++; } |
| 99 | 102 |
| 100 // Return the number of OnMouseEntered calls and reset the counter. | 103 // Return the number of OnMouseEntered calls and reset the counter. |
| 101 int EnteredCalls() { | 104 int EnteredCalls() { |
| 102 int i = entered_; | 105 int i = entered_; |
| 103 entered_ = 0; | 106 entered_ = 0; |
| 104 return i; | 107 return i; |
| 105 } | 108 } |
| 106 | 109 |
| 107 // Return the number of OnMouseExited calls and reset the counter. | 110 // Return the number of OnMouseExited calls and reset the counter. |
| 108 int ExitedCalls() { | 111 int ExitedCalls() { |
| 109 int i = exited_; | 112 int i = exited_; |
| 110 exited_ = 0; | 113 exited_ = 0; |
| 111 return i; | 114 return i; |
| 112 } | 115 } |
| 113 | 116 |
| 114 int pressed() const { return pressed_; } | 117 int pressed() const { return pressed_; } |
| 115 int released() const { return released_; } | |
| 116 | 118 |
| 117 private: | 119 private: |
| 118 int entered_ = 0; | 120 int entered_; |
| 119 int exited_ = 0; | 121 int exited_; |
| 120 | 122 |
| 121 int pressed_ = 0; | 123 int pressed_; |
| 122 int released_ = 0; | |
| 123 | 124 |
| 124 DISALLOW_COPY_AND_ASSIGN(MouseView); | 125 DISALLOW_COPY_AND_ASSIGN(MouseView); |
| 125 }; | 126 }; |
| 126 | 127 |
| 127 // A View that shows a different widget, sets capture on that widget, and | 128 // A View that shows a different widget, sets capture on that widget, and |
| 128 // initiates a nested message-loop when it receives a mouse-press event. | 129 // initiates a nested message-loop when it receives a mouse-press event. |
| 129 class NestedLoopCaptureView : public View { | 130 class NestedLoopCaptureView : public View { |
| 130 public: | 131 public: |
| 131 explicit NestedLoopCaptureView(Widget* widget) : widget_(widget) {} | 132 explicit NestedLoopCaptureView(Widget* widget) : widget_(widget) {} |
| 132 ~NestedLoopCaptureView() override {} | 133 ~NestedLoopCaptureView() override {} |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 widget1->CloseNow(); | 359 widget1->CloseNow(); |
| 359 } | 360 } |
| 360 #endif // defined(OS_WIN) | 361 #endif // defined(OS_WIN) |
| 361 | 362 |
| 362 TEST_F(WidgetTestInteractive, CaptureAutoReset) { | 363 TEST_F(WidgetTestInteractive, CaptureAutoReset) { |
| 363 Widget* toplevel = CreateTopLevelFramelessPlatformWidget(); | 364 Widget* toplevel = CreateTopLevelFramelessPlatformWidget(); |
| 364 View* container = new View; | 365 View* container = new View; |
| 365 toplevel->SetContentsView(container); | 366 toplevel->SetContentsView(container); |
| 366 | 367 |
| 367 EXPECT_FALSE(toplevel->HasCapture()); | 368 EXPECT_FALSE(toplevel->HasCapture()); |
| 368 toplevel->SetCapture(nullptr); | 369 toplevel->SetCapture(NULL); |
| 369 EXPECT_TRUE(toplevel->HasCapture()); | 370 EXPECT_TRUE(toplevel->HasCapture()); |
| 370 | 371 |
| 371 // By default, mouse release removes capture. | 372 // By default, mouse release removes capture. |
| 372 gfx::Point click_location(45, 15); | 373 gfx::Point click_location(45, 15); |
| 373 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, click_location, click_location, | 374 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, click_location, click_location, |
| 374 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, | 375 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, |
| 375 ui::EF_LEFT_MOUSE_BUTTON); | 376 ui::EF_LEFT_MOUSE_BUTTON); |
| 376 toplevel->OnMouseEvent(&release); | 377 toplevel->OnMouseEvent(&release); |
| 377 EXPECT_FALSE(toplevel->HasCapture()); | 378 EXPECT_FALSE(toplevel->HasCapture()); |
| 378 | 379 |
| 379 // Now a mouse release shouldn't remove capture. | 380 // Now a mouse release shouldn't remove capture. |
| 380 toplevel->set_auto_release_capture(false); | 381 toplevel->set_auto_release_capture(false); |
| 381 toplevel->SetCapture(nullptr); | 382 toplevel->SetCapture(NULL); |
| 382 EXPECT_TRUE(toplevel->HasCapture()); | 383 EXPECT_TRUE(toplevel->HasCapture()); |
| 383 toplevel->OnMouseEvent(&release); | 384 toplevel->OnMouseEvent(&release); |
| 384 EXPECT_TRUE(toplevel->HasCapture()); | 385 EXPECT_TRUE(toplevel->HasCapture()); |
| 385 toplevel->ReleaseCapture(); | 386 toplevel->ReleaseCapture(); |
| 386 EXPECT_FALSE(toplevel->HasCapture()); | 387 EXPECT_FALSE(toplevel->HasCapture()); |
| 387 | 388 |
| 388 toplevel->CloseNow(); | 389 toplevel->Close(); |
| 389 } | 390 RunPendingMessages(); |
| 390 | |
| 391 // Tests capture when auto-release is disabled and events are captured to a | |
| 392 // specific view. | |
| 393 TEST_F(WidgetTestInteractive, CaptureToViewWithoutAutoRelease) { | |
| 394 Widget* widget = CreateTopLevelFramelessPlatformWidget(); | |
| 395 MouseView* target_view = new MouseView; | |
| 396 widget->SetContentsView(target_view); | |
| 397 | |
| 398 // Set capture to a specific view. | |
| 399 widget->set_auto_release_capture(false); | |
| 400 widget->SetCapture(target_view); | |
| 401 EXPECT_TRUE(widget->HasCapture()); | |
| 402 | |
| 403 // A click is routed to the view and capture is not released on mouse release. | |
| 404 gfx::Point point(12, 34); | |
| 405 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, point, point, | |
| 406 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, | |
| 407 ui::EF_LEFT_MOUSE_BUTTON); | |
| 408 ui::MouseEvent release(ui::ET_MOUSE_RELEASED, point, point, | |
| 409 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, | |
| 410 ui::EF_LEFT_MOUSE_BUTTON); | |
| 411 widget->OnMouseEvent(&press); | |
| 412 widget->OnMouseEvent(&release); | |
| 413 EXPECT_EQ(1, target_view->pressed()); | |
| 414 EXPECT_EQ(1, target_view->released()); | |
| 415 | |
| 416 // Future events are still routed to the view. | |
| 417 widget->OnMouseEvent(&press); | |
| 418 widget->OnMouseEvent(&release); | |
| 419 EXPECT_EQ(2, target_view->pressed()); | |
| 420 EXPECT_EQ(2, target_view->released()); | |
| 421 | |
| 422 // Capture must be explicitly released. | |
| 423 EXPECT_TRUE(widget->HasCapture()); | |
| 424 widget->ReleaseCapture(); | |
| 425 EXPECT_FALSE(widget->HasCapture()); | |
| 426 | |
| 427 widget->CloseNow(); | |
| 428 } | 391 } |
| 429 | 392 |
| 430 TEST_F(WidgetTestInteractive, ResetCaptureOnGestureEnd) { | 393 TEST_F(WidgetTestInteractive, ResetCaptureOnGestureEnd) { |
| 431 Widget* toplevel = CreateTopLevelFramelessPlatformWidget(); | 394 Widget* toplevel = CreateTopLevelFramelessPlatformWidget(); |
| 432 View* container = new View; | 395 View* container = new View; |
| 433 toplevel->SetContentsView(container); | 396 toplevel->SetContentsView(container); |
| 434 | 397 |
| 435 View* gesture = new GestureCaptureView; | 398 View* gesture = new GestureCaptureView; |
| 436 gesture->SetBounds(0, 0, 30, 30); | 399 gesture->SetBounds(0, 0, 30, 30); |
| 437 container->AddChildView(gesture); | 400 container->AddChildView(gesture); |
| (...skipping 1306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1744 | 1707 |
| 1745 ui::KeyEvent key_event2(key_event); | 1708 ui::KeyEvent key_event2(key_event); |
| 1746 widget->OnKeyEvent(&key_event2); | 1709 widget->OnKeyEvent(&key_event2); |
| 1747 EXPECT_FALSE(key_event2.stopped_propagation()); | 1710 EXPECT_FALSE(key_event2.stopped_propagation()); |
| 1748 | 1711 |
| 1749 widget->CloseNow(); | 1712 widget->CloseNow(); |
| 1750 } | 1713 } |
| 1751 | 1714 |
| 1752 } // namespace test | 1715 } // namespace test |
| 1753 } // namespace views | 1716 } // namespace views |
| OLD | NEW |