autofill_popup_base_view_browsertest.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 17namespace autofill { 18 19namespace { 20 21using testing::Return; 22using testing::ReturnRef; 23 24class 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 38class 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(point.x(), 60 point.y(), 61 0, 62 ui::EventTimeForNow(), 63 ui::GestureEventDetails(type, 0, 0)); 64 } 65 66 void SimulateGesture(ui::GestureEvent* event) { 67 view_->OnGestureEvent(event); 68 } 69 70 protected: 71 testing::NiceMock<MockAutofillPopupViewDelegate> mock_delegate_; 72 AutofillPopupBaseView* view_; 73}; 74 75// Flaky on Win and Linux. http://crbug.com/376299 76#if defined(OS_LINUX) || defined(OS_WIN) 77#define MAYBE_GestureTest DISABLED_GestureTest 78#else 79#define MAYBE_GestureTest GestureTest 80#endif 81 82IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, MAYBE_GestureTest) { 83 gfx::Rect bounds(0, 0, 5, 5); 84 gfx::Point point = bounds.CenterPoint(); 85 EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds)); 86 87 ShowView(); 88 89 // Expectations. 90 { 91 testing::InSequence dummy; 92 EXPECT_CALL(mock_delegate_, SetSelectionAtPoint(point)).Times(2); 93 EXPECT_CALL(mock_delegate_, AcceptSelectedLine()); 94 EXPECT_CALL(mock_delegate_, SelectionCleared()); 95 } 96 97 // Tap down will select an element. 98 ui::GestureEvent tap_down_event = CreateGestureEvent(ui::ET_GESTURE_TAP_DOWN, 99 point); 100 SimulateGesture(&tap_down_event); 101 102 103 // Tapping will accept the selection. 104 ui::GestureEvent tap_event = CreateGestureEvent(ui::ET_GESTURE_TAP, point); 105 SimulateGesture(&tap_event); 106 107 // Tapping outside the bounds clears any selection. 108 ui::GestureEvent outside_tap = CreateGestureEvent(ui::ET_GESTURE_TAP, 109 gfx::Point(100, 100)); 110 SimulateGesture(&outside_tap); 111} 112 113IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, DoubleClickTest) { 114 gfx::Rect bounds(0, 0, 5, 5); 115 gfx::Point point = bounds.CenterPoint(); 116 EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds)); 117 118 ShowView(); 119 120 ui::MouseEvent mouse_down(ui::ET_MOUSE_PRESSED, 121 gfx::Point(0, 0), 122 gfx::Point(0, 0), 123 0, 0); 124 EXPECT_TRUE(static_cast<views::View*>(view_)->OnMousePressed(mouse_down)); 125 126 // Ignore double clicks. 127 mouse_down.SetClickCount(2); 128 EXPECT_FALSE(static_cast<views::View*>(view_)->OnMousePressed(mouse_down)); 129} 130 131} // namespace autofill 132