Update To 11.40.268.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   MOCK_CONST_METHOD0(element_bounds, gfx::RectF&());
36   MOCK_CONST_METHOD0(IsRTL, bool());
37 };
38
39 }  // namespace
40
41 class AutofillPopupBaseViewTest : public InProcessBrowserTest {
42  public:
43   AutofillPopupBaseViewTest() {}
44   virtual ~AutofillPopupBaseViewTest() {}
45
46   virtual void SetUpOnMainThread() override {
47     gfx::NativeView native_view =
48         browser()->tab_strip_model()->GetActiveWebContents()->GetNativeView();
49     EXPECT_CALL(mock_delegate_, container_view())
50         .WillRepeatedly(Return(native_view));
51     EXPECT_CALL(mock_delegate_, ViewDestroyed());
52
53     view_ =
54         new AutofillPopupBaseView(&mock_delegate_,
55                                   views::Widget::GetWidgetForNativeWindow(
56                                       browser()->window()->GetNativeWindow()));
57   }
58
59   void ShowView() {
60     view_->DoShow();
61   }
62
63   ui::GestureEvent CreateGestureEvent(ui::EventType type, gfx::Point point) {
64     return ui::GestureEvent(point.x(),
65                             point.y(),
66                             0,
67                             ui::EventTimeForNow(),
68                             ui::GestureEventDetails(type));
69   }
70
71   void SimulateGesture(ui::GestureEvent* event) {
72     view_->OnGestureEvent(event);
73   }
74
75  protected:
76   testing::NiceMock<MockAutofillPopupViewDelegate> mock_delegate_;
77   AutofillPopupBaseView* view_;
78 };
79
80 // Flaky on Win and Linux.  http://crbug.com/376299
81 #if defined(OS_LINUX) || defined(OS_WIN)
82 #define MAYBE_GestureTest DISABLED_GestureTest
83 #else
84 #define MAYBE_GestureTest GestureTest
85 #endif
86
87 IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, MAYBE_GestureTest) {
88   gfx::Rect bounds(0, 0, 5, 5);
89   gfx::Point point = bounds.CenterPoint();
90   EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds));
91
92   ShowView();
93
94   // Expectations.
95   {
96     testing::InSequence dummy;
97     EXPECT_CALL(mock_delegate_, SetSelectionAtPoint(point)).Times(2);
98     EXPECT_CALL(mock_delegate_, AcceptSelectedLine());
99     EXPECT_CALL(mock_delegate_, SelectionCleared());
100   }
101
102   // Tap down will select an element.
103   ui::GestureEvent tap_down_event = CreateGestureEvent(ui::ET_GESTURE_TAP_DOWN,
104                                                        point);
105   SimulateGesture(&tap_down_event);
106
107
108   // Tapping will accept the selection.
109   ui::GestureEvent tap_event = CreateGestureEvent(ui::ET_GESTURE_TAP, point);
110   SimulateGesture(&tap_event);
111
112   // Tapping outside the bounds clears any selection.
113   ui::GestureEvent outside_tap = CreateGestureEvent(ui::ET_GESTURE_TAP,
114                                                     gfx::Point(100, 100));
115   SimulateGesture(&outside_tap);
116 }
117
118 IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, DoubleClickTest) {
119   gfx::Rect bounds(0, 0, 5, 5);
120   gfx::Point point = bounds.CenterPoint();
121   EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds));
122
123   ShowView();
124
125   ui::MouseEvent mouse_down(ui::ET_MOUSE_PRESSED,
126                             gfx::Point(0, 0),
127                             gfx::Point(0, 0),
128                             0, 0);
129   EXPECT_TRUE(static_cast<views::View*>(view_)->OnMousePressed(mouse_down));
130
131   // Ignore double clicks.
132   mouse_down.SetClickCount(2);
133   EXPECT_FALSE(static_cast<views::View*>(view_)->OnMousePressed(mouse_down));
134 }
135
136 // Regression test for crbug.com/391316
137 IN_PROC_BROWSER_TEST_F(AutofillPopupBaseViewTest, CorrectBoundsTest) {
138   gfx::Rect bounds(100, 150, 5, 5);
139   EXPECT_CALL(mock_delegate_, popup_bounds()).WillRepeatedly(ReturnRef(bounds));
140
141   ShowView();
142
143   gfx::Point display_point =
144       static_cast<views::View*>(view_)->GetBoundsInScreen().origin();
145   gfx::Point expected_point = bounds.origin();
146   EXPECT_EQ(expected_point, display_point);
147 }
148
149 }  // namespace autofill