| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/string16.h" | 10 #include "base/string16.h" |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 "Miku Hatsune", "4234567890654321", month, year, | 404 "Miku Hatsune", "4234567890654321", month, year, |
| 405 has_address_fields, true, true); | 405 has_address_fields, true, true); |
| 406 } | 406 } |
| 407 | 407 |
| 408 class TestAutofillManager : public AutofillManager { | 408 class TestAutofillManager : public AutofillManager { |
| 409 public: | 409 public: |
| 410 TestAutofillManager(TabContentsWrapper* tab_contents, | 410 TestAutofillManager(TabContentsWrapper* tab_contents, |
| 411 TestPersonalDataManager* personal_data) | 411 TestPersonalDataManager* personal_data) |
| 412 : AutofillManager(tab_contents, personal_data), | 412 : AutofillManager(tab_contents, personal_data), |
| 413 personal_data_(personal_data), | 413 personal_data_(personal_data), |
| 414 autofill_enabled_(true) { | 414 autofill_enabled_(true), |
| 415 did_finish_async_form_submit_(false), |
| 416 message_loop_is_running_(false) { |
| 415 } | 417 } |
| 416 | 418 |
| 417 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } | 419 virtual bool IsAutofillEnabled() const OVERRIDE { return autofill_enabled_; } |
| 418 | 420 |
| 419 void set_autofill_enabled(bool autofill_enabled) { | 421 void set_autofill_enabled(bool autofill_enabled) { |
| 420 autofill_enabled_ = autofill_enabled; | 422 autofill_enabled_ = autofill_enabled; |
| 421 } | 423 } |
| 422 | 424 |
| 425 void set_expected_submitted_field_types( |
| 426 const std::vector<FieldTypeSet>& expected_types) { |
| 427 expected_submitted_field_types_ = expected_types; |
| 428 } |
| 429 |
| 430 virtual void UploadFormDataAsyncCallback( |
| 431 const FormStructure* submitted_form, |
| 432 const base::TimeTicks& load_time, |
| 433 const base::TimeTicks& interaction_time, |
| 434 const base::TimeTicks& submission_time) OVERRIDE { |
| 435 if (message_loop_is_running_) { |
| 436 MessageLoop::current()->Quit(); |
| 437 message_loop_is_running_ = false; |
| 438 } else { |
| 439 did_finish_async_form_submit_ = true; |
| 440 } |
| 441 |
| 442 // If we have expected field types set, make sure they match. |
| 443 if (!expected_submitted_field_types_.empty()) { |
| 444 ASSERT_EQ(expected_submitted_field_types_.size(), |
| 445 submitted_form->field_count()); |
| 446 for (size_t i = 0; i < expected_submitted_field_types_.size(); ++i) { |
| 447 SCOPED_TRACE( |
| 448 StringPrintf("Field %d with value %s", static_cast<int>(i), |
| 449 UTF16ToUTF8(submitted_form->field(i)->value).c_str())); |
| 450 const FieldTypeSet& possible_types = |
| 451 submitted_form->field(i)->possible_types(); |
| 452 EXPECT_EQ(expected_submitted_field_types_[i].size(), |
| 453 possible_types.size()); |
| 454 for (FieldTypeSet::const_iterator it = |
| 455 expected_submitted_field_types_[i].begin(); |
| 456 it != expected_submitted_field_types_[i].end(); ++it) { |
| 457 EXPECT_TRUE(possible_types.count(*it)) |
| 458 << "Expected type: " << AutofillType::FieldTypeToString(*it); |
| 459 } |
| 460 } |
| 461 } |
| 462 |
| 463 AutofillManager::UploadFormDataAsyncCallback(submitted_form, |
| 464 load_time, |
| 465 interaction_time, |
| 466 submission_time); |
| 467 } |
| 468 |
| 469 // Wait for the asynchronous OnFormSubmitted() call to complete. |
| 470 void WaitForAsyncFormSubmit() { |
| 471 if (!did_finish_async_form_submit_) { |
| 472 // TODO(isherman): It seems silly to need this variable. Is there some |
| 473 // way I can just query the message loop's state? |
| 474 message_loop_is_running_ = true; |
| 475 MessageLoop::current()->Run(); |
| 476 } else { |
| 477 did_finish_async_form_submit_ = false; |
| 478 } |
| 479 } |
| 480 |
| 423 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE { | 481 virtual void UploadFormData(const FormStructure& submitted_form) OVERRIDE { |
| 424 submitted_form_signature_ = submitted_form.FormSignature(); | 482 submitted_form_signature_ = submitted_form.FormSignature(); |
| 425 } | 483 } |
| 426 | 484 |
| 427 const std::string GetSubmittedFormSignature() { | 485 const std::string GetSubmittedFormSignature() { |
| 428 return submitted_form_signature_; | 486 return submitted_form_signature_; |
| 429 } | 487 } |
| 430 | 488 |
| 431 AutofillProfile* GetProfileWithGUID(const char* guid) { | 489 AutofillProfile* GetProfileWithGUID(const char* guid) { |
| 432 return personal_data_->GetProfileWithGUID(guid); | 490 return personal_data_->GetProfileWithGUID(guid); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 445 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id); | 503 base::StringPrintf("00000000-0000-0000-0000-%012d", credit_card_id); |
| 446 | 504 |
| 447 return PackGUIDs(GUIDPair(credit_card_guid, 0), GUIDPair(std::string(), 0)); | 505 return PackGUIDs(GUIDPair(credit_card_guid, 0), GUIDPair(std::string(), 0)); |
| 448 } | 506 } |
| 449 | 507 |
| 450 void AddSeenForm(FormStructure* form) { | 508 void AddSeenForm(FormStructure* form) { |
| 451 form_structures()->push_back(form); | 509 form_structures()->push_back(form); |
| 452 } | 510 } |
| 453 | 511 |
| 454 private: | 512 private: |
| 513 // AutofillManager is ref counted. |
| 514 virtual ~TestAutofillManager() {} |
| 515 |
| 455 // Weak reference. | 516 // Weak reference. |
| 456 TestPersonalDataManager* personal_data_; | 517 TestPersonalDataManager* personal_data_; |
| 518 |
| 457 bool autofill_enabled_; | 519 bool autofill_enabled_; |
| 520 |
| 521 bool did_finish_async_form_submit_; |
| 522 bool message_loop_is_running_; |
| 523 |
| 458 std::string submitted_form_signature_; | 524 std::string submitted_form_signature_; |
| 525 std::vector<FieldTypeSet> expected_submitted_field_types_; |
| 459 | 526 |
| 460 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); | 527 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); |
| 461 }; | 528 }; |
| 462 | 529 |
| 463 } // namespace | 530 } // namespace |
| 464 | 531 |
| 465 class AutofillManagerTest : public TabContentsWrapperTestHarness { | 532 class AutofillManagerTest : public TabContentsWrapperTestHarness { |
| 466 public: | 533 public: |
| 467 typedef AutofillManager::GUIDPair GUIDPair; | 534 typedef AutofillManager::GUIDPair GUIDPair; |
| 468 | 535 |
| 469 AutofillManagerTest() | 536 AutofillManagerTest() |
| 470 : TabContentsWrapperTestHarness(), | 537 : TabContentsWrapperTestHarness(), |
| 471 browser_thread_(BrowserThread::UI, &message_loop_) { | 538 ui_thread_(BrowserThread::UI, &message_loop_), |
| 539 file_thread_(BrowserThread::FILE) { |
| 472 } | 540 } |
| 473 | 541 |
| 474 virtual ~AutofillManagerTest() { | 542 virtual ~AutofillManagerTest() { |
| 475 // Order of destruction is important as AutofillManager relies on | 543 // Order of destruction is important as AutofillManager relies on |
| 476 // PersonalDataManager to be around when it gets destroyed. | 544 // PersonalDataManager to be around when it gets destroyed. |
| 477 autofill_manager_.reset(NULL); | 545 autofill_manager_ = NULL; |
| 478 } | 546 } |
| 479 | 547 |
| 480 virtual void SetUp() { | 548 virtual void SetUp() OVERRIDE { |
| 481 Profile* profile = new TestingProfile(); | 549 Profile* profile = new TestingProfile(); |
| 482 browser_context_.reset(profile); | 550 browser_context_.reset(profile); |
| 483 PersonalDataManagerFactory::GetInstance()->SetTestingFactory( | 551 PersonalDataManagerFactory::GetInstance()->SetTestingFactory( |
| 484 profile, TestPersonalDataManager::Build); | 552 profile, TestPersonalDataManager::Build); |
| 485 | 553 |
| 486 TabContentsWrapperTestHarness::SetUp(); | 554 TabContentsWrapperTestHarness::SetUp(); |
| 487 autofill_manager_.reset(new TestAutofillManager(contents_wrapper(), | 555 autofill_manager_ = new TestAutofillManager(contents_wrapper(), |
| 488 &personal_data_)); | 556 &personal_data_); |
| 557 |
| 558 file_thread_.Start(); |
| 559 } |
| 560 |
| 561 virtual void TearDown() OVERRIDE { |
| 562 file_thread_.Stop(); |
| 563 TabContentsWrapperTestHarness::TearDown(); |
| 489 } | 564 } |
| 490 | 565 |
| 491 void GetAutofillSuggestions(int query_id, | 566 void GetAutofillSuggestions(int query_id, |
| 492 const webkit_glue::FormData& form, | 567 const webkit_glue::FormData& form, |
| 493 const webkit_glue::FormField& field) { | 568 const webkit_glue::FormField& field) { |
| 494 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field); | 569 autofill_manager_->OnQueryFormFieldAutofill(query_id, form, field); |
| 495 } | 570 } |
| 496 | 571 |
| 497 void GetAutofillSuggestions(const webkit_glue::FormData& form, | 572 void GetAutofillSuggestions(const webkit_glue::FormData& form, |
| 498 const webkit_glue::FormField& field) { | 573 const webkit_glue::FormField& field) { |
| 499 GetAutofillSuggestions(kDefaultPageID, form, field); | 574 GetAutofillSuggestions(kDefaultPageID, form, field); |
| 500 } | 575 } |
| 501 | 576 |
| 502 void AutocompleteSuggestionsReturned(const std::vector<string16>& result) { | 577 void AutocompleteSuggestionsReturned(const std::vector<string16>& result) { |
| 503 contents_wrapper()->autocomplete_history_manager()-> | 578 contents_wrapper()->autocomplete_history_manager()-> |
| 504 SendSuggestions(&result); | 579 SendSuggestions(&result); |
| 505 } | 580 } |
| 506 | 581 |
| 507 void FormsSeen(const std::vector<webkit_glue::FormData>& forms) { | 582 void FormsSeen(const std::vector<webkit_glue::FormData>& forms) { |
| 508 autofill_manager_->OnFormsSeen(forms, base::TimeTicks()); | 583 autofill_manager_->OnFormsSeen(forms, base::TimeTicks()); |
| 509 } | 584 } |
| 510 | 585 |
| 511 void FormSubmitted(const FormData& form) { | 586 void FormSubmitted(const FormData& form) { |
| 512 autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now()); | 587 if (autofill_manager_->OnFormSubmitted(form, base::TimeTicks::Now())) |
| 588 autofill_manager_->WaitForAsyncFormSubmit(); |
| 513 } | 589 } |
| 514 | 590 |
| 515 void FillAutofillFormData(int query_id, | 591 void FillAutofillFormData(int query_id, |
| 516 const webkit_glue::FormData& form, | 592 const webkit_glue::FormData& form, |
| 517 const webkit_glue::FormField& field, | 593 const webkit_glue::FormField& field, |
| 518 int unique_id) { | 594 int unique_id) { |
| 519 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id); | 595 autofill_manager_->OnFillAutofillFormData(query_id, form, field, unique_id); |
| 520 } | 596 } |
| 521 | 597 |
| 522 int PackGUIDs(const GUIDPair& cc_guid, const GUIDPair& profile_guid) const { | 598 int PackGUIDs(const GUIDPair& cc_guid, const GUIDPair& profile_guid) const { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 if (page_id) | 639 if (page_id) |
| 564 *page_id = autofill_param.a; | 640 *page_id = autofill_param.a; |
| 565 if (results) | 641 if (results) |
| 566 *results = autofill_param.b; | 642 *results = autofill_param.b; |
| 567 | 643 |
| 568 process()->sink().ClearMessages(); | 644 process()->sink().ClearMessages(); |
| 569 return true; | 645 return true; |
| 570 } | 646 } |
| 571 | 647 |
| 572 protected: | 648 protected: |
| 573 content::TestBrowserThread browser_thread_; | 649 content::TestBrowserThread ui_thread_; |
| 650 content::TestBrowserThread file_thread_; |
| 574 | 651 |
| 575 scoped_ptr<TestAutofillManager> autofill_manager_; | 652 scoped_refptr<TestAutofillManager> autofill_manager_; |
| 576 TestPersonalDataManager personal_data_; | 653 TestPersonalDataManager personal_data_; |
| 577 | 654 |
| 578 private: | 655 private: |
| 579 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTest); | 656 DISALLOW_COPY_AND_ASSIGN(AutofillManagerTest); |
| 580 }; | 657 }; |
| 581 | 658 |
| 582 class TestFormStructure : public FormStructure { | 659 class TestFormStructure : public FormStructure { |
| 583 public: | 660 public: |
| 584 explicit TestFormStructure(const FormData& form) : FormStructure(form) {} | 661 explicit TestFormStructure(const FormData& form) : FormStructure(form) {} |
| 585 virtual ~TestFormStructure() {} | 662 virtual ~TestFormStructure() {} |
| (...skipping 2180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2766 types.insert(UNKNOWN_TYPE); | 2843 types.insert(UNKNOWN_TYPE); |
| 2767 form.fields.push_back(field); | 2844 form.fields.push_back(field); |
| 2768 expected_types.push_back(types); | 2845 expected_types.push_back(types); |
| 2769 | 2846 |
| 2770 autofill_test::CreateTestFormField("", "20", "901", "text", &field); | 2847 autofill_test::CreateTestFormField("", "20", "901", "text", &field); |
| 2771 types.clear(); | 2848 types.clear(); |
| 2772 types.insert(UNKNOWN_TYPE); | 2849 types.insert(UNKNOWN_TYPE); |
| 2773 form.fields.push_back(field); | 2850 form.fields.push_back(field); |
| 2774 expected_types.push_back(types); | 2851 expected_types.push_back(types); |
| 2775 | 2852 |
| 2776 FormStructure form_structure(form); | 2853 autofill_manager_->set_expected_submitted_field_types(expected_types); |
| 2777 autofill_manager_->DeterminePossibleFieldTypesForUpload(&form_structure); | 2854 FormSubmitted(form); |
| 2778 | |
| 2779 ASSERT_EQ(expected_types.size(), form_structure.field_count()); | |
| 2780 for (size_t i = 0; i < expected_types.size(); ++i) { | |
| 2781 SCOPED_TRACE( | |
| 2782 StringPrintf("Field %d with value %s", static_cast<int>(i), | |
| 2783 UTF16ToUTF8(form_structure.field(i)->value).c_str())); | |
| 2784 const FieldTypeSet& possible_types = | |
| 2785 form_structure.field(i)->possible_types(); | |
| 2786 EXPECT_EQ(expected_types[i].size(), possible_types.size()); | |
| 2787 for (FieldTypeSet::const_iterator it = expected_types[i].begin(); | |
| 2788 it != expected_types[i].end(); ++it) { | |
| 2789 EXPECT_TRUE(possible_types.count(*it)) | |
| 2790 << "Expected type: " << AutofillType::FieldTypeToString(*it); | |
| 2791 } | |
| 2792 } | |
| 2793 } | |
| 2794 | |
| 2795 TEST_F(AutofillManagerTest, DeterminePossibleFieldTypesForUploadStressTest) { | |
| 2796 personal_data_.ClearAutofillProfiles(); | |
| 2797 const int kNumProfiles = 5; | |
| 2798 for (int i = 0; i < kNumProfiles; ++i) { | |
| 2799 AutofillProfile* profile = new AutofillProfile; | |
| 2800 autofill_test::SetProfileInfo(profile, | |
| 2801 StringPrintf("John%d", i).c_str(), | |
| 2802 "", | |
| 2803 StringPrintf("Doe%d", i).c_str(), | |
| 2804 StringPrintf("JohnDoe%d@somesite.com", | |
| 2805 i).c_str(), | |
| 2806 "", | |
| 2807 StringPrintf("%d 1st st.", i).c_str(), | |
| 2808 "", | |
| 2809 "Memphis", "Tennessee", "38116", "USA", | |
| 2810 StringPrintf("650234%04d", i).c_str()); | |
| 2811 profile->set_guid( | |
| 2812 StringPrintf("00000000-0000-0000-0001-00000000%04d", i).c_str()); | |
| 2813 personal_data_.AddProfile(profile); | |
| 2814 } | |
| 2815 FormData form; | |
| 2816 CreateTestAddressFormData(&form); | |
| 2817 ASSERT_LT(3U, form.fields.size()); | |
| 2818 form.fields[0].value = ASCIIToUTF16("6502340001"); | |
| 2819 form.fields[1].value = ASCIIToUTF16("John1"); | |
| 2820 form.fields[2].value = ASCIIToUTF16("12345"); | |
| 2821 FormStructure form_structure(form); | |
| 2822 autofill_manager_->DeterminePossibleFieldTypesForUpload(&form_structure); | |
| 2823 ASSERT_LT(3U, form_structure.field_count()); | |
| 2824 const FieldTypeSet& possible_types0 = | |
| 2825 form_structure.field(0)->possible_types(); | |
| 2826 EXPECT_EQ(2U, possible_types0.size()); | |
| 2827 EXPECT_TRUE(possible_types0.find(PHONE_HOME_WHOLE_NUMBER) != | |
| 2828 possible_types0.end()); | |
| 2829 EXPECT_TRUE(possible_types0.find(PHONE_HOME_CITY_AND_NUMBER) != | |
| 2830 possible_types0.end()); | |
| 2831 const FieldTypeSet& possible_types1 = | |
| 2832 form_structure.field(1)->possible_types(); | |
| 2833 EXPECT_EQ(1U, possible_types1.size()); | |
| 2834 EXPECT_TRUE(possible_types1.find(NAME_FIRST) != possible_types1.end()); | |
| 2835 const FieldTypeSet& possible_types2 = | |
| 2836 form_structure.field(2)->possible_types(); | |
| 2837 EXPECT_EQ(1U, possible_types2.size()); | |
| 2838 EXPECT_TRUE(possible_types2.find(UNKNOWN_TYPE) != | |
| 2839 possible_types2.end()); | |
| 2840 } | 2855 } |
| 2841 | 2856 |
| 2842 namespace { | 2857 namespace { |
| 2843 | 2858 |
| 2844 class MockAutofillExternalDelegate : public AutofillExternalDelegate { | 2859 class MockAutofillExternalDelegate : public AutofillExternalDelegate { |
| 2845 public: | 2860 public: |
| 2846 explicit MockAutofillExternalDelegate(TabContentsWrapper* wrapper) | 2861 explicit MockAutofillExternalDelegate(TabContentsWrapper* wrapper) |
| 2847 : AutofillExternalDelegate(wrapper) {} | 2862 : AutofillExternalDelegate(wrapper) {} |
| 2848 virtual ~MockAutofillExternalDelegate() {} | 2863 virtual ~MockAutofillExternalDelegate() {} |
| 2849 | 2864 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2896 | 2911 |
| 2897 AutofillManager* autofill_manager = contents_wrapper()->autofill_manager(); | 2912 AutofillManager* autofill_manager = contents_wrapper()->autofill_manager(); |
| 2898 EXPECT_TRUE(autofill_manager->external_delegate()); | 2913 EXPECT_TRUE(autofill_manager->external_delegate()); |
| 2899 | 2914 |
| 2900 AutocompleteHistoryManager* autocomplete_history_manager = | 2915 AutocompleteHistoryManager* autocomplete_history_manager = |
| 2901 contents_wrapper()->autocomplete_history_manager(); | 2916 contents_wrapper()->autocomplete_history_manager(); |
| 2902 EXPECT_TRUE(autocomplete_history_manager->external_delegate()); | 2917 EXPECT_TRUE(autocomplete_history_manager->external_delegate()); |
| 2903 } | 2918 } |
| 2904 | 2919 |
| 2905 #endif // OS_ANDROID | 2920 #endif // OS_ANDROID |
| OLD | NEW |