OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "app/keyboard_codes.h" | 5 #include "app/keyboard_codes.h" |
6 #include "base/string_util.h" | 6 #include "base/string_util.h" |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/renderer/autofill_helper.h" |
8 #include "chrome/renderer/password_autocomplete_manager.h" | 9 #include "chrome/renderer/password_autocomplete_manager.h" |
9 #include "chrome/test/render_view_test.h" | 10 #include "chrome/test/render_view_test.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" | 12 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
12 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" | 13 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" |
13 #include "third_party/WebKit/WebKit/chromium/public/WebFormElement.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebFormElement.h" |
14 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebInputElement.h" |
15 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" |
16 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" |
17 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebVector.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 public: | 56 public: |
56 PasswordAutocompleteManagerTest() { | 57 PasswordAutocompleteManagerTest() { |
57 } | 58 } |
58 | 59 |
59 // Simulates the fill password form message being sent to the renderer. | 60 // Simulates the fill password form message being sent to the renderer. |
60 // We use that so we don't have to make RenderView::OnFillPasswordForm() | 61 // We use that so we don't have to make RenderView::OnFillPasswordForm() |
61 // protected. | 62 // protected. |
62 void SimulateOnFillPasswordForm( | 63 void SimulateOnFillPasswordForm( |
63 const PasswordFormFillData& fill_data) { | 64 const PasswordFormFillData& fill_data) { |
64 ViewMsg_FillPasswordForm msg(0, fill_data); | 65 ViewMsg_FillPasswordForm msg(0, fill_data); |
65 view_->OnMessageReceived(msg); | 66 password_autocomplete_->OnMessageReceived(msg); |
66 } | 67 } |
67 | 68 |
68 virtual void SetUp() { | 69 virtual void SetUp() { |
69 RenderViewTest::SetUp(); | 70 RenderViewTest::SetUp(); |
70 | 71 |
71 // Add a preferred login and an additional login to the FillData. | 72 // Add a preferred login and an additional login to the FillData. |
72 username1_ = ASCIIToUTF16(kAliceUsername); | 73 username1_ = ASCIIToUTF16(kAliceUsername); |
73 password1_ = ASCIIToUTF16(kAlicePassword); | 74 password1_ = ASCIIToUTF16(kAlicePassword); |
74 username2_ = ASCIIToUTF16(kBobUsername); | 75 username2_ = ASCIIToUTF16(kBobUsername); |
75 password2_ = ASCIIToUTF16(kBobPassword); | 76 password2_ = ASCIIToUTF16(kBobPassword); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 username_element_.setAutofilled(false); | 111 username_element_.setAutofilled(false); |
111 password_element_.setValue(""); | 112 password_element_.setValue(""); |
112 password_element_.setAutofilled(false); | 113 password_element_.setAutofilled(false); |
113 } | 114 } |
114 | 115 |
115 void SimulateUsernameChange(const std::string& username, | 116 void SimulateUsernameChange(const std::string& username, |
116 bool move_caret_to_end) { | 117 bool move_caret_to_end) { |
117 username_element_.setValue(WebString::fromUTF8(username)); | 118 username_element_.setValue(WebString::fromUTF8(username)); |
118 if (move_caret_to_end) | 119 if (move_caret_to_end) |
119 username_element_.setSelectionRange(username.length(), username.length()); | 120 username_element_.setSelectionRange(username.length(), username.length()); |
120 view_->textFieldDidChange(username_element_); | 121 autofill_helper_->textFieldDidChange(username_element_); |
121 // Processing is delayed because of a WebKit bug, see | 122 // Processing is delayed because of a WebKit bug, see |
122 // PasswordAutocompleteManager::TextDidChangeInTextField() for details. | 123 // PasswordAutocompleteManager::TextDidChangeInTextField() for details. |
123 MessageLoop::current()->RunAllPending(); | 124 MessageLoop::current()->RunAllPending(); |
124 } | 125 } |
125 | 126 |
126 void SimulateKeyDownEvent(const WebInputElement& element, | 127 void SimulateKeyDownEvent(const WebInputElement& element, |
127 app::KeyboardCode key_code) { | 128 app::KeyboardCode key_code) { |
128 WebKit::WebKeyboardEvent key_event; | 129 WebKit::WebKeyboardEvent key_event; |
129 key_event.windowsKeyCode = key_code; | 130 key_event.windowsKeyCode = key_code; |
130 view_->textFieldDidReceiveKeyDown(element, key_event); | 131 autofill_helper_->textFieldDidReceiveKeyDown(element, key_event); |
131 } | 132 } |
132 | 133 |
133 void CheckTextFieldsState(const std::string& username, | 134 void CheckTextFieldsState(const std::string& username, |
134 bool username_autofilled, | 135 bool username_autofilled, |
135 const std::string& password, | 136 const std::string& password, |
136 bool password_autofilled) { | 137 bool password_autofilled) { |
137 EXPECT_EQ(username, | 138 EXPECT_EQ(username, |
138 static_cast<std::string>(username_element_.value().utf8())); | 139 static_cast<std::string>(username_element_.value().utf8())); |
139 EXPECT_EQ(username_autofilled, username_element_.isAutofilled()); | 140 EXPECT_EQ(username_autofilled, username_element_.isAutofilled()); |
140 EXPECT_EQ(password, | 141 EXPECT_EQ(password, |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 SimulateUsernameChange("a", true); | 237 SimulateUsernameChange("a", true); |
237 CheckTextFieldsState("a", false, "", false); | 238 CheckTextFieldsState("a", false, "", false); |
238 SimulateUsernameChange("al", true); | 239 SimulateUsernameChange("al", true); |
239 CheckTextFieldsState("al", false, "", false); | 240 CheckTextFieldsState("al", false, "", false); |
240 SimulateUsernameChange(kAliceUsername, true); | 241 SimulateUsernameChange(kAliceUsername, true); |
241 CheckTextFieldsState(kAliceUsername, false, "", false); | 242 CheckTextFieldsState(kAliceUsername, false, "", false); |
242 | 243 |
243 // Autocomplete should happen only when the username textfield is blurred with | 244 // Autocomplete should happen only when the username textfield is blurred with |
244 // a full match. | 245 // a full match. |
245 username_element_.setValue("a"); | 246 username_element_.setValue("a"); |
246 view_->textFieldDidEndEditing(username_element_); | 247 autofill_helper_->textFieldDidEndEditing(username_element_); |
247 CheckTextFieldsState("a", false, "", false); | 248 CheckTextFieldsState("a", false, "", false); |
248 username_element_.setValue("al"); | 249 username_element_.setValue("al"); |
249 view_->textFieldDidEndEditing(username_element_); | 250 autofill_helper_->textFieldDidEndEditing(username_element_); |
250 CheckTextFieldsState("al", false, "", false); | 251 CheckTextFieldsState("al", false, "", false); |
251 username_element_.setValue("alices"); | 252 username_element_.setValue("alices"); |
252 view_->textFieldDidEndEditing(username_element_); | 253 autofill_helper_->textFieldDidEndEditing(username_element_); |
253 CheckTextFieldsState("alices", false, "", false); | 254 CheckTextFieldsState("alices", false, "", false); |
254 username_element_.setValue(ASCIIToUTF16(kAliceUsername)); | 255 username_element_.setValue(ASCIIToUTF16(kAliceUsername)); |
255 view_->textFieldDidEndEditing(username_element_); | 256 autofill_helper_->textFieldDidEndEditing(username_element_); |
256 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); | 257 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); |
257 } | 258 } |
258 | 259 |
259 // Tests that inline autocompletion works properly. | 260 // Tests that inline autocompletion works properly. |
260 TEST_F(PasswordAutocompleteManagerTest, InlineAutocomplete) { | 261 TEST_F(PasswordAutocompleteManagerTest, InlineAutocomplete) { |
261 // Simulate the browser sending back the login info. | 262 // Simulate the browser sending back the login info. |
262 SimulateOnFillPasswordForm(fill_data_); | 263 SimulateOnFillPasswordForm(fill_data_); |
263 | 264 |
264 // Clear the textfields to start fresh. | 265 // Clear the textfields to start fresh. |
265 ClearUsernameAndPasswordFields(); | 266 ClearUsernameAndPasswordFields(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 // Simulate the browser sending back the login info. | 320 // Simulate the browser sending back the login info. |
320 SimulateOnFillPasswordForm(fill_data_); | 321 SimulateOnFillPasswordForm(fill_data_); |
321 | 322 |
322 // Clear the textfields to start fresh. | 323 // Clear the textfields to start fresh. |
323 ClearUsernameAndPasswordFields(); | 324 ClearUsernameAndPasswordFields(); |
324 | 325 |
325 // To simulate a selection in the suggestion drop-down we just mimick what the | 326 // To simulate a selection in the suggestion drop-down we just mimick what the |
326 // WebView does: it sets the element value then calls | 327 // WebView does: it sets the element value then calls |
327 // didAcceptAutocompleteSuggestion on the renderer. | 328 // didAcceptAutocompleteSuggestion on the renderer. |
328 username_element_.setValue(ASCIIToUTF16(kAliceUsername)); | 329 username_element_.setValue(ASCIIToUTF16(kAliceUsername)); |
329 view_->didAcceptAutocompleteSuggestion(username_element_); | 330 autofill_helper_->didAcceptAutocompleteSuggestion(username_element_); |
330 | 331 |
331 // Autocomplete should have kicked in. | 332 // Autocomplete should have kicked in. |
332 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); | 333 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); |
333 } | 334 } |
OLD | NEW |