summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/webdata/autofill_profile_sync_difference_tracker_unittest.cc
blob: 779ad929f533e58b4cfb7bd0eb11134b1a202a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/webdata/autofill_profile_sync_difference_tracker.h"

#include "base/bind_helpers.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_task_environment.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_profile_sync_util.h"
#include "components/autofill/core/browser/country_names.h"
#include "components/autofill/core/browser/test_autofill_clock.h"
#include "components/autofill/core/browser/webdata/autofill_table.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/sync/model/model_error.h"
#include "components/webdata/common/web_database.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace autofill {
namespace {

using base::ASCIIToUTF16;
using base::MockCallback;
using testing::ElementsAre;
using testing::IsEmpty;

// Some guids for testing.
const char kGuidA[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44A";
const char kGuidB[] = "EDC609ED-7EEE-4F27-B00C-423242A9C44B";
const char kHttpOrigin[] = "http://www.example.com/";
const char kHttpsOrigin[] = "https://www.example.com/";
const char kLocaleString[] = "en-US";
const base::Time kJune2017 = base::Time::FromDoubleT(1497552271);

}  // namespace

class AutofillProfileSyncDifferenceTrackerTestBase : public testing::Test {
 public:
  AutofillProfileSyncDifferenceTrackerTestBase() {}
  ~AutofillProfileSyncDifferenceTrackerTestBase() override {}

  void SetUp() override {
    // Fix a time for implicitly constructed use_dates in AutofillProfile.
    test_clock_.SetNow(kJune2017);

    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
    db_.AddTable(&table_);
    db_.Init(temp_dir_.GetPath().AppendASCII("SyncTestWebDatabase"));
  }

  void AddAutofillProfilesToTable(
      const std::vector<AutofillProfile>& profile_list) {
    for (const auto& profile : profile_list) {
      table_.AddAutofillProfile(profile);
    }
  }

  void IncorporateRemoteProfile(const AutofillProfile& profile) {
    EXPECT_EQ(base::nullopt, tracker()->IncorporateRemoteProfile(
                                 std::make_unique<AutofillProfile>(profile)));
  }

  std::vector<AutofillProfile> FlushAndReturnProfilesToUploadToSync() {
    EXPECT_EQ(base::nullopt,
              tracker()->FlushToLocal(
                  /*autofill_changes_callback=*/base::DoNothing()));

    std::vector<std::unique_ptr<AutofillProfile>> vector_of_unique_ptrs;
    EXPECT_EQ(base::nullopt,
              tracker()->FlushToSync(
                  /*profiles_to_upload_to_sync=*/&vector_of_unique_ptrs));

    // Copy all the elements by value so that we have a vector that is easier to
    // work with in the test.
    std::vector<AutofillProfile> vector_of_values;
    for (const std::unique_ptr<AutofillProfile>& entry :
         vector_of_unique_ptrs) {
      vector_of_values.push_back(*entry);
    }
    return vector_of_values;
  }

  std::vector<AutofillProfile> GetAllLocalData() {
    std::vector<std::unique_ptr<AutofillProfile>> vector_of_unique_ptrs;
    // Meant as an assertion but I cannot use ASSERT_TRUE in non-void function.
    EXPECT_TRUE(table()->GetAutofillProfiles(&vector_of_unique_ptrs));

    // Copy all the elements by value so that we have a vector that is easier to
    // work with in the test.
    std::vector<AutofillProfile> local_data;
    for (const std::unique_ptr<AutofillProfile>& entry :
         vector_of_unique_ptrs) {
      local_data.push_back(*entry);
    }
    return local_data;
  }

  virtual AutofillProfileSyncDifferenceTracker* tracker() = 0;

  AutofillTable* table() { return &table_; }

 private:
  autofill::TestAutofillClock test_clock_;
  base::ScopedTempDir temp_dir_;
  base::test::ScopedTaskEnvironment scoped_task_environment_;
  AutofillTable table_;
  WebDatabase db_;

  DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncDifferenceTrackerTestBase);
};

class AutofillProfileSyncDifferenceTrackerTest
    : public AutofillProfileSyncDifferenceTrackerTestBase {
 public:
  AutofillProfileSyncDifferenceTrackerTest() : tracker_(table()) {}
  ~AutofillProfileSyncDifferenceTrackerTest() override {}

  AutofillProfileSyncDifferenceTracker* tracker() override { return &tracker_; }

 private:
  AutofillProfileSyncDifferenceTracker tracker_;

  DISALLOW_COPY_AND_ASSIGN(AutofillProfileSyncDifferenceTrackerTest);
};

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       IncorporateRemoteProfileShouldOverwriteProfileWithSameKey) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  AddAutofillProfilesToTable({local});

  // The remote profile is completely different but it has the same key.
  AutofillProfile remote = AutofillProfile(kGuidA, kHttpsOrigin);
  remote.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Tom"));

  IncorporateRemoteProfile(remote);

  // Nothing gets uploaded to sync and the remote profile wins.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), IsEmpty());
  EXPECT_THAT(GetAllLocalData(), ElementsAre(remote));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       IncorporateRemoteProfileShouldOverwriteUnverifiedProfileByVerified) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpsOrigin);
  local.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  AddAutofillProfilesToTable({local});

  // The remote profile has the same key but it is not verified.
  AutofillProfile remote = AutofillProfile(kGuidA, kSettingsOrigin);
  remote.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Tom"));

  IncorporateRemoteProfile(remote);

  // Nothing gets uploaded to sync and the local profile wins.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), IsEmpty());
  EXPECT_THAT(GetAllLocalData(), ElementsAre(remote));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       IncorporateRemoteProfileShouldNotOverwriteVerifiedProfileByUnverified) {
  AutofillProfile local = AutofillProfile(kGuidA, kSettingsOrigin);
  local.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  AddAutofillProfilesToTable({local});

  // The remote profile has the same key but it is not verified.
  AutofillProfile remote = AutofillProfile(kGuidA, kHttpsOrigin);
  remote.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Tom"));

  IncorporateRemoteProfile(remote);

  // Nothing gets uploaded to sync and the local profile wins.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), IsEmpty());
  EXPECT_THAT(GetAllLocalData(), ElementsAre(local));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       IncorporateRemoteProfileShouldNotOverwriteFullNameByEmptyString) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(NAME_FULL, ASCIIToUTF16("John"));
  AddAutofillProfilesToTable({local});

  // The remote profile has the same key.
  AutofillProfile remote = AutofillProfile(kGuidA, kHttpsOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("2 2st st"));

  AutofillProfile merged(remote);
  merged.SetRawInfo(NAME_FULL, ASCIIToUTF16("John"));

  IncorporateRemoteProfile(remote);

  // Nothing gets uploaded to sync and the remote profile wins except for the
  // full name.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), IsEmpty());
  EXPECT_THAT(GetAllLocalData(), ElementsAre(merged));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       IncorporateRemoteProfileShouldMergeIdenticalProfilesWithDifferentKeys) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  AddAutofillProfilesToTable({local});

  // The remote profile is identical to the local one, except that the guids and
  // origins are different.
  AutofillProfile remote = AutofillProfile(kGuidB, kHttpsOrigin);
  remote.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));

  IncorporateRemoteProfile(remote);

  // Nothing gets uploaded to sync and the remote profile wins.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), IsEmpty());
  EXPECT_THAT(GetAllLocalData(), ElementsAre(remote));
}

TEST_F(
    AutofillProfileSyncDifferenceTrackerTest,
    IncorporateRemoteProfileShouldMergeIdenticalProfilesWithDifferentKeysButKeepVerifiedOrigin) {
  AutofillProfile local = AutofillProfile(kGuidA, kSettingsOrigin);
  local.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  AddAutofillProfilesToTable({local});

  // The remote profile has the same key.
  AutofillProfile remote = AutofillProfile(kGuidB, kHttpsOrigin);
  remote.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));

  AutofillProfile merged(remote);
  merged.set_origin(kSettingsOrigin);

  IncorporateRemoteProfile(remote);

  // Nothing gets uploaded to sync and the remote profile wins except for the
  // full name.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), ElementsAre(merged));
  EXPECT_THAT(GetAllLocalData(), ElementsAre(merged));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       FlushToLocalShouldNotCallbackWhenNotNeeded) {
  MockCallback<base::OnceClosure> autofill_changes_callback;

  EXPECT_CALL(autofill_changes_callback, Run()).Times(0);
  EXPECT_EQ(base::nullopt,
            tracker()->FlushToLocal(autofill_changes_callback.Get()));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       FlushToLocalShouldCallbackWhenProfileDeleted) {
  AutofillProfile local = AutofillProfile(kGuidA, kSettingsOrigin);
  AddAutofillProfilesToTable({local});

  tracker()->IncorporateRemoteDelete(kGuidA);

  MockCallback<base::OnceClosure> autofill_changes_callback;
  EXPECT_CALL(autofill_changes_callback, Run()).Times(1);
  EXPECT_EQ(base::nullopt,
            tracker()->FlushToLocal(autofill_changes_callback.Get()));

  // On top of that, the profile should also get deleted.
  EXPECT_THAT(GetAllLocalData(), IsEmpty());
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       FlushToLocalShouldCallbackWhenProfileAdded) {
  AutofillProfile remote = AutofillProfile(kGuidA, kSettingsOrigin);
  IncorporateRemoteProfile(remote);

  MockCallback<base::OnceClosure> autofill_changes_callback;
  EXPECT_CALL(autofill_changes_callback, Run()).Times(1);
  EXPECT_EQ(base::nullopt,
            tracker()->FlushToLocal(autofill_changes_callback.Get()));

  // On top of that, the profile should also get added.
  EXPECT_THAT(GetAllLocalData(), ElementsAre(remote));
}

TEST_F(AutofillProfileSyncDifferenceTrackerTest,
       FlushToLocalShouldCallbackWhenProfileUpdated) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpsOrigin);
  AddAutofillProfilesToTable({local});

  AutofillProfile remote = AutofillProfile(kGuidA, kHttpsOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  IncorporateRemoteProfile(remote);

  MockCallback<base::OnceClosure> autofill_changes_callback;
  EXPECT_CALL(autofill_changes_callback, Run()).Times(1);
  EXPECT_EQ(base::nullopt,
            tracker()->FlushToLocal(autofill_changes_callback.Get()));

  // On top of that, the profile with key kGuidA should also get updated.
  EXPECT_THAT(GetAllLocalData(), ElementsAre(remote));
}

class AutofillProfileInitialSyncDifferenceTrackerTest
    : public AutofillProfileSyncDifferenceTrackerTestBase {
 public:
  AutofillProfileInitialSyncDifferenceTrackerTest()
      : initial_tracker_(table()) {}
  ~AutofillProfileInitialSyncDifferenceTrackerTest() override {}

  void MergeSimilarEntriesForInitialSync() {
    initial_tracker_.MergeSimilarEntriesForInitialSync(kLocaleString);
  }

  AutofillProfileSyncDifferenceTracker* tracker() override {
    return &initial_tracker_;
  }

 private:
  AutofillProfileInitialSyncDifferenceTracker initial_tracker_;

  DISALLOW_COPY_AND_ASSIGN(AutofillProfileInitialSyncDifferenceTrackerTest);
};

TEST_F(AutofillProfileInitialSyncDifferenceTrackerTest,
       MergeSimilarEntriesForInitialSyncShouldSyncUpChanges) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  local.set_use_count(27);
  AddAutofillProfilesToTable({local});

  // The remote profile matches the local one (except for origin and use count).
  AutofillProfile remote = AutofillProfile(kGuidB, kHttpsOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  remote.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Frobbers, Inc."));
  remote.set_use_count(13);

  // The remote profile wins (as regards the storage key).
  AutofillProfile merged(remote);
  // The local origin wins when merging.
  merged.set_origin(kHttpOrigin);
  // Merging two profile takes their max use count.
  merged.set_use_count(27);

  IncorporateRemoteProfile(remote);
  MergeSimilarEntriesForInitialSync();

  // The merged profile needs to get uploaded back to sync and stored locally.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), ElementsAre(merged));
  EXPECT_THAT(GetAllLocalData(), ElementsAre(merged));
}

TEST_F(AutofillProfileInitialSyncDifferenceTrackerTest,
       MergeSimilarEntriesForInitialSyncShouldNotSyncUpWhenNotNeeded) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  local.set_use_count(13);
  AddAutofillProfilesToTable({local});

  // The remote profile matches the local one and has some additional data.
  AutofillProfile remote = AutofillProfile(kGuidB, kHttpOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  remote.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Frobbers, Inc."));
  // Merging two profile takes their max use count, so use count of 27 is taken.
  remote.set_use_count(27);

  IncorporateRemoteProfile(remote);
  MergeSimilarEntriesForInitialSync();

  // Nothing gets uploaded to sync and the remote profile wins.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), IsEmpty());
  EXPECT_THAT(GetAllLocalData(), ElementsAre(remote));
}

TEST_F(AutofillProfileInitialSyncDifferenceTrackerTest,
       MergeSimilarEntriesForInitialSyncNotMatchNonsimilarEntries) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  local.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Frobbers, Inc."));
  AddAutofillProfilesToTable({local});

  // The remote profile has a different street address.
  AutofillProfile remote = AutofillProfile(kGuidB, kHttpOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("2 2st st"));
  remote.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Frobbers, Inc."));

  IncorporateRemoteProfile(remote);
  MergeSimilarEntriesForInitialSync();

  // The local profile gets uploaded (due to initial sync) and the remote
  // profile gets stored locally.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), ElementsAre(local));
  EXPECT_THAT(GetAllLocalData(), ElementsAre(local, remote));
}

TEST_F(AutofillProfileInitialSyncDifferenceTrackerTest,
       MergeSimilarEntriesForInitialSyncDoesNotMatchLocalVerifiedEntry) {
  // The local entry is verified, should not get merged.
  AutofillProfile local = AutofillProfile(kGuidA, kSettingsOrigin);
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  AddAutofillProfilesToTable({local});

  // The remote profile is similar to the local one.
  AutofillProfile remote = AutofillProfile(kGuidB, kHttpOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  remote.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Frobbers, Inc."));

  IncorporateRemoteProfile(remote);
  MergeSimilarEntriesForInitialSync();

  // The local profile gets uploaded (due to initial sync) and the remote
  // profile gets stored locally.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), ElementsAre(local));
  EXPECT_THAT(GetAllLocalData(), ElementsAre(local, remote));
}

TEST_F(AutofillProfileInitialSyncDifferenceTrackerTest,
       MergeSimilarEntriesForInitialSyncDoesNotMatchRemoteVerifiedEntry) {
  AutofillProfile local = AutofillProfile(kGuidA, kHttpOrigin);
  local.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  AddAutofillProfilesToTable({local});

  // The remote profile is similar to the local one but is verified and thus it
  // should not get merged.
  AutofillProfile remote = AutofillProfile(kGuidB, kSettingsOrigin);
  remote.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, ASCIIToUTF16("1 1st st"));
  remote.SetRawInfo(COMPANY_NAME, ASCIIToUTF16("Frobbers, Inc."));

  IncorporateRemoteProfile(remote);
  MergeSimilarEntriesForInitialSync();

  // The local profile gets uploaded (due to initial sync) and the remote
  // profile gets stored locally.
  EXPECT_THAT(FlushAndReturnProfilesToUploadToSync(), ElementsAre(local));
  EXPECT_THAT(GetAllLocalData(), ElementsAre(local, remote));
}

}  // namespace autofill