| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // PasswordFormBuilder below, plus the corresponding expectations. | 291 // PasswordFormBuilder below, plus the corresponding expectations. |
| 292 struct TestCase { | 292 struct TestCase { |
| 293 const char* autocomplete[3]; | 293 const char* autocomplete[3]; |
| 294 const char* expected_username_element; | 294 const char* expected_username_element; |
| 295 const char* expected_username_value; | 295 const char* expected_username_value; |
| 296 const char* expected_other_possible_usernames; | 296 const char* expected_other_possible_usernames; |
| 297 } cases[] = { | 297 } cases[] = { |
| 298 // When no elements are marked with autocomplete='username', the text-type | 298 // When no elements are marked with autocomplete='username', the text-type |
| 299 // input field before the first password element should get selected as | 299 // input field before the first password element should get selected as |
| 300 // the username, and the rest should be marked as alternatives. | 300 // the username, and the rest should be marked as alternatives. |
| 301 {{nullptr, nullptr, nullptr}, | 301 {{nullptr, nullptr, nullptr}, "username2", "William", "John+Smith"}, |
| 302 "username2", | |
| 303 "William", | |
| 304 "John+username1, Smith+username3"}, | |
| 305 // When a sole element is marked with autocomplete='username', it should | 302 // When a sole element is marked with autocomplete='username', it should |
| 306 // be treated as the username for sure, with no other_possible_usernames. | 303 // be treated as the username for sure, with no other_possible_usernames. |
| 307 {{"username", nullptr, nullptr}, "username1", "John", ""}, | 304 {{"username", nullptr, nullptr}, "username1", "John", ""}, |
| 308 {{nullptr, "username", nullptr}, "username2", "William", ""}, | 305 {{nullptr, "username", nullptr}, "username2", "William", ""}, |
| 309 {{nullptr, nullptr, "username"}, "username3", "Smith", ""}, | 306 {{nullptr, nullptr, "username"}, "username3", "Smith", ""}, |
| 310 // When >=2 elements have the attribute, the first should be selected as | 307 // When >=2 elements have the attribute, the first should be selected as |
| 311 // the username, and the rest should go to other_possible_usernames. | 308 // the username, and the rest should go to other_possible_usernames. |
| 312 {{"username", "username", nullptr}, | 309 {{"username", "username", nullptr}, "username1", "John", "William"}, |
| 313 "username1", | 310 {{nullptr, "username", "username"}, "username2", "William", "Smith"}, |
| 314 "John", | 311 {{"username", nullptr, "username"}, "username1", "John", "Smith"}, |
| 315 "William+username2"}, | |
| 316 {{nullptr, "username", "username"}, | |
| 317 "username2", | |
| 318 "William", | |
| 319 "Smith+username3"}, | |
| 320 {{"username", nullptr, "username"}, | |
| 321 "username1", | |
| 322 "John", | |
| 323 "Smith+username3"}, | |
| 324 {{"username", "username", "username"}, | 312 {{"username", "username", "username"}, |
| 325 "username1", | 313 "username1", |
| 326 "John", | 314 "John", |
| 327 "William+username2, Smith+username3"}, | 315 "William+Smith"}, |
| 328 // When there is an empty autocomplete attribute (i.e. autocomplete=""), | 316 // When there is an empty autocomplete attribute (i.e. autocomplete=""), |
| 329 // it should have the same effect as having no attribute whatsoever. | 317 // it should have the same effect as having no attribute whatsoever. |
| 330 {{"", "", ""}, "username2", "William", "John+username1, Smith+username3"}, | 318 {{"", "", ""}, "username2", "William", "John+Smith"}, |
| 331 {{"", "", "username"}, "username3", "Smith", ""}, | 319 {{"", "", "username"}, "username3", "Smith", ""}, |
| 332 {{"username", "", "username"}, "username1", "John", "Smith+username3"}, | 320 {{"username", "", "username"}, "username1", "John", "Smith"}, |
| 333 // It should not matter if attribute values are upper or mixed case. | 321 // It should not matter if attribute values are upper or mixed case. |
| 334 {{"USERNAME", nullptr, "uSeRNaMe"}, | 322 {{"USERNAME", nullptr, "uSeRNaMe"}, "username1", "John", "Smith"}, |
| 335 "username1", | 323 {{"uSeRNaMe", nullptr, "USERNAME"}, "username1", "John", "Smith"}}; |
| 336 "John", | |
| 337 "Smith+username3"}, | |
| 338 {{"uSeRNaMe", nullptr, "USERNAME"}, | |
| 339 "username1", | |
| 340 "John", | |
| 341 "Smith+username3"}}; | |
| 342 | 324 |
| 343 for (size_t i = 0; i < arraysize(cases); ++i) { | 325 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 344 for (size_t nonempty_username_fields = 0; nonempty_username_fields < 2; | 326 for (size_t nonempty_username_fields = 0; nonempty_username_fields < 2; |
| 345 ++nonempty_username_fields) { | 327 ++nonempty_username_fields) { |
| 346 SCOPED_TRACE(testing::Message() | 328 SCOPED_TRACE(testing::Message() |
| 347 << "Iteration " << i << " " | 329 << "Iteration " << i << " " |
| 348 << (nonempty_username_fields ? "nonempty" : "empty")); | 330 << (nonempty_username_fields ? "nonempty" : "empty")); |
| 349 | 331 |
| 350 // Repeat each test once with empty, and once with non-empty usernames. | 332 // Repeat each test once with empty, and once with non-empty usernames. |
| 351 // In the former case, no empty other_possible_usernames should be saved. | 333 // In the former case, no empty other_possible_usernames should be saved. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 371 LoadHTMLAndConvertForm(html, nullptr, false); | 353 LoadHTMLAndConvertForm(html, nullptr, false); |
| 372 ASSERT_TRUE(password_form); | 354 ASSERT_TRUE(password_form); |
| 373 | 355 |
| 374 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_element), | 356 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_element), |
| 375 password_form->username_element); | 357 password_form->username_element); |
| 376 | 358 |
| 377 if (nonempty_username_fields) { | 359 if (nonempty_username_fields) { |
| 378 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_value), | 360 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_value), |
| 379 password_form->username_value); | 361 password_form->username_value); |
| 380 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_other_possible_usernames), | 362 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_other_possible_usernames), |
| 381 OtherPossibleUsernamesToString( | 363 base::JoinString(password_form->other_possible_usernames, |
| 382 password_form->other_possible_usernames)); | 364 base::ASCIIToUTF16("+"))); |
| 383 } else { | 365 } else { |
| 384 EXPECT_TRUE(password_form->username_value.empty()); | 366 EXPECT_TRUE(password_form->username_value.empty()); |
| 385 EXPECT_TRUE(password_form->other_possible_usernames.empty()); | 367 EXPECT_TRUE(password_form->other_possible_usernames.empty()); |
| 386 } | 368 } |
| 387 | 369 |
| 388 // Do a basic sanity check that we are still having a password field. | 370 // Do a basic sanity check that we are still having a password field. |
| 389 EXPECT_EQ(base::UTF8ToUTF16("password"), password_form->password_element); | 371 EXPECT_EQ(base::UTF8ToUTF16("password"), password_form->password_element); |
| 390 EXPECT_EQ(base::UTF8ToUTF16("secret"), password_form->password_value); | 372 EXPECT_EQ(base::UTF8ToUTF16("secret"), password_form->password_value); |
| 391 } | 373 } |
| 392 } | 374 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), | 420 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), |
| 439 password_form->new_password_element); | 421 password_form->new_password_element); |
| 440 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), | 422 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), |
| 441 password_form->new_password_value); | 423 password_form->new_password_value); |
| 442 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), | 424 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), |
| 443 password_form->confirmation_password_element); | 425 password_form->confirmation_password_element); |
| 444 | 426 |
| 445 // Do a basic sanity check that we are still selecting the right username. | 427 // Do a basic sanity check that we are still selecting the right username. |
| 446 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); | 428 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); |
| 447 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); | 429 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); |
| 448 EXPECT_THAT( | 430 EXPECT_THAT(password_form->other_possible_usernames, |
| 449 password_form->other_possible_usernames, | 431 testing::ElementsAre(base::UTF8ToUTF16("Smith"))); |
| 450 testing::ElementsAre(PossibleUsernamePair( | |
| 451 base::UTF8ToUTF16("Smith"), base::UTF8ToUTF16("username2")))); | |
| 452 } | 432 } |
| 453 } | 433 } |
| 454 | 434 |
| 455 TEST_F(MAYBE_PasswordFormConversionUtilsTest, IdentifyingThreePasswordFields) { | 435 TEST_F(MAYBE_PasswordFormConversionUtilsTest, IdentifyingThreePasswordFields) { |
| 456 // Each test case consists of a set of parameters to be plugged into the | 436 // Each test case consists of a set of parameters to be plugged into the |
| 457 // PasswordFormBuilder below, plus the corresponding expectations. | 437 // PasswordFormBuilder below, plus the corresponding expectations. |
| 458 struct TestCase { | 438 struct TestCase { |
| 459 const char* password_values[3]; | 439 const char* password_values[3]; |
| 460 const char* expected_password_element; | 440 const char* expected_password_element; |
| 461 const char* expected_password_value; | 441 const char* expected_password_value; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 509 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), | 489 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), |
| 510 password_form->new_password_element); | 490 password_form->new_password_element); |
| 511 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), | 491 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), |
| 512 password_form->new_password_value); | 492 password_form->new_password_value); |
| 513 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), | 493 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), |
| 514 password_form->confirmation_password_element); | 494 password_form->confirmation_password_element); |
| 515 | 495 |
| 516 // Do a basic sanity check that we are still selecting the right username. | 496 // Do a basic sanity check that we are still selecting the right username. |
| 517 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); | 497 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); |
| 518 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); | 498 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); |
| 519 EXPECT_THAT( | 499 EXPECT_THAT(password_form->other_possible_usernames, |
| 520 password_form->other_possible_usernames, | 500 testing::ElementsAre(base::UTF8ToUTF16("Smith"))); |
| 521 testing::ElementsAre(PossibleUsernamePair( | |
| 522 base::UTF8ToUTF16("Smith"), base::UTF8ToUTF16("username2")))); | |
| 523 } | 501 } |
| 524 } | 502 } |
| 525 | 503 |
| 526 TEST_F(MAYBE_PasswordFormConversionUtilsTest, | 504 TEST_F(MAYBE_PasswordFormConversionUtilsTest, |
| 527 IdentifyingPasswordFieldsWithAutocompleteAttributes) { | 505 IdentifyingPasswordFieldsWithAutocompleteAttributes) { |
| 528 // Each test case consists of a set of parameters to be plugged into the | 506 // Each test case consists of a set of parameters to be plugged into the |
| 529 // PasswordFormBuilder below, plus the corresponding expectations. | 507 // PasswordFormBuilder below, plus the corresponding expectations. |
| 530 struct TestCase { | 508 struct TestCase { |
| 531 const char* autocomplete[3]; | 509 const char* autocomplete[3]; |
| 532 const char* expected_password_element; | 510 const char* expected_password_element; |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 863 ASSERT_TRUE(password_form); | 841 ASSERT_TRUE(password_form); |
| 864 | 842 |
| 865 // In the absence of username autocomplete attributes, the username should | 843 // In the absence of username autocomplete attributes, the username should |
| 866 // be the text input field just before 'current-password' or before | 844 // be the text input field just before 'current-password' or before |
| 867 // 'new-password', if there is no 'current-password'. | 845 // 'new-password', if there is no 'current-password'. |
| 868 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_element), | 846 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_element), |
| 869 password_form->username_element); | 847 password_form->username_element); |
| 870 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_value), | 848 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_value), |
| 871 password_form->username_value); | 849 password_form->username_value); |
| 872 if (strcmp(cases[i].expected_username_value, "William") == 0) { | 850 if (strcmp(cases[i].expected_username_value, "William") == 0) { |
| 873 EXPECT_THAT( | 851 EXPECT_THAT(password_form->other_possible_usernames, |
| 874 password_form->other_possible_usernames, | 852 testing::ElementsAre(base::UTF8ToUTF16("Smith"))); |
| 875 testing::ElementsAre(PossibleUsernamePair( | |
| 876 base::UTF8ToUTF16("Smith"), base::UTF8ToUTF16("username2")))); | |
| 877 } else { | 853 } else { |
| 878 EXPECT_THAT( | 854 EXPECT_THAT(password_form->other_possible_usernames, |
| 879 password_form->other_possible_usernames, | 855 testing::ElementsAre(base::UTF8ToUTF16("William"))); |
| 880 testing::ElementsAre(PossibleUsernamePair( | |
| 881 base::UTF8ToUTF16("William"), base::UTF8ToUTF16("username1")))); | |
| 882 } | 856 } |
| 883 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_element), | 857 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_element), |
| 884 password_form->password_element); | 858 password_form->password_element); |
| 885 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_value), | 859 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_value), |
| 886 password_form->password_value); | 860 password_form->password_value); |
| 887 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), | 861 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), |
| 888 password_form->new_password_element); | 862 password_form->new_password_element); |
| 889 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), | 863 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), |
| 890 password_form->new_password_value); | 864 password_form->new_password_value); |
| 891 EXPECT_EQ(cases[i].expected_new_password_marked_by_site, | 865 EXPECT_EQ(cases[i].expected_new_password_marked_by_site, |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1499 TEST_F(MAYBE_PasswordFormConversionUtilsTest, TooManyFieldsToParseForm) { | 1473 TEST_F(MAYBE_PasswordFormConversionUtilsTest, TooManyFieldsToParseForm) { |
| 1500 PasswordFormBuilder builder(kTestFormActionURL); | 1474 PasswordFormBuilder builder(kTestFormActionURL); |
| 1501 for (size_t i = 0; i < form_util::kMaxParseableFields + 1; ++i) | 1475 for (size_t i = 0; i < form_util::kMaxParseableFields + 1; ++i) |
| 1502 builder.AddTextField("id", "value", "autocomplete"); | 1476 builder.AddTextField("id", "value", "autocomplete"); |
| 1503 std::unique_ptr<PasswordForm> password_form = | 1477 std::unique_ptr<PasswordForm> password_form = |
| 1504 LoadHTMLAndConvertForm(builder.ProduceHTML(), nullptr, false); | 1478 LoadHTMLAndConvertForm(builder.ProduceHTML(), nullptr, false); |
| 1505 EXPECT_FALSE(password_form); | 1479 EXPECT_FALSE(password_form); |
| 1506 } | 1480 } |
| 1507 | 1481 |
| 1508 } // namespace autofill | 1482 } // namespace autofill |
| OLD | NEW |