Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / renderer / autofill / page_click_tracker_browsertest.cc
blob9a498c9e202e0f286a83cbfd06684ef784b97202
1 // Copyright (c) 2011 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.
5 #include "base/basictypes.h"
6 #include "chrome/test/base/chrome_render_view_test.h"
7 #include "components/autofill/content/renderer/page_click_listener.h"
8 #include "components/autofill/content/renderer/page_click_tracker.h"
9 #include "content/public/renderer/render_view.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebFloatPoint.h"
12 #include "third_party/WebKit/public/platform/WebSize.h"
13 #include "third_party/WebKit/public/web/WebDocument.h"
14 #include "third_party/WebKit/public/web/WebInputElement.h"
15 #include "third_party/WebKit/public/web/WebSettings.h"
16 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
17 #include "third_party/WebKit/public/web/WebView.h"
18 #include "ui/events/keycodes/keyboard_codes.h"
20 namespace autofill {
22 class TestPageClickListener : public PageClickListener {
23 public:
24 TestPageClickListener()
25 : form_control_element_clicked_called_(false),
26 was_focused_(false) {}
28 void FormControlElementClicked(const blink::WebFormControlElement& element,
29 bool was_focused) override {
30 form_control_element_clicked_called_ = true;
31 form_control_element_clicked_ = element;
32 was_focused_ = was_focused;
35 void ClearResults() {
36 form_control_element_clicked_called_ = false;
37 form_control_element_clicked_.reset();
38 was_focused_ = false;
41 bool form_control_element_clicked_called_;
42 blink::WebFormControlElement form_control_element_clicked_;
43 bool was_focused_;
46 class PageClickTrackerTest : public ChromeRenderViewTest {
47 protected:
48 void SetUp() override {
49 ChromeRenderViewTest::SetUp();
51 // RenderView creates PageClickTracker but it doesn't keep it around.
52 // Rather than make it do so for the test, we create a new object.
53 page_click_tracker_.reset(new PageClickTracker(view_->GetMainRenderFrame(),
54 &test_listener_));
56 // Must be set before loading HTML.
57 view_->GetWebView()->setDefaultPageScaleLimits(1, 4);
59 LoadHTML("<form>"
60 " <input type='text' id='text_1'></input><br>"
61 " <input type='text' id='text_2'></input><br>"
62 " <textarea id='textarea_1'></textarea><br>"
63 " <textarea id='textarea_2'></textarea><br>"
64 " <input type='button' id='button'></input><br>"
65 " <input type='button' id='button_2' disabled></input><br>"
66 "</form>");
67 GetWebWidget()->resize(blink::WebSize(500, 500));
68 GetWebWidget()->setFocus(true);
69 blink::WebDocument document = view_->GetWebView()->mainFrame()->document();
70 text_ = document.getElementById("text_1");
71 textarea_ = document.getElementById("textarea_1");
72 ASSERT_FALSE(text_.isNull());
73 ASSERT_FALSE(textarea_.isNull());
75 // Enable show-ime event when element is focused by indicating that a user
76 // gesture has been processed since load.
77 EXPECT_TRUE(SimulateElementClick("button"));
80 void TearDown() override {
81 text_.reset();
82 textarea_.reset();
83 test_listener_.ClearResults();
84 page_click_tracker_.reset();
85 ChromeRenderViewTest::TearDown();
88 scoped_ptr<PageClickTracker> page_click_tracker_;
89 TestPageClickListener test_listener_;
90 blink::WebElement text_;
91 blink::WebElement textarea_;
94 // Tests that PageClickTracker does notify correctly when an input
95 // node is clicked.
96 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) {
97 EXPECT_NE(text_, text_.document().focusedElement());
98 // Click the text field once.
99 EXPECT_TRUE(SimulateElementClick("text_1"));
100 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
101 EXPECT_FALSE(test_listener_.was_focused_);
102 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
103 test_listener_.ClearResults();
105 // Click the text field again to test that was_focused_ is set correctly.
106 EXPECT_TRUE(SimulateElementClick("text_1"));
107 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
108 EXPECT_TRUE(test_listener_.was_focused_);
109 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
110 test_listener_.ClearResults();
112 // Click the button, no notification should happen (this is not a text-input).
113 EXPECT_TRUE(SimulateElementClick("button"));
114 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
117 // Tests that PageClickTracker does not notify when there is right click.
118 TEST_F(PageClickTrackerTest, PageClickTrackerInputRightClicked) {
119 EXPECT_NE(text_, text_.document().focusedElement());
120 // Right click the text field once.
121 EXPECT_TRUE(SimulateElementRightClick("text_1"));
122 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
123 EXPECT_FALSE(test_listener_.was_focused_);
124 EXPECT_NE(text_, test_listener_.form_control_element_clicked_);
127 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusedAndClicked) {
128 // Focus the text field without a click.
129 ExecuteJavaScriptForTests("document.getElementById('text_1').focus();");
130 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
131 test_listener_.ClearResults();
133 // Click the focused text field to test that was_focused_ is set correctly.
134 EXPECT_TRUE(SimulateElementClick("text_1"));
135 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
136 EXPECT_TRUE(test_listener_.was_focused_);
137 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
140 // Tests that PageClickTracker does notify correctly when a textarea
141 // node is clicked.
142 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaClicked) {
143 // Click the textarea field once.
144 EXPECT_TRUE(SimulateElementClick("textarea_1"));
145 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
146 EXPECT_FALSE(test_listener_.was_focused_);
147 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_);
148 test_listener_.ClearResults();
150 // Click the textarea field again to test that was_focused_ is set correctly.
151 EXPECT_TRUE(SimulateElementClick("textarea_1"));
152 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
153 EXPECT_TRUE(test_listener_.was_focused_);
154 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_);
155 test_listener_.ClearResults();
157 // Click the button, no notification should happen (this is not a text-input).
158 EXPECT_TRUE(SimulateElementClick("button"));
159 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
162 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaFocusedAndClicked) {
163 // Focus the textarea without a click.
164 ExecuteJavaScriptForTests("document.getElementById('textarea_1').focus();");
165 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
166 test_listener_.ClearResults();
168 // Click the focused text field to test that was_focused_ is set correctly.
169 EXPECT_TRUE(SimulateElementClick("textarea_1"));
170 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
171 EXPECT_TRUE(test_listener_.was_focused_);
172 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_);
173 test_listener_.ClearResults();
176 TEST_F(PageClickTrackerTest, PageClickTrackerScaledTextareaClicked) {
177 EXPECT_NE(textarea_, textarea_.document().focusedElement());
178 view_->GetWebView()->setPageScaleFactor(3);
179 view_->GetWebView()->setPinchViewportOffset(blink::WebFloatPoint(50, 50));
181 // Click textarea_1.
182 SimulatePointClick(gfx::Point(30, 30));
183 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
184 EXPECT_FALSE(test_listener_.was_focused_);
185 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_);
188 TEST_F(PageClickTrackerTest, PageClickTrackerScaledTextareaTapped) {
189 EXPECT_NE(textarea_, textarea_.document().focusedElement());
190 view_->GetWebView()->setPageScaleFactor(3);
191 view_->GetWebView()->setPinchViewportOffset(blink::WebFloatPoint(50, 50));
193 // Tap textarea_1.
194 SimulateRectTap(gfx::Rect(30, 30, 30, 30));
195 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
196 EXPECT_FALSE(test_listener_.was_focused_);
197 EXPECT_TRUE(textarea_ == test_listener_.form_control_element_clicked_);
200 TEST_F(PageClickTrackerTest, PageClickTrackerDisabledInputClickedNoEvent) {
201 EXPECT_NE(text_, text_.document().focusedElement());
202 // Click the text field once.
203 EXPECT_TRUE(SimulateElementClick("text_1"));
204 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
205 EXPECT_FALSE(test_listener_.was_focused_);
206 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
207 test_listener_.ClearResults();
209 // Click the disabled element.
210 EXPECT_TRUE(SimulateElementClick("button_2"));
211 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
214 TEST_F(PageClickTrackerTest,
215 PageClickTrackerClickDisabledInputDoesNotResetClickCounter) {
216 EXPECT_NE(text_, text_.document().focusedElement());
217 // Click the text field once.
218 EXPECT_TRUE(SimulateElementClick("text_1"));
219 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
220 EXPECT_FALSE(test_listener_.was_focused_);
221 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
222 test_listener_.ClearResults();
224 // Click the disabled element.
225 EXPECT_TRUE(SimulateElementClick("button_2"));
226 EXPECT_FALSE(test_listener_.form_control_element_clicked_called_);
227 test_listener_.ClearResults();
229 // Click the text field second time. Page click tracker should know that this
230 // is the second click.
231 EXPECT_TRUE(SimulateElementClick("text_1"));
232 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
233 EXPECT_TRUE(test_listener_.was_focused_);
234 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
237 TEST_F(PageClickTrackerTest, PageClickTrackerTapNearEdgeIsPageClick) {
238 EXPECT_NE(text_, text_.document().focusedElement());
239 // Tap outside of element bounds, but tap width is overlapping the field.
240 gfx::Rect element_bounds = GetElementBounds("text_1");
241 SimulateRectTap(element_bounds -
242 gfx::Vector2d(element_bounds.width() / 2 + 1, 0));
243 EXPECT_TRUE(test_listener_.form_control_element_clicked_called_);
244 EXPECT_FALSE(test_listener_.was_focused_);
245 EXPECT_TRUE(text_ == test_listener_.form_control_element_clicked_);
248 } // namespace autofill