OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "components/autofill/content/renderer/page_click_listener.h" | 6 #include "components/autofill/content/renderer/page_click_listener.h" |
7 #include "components/autofill/content/renderer/page_click_tracker.h" | 7 #include "components/autofill/content/renderer/page_click_tracker.h" |
8 #include "content/public/renderer/render_view.h" | 8 #include "content/public/renderer/render_view.h" |
9 #include "content/public/test/render_view_test.h" | 9 #include "content/public/test/render_view_test.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/WebKit/public/web/WebDocument.h" | 11 #include "third_party/WebKit/public/web/WebDocument.h" |
12 #include "third_party/WebKit/public/web/WebInputElement.h" | 12 #include "third_party/WebKit/public/web/WebInputElement.h" |
13 #include "third_party/WebKit/public/web/WebTextAreaElement.h" | |
13 #include "third_party/WebKit/public/web/WebView.h" | 14 #include "third_party/WebKit/public/web/WebView.h" |
14 #include "third_party/WebKit/public/platform/WebSize.h" | 15 #include "third_party/WebKit/public/platform/WebSize.h" |
15 #include "ui/events/keycodes/keyboard_codes.h" | 16 #include "ui/events/keycodes/keyboard_codes.h" |
16 | 17 |
17 namespace autofill { | 18 namespace autofill { |
18 | 19 |
19 class TestPageClickListener : public PageClickListener { | 20 class TestPageClickListener : public PageClickListener { |
20 public: | 21 public: |
21 TestPageClickListener() | 22 TestPageClickListener() |
22 : input_element_clicked_called_(false), | 23 : formcontrol_element_clicked_called_(false), |
23 input_element_lost_focus_called_(false), | 24 formcontrol_element_lost_focus_called_(false), |
Ilya Sherman
2014/03/14 07:24:46
nit: "formcontrol" -> "form_control"
ziran.sun
2014/03/14 17:01:42
Done.
| |
24 was_focused_(false), | 25 was_focused_(false) { |
25 is_focused_(false) { | |
26 } | 26 } |
27 | 27 |
28 virtual void InputElementClicked(const blink::WebInputElement& element, | 28 virtual void FormControlElementClicked( |
29 bool was_focused, | 29 const blink::WebFormControlElement& element, |
30 bool is_focused) OVERRIDE { | 30 bool was_focused) OVERRIDE { |
31 input_element_clicked_called_ = true; | 31 formcontrol_element_clicked_called_ = true; |
32 element_clicked_ = element; | 32 formcontrol_element_clicked_ = element; |
33 was_focused_ = was_focused; | 33 was_focused_ = was_focused; |
34 is_focused_ = is_focused; | |
35 } | 34 } |
36 | 35 |
37 virtual void InputElementLostFocus() OVERRIDE { | 36 virtual void FormControlElementLostFocus() OVERRIDE { |
38 input_element_lost_focus_called_ = true; | 37 formcontrol_element_lost_focus_called_ = true; |
39 } | 38 } |
40 | 39 |
41 void ClearResults() { | 40 void ClearFormControlResults() { |
Ilya Sherman
2014/03/14 07:24:46
nit: The previous name of "ClearResults()" was sli
ziran.sun
2014/03/14 17:01:42
Done.
| |
42 input_element_clicked_called_ = false; | 41 formcontrol_element_clicked_called_ = false; |
43 input_element_lost_focus_called_ = false; | 42 formcontrol_element_lost_focus_called_ = false; |
44 element_clicked_.reset(); | 43 formcontrol_element_clicked_.reset(); |
45 was_focused_ = false; | 44 was_focused_ = false; |
46 is_focused_ = false; | |
47 } | 45 } |
48 | 46 |
49 bool input_element_clicked_called_; | 47 bool formcontrol_element_clicked_called_; |
50 bool input_element_lost_focus_called_; | 48 bool formcontrol_element_lost_focus_called_; |
51 blink::WebInputElement element_clicked_; | 49 blink::WebFormControlElement formcontrol_element_clicked_; |
52 bool was_focused_; | 50 bool was_focused_; |
53 bool is_focused_; | |
54 }; | 51 }; |
55 | 52 |
56 class PageClickTrackerTest : public content::RenderViewTest { | 53 class PageClickTrackerTest : public content::RenderViewTest { |
57 protected: | 54 protected: |
58 virtual void SetUp() OVERRIDE { | 55 virtual void SetUp() OVERRIDE { |
59 content::RenderViewTest::SetUp(); | 56 content::RenderViewTest::SetUp(); |
60 | 57 |
61 // RenderView creates PageClickTracker but it doesn't keep it around. | 58 // RenderView creates PageClickTracker but it doesn't keep it around. |
62 // Rather than make it do so for the test, we create a new object. | 59 // Rather than make it do so for the test, we create a new object. |
63 page_click_tracker_.reset(new PageClickTracker(view_, &test_listener_)); | 60 page_click_tracker_.reset(new PageClickTracker(view_, &test_listener_)); |
64 | 61 |
65 LoadHTML("<form>" | 62 LoadHTML("<form>" |
66 " <input type='text' id='text_1'></input><br>" | 63 " <input type='text' id='text_1'></input><br>" |
67 " <input type='text' id='text_2'></input><br>" | 64 " <input type='text' id='text_2'></input><br>" |
65 " <textarea id='textarea_1'></textarea><br>" | |
66 " <textarea id='textarea_2'></textarea><br>" | |
68 " <input type='button' id='button'></input><br>" | 67 " <input type='button' id='button'></input><br>" |
69 "</form>"); | 68 "</form>"); |
70 GetWebWidget()->resize(blink::WebSize(500, 500)); | 69 GetWebWidget()->resize(blink::WebSize(500, 500)); |
71 GetWebWidget()->setFocus(true); | 70 GetWebWidget()->setFocus(true); |
72 blink::WebDocument document = view_->GetWebView()->mainFrame()->document(); | 71 blink::WebDocument document = view_->GetWebView()->mainFrame()->document(); |
73 text_ = document.getElementById("text_1"); | 72 text_ = document.getElementById("text_1"); |
73 textarea_ = document.getElementById("textarea_1"); | |
74 ASSERT_FALSE(text_.isNull()); | 74 ASSERT_FALSE(text_.isNull()); |
75 ASSERT_FALSE(textarea_.isNull()); | |
75 } | 76 } |
76 | 77 |
77 virtual void TearDown() OVERRIDE { | 78 virtual void TearDown() OVERRIDE { |
78 text_.reset(); | 79 text_.reset(); |
79 test_listener_.ClearResults(); | 80 textarea_.reset(); |
81 test_listener_.ClearFormControlResults(); | |
80 page_click_tracker_.reset(); | 82 page_click_tracker_.reset(); |
81 content::RenderViewTest::TearDown(); | 83 content::RenderViewTest::TearDown(); |
82 } | 84 } |
83 | 85 |
84 // Send all the messages required for a complete key press. | 86 // Send all the messages required for a complete key press. |
85 void SendKeyPress(int key_code) { | 87 void SendKeyPress(int key_code) { |
86 blink::WebKeyboardEvent keyboard_event; | 88 blink::WebKeyboardEvent keyboard_event; |
87 keyboard_event.windowsKeyCode = key_code; | 89 keyboard_event.windowsKeyCode = key_code; |
88 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); | 90 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); |
89 | 91 |
90 keyboard_event.type = blink::WebInputEvent::RawKeyDown; | 92 keyboard_event.type = blink::WebInputEvent::RawKeyDown; |
91 SendWebKeyboardEvent(keyboard_event); | 93 SendWebKeyboardEvent(keyboard_event); |
92 | 94 |
93 keyboard_event.type = blink::WebInputEvent::Char; | 95 keyboard_event.type = blink::WebInputEvent::Char; |
94 SendWebKeyboardEvent(keyboard_event); | 96 SendWebKeyboardEvent(keyboard_event); |
95 | 97 |
96 keyboard_event.type = blink::WebInputEvent::KeyUp; | 98 keyboard_event.type = blink::WebInputEvent::KeyUp; |
97 SendWebKeyboardEvent(keyboard_event); | 99 SendWebKeyboardEvent(keyboard_event); |
98 } | 100 } |
99 | 101 |
100 scoped_ptr<PageClickTracker> page_click_tracker_; | 102 scoped_ptr<PageClickTracker> page_click_tracker_; |
101 TestPageClickListener test_listener_; | 103 TestPageClickListener test_listener_; |
102 blink::WebElement text_; | 104 blink::WebElement text_; |
105 blink::WebElement textarea_; | |
103 }; | 106 }; |
104 | 107 |
105 // Tests that PageClickTracker does notify correctly when a node is clicked. | 108 // Tests that PageClickTracker does notify correctly when a input |
Ilya Sherman
2014/03/14 07:24:46
nit: "a input" -> "an input"
ziran.sun
2014/03/14 17:01:42
Done.
| |
109 // node is clicked. | |
106 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { | 110 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { |
107 // Click the text field once. | 111 // Click the text field once. |
108 EXPECT_TRUE(SimulateElementClick("text_1")); | 112 EXPECT_TRUE(SimulateElementClick("text_1")); |
109 EXPECT_TRUE(test_listener_.input_element_clicked_called_); | 113 EXPECT_TRUE(test_listener_.formcontrol_element_clicked_called_); |
110 EXPECT_FALSE(test_listener_.was_focused_); | 114 EXPECT_FALSE(test_listener_.was_focused_); |
111 EXPECT_TRUE(test_listener_.is_focused_); | 115 EXPECT_TRUE(text_ == test_listener_.formcontrol_element_clicked_); |
112 EXPECT_TRUE(text_ == test_listener_.element_clicked_); | 116 test_listener_.ClearFormControlResults(); |
113 test_listener_.ClearResults(); | |
114 | 117 |
115 // Click the text field again to test that was_focused_ is set correctly. | 118 // Click the text field again to test that was_focused_ is set correctly. |
116 EXPECT_TRUE(SimulateElementClick("text_1")); | 119 EXPECT_TRUE(SimulateElementClick("text_1")); |
117 EXPECT_TRUE(test_listener_.input_element_clicked_called_); | 120 EXPECT_TRUE(test_listener_.formcontrol_element_clicked_called_); |
118 EXPECT_TRUE(test_listener_.was_focused_); | 121 EXPECT_TRUE(test_listener_.was_focused_); |
119 EXPECT_TRUE(test_listener_.is_focused_); | 122 EXPECT_TRUE(text_ == test_listener_.formcontrol_element_clicked_); |
120 EXPECT_TRUE(text_ == test_listener_.element_clicked_); | 123 test_listener_.ClearFormControlResults(); |
121 test_listener_.ClearResults(); | |
122 | 124 |
123 // Click the button, no notification should happen (this is not a text-input). | 125 // Click the button, no notification should happen (this is not a text-input). |
124 EXPECT_TRUE(SimulateElementClick("button")); | 126 EXPECT_TRUE(SimulateElementClick("button")); |
125 EXPECT_FALSE(test_listener_.input_element_clicked_called_); | 127 EXPECT_FALSE(test_listener_.formcontrol_element_clicked_called_); |
128 } | |
129 | |
130 // Tests that PageClickTracker does notify correctly when a textarea | |
131 // node is clicked. | |
132 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaClicked) { | |
133 // Click the textarea field once. | |
134 EXPECT_TRUE(SimulateElementClick("textarea_1")); | |
135 EXPECT_TRUE(test_listener_.formcontrol_element_clicked_called_); | |
136 EXPECT_FALSE(test_listener_.was_focused_); | |
137 EXPECT_TRUE(textarea_ == test_listener_.formcontrol_element_clicked_); | |
138 test_listener_.ClearFormControlResults(); | |
139 | |
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_.formcontrol_element_clicked_called_); | |
143 EXPECT_TRUE(test_listener_.was_focused_); | |
144 EXPECT_TRUE(textarea_ == test_listener_.formcontrol_element_clicked_); | |
145 test_listener_.ClearFormControlResults(); | |
146 | |
147 // Click the button, no notification should happen (this is not a text-input). | |
148 EXPECT_TRUE(SimulateElementClick("button")); | |
149 EXPECT_FALSE(test_listener_.formcontrol_element_clicked_called_); | |
126 } | 150 } |
127 | 151 |
128 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) { | 152 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) { |
129 // Gain focus on the text field by using tab. | 153 // Gain focus on the text field by using tab. |
130 EXPECT_NE(text_, text_.document().focusedElement()); | 154 EXPECT_NE(text_, text_.document().focusedElement()); |
131 SendKeyPress(ui::VKEY_TAB); | 155 SendKeyPress(ui::VKEY_TAB); |
156 | |
Ilya Sherman
2014/03/14 07:24:46
nit: Please revert this diff.
ziran.sun
2014/03/14 17:01:42
Done.
| |
132 EXPECT_EQ(text_, text_.document().focusedElement()); | 157 EXPECT_EQ(text_, text_.document().focusedElement()); |
133 EXPECT_FALSE(test_listener_.input_element_lost_focus_called_); | 158 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
134 | 159 |
135 // Click a button and ensure that the lost focus notification was sent, | 160 // Click a button and ensure that the lost focus notification was sent, |
136 // even though focus was gained without the mouse. | 161 // even though focus was gained without the mouse. |
137 EXPECT_TRUE(SimulateElementClick("button")); | 162 EXPECT_TRUE(SimulateElementClick("button")); |
138 EXPECT_TRUE(test_listener_.input_element_lost_focus_called_); | 163 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
139 test_listener_.ClearResults(); | 164 test_listener_.ClearFormControlResults(); |
140 | 165 |
141 // Click a text field and test that no lost focus notifications are sent. | 166 // Click a text field and test that no lost focus notifications are sent. |
142 EXPECT_TRUE(SimulateElementClick("text_1")); | 167 EXPECT_TRUE(SimulateElementClick("text_1")); |
143 EXPECT_FALSE(test_listener_.input_element_lost_focus_called_); | 168 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
144 test_listener_.ClearResults(); | 169 test_listener_.ClearFormControlResults(); |
145 | 170 |
146 // Select another text field to test that the notifcation for the | 171 // Select another text field to test that the notifcation for the |
147 // first text field losing focus is sent. | 172 // first text field losing focus is sent. |
148 EXPECT_TRUE(SimulateElementClick("text_2")); | 173 EXPECT_TRUE(SimulateElementClick("text_2")); |
149 EXPECT_TRUE(test_listener_.input_element_lost_focus_called_); | 174 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
150 test_listener_.ClearResults(); | 175 test_listener_.ClearFormControlResults(); |
151 | 176 |
152 // Click the button, a notification should happen since a text field has | 177 // Click the button, a notification should happen since a text field has |
153 // lost focus. | 178 // lost focus. |
154 EXPECT_TRUE(SimulateElementClick("button")); | 179 EXPECT_TRUE(SimulateElementClick("button")); |
155 EXPECT_TRUE(test_listener_.input_element_lost_focus_called_); | 180 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
156 test_listener_.ClearResults(); | 181 test_listener_.ClearFormControlResults(); |
157 | 182 |
158 // Click on a text field while the button has focus and ensure no lost focus | 183 // Click on a text field while the button has focus and ensure no lost focus |
159 // notification is sent. | 184 // notification is sent. |
160 EXPECT_TRUE(SimulateElementClick("text_1")); | 185 EXPECT_TRUE(SimulateElementClick("text_1")); |
161 EXPECT_FALSE(test_listener_.input_element_lost_focus_called_); | 186 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
162 } | 187 } |
163 | 188 |
189 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaFocusLost) { | |
190 // Gain focus on the textare field by using tab. | |
191 EXPECT_NE(textarea_, textarea_.document().focusedElement()); | |
192 SendKeyPress(ui::VKEY_TAB); | |
193 SendKeyPress(ui::VKEY_TAB); | |
194 SendKeyPress(ui::VKEY_TAB); | |
195 EXPECT_EQ(textarea_, textarea_.document().focusedElement()); | |
196 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); | |
197 | |
198 // Click a button and ensure that the lost focus notification was sent, | |
199 // even though focus was gained without the mouse. | |
200 EXPECT_TRUE(SimulateElementClick("button")); | |
201 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); | |
202 test_listener_.ClearFormControlResults(); | |
203 | |
204 // Click a textarea field and test that no lost focus notifications are sent. | |
205 EXPECT_TRUE(SimulateElementClick("textarea_1")); | |
206 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); | |
207 test_listener_.ClearFormControlResults(); | |
208 | |
209 // Select another textarea field to test that the notifcation for the | |
210 // first textarea field losing focus is sent. | |
211 EXPECT_TRUE(SimulateElementClick("textarea_2")); | |
212 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); | |
213 test_listener_.ClearFormControlResults(); | |
214 | |
215 // Click the button, a notification should happen since a textarea field has | |
216 // lost focus. | |
217 EXPECT_TRUE(SimulateElementClick("button")); | |
218 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); | |
219 test_listener_.ClearFormControlResults(); | |
220 | |
221 // Click on a textarea field while the button has focus and ensure no lost | |
222 // focus notification is sent. | |
223 EXPECT_TRUE(SimulateElementClick("textarea_1")); | |
224 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); | |
225 } | |
Ilya Sherman
2014/03/14 07:24:46
nit: Please add a blank line after this one.
ziran.sun
2014/03/14 17:01:42
Done.
| |
164 } // namespace autofill | 226 } // namespace autofill |
227 | |
Ilya Sherman
2014/03/14 07:24:46
nit: Please revert this diff.
ziran.sun
2014/03/14 17:01:42
Done.
| |
OLD | NEW |