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