Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: chrome/browser/webdata/autofill_profile_syncable_service_unittest.cc

Issue 16024018: [Autofill] Sync Autofill profiles' origins. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Hyphens? We don't need no stinkin' hyphens. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/browser/autofill_profile.h" 9 #include "components/autofill/browser/autofill_profile.h"
10 #include "components/autofill/browser/webdata/autofill_change.h" 10 #include "components/autofill/browser/webdata/autofill_change.h"
(...skipping 18 matching lines...) Expand all
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
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
170 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) 217 MergeDataAndStartSyncing(
171 .Times(1) 218 profiles_from_web_db, data_list, expected_bundle, expected_change_list);
172 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); 219 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE);
173 EXPECT_CALL(autofill_syncable_service_, 220 }
174 SaveChangesToWebData(DataBundleCheck(expected_bundle)))
175 .Times(1)
176 .WillOnce(Return(true));
177 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _))
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 221
184 // Takes ownership of sync_processor_. 222 TEST_F(AutofillProfileSyncableServiceTest, MergeIdenticalProfiles) {
185 autofill_syncable_service_.MergeDataAndStartSyncing( 223 std::vector<AutofillProfile*> profiles_from_web_db;
186 syncer::AUTOFILL_PROFILE, data_list, 224 std::string guid_present1 = kGuid1;
187 sync_processor_.PassAs<syncer::SyncChangeProcessor>(), 225 std::string guid_present2 = kGuid2;
188 scoped_ptr<syncer::SyncErrorFactory>(new syncer::SyncErrorFactoryMock())); 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);
189 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE); 340 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE);
190 } 341 }
191 342
192 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) { 343 TEST_F(AutofillProfileSyncableServiceTest, GetAllSyncData) {
193 std::vector<AutofillProfile *> profiles_from_web_db; 344 std::vector<AutofillProfile*> profiles_from_web_db;
194 std::string guid_present1 = kGuid1; 345 std::string guid_present1 = kGuid1;
195 std::string guid_present2 = kGuid2; 346 std::string guid_present2 = kGuid2;
196 347
197 profiles_from_web_db.push_back( 348 profiles_from_web_db.push_back(
198 new AutofillProfile(guid_present1, kHttpOrigin)); 349 new AutofillProfile(guid_present1, kHttpOrigin));
199 profiles_from_web_db.back()->SetRawInfo( 350 profiles_from_web_db.back()->SetRawInfo(
200 autofill::NAME_FIRST, UTF8ToUTF16("John")); 351 autofill::NAME_FIRST, UTF8ToUTF16("John"));
201 profiles_from_web_db.push_back( 352 profiles_from_web_db.push_back(
202 new AutofillProfile(guid_present2, kHttpsOrigin)); 353 new AutofillProfile(guid_present2, kHttpsOrigin));
203 profiles_from_web_db.back()->SetRawInfo( 354 profiles_from_web_db.back()->SetRawInfo(
204 autofill::NAME_FIRST, UTF8ToUTF16("Jane")); 355 autofill::NAME_FIRST, UTF8ToUTF16("Jane"));
205 356
206 EXPECT_CALL(autofill_syncable_service_, LoadAutofillData(_)) 357 syncer::SyncChangeList expected_change_list;
207 .Times(1) 358 expected_change_list.push_back(
208 .WillOnce(DoAll(CopyData(&profiles_from_web_db), Return(true))); 359 syncer::SyncChange(FROM_HERE,
209 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(_)) 360 syncer::SyncChange::ACTION_ADD,
210 .Times(1) 361 MockAutofillProfileSyncableService::CreateData(
211 .WillOnce(Return(true)); 362 *profiles_from_web_db.front())));
212 ON_CALL(*sync_processor_, ProcessSyncChanges(_, _)) 363 expected_change_list.push_back(
213 .WillByDefault(Return(syncer::SyncError())); 364 syncer::SyncChange(FROM_HERE,
214 EXPECT_CALL(*sync_processor_, 365 syncer::SyncChange::ACTION_ADD,
215 ProcessSyncChanges( 366 MockAutofillProfileSyncableService::CreateData(
216 _, 367 *profiles_from_web_db.back())));
217 Property(&syncer::SyncChangeList::size, Eq(2U))))
218 .Times(1)
219 .WillOnce(Return(syncer::SyncError()));
220 368
369 MockAutofillProfileSyncableService::DataBundle expected_bundle;
221 syncer::SyncDataList data_list; 370 syncer::SyncDataList data_list;
222 // Takes ownership of sync_processor_. 371 MergeDataAndStartSyncing(
223 autofill_syncable_service_.MergeDataAndStartSyncing( 372 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 373
228 syncer::SyncDataList data = 374 syncer::SyncDataList data =
229 autofill_syncable_service_.GetAllSyncData(syncer::AUTOFILL_PROFILE); 375 autofill_syncable_service_.GetAllSyncData(syncer::AUTOFILL_PROFILE);
230 376
231 ASSERT_EQ(2U, data.size()); 377 ASSERT_EQ(2U, data.size());
232 EXPECT_EQ(guid_present1, data[0].GetSpecifics().autofill_profile().guid()); 378 EXPECT_EQ(guid_present1, data[0].GetSpecifics().autofill_profile().guid());
233 EXPECT_EQ(guid_present2, data[1].GetSpecifics().autofill_profile().guid()); 379 EXPECT_EQ(guid_present2, data[1].GetSpecifics().autofill_profile().guid());
234 // TODO(isherman): Verify that the origins match once they are saved and read 380 EXPECT_EQ(kHttpOrigin, data[0].GetSpecifics().autofill_profile().origin());
235 // from the database and included in the Sync protocol buffers. 381 EXPECT_EQ(kHttpsOrigin, data[1].GetSpecifics().autofill_profile().origin());
236 // http://crbug.com/170401 382
237 // EXPECT_EQ(kHttpOrigin, data[0].GetSpecifics().autofill_profile().origin()); 383 autofill_syncable_service_.StopSyncing(syncer::AUTOFILL_PROFILE);
238 // EXPECT_EQ(kHttpsOrigin,
239 // data[1].GetSpecifics().autofill_profile().origin());
240 } 384 }
241 385
242 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) { 386 TEST_F(AutofillProfileSyncableServiceTest, ProcessSyncChanges) {
243 std::vector<AutofillProfile *> profiles_from_web_db; 387 std::vector<AutofillProfile *> profiles_from_web_db;
244 std::string guid_present = kGuid1; 388 std::string guid_present = kGuid1;
245 std::string guid_synced = kGuid2; 389 std::string guid_synced = kGuid2;
246 390
247 syncer::SyncChangeList change_list; 391 syncer::SyncChangeList change_list;
248 AutofillProfile profile(guid_synced, kHttpOrigin); 392 AutofillProfile profile(guid_synced, kHttpOrigin);
249 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); 393 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane"));
250 change_list.push_back( 394 change_list.push_back(
251 syncer::SyncChange(FROM_HERE, 395 syncer::SyncChange(
252 syncer::SyncChange::ACTION_ADD, 396 FROM_HERE,
253 AutofillProfileSyncableService::CreateData(profile))); 397 syncer::SyncChange::ACTION_ADD,
398 MockAutofillProfileSyncableService::CreateData(profile)));
254 AutofillProfile empty_profile(guid_present, kHttpsOrigin); 399 AutofillProfile empty_profile(guid_present, kHttpsOrigin);
255 change_list.push_back( 400 change_list.push_back(
256 syncer::SyncChange( 401 syncer::SyncChange(
257 FROM_HERE, 402 FROM_HERE,
258 syncer::SyncChange::ACTION_DELETE, 403 syncer::SyncChange::ACTION_DELETE,
259 AutofillProfileSyncableService::CreateData(empty_profile))); 404 MockAutofillProfileSyncableService::CreateData(empty_profile)));
260 405
261 AutofillProfileSyncableService::DataBundle expected_bundle; 406 MockAutofillProfileSyncableService::DataBundle expected_bundle;
262 expected_bundle.profiles_to_delete.push_back(guid_present); 407 expected_bundle.profiles_to_delete.push_back(guid_present);
263 expected_bundle.profiles_to_add.push_back(&profile); 408 expected_bundle.profiles_to_add.push_back(&profile);
264 409
265 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData( 410 EXPECT_CALL(autofill_syncable_service_, SaveChangesToWebData(
266 DataBundleCheck(expected_bundle))) 411 DataBundleCheck(expected_bundle)))
267 .Times(1) 412 .Times(1)
268 .WillOnce(Return(true)); 413 .WillOnce(Return(true));
269 414
270 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); 415 autofill_syncable_service_.set_sync_processor(sync_processor_.release());
271 syncer::SyncError error = autofill_syncable_service_.ProcessSyncChanges( 416 syncer::SyncError error = autofill_syncable_service_.ProcessSyncChanges(
272 FROM_HERE, change_list); 417 FROM_HERE, change_list);
273 418
274 EXPECT_FALSE(error.IsSet()); 419 EXPECT_FALSE(error.IsSet());
275 } 420 }
276 421
277 TEST_F(AutofillProfileSyncableServiceTest, ActOnChange) { 422 TEST_F(AutofillProfileSyncableServiceTest, AutofillProfileAdded) {
278 AutofillProfile profile(kGuid1, std::string()); 423 // Will be owned by the syncable service. Keep a reference available here for
424 // verifying test expectations.
425 TestSyncChangeProcessor* sync_change_processor = new TestSyncChangeProcessor;
426 autofill_syncable_service_.set_sync_processor(sync_change_processor);
427
428 AutofillProfile profile(kGuid1, kHttpsOrigin);
279 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane")); 429 profile.SetRawInfo(autofill::NAME_FIRST, UTF8ToUTF16("Jane"));
280 AutofillProfileChange change1(AutofillProfileChange::ADD, kGuid1, &profile); 430 AutofillProfileChange change(AutofillProfileChange::ADD, kGuid1, &profile);
281 AutofillProfileChange change2(AutofillProfileChange::REMOVE, kGuid2, NULL); 431 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 432
288 autofill_syncable_service_.set_sync_processor(sync_processor_.release()); 433 ASSERT_EQ(1U, sync_change_processor->changes().size());
289 autofill_syncable_service_.ActOnChange(change1); 434 syncer::SyncChange result = sync_change_processor->changes()[0];
290 autofill_syncable_service_.ActOnChange(change2); 435 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, result.change_type());
436
437 sync_pb::AutofillProfileSpecifics specifics =
438 result.sync_data().GetSpecifics().autofill_profile();
439 EXPECT_EQ(kGuid1, specifics.guid());
440 EXPECT_EQ(kHttpsOrigin, specifics.origin());
441 EXPECT_THAT(specifics.name_first(), testing::ElementsAre("Jane"));
442 }
443
444 TEST_F(AutofillProfileSyncableServiceTest, AutofillProfileDeleted) {
445 // Will be owned by the syncable service. Keep a reference available here for
446 // verifying test expectations.
447 TestSyncChangeProcessor* sync_change_processor = new TestSyncChangeProcessor;
448 autofill_syncable_service_.set_sync_processor(sync_change_processor);
449
450 AutofillProfileChange change(AutofillProfileChange::REMOVE, kGuid2, NULL);
451 autofill_syncable_service_.AutofillProfileChanged(change);
452
453 ASSERT_EQ(1U, sync_change_processor->changes().size());
454 syncer::SyncChange result = sync_change_processor->changes()[0];
455 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, result.change_type());
456 sync_pb::AutofillProfileSpecifics specifics =
457 result.sync_data().GetSpecifics().autofill_profile();
458 EXPECT_EQ(kGuid2, specifics.guid());
291 } 459 }
292 460
293 TEST_F(AutofillProfileSyncableServiceTest, UpdateField) { 461 TEST_F(AutofillProfileSyncableServiceTest, UpdateField) {
294 AutofillProfile profile(kGuid1, kSettingsOrigin); 462 AutofillProfile profile(kGuid1, kSettingsOrigin);
295 std::string company1 = "A Company"; 463 std::string company1 = "A Company";
296 std::string company2 = "Another Company"; 464 std::string company2 = "Another Company";
297 profile.SetRawInfo(autofill::COMPANY_NAME, UTF8ToUTF16(company1)); 465 profile.SetRawInfo(autofill::COMPANY_NAME, UTF8ToUTF16(company1));
298 EXPECT_FALSE(AutofillProfileSyncableService::UpdateField( 466 EXPECT_FALSE(AutofillProfileSyncableService::UpdateField(
299 autofill::COMPANY_NAME, company1, &profile)); 467 autofill::COMPANY_NAME, company1, &profile));
300 EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME), UTF8ToUTF16(company1)); 468 EXPECT_EQ(profile.GetRawInfo(autofill::COMPANY_NAME), UTF8ToUTF16(company1));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 TEST_F(AutofillProfileSyncableServiceTest, MergeProfile) { 511 TEST_F(AutofillProfileSyncableServiceTest, MergeProfile) {
344 AutofillProfile profile1(kGuid1, kHttpOrigin); 512 AutofillProfile profile1(kGuid1, kHttpOrigin);
345 profile1.SetRawInfo( 513 profile1.SetRawInfo(
346 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); 514 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St."));
347 515
348 std::vector<string16> values; 516 std::vector<string16> values;
349 values.push_back(UTF8ToUTF16("1@1.com")); 517 values.push_back(UTF8ToUTF16("1@1.com"));
350 values.push_back(UTF8ToUTF16("2@1.com")); 518 values.push_back(UTF8ToUTF16("2@1.com"));
351 profile1.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values); 519 profile1.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values);
352 520
353 AutofillProfile profile2(kGuid2, std::string()); 521 AutofillProfile profile2(kGuid2, kHttpsOrigin);
354 profile2.SetRawInfo( 522 profile2.SetRawInfo(
355 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); 523 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St."));
356 524
357 // |values| now is [ "1@1.com", "2@1.com", "3@1.com" ]. 525 // |values| now is [ "1@1.com", "2@1.com", "3@1.com" ].
358 values.push_back(UTF8ToUTF16("3@1.com")); 526 values.push_back(UTF8ToUTF16("3@1.com"));
359 profile2.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values); 527 profile2.SetRawMultiInfo(autofill::EMAIL_ADDRESS, values);
360 528
361 values.clear(); 529 values.clear();
362 values.push_back(UTF8ToUTF16("John")); 530 values.push_back(UTF8ToUTF16("John"));
363 profile1.SetRawMultiInfo(autofill::NAME_FIRST, values); 531 profile1.SetRawMultiInfo(autofill::NAME_FIRST, values);
(...skipping 27 matching lines...) Expand all
391 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values); 559 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values);
392 ASSERT_EQ(values.size(), 3U); 560 ASSERT_EQ(values.size(), 3U);
393 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); 561 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com"));
394 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); 562 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com"));
395 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); 563 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com"));
396 564
397 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values); 565 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values);
398 ASSERT_EQ(values.size(), 1U); 566 ASSERT_EQ(values.size(), 1U);
399 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); 567 EXPECT_EQ(values[0], UTF8ToUTF16("650234567"));
400 568
569 EXPECT_EQ(profile2.origin(), profile1.origin());
570
401 AutofillProfile profile3(kGuid3, kHttpOrigin); 571 AutofillProfile profile3(kGuid3, kHttpOrigin);
402 profile3.SetRawInfo( 572 profile3.SetRawInfo(
403 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St.")); 573 autofill::ADDRESS_HOME_LINE1, UTF8ToUTF16("111 First St."));
404 574
405 values.clear(); 575 values.clear();
406 values.push_back(UTF8ToUTF16("Jane")); 576 values.push_back(UTF8ToUTF16("Jane"));
407 profile3.SetRawMultiInfo(autofill::NAME_FIRST, values); 577 profile3.SetRawMultiInfo(autofill::NAME_FIRST, values);
408 578
409 values.clear(); 579 values.clear();
410 values.push_back(UTF8ToUTF16("Doe")); 580 values.push_back(UTF8ToUTF16("Doe"));
(...skipping 25 matching lines...) Expand all
436 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values); 606 profile1.GetRawMultiInfo(autofill::EMAIL_ADDRESS, &values);
437 ASSERT_EQ(values.size(), 3U); 607 ASSERT_EQ(values.size(), 3U);
438 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com")); 608 EXPECT_EQ(values[0], UTF8ToUTF16("1@1.com"));
439 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com")); 609 EXPECT_EQ(values[1], UTF8ToUTF16("2@1.com"));
440 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com")); 610 EXPECT_EQ(values[2], UTF8ToUTF16("3@1.com"));
441 611
442 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values); 612 profile1.GetRawMultiInfo(autofill::PHONE_HOME_WHOLE_NUMBER, &values);
443 ASSERT_EQ(values.size(), 1U); 613 ASSERT_EQ(values.size(), 1U);
444 EXPECT_EQ(values[0], UTF8ToUTF16("650234567")); 614 EXPECT_EQ(values[0], UTF8ToUTF16("650234567"));
445 } 615 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698