| 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 : input_element_clicked_called_(false), |
| 23 input_element_lost_focus_called_(false), | 24 textarea_element_clicked_called_(false), |
| 25 formcontrol_element_lost_focus_called_(false), |
| 24 was_focused_(false), | 26 was_focused_(false), |
| 25 is_focused_(false) { | 27 is_focused_(false) { |
| 26 } | 28 } |
| 27 | 29 |
| 28 virtual void InputElementClicked(const blink::WebInputElement& element, | 30 virtual void InputElementClicked(const blink::WebInputElement& element, |
| 29 bool was_focused, | 31 bool was_focused, |
| 30 bool is_focused) OVERRIDE { | 32 bool is_focused) OVERRIDE { |
| 31 input_element_clicked_called_ = true; | 33 input_element_clicked_called_ = true; |
| 32 element_clicked_ = element; | 34 input_element_clicked_ = element; |
| 33 was_focused_ = was_focused; | 35 was_focused_ = was_focused; |
| 34 is_focused_ = is_focused; | 36 is_focused_ = is_focused; |
| 35 } | 37 } |
| 36 | 38 |
| 37 virtual void InputElementLostFocus() OVERRIDE { | 39 virtual void TextAreaElementClicked(const blink::WebTextAreaElement& element, |
| 38 input_element_lost_focus_called_ = true; | 40 bool was_focused, |
| 41 bool is_focused) OVERRIDE { |
| 42 textarea_element_clicked_called_ = true; |
| 43 textarea_element_clicked_ = element; |
| 44 was_focused_ = was_focused; |
| 45 is_focused_ = is_focused; |
| 39 } | 46 } |
| 40 | 47 |
| 41 void ClearResults() { | 48 virtual void FormControlElementLostFocus() OVERRIDE { |
| 49 formcontrol_element_lost_focus_called_ = true; |
| 50 } |
| 51 |
| 52 void ClearInputResults() { |
| 42 input_element_clicked_called_ = false; | 53 input_element_clicked_called_ = false; |
| 43 input_element_lost_focus_called_ = false; | 54 formcontrol_element_lost_focus_called_ = false; |
| 44 element_clicked_.reset(); | 55 input_element_clicked_.reset(); |
| 56 was_focused_ = false; |
| 57 is_focused_ = false; |
| 58 } |
| 59 |
| 60 void ClearTextAreaResults() { |
| 61 textarea_element_clicked_called_ = false; |
| 62 formcontrol_element_lost_focus_called_ = false; |
| 63 textarea_element_clicked_.reset(); |
| 45 was_focused_ = false; | 64 was_focused_ = false; |
| 46 is_focused_ = false; | 65 is_focused_ = false; |
| 47 } | 66 } |
| 48 | 67 |
| 49 bool input_element_clicked_called_; | 68 bool input_element_clicked_called_; |
| 50 bool input_element_lost_focus_called_; | 69 bool textarea_element_clicked_called_; |
| 51 blink::WebInputElement element_clicked_; | 70 bool formcontrol_element_lost_focus_called_; |
| 71 blink::WebInputElement input_element_clicked_; |
| 72 blink::WebTextAreaElement textarea_element_clicked_; |
| 52 bool was_focused_; | 73 bool was_focused_; |
| 53 bool is_focused_; | 74 bool is_focused_; |
| 54 }; | 75 }; |
| 55 | 76 |
| 56 class PageClickTrackerTest : public content::RenderViewTest { | 77 class PageClickTrackerTest : public content::RenderViewTest { |
| 57 protected: | 78 protected: |
| 58 virtual void SetUp() OVERRIDE { | 79 virtual void SetUp() OVERRIDE { |
| 59 content::RenderViewTest::SetUp(); | 80 content::RenderViewTest::SetUp(); |
| 60 | 81 |
| 61 // RenderView creates PageClickTracker but it doesn't keep it around. | 82 // 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. | 83 // Rather than make it do so for the test, we create a new object. |
| 63 page_click_tracker_.reset(new PageClickTracker(view_, &test_listener_)); | 84 page_click_tracker_.reset(new PageClickTracker(view_, &test_listener_)); |
| 64 | 85 |
| 65 LoadHTML("<form>" | 86 LoadHTML("<form>" |
| 66 " <input type='text' id='text_1'></input><br>" | 87 " <input type='text' id='text_1'></input><br>" |
| 67 " <input type='text' id='text_2'></input><br>" | 88 " <input type='text' id='text_2'></input><br>" |
| 89 " <textarea id='textarea_1'></textarea><br>" |
| 90 " <textarea id='textarea_2'></textarea><br>" |
| 68 " <input type='button' id='button'></input><br>" | 91 " <input type='button' id='button'></input><br>" |
| 69 "</form>"); | 92 "</form>"); |
| 70 GetWebWidget()->resize(blink::WebSize(500, 500)); | 93 GetWebWidget()->resize(blink::WebSize(500, 500)); |
| 71 GetWebWidget()->setFocus(true); | 94 GetWebWidget()->setFocus(true); |
| 72 blink::WebDocument document = view_->GetWebView()->mainFrame()->document(); | 95 blink::WebDocument document = view_->GetWebView()->mainFrame()->document(); |
| 73 text_ = document.getElementById("text_1"); | 96 text_ = document.getElementById("text_1"); |
| 97 textarea_ = document.getElementById("textarea_1"); |
| 74 ASSERT_FALSE(text_.isNull()); | 98 ASSERT_FALSE(text_.isNull()); |
| 99 ASSERT_FALSE(textarea_.isNull()); |
| 75 } | 100 } |
| 76 | 101 |
| 77 virtual void TearDown() OVERRIDE { | 102 virtual void TearDown() OVERRIDE { |
| 78 text_.reset(); | 103 text_.reset(); |
| 79 test_listener_.ClearResults(); | 104 textarea_.reset(); |
| 105 test_listener_.ClearInputResults(); |
| 106 test_listener_.ClearTextAreaResults(); |
| 80 page_click_tracker_.reset(); | 107 page_click_tracker_.reset(); |
| 81 content::RenderViewTest::TearDown(); | 108 content::RenderViewTest::TearDown(); |
| 82 } | 109 } |
| 83 | 110 |
| 84 // Send all the messages required for a complete key press. | 111 // Send all the messages required for a complete key press. |
| 85 void SendKeyPress(int key_code) { | 112 void SendKeyPress(int key_code) { |
| 86 blink::WebKeyboardEvent keyboard_event; | 113 blink::WebKeyboardEvent keyboard_event; |
| 87 keyboard_event.windowsKeyCode = key_code; | 114 keyboard_event.windowsKeyCode = key_code; |
| 88 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); | 115 keyboard_event.setKeyIdentifierFromWindowsKeyCode(); |
| 89 | 116 |
| 90 keyboard_event.type = blink::WebInputEvent::RawKeyDown; | 117 keyboard_event.type = blink::WebInputEvent::RawKeyDown; |
| 91 SendWebKeyboardEvent(keyboard_event); | 118 SendWebKeyboardEvent(keyboard_event); |
| 92 | 119 |
| 93 keyboard_event.type = blink::WebInputEvent::Char; | 120 keyboard_event.type = blink::WebInputEvent::Char; |
| 94 SendWebKeyboardEvent(keyboard_event); | 121 SendWebKeyboardEvent(keyboard_event); |
| 95 | 122 |
| 96 keyboard_event.type = blink::WebInputEvent::KeyUp; | 123 keyboard_event.type = blink::WebInputEvent::KeyUp; |
| 97 SendWebKeyboardEvent(keyboard_event); | 124 SendWebKeyboardEvent(keyboard_event); |
| 98 } | 125 } |
| 99 | 126 |
| 100 scoped_ptr<PageClickTracker> page_click_tracker_; | 127 scoped_ptr<PageClickTracker> page_click_tracker_; |
| 101 TestPageClickListener test_listener_; | 128 TestPageClickListener test_listener_; |
| 102 blink::WebElement text_; | 129 blink::WebElement text_; |
| 130 blink::WebElement textarea_; |
| 103 }; | 131 }; |
| 104 | 132 |
| 105 // Tests that PageClickTracker does notify correctly when a node is clicked. | 133 // Tests that PageClickTracker does notify correctly when a node is clicked. |
| 106 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { | 134 TEST_F(PageClickTrackerTest, PageClickTrackerInputClicked) { |
| 107 // Click the text field once. | 135 // Click the text field once. |
| 108 EXPECT_TRUE(SimulateElementClick("text_1")); | 136 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 109 EXPECT_TRUE(test_listener_.input_element_clicked_called_); | 137 EXPECT_TRUE(test_listener_.input_element_clicked_called_); |
| 110 EXPECT_FALSE(test_listener_.was_focused_); | 138 EXPECT_FALSE(test_listener_.was_focused_); |
| 111 EXPECT_TRUE(test_listener_.is_focused_); | 139 EXPECT_TRUE(test_listener_.is_focused_); |
| 112 EXPECT_TRUE(text_ == test_listener_.element_clicked_); | 140 EXPECT_TRUE(text_ == test_listener_.input_element_clicked_); |
| 113 test_listener_.ClearResults(); | 141 test_listener_.ClearInputResults(); |
| 114 | 142 |
| 115 // Click the text field again to test that was_focused_ is set correctly. | 143 // Click the text field again to test that was_focused_ is set correctly. |
| 116 EXPECT_TRUE(SimulateElementClick("text_1")); | 144 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 117 EXPECT_TRUE(test_listener_.input_element_clicked_called_); | 145 EXPECT_TRUE(test_listener_.input_element_clicked_called_); |
| 118 EXPECT_TRUE(test_listener_.was_focused_); | 146 EXPECT_TRUE(test_listener_.was_focused_); |
| 119 EXPECT_TRUE(test_listener_.is_focused_); | 147 EXPECT_TRUE(test_listener_.is_focused_); |
| 120 EXPECT_TRUE(text_ == test_listener_.element_clicked_); | 148 EXPECT_TRUE(text_ == test_listener_.input_element_clicked_); |
| 121 test_listener_.ClearResults(); | 149 test_listener_.ClearInputResults(); |
| 122 | 150 |
| 123 // Click the button, no notification should happen (this is not a text-input). | 151 // Click the button, no notification should happen (this is not a text-input). |
| 124 EXPECT_TRUE(SimulateElementClick("button")); | 152 EXPECT_TRUE(SimulateElementClick("button")); |
| 125 EXPECT_FALSE(test_listener_.input_element_clicked_called_); | 153 EXPECT_FALSE(test_listener_.input_element_clicked_called_); |
| 126 } | 154 } |
| 127 | 155 |
| 156 // Tests that PageClickTracker does notify correctly when a node is clicked. |
| 157 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaClicked) { |
| 158 // Click the textarea field once. |
| 159 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
| 160 EXPECT_TRUE(test_listener_.textarea_element_clicked_called_); |
| 161 EXPECT_FALSE(test_listener_.was_focused_); |
| 162 EXPECT_TRUE(test_listener_.is_focused_); |
| 163 EXPECT_TRUE(textarea_ == test_listener_.textarea_element_clicked_); |
| 164 test_listener_.ClearTextAreaResults(); |
| 165 |
| 166 // Click the textarea field again to test that was_focused_ is set correctly. |
| 167 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
| 168 EXPECT_TRUE(test_listener_.textarea_element_clicked_called_); |
| 169 EXPECT_TRUE(test_listener_.was_focused_); |
| 170 EXPECT_TRUE(test_listener_.is_focused_); |
| 171 EXPECT_TRUE(textarea_ == test_listener_.textarea_element_clicked_); |
| 172 test_listener_.ClearTextAreaResults(); |
| 173 |
| 174 // Click the button, no notification should happen (this is not a text-input). |
| 175 EXPECT_TRUE(SimulateElementClick("button")); |
| 176 EXPECT_FALSE(test_listener_.textarea_element_clicked_called_); |
| 177 } |
| 178 |
| 128 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) { | 179 TEST_F(PageClickTrackerTest, PageClickTrackerInputFocusLost) { |
| 129 // Gain focus on the text field by using tab. | 180 // Gain focus on the text field by using tab. |
| 130 EXPECT_NE(text_, text_.document().focusedNode()); | 181 EXPECT_NE(text_, text_.document().focusedNode()); |
| 131 SendKeyPress(ui::VKEY_TAB); | 182 SendKeyPress(ui::VKEY_TAB); |
| 132 EXPECT_EQ(text_, text_.document().focusedNode()); | 183 EXPECT_EQ(text_, text_.document().focusedNode()); |
| 133 EXPECT_FALSE(test_listener_.input_element_lost_focus_called_); | 184 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
| 134 | 185 |
| 135 // Click a button and ensure that the lost focus notification was sent, | 186 // Click a button and ensure that the lost focus notification was sent, |
| 136 // even though focus was gained without the mouse. | 187 // even though focus was gained without the mouse. |
| 137 EXPECT_TRUE(SimulateElementClick("button")); | 188 EXPECT_TRUE(SimulateElementClick("button")); |
| 138 EXPECT_TRUE(test_listener_.input_element_lost_focus_called_); | 189 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
| 139 test_listener_.ClearResults(); | 190 test_listener_.ClearInputResults(); |
| 140 | 191 |
| 141 // Click a text field and test that no lost focus notifications are sent. | 192 // Click a text field and test that no lost focus notifications are sent. |
| 142 EXPECT_TRUE(SimulateElementClick("text_1")); | 193 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 143 EXPECT_FALSE(test_listener_.input_element_lost_focus_called_); | 194 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
| 144 test_listener_.ClearResults(); | 195 test_listener_.ClearInputResults(); |
| 145 | 196 |
| 146 // Select another text field to test that the notifcation for the | 197 // Select another text field to test that the notifcation for the |
| 147 // first text field losing focus is sent. | 198 // first text field losing focus is sent. |
| 148 EXPECT_TRUE(SimulateElementClick("text_2")); | 199 EXPECT_TRUE(SimulateElementClick("text_2")); |
| 149 EXPECT_TRUE(test_listener_.input_element_lost_focus_called_); | 200 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
| 150 test_listener_.ClearResults(); | 201 test_listener_.ClearInputResults(); |
| 151 | 202 |
| 152 // Click the button, a notification should happen since a text field has | 203 // Click the button, a notification should happen since a text field has |
| 153 // lost focus. | 204 // lost focus. |
| 154 EXPECT_TRUE(SimulateElementClick("button")); | 205 EXPECT_TRUE(SimulateElementClick("button")); |
| 155 EXPECT_TRUE(test_listener_.input_element_lost_focus_called_); | 206 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
| 156 test_listener_.ClearResults(); | 207 test_listener_.ClearInputResults(); |
| 157 | 208 |
| 158 // Click on a text field while the button has focus and ensure no lost focus | 209 // Click on a text field while the button has focus and ensure no lost focus |
| 159 // notification is sent. | 210 // notification is sent. |
| 160 EXPECT_TRUE(SimulateElementClick("text_1")); | 211 EXPECT_TRUE(SimulateElementClick("text_1")); |
| 161 EXPECT_FALSE(test_listener_.input_element_lost_focus_called_); | 212 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
| 162 } | 213 } |
| 163 | 214 |
| 215 TEST_F(PageClickTrackerTest, PageClickTrackerTextAreaFocusLost) { |
| 216 // Gain focus on the textare field by using tab. |
| 217 EXPECT_NE(textarea_, textarea_.document().focusedNode()); |
| 218 SendKeyPress(ui::VKEY_TAB); |
| 219 SendKeyPress(ui::VKEY_TAB); |
| 220 SendKeyPress(ui::VKEY_TAB); |
| 221 EXPECT_EQ(textarea_, textarea_.document().focusedNode()); |
| 222 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
| 223 |
| 224 // Click a button and ensure that the lost focus notification was sent, |
| 225 // even though focus was gained without the mouse. |
| 226 EXPECT_TRUE(SimulateElementClick("button")); |
| 227 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
| 228 test_listener_.ClearTextAreaResults(); |
| 229 |
| 230 // Click a textarea field and test that no lost focus notifications are sent. |
| 231 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
| 232 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
| 233 test_listener_.ClearTextAreaResults(); |
| 234 |
| 235 // Select another textarea field to test that the notifcation for the |
| 236 // first textarea field losing focus is sent. |
| 237 EXPECT_TRUE(SimulateElementClick("textarea_2")); |
| 238 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
| 239 test_listener_.ClearTextAreaResults(); |
| 240 |
| 241 // Click the button, a notification should happen since a textarea field has |
| 242 // lost focus. |
| 243 EXPECT_TRUE(SimulateElementClick("button")); |
| 244 EXPECT_TRUE(test_listener_.formcontrol_element_lost_focus_called_); |
| 245 test_listener_.ClearTextAreaResults(); |
| 246 |
| 247 // Click on a textarea field while the button has focus and ensure no lost |
| 248 // focus notification is sent. |
| 249 EXPECT_TRUE(SimulateElementClick("textarea_1")); |
| 250 EXPECT_FALSE(test_listener_.formcontrol_element_lost_focus_called_); |
| 251 } |
| 164 } // namespace autofill | 252 } // namespace autofill |
| 253 |
| OLD | NEW |