Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1792)

Unified Diff: ui/views/widget/native_widget_mac_unittest.mm

Issue 2448173002: Fix processing of mouse events on MacViews.
Patch Set: Fix processing of mouse events on MacViews. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/views/widget/native_widget_mac_unittest.mm
diff --git a/ui/views/widget/native_widget_mac_unittest.mm b/ui/views/widget/native_widget_mac_unittest.mm
index 0dec9ded50095c597488f9b4f621fe55f2e16cf5..a5ffeb7a12fda4f8b8fe0d64f058054284856b35 100644
--- a/ui/views/widget/native_widget_mac_unittest.mm
+++ b/ui/views/widget/native_widget_mac_unittest.mm
@@ -16,12 +16,14 @@
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_timeouts.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "testing/gmock/include/gmock/gmock.h"
#import "testing/gtest_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#import "ui/base/cocoa/constrained_window/constrained_window_animation.h"
#import "ui/base/cocoa/window_size_constants.h"
#import "ui/base/test/scoped_fake_full_keyboard_access.h"
+#import "ui/events/cocoa/cocoa_event_utils.h"
#import "ui/events/test/cocoa_test_event_utils.h"
#include "ui/events/test/event_generator.h"
#import "ui/gfx/mac/coordinate_conversion.h"
@@ -642,6 +644,139 @@ void WaitForPaintCount(int target) {
widget->CloseNow();
}
+class MockView : public View {
tapted 2016/10/26 00:57:00 gmock isn't allowed here. You'll need to use a dif
snake 2016/10/26 12:44:56 Done.
+ public:
+ MockView() {}
+
+ MOCK_METHOD1(OnMousePressed, bool(const ui::MouseEvent& event));
+ MOCK_METHOD1(OnMouseReleased, void(const ui::MouseEvent& event));
+ MOCK_METHOD1(OnMouseDragged, bool(const ui::MouseEvent& event));
+ MOCK_METHOD1(OnMouseMoved, void(const ui::MouseEvent& event));
+ MOCK_METHOD1(OnMouseEntered, void(const ui::MouseEvent& event));
+ MOCK_METHOD1(OnMouseExited, void(const ui::MouseEvent& event));
+ MOCK_METHOD1(OnMouseWheel, bool(const ui::MouseWheelEvent& event));
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockView);
+};
+
+// Tests that when no mouse drag is in progress the Entered / Moved / Exited
+// notifications are sent normally.
+TEST_F(NativeWidgetMacTest, MouseEnterExit) {
+ Widget* widget = CreateTopLevelPlatformWidget();
+ widget->SetBounds(gfx::Rect(0, 0, 300, 300));
+ MockView* mock_view = new MockView();
+ widget->GetContentsView()->AddChildView(mock_view);
+ mock_view->SetBounds(0, 0, 300, 300);
+ widget->Show();
+
+ // Use an event generator to ask views code to set the cursor. However, note
+ // that this does not cause Cocoa to generate tracking rectangle updates.
+ ui::test::EventGenerator event_generator(GetContext(),
+ widget->GetNativeWindow());
+
+ EXPECT_CALL(*mock_view, OnMouseEntered(::testing::_)).Times(1);
+ EXPECT_CALL(*mock_view, OnMouseMoved(::testing::_)).Times(1);
+ event_generator.MoveMouseTo(gfx::Point(50, 50));
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(1);
+ event_generator.MoveMouseTo(gfx::Point(-50, -50));
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ widget->CloseNow();
+}
+
+// Tests that we don't get Exited notification when we're handling the dragging
+// operation. When we stop the drag outside of Widget's bounds we should get the
+// Exited notification when the mouse capture will be released.
+TEST_F(NativeWidgetMacTest, MouseEnterExitWithCapture) {
+ Widget* widget = CreateTopLevelNativeWidget();
+ widget->SetBounds(gfx::Rect(0, 0, 300, 300));
+ MockView* mock_view = new MockView();
+ widget->GetContentsView()->AddChildView(mock_view);
+ mock_view->SetBounds(0, 0, 300, 300);
+ widget->Show();
+
+ // Use an event generator to ask views code to set the cursor. However, note
+ // that this does not cause Cocoa to generate tracking rectangle updates.
+ ui::test::EventGenerator event_generator(GetContext(),
+ widget->GetNativeWindow());
+
+ EXPECT_CALL(*mock_view, OnMouseEntered(::testing::_)).Times(1);
+ EXPECT_CALL(*mock_view, OnMouseMoved(::testing::_)).Times(1);
+ event_generator.MoveMouseTo(gfx::Point(50, 50));
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ EXPECT_CALL(*mock_view, OnMousePressed(::testing::_)).Times(1);
+ ON_CALL(*mock_view, OnMousePressed(::testing::_))
+ .WillByDefault(::testing::Return(true));
+
+ event_generator.PressLeftButton();
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ EXPECT_TRUE(widget->HasCapture());
+
+ // The mouse exit event should not be received, if we have capture.
+ EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(0);
+ EXPECT_CALL(*mock_view, OnMouseDragged(::testing::_)).Times(1);
+ ON_CALL(*mock_view, OnMouseDragged(::testing::_))
+ .WillByDefault(::testing::Return(true));
+ event_generator.MoveMouseTo(gfx::Point(-50, -50));
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ EXPECT_CALL(*mock_view, OnMouseReleased(::testing::_)).Times(1);
+ EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(1);
+ event_generator.ReleaseLeftButton();
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ EXPECT_FALSE(widget->HasCapture());
+
+ widget->CloseNow();
+}
+
+// Tests that when exited event have location inside Widget, it will be
+// processed.
+// TODO(art-snake): Fix ui::test::EventGenerator::SendMouseExit method.
+TEST_F(NativeWidgetMacTest, MouseExitInsideWidget) {
+ Widget* widget = CreateTopLevelPlatformWidget();
+ widget->SetBounds(gfx::Rect(0, 0, 300, 300));
+ MockView* mock_view = new MockView();
+ widget->GetContentsView()->AddChildView(mock_view);
+ mock_view->SetBounds(0, 0, 300, 300);
+ widget->Show();
+
+ ui::test::EventGenerator event_generator(GetContext(),
+ widget->GetNativeWindow());
+
+ EXPECT_CALL(*mock_view, OnMouseEntered(::testing::_)).Times(1);
+ EXPECT_CALL(*mock_view, OnMouseMoved(::testing::_)).Times(1);
+ event_generator.MoveMouseTo(gfx::Point(50, 50));
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ EXPECT_CALL(*mock_view, OnMouseExited(::testing::_)).Times(1);
+ event_generator.SendMouseExit();
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ widget->CloseNow();
+}
+
+TEST_F(NativeWidgetMacTest, MouseWheelEvent) {
+ Widget* widget = CreateTopLevelPlatformWidget();
+ widget->SetBounds(gfx::Rect(0, 0, 600, 600));
+ MockView* mock_view = new MockView();
+ widget->GetContentsView()->AddChildView(mock_view);
+ mock_view->SetBounds(0, 0, 600, 600);
+ widget->Show();
+
+ ui::test::EventGenerator event_generator(GetContext(),
+ widget->GetNativeWindow());
+ EXPECT_CALL(*mock_view, OnMouseWheel(::testing::_)).Times(1);
+ event_generator.MoveMouseWheel(1, 1);
+ ::testing::Mock::VerifyAndClear(mock_view);
+
+ widget->CloseNow();
+}
+
// Tests that an accessibility request from the system makes its way through to
// a views::Label filling the window.
TEST_F(NativeWidgetMacTest, AccessibilityIntegration) {

Powered by Google App Engine
This is Rietveld 408576698