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 OtherPossibleUsernamesToStr(*password_form)); |
364 base::ASCIIToUTF16("+"))); | |
365 } else { | 382 } else { |
366 EXPECT_TRUE(password_form->username_value.empty()); | 383 EXPECT_TRUE(password_form->username_value.empty()); |
367 EXPECT_TRUE(password_form->other_possible_usernames.empty()); | 384 EXPECT_TRUE(password_form->other_possible_usernames.empty()); |
368 } | 385 } |
369 | 386 |
370 // Do a basic sanity check that we are still having a password field. | 387 // Do a basic sanity check that we are still having a password field. |
371 EXPECT_EQ(base::UTF8ToUTF16("password"), password_form->password_element); | 388 EXPECT_EQ(base::UTF8ToUTF16("password"), password_form->password_element); |
372 EXPECT_EQ(base::UTF8ToUTF16("secret"), password_form->password_value); | 389 EXPECT_EQ(base::UTF8ToUTF16("secret"), password_form->password_value); |
373 } | 390 } |
374 } | 391 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), | 437 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), |
421 password_form->new_password_element); | 438 password_form->new_password_element); |
422 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), | 439 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), |
423 password_form->new_password_value); | 440 password_form->new_password_value); |
424 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), | 441 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), |
425 password_form->confirmation_password_element); | 442 password_form->confirmation_password_element); |
426 | 443 |
427 // Do a basic sanity check that we are still selecting the right username. | 444 // Do a basic sanity check that we are still selecting the right username. |
428 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); | 445 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); |
429 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); | 446 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); |
430 EXPECT_THAT(password_form->other_possible_usernames, | 447 EXPECT_THAT( |
431 testing::ElementsAre(base::UTF8ToUTF16("Smith"))); | 448 password_form->other_possible_usernames, |
| 449 testing::ElementsAre(PossibleUsernamePair( |
| 450 base::UTF8ToUTF16("Smith"), base::UTF8ToUTF16("username2")))); |
432 } | 451 } |
433 } | 452 } |
434 | 453 |
435 TEST_F(MAYBE_PasswordFormConversionUtilsTest, IdentifyingThreePasswordFields) { | 454 TEST_F(MAYBE_PasswordFormConversionUtilsTest, IdentifyingThreePasswordFields) { |
436 // Each test case consists of a set of parameters to be plugged into the | 455 // Each test case consists of a set of parameters to be plugged into the |
437 // PasswordFormBuilder below, plus the corresponding expectations. | 456 // PasswordFormBuilder below, plus the corresponding expectations. |
438 struct TestCase { | 457 struct TestCase { |
439 const char* password_values[3]; | 458 const char* password_values[3]; |
440 const char* expected_password_element; | 459 const char* expected_password_element; |
441 const char* expected_password_value; | 460 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), | 508 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), |
490 password_form->new_password_element); | 509 password_form->new_password_element); |
491 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), | 510 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), |
492 password_form->new_password_value); | 511 password_form->new_password_value); |
493 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), | 512 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_confirmation_element), |
494 password_form->confirmation_password_element); | 513 password_form->confirmation_password_element); |
495 | 514 |
496 // Do a basic sanity check that we are still selecting the right username. | 515 // Do a basic sanity check that we are still selecting the right username. |
497 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); | 516 EXPECT_EQ(base::UTF8ToUTF16("username1"), password_form->username_element); |
498 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); | 517 EXPECT_EQ(base::UTF8ToUTF16("William"), password_form->username_value); |
499 EXPECT_THAT(password_form->other_possible_usernames, | 518 EXPECT_THAT( |
500 testing::ElementsAre(base::UTF8ToUTF16("Smith"))); | 519 password_form->other_possible_usernames, |
| 520 testing::ElementsAre(PossibleUsernamePair( |
| 521 base::UTF8ToUTF16("Smith"), base::UTF8ToUTF16("username2")))); |
501 } | 522 } |
502 } | 523 } |
503 | 524 |
504 TEST_F(MAYBE_PasswordFormConversionUtilsTest, | 525 TEST_F(MAYBE_PasswordFormConversionUtilsTest, |
505 IdentifyingPasswordFieldsWithAutocompleteAttributes) { | 526 IdentifyingPasswordFieldsWithAutocompleteAttributes) { |
506 // Each test case consists of a set of parameters to be plugged into the | 527 // Each test case consists of a set of parameters to be plugged into the |
507 // PasswordFormBuilder below, plus the corresponding expectations. | 528 // PasswordFormBuilder below, plus the corresponding expectations. |
508 struct TestCase { | 529 struct TestCase { |
509 const char* autocomplete[3]; | 530 const char* autocomplete[3]; |
510 const char* expected_password_element; | 531 const char* expected_password_element; |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
841 ASSERT_TRUE(password_form); | 862 ASSERT_TRUE(password_form); |
842 | 863 |
843 // In the absence of username autocomplete attributes, the username should | 864 // In the absence of username autocomplete attributes, the username should |
844 // be the text input field just before 'current-password' or before | 865 // be the text input field just before 'current-password' or before |
845 // 'new-password', if there is no 'current-password'. | 866 // 'new-password', if there is no 'current-password'. |
846 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_element), | 867 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_element), |
847 password_form->username_element); | 868 password_form->username_element); |
848 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_value), | 869 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_username_value), |
849 password_form->username_value); | 870 password_form->username_value); |
850 if (strcmp(cases[i].expected_username_value, "William") == 0) { | 871 if (strcmp(cases[i].expected_username_value, "William") == 0) { |
851 EXPECT_THAT(password_form->other_possible_usernames, | 872 EXPECT_THAT( |
852 testing::ElementsAre(base::UTF8ToUTF16("Smith"))); | 873 password_form->other_possible_usernames, |
| 874 testing::ElementsAre(PossibleUsernamePair( |
| 875 base::UTF8ToUTF16("Smith"), base::UTF8ToUTF16("username2")))); |
853 } else { | 876 } else { |
854 EXPECT_THAT(password_form->other_possible_usernames, | 877 EXPECT_THAT( |
855 testing::ElementsAre(base::UTF8ToUTF16("William"))); | 878 password_form->other_possible_usernames, |
| 879 testing::ElementsAre(PossibleUsernamePair( |
| 880 base::UTF8ToUTF16("William"), base::UTF8ToUTF16("username1")))); |
856 } | 881 } |
857 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_element), | 882 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_element), |
858 password_form->password_element); | 883 password_form->password_element); |
859 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_value), | 884 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_password_value), |
860 password_form->password_value); | 885 password_form->password_value); |
861 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), | 886 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_element), |
862 password_form->new_password_element); | 887 password_form->new_password_element); |
863 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), | 888 EXPECT_EQ(base::UTF8ToUTF16(cases[i].expected_new_password_value), |
864 password_form->new_password_value); | 889 password_form->new_password_value); |
865 EXPECT_EQ(cases[i].expected_new_password_marked_by_site, | 890 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) { | 1498 TEST_F(MAYBE_PasswordFormConversionUtilsTest, TooManyFieldsToParseForm) { |
1474 PasswordFormBuilder builder(kTestFormActionURL); | 1499 PasswordFormBuilder builder(kTestFormActionURL); |
1475 for (size_t i = 0; i < form_util::kMaxParseableFields + 1; ++i) | 1500 for (size_t i = 0; i < form_util::kMaxParseableFields + 1; ++i) |
1476 builder.AddTextField("id", "value", "autocomplete"); | 1501 builder.AddTextField("id", "value", "autocomplete"); |
1477 std::unique_ptr<PasswordForm> password_form = | 1502 std::unique_ptr<PasswordForm> password_form = |
1478 LoadHTMLAndConvertForm(builder.ProduceHTML(), nullptr, false); | 1503 LoadHTMLAndConvertForm(builder.ProduceHTML(), nullptr, false); |
1479 EXPECT_FALSE(password_form); | 1504 EXPECT_FALSE(password_form); |
1480 } | 1505 } |
1481 | 1506 |
1482 } // namespace autofill | 1507 } // namespace autofill |
OLD | NEW |