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/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" | 8 #include "chrome/browser/webdata/autofill_profile_syncable_service.h" |
9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
10 #include "components/autofill/core/browser/webdata/autofill_change.h" | 10 #include "components/autofill/core/browser/webdata/autofill_change.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 const char kGuid2[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; | 29 const char kGuid2[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44C"; |
30 const char kGuid3[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; | 30 const char kGuid3[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44D"; |
31 const char kGuid4[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; | 31 const char kGuid4[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44E"; |
32 const char kHttpOrigin[] = "http://www.example.com/"; | 32 const char kHttpOrigin[] = "http://www.example.com/"; |
33 const char kHttpsOrigin[] = "https://www.example.com/"; | 33 const char kHttpsOrigin[] = "https://www.example.com/"; |
34 const char kSettingsOrigin[] = "Chrome settings"; | 34 const char kSettingsOrigin[] = "Chrome settings"; |
35 | 35 |
36 class MockAutofillProfileSyncableService | 36 class MockAutofillProfileSyncableService |
37 : public AutofillProfileSyncableService { | 37 : public AutofillProfileSyncableService { |
38 public: | 38 public: |
39 MockAutofillProfileSyncableService() { | 39 MockAutofillProfileSyncableService() {} |
40 } | |
41 virtual ~MockAutofillProfileSyncableService() {} | 40 virtual ~MockAutofillProfileSyncableService() {} |
42 | 41 |
| 42 using AutofillProfileSyncableService::DataBundle; |
| 43 using AutofillProfileSyncableService::set_sync_processor; |
| 44 using AutofillProfileSyncableService::CreateData; |
| 45 |
43 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); | 46 MOCK_METHOD1(LoadAutofillData, bool(std::vector<AutofillProfile*>*)); |
44 MOCK_METHOD1(SaveChangesToWebData, | 47 MOCK_METHOD1(SaveChangesToWebData, |
45 bool(const AutofillProfileSyncableService::DataBundle&)); | 48 bool(const AutofillProfileSyncableService::DataBundle&)); |
46 }; | 49 }; |
47 | 50 |
48 ACTION_P(CopyData, data) { | 51 ACTION_P(CopyData, data) { |
49 arg0->resize(data->size()); | 52 arg0->resize(data->size()); |
50 std::copy(data->begin(), data->end(), arg0->begin()); | 53 std::copy(data->begin(), data->end(), arg0->begin()); |
51 } | 54 } |
52 | 55 |
(...skipping 11 matching lines...) Expand all Loading... |
64 expected->sync_data().GetSpecifics().autofill_profile().guid()) { | 67 expected->sync_data().GetSpecifics().autofill_profile().guid()) { |
65 return false; | 68 return false; |
66 } | 69 } |
67 } | 70 } |
68 return true; | 71 return true; |
69 } | 72 } |
70 | 73 |
71 MATCHER_P(DataBundleCheck, n_bundle, "") { | 74 MATCHER_P(DataBundleCheck, n_bundle, "") { |
72 if ((arg.profiles_to_delete.size() != n_bundle.profiles_to_delete.size()) || | 75 if ((arg.profiles_to_delete.size() != n_bundle.profiles_to_delete.size()) || |
73 (arg.profiles_to_update.size() != n_bundle.profiles_to_update.size()) || | 76 (arg.profiles_to_update.size() != n_bundle.profiles_to_update.size()) || |
74 (arg.profiles_to_add.size() != n_bundle.profiles_to_add.size())) { | 77 (arg.profiles_to_add.size() != n_bundle.profiles_to_add.size())) |
75 return false; | 78 return false; |
76 } | |
77 for (size_t i = 0; i < arg.profiles_to_delete.size(); ++i) { | 79 for (size_t i = 0; i < arg.profiles_to_delete.size(); ++i) { |
78 if (arg.profiles_to_delete[i] != n_bundle.profiles_to_delete[i]) | 80 if (arg.profiles_to_delete[i] != n_bundle.profiles_to_delete[i]) |
79 return false; | 81 return false; |
80 } | 82 } |
81 for (size_t i = 0; i < arg.profiles_to_update.size(); ++i) { | 83 for (size_t i = 0; i < arg.profiles_to_update.size(); ++i) { |
82 if (arg.profiles_to_update[i]->guid() != | 84 if (*arg.profiles_to_update[i] != *n_bundle.profiles_to_update[i]) |
83 n_bundle.profiles_to_update[i]->guid()) { | |
84 return false; | 85 return false; |
85 } | |
86 } | 86 } |
87 for (size_t i = 0; i < arg.profiles_to_add.size(); ++i) { | 87 for (size_t i = 0; i < arg.profiles_to_add.size(); ++i) { |
88 if (arg.profiles_to_add[i]->Compare(*n_bundle.profiles_to_add[i])) | 88 if (*arg.profiles_to_add[i] != *n_bundle.profiles_to_add[i]) |
89 return false; | 89 return false; |
90 } | 90 } |
91 return true; | 91 return true; |
92 } | 92 } |
93 | 93 |
94 class MockSyncChangeProcessor : public syncer::SyncChangeProcessor { | 94 class MockSyncChangeProcessor : public syncer::SyncChangeProcessor { |
95 public: | 95 public: |
96 MockSyncChangeProcessor() {} | 96 MockSyncChangeProcessor() {} |
97 virtual ~MockSyncChangeProcessor() {} | 97 virtual ~MockSyncChangeProcessor() {} |
98 | 98 |
99 MOCK_METHOD2(ProcessSyncChanges, | 99 MOCK_METHOD2(ProcessSyncChanges, |
100 syncer::SyncError(const tracked_objects::Location&, | 100 syncer::SyncError(const tracked_objects::Location&, |
101 const syncer::SyncChangeList&)); | 101 const syncer::SyncChangeList&)); |
| 102 }; |
| 103 |
| 104 class TestSyncChangeProcessor : public syncer::SyncChangeProcessor { |
| 105 public: |
| 106 TestSyncChangeProcessor() {} |
| 107 virtual ~TestSyncChangeProcessor() {} |
| 108 |
| 109 virtual syncer::SyncError ProcessSyncChanges( |
| 110 const tracked_objects::Location& location, |
| 111 const syncer::SyncChangeList& changes) OVERRIDE { |
| 112 changes_ = changes; |
| 113 return syncer::SyncError(); |
| 114 } |
| 115 |
| 116 const syncer::SyncChangeList& changes() { return changes_; } |
| 117 |
| 118 private: |
| 119 syncer::SyncChangeList changes_; |
102 }; | 120 }; |
103 | 121 |
104 class AutofillProfileSyncableServiceTest : public testing::Test { | 122 class AutofillProfileSyncableServiceTest : public testing::Test { |
105 public: | 123 public: |
106 AutofillProfileSyncableServiceTest() | 124 AutofillProfileSyncableServiceTest() |
107 : ui_thread_(BrowserThread::UI, &message_loop_), | 125 : ui_thread_(BrowserThread::UI, &message_loop_), |
108 db_thread_(BrowserThread::DB, &message_loop_) {} | 126 db_thread_(BrowserThread::DB, &message_loop_) {} |
109 | 127 |
110 virtual void SetUp() OVERRIDE { | 128 virtual void SetUp() OVERRIDE { |
111 sync_processor_.reset(new MockSyncChangeProcessor); | 129 sync_processor_.reset(new MockSyncChangeProcessor); |
112 } | 130 } |
113 | 131 |
| 132 // Wrapper around AutofillProfileSyncableService::MergeDataAndStartSyncing() |
| 133 // that also verifies expectations. |
| 134 void MergeDataAndStartSyncing( |
| 135 const std::vector<AutofillProfile*>& profiles_from_web_db, |
| 136 const syncer::SyncDataList& data_list, |
| 137 const MockAutofillProfileSyncableService::DataBundle& expected_bundle, |
| 138 const syncer::SyncChangeList& expected_change_list) { |
| 139 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
| 140 .Times(1) |
| 141 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
| 142 EXPECT_CALL(autofill_syncable_service_, |
| 143 SaveChangesToWebData(DataBundleCheck(expected_bundle))) |
| 144 .Times(1) |
| 145 .WillOnce(Return(true)); |
| 146 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) |
| 147 .WillByDefault(Return(syncer::SyncError())); |
| 148 EXPECT_CALL(*sync_processor_, |
| 149 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) |
| 150 .Times(1) |
| 151 .WillOnce(Return(syncer::SyncError())); |
| 152 |
| 153 // Takes ownership of sync_processor_. |
| 154 autofill_syncable_service_.MergeDataAndStartSyncing( |
| 155 syncer::AUTOFILL_PROFILE, data_list, |
| 156 sync_processor_.PassAs<syncer::SyncChangeProcessor>(), |
| 157 scoped_ptr<syncer::SyncErrorFactory>( |
| 158 new syncer::SyncErrorFactoryMock())); |
| 159 } |
| 160 |
114 protected: | 161 protected: |
115 base::MessageLoop message_loop_; | 162 base::MessageLoop message_loop_; |
116 content::TestBrowserThread ui_thread_; | 163 content::TestBrowserThread ui_thread_; |
117 content::TestBrowserThread db_thread_; | 164 content::TestBrowserThread db_thread_; |
118 MockAutofillProfileSyncableService autofill_syncable_service_; | 165 MockAutofillProfileSyncableService autofill_syncable_service_; |
119 scoped_ptr<MockSyncChangeProcessor> sync_processor_; | 166 scoped_ptr<MockSyncChangeProcessor> sync_processor_; |
120 }; | 167 }; |
121 | 168 |
122 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { | 169 TEST_F(AutofillProfileSyncableServiceTest, MergeDataAndStartSyncing) { |
123 std::vector<AutofillProfile *> profiles_from_web_db; | 170 std::vector<AutofillProfile*> profiles_from_web_db; |
124 std::string guid_present1 = kGuid1; | 171 std::string guid_present1 = kGuid1; |
125 std::string guid_present2 = kGuid2; | 172 std::string guid_present2 = kGuid2; |
126 std::string guid_synced1 = kGuid3; | 173 std::string guid_synced1 = kGuid3; |
127 std::string guid_synced2 = kGuid4; | 174 std::string guid_synced2 = kGuid4; |
128 std::string origin_present1 = kHttpOrigin; | 175 std::string origin_present1 = kHttpOrigin; |
129 std::string origin_present2 = std::string(); | 176 std::string origin_present2 = std::string(); |
130 std::string origin_synced1 = kHttpsOrigin; | 177 std::string origin_synced1 = kHttpsOrigin; |
131 std::string origin_synced2 = kSettingsOrigin; | 178 std::string origin_synced2 = kSettingsOrigin; |
132 | 179 |
133 profiles_from_web_db.push_back( | 180 profiles_from_web_db.push_back( |
134 new AutofillProfile(guid_present1, origin_present1)); | 181 new AutofillProfile(guid_present1, origin_present1)); |
135 profiles_from_web_db.back()->SetRawInfo( | 182 profiles_from_web_db.back()->SetRawInfo( |
136 autofill::NAME_FIRST, UTF8ToUTF16("John")); | 183 autofill::NAME_FIRST, UTF8ToUTF16("John")); |
137 profiles_from_web_db.back()->SetRawInfo( | 184 profiles_from_web_db.back()->SetRawInfo( |
138 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); | 185 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); |
139 profiles_from_web_db.push_back( | 186 profiles_from_web_db.push_back( |
140 new AutofillProfile(guid_present2, origin_present2)); | 187 new AutofillProfile(guid_present2, origin_present2)); |
141 profiles_from_web_db.back()->SetRawInfo( | 188 profiles_from_web_db.back()->SetRawInfo( |
142 autofill::NAME_FIRST, UTF8ToUTF16("Tom")); | 189 autofill::NAME_FIRST, UTF8ToUTF16("Tom")); |
143 profiles_from_web_db.back()->SetRawInfo( | 190 profiles_from_web_db.back()->SetRawInfo( |
144 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("2 2nd st")); | 191 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("2 2nd st")); |
145 | 192 |
146 syncer::SyncDataList data_list; | 193 syncer::SyncDataList data_list; |
147 AutofillProfile profile1(guid_synced1, origin_synced1); | 194 AutofillProfile profile1(guid_synced1, origin_synced1); |
148 profile1.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); | 195 profile1.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); |
149 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); | 196 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); |
150 AutofillProfile profile2(guid_synced2, origin_synced2); | 197 AutofillProfile profile2(guid_synced2, origin_synced2); |
151 profile2.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Harry")); | 198 profile2.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Harry")); |
152 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); | 199 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); |
153 // This one will have the name updated. | 200 // This one will have the name and origin updated. |
154 AutofillProfile profile3(guid_present2, origin_synced2); | 201 AutofillProfile profile3(guid_present2, origin_synced2); |
155 profile3.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Tom Doe")); | 202 profile3.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Tom Doe")); |
156 data_list.push_back(autofill_syncable_service_.CreateData(profile3)); | 203 data_list.push_back(autofill_syncable_service_.CreateData(profile3)); |
157 | 204 |
158 syncer::SyncChangeList expected_change_list; | 205 syncer::SyncChangeList expected_change_list; |
159 expected_change_list.push_back( | 206 expected_change_list.push_back( |
160 syncer::SyncChange(FROM_HERE, | 207 syncer::SyncChange(FROM_HERE, |
161 syncer::SyncChange::ACTION_ADD, | 208 syncer::SyncChange::ACTION_ADD, |
162 AutofillProfileSyncableService::CreateData( | 209 MockAutofillProfileSyncableService::CreateData( |
163 (*profiles_from_web_db.front())))); | 210 *profiles_from_web_db.front()))); |
164 | 211 |
165 AutofillProfileSyncableService::DataBundle expected_bundle; | 212 MockAutofillProfileSyncableService::DataBundle expected_bundle; |
166 expected_bundle.profiles_to_add.push_back(&profile1); | 213 expected_bundle.profiles_to_add.push_back(&profile1); |
167 expected_bundle.profiles_to_add.push_back(&profile2); | 214 expected_bundle.profiles_to_add.push_back(&profile2); |
168 expected_bundle.profiles_to_update.push_back(&profile3); | 215 expected_bundle.profiles_to_update.push_back(&profile3); |
169 | 216 |
| 217 MergeDataAndStartSyncing( |
| 218 profiles_from_web_db, data_list, expected_bundle, expected_change_list); |
| 219 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); |
| 220 } |
| 221 |
| 222 TEST_F(AutofillProfileSyncableServiceTest, MergeIdenticalProfiles) { |
| 223 std::vector<AutofillProfile*> profiles_from_web_db; |
| 224 std::string guid_present1 = kGuid1; |
| 225 std::string guid_present2 = kGuid2; |
| 226 std::string guid_synced1 = kGuid3; |
| 227 std::string guid_synced2 = kGuid4; |
| 228 std::string origin_present1 = kHttpOrigin; |
| 229 std::string origin_present2 = kSettingsOrigin; |
| 230 std::string origin_synced1 = kHttpsOrigin; |
| 231 std::string origin_synced2 = kHttpsOrigin; |
| 232 |
| 233 profiles_from_web_db.push_back( |
| 234 new AutofillProfile(guid_present1, origin_present1)); |
| 235 profiles_from_web_db.back()->SetRawInfo( |
| 236 autofill::NAME_FIRST, UTF8ToUTF16("John")); |
| 237 profiles_from_web_db.back()->SetRawInfo( |
| 238 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); |
| 239 profiles_from_web_db.push_back( |
| 240 new AutofillProfile(guid_present2, origin_present2)); |
| 241 profiles_from_web_db.back()->SetRawInfo( |
| 242 autofill::NAME_FIRST, UTF8ToUTF16("Tom")); |
| 243 profiles_from_web_db.back()->SetRawInfo( |
| 244 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("2 2nd st")); |
| 245 |
| 246 // The synced profiles are identical to the local ones, except that the guids |
| 247 // are different. |
| 248 syncer::SyncDataList data_list; |
| 249 AutofillProfile profile1(guid_synced1, origin_synced1); |
| 250 profile1.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("John")); |
| 251 profile1.SetRawInfo(autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); |
| 252 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); |
| 253 AutofillProfile profile2(guid_synced2, origin_synced2); |
| 254 profile2.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Tom")); |
| 255 profile2.SetRawInfo(autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("2 2nd st")); |
| 256 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); |
| 257 |
| 258 AutofillProfile expected_profile(profile2); |
| 259 expected_profile.set_origin(kSettingsOrigin); |
| 260 syncer::SyncChangeList expected_change_list; |
| 261 expected_change_list.push_back( |
| 262 syncer::SyncChange(FROM_HERE, |
| 263 syncer::SyncChange::ACTION_UPDATE, |
| 264 MockAutofillProfileSyncableService::CreateData( |
| 265 expected_profile))); |
| 266 |
| 267 MockAutofillProfileSyncableService::DataBundle expected_bundle; |
| 268 expected_bundle.profiles_to_delete.push_back(guid_present1); |
| 269 expected_bundle.profiles_to_delete.push_back(guid_present2); |
| 270 expected_bundle.profiles_to_add.push_back(&profile1); |
| 271 expected_bundle.profiles_to_add.push_back(&expected_profile); |
| 272 |
| 273 MergeDataAndStartSyncing( |
| 274 profiles_from_web_db, data_list, expected_bundle, expected_change_list); |
| 275 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); |
| 276 } |
| 277 |
| 278 TEST_F(AutofillProfileSyncableServiceTest, MergeSimilarProfiles) { |
| 279 std::vector<AutofillProfile*> profiles_from_web_db; |
| 280 std::string guid_present1 = kGuid1; |
| 281 std::string guid_present2 = kGuid2; |
| 282 std::string guid_synced1 = kGuid3; |
| 283 std::string guid_synced2 = kGuid4; |
| 284 std::string origin_present1 = kHttpOrigin; |
| 285 std::string origin_present2 = kSettingsOrigin; |
| 286 std::string origin_synced1 = kHttpsOrigin; |
| 287 std::string origin_synced2 = kHttpsOrigin; |
| 288 |
| 289 profiles_from_web_db.push_back( |
| 290 new AutofillProfile(guid_present1, origin_present1)); |
| 291 profiles_from_web_db.back()->SetRawInfo( |
| 292 autofill::NAME_FIRST, UTF8ToUTF16("John")); |
| 293 profiles_from_web_db.back()->SetRawInfo( |
| 294 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); |
| 295 profiles_from_web_db.push_back( |
| 296 new AutofillProfile(guid_present2, origin_present2)); |
| 297 profiles_from_web_db.back()->SetRawInfo( |
| 298 autofill::NAME_FIRST, UTF8ToUTF16("Tom")); |
| 299 profiles_from_web_db.back()->SetRawInfo( |
| 300 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("2 2nd st")); |
| 301 |
| 302 // The synced profiles are identical to the local ones, except that the guids |
| 303 // are different. |
| 304 syncer::SyncDataList data_list; |
| 305 AutofillProfile profile1(guid_synced1, origin_synced1); |
| 306 profile1.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("John")); |
| 307 profile1.SetRawInfo(autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); |
| 308 profile1.SetRawInfo(autofill::COMPANY_NAME, UTF8ToUTF16("Frobbers, Inc.")); |
| 309 data_list.push_back(autofill_syncable_service_.CreateData(profile1)); |
| 310 AutofillProfile profile2(guid_synced2, origin_synced2); |
| 311 profile2.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Tom")); |
| 312 profile2.SetRawInfo(autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("2 2nd st")); |
| 313 profile2.SetRawInfo(autofill::COMPANY_NAME, UTF8ToUTF16("Fizzbang, LLC.")); |
| 314 data_list.push_back(autofill_syncable_service_.CreateData(profile2)); |
| 315 |
| 316 // The first profile should have its origin updated. |
| 317 // The second profile should remain as-is, because an unverified profile |
| 318 // should never overwrite a verified one. |
| 319 AutofillProfile expected_profile(profile1); |
| 320 expected_profile.set_origin(origin_present1); |
| 321 syncer::SyncChangeList expected_change_list; |
| 322 expected_change_list.push_back( |
| 323 syncer::SyncChange(FROM_HERE, |
| 324 syncer::SyncChange::ACTION_ADD, |
| 325 MockAutofillProfileSyncableService::CreateData( |
| 326 *profiles_from_web_db.back()))); |
| 327 expected_change_list.push_back( |
| 328 syncer::SyncChange(FROM_HERE, |
| 329 syncer::SyncChange::ACTION_UPDATE, |
| 330 MockAutofillProfileSyncableService::CreateData( |
| 331 expected_profile))); |
| 332 |
| 333 MockAutofillProfileSyncableService::DataBundle expected_bundle; |
| 334 expected_bundle.profiles_to_delete.push_back(guid_present1); |
| 335 expected_bundle.profiles_to_add.push_back(&expected_profile); |
| 336 expected_bundle.profiles_to_add.push_back(&profile2); |
| 337 |
| 338 MergeDataAndStartSyncing( |
| 339 profiles_from_web_db, data_list, expected_bundle, expected_change_list); |
| 340 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); |
| 341 } |
| 342 |
| 343 // Ensure that no Sync events are generated to fill in missing origins from Sync |
| 344 // with explicitly present empty ones. This ensures that the migration to add |
| 345 // origins to profiles does not generate lots of needless Sync updates. |
| 346 TEST_F(AutofillProfileSyncableServiceTest, MergeDataEmptyOrigins) { |
| 347 std::vector<AutofillProfile*> profiles_from_web_db; |
| 348 |
| 349 // Create a profile with an empty origin. |
| 350 AutofillProfile profile(kGuid1, std::string()); |
| 351 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("John")); |
| 352 profile.SetRawInfo(autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("1 1st st")); |
| 353 |
| 354 profiles_from_web_db.push_back(new AutofillProfile(profile)); |
| 355 |
| 356 // Create a Sync profile identical to |profile|, except with no origin set. |
| 357 sync_pb::EntitySpecifics specifics; |
| 358 sync_pb::AutofillProfileSpecifics* autofill_specifics = |
| 359 specifics.mutable_autofill_profile(); |
| 360 autofill_specifics->set_guid(profile.guid()); |
| 361 autofill_specifics->add_name_first("John"); |
| 362 autofill_specifics->add_name_middle(std::string()); |
| 363 autofill_specifics->add_name_last(std::string()); |
| 364 autofill_specifics->add_email_address(std::string()); |
| 365 autofill_specifics->add_phone_home_whole_number(std::string()); |
| 366 autofill_specifics->set_address_home_line1("1 1st st"); |
| 367 EXPECT_FALSE(autofill_specifics->has_origin()); |
| 368 |
| 369 syncer::SyncDataList data_list; |
| 370 data_list.push_back( |
| 371 syncer::SyncData::CreateLocalData( |
| 372 profile.guid(), profile.guid(), specifics)); |
| 373 |
| 374 MockAutofillProfileSyncableService::DataBundle expected_bundle; |
170 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 375 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) |
171 .Times(1) | 376 .Times(1) |
172 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 377 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); |
173 EXPECT_CALL(autofill_syncable_service_, | 378 EXPECT_CALL(autofill_syncable_service_, |
174 SaveChangesToWebData(DataBundleCheck(expected_bundle))) | 379 SaveChangesToWebData(DataBundleCheck(expected_bundle))) |
175 .Times(1) | 380 .Times(1) |
176 .WillOnce(Return(true)); | 381 .WillOnce(Return(true)); |
177 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) | 382 EXPECT_CALL(*sync_processor_, ProcessSyncChanges(_, _)).Times(0); |
178 .WillByDefault(Return(syncer::SyncError())); | |
179 EXPECT_CALL(*sync_processor_, | |
180 ProcessSyncChanges(_, CheckSyncChanges(expected_change_list))) | |
181 .Times(1) | |
182 .WillOnce(Return(syncer::SyncError())); | |
183 | 383 |
184 // Takes ownership of sync_processor_. | 384 // Takes ownership of sync_processor_. |
185 autofill_syncable_service_.MergeDataAndStartSyncing( | 385 autofill_syncable_service_.MergeDataAndStartSyncing( |
186 syncer::AUTOFILL_PROFILE, data_list, | 386 syncer::AUTOFILL_PROFILE, data_list, |
187 sync_processor_.PassAs<syncer::SyncChangeProcessor>(), | 387 sync_processor_.PassAs<syncer::SyncChangeProcessor>(), |
188 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | 388 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); |
| 389 |
189 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); | 390 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); |
190 } | 391 } |
191 | 392 |
192 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { | 393 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { |
193 std::vector<AutofillProfile *> profiles_from_web_db; | 394 std::vector<AutofillProfile*> profiles_from_web_db; |
194 std::string guid_present1 = kGuid1; | 395 std::string guid_present1 = kGuid1; |
195 std::string guid_present2 = kGuid2; | 396 std::string guid_present2 = kGuid2; |
196 | 397 |
197 profiles_from_web_db.push_back( | 398 profiles_from_web_db.push_back( |
198 new AutofillProfile(guid_present1, kHttpOrigin)); | 399 new AutofillProfile(guid_present1, kHttpOrigin)); |
199 profiles_from_web_db.back()->SetRawInfo( | 400 profiles_from_web_db.back()->SetRawInfo( |
200 autofill::NAME_FIRST, UTF8ToUTF16("John")); | 401 autofill::NAME_FIRST, UTF8ToUTF16("John")); |
201 profiles_from_web_db.push_back( | 402 profiles_from_web_db.push_back( |
202 new AutofillProfile(guid_present2, kHttpsOrigin)); | 403 new AutofillProfile(guid_present2, kHttpsOrigin)); |
203 profiles_from_web_db.back()->SetRawInfo( | 404 profiles_from_web_db.back()->SetRawInfo( |
204 autofill::NAME_FIRST, UTF8ToUTF16("Jane")); | 405 autofill::NAME_FIRST, UTF8ToUTF16("Jane")); |
205 | 406 |
206 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) | 407 syncer::SyncChangeList expected_change_list; |
207 .Times(1) | 408 expected_change_list.push_back( |
208 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); | 409 syncer::SyncChange(FROM_HERE, |
209 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) | 410 syncer::SyncChange::ACTION_ADD, |
210 .Times(1) | 411 MockAutofillProfileSyncableService::CreateData( |
211 .WillOnce(Return(true)); | 412 *profiles_from_web_db.front()))); |
212 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) | 413 expected_change_list.push_back( |
213 .WillByDefault(Return(syncer::SyncError())); | 414 syncer::SyncChange(FROM_HERE, |
214 EXPECT_CALL(*sync_processor_, | 415 syncer::SyncChange::ACTION_ADD, |
215 ProcessSyncChanges( | 416 MockAutofillProfileSyncableService::CreateData( |
216 _, | 417 *profiles_from_web_db.back()))); |
217 Property(&syncer::SyncChangeList::size, Eq(2U)))) | |
218 .Times(1) | |
219 .WillOnce(Return(syncer::SyncError())); | |
220 | 418 |
| 419 MockAutofillProfileSyncableService::DataBundle expected_bundle; |
221 syncer::SyncDataList data_list; | 420 syncer::SyncDataList data_list; |
222 // Takes ownership of sync_processor_. | 421 MergeDataAndStartSyncing( |
223 autofill_syncable_service_.MergeDataAndStartSyncing( | 422 profiles_from_web_db, data_list, expected_bundle, expected_change_list); |
224 syncer::AUTOFILL_PROFILE, data_list, | |
225 sync_processor_.PassAs<syncer::SyncChangeProcessor>(), | |
226 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); | |
227 | 423 |
228 syncer::SyncDataList data = | 424 syncer::SyncDataList data = |
229 autofill_syncable_service_.GetAllSyncData(syncer::AUTOFILL_PROFILE); | 425 autofill_syncable_service_.GetAllSyncData(syncer::AUTOFILL_PROFILE); |
230 | 426 |
231 ASSERT_EQ(2U, data.size()); | 427 ASSERT_EQ(2U, data.size()); |
232 EXPECT_EQ(guid_present1, data[0].GetSpecifics().autofill_profile().guid()); | 428 EXPECT_EQ(guid_present1, data[0].GetSpecifics().autofill_profile().guid()); |
233 EXPECT_EQ(guid_present2, data[1].GetSpecifics().autofill_profile().guid()); | 429 EXPECT_EQ(guid_present2, data[1].GetSpecifics().autofill_profile().guid()); |
234 // TODO(isherman): Verify that the origins match once they are saved and read | 430 EXPECT_EQ(kHttpOrigin, data[0].GetSpecifics().autofill_profile().origin()); |
235 // from the database and included in the Sync protocol buffers. | 431 EXPECT_EQ(kHttpsOrigin, data[1].GetSpecifics().autofill_profile().origin()); |
236 // http://crbug.com/170401 | 432 |
237 // EXPECT_EQ(kHttpOrigin, data[0].GetSpecifics().autofill_profile().origin()); | 433 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); |
238 // EXPECT_EQ(kHttpsOrigin, | |
239 // data[1].GetSpecifics().autofill_profile().origin()); | |
240 } | 434 } |
241 | 435 |
242 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { | 436 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { |
243 std::vector<AutofillProfile *> profiles_from_web_db; | 437 std::vector<AutofillProfile *> profiles_from_web_db; |
244 std::string guid_present = kGuid1; | 438 std::string guid_present = kGuid1; |
245 std::string guid_synced = kGuid2; | 439 std::string guid_synced = kGuid2; |
246 | 440 |
247 syncer::SyncChangeList change_list; | 441 syncer::SyncChangeList change_list; |
248 AutofillProfile profile(guid_synced, kHttpOrigin); | 442 AutofillProfile profile(guid_synced, kHttpOrigin); |
249 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); | 443 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); |
250 change_list.push_back( | 444 change_list.push_back( |
251 syncer::SyncChange(FROM_HERE, | 445 syncer::SyncChange( |
252 syncer::SyncChange::ACTION_ADD, | 446 FROM_HERE, |
253 AutofillProfileSyncableService::CreateData(profile))); | 447 syncer::SyncChange::ACTION_ADD, |
| 448 MockAutofillProfileSyncableService::CreateData(profile))); |
254 AutofillProfile empty_profile(guid_present, kHttpsOrigin); | 449 AutofillProfile empty_profile(guid_present, kHttpsOrigin); |
255 change_list.push_back( | 450 change_list.push_back( |
256 syncer::SyncChange( | 451 syncer::SyncChange( |
257 FROM_HERE, | 452 FROM_HERE, |
258 syncer::SyncChange::ACTION_DELETE, | 453 syncer::SyncChange::ACTION_DELETE, |
259 AutofillProfileSyncableService::CreateData(empty_profile))); | 454 MockAutofillProfileSyncableService::CreateData(empty_profile))); |
260 | 455 |
261 AutofillProfileSyncableService::DataBundle expected_bundle; | 456 MockAutofillProfileSyncableService::DataBundle expected_bundle; |
262 expected_bundle.profiles_to_delete.push_back(guid_present); | 457 expected_bundle.profiles_to_delete.push_back(guid_present); |
263 expected_bundle.profiles_to_add.push_back(&profile); | 458 expected_bundle.profiles_to_add.push_back(&profile); |
264 | 459 |
265 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( | 460 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( |
266 DataBundleCheck(expected_bundle))) | 461 DataBundleCheck(expected_bundle))) |
267 .Times(1) | 462 .Times(1) |
268 .WillOnce(Return(true)); | 463 .WillOnce(Return(true)); |
269 | 464 |
270 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); | 465 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); |
271 syncer::SyncError error = autofill_syncable_service_.ProcessSyncChanges( | 466 syncer::SyncError error = autofill_syncable_service_.ProcessSyncChanges( |
272 FROM_HERE, change_list); | 467 FROM_HERE, change_list); |
273 | 468 |
274 EXPECT_FALSE(error.IsSet()); | 469 EXPECT_FALSE(error.IsSet()); |
275 } | 470 } |
276 | 471 |
277 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { | 472 TEST_F(AutofillProfileSyncableServiceTest, AutofillProfileAdded) { |
278 AutofillProfile profile(kGuid1, std::string()); | 473 // Will be owned by the syncable service. Keep a reference available here for |
| 474 // verifying test expectations. |
| 475 TestSyncChangeProcessor* sync_change_processor = new TestSyncChangeProcessor; |
| 476 autofill_syncable_service_.set_sync_processor(sync_change_processor); |
| 477 |
| 478 AutofillProfile profile(kGuid1, kHttpsOrigin); |
279 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); | 479 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); |
280 AutofillProfileChange change1(AutofillProfileChange::ADD, kGuid1, &profile); | 480 AutofillProfileChange change(AutofillProfileChange::ADD, kGuid1, &profile); |
281 AutofillProfileChange change2(AutofillProfileChange::REMOVE, kGuid2, NULL); | 481 autofill_syncable_service_.AutofillProfileChanged(change); |
282 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) | |
283 .WillByDefault( | |
284 Return(syncer::SyncError(FROM_HERE, std::string("an error"), | |
285 syncer::AUTOFILL_PROFILE))); | |
286 EXPECT_CALL(*sync_processor_, ProcessSyncChanges(_, _)).Times(2); | |
287 | 482 |
288 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); | 483 ASSERT_EQ(1U, sync_change_processor->changes().size()); |
289 autofill_syncable_service_.ActOnChange(change1); | 484 syncer::SyncChange result = sync_change_processor->changes()[0]; |
290 autofill_syncable_service_.ActOnChange(change2); | 485 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, result.change_type()); |
| 486 |
| 487 sync_pb::AutofillProfileSpecifics specifics = |
| 488 result.sync_data().GetSpecifics().autofill_profile(); |
| 489 EXPECT_EQ(kGuid1, specifics.guid()); |
| 490 EXPECT_EQ(kHttpsOrigin, specifics.origin()); |
| 491 EXPECT_THAT(specifics.name_first(), testing::ElementsAre("Jane")); |
| 492 } |
| 493 |
| 494 TEST_F(AutofillProfileSyncableServiceTest, AutofillProfileDeleted) { |
| 495 // Will be owned by the syncable service. Keep a reference available here for |
| 496 // verifying test expectations. |
| 497 TestSyncChangeProcessor* sync_change_processor = new TestSyncChangeProcessor; |
| 498 autofill_syncable_service_.set_sync_processor(sync_change_processor); |
| 499 |
| 500 AutofillProfileChange change(AutofillProfileChange::REMOVE, kGuid2, NULL); |
| 501 autofill_syncable_service_.AutofillProfileChanged(change); |
| 502 |
| 503 ASSERT_EQ(1U, sync_change_processor->changes().size()); |
| 504 syncer::SyncChange result = sync_change_processor->changes()[0]; |
| 505 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, result.change_type()); |
| 506 sync_pb::AutofillProfileSpecifics specifics = |
| 507 result.sync_data().GetSpecifics().autofill_profile(); |
| 508 EXPECT_EQ(kGuid2, specifics.guid()); |
291 } | 509 } |
292 | 510 |
293 TEST_F(AutofillProfileSyncableServiceTest, UpdateField) { | 511 TEST_F(AutofillProfileSyncableServiceTest, UpdateField) { |
294 AutofillProfile profile(kGuid1, kSettingsOrigin); | 512 AutofillProfile profile(kGuid1, kSettingsOrigin); |
295 std::string company1 = "A Company"; | 513 std::string company1 = "A Company"; |
296 std::string company2 = "Another Company"; | 514 std::string company2 = "Another Company"; |
297 profile.SetRawInfo(autofill::COMPANY_NAME, UTF8ToUTF16(company1)); | 515 profile.SetRawInfo(autofill::COMPANY_NAME, UTF8ToUTF16(company1)); |
298 EXPECT_FALSE(AutofillProfileSyncableService::UpdateField( | 516 EXPECT_FALSE(AutofillProfileSyncableService::UpdateField( |
299 autofill::COMPANY_NAME, company1, &profile)); | 517 autofill::COMPANY_NAME, company1, &profile)); |
300 EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME), UTF8ToUTF16(company1)); | 518 EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME), UTF8ToUTF16(company1)); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 TEST_F(AutofillProfileSyncableServiceTest, MergeProfile) { | 561 TEST_F(AutofillProfileSyncableServiceTest, MergeProfile) { |
344 AutofillProfile profile1(kGuid1, kHttpOrigin); | 562 AutofillProfile profile1(kGuid1, kHttpOrigin); |
345 profile1.SetRawInfo( | 563 profile1.SetRawInfo( |
346 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); | 564 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); |
347 | 565 |
348 std::vector<string16> values; | 566 std::vector<string16> values; |
349 values.push_back(UTF8ToUTF16("1@1.com")); | 567 values.push_back(UTF8ToUTF16("1@1.com")); |
350 values.push_back(UTF8ToUTF16("2@1.com")); | 568 values.push_back(UTF8ToUTF16("2@1.com")); |
351 profile1.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values); | 569 profile1.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values); |
352 | 570 |
353 AutofillProfile profile2(kGuid2, std::string()); | 571 AutofillProfile profile2(kGuid2, kHttpsOrigin); |
354 profile2.SetRawInfo( | 572 profile2.SetRawInfo( |
355 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); | 573 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); |
356 | 574 |
357 // |values| now is [ "1@1.com", "2@1.com", "3@1.com" ]. | 575 // |values| now is [ "1@1.com", "2@1.com", "3@1.com" ]. |
358 values.push_back(UTF8ToUTF16("3@1.com")); | 576 values.push_back(UTF8ToUTF16("3@1.com")); |
359 profile2.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values); | 577 profile2.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values); |
360 | 578 |
361 values.clear(); | 579 values.clear(); |
362 values.push_back(UTF8ToUTF16("John")); | 580 values.push_back(UTF8ToUTF16("John")); |
363 profile1.SetRawMultiInfo(autofill::NAME_FIRST, values); | 581 profile1.SetRawMultiInfo(autofill::NAME_FIRST, values); |
(...skipping 27 matching lines...) Expand all Loading... |
391 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values); | 609 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values); |
392 ASSERT_EQ(values.size(), 3U); | 610 ASSERT_EQ(values.size(), 3U); |
393 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); | 611 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); |
394 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); | 612 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); |
395 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); | 613 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); |
396 | 614 |
397 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values); | 615 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values); |
398 ASSERT_EQ(values.size(), 1U); | 616 ASSERT_EQ(values.size(), 1U); |
399 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); | 617 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); |
400 | 618 |
| 619 EXPECT_EQ(profile2.origin(), profile1.origin()); |
| 620 |
401 AutofillProfile profile3(kGuid3, kHttpOrigin); | 621 AutofillProfile profile3(kGuid3, kHttpOrigin); |
402 profile3.SetRawInfo( | 622 profile3.SetRawInfo( |
403 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); | 623 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); |
404 | 624 |
405 values.clear(); | 625 values.clear(); |
406 values.push_back(UTF8ToUTF16("Jane")); | 626 values.push_back(UTF8ToUTF16("Jane")); |
407 profile3.SetRawMultiInfo(autofill::NAME_FIRST, values); | 627 profile3.SetRawMultiInfo(autofill::NAME_FIRST, values); |
408 | 628 |
409 values.clear(); | 629 values.clear(); |
410 values.push_back(UTF8ToUTF16("Doe")); | 630 values.push_back(UTF8ToUTF16("Doe")); |
(...skipping 25 matching lines...) Expand all Loading... |
436 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values); | 656 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values); |
437 ASSERT_EQ(values.size(), 3U); | 657 ASSERT_EQ(values.size(), 3U); |
438 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); | 658 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); |
439 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); | 659 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); |
440 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); | 660 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); |
441 | 661 |
442 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values); | 662 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values); |
443 ASSERT_EQ(values.size(), 1U); | 663 ASSERT_EQ(values.size(), 1U); |
444 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); | 664 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); |
445 } | 665 } |
OLD | NEW |