| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
| 12 #include "base/guid.h" | 12 #include "base/guid.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
| 18 #include "components/autofill/core/browser/autofill_profile.h" | 18 #include "components/autofill/core/browser/autofill_profile.h" |
| 19 #include "components/autofill/core/browser/autofill_test_utils.h" | 19 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 20 #include "components/autofill/core/browser/form_structure.h" | 20 #include "components/autofill/core/browser/form_structure.h" |
| 21 #include "components/autofill/core/browser/personal_data_manager.h" | 21 #include "components/autofill/core/browser/personal_data_manager.h" |
| 22 #include "components/autofill/core/browser/personal_data_manager_observer.h" | 22 #include "components/autofill/core/browser/personal_data_manager_observer.h" |
| 23 #include "components/autofill/core/browser/webdata/autofill_table.h" | 23 #include "components/autofill/core/browser/webdata/autofill_table.h" |
| 24 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" | 24 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
| 25 #include "components/autofill/core/common/autofill_pref_names.h" | 25 #include "components/autofill/core/common/autofill_pref_names.h" |
| 26 #include "components/autofill/core/common/autofill_switches.h" | 26 #include "components/autofill/core/common/autofill_switches.h" |
| 27 #include "components/autofill/core/common/form_data.h" | 27 #include "components/autofill/core/common/form_data.h" |
| 28 #include "components/signin/core/browser/account_tracker_service.h" |
| 29 #include "components/signin/core/browser/test_signin_client.h" |
| 28 #include "components/signin/core/common/signin_pref_names.h" | 30 #include "components/signin/core/common/signin_pref_names.h" |
| 29 #include "components/webdata/common/web_data_service_base.h" | 31 #include "components/webdata/common/web_data_service_base.h" |
| 30 #include "components/webdata/common/web_database_service.h" | 32 #include "components/webdata/common/web_database_service.h" |
| 33 #include "google_apis/gaia/fake_oauth2_token_service.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 34 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 33 | 36 |
| 34 using base::ASCIIToUTF16; | 37 using base::ASCIIToUTF16; |
| 35 using base::UTF8ToUTF16; | 38 using base::UTF8ToUTF16; |
| 36 | 39 |
| 37 namespace autofill { | 40 namespace autofill { |
| 38 namespace { | 41 namespace { |
| 39 | 42 |
| 40 enum UserMode { USER_MODE_NORMAL, USER_MODE_INCOGNITO }; | 43 enum UserMode { USER_MODE_NORMAL, USER_MODE_INCOGNITO }; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 protected: | 90 protected: |
| 88 PersonalDataManagerTest() : autofill_table_(nullptr) {} | 91 PersonalDataManagerTest() : autofill_table_(nullptr) {} |
| 89 | 92 |
| 90 void SetUp() override { | 93 void SetUp() override { |
| 91 prefs_ = test::PrefServiceForTesting(); | 94 prefs_ = test::PrefServiceForTesting(); |
| 92 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 95 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 93 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB"); | 96 base::FilePath path = temp_dir_.path().AppendASCII("TestWebDB"); |
| 94 web_database_ = new WebDatabaseService(path, | 97 web_database_ = new WebDatabaseService(path, |
| 95 base::MessageLoopProxy::current(), | 98 base::MessageLoopProxy::current(), |
| 96 base::MessageLoopProxy::current()); | 99 base::MessageLoopProxy::current()); |
| 100 |
| 101 // Setup account tracker. |
| 102 signin_client_.reset(new TestSigninClient(prefs_.get())); |
| 103 fake_oauth2_token_service_.reset(new FakeOAuth2TokenService()); |
| 104 account_tracker_.reset(new AccountTrackerService()); |
| 105 account_tracker_->Initialize(fake_oauth2_token_service_.get(), |
| 106 signin_client_.get()); |
| 107 |
| 97 // Hacky: hold onto a pointer but pass ownership. | 108 // Hacky: hold onto a pointer but pass ownership. |
| 98 autofill_table_ = new AutofillTable("en-US"); | 109 autofill_table_ = new AutofillTable("en-US"); |
| 99 web_database_->AddTable(scoped_ptr<WebDatabaseTable>(autofill_table_)); | 110 web_database_->AddTable(scoped_ptr<WebDatabaseTable>(autofill_table_)); |
| 100 web_database_->LoadDatabase(); | 111 web_database_->LoadDatabase(); |
| 101 autofill_database_service_ = | 112 autofill_database_service_ = |
| 102 new AutofillWebDataService(web_database_, | 113 new AutofillWebDataService(web_database_, |
| 103 base::MessageLoopProxy::current(), | 114 base::MessageLoopProxy::current(), |
| 104 base::MessageLoopProxy::current(), | 115 base::MessageLoopProxy::current(), |
| 105 WebDataServiceBase::ProfileErrorCallback()); | 116 WebDataServiceBase::ProfileErrorCallback()); |
| 106 autofill_database_service_->Init(); | 117 autofill_database_service_->Init(); |
| 107 | 118 |
| 108 test::DisableSystemServices(prefs_.get()); | 119 test::DisableSystemServices(prefs_.get()); |
| 109 ResetPersonalDataManager(USER_MODE_NORMAL); | 120 ResetPersonalDataManager(USER_MODE_NORMAL); |
| 110 } | 121 } |
| 111 | 122 |
| 123 void TearDown() override { |
| 124 // Order of destruction is important as AutofillManager relies on |
| 125 // PersonalDataManager to be around when it gets destroyed. |
| 126 account_tracker_->Shutdown(); |
| 127 fake_oauth2_token_service_.reset(); |
| 128 account_tracker_.reset(); |
| 129 signin_client_.reset(); |
| 130 } |
| 131 |
| 112 void ResetPersonalDataManager(UserMode user_mode) { | 132 void ResetPersonalDataManager(UserMode user_mode) { |
| 113 bool is_incognito = (user_mode == USER_MODE_INCOGNITO); | 133 bool is_incognito = (user_mode == USER_MODE_INCOGNITO); |
| 114 personal_data_.reset(new PersonalDataManager("en")); | 134 personal_data_.reset(new PersonalDataManager("en")); |
| 115 personal_data_->Init( | 135 personal_data_->Init( |
| 116 scoped_refptr<AutofillWebDataService>(autofill_database_service_), | 136 scoped_refptr<AutofillWebDataService>(autofill_database_service_), |
| 117 prefs_.get(), | 137 prefs_.get(), |
| 138 account_tracker_.get(), |
| 118 is_incognito); | 139 is_incognito); |
| 119 personal_data_->AddObserver(&personal_data_observer_); | 140 personal_data_->AddObserver(&personal_data_observer_); |
| 120 | 141 |
| 121 // Verify that the web database has been updated and the notification sent. | 142 // Verify that the web database has been updated and the notification sent. |
| 122 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) | 143 EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()) |
| 123 .WillOnce(QuitMainMessageLoop()); | 144 .WillOnce(QuitMainMessageLoop()); |
| 124 base::MessageLoop::current()->Run(); | 145 base::MessageLoop::current()->Run(); |
| 125 } | 146 } |
| 126 | 147 |
| 127 void EnableWalletCardImport() { | 148 void EnableWalletCardImport() { |
| 128 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true); | 149 prefs_->SetBoolean(prefs::kAutofillWalletSyncExperimentEnabled, true); |
| 129 prefs_->SetString(::prefs::kGoogleServicesUsername, "syncuser@example.com"); | 150 std::string account_id = |
| 151 account_tracker_->SeedAccountInfo("12345", "syncuser@example.com"); |
| 152 prefs_->SetString(::prefs::kGoogleServicesAccountId, account_id); |
| 130 } | 153 } |
| 131 | 154 |
| 132 // The temporary directory should be deleted at the end to ensure that | 155 // The temporary directory should be deleted at the end to ensure that |
| 133 // files are not used anymore and deletion succeeds. | 156 // files are not used anymore and deletion succeeds. |
| 134 base::ScopedTempDir temp_dir_; | 157 base::ScopedTempDir temp_dir_; |
| 135 base::MessageLoopForUI message_loop_; | 158 base::MessageLoopForUI message_loop_; |
| 136 scoped_ptr<PrefService> prefs_; | 159 scoped_ptr<PrefService> prefs_; |
| 160 scoped_ptr<FakeOAuth2TokenService> fake_oauth2_token_service_; |
| 161 scoped_ptr<AccountTrackerService> account_tracker_; |
| 162 scoped_ptr<TestSigninClient> signin_client_; |
| 137 scoped_refptr<AutofillWebDataService> autofill_database_service_; | 163 scoped_refptr<AutofillWebDataService> autofill_database_service_; |
| 138 scoped_refptr<WebDatabaseService> web_database_; | 164 scoped_refptr<WebDatabaseService> web_database_; |
| 139 AutofillTable* autofill_table_; // weak ref | 165 AutofillTable* autofill_table_; // weak ref |
| 140 PersonalDataLoadedObserverMock personal_data_observer_; | 166 PersonalDataLoadedObserverMock personal_data_observer_; |
| 141 scoped_ptr<PersonalDataManager> personal_data_; | 167 scoped_ptr<PersonalDataManager> personal_data_; |
| 142 }; | 168 }; |
| 143 | 169 |
| 144 TEST_F(PersonalDataManagerTest, AddProfile) { | 170 TEST_F(PersonalDataManagerTest, AddProfile) { |
| 145 // Add profile0 to the database. | 171 // Add profile0 to the database. |
| 146 AutofillProfile profile0(autofill::test::GetFullProfile()); | 172 AutofillProfile profile0(autofill::test::GetFullProfile()); |
| (...skipping 3074 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3221 | 3247 |
| 3222 personal_data_->ClearAllServerData(); | 3248 personal_data_->ClearAllServerData(); |
| 3223 | 3249 |
| 3224 // Reload the database, everything should be gone. | 3250 // Reload the database, everything should be gone. |
| 3225 ResetPersonalDataManager(USER_MODE_NORMAL); | 3251 ResetPersonalDataManager(USER_MODE_NORMAL); |
| 3226 EXPECT_TRUE(personal_data_->GetCreditCards().empty()); | 3252 EXPECT_TRUE(personal_data_->GetCreditCards().empty()); |
| 3227 EXPECT_TRUE(personal_data_->GetProfiles().empty()); | 3253 EXPECT_TRUE(personal_data_->GetProfiles().empty()); |
| 3228 } | 3254 } |
| 3229 | 3255 |
| 3230 } // namespace autofill | 3256 } // namespace autofill |
| OLD | NEW |