OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/test/base/chrome_render_view_test.h" | 9 #include "chrome/test/base/chrome_render_view_test.h" |
10 #include "components/autofill/content/common/autofill_messages.h" | 10 #include "components/autofill/content/common/autofill_messages.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 AutofillMsg_AccountCreationFormsDetected msg(0, forms); | 51 AutofillMsg_AccountCreationFormsDetected msg(0, forms); |
52 password_generation_->OnMessageReceived(msg); | 52 password_generation_->OnMessageReceived(msg); |
53 } | 53 } |
54 | 54 |
55 void ExpectPasswordGenerationAvailable(const char* element_id, | 55 void ExpectPasswordGenerationAvailable(const char* element_id, |
56 bool available) { | 56 bool available) { |
57 WebDocument document = GetMainFrame()->document(); | 57 WebDocument document = GetMainFrame()->document(); |
58 WebElement element = | 58 WebElement element = |
59 document.getElementById(WebString::fromUTF8(element_id)); | 59 document.getElementById(WebString::fromUTF8(element_id)); |
60 ASSERT_FALSE(element.isNull()); | 60 ASSERT_FALSE(element.isNull()); |
61 WebInputElement target_element = element.to<WebInputElement>(); | |
62 ExecuteJavaScript( | 61 ExecuteJavaScript( |
63 base::StringPrintf("document.getElementById('%s').focus();", | 62 base::StringPrintf("document.getElementById('%s').focus();", |
64 element_id).c_str()); | 63 element_id).c_str()); |
65 if (available) { | 64 if (available) { |
66 ASSERT_EQ(1u, password_generation_->messages().size()); | 65 ASSERT_EQ(1u, password_generation_->messages().size()); |
67 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, | 66 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, |
68 password_generation_->messages()[0]->type()); | 67 password_generation_->messages()[0]->type()); |
69 } else { | 68 } else { |
70 EXPECT_EQ(0u, password_generation_->messages().size()); | 69 EXPECT_EQ(0u, password_generation_->messages().size()); |
71 } | 70 } |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 ExpectPasswordGenerationAvailable("first_password", false); | 248 ExpectPasswordGenerationAvailable("first_password", false); |
250 | 249 |
251 // Receive the account creation forms detected message. Show password | 250 // Receive the account creation forms detected message. Show password |
252 // generation icon. | 251 // generation icon. |
253 LoadHTML(kAccountCreationFormHTML); | 252 LoadHTML(kAccountCreationFormHTML); |
254 SetNotBlacklistedMessage(kAccountCreationFormHTML); | 253 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
255 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); | 254 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
256 ExpectPasswordGenerationAvailable("first_password", true); | 255 ExpectPasswordGenerationAvailable("first_password", true); |
257 } | 256 } |
258 | 257 |
| 258 TEST_F(PasswordGenerationAgentTest, MaximumOfferSize) { |
| 259 LoadHTML(kAccountCreationFormHTML); |
| 260 SetNotBlacklistedMessage(kAccountCreationFormHTML); |
| 261 SetAccountCreationFormsDetectedMessage(kAccountCreationFormHTML); |
| 262 ExpectPasswordGenerationAvailable("first_password", true); |
| 263 |
| 264 WebDocument document = GetMainFrame()->document(); |
| 265 WebElement element = |
| 266 document.getElementById(WebString::fromUTF8("first_password")); |
| 267 ASSERT_FALSE(element.isNull()); |
| 268 WebInputElement first_password_element = element.to<WebInputElement>(); |
| 269 |
| 270 // Make a password just under maximum offer size. |
| 271 first_password_element.setValue( |
| 272 base::ASCIIToUTF16( |
| 273 std::string(password_generation_->kMaximumOfferSize - 1, 'a'))); |
| 274 // Cast to WebAutofillClient where textFieldDidChange() is public. |
| 275 static_cast<blink::WebAutofillClient*>(autofill_agent_)->textFieldDidChange( |
| 276 first_password_element); |
| 277 // textFieldDidChange posts a task, so we need to wait until it's been |
| 278 // processed. |
| 279 base::MessageLoop::current()->RunUntilIdle(); |
| 280 // There should now be a message to show the UI. |
| 281 ASSERT_EQ(1u, password_generation_->messages().size()); |
| 282 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, |
| 283 password_generation_->messages()[0]->type()); |
| 284 password_generation_->clear_messages(); |
| 285 |
| 286 // Simulate a user typing a password just over maximum offer size. |
| 287 first_password_element.setValue( |
| 288 base::ASCIIToUTF16( |
| 289 std::string(password_generation_->kMaximumOfferSize + 1, 'a'))); |
| 290 // Cast to WebAutofillClient where textFieldDidChange() is public. |
| 291 static_cast<blink::WebAutofillClient*>(autofill_agent_)->textFieldDidChange( |
| 292 first_password_element); |
| 293 // textFieldDidChange posts a task, so we need to wait until it's been |
| 294 // processed. |
| 295 base::MessageLoop::current()->RunUntilIdle(); |
| 296 // There should now be a message to hide the UI. |
| 297 ASSERT_EQ(1u, password_generation_->messages().size()); |
| 298 EXPECT_EQ(AutofillHostMsg_HidePasswordGenerationPopup::ID, |
| 299 password_generation_->messages()[0]->type()); |
| 300 password_generation_->clear_messages(); |
| 301 |
| 302 // Simulate the user deleting characters. The generation popup should be shown |
| 303 // again. |
| 304 first_password_element.setValue( |
| 305 base::ASCIIToUTF16( |
| 306 std::string(password_generation_->kMaximumOfferSize, 'a'))); |
| 307 // Cast to WebAutofillClient where textFieldDidChange() is public. |
| 308 static_cast<blink::WebAutofillClient*>(autofill_agent_)->textFieldDidChange( |
| 309 first_password_element); |
| 310 // textFieldDidChange posts a task, so we need to wait until it's been |
| 311 // processed. |
| 312 base::MessageLoop::current()->RunUntilIdle(); |
| 313 // There should now be a message to show the UI. |
| 314 ASSERT_EQ(1u, password_generation_->messages().size()); |
| 315 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, |
| 316 password_generation_->messages()[0]->type()); |
| 317 password_generation_->clear_messages(); |
| 318 |
| 319 // Change focus. Bubble should be hidden, but that is handled by AutofilAgent, |
| 320 // so no messages are sent. |
| 321 ExecuteJavaScript("document.getElementById('username').focus();"); |
| 322 EXPECT_EQ(0u, password_generation_->messages().size()); |
| 323 password_generation_->clear_messages(); |
| 324 |
| 325 // Focusing the password field will bring up the generation UI again. |
| 326 ExecuteJavaScript("document.getElementById('first_password').focus();"); |
| 327 EXPECT_EQ(1u, password_generation_->messages().size()); |
| 328 EXPECT_EQ(AutofillHostMsg_ShowPasswordGenerationPopup::ID, |
| 329 password_generation_->messages()[0]->type()); |
| 330 password_generation_->clear_messages(); |
| 331 } |
| 332 |
259 } // namespace autofill | 333 } // namespace autofill |
OLD | NEW |