summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search_engines/template_url_service_sync_unittest.cc
blob: b425a276681bed298243a47b27b303b7599f4086 (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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
// Copyright (c) 2012 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 "base/memory/scoped_ptr.h"
#include "base/string_util.h"
#include "base/time.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/sync/protocol/search_engine_specifics.pb.h"
#include "chrome/browser/sync/protocol/sync.pb.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_pref_service.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/notification_service.h"
#include "testing/gtest/include/gtest/gtest.h"

using base::Time;

namespace {

// Extract the GUID from a search engine SyncData.
std::string GetGUID(const SyncData& sync_data) {
  return sync_data.GetSpecifics().search_engine().sync_guid();
}

// Extract the URL from a search engine SyncData.
std::string GetURL(const SyncData& sync_data) {
  return sync_data.GetSpecifics().search_engine().url();
}

// Extract the keyword from a search engine SyncData.
std::string GetKeyword(const SyncData& sync_data) {
  return sync_data.GetSpecifics().search_engine().keyword();
}

// TODO(stevet): Share these with template_url_service_unittest.
// Set the managed preferences for the default search provider and trigger
// notification.
void SetManagedDefaultSearchPreferences(TemplateURLService* turl_service,
                                        TestingProfile* profile,
                                        bool enabled,
                                        const char* name,
                                        const char* search_url,
                                        const char* suggest_url,
                                        const char* icon_url,
                                        const char* encodings,
                                        const char* keyword) {
  TestingPrefService* pref_service = profile->GetTestingPrefService();
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderEnabled,
                               Value::CreateBooleanValue(enabled));
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderName,
                               Value::CreateStringValue(name));
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderSearchURL,
                               Value::CreateStringValue(search_url));
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderSuggestURL,
                               Value::CreateStringValue(suggest_url));
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderIconURL,
                               Value::CreateStringValue(icon_url));
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderEncodings,
                               Value::CreateStringValue(encodings));
  pref_service->SetManagedPref(prefs::kDefaultSearchProviderKeyword,
                               Value::CreateStringValue(keyword));
}

// Remove all the managed preferences for the default search provider and
// trigger notification.
void RemoveManagedDefaultSearchPreferences(TemplateURLService* turl_service,
                                           TestingProfile* profile) {
  TestingPrefService* pref_service = profile->GetTestingPrefService();
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSearchURL);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEnabled);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderName);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderSuggestURL);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderIconURL);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderEncodings);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderKeyword);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderID);
  pref_service->RemoveManagedPref(prefs::kDefaultSearchProviderPrepopulateID);
}

// Dummy SyncChangeProcessor used to help review what SyncChanges are pushed
// back up to Sync.
class TestChangeProcessor : public SyncChangeProcessor {
 public:
  TestChangeProcessor() : erroneous_(false) {
  }
  virtual ~TestChangeProcessor() { }

  // Store a copy of all the changes passed in so we can examine them later.
  virtual SyncError ProcessSyncChanges(
      const tracked_objects::Location& from_here,
      const SyncChangeList& change_list) {
    if (erroneous_)
      return SyncError(FROM_HERE, "Some error.", syncable::SEARCH_ENGINES);

    change_map_.erase(change_map_.begin(), change_map_.end());
    for (SyncChangeList::const_iterator iter = change_list.begin();
        iter != change_list.end(); ++iter) {
      change_map_[GetGUID(iter->sync_data())] = *iter;
    }

    return SyncError();
  }

  bool ContainsGUID(const std::string& guid) {
    return change_map_.find(guid) != change_map_.end();
  }

  SyncChange GetChangeByGUID(const std::string& guid) {
    DCHECK(ContainsGUID(guid));
    return change_map_[guid];
  }

  int change_list_size() { return change_map_.size(); }

  void set_erroneous(bool erroneous) { erroneous_ = erroneous; }

 private:
  // Track the changes received in ProcessSyncChanges.
  std::map<std::string, SyncChange> change_map_;
  bool erroneous_;

  DISALLOW_COPY_AND_ASSIGN(TestChangeProcessor);
};

class TemplateURLServiceSyncTest : public testing::Test {
 public:
  typedef TemplateURLService::SyncDataMap SyncDataMap;

  TemplateURLServiceSyncTest() {}

  virtual void SetUp() {
    profile_a_.reset(new TestingProfile);
    TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
        profile_a_.get());
    model_a_.reset(new TemplateURLService(profile_a_.get()));
    model_a_->Load();
    profile_b_.reset(new TestingProfile);
    TemplateURLServiceFactory::GetInstance()->RegisterUserPrefsOnProfile(
        profile_b_.get());
    model_b_.reset(new TemplateURLService(profile_b_.get()));
    model_b_->Load();
  }

  virtual void TearDown() { }

  TemplateURLService* model() { return model_a_.get(); }
  // For readability, we redefine an accessor for Model A for use in tests that
  // involve syncing two models.
  TemplateURLService* model_a() { return model_a_.get(); }
  TemplateURLService* model_b() { return model_b_.get(); }
  TestChangeProcessor* processor() { return &processor_; }

  // Create a TemplateURL with some test values. The caller owns the returned
  // TemplateURL*.
  TemplateURL* CreateTestTemplateURL() const {
    TemplateURL* turl = new TemplateURL();
    turl->SetURL("http://www.unittest.com/", 0, 0);
    turl->set_keyword(ASCIIToUTF16("unittest"));
    turl->set_short_name(ASCIIToUTF16("unittest"));
    turl->set_safe_for_autoreplace(true);
    GURL favicon_url("http://favicon.url");
    turl->SetFaviconURL(favicon_url);
    turl->set_date_created(Time::FromTimeT(100));
    turl->set_last_modified(Time::FromTimeT(100));
    turl->SetPrepopulateId(999999);
    return turl;
  }

  // Convenience helpers for creating TemplateURLs with specific fields.
  TemplateURL* CreateTestTemplateURL(std::string keyword,
                                     std::string url) const {
    TemplateURL* turl = CreateTestTemplateURL();
    turl->set_keyword(UTF8ToUTF16(keyword));
    turl->SetURL(url, 0, 0);
    return turl;
  }

  TemplateURL* CreateTestTemplateURL(std::string keyword,
                                     std::string url,
                                     std::string guid) const {
    TemplateURL* turl = CreateTestTemplateURL(keyword, url);
    if (!guid.empty())
      turl->set_sync_guid(guid);
    return turl;
  }

  TemplateURL* CreateTestTemplateURL(std::string keyword,
                                     std::string url,
                                     std::string guid,
                                     time_t last_mod) const {
    TemplateURL* turl = CreateTestTemplateURL(keyword, url, guid);
    turl->set_last_modified(Time::FromTimeT(last_mod));
    return turl;
  }

  // Verifies the two TemplateURLs are equal.
  // TODO(stevet): Share this with TemplateURLServiceTest.
  void AssertEquals(const TemplateURL& expected,
                    const TemplateURL& actual) const {
    ASSERT_TRUE(TemplateURLRef::SameUrlRefs(expected.url(), actual.url()));
    ASSERT_TRUE(TemplateURLRef::SameUrlRefs(expected.suggestions_url(),
                                            actual.suggestions_url()));
    ASSERT_EQ(expected.keyword(), actual.keyword());
    ASSERT_EQ(expected.short_name(), actual.short_name());
    ASSERT_EQ(JoinString(expected.input_encodings(), ';'),
              JoinString(actual.input_encodings(), ';'));
    ASSERT_TRUE(expected.GetFaviconURL() == actual.GetFaviconURL());
    ASSERT_EQ(expected.safe_for_autoreplace(), actual.safe_for_autoreplace());
    ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list());
    ASSERT_TRUE(expected.date_created() == actual.date_created());
    ASSERT_TRUE(expected.last_modified() == actual.last_modified());
  }

  // Expect that two SyncDataLists have equal contents, in terms of the
  // sync_guid, keyword, and url fields.
  void AssertEquals(const SyncDataList& data1,
                    const SyncDataList& data2) const {
    SyncDataMap map1 = TemplateURLService::CreateGUIDToSyncDataMap(data1);
    SyncDataMap map2 = TemplateURLService::CreateGUIDToSyncDataMap(data2);

    for (SyncDataMap::const_iterator iter1 = map1.begin();
        iter1 != map1.end(); iter1++) {
      SyncDataMap::iterator iter2 = map2.find(iter1->first);
      if (iter2 != map2.end()) {
        ASSERT_EQ(GetKeyword(iter1->second), GetKeyword(iter2->second));
        ASSERT_EQ(GetURL(iter1->second), GetURL(iter2->second));
        map2.erase(iter2);
      }
    }
    EXPECT_EQ(0U, map2.size());
  }

  // Convenience helper for creating SyncChanges. Takes ownership of |turl|.
  SyncChange CreateTestSyncChange(SyncChange::SyncChangeType type,
                                  TemplateURL* turl) const {
    // We take control of the TemplateURL so make sure it's cleaned up after
    // we create data out of it.
    scoped_ptr<TemplateURL> scoped_turl(turl);
    return SyncChange(
        type, TemplateURLService::CreateSyncDataFromTemplateURL(*scoped_turl));
  }

  // Helper that creates some initial sync data. We cheat a little by specifying
  // GUIDs for easy identification later. We also make the last_modified times
  // slightly older than CreateTestTemplateURL's default, to test conflict
  // resolution.
  SyncDataList CreateInitialSyncData() const {
    SyncDataList list;

    scoped_ptr<TemplateURL> turl(
        CreateTestTemplateURL("key1", "http://key1.com", "key1", 90));
    list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl));
    turl.reset(CreateTestTemplateURL("key2", "http://key2.com", "key2", 90));
    list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl));
    turl.reset(CreateTestTemplateURL("key3", "http://key3.com", "key3", 90));
    list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl));

    return list;
  }

 protected:
  // We keep two TemplateURLServices to test syncing between them.
  scoped_ptr<TestingProfile> profile_a_;
  scoped_ptr<TemplateURLService> model_a_;
  scoped_ptr<TestingProfile> profile_b_;
  scoped_ptr<TemplateURLService> model_b_;

  // Our dummy ChangeProcessor used to inspect changes pushed to Sync.
  TestChangeProcessor processor_;

  DISALLOW_COPY_AND_ASSIGN(TemplateURLServiceSyncTest);
};

}  // namespace

TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) {
  // Create a TemplateURL and convert it into a sync specific type.
  scoped_ptr<TemplateURL> turl(CreateTestTemplateURL());
  SyncData sync_data = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
  // Convert the specifics back to a TemplateURL.
  scoped_ptr<TemplateURL> deserialized(
      TemplateURLService::CreateTemplateURLFromSyncData(sync_data));
  EXPECT_TRUE(deserialized.get());
  // Ensure that the original and the deserialized TURLs are equal in values.
  AssertEquals(*turl, *deserialized);
}

TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataBasic) {
  model()->Add(CreateTestTemplateURL("key1", "http://key1.com"));
  model()->Add(CreateTestTemplateURL("key2", "http://key2.com"));
  model()->Add(CreateTestTemplateURL("key3", "http://key3.com"));
  SyncDataList all_sync_data =
      model()->GetAllSyncData(syncable::SEARCH_ENGINES);

  EXPECT_EQ(3U, all_sync_data.size());

  for (SyncDataList::const_iterator iter = all_sync_data.begin();
      iter != all_sync_data.end(); ++iter) {
    std::string guid = GetGUID(*iter);
    const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
    scoped_ptr<TemplateURL> deserialized(
        TemplateURLService::CreateTemplateURLFromSyncData(*iter));
    AssertEquals(*service_turl, *deserialized);
  }
}

TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoExtensions) {
  model()->Add(CreateTestTemplateURL("key1", "http://key1.com"));
  model()->Add(CreateTestTemplateURL("key2", "http://key2.com"));
  model()->Add(CreateTestTemplateURL(
      "key3", "chrome-extension://blahblahblah"));
  SyncDataList all_sync_data =
      model()->GetAllSyncData(syncable::SEARCH_ENGINES);

  EXPECT_EQ(2U, all_sync_data.size());

  for (SyncDataList::const_iterator iter = all_sync_data.begin();
      iter != all_sync_data.end(); ++iter) {
    std::string guid = GetGUID(*iter);
    const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
    scoped_ptr<TemplateURL> deserialized(
        TemplateURLService::CreateTemplateURLFromSyncData(*iter));
    AssertEquals(*service_turl, *deserialized);
  }
}

TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) {
  model()->Add(CreateTestTemplateURL("key1", "http://key1.com"));
  model()->Add(CreateTestTemplateURL("key2", "http://key2.com"));
  TemplateURL* managed_turl = CreateTestTemplateURL(
      "key3", "http://key3.com");
  managed_turl->set_created_by_policy(true);
  model()->Add(managed_turl);
  SyncDataList all_sync_data =
      model()->GetAllSyncData(syncable::SEARCH_ENGINES);

  EXPECT_EQ(2U, all_sync_data.size());

  for (SyncDataList::const_iterator iter = all_sync_data.begin();
      iter != all_sync_data.end(); ++iter) {
    std::string guid = GetGUID(*iter);
    const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
    scoped_ptr<TemplateURL> deserialized(
        TemplateURLService::CreateTemplateURLFromSyncData(*iter));
    ASSERT_FALSE(service_turl->created_by_policy());
    AssertEquals(*service_turl, *deserialized);
  }
}

TEST_F(TemplateURLServiceSyncTest, UniquifyKeyword) {
  model()->Add(CreateTestTemplateURL("key1", "http://key1.com"));
  // Create a key that conflicts with something in the model.
  scoped_ptr<TemplateURL> turl(CreateTestTemplateURL("key1",
                                                     "http://new.com",
                                                     "xyz"));
  string16 new_keyword = model()->UniquifyKeyword(*turl);

  EXPECT_EQ(UTF8ToUTF16("new.com"), new_keyword);
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));

  turl->set_keyword(new_keyword);
  model()->Add(turl.release());
  // Test a second collision. This time it should be resolved by actually
  // modifying the original keyword, since the autogenerated keyword is already
  // used.
  turl.reset(CreateTestTemplateURL("key1", "http://new.com"));
  new_keyword = model()->UniquifyKeyword(*turl);

  EXPECT_EQ(UTF8ToUTF16("key1_"), new_keyword);
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));

  turl->set_keyword(new_keyword);
  model()->Add(turl.release());
  // Test a third collision. This should collide on both the autogenerated
  // keyword and the first uniquification attempt.
  turl.reset(CreateTestTemplateURL("key1", "http://new.com"));
  new_keyword = model()->UniquifyKeyword(*turl);

  EXPECT_EQ(UTF8ToUTF16("key1__"), new_keyword);
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));
}

TEST_F(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict) {
  TemplateURL* original_turl =
      CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9000);
  string16 original_turl_keyword = original_turl->keyword();
  model()->Add(original_turl);

  // Create a key that does not conflict with something in the model.
  scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL("unique",
                                                          "http://new.com"));
  string16 sync_keyword = sync_turl->keyword();
  SyncChangeList changes;

  // No conflict, no TURLs changed, no changes.
  EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
  EXPECT_EQ(original_turl_keyword, original_turl->keyword());
  EXPECT_EQ(sync_keyword, sync_turl->keyword());
  EXPECT_EQ(0U, changes.size());

  // Change sync keyword to something that conflicts, and make it older.
  // Conflict, sync keyword is uniquified, and a SyncChange is added.
  sync_turl->set_keyword(original_turl->keyword());
  sync_turl->set_last_modified(Time::FromTimeT(8999));
  EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
  EXPECT_NE(sync_keyword, sync_turl->keyword());
  EXPECT_EQ(original_turl_keyword, original_turl->keyword());
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
  EXPECT_EQ(1U, changes.size());
  changes.clear();

  // Sync is newer. Original TemplateURL keyword is uniquified, no SyncChange
  // is added. Also ensure that this does not change the safe_for_autoreplace
  // flag or the TemplateURLID in the original.
  EXPECT_TRUE(original_turl->safe_for_autoreplace());
  original_turl->set_id(1337);
  sync_turl->set_id(1);
  sync_turl->set_keyword(original_turl->keyword());
  sync_keyword = sync_turl->keyword();
  sync_turl->set_last_modified(Time::FromTimeT(9001));
  EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
  EXPECT_EQ(sync_keyword, sync_turl->keyword());
  EXPECT_NE(original_turl_keyword, original_turl->keyword());
  EXPECT_TRUE(original_turl->safe_for_autoreplace());
  EXPECT_EQ(1337, original_turl->id());
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
  EXPECT_EQ(0U, changes.size());

  // Equal times. Same result as above. Sync left alone, original uniquified so
  // sync_turl can fit.
  sync_turl->set_keyword(original_turl->keyword());
  sync_keyword = sync_turl->keyword();
  // Note that we have to reset original_turl's last_modified time as it was
  // modified above.
  original_turl->set_last_modified(Time::FromTimeT(9000));
  sync_turl->set_last_modified(Time::FromTimeT(9000));
  EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
  EXPECT_EQ(sync_keyword, sync_turl->keyword());
  EXPECT_NE(original_turl_keyword, original_turl->keyword());
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
  EXPECT_EQ(0U, changes.size());

  // Sync is newer, but original TemplateURL is created by policy, so it wins.
  // Sync keyword is uniquified, and a SyncChange is added.
  original_turl_keyword = original_turl->keyword();
  sync_turl->set_keyword(original_turl->keyword());
  sync_turl->set_last_modified(Time::FromTimeT(9999));
  original_turl->set_created_by_policy(true);
  EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
  EXPECT_NE(sync_keyword, sync_turl->keyword());
  EXPECT_EQ(original_turl_keyword, original_turl->keyword());
  EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
  EXPECT_EQ(1U, changes.size());
  changes.clear();
}

TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) {
  TemplateURL* original_turl =
      CreateTestTemplateURL("key1", "http://key1.com");
  model()->Add(original_turl);

  // No matches at all.
  scoped_ptr<TemplateURL> sync_turl(
      CreateTestTemplateURL("key2", "http://key2.com"));
  EXPECT_EQ(NULL, model()->FindDuplicateOfSyncTemplateURL(*sync_turl));

  // URL matches, but not keyword. No dupe.
  sync_turl->SetURL("http://key1.com", 0 , 0);
  EXPECT_EQ(NULL, model()->FindDuplicateOfSyncTemplateURL(*sync_turl));

  // Keyword matches, but not URL. No dupe.
  sync_turl->SetURL("http://key2.com", 0 , 0);
  sync_turl->set_keyword(UTF8ToUTF16("key1"));
  EXPECT_EQ(NULL, model()->FindDuplicateOfSyncTemplateURL(*sync_turl));

  // Duplicate.
  sync_turl->SetURL("http://key1.com", 0 , 0);
  const TemplateURL* dupe_turl =
      model()->FindDuplicateOfSyncTemplateURL(*sync_turl);
  ASSERT_TRUE(dupe_turl);
  EXPECT_EQ(dupe_turl->keyword(), sync_turl->keyword());
  EXPECT_EQ(dupe_turl->url()->url(), sync_turl->url()->url());
}

TEST_F(TemplateURLServiceSyncTest, MergeSyncAndLocalURLDuplicates) {
  TemplateURL* original_turl =
      CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9000);
  model()->Add(original_turl);
  TemplateURL* sync_turl =
      CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9001);
  SyncChangeList changes;

  // The sync TemplateURL is newer. It should replace the original TemplateURL.
  // Note that MergeSyncAndLocalURLDuplicates takes ownership of sync_turl.
  model()->MergeSyncAndLocalURLDuplicates(sync_turl, original_turl, &changes);
  const TemplateURL* result =
      model()->GetTemplateURLForKeyword(UTF8ToUTF16("key1"));
  ASSERT_TRUE(result);
  EXPECT_EQ(9001, result->last_modified().ToTimeT());
  EXPECT_EQ(0U, changes.size());

  // The sync TemplateURL is older. The existing TemplateURL should win and a
  // SyncChange should be added to the list.
  TemplateURL* sync_turl2 =
      CreateTestTemplateURL("key1", "http://key1.com", std::string(), 8999);
  model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes);
  result = model()->GetTemplateURLForKeyword(UTF8ToUTF16("key1"));
  ASSERT_TRUE(result);
  EXPECT_EQ(9001, result->last_modified().ToTimeT());
  EXPECT_EQ(1U, changes.size());
}

TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) {
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      SyncDataList(),  // Empty.
      processor());

  EXPECT_EQ(0U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  EXPECT_EQ(0, processor()->change_list_size());
}

TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) {
  SyncDataList initial_data = CreateInitialSyncData();

  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  // We expect the model to have accepted all of the initial sync data. Search
  // through the model using the GUIDs to ensure that they're present.
  for (SyncDataList::const_iterator iter = initial_data.begin();
      iter != initial_data.end(); ++iter) {
    std::string guid = GetGUID(*iter);
    EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
  }

  EXPECT_EQ(0, processor()->change_list_size());
}

TEST_F(TemplateURLServiceSyncTest, MergeInAllNewData) {
  model()->Add(CreateTestTemplateURL(
      "google.com", "http://google.com", "abc"));
  model()->Add(CreateTestTemplateURL("yahoo.com", "http://yahoo.com", "def"));
  model()->Add(CreateTestTemplateURL("bing.com", "http://bing.com", "xyz"));
  SyncDataList initial_data = CreateInitialSyncData();

  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  EXPECT_EQ(6U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  // We expect the model to have accepted all of the initial sync data. Search
  // through the model using the GUIDs to ensure that they're present.
  for (SyncDataList::const_iterator iter = initial_data.begin();
      iter != initial_data.end(); ++iter) {
    std::string guid = GetGUID(*iter);
    EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
  }
  // All the original TemplateURLs should also remain in the model.
  EXPECT_TRUE(model()->GetTemplateURLForKeyword(UTF8ToUTF16("google.com")));
  EXPECT_TRUE(model()->GetTemplateURLForKeyword(UTF8ToUTF16("yahoo.com")));
  EXPECT_TRUE(model()->GetTemplateURLForKeyword(UTF8ToUTF16("bing.com")));
  // Ensure that Sync received the expected changes.
  EXPECT_EQ(3, processor()->change_list_size());
  EXPECT_TRUE(processor()->ContainsGUID("abc"));
  EXPECT_TRUE(processor()->ContainsGUID("def"));
  EXPECT_TRUE(processor()->ContainsGUID("xyz"));
}

TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) {
  // The local data is the same as the sync data merged in. i.e. - There have
  // been no changes since the last time we synced. Even the last_modified
  // timestamps are the same.
  SyncDataList initial_data = CreateInitialSyncData();
  for (SyncDataList::const_iterator iter = initial_data.begin();
      iter != initial_data.end(); ++iter) {
    TemplateURL* converted =
        TemplateURLService::CreateTemplateURLFromSyncData(*iter);
    model()->Add(converted);
  }

  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  for (SyncDataList::const_iterator iter = initial_data.begin();
      iter != initial_data.end(); ++iter) {
    std::string guid = GetGUID(*iter);
    EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
  }
  EXPECT_EQ(0, processor()->change_list_size());
}

TEST_F(TemplateURLServiceSyncTest, MergeUpdateFromSync) {
  // The local data is the same as the sync data merged in, but timestamps have
  // changed. Ensure the right fields are merged in.
  SyncDataList initial_data;
  TemplateURL* turl1 = CreateTestTemplateURL(
      "google.com", "http://google.com", "abc", 9000);
  model()->Add(turl1);
  TemplateURL* turl2 = CreateTestTemplateURL(
      "bing.com", "http://bing.com", "xyz", 9000);
  model()->Add(turl2);

  TemplateURL turl1_newer(*turl1);
  turl1_newer.set_last_modified(Time::FromTimeT(9999));
  turl1_newer.SetURL("http://google.ca", 0, 0);
  initial_data.push_back(
      TemplateURLService::CreateSyncDataFromTemplateURL(turl1_newer));

  TemplateURL turl2_older(*turl2);
  turl2_older.set_last_modified(Time::FromTimeT(8888));
  turl2_older.SetURL("http://bing.ca", 0, 0);
  initial_data.push_back(
      TemplateURLService::CreateSyncDataFromTemplateURL(turl2_older));

  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  // Both were local updates, so we expect the same count.
  EXPECT_EQ(2U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());

  // Check that the first replaced the initial Google TemplateURL.
  EXPECT_EQ(turl1, model()->GetTemplateURLForGUID("abc"));
  EXPECT_EQ("http://google.ca", turl1->url()->url());

  // Check that the second produced an upstream update to the Bing TemplateURL.
  EXPECT_EQ(1, processor()->change_list_size());
  ASSERT_TRUE(processor()->ContainsGUID("xyz"));
  SyncChange change = processor()->GetChangeByGUID("xyz");
  EXPECT_TRUE(change.change_type() == SyncChange::ACTION_UPDATE);
  EXPECT_EQ("http://bing.com", GetURL(change.sync_data()));
}

TEST_F(TemplateURLServiceSyncTest, MergeAddFromOlderSyncData) {
  // GUIDs all differ, so this is data to be added from Sync, but the timestamps
  // from Sync are older. Set up the local data so that one is a dupe, one has a
  // conflicting keyword, and the last has no conflicts (a clean ADD).
  SyncDataList initial_data = CreateInitialSyncData();
  TemplateURL* turl = TemplateURLService::CreateTemplateURLFromSyncData(
      initial_data.at(0));
  ASSERT_TRUE(turl);
  turl->set_sync_guid("aaa");
  turl->set_last_modified(Time::FromTimeT(100));
  model()->Add(turl);  // dupe

  turl = TemplateURLService::CreateTemplateURLFromSyncData(
      initial_data.at(1));
  ASSERT_TRUE(turl);
  turl->SetURL("http://expected.com", 0, 0);
  turl->set_sync_guid("bbb");
  turl->set_last_modified(Time::FromTimeT(100));
  model()->Add(turl);  // keyword conflict

  model()->Add(CreateTestTemplateURL(
      "unique", "http://unique.com", "ccc"));  // add

  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  // The dupe results in a merge. The other two should be added to the model.
  EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());

  // The key1 duplicate results in the local copy winning. Ensure that Sync's
  // copy was not added, and the local copy is pushed upstream to Sync as an
  // update. The local copy should have received the sync data's GUID.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
  // Check changes for the UPDATE.
  ASSERT_TRUE(processor()->ContainsGUID("key1"));
  SyncChange key1_change = processor()->GetChangeByGUID("key1");
  EXPECT_EQ(SyncChange::ACTION_UPDATE, key1_change.change_type());
  EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa"));

  // The key2 keyword conflict results in the local copy winning, so ensure it
  // retains the original keyword, and that an update to the sync copy is pushed
  // upstream to Sync. Both TemplateURLs should be found locally, however.
  const TemplateURL* key2 = model()->GetTemplateURLForGUID("bbb");
  EXPECT_TRUE(key2);
  EXPECT_EQ(UTF8ToUTF16("key2"), key2->keyword());
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  // Check changes for the UPDATE.
  ASSERT_TRUE(processor()->ContainsGUID("key2"));
  SyncChange key2_change = processor()->GetChangeByGUID("key2");
  EXPECT_EQ(SyncChange::ACTION_UPDATE, key2_change.change_type());
  EXPECT_EQ("key2.com", GetKeyword(key2_change.sync_data()));

  // The last TemplateURL should have had no conflicts and was just added. It
  // should not have replaced the third local TemplateURL.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("ccc"));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));

  // Two UPDATEs and two ADDs.
  EXPECT_EQ(4, processor()->change_list_size());
  // Two ADDs should be pushed up to Sync.
  ASSERT_TRUE(processor()->ContainsGUID("bbb"));
  EXPECT_EQ(SyncChange::ACTION_ADD,
            processor()->GetChangeByGUID("bbb").change_type());
  ASSERT_TRUE(processor()->ContainsGUID("ccc"));
  EXPECT_EQ(SyncChange::ACTION_ADD,
            processor()->GetChangeByGUID("ccc").change_type());
}

TEST_F(TemplateURLServiceSyncTest, MergeAddFromNewerSyncData) {
  // GUIDs all differ, so this is data to be added from Sync, but the timestamps
  // from Sync are newer. Set up the local data so that one is a dupe, one has a
  // conflicting keyword, and the last has no conflicts (a clean ADD).
  SyncDataList initial_data = CreateInitialSyncData();
  TemplateURL* turl = TemplateURLService::CreateTemplateURLFromSyncData(
      initial_data.at(0));
  ASSERT_TRUE(turl);
  turl->set_sync_guid("aaa");
  turl->set_last_modified(Time::FromTimeT(10));
  model()->Add(turl);  // dupe

  turl = TemplateURLService::CreateTemplateURLFromSyncData(
      initial_data.at(1));
  ASSERT_TRUE(turl);
  turl->SetURL("http://expected.com", 0, 0);
  turl->set_sync_guid("bbb");
  turl->set_last_modified(Time::FromTimeT(10));
  model()->Add(turl);  // keyword conflict

  model()->Add(CreateTestTemplateURL(
      "unique", "http://unique.com", "ccc", 10));  // add

  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  // The dupe results in a merge. The other two should be added to the model.
  EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());

  // The key1 duplicate results in Sync's copy winning. Ensure that Sync's
  // copy replaced the local copy.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
  EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa"));

  // The key2 keyword conflict results in Sync's copy winning, so ensure it
  // retains the original keyword. The local copy should get a uniquified
  // keyword. Both TemplateURLs should be found locally.
  const TemplateURL* key2_sync = model()->GetTemplateURLForGUID("key2");
  EXPECT_TRUE(key2_sync);
  EXPECT_EQ(UTF8ToUTF16("key2"), key2_sync->keyword());
  const TemplateURL* key2_local = model()->GetTemplateURLForGUID("bbb");
  EXPECT_TRUE(key2_local);
  EXPECT_EQ(UTF8ToUTF16("expected.com"), key2_local->keyword());

  // The last TemplateURL should have had no conflicts and was just added. It
  // should not have replaced the third local TemplateURL.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("ccc"));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));

  // Two ADDs.
  EXPECT_EQ(2, processor()->change_list_size());
  // Two ADDs should be pushed up to Sync.
  ASSERT_TRUE(processor()->ContainsGUID("bbb"));
  EXPECT_EQ(SyncChange::ACTION_ADD,
            processor()->GetChangeByGUID("bbb").change_type());
  ASSERT_TRUE(processor()->ContainsGUID("ccc"));
  EXPECT_EQ(SyncChange::ACTION_ADD,
            processor()->GetChangeByGUID("ccc").change_type());
}

TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) {
  // We initially have no data.
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      SyncDataList(),
      processor());

  // Set up a bunch of ADDs.
  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("key1", "http://key1.com", "key1")));
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("key2", "http://key2.com", "key2")));
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("key3", "http://key3.com", "key3")));

  model()->ProcessSyncChanges(FROM_HERE, changes);

  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  EXPECT_EQ(0, processor()->change_list_size());
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
}

TEST_F(TemplateURLServiceSyncTest, ProcessChangesNoConflicts) {
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  // Process different types of changes, without conflicts.
  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("key4", "http://key4.com", "key4")));
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_UPDATE,
      CreateTestTemplateURL("newkeyword", "http://new.com", "key2")));
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_DELETE,
      CreateTestTemplateURL("key3", "http://key3.com", "key3")));

  model()->ProcessSyncChanges(FROM_HERE, changes);

  // Add one, remove one, update one, so the number shouldn't change.
  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  EXPECT_EQ(0, processor()->change_list_size());
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  const TemplateURL* turl = model()->GetTemplateURLForGUID("key2");
  EXPECT_TRUE(turl);
  EXPECT_EQ(UTF8ToUTF16("newkeyword"), turl->keyword());
  EXPECT_EQ("http://new.com", turl->url()->url());
  EXPECT_FALSE(model()->GetTemplateURLForGUID("key3"));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key4"));
}

TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsSyncWins) {
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  // Process different types of changes, with conflicts. Note that all this data
  // has a newer timestamp, so Sync will win in these scenarios.
  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("key2", "http://new.com", "aaa")));
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_UPDATE,
      CreateTestTemplateURL("key3", "http://key3.com", "key1")));

  model()->ProcessSyncChanges(FROM_HERE, changes);

  // Add one, update one, so we're up to 4.
  EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  // Sync is always newer here, so it should always win (i.e. - local changes,
  // nothing pushed to Sync).
  EXPECT_EQ(0, processor()->change_list_size());

  // aaa conflicts with key2 and wins, forcing key2's keyword to update.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("aaa"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("aaa"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key2")));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("key2"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key2.com")));
  // key1 update conflicts with key3 and wins, forcing key3's keyword to update.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("key1"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key3")));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("key3"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key3.com")));
}

TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsLocalWins) {
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  // Process different types of changes, with conflicts. Note that all this data
  // has an older timestamp, so the local data will win in these scenarios.
  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("key2", "http://new.com", "aaa", 10)));
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_UPDATE,
      CreateTestTemplateURL("key3", "http://key3.com", "key1", 10)));

  model()->ProcessSyncChanges(FROM_HERE, changes);

  // Add one, update one, so we're up to 4.
  EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  // Local data wins twice so two updates are pushed up to Sync.
  EXPECT_EQ(2, processor()->change_list_size());

  // aaa conflicts with key2 and loses, forcing it's keyword to update.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("aaa"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("aaa"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("new.com")));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("key2"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key2")));
  // key1 update conflicts with key3 and loses, forcing key1's keyword to
  // update.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("key1"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key3.com")));
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
  EXPECT_EQ(model()->GetTemplateURLForGUID("key3"),
            model()->GetTemplateURLForKeyword(UTF8ToUTF16("key3")));

  ASSERT_TRUE(processor()->ContainsGUID("aaa"));
  EXPECT_EQ(SyncChange::ACTION_UPDATE,
            processor()->GetChangeByGUID("aaa").change_type());
  ASSERT_TRUE(processor()->ContainsGUID("key1"));
  EXPECT_EQ(SyncChange::ACTION_UPDATE,
            processor()->GetChangeByGUID("key1").change_type());
}

TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) {
  // Ensure that ProcessTemplateURLChange is called and pushes the correct
  // changes to Sync whenever local changes are made to TemplateURLs.
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  // Add a new search engine.
  TemplateURL* new_turl =
      CreateTestTemplateURL("baidu", "http://baidu.cn", "new");
  model()->Add(new_turl);
  EXPECT_EQ(1, processor()->change_list_size());
  ASSERT_TRUE(processor()->ContainsGUID("new"));
  SyncChange change = processor()->GetChangeByGUID("new");
  EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type());
  EXPECT_EQ("baidu", GetKeyword(change.sync_data()));
  EXPECT_EQ("http://baidu.cn", GetURL(change.sync_data()));

  // Change a keyword.
  const TemplateURL* existing_turl = model()->GetTemplateURLForGUID("key1");
  model()->ResetTemplateURL(existing_turl,
                            existing_turl->short_name(),
                            UTF8ToUTF16("k"),
                            existing_turl->url()->url());
  EXPECT_EQ(1, processor()->change_list_size());
  ASSERT_TRUE(processor()->ContainsGUID("key1"));
  change = processor()->GetChangeByGUID("key1");
  EXPECT_EQ(SyncChange::ACTION_UPDATE, change.change_type());
  EXPECT_EQ("k", GetKeyword(change.sync_data()));

  // Remove an existing search engine.
  existing_turl = model()->GetTemplateURLForGUID("key2");
  model()->Remove(existing_turl);
  EXPECT_EQ(1, processor()->change_list_size());
  ASSERT_TRUE(processor()->ContainsGUID("key2"));
  change = processor()->GetChangeByGUID("key2");
  EXPECT_EQ(SyncChange::ACTION_DELETE, change.change_type());
}

TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsBasic) {
  // Start off B with some empty data.
  model_b()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  // Merge A and B. All of B's data should transfer over to A, which initially
  // has no data.
  model_a()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      model_b()->GetAllSyncData(syncable::SEARCH_ENGINES),
      model_b());

  // They should be consistent.
  AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES),
               model_b()->GetAllSyncData(syncable::SEARCH_ENGINES));
}

TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsDupesAndConflicts) {
  // Start off B with some empty data.
  model_b()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  // Set up A so we have some interesting duplicates and conflicts.
  model_a()->Add(CreateTestTemplateURL(
      "key4", "http://key4.com", "key4"));  // Added
  model_a()->Add(CreateTestTemplateURL(
      "key2", "http://key2.com", "key2"));  // Merge - Copy of key2.
  model_a()->Add(CreateTestTemplateURL(
      "key3", "http://key3.com", "key5", 10));  // Merge - Dupe of key3.
  model_a()->Add(CreateTestTemplateURL(
      "key1", "http://key6.com", "key6", 10));  // Keyword conflict with key1

  // Merge A and B.
  model_a()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      model_b()->GetAllSyncData(syncable::SEARCH_ENGINES),
      model_b());

  // They should be consistent.
  AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES),
               model_b()->GetAllSyncData(syncable::SEARCH_ENGINES));
}

TEST_F(TemplateURLServiceSyncTest, StopSyncing) {
  SyncError error = model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());
  ASSERT_FALSE(error.IsSet());
  model()->StopSyncing(syncable::SEARCH_ENGINES);

  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_UPDATE,
      CreateTestTemplateURL("newkeyword", "http://new.com", "key2")));
  error = model()->ProcessSyncChanges(FROM_HERE, changes);
  EXPECT_TRUE(error.IsSet());

  // Ensure that the sync changes were not accepted.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  EXPECT_FALSE(model()->GetTemplateURLForKeyword(UTF8ToUTF16("newkeyword")));
}

TEST_F(TemplateURLServiceSyncTest, SyncErrorOnInitialSync) {
  processor()->set_erroneous(true);
  SyncError error = model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());
  EXPECT_TRUE(error.IsSet());

  // Ensure that if the initial merge was erroneous, then subsequence attempts
  // to push data into the local model are rejected, since the model was never
  // successfully associated with Sync in the first place.
  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_UPDATE,
      CreateTestTemplateURL("newkeyword", "http://new.com", "key2")));
  processor()->set_erroneous(false);
  error = model()->ProcessSyncChanges(FROM_HERE, changes);
  EXPECT_TRUE(error.IsSet());

  // Ensure that the sync changes were not accepted.
  EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
  EXPECT_FALSE(model()->GetTemplateURLForKeyword(UTF8ToUTF16("newkeyword")));
}

TEST_F(TemplateURLServiceSyncTest, SyncErrorOnLaterSync) {
  // Ensure that if the SyncProcessor succeeds in the initial merge, but fails
  // in future ProcessSyncChanges, we still return an error.
  SyncError error = model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());
  ASSERT_FALSE(error.IsSet());

  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_UPDATE,
      CreateTestTemplateURL("newkeyword", "http://new.com", "key2")));
  processor()->set_erroneous(true);
  error = model()->ProcessSyncChanges(FROM_HERE, changes);
  EXPECT_TRUE(error.IsSet());
}

TEST_F(TemplateURLServiceSyncTest, MergeTwiceWithSameSyncData) {
  // Ensure that a second merge with the same data as the first does not
  // actually update the local data.
  SyncDataList initial_data;
  initial_data.push_back(CreateInitialSyncData().at(0));
  TemplateURL* turl = TemplateURLService::CreateTemplateURLFromSyncData(
      initial_data.at(0));
  ASSERT_TRUE(turl);
  turl->set_last_modified(Time::FromTimeT(10));  // earlier
  model()->Add(turl);

  SyncError error = model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());
  ASSERT_FALSE(error.IsSet());

  // We should have updated the original TemplateURL with Sync's version.
  // Keep a copy of it so we can compare it after we re-merge.
  ASSERT_TRUE(model()->GetTemplateURLForGUID("key1"));
  TemplateURL updated_turl(*model()->GetTemplateURLForGUID("key1"));
  EXPECT_EQ(Time::FromTimeT(90), updated_turl.last_modified());

  // Modify a single field of the initial data. This should not be updated in
  // the second merge, as the last_modified timestamp remains the same.
  scoped_ptr<TemplateURL> temp_turl(
      TemplateURLService::CreateTemplateURLFromSyncData(initial_data.at(0)));
  temp_turl->set_short_name(UTF8ToUTF16("SomethingDifferent"));
  initial_data.clear();
  initial_data.push_back(
      TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl));

  // Remerge the data again. This simulates shutting down and syncing again
  // at a different time, but the cloud data has not changed.
  model()->StopSyncing(syncable::SEARCH_ENGINES);
  error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
                                            initial_data,
                                            processor());
  ASSERT_FALSE(error.IsSet());

  // Check that the TemplateURL was not modified.
  const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1");
  ASSERT_TRUE(reupdated_turl);
  AssertEquals(updated_turl, *reupdated_turl);
}

TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) {
  SyncDataList initial_data = CreateInitialSyncData();
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());
  model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));

  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  const TemplateURL* default_search = model()->GetDefaultSearchProvider();
  ASSERT_TRUE(default_search);

  // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in
  // the model yet. Ensure that the default has not changed in any way.
  profile_a_->GetTestingPrefService()->SetString(
      prefs::kSyncedDefaultSearchProviderGUID,
      "newdefault");

  ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());

  // Bring in a random new search engine with a different GUID. Ensure that
  // it doesn't change the default.
  SyncChangeList changes1;
  changes1.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("random", "http://random.com", "random")));
  model()->ProcessSyncChanges(FROM_HERE, changes1);

  EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());

  // Finally, bring in the expected entry with the right GUID. Ensure that
  // the default has changed to the new search engine.
  SyncChangeList changes2;
  changes2.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("new", "http://new.com", "newdefault")));
  model()->ProcessSyncChanges(FROM_HERE, changes2);

  EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
  ASSERT_EQ("newdefault", model()->GetDefaultSearchProvider()->sync_guid());
}

TEST_F(TemplateURLServiceSyncTest, SyncedDefaultArrivesAfterStartup) {
  // Start with the default set to something in the model before we start
  // syncing.
  model()->Add(CreateTestTemplateURL(
      "what", "http://thewhat.com", "initdefault"));
  model()->SetDefaultSearchProvider(
      model()->GetTemplateURLForGUID("initdefault"));

  const TemplateURL* default_search = model()->GetDefaultSearchProvider();
  ASSERT_TRUE(default_search);

  // Set kSyncedDefaultSearchProviderGUID to something that is not yet in
  // the model but is expected in the initial sync. Ensure that this doesn't
  // change our default since we're not quite syncing yet.
  profile_a_->GetTestingPrefService()->SetString(
      prefs::kSyncedDefaultSearchProviderGUID,
      "key2");

  EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());

  // Now sync the initial data, which will include the search engine entry
  // destined to become the new default.
  SyncDataList initial_data = CreateInitialSyncData();
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  // Ensure that the new default has been set.
  EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
  ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid());
}

TEST_F(TemplateURLServiceSyncTest, SyncedDefaultAlreadySetOnStartup) {
  // Start with the default set to something in the model before we start
  // syncing.
  const char kGUID[] = "initdefault";
  model()->Add(CreateTestTemplateURL("what", "http://thewhat.com", kGUID));
  model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID(kGUID));

  const TemplateURL* default_search = model()->GetDefaultSearchProvider();
  ASSERT_TRUE(default_search);

  // Set kSyncedDefaultSearchProviderGUID to the current default.
  profile_a_->GetTestingPrefService()->SetString(
      prefs::kSyncedDefaultSearchProviderGUID,
      kGUID);

  EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());

  // Now sync the initial data.
  SyncDataList initial_data = CreateInitialSyncData();
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());

  // Ensure that the new entries were added and the default has not changed.
  EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
}

TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
  // First start off with a few entries and make sure we can set an unmanaged
  // default search provider.
  SyncDataList initial_data = CreateInitialSyncData();
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      initial_data,
      processor());
  model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));

  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  ASSERT_FALSE(model()->is_default_search_managed());
  ASSERT_TRUE(model()->GetDefaultSearchProvider());

  // Change the default search provider to a managed one.
  const char kName[] = "manageddefault";
  const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}";
  const char kIconURL[] = "http://manageddefault.com/icon.jpg";
  const char kEncodings[] = "UTF-16;UTF-32";
  SetManagedDefaultSearchPreferences(model(), profile_a_.get(), true, kName,
                                     kSearchURL, "", kIconURL, kEncodings, "");
  const TemplateURL* dsp_turl = model()->GetDefaultSearchProvider();

  EXPECT_TRUE(model()->is_default_search_managed());

  // Add a new entry from Sync. It should still sync in despite the default
  // being managed.
  SyncChangeList changes;
  changes.push_back(CreateTestSyncChange(
      SyncChange::ACTION_ADD,
      CreateTestTemplateURL("newkeyword", "http://new.com", "newdefault")));
  model()->ProcessSyncChanges(FROM_HERE, changes);

  EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());

  // Change kSyncedDefaultSearchProviderGUID to point to the new entry and
  // ensure that the DSP remains managed.
  profile_a_->GetTestingPrefService()->SetString(
      prefs::kSyncedDefaultSearchProviderGUID,
      "newdefault");

  EXPECT_EQ(dsp_turl, model()->GetDefaultSearchProvider());
  EXPECT_TRUE(model()->is_default_search_managed());

  // Go unmanaged. Ensure that the DSP changes to the expected pending entry
  // from Sync.
  const TemplateURL* expected_default =
      model()->GetTemplateURLForGUID("newdefault");
  RemoveManagedDefaultSearchPreferences(model(), profile_a_.get());

  EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
}

TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) {
  // If the value from Sync is a duplicate of the local default and is newer, it
  // should safely replace the local value and set as the new default.
  TemplateURL* default_turl =
      CreateTestTemplateURL("key1", "http://key1.com", "whateverguid", 10);
  model()->Add(default_turl);
  model()->SetDefaultSearchProvider(default_turl);

  // The key1 entry should be a duplicate of the default.
  model()->MergeDataAndStartSyncing(
      syncable::SEARCH_ENGINES,
      CreateInitialSyncData(),
      processor());

  EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
  EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid"));
  EXPECT_EQ(model()->GetDefaultSearchProvider(),
            model()->GetTemplateURLForGUID("key1"));
}