autofill_popup_base_view_browsertest.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_METHOD1(AcceptSelectionAtPoint, void(const gfx::Point&)); 30 MOCK_METHOD0(SelectionCleared, void()); 31 MOCK_METHOD1(ShouldRepostEvent, bool(const ui::MouseEvent&)); 32 MOCK_CONST_METHOD0(ShouldHideOnOutsideClick, bool()); 33 MOCK_CONST_METHOD0(popup_bounds, gfx::Rect&()); 34 MOCK_METHOD0(container_view, gfx::NativeView()); 35}; 36 37} // namespace 38 39class AutofillPopupBaseViewTest : public InProcessBrowserTest { 40 public: 41 AutofillPopupBaseViewTest() {} 42 virtual ~AutofillPopupBaseViewTest() {} 43 44 virtual void SetUpOnMainThread() OVERRIDE { 45 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); 46 EXPECT_CALL(mock_delegate_, container_view()) 47 .WillRepeatedly(Return(window)); 48 EXPECT_CALL(mock_delegate_, ShouldHideOnOutsideClick()) 49 .WillRepeatedly(Return(false)); 50 EXPECT_CALL(mock_delegate_, ViewDestroyed()).Times(1); 51 52 view_ = new AutofillPopupBaseView( 53 &mock_delegate_, 54 views::Widget::GetWidgetForNativeWindow(window)); 55 } 56 57 void ShowView() { 58 view_->DoShow(); 59 } 60 61 ui::GestureEvent CreateGestureEvent(ui::EventType type, gfx::Point point) { 62 return ui::GestureEvent(type, 63 point.x(), 64 point.y(), 65 0, 66 ui::EventTimeForNow(), 67 ui::GestureEventDetails(type, 0, 0), 68 0); 69 } 70 71 void SimulateGesture(ui::GestureEvent* event) { 72 view_->OnGestureEvent(event); 73 } 74 75 protected: 76 MockAutofillPopupViewDelegate mock_delegate_; 77 AutofillPopupBaseView* view_; 78}; 79 80IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, GestureTest) { 81 gfx::Rect bounds(0, 0, 5, 5); 82 gfx::Point point = bounds.CenterPoint(); 83 EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds)); 84 85 ShowView(); 86 87 // Expectations. 88 { 89 testing::InSequence dummy; 90 EXPECT_CALL(mock_delegate_, SetSelectionAtPoint(point)).Times(1); 91 EXPECT_CALL(mock_delegate_, AcceptSelectionAtPoint(point)).Times(1); 92 EXPECT_CALL(mock_delegate_, SelectionCleared()).Times(1); 93 } 94 95 // Tap down will select an element. 96 ui::GestureEvent tap_down_event = CreateGestureEvent(ui::ET_GESTURE_TAP_DOWN, 97 point); 98 SimulateGesture(&tap_down_event); 99 100 101 // Tapping will accept the selection. 102 ui::GestureEvent tap_event = CreateGestureEvent(ui::ET_GESTURE_TAP, point); 103 SimulateGesture(&tap_event); 104 105 // Tapping outside the bounds clears any selection. 106 ui::GestureEvent outside_tap = CreateGestureEvent(ui::ET_GESTURE_TAP, 107 gfx::Point(100, 100)); 108 SimulateGesture(&outside_tap); 109} 110 111} // namespace autofill 112