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

Side by Side Diff: chrome/browser/search_engines/template_url_service_sync_unittest.cc

Issue 10381016: Remove the "autogenerate keyword" bit on TemplateURL. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/scoped_ptr.h" 5 #include "base/memory/scoped_ptr.h"
6 #include "base/string_util.h" 6 #include "base/string_util.h"
7 #include "base/time.h" 7 #include "base/time.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/search_engines/search_terms_data.h"
9 #include "chrome/browser/search_engines/template_url.h" 10 #include "chrome/browser/search_engines/template_url.h"
10 #include "chrome/browser/search_engines/template_url_service.h" 11 #include "chrome/browser/search_engines/template_url_service.h"
11 #include "chrome/browser/search_engines/template_url_service_factory.h" 12 #include "chrome/browser/search_engines/template_url_service_factory.h"
12 #include "chrome/browser/search_engines/template_url_service_test_util.h" 13 #include "chrome/browser/search_engines/template_url_service_test_util.h"
13 #include "chrome/browser/sync/api/sync_error_factory.h" 14 #include "chrome/browser/sync/api/sync_error_factory.h"
14 #include "chrome/browser/sync/api/sync_error_factory_mock.h" 15 #include "chrome/browser/sync/api/sync_error_factory_mock.h"
15 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
18 #include "chrome/common/url_constants.h"
17 #include "chrome/test/base/testing_pref_service.h" 19 #include "chrome/test/base/testing_pref_service.h"
18 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/browser/notification_service.h" 21 #include "content/public/browser/notification_service.h"
22 #include "net/base/net_util.h"
20 #include "sync/protocol/search_engine_specifics.pb.h" 23 #include "sync/protocol/search_engine_specifics.pb.h"
21 #include "sync/protocol/sync.pb.h" 24 #include "sync/protocol/sync.pb.h"
22 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
23 26
24 using base::Time; 27 using base::Time;
25 28
26 namespace { 29 namespace {
27 30
28 // Extract the GUID from a search engine SyncData. 31 // Extract the GUID from a search engine SyncData.
29 std::string GetGUID(const SyncData& sync_data) { 32 std::string GetGUID(const SyncData& sync_data) {
30 return sync_data.GetSpecifics().search_engine().sync_guid(); 33 return sync_data.GetSpecifics().search_engine().sync_guid();
31 } 34 }
32 35
33 // Extract the URL from a search engine SyncData. 36 // Extract the URL from a search engine SyncData.
34 std::string GetURL(const SyncData& sync_data) { 37 std::string GetURL(const SyncData& sync_data) {
35 return sync_data.GetSpecifics().search_engine().url(); 38 return sync_data.GetSpecifics().search_engine().url();
36 } 39 }
37 40
38 // Extract the keyword from a search engine SyncData. 41 // Extract the keyword from a search engine SyncData.
39 std::string GetKeyword(const SyncData& sync_data) { 42 std::string GetKeyword(const SyncData& sync_data) {
40 return sync_data.GetSpecifics().search_engine().keyword(); 43 return sync_data.GetSpecifics().search_engine().keyword();
41 } 44 }
42 45
43 // Much like TemplateURLService::CreateSyncDataFromTemplateURL(), but allows the 46 // Much like TemplateURLService::CreateSyncDataFromTemplateURL(), but allows the
44 // caller to override the URL or GUID fields with empty strings, in order to 47 // caller to override the keyword, URL, or GUID fields with empty strings, in
45 // create illegal data that should be DELETEd when we try to sync it to a 48 // order to create custom data that should be handled specially when synced to a
46 // client. 49 // client.
47 SyncData CreateBogusSyncData(const TemplateURL& turl, 50 SyncData CreateCustomSyncData(const TemplateURL& turl,
48 const std::string& url, 51 bool autogenerate_keyword,
49 const std::string& sync_guid) { 52 const std::string& url,
53 const std::string& sync_guid) {
50 sync_pb::EntitySpecifics specifics; 54 sync_pb::EntitySpecifics specifics;
51 sync_pb::SearchEngineSpecifics* se_specifics = 55 sync_pb::SearchEngineSpecifics* se_specifics =
52 specifics.mutable_search_engine(); 56 specifics.mutable_search_engine();
53 se_specifics->set_short_name(UTF16ToUTF8(turl.short_name())); 57 se_specifics->set_short_name(UTF16ToUTF8(turl.short_name()));
54 se_specifics->set_keyword(UTF16ToUTF8(turl.keyword())); 58 se_specifics->set_keyword(
59 autogenerate_keyword ? std::string() : UTF16ToUTF8(turl.keyword()));
55 se_specifics->set_favicon_url(turl.favicon_url().spec()); 60 se_specifics->set_favicon_url(turl.favicon_url().spec());
56 se_specifics->set_url(url); 61 se_specifics->set_url(url);
57 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace()); 62 se_specifics->set_safe_for_autoreplace(turl.safe_for_autoreplace());
58 se_specifics->set_originating_url(turl.originating_url().spec()); 63 se_specifics->set_originating_url(turl.originating_url().spec());
59 se_specifics->set_date_created(turl.date_created().ToInternalValue()); 64 se_specifics->set_date_created(turl.date_created().ToInternalValue());
60 se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';')); 65 se_specifics->set_input_encodings(JoinString(turl.input_encodings(), ';'));
61 se_specifics->set_show_in_default_list(turl.show_in_default_list()); 66 se_specifics->set_show_in_default_list(turl.show_in_default_list());
62 se_specifics->set_suggestions_url(turl.suggestions_url()); 67 se_specifics->set_suggestions_url(turl.suggestions_url());
63 se_specifics->set_prepopulate_id(turl.prepopulate_id()); 68 se_specifics->set_prepopulate_id(turl.prepopulate_id());
64 se_specifics->set_autogenerate_keyword(turl.autogenerate_keyword()); 69 se_specifics->set_autogenerate_keyword(autogenerate_keyword);
65 se_specifics->set_instant_url(turl.instant_url()); 70 se_specifics->set_instant_url(turl.instant_url());
66 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); 71 se_specifics->set_last_modified(turl.last_modified().ToInternalValue());
67 se_specifics->set_sync_guid(sync_guid); 72 se_specifics->set_sync_guid(sync_guid);
68 return SyncData::CreateLocalData(turl.sync_guid(), // Must be valid! 73 return SyncData::CreateLocalData(turl.sync_guid(), // Must be valid!
69 se_specifics->keyword(), specifics); 74 se_specifics->keyword(), specifics);
70 } 75 }
71 76
72 77
73 // TestChangeProcessor -------------------------------------------------------- 78 // TestChangeProcessor --------------------------------------------------------
74 79
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 TestChangeProcessor* processor() { return sync_processor_.get(); } 187 TestChangeProcessor* processor() { return sync_processor_.get(); }
183 scoped_ptr<SyncChangeProcessor> PassProcessor(); 188 scoped_ptr<SyncChangeProcessor> PassProcessor();
184 scoped_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory(); 189 scoped_ptr<SyncErrorFactory> CreateAndPassSyncErrorFactory();
185 190
186 // Create a TemplateURL with some test values. The caller owns the returned 191 // Create a TemplateURL with some test values. The caller owns the returned
187 // TemplateURL*. 192 // TemplateURL*.
188 TemplateURL* CreateTestTemplateURL(const string16& keyword, 193 TemplateURL* CreateTestTemplateURL(const string16& keyword,
189 const std::string& url, 194 const std::string& url,
190 const std::string& guid = std::string(), 195 const std::string& guid = std::string(),
191 time_t last_mod = 100, 196 time_t last_mod = 100,
192 bool created_by_policy = false) const; 197 bool created_by_policy = false,
198 bool safe_for_autoreplace = true) const;
193 199
194 // Verifies the two TemplateURLs are equal. 200 // Verifies the two TemplateURLs are equal.
195 // TODO(stevet): Share this with TemplateURLServiceTest. 201 // TODO(stevet): Share this with TemplateURLServiceTest.
196 void AssertEquals(const TemplateURL& expected, 202 void AssertEquals(const TemplateURL& expected,
197 const TemplateURL& actual) const; 203 const TemplateURL& actual) const;
198 204
199 // Expect that two SyncDataLists have equal contents, in terms of the 205 // Expect that two SyncDataLists have equal contents, in terms of the
200 // sync_guid, keyword, and url fields. 206 // sync_guid, keyword, and url fields.
201 void AssertEquals(const SyncDataList& data1, 207 void AssertEquals(const SyncDataList& data1,
202 const SyncDataList& data2) const; 208 const SyncDataList& data2) const;
203 209
204 // Convenience helper for creating SyncChanges. Takes ownership of |turl|. 210 // Convenience helper for creating SyncChanges. Takes ownership of |turl|.
205 SyncChange CreateTestSyncChange(SyncChange::SyncChangeType type, 211 SyncChange CreateTestSyncChange(SyncChange::SyncChangeType type,
206 TemplateURL* turl) const; 212 TemplateURL* turl) const;
207 213
208 // Helper that creates some initial sync data. We cheat a little by specifying 214 // Helper that creates some initial sync data. We cheat a little by specifying
209 // GUIDs for easy identification later. We also make the last_modified times 215 // GUIDs for easy identification later. We also make the last_modified times
210 // slightly older than CreateTestTemplateURL's default, to test conflict 216 // slightly older than CreateTestTemplateURL's default, to test conflict
211 // resolution. 217 // resolution.
212 SyncDataList CreateInitialSyncData() const; 218 SyncDataList CreateInitialSyncData() const;
213 219
214 // Syntactic sugar. 220 // Syntactic sugar.
215 TemplateURL* Deserialize(const SyncData& sync_data); 221 TemplateURL* Deserialize(const SyncData& sync_data);
216 222
223 // Some tests need to use TemplateURLs with hosts or keywords like
224 // "google.com". These can conflict with both the pref-based "initial default
225 // search provider" and the prepopulate data, so this function loads the
226 // prepopulate data and then updates any Google TemplateURL to have a
227 // different hostname and keyword. For caller convenience, it then returns
228 // the Google hostname to use for any future Google keywords that get created,
229 // based on what the default Google base URL value is.
230 std::string MoveInitialGoogleTemplateURLAside();
231
217 protected: 232 protected:
218 // We keep two TemplateURLServices to test syncing between them. 233 // We keep two TemplateURLServices to test syncing between them.
219 TemplateURLServiceTestUtil test_util_a_; 234 TemplateURLServiceTestUtil test_util_a_;
220 scoped_ptr<TestingProfile> profile_b_; 235 scoped_ptr<TestingProfile> profile_b_;
221 scoped_ptr<TemplateURLService> model_b_; 236 scoped_ptr<TemplateURLService> model_b_;
222 237
223 // Our dummy ChangeProcessor used to inspect changes pushed to Sync. 238 // Our dummy ChangeProcessor used to inspect changes pushed to Sync.
224 scoped_ptr<TestChangeProcessor> sync_processor_; 239 scoped_ptr<TestChangeProcessor> sync_processor_;
225 scoped_ptr<SyncChangeProcessorDelegate> sync_processor_delegate_; 240 scoped_ptr<SyncChangeProcessorDelegate> sync_processor_delegate_;
226 241
(...skipping 30 matching lines...) Expand all
257 scoped_ptr<SyncErrorFactory> TemplateURLServiceSyncTest:: 272 scoped_ptr<SyncErrorFactory> TemplateURLServiceSyncTest::
258 CreateAndPassSyncErrorFactory() { 273 CreateAndPassSyncErrorFactory() {
259 return scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock()); 274 return scoped_ptr<SyncErrorFactory>(new SyncErrorFactoryMock());
260 } 275 }
261 276
262 TemplateURL* TemplateURLServiceSyncTest::CreateTestTemplateURL( 277 TemplateURL* TemplateURLServiceSyncTest::CreateTestTemplateURL(
263 const string16& keyword, 278 const string16& keyword,
264 const std::string& url, 279 const std::string& url,
265 const std::string& guid, 280 const std::string& guid,
266 time_t last_mod, 281 time_t last_mod,
267 bool created_by_policy) const { 282 bool created_by_policy,
283 bool safe_for_autoreplace) const {
268 TemplateURLData data; 284 TemplateURLData data;
269 data.short_name = ASCIIToUTF16("unittest"); 285 data.short_name = ASCIIToUTF16("unittest");
270 data.SetKeyword(keyword); 286 data.SetKeyword(keyword);
271 data.SetURL(url); 287 data.SetURL(url);
272 data.favicon_url = GURL("http://favicon.url"); 288 data.favicon_url = GURL("http://favicon.url");
273 data.safe_for_autoreplace = true; 289 data.safe_for_autoreplace = safe_for_autoreplace;
274 data.date_created = Time::FromTimeT(100); 290 data.date_created = Time::FromTimeT(100);
275 data.last_modified = Time::FromTimeT(last_mod); 291 data.last_modified = Time::FromTimeT(last_mod);
276 data.created_by_policy = created_by_policy; 292 data.created_by_policy = created_by_policy;
277 data.prepopulate_id = 999999; 293 data.prepopulate_id = 999999;
278 if (!guid.empty()) 294 if (!guid.empty())
279 data.sync_guid = guid; 295 data.sync_guid = guid;
280 return new TemplateURL(NULL, data); 296 return new TemplateURL(NULL, data);
281 } 297 }
282 298
283 void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected, 299 void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 return list; 353 return list;
338 } 354 }
339 355
340 TemplateURL* TemplateURLServiceSyncTest::Deserialize( 356 TemplateURL* TemplateURLServiceSyncTest::Deserialize(
341 const SyncData& sync_data) { 357 const SyncData& sync_data) {
342 SyncChangeList dummy; 358 SyncChangeList dummy;
343 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(NULL, 359 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(NULL,
344 NULL, sync_data, &dummy); 360 NULL, sync_data, &dummy);
345 } 361 }
346 362
363 std::string TemplateURLServiceSyncTest::MoveInitialGoogleTemplateURLAside() {
364 test_util_a_.ResetModel(true);
365 std::string google_hostname(
366 GURL(UIThreadSearchTermsData().GoogleBaseURLValue()).host());
367 TemplateURL* google = model()->GetTemplateURLForHost(google_hostname);
368 if (google != NULL) {
369 model()->ResetTemplateURL(google, ASCIIToUTF16("moved"),
370 ASCIIToUTF16("moved"), "http://moved/{searchTerms}");
371 }
372 return google_hostname;
373 }
374
347 375
348 // Actual tests --------------------------------------------------------------- 376 // Actual tests ---------------------------------------------------------------
349 377
350 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) { 378 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) {
351 // Create a TemplateURL and convert it into a sync specific type. 379 // Create a TemplateURL and convert it into a sync specific type.
352 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("unittest"), 380 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("unittest"),
353 "http://www.unittest.com/")); 381 "http://www.unittest.com/"));
354 SyncData sync_data = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 382 SyncData sync_data = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
355 // Convert the specifics back to a TemplateURL. 383 // Convert the specifics back to a TemplateURL.
356 scoped_ptr<TemplateURL> deserialized(Deserialize(sync_data)); 384 scoped_ptr<TemplateURL> deserialized(Deserialize(sync_data));
(...skipping 17 matching lines...) Expand all
374 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); 402 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
375 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter)); 403 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter));
376 AssertEquals(*service_turl, *deserialized); 404 AssertEquals(*service_turl, *deserialized);
377 } 405 }
378 } 406 }
379 407
380 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoExtensions) { 408 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoExtensions) {
381 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); 409 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com"));
382 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); 410 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com"));
383 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), 411 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"),
384 "chrome-extension://blahblahblah")); 412 std::string(chrome::kExtensionScheme) + "://blahblahblah"));
385 SyncDataList all_sync_data = 413 SyncDataList all_sync_data =
386 model()->GetAllSyncData(syncable::SEARCH_ENGINES); 414 model()->GetAllSyncData(syncable::SEARCH_ENGINES);
387 415
388 EXPECT_EQ(2U, all_sync_data.size()); 416 EXPECT_EQ(2U, all_sync_data.size());
389 417
390 for (SyncDataList::const_iterator iter = all_sync_data.begin(); 418 for (SyncDataList::const_iterator iter = all_sync_data.begin();
391 iter != all_sync_data.end(); ++iter) { 419 iter != all_sync_data.end(); ++iter) {
392 std::string guid = GetGUID(*iter); 420 std::string guid = GetGUID(*iter);
393 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); 421 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
394 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter)); 422 scoped_ptr<TemplateURL> deserialized(Deserialize(*iter));
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 467
440 // Test a third collision. This should collide on both the autogenerated 468 // Test a third collision. This should collide on both the autogenerated
441 // keyword and the first uniquification attempt. 469 // keyword and the first uniquification attempt.
442 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com")); 470 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com"));
443 new_keyword = model()->UniquifyKeyword(*turl); 471 new_keyword = model()->UniquifyKeyword(*turl);
444 EXPECT_EQ(ASCIIToUTF16("key1__"), new_keyword); 472 EXPECT_EQ(ASCIIToUTF16("key1__"), new_keyword);
445 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); 473 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));
446 } 474 }
447 475
448 TEST_F(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict) { 476 TEST_F(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict) {
477 // Create a keyword that conflicts, and make it older.
478 // Conflict, sync keyword is uniquified, and a SyncChange is added.
449 string16 original_turl_keyword = ASCIIToUTF16("key1"); 479 string16 original_turl_keyword = ASCIIToUTF16("key1");
450 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword, 480 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword,
451 "http://key1.com", std::string(), 9000); 481 "http://key1.com", std::string(), 9000);
452 model()->Add(original_turl); 482 model()->Add(original_turl);
453 483 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(original_turl_keyword,
454 // Create a key that does not conflict with something in the model. 484 "http://new.com", "remote", 8999));
455 scoped_ptr<TemplateURL> sync_turl(
456 CreateTestTemplateURL(ASCIIToUTF16("unique"), "http://new.com"));
457 string16 sync_keyword = sync_turl->keyword();
458 SyncChangeList changes; 485 SyncChangeList changes;
459 486 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
460 // No conflict, no TURLs changed, no changes.
461 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
462 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
463 EXPECT_EQ(sync_keyword, sync_turl->keyword());
464 EXPECT_EQ(0U, changes.size());
465
466 // Change sync keyword to something that conflicts, and make it older.
467 // Conflict, sync keyword is uniquified, and a SyncChange is added.
468 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com",
469 std::string(), 8999));
470 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
471 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); 487 EXPECT_NE(original_turl_keyword, sync_turl->keyword());
472 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); 488 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
473 EXPECT_EQ(1U, changes.size()); 489 ASSERT_EQ(1U, changes.size());
490 EXPECT_EQ("remote", GetGUID(changes[0].sync_data()));
474 changes.clear(); 491 changes.clear();
475 492
476 // Sync is newer. Original TemplateURL keyword is uniquified, no SyncChange 493 // Sync is newer. Original TemplateURL keyword is uniquified. A SyncChange is
477 // is added. Also ensure that this does not change the safe_for_autoreplace 494 // added (which in a normal run would be deleted by
478 // flag or the TemplateURLID in the original. 495 // PreventDuplicateGUIDUpdates() after we subsequently add an ACTION_ADD
496 // change for the local URL). Also ensure that this does not change the
497 // safe_for_autoreplace flag or the TemplateURLID in the original.
479 model()->Remove(original_turl); 498 model()->Remove(original_turl);
480 original_turl = CreateTestTemplateURL(original_turl_keyword, 499 original_turl = CreateTestTemplateURL(original_turl_keyword,
481 "http://key1.com", std::string(), 9000); 500 "http://key1.com", "local", 9000);
482 model()->Add(original_turl); 501 model()->Add(original_turl);
483 TemplateURLID original_id = original_turl->id(); 502 TemplateURLID original_id = original_turl->id();
484 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", 503 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com",
485 std::string(), 9001)); 504 std::string(), 9001));
486 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes)); 505 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
487 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); 506 EXPECT_EQ(original_turl_keyword, sync_turl->keyword());
488 EXPECT_NE(original_turl_keyword, original_turl->keyword()); 507 EXPECT_NE(original_turl_keyword, original_turl->keyword());
489 EXPECT_TRUE(original_turl->safe_for_autoreplace()); 508 EXPECT_TRUE(original_turl->safe_for_autoreplace());
490 EXPECT_EQ(original_id, original_turl->id()); 509 EXPECT_EQ(original_id, original_turl->id());
491 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); 510 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword));
492 EXPECT_EQ(0U, changes.size()); 511 ASSERT_EQ(1U, changes.size());
512 EXPECT_EQ("local", GetGUID(changes[0].sync_data()));
493 changes.clear(); 513 changes.clear();
494 514
495 // Equal times. Same result as above. Sync left alone, original uniquified so 515 // Equal times. Same result as above. Sync left alone, original uniquified so
496 // sync_turl can fit. 516 // sync_turl can fit.
497 model()->Remove(original_turl); 517 model()->Remove(original_turl);
498 original_turl = CreateTestTemplateURL(original_turl_keyword, 518 original_turl = CreateTestTemplateURL(original_turl_keyword,
499 "http://key1.com", std::string(), 9000); 519 "http://key1.com", "local2", 9000);
500 model()->Add(original_turl); 520 model()->Add(original_turl);
501 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", 521 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com",
502 std::string(), 9000)); 522 std::string(), 9000));
503 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes)); 523 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
504 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); 524 EXPECT_EQ(original_turl_keyword, sync_turl->keyword());
505 EXPECT_NE(original_turl_keyword, original_turl->keyword()); 525 EXPECT_NE(original_turl_keyword, original_turl->keyword());
506 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); 526 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword));
507 EXPECT_EQ(0U, changes.size()); 527 ASSERT_EQ(1U, changes.size());
528 EXPECT_EQ("local2", GetGUID(changes[0].sync_data()));
508 changes.clear(); 529 changes.clear();
509 530
510 // Sync is newer, but original TemplateURL is created by policy, so it wins. 531 // Sync is newer, but original TemplateURL is created by policy, so it wins.
511 // Sync keyword is uniquified, and a SyncChange is added. 532 // Sync keyword is uniquified, and a SyncChange is added.
512 model()->Remove(original_turl); 533 model()->Remove(original_turl);
513 original_turl = CreateTestTemplateURL(original_turl_keyword, 534 original_turl = CreateTestTemplateURL(original_turl_keyword,
514 "http://key1.com", std::string(), 9000, true); 535 "http://key1.com", std::string(), 9000, true);
515 model()->Add(original_turl); 536 model()->Add(original_turl);
516 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", 537 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com",
517 std::string(), 9999)); 538 "remote2", 9999));
518 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes)); 539 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
519 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); 540 EXPECT_NE(original_turl_keyword, sync_turl->keyword());
520 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); 541 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
521 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); 542 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
522 EXPECT_EQ(1U, changes.size()); 543 ASSERT_EQ(1U, changes.size());
544 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data()));
523 changes.clear(); 545 changes.clear();
524 } 546 }
525 547
526 TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) { 548 TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) {
527 TemplateURL* original_turl = 549 TemplateURL* original_turl =
528 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com"); 550 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com");
529 model()->Add(original_turl); 551 model()->Add(original_turl);
530 552
531 // No matches at all. 553 // No matches at all.
532 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(ASCIIToUTF16("key2"), 554 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(ASCIIToUTF16("key2"),
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 TemplateURL* sync_turl2 = CreateTestTemplateURL(ASCIIToUTF16("key1"), 596 TemplateURL* sync_turl2 = CreateTestTemplateURL(ASCIIToUTF16("key1"),
575 "http://key1.com", std::string(), 8999); 597 "http://key1.com", std::string(), 8999);
576 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes); 598 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes);
577 result = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); 599 result = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1"));
578 ASSERT_TRUE(result); 600 ASSERT_TRUE(result);
579 EXPECT_EQ(9001, result->last_modified().ToTimeT()); 601 EXPECT_EQ(9001, result->last_modified().ToTimeT());
580 EXPECT_EQ(1U, changes.size()); 602 EXPECT_EQ(1U, changes.size());
581 } 603 }
582 604
583 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) { 605 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) {
584 model()->MergeDataAndStartSyncing( 606 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, SyncDataList(),
585 syncable::SEARCH_ENGINES, SyncDataList(), 607 PassProcessor(), CreateAndPassSyncErrorFactory());
586 PassProcessor(),
587 CreateAndPassSyncErrorFactory());
588 608
589 EXPECT_EQ(0U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 609 EXPECT_EQ(0U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
590 EXPECT_EQ(0U, processor()->change_list_size()); 610 EXPECT_EQ(0U, processor()->change_list_size());
591 } 611 }
592 612
593 TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) { 613 TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) {
594 SyncDataList initial_data = CreateInitialSyncData(); 614 SyncDataList initial_data = CreateInitialSyncData();
595 615
596 model()->MergeDataAndStartSyncing( 616 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
597 syncable::SEARCH_ENGINES, initial_data, 617 PassProcessor(), CreateAndPassSyncErrorFactory());
598 PassProcessor(),
599 CreateAndPassSyncErrorFactory());
600 618
601 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 619 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
602 // We expect the model to have accepted all of the initial sync data. Search 620 // We expect the model to have accepted all of the initial sync data. Search
603 // through the model using the GUIDs to ensure that they're present. 621 // through the model using the GUIDs to ensure that they're present.
604 for (SyncDataList::const_iterator iter = initial_data.begin(); 622 for (SyncDataList::const_iterator iter = initial_data.begin();
605 iter != initial_data.end(); ++iter) { 623 iter != initial_data.end(); ++iter) {
606 std::string guid = GetGUID(*iter); 624 std::string guid = GetGUID(*iter);
607 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 625 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
608 } 626 }
609 627
610 EXPECT_EQ(0U, processor()->change_list_size()); 628 EXPECT_EQ(0U, processor()->change_list_size());
611 } 629 }
612 630
613 TEST_F(TemplateURLServiceSyncTest, MergeInAllNewData) { 631 TEST_F(TemplateURLServiceSyncTest, MergeInAllNewData) {
614 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("google.com"), 632 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("abc.com"), "http://abc.com",
615 "http://google.com", "abc")); 633 "abc"));
616 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("yahoo.com"), 634 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("def.com"), "http://def.com",
617 "http://yahoo.com", "def")); 635 "def"));
618 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("bing.com"), 636 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("xyz.com"), "http://xyz.com",
619 "http://bing.com", "xyz")); 637 "xyz"));
620 SyncDataList initial_data = CreateInitialSyncData(); 638 SyncDataList initial_data = CreateInitialSyncData();
621 639
622 model()->MergeDataAndStartSyncing( 640 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
623 syncable::SEARCH_ENGINES, initial_data, 641 PassProcessor(), CreateAndPassSyncErrorFactory());
624 PassProcessor(),
625 CreateAndPassSyncErrorFactory());
626 642
627 EXPECT_EQ(6U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 643 EXPECT_EQ(6U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
628 // We expect the model to have accepted all of the initial sync data. Search 644 // We expect the model to have accepted all of the initial sync data. Search
629 // through the model using the GUIDs to ensure that they're present. 645 // through the model using the GUIDs to ensure that they're present.
630 for (SyncDataList::const_iterator iter = initial_data.begin(); 646 for (SyncDataList::const_iterator iter = initial_data.begin();
631 iter != initial_data.end(); ++iter) { 647 iter != initial_data.end(); ++iter) {
632 std::string guid = GetGUID(*iter); 648 std::string guid = GetGUID(*iter);
633 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 649 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
634 } 650 }
635 // All the original TemplateURLs should also remain in the model. 651 // All the original TemplateURLs should also remain in the model.
636 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("google.com"))); 652 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("abc.com")));
637 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("yahoo.com"))); 653 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("def.com")));
638 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("bing.com"))); 654 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("xyz.com")));
639 // Ensure that Sync received the expected changes. 655 // Ensure that Sync received the expected changes.
640 EXPECT_EQ(3U, processor()->change_list_size()); 656 EXPECT_EQ(3U, processor()->change_list_size());
641 EXPECT_TRUE(processor()->contains_guid("abc")); 657 EXPECT_TRUE(processor()->contains_guid("abc"));
642 EXPECT_TRUE(processor()->contains_guid("def")); 658 EXPECT_TRUE(processor()->contains_guid("def"));
643 EXPECT_TRUE(processor()->contains_guid("xyz")); 659 EXPECT_TRUE(processor()->contains_guid("xyz"));
644 } 660 }
645 661
646 TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) { 662 TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) {
647 // The local data is the same as the sync data merged in. i.e. - There have 663 // The local data is the same as the sync data merged in. i.e. - There have
648 // been no changes since the last time we synced. Even the last_modified 664 // been no changes since the last time we synced. Even the last_modified
649 // timestamps are the same. 665 // timestamps are the same.
650 SyncDataList initial_data = CreateInitialSyncData(); 666 SyncDataList initial_data = CreateInitialSyncData();
651 for (SyncDataList::const_iterator iter = initial_data.begin(); 667 for (SyncDataList::const_iterator iter = initial_data.begin();
652 iter != initial_data.end(); ++iter) { 668 iter != initial_data.end(); ++iter) {
653 TemplateURL* converted = Deserialize(*iter); 669 TemplateURL* converted = Deserialize(*iter);
654 model()->Add(converted); 670 model()->Add(converted);
655 } 671 }
656 672
657 model()->MergeDataAndStartSyncing( 673 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
658 syncable::SEARCH_ENGINES, initial_data, 674 PassProcessor(), CreateAndPassSyncErrorFactory());
659 PassProcessor(),
660 CreateAndPassSyncErrorFactory());
661 675
662 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 676 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
663 for (SyncDataList::const_iterator iter = initial_data.begin(); 677 for (SyncDataList::const_iterator iter = initial_data.begin();
664 iter != initial_data.end(); ++iter) { 678 iter != initial_data.end(); ++iter) {
665 std::string guid = GetGUID(*iter); 679 std::string guid = GetGUID(*iter);
666 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 680 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
667 } 681 }
668 EXPECT_EQ(0U, processor()->change_list_size()); 682 EXPECT_EQ(0U, processor()->change_list_size());
669 } 683 }
670 684
671 TEST_F(TemplateURLServiceSyncTest, MergeUpdateFromSync) { 685 TEST_F(TemplateURLServiceSyncTest, MergeUpdateFromSync) {
672 // The local data is the same as the sync data merged in, but timestamps have 686 // The local data is the same as the sync data merged in, but timestamps have
673 // changed. Ensure the right fields are merged in. 687 // changed. Ensure the right fields are merged in.
674 SyncDataList initial_data; 688 SyncDataList initial_data;
675 TemplateURL* turl1 = CreateTestTemplateURL(ASCIIToUTF16("google.com"), 689 TemplateURL* turl1 = CreateTestTemplateURL(ASCIIToUTF16("abc.com"),
676 "http://google.com", "abc", 9000); 690 "http://abc.com", "abc", 9000);
677 model()->Add(turl1); 691 model()->Add(turl1);
678 TemplateURL* turl2 = CreateTestTemplateURL(ASCIIToUTF16("bing.com"), 692 TemplateURL* turl2 = CreateTestTemplateURL(ASCIIToUTF16("xyz.com"),
679 "http://bing.com", "xyz", 9000); 693 "http://xyz.com", "xyz", 9000);
680 model()->Add(turl2); 694 model()->Add(turl2);
681 695
682 scoped_ptr<TemplateURL> turl1_newer(CreateTestTemplateURL( 696 scoped_ptr<TemplateURL> turl1_newer(CreateTestTemplateURL(
683 ASCIIToUTF16("google.com"), "http://google.ca", "abc", 9999)); 697 ASCIIToUTF16("abc.com"), "http://abc.ca", "abc", 9999));
684 initial_data.push_back( 698 initial_data.push_back(
685 TemplateURLService::CreateSyncDataFromTemplateURL(*turl1_newer)); 699 TemplateURLService::CreateSyncDataFromTemplateURL(*turl1_newer));
686 700
687 scoped_ptr<TemplateURL> turl2_older(CreateTestTemplateURL( 701 scoped_ptr<TemplateURL> turl2_older(CreateTestTemplateURL(
688 ASCIIToUTF16("bing.com"), "http://bing.ca", "xyz", 8888)); 702 ASCIIToUTF16("xyz.com"), "http://xyz.ca", "xyz", 8888));
689 initial_data.push_back( 703 initial_data.push_back(
690 TemplateURLService::CreateSyncDataFromTemplateURL(*turl2_older)); 704 TemplateURLService::CreateSyncDataFromTemplateURL(*turl2_older));
691 705
692 model()->MergeDataAndStartSyncing( 706 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
693 syncable::SEARCH_ENGINES, initial_data, 707 PassProcessor(), CreateAndPassSyncErrorFactory());
694 PassProcessor(),
695 CreateAndPassSyncErrorFactory());
696 708
697 // Both were local updates, so we expect the same count. 709 // Both were local updates, so we expect the same count.
698 EXPECT_EQ(2U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 710 EXPECT_EQ(2U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
699 711
700 // Check that the first replaced the initial Google TemplateURL. 712 // Check that the first replaced the initial abc TemplateURL.
701 EXPECT_EQ(turl1, model()->GetTemplateURLForGUID("abc")); 713 EXPECT_EQ(turl1, model()->GetTemplateURLForGUID("abc"));
702 EXPECT_EQ("http://google.ca", turl1->url()); 714 EXPECT_EQ("http://abc.ca", turl1->url());
703 715
704 // Check that the second produced an upstream update to the Bing TemplateURL. 716 // Check that the second produced an upstream update to the xyz TemplateURL.
705 EXPECT_EQ(1U, processor()->change_list_size()); 717 EXPECT_EQ(1U, processor()->change_list_size());
706 ASSERT_TRUE(processor()->contains_guid("xyz")); 718 ASSERT_TRUE(processor()->contains_guid("xyz"));
707 SyncChange change = processor()->change_for_guid("xyz"); 719 SyncChange change = processor()->change_for_guid("xyz");
708 EXPECT_TRUE(change.change_type() == SyncChange::ACTION_UPDATE); 720 EXPECT_TRUE(change.change_type() == SyncChange::ACTION_UPDATE);
709 EXPECT_EQ("http://bing.com", GetURL(change.sync_data())); 721 EXPECT_EQ("http://xyz.com", GetURL(change.sync_data()));
710 } 722 }
711 723
712 TEST_F(TemplateURLServiceSyncTest, MergeAddFromOlderSyncData) { 724 TEST_F(TemplateURLServiceSyncTest, MergeAddFromOlderSyncData) {
713 // GUIDs all differ, so this is data to be added from Sync, but the timestamps 725 // GUIDs all differ, so this is data to be added from Sync, but the timestamps
714 // from Sync are older. Set up the local data so that one is a dupe, one has a 726 // from Sync are older. Set up the local data so that one is a dupe, one has a
715 // conflicting keyword, and the last has no conflicts (a clean ADD). 727 // conflicting keyword, and the last has no conflicts (a clean ADD).
716 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", 728 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com",
717 "aaa", 100)); // dupe 729 "aaa", 100)); // dupe
718 730
719 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), 731 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"),
720 "http://expected.com", "bbb", 100)); // keyword conflict 732 "http://expected.com", "bbb", 100)); // keyword conflict
721 733
722 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), 734 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"),
723 "http://unique.com", "ccc")); // add 735 "http://unique.com", "ccc")); // add
724 736
725 model()->MergeDataAndStartSyncing( 737 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
726 syncable::SEARCH_ENGINES, CreateInitialSyncData(), 738 CreateInitialSyncData(), PassProcessor(),
727 PassProcessor(), 739 CreateAndPassSyncErrorFactory());
728 CreateAndPassSyncErrorFactory());
729 740
730 // The dupe results in a merge. The other two should be added to the model. 741 // The dupe results in a merge. The other two should be added to the model.
731 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 742 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
732 743
733 // The key1 duplicate results in the local copy winning. Ensure that Sync's 744 // The key1 duplicate results in the local copy winning. Ensure that Sync's
734 // copy was not added, and the local copy is pushed upstream to Sync as an 745 // copy was not added, and the local copy is pushed upstream to Sync as an
735 // update. The local copy should have received the sync data's GUID. 746 // update. The local copy should have received the sync data's GUID.
736 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 747 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
737 // Check changes for the UPDATE. 748 // Check changes for the UPDATE.
738 ASSERT_TRUE(processor()->contains_guid("key1")); 749 ASSERT_TRUE(processor()->contains_guid("key1"));
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
775 // conflicting keyword, and the last has no conflicts (a clean ADD). 786 // conflicting keyword, and the last has no conflicts (a clean ADD).
776 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", 787 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com",
777 "aaa", 10)); // dupe 788 "aaa", 10)); // dupe
778 789
779 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), 790 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"),
780 "http://expected.com", "bbb", 10)); // keyword conflict 791 "http://expected.com", "bbb", 10)); // keyword conflict
781 792
782 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), 793 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"),
783 "http://unique.com", "ccc", 10)); // add 794 "http://unique.com", "ccc", 10)); // add
784 795
785 model()->MergeDataAndStartSyncing( 796 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
786 syncable::SEARCH_ENGINES, 797 CreateInitialSyncData(), PassProcessor(),
787 CreateInitialSyncData(), 798 CreateAndPassSyncErrorFactory());
788 PassProcessor(),
789 CreateAndPassSyncErrorFactory());
790 799
791 // The dupe results in a merge. The other two should be added to the model. 800 // The dupe results in a merge. The other two should be added to the model.
792 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 801 EXPECT_EQ(5U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
793 802
794 // The key1 duplicate results in Sync's copy winning. Ensure that Sync's 803 // The key1 duplicate results in Sync's copy winning. Ensure that Sync's
795 // copy replaced the local copy. 804 // copy replaced the local copy.
796 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 805 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
797 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); 806 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa"));
798 807
799 // The key2 keyword conflict results in Sync's copy winning, so ensure it 808 // The key2 keyword conflict results in Sync's copy winning, so ensure it
(...skipping 17 matching lines...) Expand all
817 ASSERT_TRUE(processor()->contains_guid("bbb")); 826 ASSERT_TRUE(processor()->contains_guid("bbb"));
818 EXPECT_EQ(SyncChange::ACTION_ADD, 827 EXPECT_EQ(SyncChange::ACTION_ADD,
819 processor()->change_for_guid("bbb").change_type()); 828 processor()->change_for_guid("bbb").change_type());
820 ASSERT_TRUE(processor()->contains_guid("ccc")); 829 ASSERT_TRUE(processor()->contains_guid("ccc"));
821 EXPECT_EQ(SyncChange::ACTION_ADD, 830 EXPECT_EQ(SyncChange::ACTION_ADD,
822 processor()->change_for_guid("ccc").change_type()); 831 processor()->change_for_guid("ccc").change_type());
823 } 832 }
824 833
825 TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) { 834 TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) {
826 // We initially have no data. 835 // We initially have no data.
827 model()->MergeDataAndStartSyncing( 836 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, SyncDataList(),
828 syncable::SEARCH_ENGINES, SyncDataList(), 837 PassProcessor(), CreateAndPassSyncErrorFactory());
829 PassProcessor(),
830 CreateAndPassSyncErrorFactory());
831 838
832 // Set up a bunch of ADDs. 839 // Set up a bunch of ADDs.
833 SyncChangeList changes; 840 SyncChangeList changes;
834 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 841 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
835 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1"))); 842 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")));
836 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 843 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
837 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", "key2"))); 844 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", "key2")));
838 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 845 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
839 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3"))); 846 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3")));
840 847
841 model()->ProcessSyncChanges(FROM_HERE, changes); 848 model()->ProcessSyncChanges(FROM_HERE, changes);
842 849
843 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 850 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
844 EXPECT_EQ(0U, processor()->change_list_size()); 851 EXPECT_EQ(0U, processor()->change_list_size());
845 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 852 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
846 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 853 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
847 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); 854 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
848 } 855 }
849 856
850 TEST_F(TemplateURLServiceSyncTest, ProcessChangesNoConflicts) { 857 TEST_F(TemplateURLServiceSyncTest, ProcessChangesNoConflicts) {
851 model()->MergeDataAndStartSyncing( 858 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
852 syncable::SEARCH_ENGINES, 859 CreateInitialSyncData(), PassProcessor(),
853 CreateInitialSyncData(), PassProcessor(), 860 CreateAndPassSyncErrorFactory());
854 CreateAndPassSyncErrorFactory());
855 861
856 // Process different types of changes, without conflicts. 862 // Process different types of changes, without conflicts.
857 SyncChangeList changes; 863 SyncChangeList changes;
858 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 864 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
859 CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com", "key4"))); 865 CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com", "key4")));
860 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 866 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
861 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 867 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
862 "key2"))); 868 "key2")));
863 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_DELETE, 869 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_DELETE,
864 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3"))); 870 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3")));
865 871
866 model()->ProcessSyncChanges(FROM_HERE, changes); 872 model()->ProcessSyncChanges(FROM_HERE, changes);
867 873
868 // Add one, remove one, update one, so the number shouldn't change. 874 // Add one, remove one, update one, so the number shouldn't change.
869 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 875 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
870 EXPECT_EQ(0U, processor()->change_list_size()); 876 EXPECT_EQ(0U, processor()->change_list_size());
871 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 877 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
872 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 878 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
873 const TemplateURL* turl = model()->GetTemplateURLForGUID("key2"); 879 const TemplateURL* turl = model()->GetTemplateURLForGUID("key2");
874 EXPECT_TRUE(turl); 880 EXPECT_TRUE(turl);
875 EXPECT_EQ(ASCIIToUTF16("newkeyword"), turl->keyword()); 881 EXPECT_EQ(ASCIIToUTF16("newkeyword"), turl->keyword());
876 EXPECT_EQ("http://new.com", turl->url()); 882 EXPECT_EQ("http://new.com", turl->url());
877 EXPECT_FALSE(model()->GetTemplateURLForGUID("key3")); 883 EXPECT_FALSE(model()->GetTemplateURLForGUID("key3"));
878 EXPECT_TRUE(model()->GetTemplateURLForGUID("key4")); 884 EXPECT_TRUE(model()->GetTemplateURLForGUID("key4"));
879 } 885 }
880 886
881 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsSyncWins) { 887 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsSyncWins) {
882 model()->MergeDataAndStartSyncing( 888 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
883 syncable::SEARCH_ENGINES, 889 CreateInitialSyncData(), PassProcessor(),
884 CreateInitialSyncData(), PassProcessor(), 890 CreateAndPassSyncErrorFactory());
885 CreateAndPassSyncErrorFactory());
886 891
887 // Process different types of changes, with conflicts. Note that all this data 892 // Process different types of changes, with conflicts. Note that all this data
888 // has a newer timestamp, so Sync will win in these scenarios. 893 // has a newer timestamp, so Sync will win in these scenarios.
889 SyncChangeList changes; 894 SyncChangeList changes;
890 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 895 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
891 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa"))); 896 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa")));
892 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 897 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
893 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1"))); 898 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1")));
894 899
895 model()->ProcessSyncChanges(FROM_HERE, changes); 900 model()->ProcessSyncChanges(FROM_HERE, changes);
(...skipping 14 matching lines...) Expand all
910 // key1 update conflicts with key3 and wins, forcing key3's keyword to update. 915 // key1 update conflicts with key3 and wins, forcing key3's keyword to update.
911 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); 916 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1"));
912 EXPECT_EQ(model()->GetTemplateURLForGUID("key1"), 917 EXPECT_EQ(model()->GetTemplateURLForGUID("key1"),
913 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3"))); 918 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3")));
914 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); 919 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3"));
915 EXPECT_EQ(model()->GetTemplateURLForGUID("key3"), 920 EXPECT_EQ(model()->GetTemplateURLForGUID("key3"),
916 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3.com"))); 921 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3.com")));
917 } 922 }
918 923
919 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsLocalWins) { 924 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithConflictsLocalWins) {
920 model()->MergeDataAndStartSyncing( 925 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
921 syncable::SEARCH_ENGINES, 926 CreateInitialSyncData(), PassProcessor(),
922 CreateInitialSyncData(), PassProcessor(), 927 CreateAndPassSyncErrorFactory());
923 CreateAndPassSyncErrorFactory());
924 928
925 // Process different types of changes, with conflicts. Note that all this data 929 // Process different types of changes, with conflicts. Note that all this data
926 // has an older timestamp, so the local data will win in these scenarios. 930 // has an older timestamp, so the local data will win in these scenarios.
927 SyncChangeList changes; 931 SyncChangeList changes;
928 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD, 932 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
929 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa", 933 CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://new.com", "aaa",
930 10))); 934 10)));
931 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 935 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
932 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1", 936 CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key1",
933 10))); 937 10)));
(...skipping 25 matching lines...) Expand all
959 EXPECT_EQ(SyncChange::ACTION_UPDATE, 963 EXPECT_EQ(SyncChange::ACTION_UPDATE,
960 processor()->change_for_guid("aaa").change_type()); 964 processor()->change_for_guid("aaa").change_type());
961 ASSERT_TRUE(processor()->contains_guid("key1")); 965 ASSERT_TRUE(processor()->contains_guid("key1"));
962 EXPECT_EQ(SyncChange::ACTION_UPDATE, 966 EXPECT_EQ(SyncChange::ACTION_UPDATE,
963 processor()->change_for_guid("key1").change_type()); 967 processor()->change_for_guid("key1").change_type());
964 } 968 }
965 969
966 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { 970 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) {
967 // Ensure that ProcessTemplateURLChange is called and pushes the correct 971 // Ensure that ProcessTemplateURLChange is called and pushes the correct
968 // changes to Sync whenever local changes are made to TemplateURLs. 972 // changes to Sync whenever local changes are made to TemplateURLs.
969 model()->MergeDataAndStartSyncing( 973 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
970 syncable::SEARCH_ENGINES, 974 CreateInitialSyncData(), PassProcessor(),
971 CreateInitialSyncData(), PassProcessor(), 975 CreateAndPassSyncErrorFactory());
972 CreateAndPassSyncErrorFactory());
973 976
974 // Add a new search engine. 977 // Add a new search engine.
975 TemplateURL* new_turl = 978 TemplateURL* new_turl =
976 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"); 979 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new");
977 model()->Add(new_turl); 980 model()->Add(new_turl);
978 EXPECT_EQ(1U, processor()->change_list_size()); 981 EXPECT_EQ(1U, processor()->change_list_size());
979 ASSERT_TRUE(processor()->contains_guid("new")); 982 ASSERT_TRUE(processor()->contains_guid("new"));
980 SyncChange change = processor()->change_for_guid("new"); 983 SyncChange change = processor()->change_for_guid("new");
981 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type()); 984 EXPECT_EQ(SyncChange::ACTION_ADD, change.change_type());
982 EXPECT_EQ("baidu", GetKeyword(change.sync_data())); 985 EXPECT_EQ("baidu", GetKeyword(change.sync_data()));
(...skipping 11 matching lines...) Expand all
994 997
995 // Remove an existing search engine. 998 // Remove an existing search engine.
996 existing_turl = model()->GetTemplateURLForGUID("key2"); 999 existing_turl = model()->GetTemplateURLForGUID("key2");
997 model()->Remove(existing_turl); 1000 model()->Remove(existing_turl);
998 EXPECT_EQ(1U, processor()->change_list_size()); 1001 EXPECT_EQ(1U, processor()->change_list_size());
999 ASSERT_TRUE(processor()->contains_guid("key2")); 1002 ASSERT_TRUE(processor()->contains_guid("key2"));
1000 change = processor()->change_for_guid("key2"); 1003 change = processor()->change_for_guid("key2");
1001 EXPECT_EQ(SyncChange::ACTION_DELETE, change.change_type()); 1004 EXPECT_EQ(SyncChange::ACTION_DELETE, change.change_type());
1002 } 1005 }
1003 1006
1007 TEST_F(TemplateURLServiceSyncTest, ProcessChangesWithLocalExtensions) {
1008 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1009 CreateInitialSyncData(), PassProcessor(),
1010 CreateAndPassSyncErrorFactory());
1011
1012 // Add some extension keywords locally. These shouldn't be synced.
1013 TemplateURL* extension1 = CreateTestTemplateURL(ASCIIToUTF16("keyword1"),
1014 std::string(chrome::kExtensionScheme) + "://extension1");
1015 model()->Add(extension1);
1016 TemplateURL* extension2 = CreateTestTemplateURL(ASCIIToUTF16("keyword2"),
1017 std::string(chrome::kExtensionScheme) + "://extension2");
1018 model()->Add(extension2);
1019 EXPECT_EQ(0U, processor()->change_list_size());
1020
1021 // Create some sync changes that will conflict with the extension keywords.
1022 SyncChangeList changes;
1023 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
1024 CreateTestTemplateURL(ASCIIToUTF16("keyword1"), "http://aaa.com")));
1025 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_ADD,
1026 CreateTestTemplateURL(ASCIIToUTF16("keyword2"), "http://bbb.com",
1027 std::string(), 100, false, false)));
1028 model()->ProcessSyncChanges(FROM_HERE, changes);
1029
1030 // The synced keywords should be added unchanged, and the result of
1031 // GetTemplateURLForKeyword() for each keyword should depend on whether the
1032 // synced keyword is replaceable or not.
1033 EXPECT_EQ(extension1,
1034 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")));
1035 EXPECT_FALSE(model()->GetTemplateURLForHost("aaa.com") == NULL);
1036 TemplateURL* url_for_keyword2 =
1037 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"));
1038 EXPECT_NE(extension2, url_for_keyword2);
1039 EXPECT_EQ("http://bbb.com", url_for_keyword2->url());
1040 // Note that extensions don't use host-based keywords, so we can't check that
1041 // |extension2| is still in the model using GetTemplateURLForHost(); and we
1042 // have to create a full-fledged Extension to use
1043 // GetTemplateURLForExtension(), which is annoying, so just grab all the URLs
1044 // from the model and search for |extension2| within them.
1045 TemplateURLService::TemplateURLVector template_urls(
1046 model()->GetTemplateURLs());
1047 EXPECT_FALSE(std::find(template_urls.begin(), template_urls.end(),
1048 extension2) == template_urls.end());
1049 }
1050
1051 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordMigrated) {
1052 std::string google_hostname(MoveInitialGoogleTemplateURLAside());
1053
1054 // Create a couple of sync entries with autogenerated keywords.
1055 SyncDataList initial_data;
1056 scoped_ptr<TemplateURL> turl(
1057 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1"));
1058 initial_data.push_back(
1059 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1060 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"),
1061 "{google:baseURL}search?q={searchTerms}", "key2"));
1062 initial_data.push_back(
1063 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1064
1065 // Now try to sync the data locally.
1066 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1067 PassProcessor(), CreateAndPassSyncErrorFactory());
1068
1069 // Both entries should have been added, with explicit keywords.
1070 TemplateURL* key1 = model()->GetTemplateURLForHost("key1.com");
1071 ASSERT_FALSE(key1 == NULL);
1072 EXPECT_EQ(ASCIIToUTF16("key1.com"), key1->keyword());
1073 TemplateURL* key2 = model()->GetTemplateURLForHost(google_hostname);
1074 ASSERT_FALSE(key2 == NULL);
1075 string16 google_keyword(net::StripWWW(ASCIIToUTF16(google_hostname)));
1076 EXPECT_EQ(google_keyword, key2->keyword());
1077
1078 // We should also have gotten some corresponding UPDATEs pushed upstream.
1079 EXPECT_GE(processor()->change_list_size(), 2U);
1080 ASSERT_TRUE(processor()->contains_guid("key1"));
1081 SyncChange key1_change = processor()->change_for_guid("key1");
1082 EXPECT_EQ(SyncChange::ACTION_UPDATE, key1_change.change_type());
1083 EXPECT_EQ("key1.com", GetKeyword(key1_change.sync_data()));
1084 ASSERT_TRUE(processor()->contains_guid("key2"));
1085 SyncChange key2_change = processor()->change_for_guid("key2");
1086 EXPECT_EQ(SyncChange::ACTION_UPDATE, key2_change.change_type());
1087 EXPECT_EQ(google_keyword, UTF8ToUTF16(GetKeyword(key2_change.sync_data())));
1088 }
1089
1090 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordConflicts) {
1091 std::string google_hostname(MoveInitialGoogleTemplateURLAside());
1092
1093 // Sync brings in some autogenerated keywords, but the generated keywords we
1094 // try to create conflict with ones in the model.
1095 string16 google_keyword(net::StripWWW(ASCIIToUTF16(google_hostname)));
1096 TemplateURL* google = CreateTestTemplateURL(google_keyword,
1097 "{google:baseURL}1/search?q={searchTerms}");
1098 model()->Add(google);
1099 TemplateURL* other =
1100 CreateTestTemplateURL(ASCIIToUTF16("other.com"), "http://other.com/foo");
1101 model()->Add(other);
1102 SyncDataList initial_data;
1103 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("sync1"),
1104 "{google:baseURL}2/search?q={searchTerms}", "sync1", 50));
1105 initial_data.push_back(
1106 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1107 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("sync2"),
1108 "http://other.com/search?q={searchTerms}", "sync2", 150));
1109 initial_data.push_back(
1110 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1111 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1112 PassProcessor(), CreateAndPassSyncErrorFactory());
1113
1114 // In this case, the conflicts should be handled just like any other keyword
1115 // conflicts -- the later-modified TemplateURL is assumed to be authoritative.
1116 EXPECT_EQ(google_keyword, google->keyword());
1117 EXPECT_EQ(google_keyword + ASCIIToUTF16("_"),
1118 model()->GetTemplateURLForGUID("sync1")->keyword());
1119 EXPECT_EQ(ASCIIToUTF16("other.com_"), other->keyword());
1120 EXPECT_EQ(ASCIIToUTF16("other.com"),
1121 model()->GetTemplateURLForGUID("sync2")->keyword());
1122
1123 // Both synced URLs should have associated UPDATEs, since both needed their
1124 // keywords to be generated (and sync1 needed conflict resolution as well).
1125 EXPECT_GE(processor()->change_list_size(), 2U);
1126 ASSERT_TRUE(processor()->contains_guid("sync1"));
1127 SyncChange sync1_change = processor()->change_for_guid("sync1");
1128 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync1_change.change_type());
1129 EXPECT_EQ(google_keyword + ASCIIToUTF16("_"),
1130 UTF8ToUTF16(GetKeyword(sync1_change.sync_data())));
1131 ASSERT_TRUE(processor()->contains_guid("sync2"));
1132 SyncChange sync2_change = processor()->change_for_guid("sync2");
1133 EXPECT_EQ(SyncChange::ACTION_UPDATE, sync2_change.change_type());
1134 EXPECT_EQ("other.com", GetKeyword(sync2_change.sync_data()));
1135 }
1136
1137 TEST_F(TemplateURLServiceSyncTest, TwoAutogeneratedKeywordsUsingGoogleBaseURL) {
1138 std::string google_hostname(MoveInitialGoogleTemplateURLAside());
1139
1140 // Sync brings in two autogenerated keywords and both use Google base URLs.
1141 // We make the first older so that it will get renamed once before the second
1142 // and then again once after (when we resolve conflicts for the second).
1143 SyncDataList initial_data;
1144 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"),
1145 "{google:baseURL}1/search?q={searchTerms}", "key1", 50));
1146 initial_data.push_back(
1147 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1148 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"),
1149 "{google:baseURL}2/search?q={searchTerms}", "key2"));
1150 initial_data.push_back(
1151 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1152 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1153 PassProcessor(), CreateAndPassSyncErrorFactory());
1154
1155 // We should still have coalesced the updates to one each.
1156 string16 google_keyword(net::StripWWW(ASCIIToUTF16(google_hostname)));
1157 TemplateURL* keyword1 =
1158 model()->GetTemplateURLForKeyword(google_keyword + ASCIIToUTF16("_"));
1159 ASSERT_FALSE(keyword1 == NULL);
1160 EXPECT_EQ("key1", keyword1->sync_guid());
1161 TemplateURL* keyword2 = model()->GetTemplateURLForKeyword(google_keyword);
1162 ASSERT_FALSE(keyword2 == NULL);
1163 EXPECT_EQ("key2", keyword2->sync_guid());
1164 EXPECT_GE(processor()->change_list_size(), 2U);
1165 ASSERT_TRUE(processor()->contains_guid("key1"));
1166 SyncChange key1_change = processor()->change_for_guid("key1");
1167 EXPECT_EQ(SyncChange::ACTION_UPDATE, key1_change.change_type());
1168 EXPECT_EQ(keyword1->keyword(),
1169 UTF8ToUTF16(GetKeyword(key1_change.sync_data())));
1170 ASSERT_TRUE(processor()->contains_guid("key2"));
1171 SyncChange key2_change = processor()->change_for_guid("key2");
1172 EXPECT_EQ(SyncChange::ACTION_UPDATE, key2_change.change_type());
1173 EXPECT_EQ(keyword2->keyword(),
1174 UTF8ToUTF16(GetKeyword(key2_change.sync_data())));
1175 }
1176
1004 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsBasic) { 1177 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsBasic) {
1005 // Start off B with some empty data. 1178 // Start off B with some empty data.
1006 model_b()->MergeDataAndStartSyncing( 1179 model_b()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1007 syncable::SEARCH_ENGINES, 1180 CreateInitialSyncData(), PassProcessor(),
1008 CreateInitialSyncData(), PassProcessor(), 1181 CreateAndPassSyncErrorFactory());
1009 CreateAndPassSyncErrorFactory());
1010 1182
1011 // Merge A and B. All of B's data should transfer over to A, which initially 1183 // Merge A and B. All of B's data should transfer over to A, which initially
1012 // has no data. 1184 // has no data.
1013 scoped_ptr<SyncChangeProcessorDelegate> delegate_b( 1185 scoped_ptr<SyncChangeProcessorDelegate> delegate_b(
1014 new SyncChangeProcessorDelegate(model_b())); 1186 new SyncChangeProcessorDelegate(model_b()));
1015 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, 1187 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1016 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES), 1188 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES),
1017 delegate_b.PassAs<SyncChangeProcessor>(), 1189 delegate_b.PassAs<SyncChangeProcessor>(),
1018 CreateAndPassSyncErrorFactory()); 1190 CreateAndPassSyncErrorFactory());
1019 1191
1020 // They should be consistent. 1192 // They should be consistent.
1021 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES), 1193 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES),
1022 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES)); 1194 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES));
1023 } 1195 }
1024 1196
1025 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsDupesAndConflicts) { 1197 TEST_F(TemplateURLServiceSyncTest, MergeTwoClientsDupesAndConflicts) {
1026 // Start off B with some empty data. 1198 // Start off B with some empty data.
1027 model_b()->MergeDataAndStartSyncing( 1199 model_b()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1028 syncable::SEARCH_ENGINES, 1200 CreateInitialSyncData(), PassProcessor(),
1029 CreateInitialSyncData(), PassProcessor(), 1201 CreateAndPassSyncErrorFactory());
1030 CreateAndPassSyncErrorFactory());
1031 1202
1032 // Set up A so we have some interesting duplicates and conflicts. 1203 // Set up A so we have some interesting duplicates and conflicts.
1033 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com", 1204 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key4"), "http://key4.com",
1034 "key4")); // Added 1205 "key4")); // Added
1035 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", 1206 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com",
1036 "key2")); // Merge - Copy of key2. 1207 "key2")); // Merge - Copy of key2.
1037 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", 1208 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com",
1038 "key5", 10)); // Merge - Dupe of key3. 1209 "key5", 10)); // Merge - Dupe of key3.
1039 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key6.com", 1210 model_a()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key6.com",
1040 "key6", 10)); // Conflict with key1 1211 "key6", 10)); // Conflict with key1
1041 1212
1042 // Merge A and B. 1213 // Merge A and B.
1043 scoped_ptr<SyncChangeProcessorDelegate> delegate_b( 1214 scoped_ptr<SyncChangeProcessorDelegate> delegate_b(
1044 new SyncChangeProcessorDelegate(model_b())); 1215 new SyncChangeProcessorDelegate(model_b()));
1045 model_a()->MergeDataAndStartSyncing( 1216 model_a()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1046 syncable::SEARCH_ENGINES,
1047 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES), 1217 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES),
1048 delegate_b.PassAs<SyncChangeProcessor>(), 1218 delegate_b.PassAs<SyncChangeProcessor>(),
1049 CreateAndPassSyncErrorFactory()); 1219 CreateAndPassSyncErrorFactory());
1050 1220
1051 // They should be consistent. 1221 // They should be consistent.
1052 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES), 1222 AssertEquals(model_a()->GetAllSyncData(syncable::SEARCH_ENGINES),
1053 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES)); 1223 model_b()->GetAllSyncData(syncable::SEARCH_ENGINES));
1054 } 1224 }
1055 1225
1056 TEST_F(TemplateURLServiceSyncTest, StopSyncing) { 1226 TEST_F(TemplateURLServiceSyncTest, StopSyncing) {
1057 SyncError error = model()->MergeDataAndStartSyncing( 1227 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1058 syncable::SEARCH_ENGINES,
1059 CreateInitialSyncData(), PassProcessor(), 1228 CreateInitialSyncData(), PassProcessor(),
1060 CreateAndPassSyncErrorFactory()); 1229 CreateAndPassSyncErrorFactory());
1061 ASSERT_FALSE(error.IsSet()); 1230 ASSERT_FALSE(error.IsSet());
1062 model()->StopSyncing(syncable::SEARCH_ENGINES); 1231 model()->StopSyncing(syncable::SEARCH_ENGINES);
1063 1232
1064 SyncChangeList changes; 1233 SyncChangeList changes;
1065 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 1234 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
1066 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 1235 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
1067 "key2"))); 1236 "key2")));
1068 error = model()->ProcessSyncChanges(FROM_HERE, changes); 1237 error = model()->ProcessSyncChanges(FROM_HERE, changes);
1069 EXPECT_TRUE(error.IsSet()); 1238 EXPECT_TRUE(error.IsSet());
1070 1239
1071 // Ensure that the sync changes were not accepted. 1240 // Ensure that the sync changes were not accepted.
1072 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 1241 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
1073 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword"))); 1242 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword")));
1074 } 1243 }
1075 1244
1076 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnInitialSync) { 1245 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnInitialSync) {
1077 processor()->set_erroneous(true); 1246 processor()->set_erroneous(true);
1078 SyncError error = model()->MergeDataAndStartSyncing( 1247 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1079 syncable::SEARCH_ENGINES,
1080 CreateInitialSyncData(), PassProcessor(), 1248 CreateInitialSyncData(), PassProcessor(),
1081 CreateAndPassSyncErrorFactory()); 1249 CreateAndPassSyncErrorFactory());
1082 EXPECT_TRUE(error.IsSet()); 1250 EXPECT_TRUE(error.IsSet());
1083 1251
1084 // Ensure that if the initial merge was erroneous, then subsequence attempts 1252 // Ensure that if the initial merge was erroneous, then subsequence attempts
1085 // to push data into the local model are rejected, since the model was never 1253 // to push data into the local model are rejected, since the model was never
1086 // successfully associated with Sync in the first place. 1254 // successfully associated with Sync in the first place.
1087 SyncChangeList changes; 1255 SyncChangeList changes;
1088 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 1256 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
1089 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 1257 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
1090 "key2"))); 1258 "key2")));
1091 processor()->set_erroneous(false); 1259 processor()->set_erroneous(false);
1092 error = model()->ProcessSyncChanges(FROM_HERE, changes); 1260 error = model()->ProcessSyncChanges(FROM_HERE, changes);
1093 EXPECT_TRUE(error.IsSet()); 1261 EXPECT_TRUE(error.IsSet());
1094 1262
1095 // Ensure that the sync changes were not accepted. 1263 // Ensure that the sync changes were not accepted.
1096 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); 1264 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2"));
1097 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword"))); 1265 EXPECT_FALSE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("newkeyword")));
1098 } 1266 }
1099 1267
1100 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnLaterSync) { 1268 TEST_F(TemplateURLServiceSyncTest, SyncErrorOnLaterSync) {
1101 // Ensure that if the SyncProcessor succeeds in the initial merge, but fails 1269 // Ensure that if the SyncProcessor succeeds in the initial merge, but fails
1102 // in future ProcessSyncChanges, we still return an error. 1270 // in future ProcessSyncChanges, we still return an error.
1103 SyncError error = model()->MergeDataAndStartSyncing( 1271 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1104 syncable::SEARCH_ENGINES,
1105 CreateInitialSyncData(), PassProcessor(), 1272 CreateInitialSyncData(), PassProcessor(),
1106 CreateAndPassSyncErrorFactory()); 1273 CreateAndPassSyncErrorFactory());
1107 ASSERT_FALSE(error.IsSet()); 1274 ASSERT_FALSE(error.IsSet());
1108 1275
1109 SyncChangeList changes; 1276 SyncChangeList changes;
1110 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE, 1277 changes.push_back(CreateTestSyncChange(SyncChange::ACTION_UPDATE,
1111 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com", 1278 CreateTestTemplateURL(ASCIIToUTF16("newkeyword"), "http://new.com",
1112 "key2"))); 1279 "key2")));
1113 processor()->set_erroneous(true); 1280 processor()->set_erroneous(true);
1114 error = model()->ProcessSyncChanges(FROM_HERE, changes); 1281 error = model()->ProcessSyncChanges(FROM_HERE, changes);
1115 EXPECT_TRUE(error.IsSet()); 1282 EXPECT_TRUE(error.IsSet());
1116 } 1283 }
1117 1284
1118 TEST_F(TemplateURLServiceSyncTest, MergeTwiceWithSameSyncData) { 1285 TEST_F(TemplateURLServiceSyncTest, MergeTwiceWithSameSyncData) {
1119 // Ensure that a second merge with the same data as the first does not 1286 // Ensure that a second merge with the same data as the first does not
1120 // actually update the local data. 1287 // actually update the local data.
1121 SyncDataList initial_data; 1288 SyncDataList initial_data;
1122 initial_data.push_back(CreateInitialSyncData()[0]); 1289 initial_data.push_back(CreateInitialSyncData()[0]);
1123 1290
1124 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", 1291 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com",
1125 "key1", 10)); // earlier 1292 "key1", 10)); // earlier
1126 1293
1127 SyncError error = model()->MergeDataAndStartSyncing( 1294 SyncError error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1128 syncable::SEARCH_ENGINES, 1295 initial_data, PassProcessor(), CreateAndPassSyncErrorFactory());
1129 initial_data, PassProcessor(),
1130 CreateAndPassSyncErrorFactory());
1131 ASSERT_FALSE(error.IsSet()); 1296 ASSERT_FALSE(error.IsSet());
1132 1297
1133 // We should have updated the original TemplateURL with Sync's version. 1298 // We should have updated the original TemplateURL with Sync's version.
1134 // Keep a copy of it so we can compare it after we re-merge. 1299 // Keep a copy of it so we can compare it after we re-merge.
1135 TemplateURL* key1_url = model()->GetTemplateURLForGUID("key1"); 1300 TemplateURL* key1_url = model()->GetTemplateURLForGUID("key1");
1136 ASSERT_TRUE(key1_url); 1301 ASSERT_TRUE(key1_url);
1137 scoped_ptr<TemplateURL> updated_turl(new TemplateURL(key1_url->profile(), 1302 scoped_ptr<TemplateURL> updated_turl(new TemplateURL(key1_url->profile(),
1138 key1_url->data())); 1303 key1_url->data()));
1139 EXPECT_EQ(Time::FromTimeT(90), updated_turl->last_modified()); 1304 EXPECT_EQ(Time::FromTimeT(90), updated_turl->last_modified());
1140 1305
1141 // Modify a single field of the initial data. This should not be updated in 1306 // Modify a single field of the initial data. This should not be updated in
1142 // the second merge, as the last_modified timestamp remains the same. 1307 // the second merge, as the last_modified timestamp remains the same.
1143 scoped_ptr<TemplateURL> temp_turl(Deserialize(initial_data[0])); 1308 scoped_ptr<TemplateURL> temp_turl(Deserialize(initial_data[0]));
1144 TemplateURLData data(temp_turl->data()); 1309 TemplateURLData data(temp_turl->data());
1145 data.short_name = ASCIIToUTF16("SomethingDifferent"); 1310 data.short_name = ASCIIToUTF16("SomethingDifferent");
1146 temp_turl.reset(new TemplateURL(temp_turl->profile(), data)); 1311 temp_turl.reset(new TemplateURL(temp_turl->profile(), data));
1147 initial_data.clear(); 1312 initial_data.clear();
1148 initial_data.push_back( 1313 initial_data.push_back(
1149 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl)); 1314 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl));
1150 1315
1151 // Remerge the data again. This simulates shutting down and syncing again 1316 // Remerge the data again. This simulates shutting down and syncing again
1152 // at a different time, but the cloud data has not changed. 1317 // at a different time, but the cloud data has not changed.
1153 model()->StopSyncing(syncable::SEARCH_ENGINES); 1318 model()->StopSyncing(syncable::SEARCH_ENGINES);
1154 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate( 1319 sync_processor_delegate_.reset(new SyncChangeProcessorDelegate(
1155 sync_processor_.get())); 1320 sync_processor_.get()));
1156 error = model()->MergeDataAndStartSyncing( 1321 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1157 syncable::SEARCH_ENGINES, 1322 initial_data, PassProcessor(), CreateAndPassSyncErrorFactory());
1158 initial_data, PassProcessor(),
1159 CreateAndPassSyncErrorFactory());
1160 ASSERT_FALSE(error.IsSet()); 1323 ASSERT_FALSE(error.IsSet());
1161 1324
1162 // Check that the TemplateURL was not modified. 1325 // Check that the TemplateURL was not modified.
1163 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1"); 1326 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1");
1164 ASSERT_TRUE(reupdated_turl); 1327 ASSERT_TRUE(reupdated_turl);
1165 AssertEquals(*updated_turl, *reupdated_turl); 1328 AssertEquals(*updated_turl, *reupdated_turl);
1166 } 1329 }
1167 1330
1168 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) { 1331 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultGUIDArrivesFirst) {
1169 SyncDataList initial_data = CreateInitialSyncData(); 1332 SyncDataList initial_data = CreateInitialSyncData();
1170 // The default search provider should support replacement. 1333 // The default search provider should support replacement.
1171 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key2"), 1334 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key2"),
1172 "http://key2.com/{searchTerms}", "key2", 90)); 1335 "http://key2.com/{searchTerms}", "key2", 90));
1173 initial_data[1] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 1336 initial_data[1] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
1174 model()->MergeDataAndStartSyncing( 1337 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1175 syncable::SEARCH_ENGINES, initial_data, 1338 PassProcessor(), CreateAndPassSyncErrorFactory());
1176 PassProcessor(),
1177 CreateAndPassSyncErrorFactory());
1178 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); 1339 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
1179 1340
1180 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1341 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1181 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); 1342 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1182 ASSERT_TRUE(default_search); 1343 ASSERT_TRUE(default_search);
1183 1344
1184 // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in 1345 // Change kSyncedDefaultSearchProviderGUID to a GUID that does not exist in
1185 // the model yet. Ensure that the default has not changed in any way. 1346 // the model yet. Ensure that the default has not changed in any way.
1186 profile_a()->GetTestingPrefService()->SetString( 1347 profile_a()->GetTestingPrefService()->SetString(
1187 prefs::kSyncedDefaultSearchProviderGUID, "newdefault"); 1348 prefs::kSyncedDefaultSearchProviderGUID, "newdefault");
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1233 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider()); 1394 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
1234 1395
1235 // Now sync the initial data, which will include the search engine entry 1396 // Now sync the initial data, which will include the search engine entry
1236 // destined to become the new default. 1397 // destined to become the new default.
1237 SyncDataList initial_data = CreateInitialSyncData(); 1398 SyncDataList initial_data = CreateInitialSyncData();
1238 // The default search provider should support replacement. 1399 // The default search provider should support replacement.
1239 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key2"), 1400 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key2"),
1240 "http://key2.com/{searchTerms}", "key2", 90)); 1401 "http://key2.com/{searchTerms}", "key2", 90));
1241 initial_data[1] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 1402 initial_data[1] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
1242 1403
1243 model()->MergeDataAndStartSyncing( 1404 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1244 syncable::SEARCH_ENGINES, initial_data, 1405 PassProcessor(), CreateAndPassSyncErrorFactory());
1245 PassProcessor(),
1246 CreateAndPassSyncErrorFactory());
1247 1406
1248 // Ensure that the new default has been set. 1407 // Ensure that the new default has been set.
1249 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1408 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1250 ASSERT_NE(default_search, model()->GetDefaultSearchProvider()); 1409 ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
1251 ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid()); 1410 ASSERT_EQ("key2", model()->GetDefaultSearchProvider()->sync_guid());
1252 } 1411 }
1253 1412
1254 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultAlreadySetOnStartup) { 1413 TEST_F(TemplateURLServiceSyncTest, SyncedDefaultAlreadySetOnStartup) {
1255 // Start with the default set to something in the model before we start 1414 // Start with the default set to something in the model before we start
1256 // syncing. 1415 // syncing.
1257 const char kGUID[] = "initdefault"; 1416 const char kGUID[] = "initdefault";
1258 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"), 1417 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("what"),
1259 "http://thewhat.com/{searchTerms}", 1418 "http://thewhat.com/{searchTerms}",
1260 kGUID)); 1419 kGUID));
1261 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID(kGUID)); 1420 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID(kGUID));
1262 1421
1263 const TemplateURL* default_search = model()->GetDefaultSearchProvider(); 1422 const TemplateURL* default_search = model()->GetDefaultSearchProvider();
1264 ASSERT_TRUE(default_search); 1423 ASSERT_TRUE(default_search);
1265 1424
1266 // Set kSyncedDefaultSearchProviderGUID to the current default. 1425 // Set kSyncedDefaultSearchProviderGUID to the current default.
1267 profile_a()->GetTestingPrefService()->SetString( 1426 profile_a()->GetTestingPrefService()->SetString(
1268 prefs::kSyncedDefaultSearchProviderGUID, kGUID); 1427 prefs::kSyncedDefaultSearchProviderGUID, kGUID);
1269 1428
1270 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider()); 1429 EXPECT_EQ(default_search, model()->GetDefaultSearchProvider());
1271 1430
1272 // Now sync the initial data. 1431 // Now sync the initial data.
1273 SyncDataList initial_data = CreateInitialSyncData(); 1432 SyncDataList initial_data = CreateInitialSyncData();
1274 model()->MergeDataAndStartSyncing( 1433 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1275 syncable::SEARCH_ENGINES, initial_data, 1434 PassProcessor(), CreateAndPassSyncErrorFactory());
1276 PassProcessor(),
1277 CreateAndPassSyncErrorFactory());
1278 1435
1279 // Ensure that the new entries were added and the default has not changed. 1436 // Ensure that the new entries were added and the default has not changed.
1280 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1437 EXPECT_EQ(4U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1281 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider()); 1438 ASSERT_EQ(default_search, model()->GetDefaultSearchProvider());
1282 } 1439 }
1283 1440
1284 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) { 1441 TEST_F(TemplateURLServiceSyncTest, SyncWithManagedDefaultSearch) {
1285 // First start off with a few entries and make sure we can set an unmanaged 1442 // First start off with a few entries and make sure we can set an unmanaged
1286 // default search provider. 1443 // default search provider.
1287 SyncDataList initial_data = CreateInitialSyncData(); 1444 SyncDataList initial_data = CreateInitialSyncData();
1288 model()->MergeDataAndStartSyncing( 1445 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1289 syncable::SEARCH_ENGINES, initial_data, 1446 PassProcessor(), CreateAndPassSyncErrorFactory());
1290 PassProcessor(),
1291 CreateAndPassSyncErrorFactory());
1292 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2")); 1447 model()->SetDefaultSearchProvider(model()->GetTemplateURLForGUID("key2"));
1293 1448
1294 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1449 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1295 ASSERT_FALSE(model()->is_default_search_managed()); 1450 ASSERT_FALSE(model()->is_default_search_managed());
1296 ASSERT_TRUE(model()->GetDefaultSearchProvider()); 1451 ASSERT_TRUE(model()->GetDefaultSearchProvider());
1297 1452
1298 // Change the default search provider to a managed one. 1453 // Change the default search provider to a managed one.
1299 const char kName[] = "manageddefault"; 1454 const char kName[] = "manageddefault";
1300 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}"; 1455 const char kSearchURL[] = "http://manageddefault.com/search?t={searchTerms}";
1301 const char kIconURL[] = "http://manageddefault.com/icon.jpg"; 1456 const char kIconURL[] = "http://manageddefault.com/icon.jpg";
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 "http://key1.com/{searchTerms}", "whateverguid", 10); 1497 "http://key1.com/{searchTerms}", "whateverguid", 10);
1343 model()->Add(default_turl); 1498 model()->Add(default_turl);
1344 model()->SetDefaultSearchProvider(default_turl); 1499 model()->SetDefaultSearchProvider(default_turl);
1345 1500
1346 SyncDataList initial_data = CreateInitialSyncData(); 1501 SyncDataList initial_data = CreateInitialSyncData();
1347 // The key1 entry should be a duplicate of the default. 1502 // The key1 entry should be a duplicate of the default.
1348 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"), 1503 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"),
1349 "http://key1.com/{searchTerms}", "key1", 90)); 1504 "http://key1.com/{searchTerms}", "key1", 90));
1350 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 1505 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
1351 1506
1352 model()->MergeDataAndStartSyncing( 1507 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1353 syncable::SEARCH_ENGINES, 1508 PassProcessor(), CreateAndPassSyncErrorFactory());
1354 initial_data, PassProcessor(),
1355 CreateAndPassSyncErrorFactory());
1356 1509
1357 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size()); 1510 EXPECT_EQ(3U, model()->GetAllSyncData(syncable::SEARCH_ENGINES).size());
1358 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); 1511 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid"));
1359 EXPECT_EQ(model()->GetDefaultSearchProvider(), 1512 EXPECT_EQ(model()->GetDefaultSearchProvider(),
1360 model()->GetTemplateURLForGUID("key1")); 1513 model()->GetTemplateURLForGUID("key1"));
1361 } 1514 }
1362 1515
1363 TEST_F(TemplateURLServiceSyncTest, DeleteBogusData) { 1516 TEST_F(TemplateURLServiceSyncTest, DeleteBogusData) {
1364 // Create a couple of bogus entries to sync. 1517 // Create a couple of bogus entries to sync.
1365 SyncDataList initial_data; 1518 SyncDataList initial_data;
1366 scoped_ptr<TemplateURL> turl( 1519 scoped_ptr<TemplateURL> turl(
1367 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); 1520 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1"));
1368 initial_data.push_back( 1521 initial_data.push_back(
1369 CreateBogusSyncData(*turl, std::string(), turl->sync_guid())); 1522 CreateCustomSyncData(*turl, false, std::string(), turl->sync_guid()));
1370 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); 1523 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com"));
1371 initial_data.push_back( 1524 initial_data.push_back(
1372 CreateBogusSyncData(*turl, turl->url(), std::string())); 1525 CreateCustomSyncData(*turl, false, turl->url(), std::string()));
1373 1526
1374 // Now try to sync the data locally. 1527 // Now try to sync the data locally.
1375 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data, 1528 model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES, initial_data,
1376 PassProcessor(), 1529 PassProcessor(), CreateAndPassSyncErrorFactory());
1377 CreateAndPassSyncErrorFactory());
1378 1530
1379 // Nothing should have been added, and both bogus entries should be marked for 1531 // Nothing should have been added, and both bogus entries should be marked for
1380 // deletion. 1532 // deletion.
1381 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); 1533 EXPECT_EQ(0U, model()->GetTemplateURLs().size());
1382 EXPECT_EQ(2U, processor()->change_list_size()); 1534 EXPECT_EQ(2U, processor()->change_list_size());
1383 ASSERT_TRUE(processor()->contains_guid("key1")); 1535 ASSERT_TRUE(processor()->contains_guid("key1"));
1384 EXPECT_EQ(SyncChange::ACTION_DELETE, 1536 EXPECT_EQ(SyncChange::ACTION_DELETE,
1385 processor()->change_for_guid("key1").change_type()); 1537 processor()->change_for_guid("key1").change_type());
1386 ASSERT_TRUE(processor()->contains_guid(std::string())); 1538 ASSERT_TRUE(processor()->contains_guid(std::string()));
1387 EXPECT_EQ(SyncChange::ACTION_DELETE, 1539 EXPECT_EQ(SyncChange::ACTION_DELETE,
1388 processor()->change_for_guid(std::string()).change_type()); 1540 processor()->change_for_guid(std::string()).change_type());
1389 } 1541 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698