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
3 // found in the LICENSE file.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/sync/test/integration/autofill_helper.h"
7 #include "chrome/browser/sync/test/integration/bookmarks_helper.h"
8 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
9 #include "chrome/browser/sync/test/integration/sync_test.h"
10 #include "components/autofill/core/browser/autofill_profile.h"
11 #include "components/autofill/core/browser/credit_card.h"
12 #include "components/autofill/core/browser/personal_data_manager.h"
13 #include "components/autofill/core/browser/webdata/autofill_entry.h"
14 #include "components/autofill/core/browser/webdata/autofill_table.h"
16 using autofill::AutofillKey
;
17 using autofill::AutofillTable
;
18 using autofill::AutofillProfile
;
19 using autofill::AutofillType
;
20 using autofill::CreditCard
;
21 using autofill::PersonalDataManager
;
22 using autofill_helper::AddKeys
;
23 using autofill_helper::AddProfile
;
24 using autofill_helper::CreateAutofillProfile
;
25 using autofill_helper::GetAllKeys
;
26 using autofill_helper::GetAllProfiles
;
27 using autofill_helper::GetPersonalDataManager
;
28 using autofill_helper::KeysMatch
;
29 using autofill_helper::ProfilesMatch
;
30 using autofill_helper::PROFILE_FRASIER
;
31 using autofill_helper::PROFILE_HOMER
;
32 using autofill_helper::PROFILE_MARION
;
33 using autofill_helper::PROFILE_NULL
;
34 using autofill_helper::RemoveKey
;
35 using autofill_helper::RemoveProfile
;
36 using autofill_helper::SetCreditCards
;
37 using autofill_helper::UpdateProfile
;
38 using bookmarks_helper::AddFolder
;
39 using bookmarks_helper::AddURL
;
40 using bookmarks_helper::IndexedURL
;
41 using bookmarks_helper::IndexedURLTitle
;
43 class TwoClientAutofillSyncTest
: public SyncTest
{
45 TwoClientAutofillSyncTest() : SyncTest(TWO_CLIENT
) { count
= 0; }
46 virtual ~TwoClientAutofillSyncTest() {}
48 // We do this so as to make a change that will trigger the autofill to sync.
49 // By default autofill does not sync unless there is some other change.
50 void MakeABookmarkChange(int profile
) {
52 AddURL(profile
, IndexedURLTitle(count
), GURL(IndexedURL(count
))));
57 DISALLOW_COPY_AND_ASSIGN(TwoClientAutofillSyncTest
);
60 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, WebDataServiceSanity
) {
61 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
63 // Client0 adds a key.
64 std::set
<AutofillKey
> keys
;
65 keys
.insert(AutofillKey("name0", "value0"));
67 MakeABookmarkChange(0);
68 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
69 ASSERT_TRUE(KeysMatch(0, 1));
70 ASSERT_EQ(1U, GetAllKeys(0).size());
72 // Client1 adds a key.
74 keys
.insert(AutofillKey("name1", "value1-0"));
76 MakeABookmarkChange(1);
77 ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
78 ASSERT_TRUE(KeysMatch(0, 1));
79 ASSERT_EQ(2U, GetAllKeys(0).size());
81 // Client0 adds a key with the same name.
83 keys
.insert(AutofillKey("name1", "value1-1"));
85 MakeABookmarkChange(0);
86 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
87 ASSERT_TRUE(KeysMatch(0, 1));
88 ASSERT_EQ(3U, GetAllKeys(0).size());
90 // Client1 removes a key.
91 RemoveKey(1, AutofillKey("name1", "value1-0"));
92 MakeABookmarkChange(1);
93 ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
94 ASSERT_TRUE(KeysMatch(0, 1));
95 ASSERT_EQ(2U, GetAllKeys(0).size());
97 // Client0 removes the rest.
98 RemoveKey(0, AutofillKey("name0", "value0"));
99 RemoveKey(0, AutofillKey("name1", "value1-1"));
100 MakeABookmarkChange(0);
101 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
102 ASSERT_TRUE(KeysMatch(0, 1));
103 ASSERT_EQ(0U, GetAllKeys(0).size());
107 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, AddUnicodeProfile
) {
108 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
110 std::set
<AutofillKey
> keys
;
111 keys
.insert(AutofillKey(base::WideToUTF16(L
"Sigur R\u00F3s"),
112 base::WideToUTF16(L
"\u00C1g\u00E6tis byrjun")));
114 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
115 ASSERT_TRUE(AwaitQuiescence());
116 ASSERT_TRUE(KeysMatch(0, 1));
119 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
,
120 AddDuplicateNamesToSameProfile
) {
121 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
123 std::set
<AutofillKey
> keys
;
124 keys
.insert(AutofillKey("name0", "value0-0"));
125 keys
.insert(AutofillKey("name0", "value0-1"));
126 keys
.insert(AutofillKey("name1", "value1"));
128 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
129 ASSERT_TRUE(AwaitQuiescence());
130 ASSERT_TRUE(KeysMatch(0, 1));
131 ASSERT_EQ(2U, GetAllKeys(0).size());
134 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
,
135 AddDuplicateNamesToDifferentProfiles
) {
136 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
138 std::set
<AutofillKey
> keys0
;
139 keys0
.insert(AutofillKey("name0", "value0-0"));
140 keys0
.insert(AutofillKey("name1", "value1"));
143 std::set
<AutofillKey
> keys1
;
144 keys1
.insert(AutofillKey("name0", "value0-1"));
145 keys1
.insert(AutofillKey("name2", "value2"));
146 keys1
.insert(AutofillKey("name3", "value3"));
149 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
150 ASSERT_TRUE(AwaitQuiescence());
151 ASSERT_TRUE(KeysMatch(0, 1));
152 ASSERT_EQ(5U, GetAllKeys(0).size());
155 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
,
156 PersonalDataManagerSanity
) {
157 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
159 // Client0 adds a profile.
160 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
161 MakeABookmarkChange(0);
162 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
163 ASSERT_TRUE(ProfilesMatch(0, 1));
164 ASSERT_EQ(1U, GetAllProfiles(0).size());
166 // Client1 adds a profile.
167 AddProfile(1, CreateAutofillProfile(PROFILE_MARION
));
168 MakeABookmarkChange(1);
169 ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
170 ASSERT_TRUE(ProfilesMatch(0, 1));
171 ASSERT_EQ(2U, GetAllProfiles(0).size());
173 // Client0 adds the same profile.
174 AddProfile(0, CreateAutofillProfile(PROFILE_MARION
));
175 MakeABookmarkChange(0);
176 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
177 ASSERT_TRUE(ProfilesMatch(0, 1));
178 ASSERT_EQ(2U, GetAllProfiles(0).size());
180 // Client1 removes a profile.
181 RemoveProfile(1, GetAllProfiles(1)[0]->guid());
182 MakeABookmarkChange(1);
183 ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
184 ASSERT_TRUE(ProfilesMatch(0, 1));
185 ASSERT_EQ(1U, GetAllProfiles(0).size());
187 // Client0 updates a profile.
189 GetAllProfiles(0)[0]->guid(),
190 AutofillType(autofill::NAME_FIRST
),
191 base::ASCIIToUTF16("Bart"));
192 MakeABookmarkChange(0);
193 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
194 ASSERT_TRUE(ProfilesMatch(0, 1));
195 ASSERT_EQ(1U, GetAllProfiles(0).size());
197 // Client1 removes remaining profile.
198 RemoveProfile(1, GetAllProfiles(1)[0]->guid());
199 MakeABookmarkChange(1);
200 ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
201 ASSERT_TRUE(ProfilesMatch(0, 1));
202 ASSERT_EQ(0U, GetAllProfiles(0).size());
206 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, AddDuplicateProfiles
) {
207 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
209 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
210 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
211 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
212 ASSERT_TRUE(AwaitQuiescence());
213 ASSERT_TRUE(ProfilesMatch(0, 1));
214 ASSERT_EQ(1U, GetAllProfiles(0).size());
218 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, SameProfileWithConflict
) {
219 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
221 AutofillProfile profile0
= CreateAutofillProfile(PROFILE_HOMER
);
222 AutofillProfile profile1
= CreateAutofillProfile(PROFILE_HOMER
);
223 profile1
.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER
,
224 base::ASCIIToUTF16("1234567890"));
226 AddProfile(0, profile0
);
227 AddProfile(1, profile1
);
228 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
229 ASSERT_TRUE(AwaitQuiescence());
230 ASSERT_TRUE(ProfilesMatch(0, 1));
231 ASSERT_EQ(1U, GetAllProfiles(0).size());
235 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, AddEmptyProfile
) {
236 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
238 AddProfile(0, CreateAutofillProfile(PROFILE_NULL
));
239 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
240 ASSERT_TRUE(ProfilesMatch(0, 1));
241 ASSERT_EQ(0U, GetAllProfiles(0).size());
245 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, AddProfile
) {
246 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
248 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
249 MakeABookmarkChange(0);
250 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
251 ASSERT_TRUE(ProfilesMatch(0, 1));
252 ASSERT_EQ(1U, GetAllProfiles(0).size());
256 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, AddMultipleProfiles
) {
257 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
259 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
260 AddProfile(0, CreateAutofillProfile(PROFILE_MARION
));
261 AddProfile(0, CreateAutofillProfile(PROFILE_FRASIER
));
262 MakeABookmarkChange(0);
263 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
264 ASSERT_TRUE(ProfilesMatch(0, 1));
265 ASSERT_EQ(3U, GetAllProfiles(0).size());
269 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, DeleteProfile
) {
270 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
272 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
273 MakeABookmarkChange(0);
274 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
275 ASSERT_TRUE(ProfilesMatch(0, 1));
276 ASSERT_EQ(1U, GetAllProfiles(0).size());
278 RemoveProfile(1, GetAllProfiles(1)[0]->guid());
279 MakeABookmarkChange(1);
280 ASSERT_TRUE(GetClient(1)->AwaitMutualSyncCycleCompletion(GetClient(0)));
281 ASSERT_TRUE(ProfilesMatch(0, 1));
282 ASSERT_EQ(0U, GetAllProfiles(0).size());
286 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, MergeProfiles
) {
287 ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
289 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
290 AddProfile(1, CreateAutofillProfile(PROFILE_MARION
));
291 AddProfile(1, CreateAutofillProfile(PROFILE_FRASIER
));
292 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
293 ASSERT_TRUE(AwaitQuiescence());
294 ASSERT_TRUE(ProfilesMatch(0, 1));
295 ASSERT_EQ(3U, GetAllProfiles(0).size());
299 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, UpdateFields
) {
300 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
302 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
303 MakeABookmarkChange(0);
304 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
305 ASSERT_TRUE(ProfilesMatch(0, 1));
306 ASSERT_EQ(1U, GetAllProfiles(0).size());
309 GetAllProfiles(0)[0]->guid(),
310 AutofillType(autofill::NAME_FIRST
),
311 base::ASCIIToUTF16("Lisa"));
313 GetAllProfiles(0)[0]->guid(),
314 AutofillType(autofill::EMAIL_ADDRESS
),
315 base::ASCIIToUTF16("grrrl@TV.com"));
316 MakeABookmarkChange(0);
317 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
318 ASSERT_TRUE(ProfilesMatch(0, 1));
319 ASSERT_EQ(1U, GetAllProfiles(0).size());
323 // This test is flaky on all platforms. See crbug.com/152551.
324 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, DISABLED_ConflictingFields
) {
325 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
327 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
328 MakeABookmarkChange(0);
329 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
330 ASSERT_TRUE(ProfilesMatch(0, 1));
331 ASSERT_EQ(1U, GetAllProfiles(0).size());
333 GetAllProfiles(0)[0]->guid(),
334 AutofillType(autofill::NAME_FIRST
),
335 base::ASCIIToUTF16("Lisa"));
336 MakeABookmarkChange(0);
338 GetAllProfiles(1)[0]->guid(),
339 AutofillType(autofill::NAME_FIRST
),
340 base::ASCIIToUTF16("Bart"));
341 MakeABookmarkChange(1);
342 ASSERT_TRUE(AwaitQuiescence());
343 ASSERT_TRUE(ProfilesMatch(0, 1));
344 ASSERT_EQ(1U, GetAllProfiles(0).size());
348 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, DisableAutofill
) {
349 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
351 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
352 MakeABookmarkChange(0);
353 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
354 ASSERT_TRUE(ProfilesMatch(0, 1));
355 ASSERT_EQ(1U, GetAllProfiles(0).size());
357 ASSERT_TRUE(GetClient(0)->DisableSyncForDatatype(syncer::AUTOFILL
));
358 AddProfile(0, CreateAutofillProfile(PROFILE_FRASIER
));
359 MakeABookmarkChange(0);
360 ASSERT_TRUE(AwaitQuiescence());
361 ASSERT_FALSE(ProfilesMatch(0, 1));
362 ASSERT_EQ(2U, GetAllProfiles(0).size());
363 ASSERT_EQ(1U, GetAllProfiles(1).size());
365 ASSERT_TRUE(GetClient(0)->EnableSyncForDatatype(syncer::AUTOFILL
));
366 MakeABookmarkChange(0);
367 ASSERT_TRUE(AwaitQuiescence());
368 ASSERT_TRUE(ProfilesMatch(0, 1));
369 ASSERT_EQ(2U, GetAllProfiles(0).size());
373 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, DisableSync
) {
374 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
376 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
377 MakeABookmarkChange(0);
378 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
379 ASSERT_TRUE(ProfilesMatch(0, 1));
380 ASSERT_EQ(1U, GetAllProfiles(0).size());
382 ASSERT_TRUE(GetClient(1)->DisableSyncForAllDatatypes());
383 AddProfile(0, CreateAutofillProfile(PROFILE_FRASIER
));
384 MakeABookmarkChange(0);
385 ASSERT_TRUE(GetClient(0)->AwaitFullSyncCompletion());
386 ASSERT_FALSE(ProfilesMatch(0, 1));
387 ASSERT_EQ(2U, GetAllProfiles(0).size());
388 ASSERT_EQ(1U, GetAllProfiles(1).size());
390 ASSERT_TRUE(GetClient(1)->EnableSyncForAllDatatypes());
391 ASSERT_TRUE(AwaitQuiescence());
392 ASSERT_TRUE(ProfilesMatch(0, 1));
393 ASSERT_EQ(2U, GetAllProfiles(0).size());
397 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, MaxLength
) {
398 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
400 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
401 MakeABookmarkChange(0);
402 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
403 ASSERT_TRUE(ProfilesMatch(0, 1));
404 ASSERT_EQ(1U, GetAllProfiles(0).size());
406 base::string16
max_length_string(AutofillTable::kMaxDataLength
, '.');
408 GetAllProfiles(0)[0]->guid(),
409 AutofillType(autofill::NAME_FIRST
),
412 GetAllProfiles(0)[0]->guid(),
413 AutofillType(autofill::NAME_LAST
),
416 GetAllProfiles(0)[0]->guid(),
417 AutofillType(autofill::EMAIL_ADDRESS
),
420 GetAllProfiles(0)[0]->guid(),
421 AutofillType(autofill::ADDRESS_HOME_LINE1
),
424 MakeABookmarkChange(0);
425 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
426 ASSERT_TRUE(ProfilesMatch(0, 1));
429 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, ExceedsMaxLength
) {
430 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
432 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
433 MakeABookmarkChange(0);
434 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
435 ASSERT_TRUE(ProfilesMatch(0, 1));
436 ASSERT_EQ(1U, GetAllProfiles(0).size());
438 base::string16
exceeds_max_length_string(
439 AutofillTable::kMaxDataLength
+ 1, '.');
441 GetAllProfiles(0)[0]->guid(),
442 AutofillType(autofill::NAME_FIRST
),
443 exceeds_max_length_string
);
445 GetAllProfiles(0)[0]->guid(),
446 AutofillType(autofill::NAME_LAST
),
447 exceeds_max_length_string
);
449 GetAllProfiles(0)[0]->guid(),
450 AutofillType(autofill::EMAIL_ADDRESS
),
451 exceeds_max_length_string
);
453 GetAllProfiles(0)[0]->guid(),
454 AutofillType(autofill::ADDRESS_HOME_LINE1
),
455 exceeds_max_length_string
);
457 MakeABookmarkChange(0);
458 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
459 ASSERT_FALSE(ProfilesMatch(0, 1));
462 // Test credit cards don't sync.
463 IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest
, NoCreditCardSync
) {
464 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
466 AddProfile(0, CreateAutofillProfile(PROFILE_HOMER
));
469 card
.SetRawInfo(autofill::CREDIT_CARD_NUMBER
,
470 base::ASCIIToUTF16("6011111111111117"));
471 std::vector
<CreditCard
> credit_cards
;
472 credit_cards
.push_back(card
);
473 SetCreditCards(0, &credit_cards
);
475 MakeABookmarkChange(0);
476 ASSERT_TRUE(GetClient(0)->AwaitMutualSyncCycleCompletion(GetClient(1)));
477 ASSERT_TRUE(ProfilesMatch(0, 1));
478 ASSERT_EQ(1U, GetAllProfiles(0).size());
480 PersonalDataManager
* pdm
= GetPersonalDataManager(1);
481 ASSERT_EQ(0U, pdm
->GetCreditCards().size());