Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: components/autofill/core/browser/autofill_manager_unittest.cc

Issue 1494373003: [Autofill] Send Autofill upload when active form loses focus. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed test Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <algorithm> 5 #include <algorithm>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 class TestAutofillManager : public AutofillManager { 464 class TestAutofillManager : public AutofillManager {
465 public: 465 public:
466 TestAutofillManager(AutofillDriver* driver, 466 TestAutofillManager(AutofillDriver* driver,
467 AutofillClient* client, 467 AutofillClient* client,
468 TestPersonalDataManager* personal_data) 468 TestPersonalDataManager* personal_data)
469 : AutofillManager(driver, client, personal_data), 469 : AutofillManager(driver, client, personal_data),
470 personal_data_(personal_data), 470 personal_data_(personal_data),
471 autofill_enabled_(true), 471 autofill_enabled_(true),
472 credit_card_upload_enabled_(true), 472 credit_card_upload_enabled_(true),
473 credit_card_was_uploaded_(false), 473 credit_card_was_uploaded_(false),
474 expect_all_unknown_possible_types_(false) { 474 expect_all_unknown_possible_types_(false),
475 expected_observed_submission_(true) {
475 set_payments_client( 476 set_payments_client(
476 new TestPaymentsClient(driver->GetURLRequestContext(), this)); 477 new TestPaymentsClient(driver->GetURLRequestContext(), this));
477 } 478 }
478 ~TestAutofillManager() override {} 479 ~TestAutofillManager() override {}
479 480
480 bool IsAutofillEnabled() const override { return autofill_enabled_; } 481 bool IsAutofillEnabled() const override { return autofill_enabled_; }
481 482
482 void set_autofill_enabled(bool autofill_enabled) { 483 void set_autofill_enabled(bool autofill_enabled) {
483 autofill_enabled_ = autofill_enabled; 484 autofill_enabled_ = autofill_enabled;
484 } 485 }
485 486
486 bool IsCreditCardUploadEnabled() override { 487 bool IsCreditCardUploadEnabled() override {
487 return credit_card_upload_enabled_; 488 return credit_card_upload_enabled_;
488 } 489 }
489 490
490 void set_credit_card_upload_enabled(bool credit_card_upload_enabled) { 491 void set_credit_card_upload_enabled(bool credit_card_upload_enabled) {
491 credit_card_upload_enabled_ = credit_card_upload_enabled; 492 credit_card_upload_enabled_ = credit_card_upload_enabled;
492 } 493 }
493 494
494 bool credit_card_was_uploaded() { return credit_card_was_uploaded_; } 495 bool credit_card_was_uploaded() { return credit_card_was_uploaded_; }
495 496
496 void set_expected_submitted_field_types( 497 void set_expected_submitted_field_types(
497 const std::vector<ServerFieldTypeSet>& expected_types) { 498 const std::vector<ServerFieldTypeSet>& expected_types) {
498 expected_submitted_field_types_ = expected_types; 499 expected_submitted_field_types_ = expected_types;
499 } 500 }
500 501
501 void UploadFormDataAsyncCallback( 502 void set_expected_observed_submission(bool expected) {
502 const FormStructure* submitted_form, 503 expected_observed_submission_ = expected;
503 const base::TimeTicks& load_time, 504 }
504 const base::TimeTicks& interaction_time, 505
505 const base::TimeTicks& submission_time) override { 506 void UploadFormDataAsyncCallback(const FormStructure* submitted_form,
507 const base::TimeTicks& load_time,
508 const base::TimeTicks& interaction_time,
509 const base::TimeTicks& submission_time,
510 bool observed_submission) override {
506 run_loop_->Quit(); 511 run_loop_->Quit();
507 512
513 EXPECT_EQ(expected_observed_submission_, observed_submission);
514
508 // If we have expected field types set, make sure they match. 515 // If we have expected field types set, make sure they match.
509 if (!expected_submitted_field_types_.empty()) { 516 if (!expected_submitted_field_types_.empty()) {
510 ASSERT_EQ(expected_submitted_field_types_.size(), 517 ASSERT_EQ(expected_submitted_field_types_.size(),
511 submitted_form->field_count()); 518 submitted_form->field_count());
512 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) { 519 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) {
513 SCOPED_TRACE( 520 SCOPED_TRACE(
514 base::StringPrintf( 521 base::StringPrintf(
515 "Field %d with value %s", static_cast<int>(i), 522 "Field %d with value %s", static_cast<int>(i),
516 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str())); 523 base::UTF16ToUTF8(submitted_form->field(i)->value).c_str()));
517 const ServerFieldTypeSet& possible_types = 524 const ServerFieldTypeSet& possible_types =
518 submitted_form->field(i)->possible_types(); 525 submitted_form->field(i)->possible_types();
519 EXPECT_EQ(expected_submitted_field_types_[i].size(), 526 EXPECT_EQ(expected_submitted_field_types_[i].size(),
520 possible_types.size()); 527 possible_types.size());
521 for (ServerFieldTypeSet::const_iterator it = 528 for (ServerFieldTypeSet::const_iterator it =
522 expected_submitted_field_types_[i].begin(); 529 expected_submitted_field_types_[i].begin();
523 it != expected_submitted_field_types_[i].end(); ++it) { 530 it != expected_submitted_field_types_[i].end(); ++it) {
524 EXPECT_TRUE(possible_types.count(*it)) 531 EXPECT_TRUE(possible_types.count(*it))
525 << "Expected type: " << AutofillType(*it).ToString(); 532 << "Expected type: " << AutofillType(*it).ToString();
526 } 533 }
527 } 534 }
528 } 535 }
529 536
530 AutofillManager::UploadFormDataAsyncCallback(submitted_form, 537 AutofillManager::UploadFormDataAsyncCallback(
531 load_time, 538 submitted_form, load_time, interaction_time, submission_time,
532 interaction_time, 539 observed_submission);
533 submission_time);
534 } 540 }
535 541
536 // Resets the run loop so that it can wait for an asynchronous form 542 // Resets the run loop so that it can wait for an asynchronous form
537 // submission to complete. 543 // submission to complete.
538 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); } 544 void ResetRunLoop() { run_loop_.reset(new base::RunLoop()); }
539 545
540 // Wait for the asynchronous OnWillSubmitForm() call to complete. 546 // Wait for the asynchronous calls within StartUploadProcess() to complete.
541 void WaitForAsyncOnWillSubmitForm() { run_loop_->Run(); } 547 void WaitForAsyncUploadProcess() { run_loop_->Run(); }
542 548
543 void UploadFormData(const FormStructure& submitted_form) override { 549 void UploadFormData(const FormStructure& submitted_form) override {
544 submitted_form_signature_ = submitted_form.FormSignature(); 550 submitted_form_signature_ = submitted_form.FormSignature();
545 } 551 }
546 552
547 const std::string GetSubmittedFormSignature() { 553 const std::string GetSubmittedFormSignature() {
548 return submitted_form_signature_; 554 return submitted_form_signature_;
549 } 555 }
550 556
551 AutofillProfile* GetProfileWithGUID(const char* guid) { 557 AutofillProfile* GetProfileWithGUID(const char* guid) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 credit_card_was_uploaded_ = true; 590 credit_card_was_uploaded_ = true;
585 }; 591 };
586 592
587 // Weak reference. 593 // Weak reference.
588 TestPersonalDataManager* personal_data_; 594 TestPersonalDataManager* personal_data_;
589 595
590 bool autofill_enabled_; 596 bool autofill_enabled_;
591 bool credit_card_upload_enabled_; 597 bool credit_card_upload_enabled_;
592 bool credit_card_was_uploaded_; 598 bool credit_card_was_uploaded_;
593 bool expect_all_unknown_possible_types_; 599 bool expect_all_unknown_possible_types_;
600 bool expected_observed_submission_;
594 601
595 scoped_ptr<base::RunLoop> run_loop_; 602 scoped_ptr<base::RunLoop> run_loop_;
596 603
597 std::string submitted_form_signature_; 604 std::string submitted_form_signature_;
598 std::vector<ServerFieldTypeSet> expected_submitted_field_types_; 605 std::vector<ServerFieldTypeSet> expected_submitted_field_types_;
599 606
600 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); 607 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager);
601 }; 608 };
602 609
603 class TestAutofillExternalDelegate : public AutofillExternalDelegate { 610 class TestAutofillExternalDelegate : public AutofillExternalDelegate {
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result); 759 autofill_manager_->autocomplete_history_manager_->SendSuggestions(&result);
753 } 760 }
754 761
755 void FormsSeen(const std::vector<FormData>& forms) { 762 void FormsSeen(const std::vector<FormData>& forms) {
756 autofill_manager_->OnFormsSeen(forms, base::TimeTicks()); 763 autofill_manager_->OnFormsSeen(forms, base::TimeTicks());
757 } 764 }
758 765
759 void FormSubmitted(const FormData& form) { 766 void FormSubmitted(const FormData& form) {
760 autofill_manager_->ResetRunLoop(); 767 autofill_manager_->ResetRunLoop();
761 if (autofill_manager_->OnWillSubmitForm(form, base::TimeTicks::Now())) 768 if (autofill_manager_->OnWillSubmitForm(form, base::TimeTicks::Now()))
762 autofill_manager_->WaitForAsyncOnWillSubmitForm(); 769 autofill_manager_->WaitForAsyncUploadProcess();
763 autofill_manager_->OnFormSubmitted(form); 770 autofill_manager_->OnFormSubmitted(form);
764 } 771 }
765 772
766 void FillAutofillFormData(int query_id, 773 void FillAutofillFormData(int query_id,
767 const FormData& form, 774 const FormData& form,
768 const FormFieldData& field, 775 const FormFieldData& field,
769 int unique_id) { 776 int unique_id) {
770 autofill_manager_->FillOrPreviewForm( 777 autofill_manager_->FillOrPreviewForm(
771 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field, 778 AutofillDriver::FORM_DATA_ACTION_FILL, query_id, form, field,
772 unique_id); 779 unique_id);
(...skipping 1710 matching lines...) Expand 10 before | Expand all | Expand 10 after
2483 MakeFrontendID(std::string(), guid), 2490 MakeFrontendID(std::string(), guid),
2484 &response_page_id, &response_data); 2491 &response_page_id, &response_data);
2485 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID, 2492 ExpectFilledAddressFormElvis(response_page_id, response_data, kDefaultPageID,
2486 false); 2493 false);
2487 2494
2488 // Simulate OnWillSubmitForm(). We should *not* be calling into the PDM at 2495 // Simulate OnWillSubmitForm(). We should *not* be calling into the PDM at
2489 // this point (since the form was not submitted). Does not call 2496 // this point (since the form was not submitted). Does not call
2490 // OnFormSubmitted. 2497 // OnFormSubmitted.
2491 autofill_manager_->ResetRunLoop(); 2498 autofill_manager_->ResetRunLoop();
2492 autofill_manager_->OnWillSubmitForm(response_data, base::TimeTicks::Now()); 2499 autofill_manager_->OnWillSubmitForm(response_data, base::TimeTicks::Now());
2493 autofill_manager_->WaitForAsyncOnWillSubmitForm(); 2500 autofill_manager_->WaitForAsyncUploadProcess();
2494 EXPECT_EQ(0, personal_data_.num_times_save_imported_profile_called()); 2501 EXPECT_EQ(0, personal_data_.num_times_save_imported_profile_called());
2495 } 2502 }
2496 2503
2497 // Test that when Autocomplete is enabled and Autofill is disabled, form 2504 // Test that when Autocomplete is enabled and Autofill is disabled, form
2498 // submissions are still received by AutocompleteHistoryManager. 2505 // submissions are still received by AutocompleteHistoryManager.
2499 TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) { 2506 TEST_F(AutofillManagerTest, FormSubmittedAutocompleteEnabled) {
2500 TestAutofillClient client; 2507 TestAutofillClient client;
2501 autofill_manager_.reset( 2508 autofill_manager_.reset(
2502 new TestAutofillManager(autofill_driver_.get(), &client, NULL)); 2509 new TestAutofillManager(autofill_driver_.get(), &client, NULL));
2503 autofill_manager_->set_autofill_enabled(false); 2510 autofill_manager_->set_autofill_enabled(false);
(...skipping 838 matching lines...) Expand 10 before | Expand all | Expand 10 after
3342 FormData form; 3349 FormData form;
3343 test::CreateTestAddressFormData(&form); 3350 test::CreateTestAddressFormData(&form);
3344 std::vector<FormData> forms(1, form); 3351 std::vector<FormData> forms(1, form);
3345 FormsSeen(forms); 3352 FormsSeen(forms);
3346 const FormFieldData& field = form.fields[0]; 3353 const FormFieldData& field = form.fields[0];
3347 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() 3354 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery()
3348 3355
3349 EXPECT_TRUE(external_delegate_->on_query_seen()); 3356 EXPECT_TRUE(external_delegate_->on_query_seen());
3350 } 3357 }
3351 3358
3359 // Test that unfocusing a filled form sends an upload with types matching the
3360 // fields.
3361 TEST_F(AutofillManagerTest, OnTextFieldDidChangeAndUnfocus_Upload) {
3362 // Set up our form data (it's already filled out with user data).
3363 FormData form;
3364 form.name = ASCIIToUTF16("MyForm");
3365 form.origin = GURL("http://myform.com/form.html");
3366 form.action = GURL("http://myform.com/submit.html");
3367
3368 std::vector<ServerFieldTypeSet> expected_types;
3369 ServerFieldTypeSet types;
3370
3371 FormFieldData field;
3372 test::CreateTestFormField("First Name", "firstname", "Elvis", "text", &field);
3373 form.fields.push_back(field);
3374 types.insert(NAME_FIRST);
3375 expected_types.push_back(types);
3376
3377 test::CreateTestFormField("Last Name", "lastname", "Presley", "text", &field);
3378 form.fields.push_back(field);
3379 types.clear();
3380 types.insert(NAME_LAST);
3381 expected_types.push_back(types);
3382
3383 test::CreateTestFormField("Email", "email", "theking@gmail.com", "text",
3384 &field);
3385 form.fields.push_back(field);
3386 types.clear();
3387 types.insert(EMAIL_ADDRESS);
3388 expected_types.push_back(types);
3389
3390 FormsSeen(std::vector<FormData>(1, form));
3391
3392 // We will expect these types in the upload and no observed submission (the
3393 // callback initiated by WaitForAsyncUploadProcess checks these expectations.)
3394 autofill_manager_->set_expected_submitted_field_types(expected_types);
3395 autofill_manager_->set_expected_observed_submission(false);
3396
3397 // Simulate editing a field.
3398 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
3399 base::TimeTicks::Now());
3400
3401 autofill_manager_->ResetRunLoop();
3402 // Simulate lost of focus on the form.
3403 autofill_manager_->OnFocusNoLongerOnForm();
3404 // Wait for upload to complete (will check expected types as well).
3405 autofill_manager_->WaitForAsyncUploadProcess();
3406 }
3407
3408 // Test that navigating with a filled form sends an upload with types matching
3409 // the fields.
3410 TEST_F(AutofillManagerTest, OnTextFieldDidChangeAndNavigation_Upload) {
3411 // Set up our form data (it's already filled out with user data).
3412 FormData form;
3413 form.name = ASCIIToUTF16("MyForm");
3414 form.origin = GURL("http://myform.com/form.html");
3415 form.action = GURL("http://myform.com/submit.html");
3416
3417 std::vector<ServerFieldTypeSet> expected_types;
3418 ServerFieldTypeSet types;
3419
3420 FormFieldData field;
3421 test::CreateTestFormField("First Name", "firstname", "Elvis", "text", &field);
3422 form.fields.push_back(field);
3423 types.insert(NAME_FIRST);
3424 expected_types.push_back(types);
3425
3426 test::CreateTestFormField("Last Name", "lastname", "Presley", "text", &field);
3427 form.fields.push_back(field);
3428 types.clear();
3429 types.insert(NAME_LAST);
3430 expected_types.push_back(types);
3431
3432 test::CreateTestFormField("Email", "email", "theking@gmail.com", "text",
3433 &field);
3434 form.fields.push_back(field);
3435 types.clear();
3436 types.insert(EMAIL_ADDRESS);
3437 expected_types.push_back(types);
3438
3439 FormsSeen(std::vector<FormData>(1, form));
3440
3441 // We will expect these types in the upload and no observed submission. (the
3442 // callback initiated by WaitForAsyncUploadProcess checks these expectations.)
3443 autofill_manager_->set_expected_submitted_field_types(expected_types);
3444 autofill_manager_->set_expected_observed_submission(false);
3445
3446 // Simulate editing a field.
3447 autofill_manager_->OnTextFieldDidChange(form, form.fields.front(),
3448 base::TimeTicks::Now());
3449
3450 autofill_manager_->ResetRunLoop();
3451 // Simulate a navigation so that the pending form is uploaded.
3452 autofill_manager_->Reset();
3453 // Wait for upload to complete (will check expected types as well).
3454 autofill_manager_->WaitForAsyncUploadProcess();
3455 }
3456
3457 // Test that unfocusing a filled form sends an upload with types matching the
3458 // fields.
3459 TEST_F(AutofillManagerTest, OnDidFillAutofillFormDataAndUnfocus_Upload) {
3460 // Set up our form data (empty).
3461 FormData form;
3462 form.name = ASCIIToUTF16("MyForm");
3463 form.origin = GURL("http://myform.com/form.html");
3464 form.action = GURL("http://myform.com/submit.html");
3465
3466 std::vector<ServerFieldTypeSet> expected_types;
3467
3468 // These fields should all match.
3469 ServerFieldTypeSet types;
3470 FormFieldData field;
3471 test::CreateTestFormField("First Name", "firstname", "", "text", &field);
3472 form.fields.push_back(field);
3473 types.insert(NAME_FIRST);
3474 expected_types.push_back(types);
3475
3476 test::CreateTestFormField("Last Name", "lastname", "", "text", &field);
3477 form.fields.push_back(field);
3478 types.clear();
3479 types.insert(NAME_LAST);
3480 expected_types.push_back(types);
3481
3482 test::CreateTestFormField("Email", "email", "", "text", &field);
3483 form.fields.push_back(field);
3484 types.clear();
3485 types.insert(EMAIL_ADDRESS);
3486 expected_types.push_back(types);
3487
3488 FormsSeen(std::vector<FormData>(1, form));
3489
3490 // We will expect these types in the upload and no observed submission. (the
3491 // callback initiated by WaitForAsyncUploadProcess checks these expectations.)
3492 autofill_manager_->set_expected_submitted_field_types(expected_types);
3493 autofill_manager_->set_expected_observed_submission(false);
3494
3495 // Form was autofilled with user data.
3496 form.fields[0].value = base::ASCIIToUTF16("Elvis");
3497 form.fields[1].value = base::ASCIIToUTF16("Presley");
3498 form.fields[2].value = base::ASCIIToUTF16("theking@gmail.com");
3499 autofill_manager_->OnDidFillAutofillFormData(form, base::TimeTicks::Now());
3500
3501 autofill_manager_->ResetRunLoop();
3502 // Simulate lost of focus on the form.
3503 autofill_manager_->OnFocusNoLongerOnForm();
3504 // Wait for upload to complete.
3505 autofill_manager_->WaitForAsyncUploadProcess();
3506 }
3507
3352 // Test to verify suggestions appears for forms having credit card number split 3508 // Test to verify suggestions appears for forms having credit card number split
3353 // across fields. 3509 // across fields.
3354 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) { 3510 TEST_F(AutofillManagerTest, GetCreditCardSuggestionsForNumberSpitAcrossFields) {
3355 // Set up our form data with credit card number split across fields. 3511 // Set up our form data with credit card number split across fields.
3356 FormData form; 3512 FormData form;
3357 form.name = ASCIIToUTF16("MyForm"); 3513 form.name = ASCIIToUTF16("MyForm");
3358 form.origin = GURL("https://myform.com/form.html"); 3514 form.origin = GURL("https://myform.com/form.html");
3359 form.action = GURL("https://myform.com/submit.html"); 3515 form.action = GURL("https://myform.com/submit.html");
3360 3516
3361 FormFieldData name_field; 3517 FormFieldData name_field;
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
3864 // Make sure it's reset for the next test case. 4020 // Make sure it's reset for the next test case.
3865 autofill_driver_->SetIsOffTheRecord(false); 4021 autofill_driver_->SetIsOffTheRecord(false);
3866 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_4)); 4022 EXPECT_TRUE(autofill_manager_->ShouldUploadForm(form_structure_4));
3867 4023
3868 // Autofill disabled. 4024 // Autofill disabled.
3869 autofill_manager_->set_autofill_enabled(false); 4025 autofill_manager_->set_autofill_enabled(false);
3870 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_3)); 4026 EXPECT_FALSE(autofill_manager_->ShouldUploadForm(form_structure_3));
3871 } 4027 }
3872 4028
3873 } // namespace autofill 4029 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/core/browser/autofill_manager.cc ('k') | components/autofill/core/browser/autofill_metrics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698