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