two_client_autofill_sync_test.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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.
4
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_integration_test_util.h"
10#include "chrome/browser/sync/test/integration/sync_test.h"
11#include "components/autofill/core/browser/autofill_profile.h"
12#include "components/autofill/core/browser/credit_card.h"
13#include "components/autofill/core/browser/personal_data_manager.h"
14#include "components/autofill/core/browser/webdata/autofill_entry.h"
15#include "components/autofill/core/browser/webdata/autofill_table.h"
16
17using autofill::AutofillKey;
18using autofill::AutofillTable;
19using autofill::AutofillProfile;
20using autofill::AutofillType;
21using autofill::CreditCard;
22using autofill::PersonalDataManager;
23using autofill_helper::AddKeys;
24using autofill_helper::AddProfile;
25using autofill_helper::AwaitKeysMatch;
26using autofill_helper::AwaitProfilesMatch;
27using autofill_helper::CreateAutofillProfile;
28using autofill_helper::GetAllKeys;
29using autofill_helper::GetAllProfiles;
30using autofill_helper::GetPersonalDataManager;
31using autofill_helper::KeysMatch;
32using autofill_helper::ProfilesMatch;
33using autofill_helper::PROFILE_FRASIER;
34using autofill_helper::PROFILE_HOMER;
35using autofill_helper::PROFILE_MARION;
36using autofill_helper::PROFILE_NULL;
37using autofill_helper::RemoveKey;
38using autofill_helper::RemoveProfile;
39using autofill_helper::SetCreditCards;
40using autofill_helper::UpdateProfile;
41using bookmarks_helper::AddFolder;
42using bookmarks_helper::AddURL;
43using bookmarks_helper::IndexedURL;
44using bookmarks_helper::IndexedURLTitle;
45using sync_integration_test_util::AwaitCommitActivityCompletion;
46
47class TwoClientAutofillSyncTest : public SyncTest {
48 public:
49  TwoClientAutofillSyncTest() : SyncTest(TWO_CLIENT) { count = 0; }
50  virtual ~TwoClientAutofillSyncTest() {}
51
52  virtual bool TestUsesSelfNotifications() OVERRIDE { return false; }
53
54  // We do this so as to make a change that will trigger the autofill to sync.
55  // By default autofill does not sync unless there is some other change.
56  void MakeABookmarkChange(int profile) {
57    ASSERT_TRUE(
58        AddURL(profile, IndexedURLTitle(count), GURL(IndexedURL(count))));
59    ++count;
60  }
61 private:
62  int count;
63  DISALLOW_COPY_AND_ASSIGN(TwoClientAutofillSyncTest);
64};
65
66IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, WebDataServiceSanity) {
67  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
68
69  // Client0 adds a key.
70  std::set<AutofillKey> keys;
71  keys.insert(AutofillKey("name0", "value0"));
72  AddKeys(0, keys);
73  MakeABookmarkChange(0);
74  ASSERT_TRUE(AwaitKeysMatch(0, 1));
75  ASSERT_EQ(1U, GetAllKeys(0).size());
76
77  // Client1 adds a key.
78  keys.clear();
79  keys.insert(AutofillKey("name1", "value1-0"));
80  AddKeys(1, keys);
81  MakeABookmarkChange(1);
82  ASSERT_TRUE(AwaitKeysMatch(0, 1));
83  ASSERT_EQ(2U, GetAllKeys(0).size());
84
85  // Client0 adds a key with the same name.
86  keys.clear();
87  keys.insert(AutofillKey("name1", "value1-1"));
88  AddKeys(0, keys);
89  MakeABookmarkChange(0);
90  ASSERT_TRUE(AwaitKeysMatch(0, 1));
91  ASSERT_EQ(3U, GetAllKeys(0).size());
92
93  // Client1 removes a key.
94  RemoveKey(1, AutofillKey("name1", "value1-0"));
95  MakeABookmarkChange(1);
96  ASSERT_TRUE(AwaitKeysMatch(0, 1));
97  ASSERT_EQ(2U, GetAllKeys(0).size());
98
99  // Client0 removes the rest.
100  RemoveKey(0, AutofillKey("name0", "value0"));
101  RemoveKey(0, AutofillKey("name1", "value1-1"));
102  MakeABookmarkChange(0);
103  ASSERT_TRUE(AwaitKeysMatch(0, 1));
104  ASSERT_EQ(0U, GetAllKeys(0).size());
105}
106
107// TCM ID - 3678296.
108IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, AddUnicodeProfile) {
109  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
110
111  std::set<AutofillKey> keys;
112  keys.insert(AutofillKey(base::WideToUTF16(L"Sigur R\u00F3s"),
113                          base::WideToUTF16(L"\u00C1g\u00E6tis byrjun")));
114  AddKeys(0, keys);
115  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
116  ASSERT_TRUE(AwaitKeysMatch(0, 1));
117}
118
119IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest,
120                       AddDuplicateNamesToSameProfile) {
121  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
122
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"));
127  AddKeys(0, keys);
128  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
129  ASSERT_TRUE(AwaitKeysMatch(0, 1));
130  ASSERT_EQ(2U, GetAllKeys(0).size());
131}
132
133IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest,
134                       AddDuplicateNamesToDifferentProfiles) {
135  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
136
137  std::set<AutofillKey> keys0;
138  keys0.insert(AutofillKey("name0", "value0-0"));
139  keys0.insert(AutofillKey("name1", "value1"));
140  AddKeys(0, keys0);
141
142  std::set<AutofillKey> keys1;
143  keys1.insert(AutofillKey("name0", "value0-1"));
144  keys1.insert(AutofillKey("name2", "value2"));
145  keys1.insert(AutofillKey("name3", "value3"));
146  AddKeys(1, keys1);
147
148  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
149  ASSERT_TRUE(AwaitKeysMatch(0, 1));
150  ASSERT_EQ(5U, GetAllKeys(0).size());
151}
152
153IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest,
154                       PersonalDataManagerSanity) {
155  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
156
157  // Client0 adds a profile.
158  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
159  MakeABookmarkChange(0);
160  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
161  ASSERT_EQ(1U, GetAllProfiles(0).size());
162
163  // Client1 adds a profile.
164  AddProfile(1, CreateAutofillProfile(PROFILE_MARION));
165  MakeABookmarkChange(1);
166  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
167  ASSERT_EQ(2U, GetAllProfiles(0).size());
168
169  // Client0 adds the same profile.
170  AddProfile(0, CreateAutofillProfile(PROFILE_MARION));
171  MakeABookmarkChange(0);
172  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
173  ASSERT_EQ(2U, GetAllProfiles(0).size());
174
175  // Client1 removes a profile.
176  RemoveProfile(1, GetAllProfiles(1)[0]->guid());
177  MakeABookmarkChange(1);
178  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
179  ASSERT_EQ(1U, GetAllProfiles(0).size());
180
181  // Client0 updates a profile.
182  UpdateProfile(0,
183                GetAllProfiles(0)[0]->guid(),
184                AutofillType(autofill::NAME_FIRST),
185                base::ASCIIToUTF16("Bart"));
186  MakeABookmarkChange(0);
187  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
188  ASSERT_EQ(1U, GetAllProfiles(0).size());
189
190  // Client1 removes remaining profile.
191  RemoveProfile(1, GetAllProfiles(1)[0]->guid());
192  MakeABookmarkChange(1);
193  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
194  ASSERT_EQ(0U, GetAllProfiles(0).size());
195}
196
197// TCM ID - 7261786.
198IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, AddDuplicateProfiles) {
199  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
200
201  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
202  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
203  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
204  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
205  ASSERT_EQ(1U, GetAllProfiles(0).size());
206}
207
208// TCM ID - 3636294.
209IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, SameProfileWithConflict) {
210  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
211
212  AutofillProfile profile0 = CreateAutofillProfile(PROFILE_HOMER);
213  AutofillProfile profile1 = CreateAutofillProfile(PROFILE_HOMER);
214  profile1.SetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER,
215                      base::ASCIIToUTF16("1234567890"));
216
217  AddProfile(0, profile0);
218  AddProfile(1, profile1);
219  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
220  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
221  ASSERT_EQ(1U, GetAllProfiles(0).size());
222}
223
224// TCM ID - 3626291.
225IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, AddEmptyProfile) {
226  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
227
228  AddProfile(0, CreateAutofillProfile(PROFILE_NULL));
229  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
230  ASSERT_EQ(0U, GetAllProfiles(0).size());
231}
232
233// TCM ID - 3616283.
234IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, AddProfile) {
235  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
236
237  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
238  MakeABookmarkChange(0);
239  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
240  ASSERT_EQ(1U, GetAllProfiles(0).size());
241}
242
243// TCM ID - 3632260.
244IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, AddMultipleProfiles) {
245  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
246
247  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
248  AddProfile(0, CreateAutofillProfile(PROFILE_MARION));
249  AddProfile(0, CreateAutofillProfile(PROFILE_FRASIER));
250  MakeABookmarkChange(0);
251  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
252  ASSERT_EQ(3U, GetAllProfiles(0).size());
253}
254
255// TCM ID - 3602257.
256IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, DeleteProfile) {
257  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
258
259  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
260  MakeABookmarkChange(0);
261  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
262  ASSERT_EQ(1U, GetAllProfiles(0).size());
263
264  RemoveProfile(1, GetAllProfiles(1)[0]->guid());
265  MakeABookmarkChange(1);
266  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
267  ASSERT_EQ(0U, GetAllProfiles(0).size());
268}
269
270// TCM ID - 3627300.
271IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, MergeProfiles) {
272  ASSERT_TRUE(SetupClients()) << "SetupClients() failed.";
273
274  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
275  AddProfile(1, CreateAutofillProfile(PROFILE_MARION));
276  AddProfile(1, CreateAutofillProfile(PROFILE_FRASIER));
277  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
278  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
279  ASSERT_EQ(3U, GetAllProfiles(0).size());
280}
281
282// TCM ID - 3665264.
283IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, UpdateFields) {
284  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
285
286  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
287  MakeABookmarkChange(0);
288  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
289  ASSERT_EQ(1U, GetAllProfiles(0).size());
290
291  UpdateProfile(0,
292                GetAllProfiles(0)[0]->guid(),
293                AutofillType(autofill::NAME_FIRST),
294                base::ASCIIToUTF16("Lisa"));
295  UpdateProfile(0,
296                GetAllProfiles(0)[0]->guid(),
297                AutofillType(autofill::EMAIL_ADDRESS),
298                base::ASCIIToUTF16("grrrl@TV.com"));
299  MakeABookmarkChange(0);
300  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
301  ASSERT_EQ(1U, GetAllProfiles(0).size());
302}
303
304// TCM ID - 3628299.
305IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, ConflictingFields) {
306  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
307
308  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
309  MakeABookmarkChange(0);
310  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
311  ASSERT_EQ(1U, GetAllProfiles(0).size());
312  UpdateProfile(0,
313                GetAllProfiles(0)[0]->guid(),
314                AutofillType(autofill::NAME_FIRST),
315                base::ASCIIToUTF16("Lisa"));
316  MakeABookmarkChange(0);
317  UpdateProfile(1,
318                GetAllProfiles(1)[0]->guid(),
319                AutofillType(autofill::NAME_FIRST),
320                base::ASCIIToUTF16("Bart"));
321  MakeABookmarkChange(1);
322  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
323  ASSERT_EQ(1U, GetAllProfiles(0).size());
324}
325
326// TCM ID - 3608295.
327IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, MaxLength) {
328  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
329
330  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
331  MakeABookmarkChange(0);
332  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
333  ASSERT_EQ(1U, GetAllProfiles(0).size());
334
335  base::string16 max_length_string(AutofillTable::kMaxDataLength, '.');
336  UpdateProfile(0,
337                GetAllProfiles(0)[0]->guid(),
338                AutofillType(autofill::NAME_FULL),
339                max_length_string);
340  UpdateProfile(0,
341                GetAllProfiles(0)[0]->guid(),
342                AutofillType(autofill::EMAIL_ADDRESS),
343                max_length_string);
344  UpdateProfile(0,
345                GetAllProfiles(0)[0]->guid(),
346                AutofillType(autofill::ADDRESS_HOME_LINE1),
347                max_length_string);
348
349  MakeABookmarkChange(0);
350  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
351}
352
353IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, ExceedsMaxLength) {
354  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
355
356  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
357  MakeABookmarkChange(0);
358  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
359  ASSERT_EQ(1U, GetAllProfiles(0).size());
360
361  base::string16 exceeds_max_length_string(
362      AutofillTable::kMaxDataLength + 1, '.');
363  UpdateProfile(0,
364                GetAllProfiles(0)[0]->guid(),
365                AutofillType(autofill::NAME_FIRST),
366                exceeds_max_length_string);
367  UpdateProfile(0,
368                GetAllProfiles(0)[0]->guid(),
369                AutofillType(autofill::NAME_LAST),
370                exceeds_max_length_string);
371  UpdateProfile(0,
372                GetAllProfiles(0)[0]->guid(),
373                AutofillType(autofill::EMAIL_ADDRESS),
374                exceeds_max_length_string);
375  UpdateProfile(0,
376                GetAllProfiles(0)[0]->guid(),
377                AutofillType(autofill::ADDRESS_HOME_LINE1),
378                exceeds_max_length_string);
379
380  MakeABookmarkChange(0);
381  ASSERT_TRUE(bookmarks_helper::AwaitAllModelsMatch());
382  EXPECT_FALSE(ProfilesMatch(0, 1));
383}
384
385// Test credit cards don't sync.
386IN_PROC_BROWSER_TEST_F(TwoClientAutofillSyncTest, NoCreditCardSync) {
387  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
388
389  AddProfile(0, CreateAutofillProfile(PROFILE_HOMER));
390
391  CreditCard card;
392  card.SetRawInfo(autofill::CREDIT_CARD_NUMBER,
393                  base::ASCIIToUTF16("6011111111111117"));
394  std::vector<CreditCard> credit_cards;
395  credit_cards.push_back(card);
396  SetCreditCards(0, &credit_cards);
397
398  MakeABookmarkChange(0);
399  ASSERT_TRUE(AwaitProfilesMatch(0, 1));
400  ASSERT_EQ(1U, GetAllProfiles(0).size());
401
402  PersonalDataManager* pdm = GetPersonalDataManager(1);
403  ASSERT_EQ(0U, pdm->GetCreditCards().size());
404}
405