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