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"
22 class TestPageClickListener
: public PageClickListener
{
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
;
36 form_control_element_clicked_called_
= false;
37 form_control_element_clicked_
.reset();
41 bool form_control_element_clicked_called_
;
42 blink::WebFormControlElement form_control_element_clicked_
;
46 class PageClickTrackerTest
: public ChromeRenderViewTest
{
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_
, &test_listener_
));
55 // Must be set before loading HTML.
56 view_
->GetWebView()->setDefaultPageScaleLimits(1, 4);
57 view_
->GetWebView()->settings()->setPinchVirtualViewportEnabled(true);
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>"
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
{
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
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 TEST_F(PageClickTrackerTest
, PageClickTrackerInputFocusedAndClicked
) {
118 // Focus the text field without a click.
119 ExecuteJavaScript("document.getElementById('text_1').focus();");
120 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
121 test_listener_
.ClearResults();
123 // Click the focused text field to test that was_focused_ is set correctly.
124 EXPECT_TRUE(SimulateElementClick("text_1"));
125 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
126 EXPECT_TRUE(test_listener_
.was_focused_
);
127 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
130 // Tests that PageClickTracker does notify correctly when a textarea
132 TEST_F(PageClickTrackerTest
, PageClickTrackerTextAreaClicked
) {
133 // Click the textarea field once.
134 EXPECT_TRUE(SimulateElementClick("textarea_1"));
135 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
136 EXPECT_FALSE(test_listener_
.was_focused_
);
137 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
138 test_listener_
.ClearResults();
140 // Click the textarea field again to test that was_focused_ is set correctly.
141 EXPECT_TRUE(SimulateElementClick("textarea_1"));
142 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
143 EXPECT_TRUE(test_listener_
.was_focused_
);
144 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
145 test_listener_
.ClearResults();
147 // Click the button, no notification should happen (this is not a text-input).
148 EXPECT_TRUE(SimulateElementClick("button"));
149 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
152 TEST_F(PageClickTrackerTest
, PageClickTrackerTextAreaFocusedAndClicked
) {
153 // Focus the textarea without a click.
154 ExecuteJavaScript("document.getElementById('textarea_1').focus();");
155 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
156 test_listener_
.ClearResults();
158 // Click the focused text field to test that was_focused_ is set correctly.
159 EXPECT_TRUE(SimulateElementClick("textarea_1"));
160 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
161 EXPECT_TRUE(test_listener_
.was_focused_
);
162 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
163 test_listener_
.ClearResults();
166 TEST_F(PageClickTrackerTest
, PageClickTrackerScaledTextareaClicked
) {
167 EXPECT_NE(text_
, text_
.document().focusedElement());
168 view_
->GetWebView()->setPageScaleFactor(3);
169 view_
->GetWebView()->setPinchViewportOffset(blink::WebFloatPoint(50, 50));
172 SimulatePointClick(gfx::Point(30, 30));
173 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
174 EXPECT_FALSE(test_listener_
.was_focused_
);
175 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
178 TEST_F(PageClickTrackerTest
, PageClickTrackerDisabledInputClickedNoEvent
) {
179 EXPECT_NE(text_
, text_
.document().focusedElement());
180 // Click the text field once.
181 EXPECT_TRUE(SimulateElementClick("text_1"));
182 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
183 EXPECT_FALSE(test_listener_
.was_focused_
);
184 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
185 test_listener_
.ClearResults();
187 // Click the disabled element.
188 EXPECT_TRUE(SimulateElementClick("button_2"));
189 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
192 } // namespace autofill