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 "components/password_manager/core/browser/password_autofill_manager.h" | 5 #include "components/password_manager/core/browser/password_autofill_manager.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
7 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
10 #include "components/autofill/core/browser/popup_item_ids.h" | 11 #include "components/autofill/core/browser/popup_item_ids.h" |
11 #include "components/autofill/core/browser/suggestion_test_helpers.h" | 12 #include "components/autofill/core/browser/suggestion_test_helpers.h" |
12 #include "components/autofill/core/browser/test_autofill_client.h" | 13 #include "components/autofill/core/browser/test_autofill_client.h" |
13 #include "components/autofill/core/browser/test_autofill_driver.h" | 14 #include "components/autofill/core/browser/test_autofill_driver.h" |
14 #include "components/autofill/core/common/autofill_constants.h" | 15 #include "components/autofill/core/common/autofill_constants.h" |
| 16 #include "components/autofill/core/common/autofill_switches.h" |
15 #include "components/autofill/core/common/form_field_data.h" | 17 #include "components/autofill/core/common/form_field_data.h" |
16 #include "components/autofill/core/common/password_form_fill_data.h" | 18 #include "components/autofill/core/common/password_form_fill_data.h" |
17 #include "components/password_manager/core/browser/stub_password_manager_client.
h" | 19 #include "components/password_manager/core/browser/stub_password_manager_client.
h" |
18 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" | 20 #include "components/password_manager/core/browser/stub_password_manager_driver.
h" |
19 #include "components/strings/grit/components_strings.h" | 21 #include "components/strings/grit/components_strings.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/gfx/geometry/rect_f.h" | 25 #include "ui/gfx/geometry/rect_f.h" |
24 | 26 |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 element_bounds, _, | 349 element_bounds, _, |
348 SuggestionVectorValuesAre(testing::UnorderedElementsAre( | 350 SuggestionVectorValuesAre(testing::UnorderedElementsAre( |
349 title, | 351 title, |
350 test_username_)), | 352 test_username_)), |
351 _)); | 353 _)); |
352 password_autofill_manager_->OnShowPasswordSuggestions( | 354 password_autofill_manager_->OnShowPasswordSuggestions( |
353 dummy_key, base::i18n::RIGHT_TO_LEFT, test_username_, | 355 dummy_key, base::i18n::RIGHT_TO_LEFT, test_username_, |
354 autofill::IS_PASSWORD_FIELD, element_bounds); | 356 autofill::IS_PASSWORD_FIELD, element_bounds); |
355 } | 357 } |
356 | 358 |
| 359 // Test that suggestion tokens (substrings separated by characters from |
| 360 // " .,-_@") are matched against field contents. |
| 361 TEST_F(PasswordAutofillManagerTest, DisplaySuggestionsWithMatchingTokens) { |
| 362 // Token matching is currently behind a flag. |
| 363 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 364 autofill::switches::kEnableSuggestionsWithSubstringMatch); |
| 365 |
| 366 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); |
| 367 scoped_ptr<MockAutofillClient> autofill_client(new MockAutofillClient); |
| 368 InitializePasswordAutofillManager(client.get(), autofill_client.get()); |
| 369 |
| 370 gfx::RectF element_bounds; |
| 371 autofill::PasswordFormFillData data; |
| 372 base::string16 username = base::ASCIIToUTF16("foo.bar@example.com"); |
| 373 data.username_field.value = username; |
| 374 data.password_field.value = base::ASCIIToUTF16("foobar"); |
| 375 data.preferred_realm = "http://foo.com/"; |
| 376 |
| 377 autofill::PasswordAndRealm additional; |
| 378 additional.realm = "https://foobarrealm.org"; |
| 379 base::string16 additional_username(base::ASCIIToUTF16("bar.foo@example.com")); |
| 380 data.additional_logins[additional_username] = additional; |
| 381 |
| 382 autofill::UsernamesCollectionKey usernames_key; |
| 383 usernames_key.realm = "http://yetanother.net"; |
| 384 std::vector<base::string16> other_names; |
| 385 base::string16 other_username(base::ASCIIToUTF16("example@foo.com")); |
| 386 other_names.push_back(other_username); |
| 387 data.other_possible_usernames[usernames_key] = other_names; |
| 388 |
| 389 int dummy_key = 0; |
| 390 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data); |
| 391 |
| 392 // Simulate displaying suggestions for field contents "foo", check that |
| 393 // matching ones are displayed. |
| 394 EXPECT_CALL( |
| 395 *autofill_client, |
| 396 ShowAutofillPopup(element_bounds, _, |
| 397 SuggestionVectorValuesAre(testing::UnorderedElementsAre( |
| 398 username, additional_username, other_username)), |
| 399 _)); |
| 400 password_autofill_manager_->OnShowPasswordSuggestions( |
| 401 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("foo"), false, |
| 402 element_bounds); |
| 403 } |
| 404 |
| 405 // Test that suggestions which do not have a prefix match or prefix-token match |
| 406 // with the field contents are not matched. |
| 407 TEST_F(PasswordAutofillManagerTest, NoSuggestionForNonPrefixTokenMatch) { |
| 408 // Token matching is currently behind a flag. |
| 409 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 410 autofill::switches::kEnableSuggestionsWithSubstringMatch); |
| 411 |
| 412 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); |
| 413 scoped_ptr<MockAutofillClient> autofill_client(new MockAutofillClient); |
| 414 InitializePasswordAutofillManager(client.get(), autofill_client.get()); |
| 415 |
| 416 gfx::RectF element_bounds; |
| 417 autofill::PasswordFormFillData data; |
| 418 base::string16 username = base::ASCIIToUTF16("foo.bar@example.com"); |
| 419 data.username_field.value = username; |
| 420 data.password_field.value = base::ASCIIToUTF16("foobar"); |
| 421 data.preferred_realm = "http://foo.com/"; |
| 422 |
| 423 autofill::PasswordAndRealm additional; |
| 424 additional.realm = "https://foobarrealm.org"; |
| 425 base::string16 additional_username(base::ASCIIToUTF16("bar.foo@example.com")); |
| 426 data.additional_logins[additional_username] = additional; |
| 427 |
| 428 autofill::UsernamesCollectionKey usernames_key; |
| 429 usernames_key.realm = "http://yetanother.net"; |
| 430 std::vector<base::string16> other_names; |
| 431 base::string16 other_username(base::ASCIIToUTF16("example@foo.com")); |
| 432 other_names.push_back(other_username); |
| 433 data.other_possible_usernames[usernames_key] = other_names; |
| 434 |
| 435 int dummy_key = 0; |
| 436 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data); |
| 437 |
| 438 // Simulate displaying suggestions for field contents "oo". Check that none |
| 439 // appear, because none has a token with a prefix "oo". |
| 440 EXPECT_CALL(*autofill_client, ShowAutofillPopup(_, _, _, _)).Times(0); |
| 441 |
| 442 password_autofill_manager_->OnShowPasswordSuggestions( |
| 443 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("oo"), false, |
| 444 element_bounds); |
| 445 } |
| 446 |
| 447 // Test matching when field contents contains suggestion token separators. |
| 448 TEST_F(PasswordAutofillManagerTest, |
| 449 MatchingContentsWithSuggestionTokenSeparator) { |
| 450 // Token matching is currently behind a flag. |
| 451 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 452 autofill::switches::kEnableSuggestionsWithSubstringMatch); |
| 453 |
| 454 scoped_ptr<TestPasswordManagerClient> client(new TestPasswordManagerClient); |
| 455 scoped_ptr<MockAutofillClient> autofill_client(new MockAutofillClient); |
| 456 InitializePasswordAutofillManager(client.get(), autofill_client.get()); |
| 457 |
| 458 gfx::RectF element_bounds; |
| 459 autofill::PasswordFormFillData data; |
| 460 base::string16 username = base::ASCIIToUTF16("foo.bar@example.com"); |
| 461 data.username_field.value = username; |
| 462 data.password_field.value = base::ASCIIToUTF16("foobar"); |
| 463 data.preferred_realm = "http://foo.com/"; |
| 464 |
| 465 autofill::PasswordAndRealm additional; |
| 466 additional.realm = "https://foobarrealm.org"; |
| 467 base::string16 additional_username(base::ASCIIToUTF16("bar.foo@example.com")); |
| 468 data.additional_logins[additional_username] = additional; |
| 469 |
| 470 autofill::UsernamesCollectionKey usernames_key; |
| 471 usernames_key.realm = "http://yetanother.net"; |
| 472 std::vector<base::string16> other_names; |
| 473 base::string16 other_username(base::ASCIIToUTF16("example@foo.com")); |
| 474 other_names.push_back(other_username); |
| 475 data.other_possible_usernames[usernames_key] = other_names; |
| 476 |
| 477 int dummy_key = 0; |
| 478 password_autofill_manager_->OnAddPasswordFormMapping(dummy_key, data); |
| 479 |
| 480 // Simulate displaying suggestions for field contents "foo@exam", check that |
| 481 // matching one is displayed. Please also note here that field contents |
| 482 // contains token separators '@', |IsContentsPrefixOfSuggestionToken()| only |
| 483 // picks suggestions that passes 2 constrains; firstly whether field contents |
| 484 // is a substring of suggestions, and secondly whether character right before |
| 485 // suggestions offset is one of the splitting characters. In our test case, |
| 486 // only |additional_username| passes both the constrains and apparently we |
| 487 // could see only that in matching suggestion list. |
| 488 EXPECT_CALL( |
| 489 *autofill_client, |
| 490 ShowAutofillPopup(element_bounds, _, |
| 491 SuggestionVectorValuesAre( |
| 492 testing::UnorderedElementsAre(additional_username)), |
| 493 _)); |
| 494 password_autofill_manager_->OnShowPasswordSuggestions( |
| 495 dummy_key, base::i18n::RIGHT_TO_LEFT, base::ASCIIToUTF16("foo@exam"), |
| 496 false, element_bounds); |
| 497 } |
| 498 |
357 } // namespace password_manager | 499 } // namespace password_manager |
OLD | NEW |