| 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 "base/location.h" | 5 #include "base/location.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/autofill/autofill_profile.h" | 7 #include "chrome/browser/autofill/autofill_profile.h" |
| 8 #include "chrome/browser/sync/internal_api/read_node_mock.h" | 8 #include "chrome/browser/sync/internal_api/read_node_mock.h" |
| 9 #include "chrome/browser/sync/internal_api/syncapi_mock.h" | 9 #include "chrome/browser/sync/internal_api/syncapi_mock.h" |
| 10 #include "chrome/browser/sync/syncable/syncable.h" | 10 #include "chrome/browser/sync/syncable/syncable.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 class MockAutofillProfileSyncableService | 25 class MockAutofillProfileSyncableService |
| 26 : public AutofillProfileSyncableService { | 26 : public AutofillProfileSyncableService { |
| 27 public: | 27 public: |
| 28 MockAutofillProfileSyncableService() { | 28 MockAutofillProfileSyncableService() { |
| 29 } | 29 } |
| 30 virtual ~MockAutofillProfileSyncableService() {} | 30 virtual ~MockAutofillProfileSyncableService() {} |
| 31 | 31 |
| 32 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); | 32 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); |
| 33 MOCK_METHOD1(SaveChangesToWebData, | 33 MOCK_METHOD1(SaveChangesToWebData, |
| 34 bool(const AutofillProfileSyncableService::DataBundle&)); | 34 bool(const AutofillProfileSyncableService::DataBundle&)); |
| 35 | |
| 36 // Helper for tests that do not need to setup service completely through the | |
| 37 // MergeDataAndStartSyncing(). | |
| 38 class AutoSetSyncProcessor { | |
| 39 public: | |
| 40 AutoSetSyncProcessor(MockAutofillProfileSyncableService* service, | |
| 41 SyncChangeProcessor* sync_processor) | |
| 42 : service_(service) { | |
| 43 service->set_sync_processor(sync_processor); | |
| 44 } | |
| 45 ~AutoSetSyncProcessor() { | |
| 46 service_->set_sync_processor(NULL); | |
| 47 } | |
| 48 MockAutofillProfileSyncableService* service_; | |
| 49 }; | |
| 50 }; | 35 }; |
| 51 | 36 |
| 52 ACTION_P(CopyData, data) { | 37 ACTION_P(CopyData, data) { |
| 53 arg0->resize(data->size()); | 38 arg0->resize(data->size()); |
| 54 std::copy(data->begin(), data->end(), arg0->begin()); | 39 std::copy(data->begin(), data->end(), arg0->begin()); |
| 55 } | 40 } |
| 56 | 41 |
| 57 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { | 42 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { |
| 58 if (arg.size() != n_sync_changes_list.size()) | 43 if (arg.size() != n_sync_changes_list.size()) |
| 59 return false; | 44 return false; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 MOCK_METHOD2(ProcessSyncChanges, | 90 MOCK_METHOD2(ProcessSyncChanges, |
| 106 SyncError(const tracked_objects::Location&, | 91 SyncError(const tracked_objects::Location&, |
| 107 const SyncChangeList&)); | 92 const SyncChangeList&)); |
| 108 }; | 93 }; |
| 109 | 94 |
| 110 class AutofillProfileSyncableServiceTest : public testing::Test { | 95 class AutofillProfileSyncableServiceTest : public testing::Test { |
| 111 public: | 96 public: |
| 112 AutofillProfileSyncableServiceTest() | 97 AutofillProfileSyncableServiceTest() |
| 113 : db_thread_(BrowserThread::DB, &message_loop_) {} | 98 : db_thread_(BrowserThread::DB, &message_loop_) {} |
| 114 | 99 |
| 100 virtual void SetUp() OVERRIDE { |
| 101 sync_processor_.reset(new MockSyncChangeProcessor); |
| 102 } |
| 103 |
| 104 virtual void TearDown() OVERRIDE { |
| 105 // Each test passes ownership of the sync processor to the SyncableService. |
| 106 // We don't release it immediately so we can verify the mock calls, so |
| 107 // release it at teardown. Any test that doesn't call |
| 108 // MergeDataAndStartSyncing or set_sync_processor must ensure the |
| 109 // sync_processor_ gets properly reset. |
| 110 ignore_result(sync_processor_.release()); |
| 111 } |
| 115 protected: | 112 protected: |
| 116 MessageLoop message_loop_; | 113 MessageLoop message_loop_; |
| 117 BrowserThread db_thread_; | 114 BrowserThread db_thread_; |
| 118 MockAutofillProfileSyncableService autofill_syncable_service_; | 115 MockAutofillProfileSyncableService autofill_syncable_service_; |
| 119 MockSyncChangeProcessor sync_processor_; | 116 scoped_ptr<MockSyncChangeProcessor> sync_processor_; |
| 120 }; | 117 }; |
| 121 | 118 |
| 122 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { | 119 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { |
| 123 std::vector<AutofillProfile *> profiles_from_web_db; | 120 std::vector<AutofillProfile *> profiles_from_web_db; |
| 124 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | 121 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 125 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 122 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 126 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; | 123 std::string guid_synced1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; |
| 127 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; | 124 std::string guid_synced2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; |
| 128 | 125 |
| 129 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | 126 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 154 expected_bundle.profiles_to_add.push_back(&profile2); | 151 expected_bundle.profiles_to_add.push_back(&profile2); |
| 155 expected_bundle.profiles_to_update.push_back(&profile3); | 152 expected_bundle.profiles_to_update.push_back(&profile3); |
| 156 | 153 |
| 157 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 154 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 158 .Times(1) | 155 .Times(1) |
| 159 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 156 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 160 EXPECT_CALL(autofill_syncable_service_, | 157 EXPECT_CALL(autofill_syncable_service_, |
| 161 SaveChangesToWebData(DataBundleCheck(expected_bundle))) | 158 SaveChangesToWebData(DataBundleCheck(expected_bundle))) |
| 162 .Times(1) | 159 .Times(1) |
| 163 .WillOnce(Return(true)); | 160 .WillOnce(Return(true)); |
| 164 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | 161 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 165 .WillByDefault(Return(SyncError())); | 162 .WillByDefault(Return(SyncError())); |
| 166 EXPECT_CALL(sync_processor_, | 163 EXPECT_CALL(*sync_processor_, |
| 167 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) | 164 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) |
| 168 .Times(1) | 165 .Times(1) |
| 169 .WillOnce(Return(SyncError())); | 166 .WillOnce(Return(SyncError())); |
| 170 | 167 |
| 168 // Takes ownership of sync_processor_. |
| 171 autofill_syncable_service_.MergeDataAndStartSyncing( | 169 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 172 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); | 170 syncable::AUTOFILL_PROFILE, data_list, sync_processor_.get()); |
| 173 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); | 171 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); |
| 174 } | 172 } |
| 175 | 173 |
| 176 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { | 174 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { |
| 177 std::vector<AutofillProfile *> profiles_from_web_db; | 175 std::vector<AutofillProfile *> profiles_from_web_db; |
| 178 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | 176 std::string guid_present1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 179 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 177 std::string guid_present2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 180 | 178 |
| 181 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | 179 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| 182 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | 180 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 183 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); | 181 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
| 184 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 182 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 185 | 183 |
| 186 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 184 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 187 .Times(1) | 185 .Times(1) |
| 188 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 186 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 189 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) | 187 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) |
| 190 .Times(1) | 188 .Times(1) |
| 191 .WillOnce(Return(true)); | 189 .WillOnce(Return(true)); |
| 192 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | 190 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 193 .WillByDefault(Return(SyncError())); | 191 .WillByDefault(Return(SyncError())); |
| 194 EXPECT_CALL(sync_processor_, | 192 EXPECT_CALL(*sync_processor_, |
| 195 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) | 193 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) |
| 196 .Times(1) | 194 .Times(1) |
| 197 .WillOnce(Return(SyncError())); | 195 .WillOnce(Return(SyncError())); |
| 198 | 196 |
| 199 SyncDataList data_list; | 197 SyncDataList data_list; |
| 198 // Takes ownership of sync_processor_. |
| 200 autofill_syncable_service_.MergeDataAndStartSyncing( | 199 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 201 syncable::AUTOFILL_PROFILE, data_list, &sync_processor_); | 200 syncable::AUTOFILL_PROFILE, data_list, sync_processor_.get()); |
| 202 | 201 |
| 203 SyncDataList data = | 202 SyncDataList data = |
| 204 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); | 203 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); |
| 205 | 204 |
| 206 EXPECT_EQ(2U, data.size()); | 205 EXPECT_EQ(2U, data.size()); |
| 207 EXPECT_EQ(guid_present1, data.front().GetSpecifics() | 206 EXPECT_EQ(guid_present1, data.front().GetSpecifics() |
| 208 .GetExtension(sync_pb::autofill_profile).guid()); | 207 .GetExtension(sync_pb::autofill_profile).guid()); |
| 209 EXPECT_EQ(guid_present2, data.back().GetSpecifics() | 208 EXPECT_EQ(guid_present2, data.back().GetSpecifics() |
| 210 .GetExtension(sync_pb::autofill_profile).guid()); | 209 .GetExtension(sync_pb::autofill_profile).guid()); |
| 211 } | 210 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 228 | 227 |
| 229 AutofillProfileSyncableService::DataBundle expected_bundle; | 228 AutofillProfileSyncableService::DataBundle expected_bundle; |
| 230 expected_bundle.profiles_to_delete.push_back(guid_present); | 229 expected_bundle.profiles_to_delete.push_back(guid_present); |
| 231 expected_bundle.profiles_to_add.push_back(&profile); | 230 expected_bundle.profiles_to_add.push_back(&profile); |
| 232 | 231 |
| 233 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( | 232 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( |
| 234 DataBundleCheck(expected_bundle))) | 233 DataBundleCheck(expected_bundle))) |
| 235 .Times(1) | 234 .Times(1) |
| 236 .WillOnce(Return(true)); | 235 .WillOnce(Return(true)); |
| 237 | 236 |
| 238 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | 237 autofill_syncable_service_.set_sync_processor(sync_processor_.get()); |
| 239 &autofill_syncable_service_, &sync_processor_); | |
| 240 SyncError error = autofill_syncable_service_.ProcessSyncChanges( | 238 SyncError error = autofill_syncable_service_.ProcessSyncChanges( |
| 241 FROM_HERE, change_list); | 239 FROM_HERE, change_list); |
| 242 | 240 |
| 243 EXPECT_FALSE(error.IsSet()); | 241 EXPECT_FALSE(error.IsSet()); |
| 244 } | 242 } |
| 245 | 243 |
| 246 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { | 244 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { |
| 247 std::string guid1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; | 245 std::string guid1 = "EDC609ED-7EEE-4F27-B00C-423242A9C44B"; |
| 248 std::string guid2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 246 std::string guid2 = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
| 249 | 247 |
| 250 AutofillProfile profile(guid1); | 248 AutofillProfile profile(guid1); |
| 251 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 249 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 252 AutofillProfileChange change1(AutofillProfileChange::ADD, guid1, &profile); | 250 AutofillProfileChange change1(AutofillProfileChange::ADD, guid1, &profile); |
| 253 AutofillProfileChange change2(AutofillProfileChange::REMOVE, guid2, NULL); | 251 AutofillProfileChange change2(AutofillProfileChange::REMOVE, guid2, NULL); |
| 254 ON_CALL(sync_processor_, ProcessSyncChanges(_, _)) | 252 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 255 .WillByDefault(Return(SyncError(FROM_HERE, std::string("an error"), | 253 .WillByDefault(Return(SyncError(FROM_HERE, std::string("an error"), |
| 256 syncable::AUTOFILL_PROFILE))); | 254 syncable::AUTOFILL_PROFILE))); |
| 257 | 255 |
| 258 MockAutofillProfileSyncableService::AutoSetSyncProcessor temp( | 256 autofill_syncable_service_.set_sync_processor(sync_processor_.get()); |
| 259 &autofill_syncable_service_, &sync_processor_); | |
| 260 autofill_syncable_service_.ActOnChange(change1); | 257 autofill_syncable_service_.ActOnChange(change1); |
| 261 autofill_syncable_service_.ActOnChange(change2); | 258 autofill_syncable_service_.ActOnChange(change2); |
| 262 } | 259 } |
| OLD | NEW |