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 "components/autofill/content/renderer/page_click_listener.h"
7 #include "components/autofill/content/renderer/page_click_tracker.h"
8 #include "content/public/renderer/render_view.h"
9 #include "content/public/test/render_view_test.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/web/WebDocument.h"
12 #include "third_party/WebKit/public/web/WebInputElement.h"
13 #include "third_party/WebKit/public/web/WebTextAreaElement.h"
14 #include "third_party/WebKit/public/web/WebView.h"
15 #include "third_party/WebKit/public/platform/WebSize.h"
16 #include "ui/events/keycodes/keyboard_codes.h"
20 class TestPageClickListener
: public PageClickListener
{
22 TestPageClickListener()
23 : form_control_element_clicked_called_(false),
24 was_focused_(false) {}
26 void FormControlElementClicked(const blink::WebFormControlElement
& element
,
27 bool was_focused
) override
{
28 form_control_element_clicked_called_
= true;
29 form_control_element_clicked_
= element
;
30 was_focused_
= was_focused
;
34 form_control_element_clicked_called_
= false;
35 form_control_element_clicked_
.reset();
39 bool form_control_element_clicked_called_
;
40 blink::WebFormControlElement form_control_element_clicked_
;
44 class PageClickTrackerTest
: public content::RenderViewTest
{
46 void SetUp() override
{
47 content::RenderViewTest::SetUp();
49 // RenderView creates PageClickTracker but it doesn't keep it around.
50 // Rather than make it do so for the test, we create a new object.
51 page_click_tracker_
.reset(new PageClickTracker(view_
, &test_listener_
));
54 " <input type='text' id='text_1'></input><br>"
55 " <input type='text' id='text_2'></input><br>"
56 " <textarea id='textarea_1'></textarea><br>"
57 " <textarea id='textarea_2'></textarea><br>"
58 " <input type='button' id='button'></input><br>"
60 GetWebWidget()->resize(blink::WebSize(500, 500));
61 GetWebWidget()->setFocus(true);
62 blink::WebDocument document
= view_
->GetWebView()->mainFrame()->document();
63 text_
= document
.getElementById("text_1");
64 textarea_
= document
.getElementById("textarea_1");
65 ASSERT_FALSE(text_
.isNull());
66 ASSERT_FALSE(textarea_
.isNull());
69 void TearDown() override
{
72 test_listener_
.ClearResults();
73 page_click_tracker_
.reset();
74 content::RenderViewTest::TearDown();
77 // Simulates a click on the given element and then waits for the stack
79 void SendElementClick(const std::string
& element_id
) {
80 EXPECT_TRUE(SimulateElementClick(element_id
));
81 ProcessPendingMessages();
84 // Send all the messages required for a complete key press.
85 void SendKeyPress(int key_code
) {
86 blink::WebKeyboardEvent keyboard_event
;
87 keyboard_event
.windowsKeyCode
= key_code
;
88 keyboard_event
.setKeyIdentifierFromWindowsKeyCode();
90 keyboard_event
.type
= blink::WebInputEvent::RawKeyDown
;
91 SendWebKeyboardEvent(keyboard_event
);
93 keyboard_event
.type
= blink::WebInputEvent::Char
;
94 SendWebKeyboardEvent(keyboard_event
);
96 keyboard_event
.type
= blink::WebInputEvent::KeyUp
;
97 SendWebKeyboardEvent(keyboard_event
);
100 scoped_ptr
<PageClickTracker
> page_click_tracker_
;
101 TestPageClickListener test_listener_
;
102 blink::WebElement text_
;
103 blink::WebElement textarea_
;
106 // Tests that PageClickTracker does notify correctly when an input
108 TEST_F(PageClickTrackerTest
, PageClickTrackerInputClicked
) {
109 EXPECT_NE(text_
, text_
.document().focusedElement());
110 // Click the text field once.
111 SendElementClick("text_1");
112 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
113 EXPECT_FALSE(test_listener_
.was_focused_
);
114 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
115 test_listener_
.ClearResults();
117 // Click the text field again to test that was_focused_ is set correctly.
118 SendElementClick("text_1");
119 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
120 EXPECT_TRUE(test_listener_
.was_focused_
);
121 EXPECT_TRUE(text_
== test_listener_
.form_control_element_clicked_
);
122 test_listener_
.ClearResults();
124 // Click the button, no notification should happen (this is not a text-input).
125 SendElementClick("button");
126 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
129 // Tests that PageClickTracker does notify correctly when a textarea
131 TEST_F(PageClickTrackerTest
, PageClickTrackerTextAreaClicked
) {
132 // Click the textarea field once.
133 SendElementClick("textarea_1");
134 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
135 EXPECT_FALSE(test_listener_
.was_focused_
);
136 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
137 test_listener_
.ClearResults();
139 // Click the textarea field again to test that was_focused_ is set correctly.
140 SendElementClick("textarea_1");
141 EXPECT_TRUE(test_listener_
.form_control_element_clicked_called_
);
142 EXPECT_TRUE(test_listener_
.was_focused_
);
143 EXPECT_TRUE(textarea_
== test_listener_
.form_control_element_clicked_
);
144 test_listener_
.ClearResults();
146 // Click the button, no notification should happen (this is not a text-input).
147 SendElementClick("button");
148 EXPECT_FALSE(test_listener_
.form_control_element_clicked_called_
);
151 } // namespace autofill