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 "chrome/browser/ui/views/autofill/autofill_popup_base_view.h" 6 7 #include "chrome/browser/ui/autofill/autofill_popup_view_delegate.h" 8 #include "chrome/browser/ui/browser.h" 9 #include "chrome/browser/ui/browser_window.h" 10 #include "chrome/test/base/in_process_browser_test.h" 11 #include "testing/gmock/include/gmock/gmock.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "ui/events/event_utils.h" 14 #include "ui/views/test/views_test_base.h" 15 #include "ui/views/widget/widget.h" 16 17 namespace autofill { 18 19 namespace { 20 21 using testing::Return; 22 using testing::ReturnRef; 23 24 class MockAutofillPopupViewDelegate : public AutofillPopupViewDelegate { 25 public: 26 MOCK_METHOD0(Hide, void()); 27 MOCK_METHOD0(ViewDestroyed, void()); 28 MOCK_METHOD1(SetSelectionAtPoint, void(const gfx::Point&)); 29 MOCK_METHOD0(AcceptSelectedLine, bool()); 30 MOCK_METHOD0(SelectionCleared, void()); 31 // TODO(jdduke): Mock this method upon resolution of crbug.com/352463. 32 MOCK_CONST_METHOD0(popup_bounds, gfx::Rect&()); 33 MOCK_METHOD0(container_view, gfx::NativeView()); 34 }; 35 36 } // namespace 37 38 class AutofillPopupBaseViewTest : public InProcessBrowserTest { 39 public: 40 AutofillPopupBaseViewTest() {} 41 virtual ~AutofillPopupBaseViewTest() {} 42 43 virtual void SetUpOnMainThread() OVERRIDE { 44 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); 45 EXPECT_CALL(mock_delegate_, container_view()) 46 .WillRepeatedly(Return(window)); 47 EXPECT_CALL(mock_delegate_, ViewDestroyed()); 48 49 view_ = new AutofillPopupBaseView( 50 &mock_delegate_, 51 views::Widget::GetWidgetForNativeWindow(window)); 52 } 53 54 void ShowView() { 55 view_->DoShow(); 56 } 57 58 ui::GestureEvent CreateGestureEvent(ui::EventType type, gfx::Point point) { 59 return ui::GestureEvent(type, 60 point.x(), 61 point.y(), 62 0, 63 ui::EventTimeForNow(), 64 ui::GestureEventDetails(type, 0, 0), 65 0); 66 } 67 68 void SimulateGesture(ui::GestureEvent* event) { 69 view_->OnGestureEvent(event); 70 } 71 72 protected: 73 testing::NiceMock<MockAutofillPopupViewDelegate> mock_delegate_; 74 AutofillPopupBaseView* view_; 75 }; 76 77 // Flaky on Win only. http://crbug.com/376299 78 #if defined(OS_WIN) 79 #define MAYBE_GestureTest DISABLED_GestureTest 80 #else 81 #define MAYBE_GestureTest GestureTest 82 #endif 83 84 IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, MAYBE_GestureTest) { 85 gfx::Rect bounds(0, 0, 5, 5); 86 gfx::Point point = bounds.CenterPoint(); 87 EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds)); 88 89 ShowView(); 90 91 // Expectations. 92 { 93 testing::InSequence dummy; 94 EXPECT_CALL(mock_delegate_, SetSelectionAtPoint(point)).Times(2); 95 EXPECT_CALL(mock_delegate_, AcceptSelectedLine()); 96 EXPECT_CALL(mock_delegate_, SelectionCleared()); 97 } 98 99 // Tap down will select an element. 100 ui::GestureEvent tap_down_event = CreateGestureEvent(ui::ET_GESTURE_TAP_DOWN, 101 point); 102 SimulateGesture(&tap_down_event); 103 104 105 // Tapping will accept the selection. 106 ui::GestureEvent tap_event = CreateGestureEvent(ui::ET_GESTURE_TAP, point); 107 SimulateGesture(&tap_event); 108 109 // Tapping outside the bounds clears any selection. 110 ui::GestureEvent outside_tap = CreateGestureEvent(ui::ET_GESTURE_TAP, 111 gfx::Point(100, 100)); 112 SimulateGesture(&outside_tap); 113 } 114 115 IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, DoubleClickTest) { 116 gfx::Rect bounds(0, 0, 5, 5); 117 gfx::Point point = bounds.CenterPoint(); 118 EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds)); 119 120 ShowView(); 121 122 ui::MouseEvent mouse_down(ui::ET_MOUSE_PRESSED, 123 gfx::Point(0, 0), 124 gfx::Point(0, 0), 125 0, 0); 126 EXPECT_TRUE(static_cast<views::View*>(view_)->OnMousePressed(mouse_down)); 127 128 // Ignore double clicks. 129 mouse_down.SetClickCount(2); 130 EXPECT_FALSE(static_cast<views::View*>(view_)->OnMousePressed(mouse_down)); 131 } 132 133 } // namespace autofill 134