| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/prefs/public/pref_service_base.h" | 11 #include "base/prefs/public/pref_service_base.h" |
| 12 #include "base/string16.h" | 12 #include "base/string16.h" |
| 13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/tuple.h" | 16 #include "base/tuple.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "chrome/browser/autofill/autocomplete_history_manager.h" | 18 #include "chrome/browser/autofill/autocomplete_history_manager.h" |
| 19 #include "chrome/browser/autofill/autofill_common_test.h" | 19 #include "chrome/browser/autofill/autofill_common_test.h" |
| 20 #include "chrome/browser/autofill/autofill_manager.h" | 20 #include "chrome/browser/autofill/autofill_manager.h" |
| 21 #include "chrome/browser/autofill/autofill_profile.h" | 21 #include "chrome/browser/autofill/autofill_profile.h" |
| 22 #include "chrome/browser/autofill/credit_card.h" | 22 #include "chrome/browser/autofill/credit_card.h" |
| 23 #include "chrome/browser/autofill/personal_data_manager.h" | 23 #include "chrome/browser/autofill/personal_data_manager.h" |
| 24 #include "chrome/browser/autofill/personal_data_manager_factory.h" | 24 #include "chrome/browser/autofill/personal_data_manager_factory.h" |
| 25 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 25 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 26 #include "chrome/browser/password_manager/password_manager.h" |
| 27 #include "chrome/browser/password_manager/password_manager_delegate_impl.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/browser/sync/profile_sync_service.h" | 29 #include "chrome/browser/sync/profile_sync_service.h" |
| 28 #include "chrome/browser/sync/profile_sync_service_factory.h" | 30 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 29 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" | 31 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h" |
| 30 #include "chrome/browser/ui/browser.h" | 32 #include "chrome/browser/ui/browser.h" |
| 31 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 32 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" | |
| 33 #include "chrome/common/autofill_messages.h" | 33 #include "chrome/common/autofill_messages.h" |
| 34 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
| 35 #include "chrome/common/form_data.h" | 35 #include "chrome/common/form_data.h" |
| 36 #include "chrome/common/form_field_data.h" | 36 #include "chrome/common/form_field_data.h" |
| 37 #include "chrome/common/pref_names.h" | 37 #include "chrome/common/pref_names.h" |
| 38 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 38 #include "chrome/test/base/testing_profile.h" | 39 #include "chrome/test/base/testing_profile.h" |
| 39 #include "content/public/browser/web_contents.h" | 40 #include "content/public/browser/web_contents.h" |
| 40 #include "content/public/test/mock_render_process_host.h" | 41 #include "content/public/test/mock_render_process_host.h" |
| 41 #include "content/public/test/test_browser_thread.h" | 42 #include "content/public/test/test_browser_thread.h" |
| 42 #include "googleurl/src/gurl.h" | 43 #include "googleurl/src/gurl.h" |
| 43 #include "grit/generated_resources.h" | 44 #include "grit/generated_resources.h" |
| 44 #include "ipc/ipc_test_sink.h" | 45 #include "ipc/ipc_test_sink.h" |
| 45 #include "testing/gmock/include/gmock/gmock.h" | 46 #include "testing/gmock/include/gmock/gmock.h" |
| 46 #include "testing/gtest/include/gtest/gtest.h" | 47 #include "testing/gtest/include/gtest/gtest.h" |
| 47 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 48 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| (...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 | 574 |
| 574 std::string submitted_form_signature_; | 575 std::string submitted_form_signature_; |
| 575 std::vector<FieldTypeSet> expected_submitted_field_types_; | 576 std::vector<FieldTypeSet> expected_submitted_field_types_; |
| 576 std::vector<bool> sent_states_; | 577 std::vector<bool> sent_states_; |
| 577 | 578 |
| 578 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); | 579 DISALLOW_COPY_AND_ASSIGN(TestAutofillManager); |
| 579 }; | 580 }; |
| 580 | 581 |
| 581 } // namespace | 582 } // namespace |
| 582 | 583 |
| 583 class AutofillManagerTest : public TabContentsTestHarness { | 584 class AutofillManagerTest : public ChromeRenderViewHostTestHarness { |
| 584 public: | 585 public: |
| 585 typedef AutofillManager::GUIDPair GUIDPair; | 586 typedef AutofillManager::GUIDPair GUIDPair; |
| 586 | 587 |
| 587 AutofillManagerTest() | 588 AutofillManagerTest() |
| 588 : TabContentsTestHarness(), | 589 : ChromeRenderViewHostTestHarness(), |
| 589 ui_thread_(BrowserThread::UI, &message_loop_), | 590 ui_thread_(BrowserThread::UI, &message_loop_), |
| 590 file_thread_(BrowserThread::FILE) { | 591 file_thread_(BrowserThread::FILE) { |
| 591 } | 592 } |
| 592 | 593 |
| 593 virtual ~AutofillManagerTest() { | 594 virtual ~AutofillManagerTest() { |
| 594 } | 595 } |
| 595 | 596 |
| 596 virtual void SetUp() OVERRIDE { | 597 virtual void SetUp() OVERRIDE { |
| 597 Profile* profile = new TestingProfile(); | 598 Profile* profile = new TestingProfile(); |
| 598 browser_context_.reset(profile); | 599 browser_context_.reset(profile); |
| 599 PersonalDataManagerFactory::GetInstance()->SetTestingFactory( | 600 PersonalDataManagerFactory::GetInstance()->SetTestingFactory( |
| 600 profile, TestPersonalDataManager::Build); | 601 profile, TestPersonalDataManager::Build); |
| 601 | 602 |
| 602 TabContentsTestHarness::SetUp(); | 603 ChromeRenderViewHostTestHarness::SetUp(); |
| 603 TabAutofillManagerDelegate::CreateForWebContents(web_contents()); | 604 TabAutofillManagerDelegate::CreateForWebContents(web_contents()); |
| 604 autofill_manager_ = new TestAutofillManager( | 605 autofill_manager_ = new TestAutofillManager( |
| 605 web_contents(), | 606 web_contents(), |
| 606 TabAutofillManagerDelegate::FromWebContents(web_contents()), | 607 TabAutofillManagerDelegate::FromWebContents(web_contents()), |
| 607 &personal_data_); | 608 &personal_data_); |
| 608 | 609 |
| 609 file_thread_.Start(); | 610 file_thread_.Start(); |
| 610 } | 611 } |
| 611 | 612 |
| 612 virtual void TearDown() OVERRIDE { | 613 virtual void TearDown() OVERRIDE { |
| 613 // Order of destruction is important as AutofillManager relies on | 614 // Order of destruction is important as AutofillManager relies on |
| 614 // PersonalDataManager to be around when it gets destroyed. Also, a real | 615 // PersonalDataManager to be around when it gets destroyed. Also, a real |
| 615 // AutofillManager is tied to the lifetime of the WebContents, so it must | 616 // AutofillManager is tied to the lifetime of the WebContents, so it must |
| 616 // be destroyed at the destruction of the WebContents. | 617 // be destroyed at the destruction of the WebContents. |
| 617 autofill_manager_ = NULL; | 618 autofill_manager_ = NULL; |
| 618 file_thread_.Stop(); | 619 file_thread_.Stop(); |
| 619 TabContentsTestHarness::TearDown(); | 620 ChromeRenderViewHostTestHarness::TearDown(); |
| 620 } | 621 } |
| 621 | 622 |
| 622 void UpdatePasswordGenerationState(bool new_renderer) { | 623 void UpdatePasswordGenerationState(bool new_renderer) { |
| 623 autofill_manager_->UpdatePasswordGenerationState(NULL, new_renderer); | 624 autofill_manager_->UpdatePasswordGenerationState(NULL, new_renderer); |
| 624 } | 625 } |
| 625 | 626 |
| 626 void GetAutofillSuggestions(int query_id, | 627 void GetAutofillSuggestions(int query_id, |
| 627 const FormData& form, | 628 const FormData& form, |
| 628 const FormFieldData& field) { | 629 const FormFieldData& field) { |
| 629 autofill_manager_->OnQueryFormFieldAutofill(query_id, | 630 autofill_manager_->OnQueryFormFieldAutofill(query_id, |
| (...skipping 2281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2911 types.clear(); | 2912 types.clear(); |
| 2912 types.insert(UNKNOWN_TYPE); | 2913 types.insert(UNKNOWN_TYPE); |
| 2913 form.fields.push_back(field); | 2914 form.fields.push_back(field); |
| 2914 expected_types.push_back(types); | 2915 expected_types.push_back(types); |
| 2915 | 2916 |
| 2916 autofill_manager_->set_expected_submitted_field_types(expected_types); | 2917 autofill_manager_->set_expected_submitted_field_types(expected_types); |
| 2917 FormSubmitted(form); | 2918 FormSubmitted(form); |
| 2918 } | 2919 } |
| 2919 | 2920 |
| 2920 TEST_F(AutofillManagerTest, UpdatePasswordSyncState) { | 2921 TEST_F(AutofillManagerTest, UpdatePasswordSyncState) { |
| 2922 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); |
| 2923 PasswordManager::CreateForWebContentsAndDelegate( |
| 2924 web_contents(), |
| 2925 PasswordManagerDelegateImpl::FromWebContents(web_contents())); |
| 2926 |
| 2921 PrefServiceBase* prefs = PrefServiceBase::FromBrowserContext(profile()); | 2927 PrefServiceBase* prefs = PrefServiceBase::FromBrowserContext(profile()); |
| 2922 | 2928 |
| 2923 // Allow this test to control what should get synced. | 2929 // Allow this test to control what should get synced. |
| 2924 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); | 2930 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); |
| 2925 // Always set password generation enabled check box so we can test the | 2931 // Always set password generation enabled check box so we can test the |
| 2926 // behavior of password sync. | 2932 // behavior of password sync. |
| 2927 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); | 2933 prefs->SetBoolean(prefs::kPasswordGenerationEnabled, true); |
| 2928 | 2934 |
| 2929 // Sync some things, but not passwords. Shouldn't send anything since | 2935 // Sync some things, but not passwords. Shouldn't send anything since |
| 2930 // password generation is disabled by default. | 2936 // password generation is disabled by default. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2969 | 2975 |
| 2970 // When a new render_view is created, we send the state even if it's the | 2976 // When a new render_view is created, we send the state even if it's the |
| 2971 // same. | 2977 // same. |
| 2972 UpdatePasswordGenerationState(true); | 2978 UpdatePasswordGenerationState(true); |
| 2973 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size()); | 2979 EXPECT_EQ(1u, autofill_manager_->GetSentStates().size()); |
| 2974 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]); | 2980 EXPECT_FALSE(autofill_manager_->GetSentStates()[0]); |
| 2975 autofill_manager_->ClearSentStates(); | 2981 autofill_manager_->ClearSentStates(); |
| 2976 } | 2982 } |
| 2977 | 2983 |
| 2978 TEST_F(AutofillManagerTest, UpdatePasswordGenerationState) { | 2984 TEST_F(AutofillManagerTest, UpdatePasswordGenerationState) { |
| 2985 PasswordManagerDelegateImpl::CreateForWebContents(web_contents()); |
| 2986 PasswordManager::CreateForWebContentsAndDelegate( |
| 2987 web_contents(), |
| 2988 PasswordManagerDelegateImpl::FromWebContents(web_contents())); |
| 2989 |
| 2979 PrefServiceBase* prefs = PrefServiceBase::FromBrowserContext(profile()); | 2990 PrefServiceBase* prefs = PrefServiceBase::FromBrowserContext(profile()); |
| 2980 | 2991 |
| 2981 // Always set password sync enabled so we can test the behavior of password | 2992 // Always set password sync enabled so we can test the behavior of password |
| 2982 // generation. | 2993 // generation. |
| 2983 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); | 2994 prefs->SetBoolean(prefs::kSyncKeepEverythingSynced, false); |
| 2984 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile( | 2995 ProfileSyncService* sync_service = ProfileSyncServiceFactory::GetForProfile( |
| 2985 profile()); | 2996 profile()); |
| 2986 sync_service->SetSyncSetupCompleted(); | 2997 sync_service->SetSyncSetupCompleted(); |
| 2987 syncer::ModelTypeSet preferred_set; | 2998 syncer::ModelTypeSet preferred_set; |
| 2988 preferred_set.Put(syncer::PASSWORDS); | 2999 preferred_set.Put(syncer::PASSWORDS); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3106 | 3117 |
| 3107 FormData form; | 3118 FormData form; |
| 3108 CreateTestAddressFormData(&form); | 3119 CreateTestAddressFormData(&form); |
| 3109 std::vector<FormData> forms(1, form); | 3120 std::vector<FormData> forms(1, form); |
| 3110 FormsSeen(forms); | 3121 FormsSeen(forms); |
| 3111 const FormFieldData& field = form.fields[0]; | 3122 const FormFieldData& field = form.fields[0]; |
| 3112 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() | 3123 GetAutofillSuggestions(form, field); // should call the delegate's OnQuery() |
| 3113 | 3124 |
| 3114 autofill_manager_->SetExternalDelegate(NULL); | 3125 autofill_manager_->SetExternalDelegate(NULL); |
| 3115 } | 3126 } |
| 3116 | |
| 3117 #if defined(OS_ANDROID) || defined(TOOLKIT_GTK) | |
| 3118 // OS_ANDROID defines an external delegate, but prerequisites for | |
| 3119 // landing autofill_external_delegate_android.cc in the Chromium tree | |
| 3120 // have not themselves landed. | |
| 3121 | |
| 3122 // Turn on the external delegate. Recreate a WebContents. Make sure | |
| 3123 // an external delegate was set in the proper structures. | |
| 3124 TEST_F(AutofillManagerTest, TestTabContentsWithExternalDelegate) { | |
| 3125 CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 3126 switches::kExternalAutofillPopup); | |
| 3127 | |
| 3128 // Setting the contents creates a new TabContents. | |
| 3129 WebContents* contents = CreateTestWebContents(); | |
| 3130 SetContents(contents); | |
| 3131 | |
| 3132 AutofillManager* autofill_manager = | |
| 3133 AutofillManager::FromWebContents(contents); | |
| 3134 EXPECT_TRUE(autofill_manager->external_delegate()); | |
| 3135 | |
| 3136 AutocompleteHistoryManager* autocomplete_history_manager = | |
| 3137 &autofill_manager->autocomplete_history_manager_; | |
| 3138 EXPECT_TRUE(autocomplete_history_manager->external_delegate()); | |
| 3139 } | |
| 3140 | |
| 3141 #endif // OS_ANDROID | |
| OLD | NEW |