Chromium Code Reviews| 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/password_autocomplete_manager.h" | 8 #include "chrome/renderer/password_autocomplete_manager.h" |
| 9 #include "chrome/test/render_view_test.h" | 9 #include "chrome/test/render_view_test.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 // The name of the username/password element in the form. | 33 // The name of the username/password element in the form. |
| 34 const char* const kUsernameName = "username"; | 34 const char* const kUsernameName = "username"; |
| 35 const char* const kPasswordName = "password"; | 35 const char* const kPasswordName = "password"; |
| 36 | 36 |
| 37 const char* const kAliceUsername = "alice"; | 37 const char* const kAliceUsername = "alice"; |
| 38 const char* const kAlicePassword = "password"; | 38 const char* const kAlicePassword = "password"; |
| 39 const char* const kBobUsername = "bob"; | 39 const char* const kBobUsername = "bob"; |
| 40 const char* const kBobPassword = "secret"; | 40 const char* const kBobPassword = "secret"; |
| 41 const char* const kCarolUsername = "Carol"; | |
| 42 const char* const kCarolPassword = "test"; | |
| 43 | |
| 41 | 44 |
| 42 const char* const kFormHTML = | 45 const char* const kFormHTML = |
| 43 "<FORM name='LoginTestForm' action='http://www.bidule.com'>" | 46 "<FORM name='LoginTestForm' action='http://www.bidule.com'>" |
| 44 " <INPUT type='text' id='username'/>" | 47 " <INPUT type='text' id='username'/>" |
| 45 " <INPUT type='password' id='password'/>" | 48 " <INPUT type='password' id='password'/>" |
| 46 " <INPUT type='submit' value='Login'/>" | 49 " <INPUT type='submit' value='Login'/>" |
| 47 "</FORM>"; | 50 "</FORM>"; |
| 48 | 51 |
| 49 class PasswordAutocompleteManagerTest : public RenderViewTest { | 52 class PasswordAutocompleteManagerTest : public RenderViewTest { |
| 50 public: | 53 public: |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 61 } | 64 } |
| 62 | 65 |
| 63 virtual void SetUp() { | 66 virtual void SetUp() { |
| 64 RenderViewTest::SetUp(); | 67 RenderViewTest::SetUp(); |
| 65 | 68 |
| 66 // Add a preferred login and an additional login to the FillData. | 69 // Add a preferred login and an additional login to the FillData. |
| 67 username1_ = ASCIIToUTF16(kAliceUsername); | 70 username1_ = ASCIIToUTF16(kAliceUsername); |
| 68 password1_ = ASCIIToUTF16(kAlicePassword); | 71 password1_ = ASCIIToUTF16(kAlicePassword); |
| 69 username2_ = ASCIIToUTF16(kBobUsername); | 72 username2_ = ASCIIToUTF16(kBobUsername); |
| 70 password2_ = ASCIIToUTF16(kBobPassword); | 73 password2_ = ASCIIToUTF16(kBobPassword); |
| 74 username3_ = ASCIIToUTF16(kCarolUsername); | |
| 75 password3_ = ASCIIToUTF16(kCarolPassword); | |
| 71 | 76 |
| 72 fill_data_.basic_data.fields.push_back( | 77 fill_data_.basic_data.fields.push_back( |
| 73 FormField(string16(), ASCIIToUTF16(kUsernameName), | 78 FormField(string16(), ASCIIToUTF16(kUsernameName), |
| 74 username1_, string16(), 0)); | 79 username1_, string16(), 0)); |
| 75 fill_data_.basic_data.fields.push_back( | 80 fill_data_.basic_data.fields.push_back( |
| 76 FormField(string16(), ASCIIToUTF16(kPasswordName), | 81 FormField(string16(), ASCIIToUTF16(kPasswordName), |
| 77 password1_, string16(), 0)); | 82 password1_, string16(), 0)); |
| 78 fill_data_.additional_logins[username2_] = password2_; | 83 fill_data_.additional_logins[username2_] = password2_; |
| 84 fill_data_.additional_logins[username3_] = password3_; | |
| 79 | 85 |
| 80 // We need to set the origin so it matches the frame URL and the action so | 86 // We need to set the origin so it matches the frame URL and the action so |
| 81 // it matches the form action, otherwise we won't autocomplete. | 87 // it matches the form action, otherwise we won't autocomplete. |
| 82 std::string origin("data:text/html;charset=utf-8,"); | 88 std::string origin("data:text/html;charset=utf-8,"); |
| 83 origin += kFormHTML; | 89 origin += kFormHTML; |
| 84 fill_data_.basic_data.origin = GURL(origin); | 90 fill_data_.basic_data.origin = GURL(origin); |
| 85 fill_data_.basic_data.action = GURL("http://www.bidule.com"); | 91 fill_data_.basic_data.action = GURL("http://www.bidule.com"); |
| 86 | 92 |
| 87 LoadHTML(kFormHTML); | 93 LoadHTML(kFormHTML); |
| 88 | 94 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 EXPECT_EQ(password_autofilled, password_element_.isAutofilled()); | 140 EXPECT_EQ(password_autofilled, password_element_.isAutofilled()); |
| 135 } | 141 } |
| 136 | 142 |
| 137 void CheckUsernameSelection(int start, int end) { | 143 void CheckUsernameSelection(int start, int end) { |
| 138 EXPECT_EQ(start, username_element_.selectionStart()); | 144 EXPECT_EQ(start, username_element_.selectionStart()); |
| 139 EXPECT_EQ(end, username_element_.selectionEnd()); | 145 EXPECT_EQ(end, username_element_.selectionEnd()); |
| 140 } | 146 } |
| 141 | 147 |
| 142 string16 username1_; | 148 string16 username1_; |
| 143 string16 username2_; | 149 string16 username2_; |
| 150 string16 username3_; | |
| 144 string16 password1_; | 151 string16 password1_; |
| 145 string16 password2_; | 152 string16 password2_; |
| 153 string16 password3_; | |
| 146 PasswordFormFillData fill_data_; | 154 PasswordFormFillData fill_data_; |
| 147 | 155 |
| 148 WebInputElement username_element_; | 156 WebInputElement username_element_; |
| 149 WebInputElement password_element_; | 157 WebInputElement password_element_; |
| 150 | 158 |
| 151 private: | 159 private: |
| 152 DISALLOW_COPY_AND_ASSIGN(PasswordAutocompleteManagerTest); | 160 DISALLOW_COPY_AND_ASSIGN(PasswordAutocompleteManagerTest); |
| 153 }; | 161 }; |
| 154 | 162 |
| 155 // Tests that the password login is autocompleted as expected when the browser | 163 // Tests that the password login is autocompleted as expected when the browser |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 // should be empty. | 291 // should be empty. |
| 284 SimulateUsernameChange("alf", true); | 292 SimulateUsernameChange("alf", true); |
| 285 CheckTextFieldsState("alf", false, "", false); | 293 CheckTextFieldsState("alf", false, "", false); |
| 286 CheckUsernameSelection(3, 3); // No selection. | 294 CheckUsernameSelection(3, 3); // No selection. |
| 287 | 295 |
| 288 // Ok, so now the user removes all the text and enters the letter 'b'. | 296 // Ok, so now the user removes all the text and enters the letter 'b'. |
| 289 SimulateUsernameChange("b", true); | 297 SimulateUsernameChange("b", true); |
| 290 // The username and password fields should match the 'bob' entry. | 298 // The username and password fields should match the 'bob' entry. |
| 291 CheckTextFieldsState(kBobUsername, true, kBobPassword, true); | 299 CheckTextFieldsState(kBobUsername, true, kBobPassword, true); |
| 292 CheckUsernameSelection(1, 3); | 300 CheckUsernameSelection(1, 3); |
| 301 | |
| 302 // Then, the user again removes all the text and types an uppercase 'C'. | |
| 303 SimulateUsernameChange("C", true); | |
| 304 // The username and password fields should match the 'Carol' entry. | |
| 305 CheckTextFieldsState(kCarolUsername, true, kCarolPassword, true); | |
| 306 CheckUsernameSelection(1, 5); | |
| 307 // Finally, the user removes all the text and types a lowercase 'c'. We only | |
| 308 // want case-sensitive autocompletion, so the username and the selected range | |
| 309 // should be empty. | |
| 310 SimulateUsernameChange("c", true); | |
| 311 CheckTextFieldsState("c", false, "", false); | |
| 312 CheckUsernameSelection(1, 1); | |
| 293 } | 313 } |
| 294 | 314 |
| 295 // Tests that selecting and item in the suggestion drop-down works. | 315 // Tests that selecting and item in the suggestion drop-down works. |
| 296 TEST_F(PasswordAutocompleteManagerTest, SuggestionSelect) { | 316 TEST_F(PasswordAutocompleteManagerTest, SuggestionSelect) { |
| 297 // Simulate the browser sending back the login info. | 317 // Simulate the browser sending back the login info. |
| 298 SimulateOnFillPasswordForm(fill_data_); | 318 SimulateOnFillPasswordForm(fill_data_); |
| 299 | 319 |
| 300 // Clear the textfields to start fresh. | 320 // Clear the textfields to start fresh. |
| 301 ClearUsernameAndPasswordFields(); | 321 ClearUsernameAndPasswordFields(); |
| 302 | 322 |
| 303 // To simulate a selection in the suggestion drop-down we just mimick what the | 323 // To simulate a selection in the suggestion drop-down we just mimick what the |
| 304 // WebView does: it sets the element value then calls | 324 // WebView does: it sets the element value then calls |
| 305 // didAcceptAutocompleteSuggestion on the renderer. | 325 // didAcceptAutocompleteSuggestion on the renderer. |
| 306 username_element_.setValue(ASCIIToUTF16(kAliceUsername)); | 326 username_element_.setValue(ASCIIToUTF16(kAliceUsername)); |
| 307 view_->didAcceptAutocompleteSuggestion(username_element_); | 327 view_->didAcceptAutocompleteSuggestion(username_element_); |
| 308 | 328 |
| 309 // Autocomplete should have kicked in. | 329 // Autocomplete should have kicked in. |
| 310 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); | 330 CheckTextFieldsState(kAliceUsername, true, kAlicePassword, true); |
| 311 } | 331 } |
| 312 | 332 |
| 313 } // namespace | 333 } |
|
Ilya Sherman
2010/11/24 09:05:39
nit: We actually want the " // namespace" -- it i
| |
| OLD | NEW |