| 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 "base/location.h" | 5 #include "base/location.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/autofill/autofill_profile.h" | 8 #include "chrome/browser/autofill/autofill_profile.h" |
| 9 #include "chrome/browser/webdata/autofill_change.h" | 9 #include "chrome/browser/webdata/autofill_change.h" |
| 10 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" | 10 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 ACTION_P(CopyData, data) { | 44 ACTION_P(CopyData, data) { |
| 45 arg0->resize(data->size()); | 45 arg0->resize(data->size()); |
| 46 std::copy(data->begin(), data->end(), arg0->begin()); | 46 std::copy(data->begin(), data->end(), arg0->begin()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { | 49 MATCHER_P(CheckSyncChanges, n_sync_changes_list, "") { |
| 50 if (arg.size() != n_sync_changes_list.size()) | 50 if (arg.size() != n_sync_changes_list.size()) |
| 51 return false; | 51 return false; |
| 52 SyncChangeList::const_iterator passed, expected; | 52 csync::SyncChangeList::const_iterator passed, expected; |
| 53 for (passed = arg.begin(), expected = n_sync_changes_list.begin(); | 53 for (passed = arg.begin(), expected = n_sync_changes_list.begin(); |
| 54 passed != arg.end() && expected != n_sync_changes_list.end(); | 54 passed != arg.end() && expected != n_sync_changes_list.end(); |
| 55 ++passed, ++expected) { | 55 ++passed, ++expected) { |
| 56 DCHECK(passed->IsValid()); | 56 DCHECK(passed->IsValid()); |
| 57 if (passed->change_type() != expected->change_type()) | 57 if (passed->change_type() != expected->change_type()) |
| 58 return false; | 58 return false; |
| 59 if (passed->sync_data().GetSpecifics().autofill_profile().guid() != | 59 if (passed->sync_data().GetSpecifics().autofill_profile().guid() != |
| 60 expected->sync_data().GetSpecifics().autofill_profile().guid()) { | 60 expected->sync_data().GetSpecifics().autofill_profile().guid()) { |
| 61 return false; | 61 return false; |
| 62 } | 62 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 80 return false; | 80 return false; |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 for (size_t i = 0; i < arg.profiles_to_add.size(); ++i) { | 83 for (size_t i = 0; i < arg.profiles_to_add.size(); ++i) { |
| 84 if (arg.profiles_to_add[i]->Compare(*n_bundle.profiles_to_add[i])) | 84 if (arg.profiles_to_add[i]->Compare(*n_bundle.profiles_to_add[i])) |
| 85 return false; | 85 return false; |
| 86 } | 86 } |
| 87 return true; | 87 return true; |
| 88 } | 88 } |
| 89 | 89 |
| 90 class MockSyncChangeProcessor : public SyncChangeProcessor { | 90 class MockSyncChangeProcessor : public csync::SyncChangeProcessor { |
| 91 public: | 91 public: |
| 92 MockSyncChangeProcessor() {} | 92 MockSyncChangeProcessor() {} |
| 93 virtual ~MockSyncChangeProcessor() {} | 93 virtual ~MockSyncChangeProcessor() {} |
| 94 | 94 |
| 95 MOCK_METHOD2(ProcessSyncChanges, | 95 MOCK_METHOD2(ProcessSyncChanges, |
| 96 SyncError(const tracked_objects::Location&, | 96 csync::SyncError(const tracked_objects::Location&, |
| 97 const SyncChangeList&)); | 97 const csync::SyncChangeList&)); |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 class AutofillProfileSyncableServiceTest : public testing::Test { | 100 class AutofillProfileSyncableServiceTest : public testing::Test { |
| 101 public: | 101 public: |
| 102 AutofillProfileSyncableServiceTest() | 102 AutofillProfileSyncableServiceTest() |
| 103 : ui_thread_(BrowserThread::UI, &message_loop_), | 103 : ui_thread_(BrowserThread::UI, &message_loop_), |
| 104 db_thread_(BrowserThread::DB, &message_loop_) {} | 104 db_thread_(BrowserThread::DB, &message_loop_) {} |
| 105 | 105 |
| 106 virtual void SetUp() OVERRIDE { | 106 virtual void SetUp() OVERRIDE { |
| 107 sync_processor_.reset(new MockSyncChangeProcessor); | 107 sync_processor_.reset(new MockSyncChangeProcessor); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 124 | 124 |
| 125 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | 125 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| 126 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | 126 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 127 profiles_from_web_db.back()->SetInfo(ADDRESS_HOME_LINE1, | 127 profiles_from_web_db.back()->SetInfo(ADDRESS_HOME_LINE1, |
| 128 UTF8ToUTF16("1 1st st")); | 128 UTF8ToUTF16("1 1st st")); |
| 129 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); | 129 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
| 130 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Tom")); | 130 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Tom")); |
| 131 profiles_from_web_db.back()->SetInfo(ADDRESS_HOME_LINE1, | 131 profiles_from_web_db.back()->SetInfo(ADDRESS_HOME_LINE1, |
| 132 UTF8ToUTF16("2 2nd st")); | 132 UTF8ToUTF16("2 2nd st")); |
| 133 | 133 |
| 134 SyncDataList data_list; | 134 csync::SyncDataList data_list; |
| 135 AutofillProfile profile1(guid_synced1); | 135 AutofillProfile profile1(guid_synced1); |
| 136 profile1.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 136 profile1.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 137 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); | 137 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); |
| 138 AutofillProfile profile2(guid_synced2); | 138 AutofillProfile profile2(guid_synced2); |
| 139 profile2.SetInfo(NAME_FIRST, UTF8ToUTF16("Harry")); | 139 profile2.SetInfo(NAME_FIRST, UTF8ToUTF16("Harry")); |
| 140 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); | 140 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); |
| 141 // This one will have the name updated. | 141 // This one will have the name updated. |
| 142 AutofillProfile profile3(guid_present2); | 142 AutofillProfile profile3(guid_present2); |
| 143 profile3.SetInfo(NAME_FIRST, UTF8ToUTF16("Tom Doe")); | 143 profile3.SetInfo(NAME_FIRST, UTF8ToUTF16("Tom Doe")); |
| 144 data_list.push_back(autofill_syncable_service_.CreateData(profile3)); | 144 data_list.push_back(autofill_syncable_service_.CreateData(profile3)); |
| 145 | 145 |
| 146 SyncChangeList expected_change_list; | 146 csync::SyncChangeList expected_change_list; |
| 147 expected_change_list.push_back( | 147 expected_change_list.push_back( |
| 148 SyncChange(SyncChange::ACTION_ADD, | 148 csync::SyncChange(csync::SyncChange::ACTION_ADD, |
| 149 AutofillProfileSyncableService::CreateData( | 149 AutofillProfileSyncableService::CreateData( |
| 150 (*profiles_from_web_db.front())))); | 150 (*profiles_from_web_db.front())))); |
| 151 | 151 |
| 152 AutofillProfileSyncableService::DataBundle expected_bundle; | 152 AutofillProfileSyncableService::DataBundle expected_bundle; |
| 153 expected_bundle.profiles_to_add.push_back(&profile1); | 153 expected_bundle.profiles_to_add.push_back(&profile1); |
| 154 expected_bundle.profiles_to_add.push_back(&profile2); | 154 expected_bundle.profiles_to_add.push_back(&profile2); |
| 155 expected_bundle.profiles_to_update.push_back(&profile3); | 155 expected_bundle.profiles_to_update.push_back(&profile3); |
| 156 | 156 |
| 157 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 157 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 158 .Times(1) | 158 .Times(1) |
| 159 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 159 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 160 EXPECT_CALL(autofill_syncable_service_, | 160 EXPECT_CALL(autofill_syncable_service_, |
| 161 SaveChangesToWebData(DataBundleCheck(expected_bundle))) | 161 SaveChangesToWebData(DataBundleCheck(expected_bundle))) |
| 162 .Times(1) | 162 .Times(1) |
| 163 .WillOnce(Return(true)); | 163 .WillOnce(Return(true)); |
| 164 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) | 164 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 165 .WillByDefault(Return(SyncError())); | 165 .WillByDefault(Return(csync::SyncError())); |
| 166 EXPECT_CALL(*sync_processor_, | 166 EXPECT_CALL(*sync_processor_, |
| 167 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) | 167 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) |
| 168 .Times(1) | 168 .Times(1) |
| 169 .WillOnce(Return(SyncError())); | 169 .WillOnce(Return(csync::SyncError())); |
| 170 | 170 |
| 171 // Takes ownership of sync_processor_. | 171 // Takes ownership of sync_processor_. |
| 172 autofill_syncable_service_.MergeDataAndStartSyncing( | 172 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 173 syncable::AUTOFILL_PROFILE, data_list, | 173 syncable::AUTOFILL_PROFILE, data_list, |
| 174 sync_processor_.PassAs<SyncChangeProcessor>(), | 174 sync_processor_.PassAs<csync::SyncChangeProcessor>(), |
| 175 scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock())); | 175 scoped_ptr<csync::SyncErrorFactory>(new csync::SyncErrorFactoryMock())); |
| 176 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); | 176 autofill_syncable_service_.StopSyncing(syncable::AUTOFILL_PROFILE); |
| 177 } | 177 } |
| 178 | 178 |
| 179 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { | 179 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { |
| 180 std::vector<AutofillProfile *> profiles_from_web_db; | 180 std::vector<AutofillProfile *> profiles_from_web_db; |
| 181 std::string guid_present1 = kGuid1; | 181 std::string guid_present1 = kGuid1; |
| 182 std::string guid_present2 = kGuid2; | 182 std::string guid_present2 = kGuid2; |
| 183 | 183 |
| 184 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); | 184 profiles_from_web_db.push_back(new AutofillProfile(guid_present1)); |
| 185 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); | 185 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("John")); |
| 186 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); | 186 profiles_from_web_db.push_back(new AutofillProfile(guid_present2)); |
| 187 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 187 profiles_from_web_db.back()->SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 188 | 188 |
| 189 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 189 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 190 .Times(1) | 190 .Times(1) |
| 191 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 191 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 192 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) | 192 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) |
| 193 .Times(1) | 193 .Times(1) |
| 194 .WillOnce(Return(true)); | 194 .WillOnce(Return(true)); |
| 195 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) | 195 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 196 .WillByDefault(Return(SyncError())); | 196 .WillByDefault(Return(csync::SyncError())); |
| 197 EXPECT_CALL(*sync_processor_, | 197 EXPECT_CALL(*sync_processor_, |
| 198 ProcessSyncChanges(_, Property(&SyncChangeList::size, Eq(2U)))) | 198 ProcessSyncChanges( |
| 199 _, |
| 200 Property(&csync::SyncChangeList::size, Eq(2U)))) |
| 199 .Times(1) | 201 .Times(1) |
| 200 .WillOnce(Return(SyncError())); | 202 .WillOnce(Return(csync::SyncError())); |
| 201 | 203 |
| 202 SyncDataList data_list; | 204 csync::SyncDataList data_list; |
| 203 // Takes ownership of sync_processor_. | 205 // Takes ownership of sync_processor_. |
| 204 autofill_syncable_service_.MergeDataAndStartSyncing( | 206 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 205 syncable::AUTOFILL_PROFILE, data_list, | 207 syncable::AUTOFILL_PROFILE, data_list, |
| 206 sync_processor_.PassAs<SyncChangeProcessor>(), | 208 sync_processor_.PassAs<csync::SyncChangeProcessor>(), |
| 207 scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock())); | 209 scoped_ptr<csync::SyncErrorFactory>(new csync::SyncErrorFactoryMock())); |
| 208 | 210 |
| 209 SyncDataList data = | 211 csync::SyncDataList data = |
| 210 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); | 212 autofill_syncable_service_.GetAllSyncData(syncable::AUTOFILL_PROFILE); |
| 211 | 213 |
| 212 EXPECT_EQ(2U, data.size()); | 214 EXPECT_EQ(2U, data.size()); |
| 213 EXPECT_EQ(guid_present1, data.front().GetSpecifics() | 215 EXPECT_EQ(guid_present1, data.front().GetSpecifics() |
| 214 .autofill_profile().guid()); | 216 .autofill_profile().guid()); |
| 215 EXPECT_EQ(guid_present2, data.back().GetSpecifics() | 217 EXPECT_EQ(guid_present2, data.back().GetSpecifics() |
| 216 .autofill_profile().guid()); | 218 .autofill_profile().guid()); |
| 217 } | 219 } |
| 218 | 220 |
| 219 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { | 221 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { |
| 220 std::vector<AutofillProfile *> profiles_from_web_db; | 222 std::vector<AutofillProfile *> profiles_from_web_db; |
| 221 std::string guid_present = kGuid1; | 223 std::string guid_present = kGuid1; |
| 222 std::string guid_synced = kGuid2; | 224 std::string guid_synced = kGuid2; |
| 223 | 225 |
| 224 SyncChangeList change_list; | 226 csync::SyncChangeList change_list; |
| 225 AutofillProfile profile(guid_synced); | 227 AutofillProfile profile(guid_synced); |
| 226 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 228 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 227 change_list.push_back( | 229 change_list.push_back( |
| 228 SyncChange(SyncChange::ACTION_ADD, | 230 csync::SyncChange(csync::SyncChange::ACTION_ADD, |
| 229 AutofillProfileSyncableService::CreateData(profile))); | 231 AutofillProfileSyncableService::CreateData(profile))); |
| 230 AutofillProfile empty_profile(guid_present); | 232 AutofillProfile empty_profile(guid_present); |
| 231 change_list.push_back( | 233 change_list.push_back( |
| 232 SyncChange(SyncChange::ACTION_DELETE, | 234 csync::SyncChange(csync::SyncChange::ACTION_DELETE, |
| 233 AutofillProfileSyncableService::CreateData(empty_profile))); | 235 AutofillProfileSyncableService::CreateData(empty_profile))); |
| 234 | 236 |
| 235 AutofillProfileSyncableService::DataBundle expected_bundle; | 237 AutofillProfileSyncableService::DataBundle expected_bundle; |
| 236 expected_bundle.profiles_to_delete.push_back(guid_present); | 238 expected_bundle.profiles_to_delete.push_back(guid_present); |
| 237 expected_bundle.profiles_to_add.push_back(&profile); | 239 expected_bundle.profiles_to_add.push_back(&profile); |
| 238 | 240 |
| 239 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( | 241 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( |
| 240 DataBundleCheck(expected_bundle))) | 242 DataBundleCheck(expected_bundle))) |
| 241 .Times(1) | 243 .Times(1) |
| 242 .WillOnce(Return(true)); | 244 .WillOnce(Return(true)); |
| 243 | 245 |
| 244 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); | 246 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); |
| 245 SyncError error = autofill_syncable_service_.ProcessSyncChanges( | 247 csync::SyncError error = autofill_syncable_service_.ProcessSyncChanges( |
| 246 FROM_HERE, change_list); | 248 FROM_HERE, change_list); |
| 247 | 249 |
| 248 EXPECT_FALSE(error.IsSet()); | 250 EXPECT_FALSE(error.IsSet()); |
| 249 } | 251 } |
| 250 | 252 |
| 251 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { | 253 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { |
| 252 AutofillProfile profile(kGuid1); | 254 AutofillProfile profile(kGuid1); |
| 253 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); | 255 profile.SetInfo(NAME_FIRST, UTF8ToUTF16("Jane")); |
| 254 AutofillProfileChange change1(AutofillProfileChange::ADD, kGuid1, &profile); | 256 AutofillProfileChange change1(AutofillProfileChange::ADD, kGuid1, &profile); |
| 255 AutofillProfileChange change2(AutofillProfileChange::REMOVE, kGuid2, NULL); | 257 AutofillProfileChange change2(AutofillProfileChange::REMOVE, kGuid2, NULL); |
| 256 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) | 258 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 257 .WillByDefault(Return(SyncError(FROM_HERE, std::string("an error"), | 259 .WillByDefault(Return(csync::SyncError(FROM_HERE, std::string("an error"), |
| 258 syncable::AUTOFILL_PROFILE))); | 260 syncable::AUTOFILL_PROFILE))); |
| 259 EXPECT_CALL(*sync_processor_, ProcessSyncChanges(_, _)).Times(2); | 261 EXPECT_CALL(*sync_processor_, ProcessSyncChanges(_, _)).Times(2); |
| 260 | 262 |
| 261 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); | 263 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); |
| 262 autofill_syncable_service_.ActOnChange(change1); | 264 autofill_syncable_service_.ActOnChange(change1); |
| 263 autofill_syncable_service_.ActOnChange(change2); | 265 autofill_syncable_service_.ActOnChange(change2); |
| 264 } | 266 } |
| 265 | 267 |
| 266 TEST_F(AutofillProfileSyncableServiceTest, UpdateField) { | 268 TEST_F(AutofillProfileSyncableServiceTest, UpdateField) { |
| 267 AutofillProfile profile(kGuid1); | 269 AutofillProfile profile(kGuid1); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 profile1.GetMultiInfo(EMAIL_ADDRESS, &values); | 405 profile1.GetMultiInfo(EMAIL_ADDRESS, &values); |
| 404 ASSERT_EQ(values.size(), 3U); | 406 ASSERT_EQ(values.size(), 3U); |
| 405 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); | 407 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); |
| 406 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); | 408 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); |
| 407 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); | 409 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); |
| 408 | 410 |
| 409 profile1.GetMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values); | 411 profile1.GetMultiInfo(PHONE_HOME_WHOLE_NUMBER, &values); |
| 410 ASSERT_EQ(values.size(), 1U); | 412 ASSERT_EQ(values.size(), 1U); |
| 411 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); | 413 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); |
| 412 } | 414 } |
| OLD | NEW |