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_
->GetMainRenderFrame(),
56 // Must be set before loading HTML.
57 view_
->GetWebView()->setDefaultPageScaleLimits(1, 4);
58 view_
->GetWebView()->settings()->setPinchVirtualViewportEnabled(true);
61 " <input type='text' id='text_1'></input><br>"
62 " <input type='text' id='text_2'></input><br>"
63 " <textarea id='textarea_1'></textarea><br>"
64 " <textarea id='textarea_2'></textarea><br>"
65 " <input type='button' id='button'></input><br>"
66 " <input type='button' id='button_2' disabled></input><br>"
68 GetWebWidget()->resize(blink::WebSize(500, 500));
69 GetWebWidget()->setFocus(true);
70 blink::WebDocument document
= view_
->GetWebView()->mainFrame()->document();
71 text_
= document
.getElementById("text_1");
72 textarea_
= document
.getElementById("textarea_1");
73 ASSERT_FALSE(text_
.isNull());
74 ASSERT_FALSE(textarea_
.isNull());
76 // Enable show-ime event when element is focused by indicating that a user
77 // gesture has been processed since load.
78 EXPECT_TRUE(SimulateElementClick("button"));
81 void TearDown() override
{
84 test_listener_
.ClearResults();
85 page_click_tracker_
.reset();
86 ChromeRenderViewTest::TearDown();
89 scoped_ptr
<PageClickTracker
> page_click_tracker_
;
90 TestPageClickListener test_listener_
;
91 blink::WebElement text_
;
92 blink::WebElement textarea_
;
95 // Tests that PageClickTracker does notify correctly when an input
97 TEST_F(PageClickTrackerTest
, PageClickTrackerInputClicked
) {
98 EXPECT_NE(text_
, text_
.document().focusedElement());
99 // Click the text field once.
100 EXPECT_TRUE(SimulateElementClick("text_1"));
101 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
102 EXPECT_FALSE(test_listener_
.was_focused_
);
103 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
104 test_listener_
.ClearResults();
106 // Click the text field again to test that was_focused_ is set correctly.
107 EXPECT_TRUE(SimulateElementClick("text_1"));
108 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
109 EXPECT_TRUE(test_listener_
.was_focused_
);
110 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
111 test_listener_
.ClearResults();
113 // Click the button, no notification should happen (this is not a text-input).
114 EXPECT_TRUE(SimulateElementClick("button"));
115 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
118 TEST_F(PageClickTrackerTest
, PageClickTrackerInputFocusedAndClicked
) {
119 // Focus the text field without a click.
120 ExecuteJavaScript("document.getElementById('text_1').focus();");
121 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
122 test_listener_
.ClearResults();
124 // Click the focused text field to test that was_focused_ is set correctly.
125 EXPECT_TRUE(SimulateElementClick("text_1"));
126 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
127 EXPECT_TRUE(test_listener_
.was_focused_
);
128 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
131 // Tests that PageClickTracker does notify correctly when a textarea
133 TEST_F(PageClickTrackerTest
, PageClickTrackerTextAreaClicked
) {
134 // Click the textarea field once.
135 EXPECT_TRUE(SimulateElementClick("textarea_1"));
136 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
137 EXPECT_FALSE(test_listener_
.was_focused_
);
138 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
139 test_listener_
.ClearResults();
141 // Click the textarea field again to test that was_focused_ is set correctly.
142 EXPECT_TRUE(SimulateElementClick("textarea_1"));
143 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
144 EXPECT_TRUE(test_listener_
.was_focused_
);
145 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
146 test_listener_
.ClearResults();
148 // Click the button, no notification should happen (this is not a text-input).
149 EXPECT_TRUE(SimulateElementClick("button"));
150 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
153 TEST_F(PageClickTrackerTest
, PageClickTrackerTextAreaFocusedAndClicked
) {
154 // Focus the textarea without a click.
155 ExecuteJavaScript("document.getElementById('textarea_1').focus();");
156 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
157 test_listener_
.ClearResults();
159 // Click the focused text field to test that was_focused_ is set correctly.
160 EXPECT_TRUE(SimulateElementClick("textarea_1"));
161 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
162 EXPECT_TRUE(test_listener_
.was_focused_
);
163 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
164 test_listener_
.ClearResults();
167 TEST_F(PageClickTrackerTest
, PageClickTrackerScaledTextareaClicked
) {
168 EXPECT_NE(text_
, text_
.document().focusedElement());
169 view_
->GetWebView()->setPageScaleFactor(3);
170 view_
->GetWebView()->setPinchViewportOffset(blink::WebFloatPoint(50, 50));
173 SimulatePointClick(gfx::Point(30, 30));
174 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
175 EXPECT_FALSE(test_listener_
.was_focused_
);
176 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
179 TEST_F(PageClickTrackerTest
, PageClickTrackerDisabledInputClickedNoEvent
) {
180 EXPECT_NE(text_
, text_
.document().focusedElement());
181 // Click the text field once.
182 EXPECT_TRUE(SimulateElementClick("text_1"));
183 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
184 EXPECT_FALSE(test_listener_
.was_focused_
);
185 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
186 test_listener_
.ClearResults();
188 // Click the disabled element.
189 EXPECT_TRUE(SimulateElementClick("button_2"));
190 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
193 } // namespace autofill