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

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

Issue 2290503003: Remove use of stl_util in search_engines. (Closed)
Patch Set: fix broken test Created 4 years, 3 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
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // involve syncing two models. 199 // involve syncing two models.
200 TemplateURLService* model_a() { return test_util_a_->model(); } 200 TemplateURLService* model_a() { return test_util_a_->model(); }
201 TemplateURLService* model_b() { return test_util_b_->model(); } 201 TemplateURLService* model_b() { return test_util_b_->model(); }
202 TestingProfile* profile_a() { return test_util_a_->profile(); } 202 TestingProfile* profile_a() { return test_util_a_->profile(); }
203 TestChangeProcessor* processor() { return sync_processor_.get(); } 203 TestChangeProcessor* processor() { return sync_processor_.get(); }
204 std::unique_ptr<syncer::SyncChangeProcessor> PassProcessor(); 204 std::unique_ptr<syncer::SyncChangeProcessor> PassProcessor();
205 std::unique_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory(); 205 std::unique_ptr<syncer::SyncErrorFactory> CreateAndPassSyncErrorFactory();
206 206
207 // Creates a TemplateURL with some test values. The caller owns the returned 207 // Creates a TemplateURL with some test values. The caller owns the returned
208 // TemplateURL*. 208 // TemplateURL*.
209 TemplateURL* CreateTestTemplateURL(const base::string16& keyword, 209 std::unique_ptr<TemplateURL> CreateTestTemplateURL(
210 const std::string& url, 210 const base::string16& keyword,
211 const std::string& guid = std::string(), 211 const std::string& url,
212 time_t last_mod = 100, 212 const std::string& guid = std::string(),
213 bool safe_for_autoreplace = false, 213 time_t last_mod = 100,
214 bool created_by_policy = false, 214 bool safe_for_autoreplace = false,
215 int prepopulate_id = 999999) const; 215 bool created_by_policy = false,
216 int prepopulate_id = 999999) const;
216 217
217 // Verifies the two TemplateURLs are equal. 218 // Verifies the two TemplateURLs are equal.
218 // TODO(stevet): Share this with TemplateURLServiceTest. 219 // TODO(stevet): Share this with TemplateURLServiceTest.
219 void AssertEquals(const TemplateURL& expected, 220 void AssertEquals(const TemplateURL& expected,
220 const TemplateURL& actual) const; 221 const TemplateURL& actual) const;
221 222
222 // Expect that two syncer::SyncDataLists have equal contents, in terms of the 223 // Expect that two syncer::SyncDataLists have equal contents, in terms of the
223 // sync_guid, keyword, and url fields. 224 // sync_guid, keyword, and url fields.
224 void AssertEquals(const syncer::SyncDataList& data1, 225 void AssertEquals(const syncer::SyncDataList& data1,
225 const syncer::SyncDataList& data2) const; 226 const syncer::SyncDataList& data2) const;
226 227
227 // Convenience helper for creating SyncChanges. Takes ownership of |turl|. 228 // Convenience helper for creating SyncChanges. Takes ownership of |turl|.
228 syncer::SyncChange CreateTestSyncChange( 229 syncer::SyncChange CreateTestSyncChange(
229 syncer::SyncChange::SyncChangeType type, 230 syncer::SyncChange::SyncChangeType type,
230 TemplateURL* turl) const; 231 std::unique_ptr<TemplateURL> turl) const;
231 232
232 // Helper that creates some initial sync data. We cheat a little by specifying 233 // Helper that creates some initial sync data. We cheat a little by specifying
233 // GUIDs for easy identification later. We also make the last_modified times 234 // GUIDs for easy identification later. We also make the last_modified times
234 // slightly older than CreateTestTemplateURL's default, to test conflict 235 // slightly older than CreateTestTemplateURL's default, to test conflict
235 // resolution. 236 // resolution.
236 syncer::SyncDataList CreateInitialSyncData() const; 237 syncer::SyncDataList CreateInitialSyncData() const;
237 238
238 // Syntactic sugar. 239 // Syntactic sugar.
239 std::unique_ptr<TemplateURL> Deserialize(const syncer::SyncData& sync_data); 240 std::unique_ptr<TemplateURL> Deserialize(const syncer::SyncData& sync_data);
240 241
241 // Creates a new TemplateURL copying the fields of |turl| but replacing 242 // Creates a new TemplateURL copying the fields of |turl| but replacing
242 // the |url| and |guid| and initializing the date_created and last_modified 243 // the |url| and |guid| and initializing the date_created and last_modified
243 // timestamps to a default value of 100. The caller owns the returned 244 // timestamps to a default value of 100.
244 // TemplateURL*. 245 std::unique_ptr<TemplateURL> CopyTemplateURL(const TemplateURLData* turl,
245 TemplateURL* CopyTemplateURL(const TemplateURLData* turl, 246 const std::string& url,
246 const std::string& url, 247 const std::string& guid);
247 const std::string& guid);
248 248
249 protected: 249 protected:
250 content::TestBrowserThreadBundle thread_bundle_; 250 content::TestBrowserThreadBundle thread_bundle_;
251 // We keep two TemplateURLServices to test syncing between them. 251 // We keep two TemplateURLServices to test syncing between them.
252 std::unique_ptr<TemplateURLServiceTestUtil> test_util_a_; 252 std::unique_ptr<TemplateURLServiceTestUtil> test_util_a_;
253 std::unique_ptr<TemplateURLServiceTestUtil> test_util_b_; 253 std::unique_ptr<TemplateURLServiceTestUtil> test_util_b_;
254 254
255 // Our dummy ChangeProcessor used to inspect changes pushed to Sync. 255 // Our dummy ChangeProcessor used to inspect changes pushed to Sync.
256 std::unique_ptr<TestChangeProcessor> sync_processor_; 256 std::unique_ptr<TestChangeProcessor> sync_processor_;
257 std::unique_ptr<syncer::SyncChangeProcessorWrapperForTest> 257 std::unique_ptr<syncer::SyncChangeProcessorWrapperForTest>
(...skipping 28 matching lines...) Expand all
286 TemplateURLServiceSyncTest::PassProcessor() { 286 TemplateURLServiceSyncTest::PassProcessor() {
287 return std::move(sync_processor_wrapper_); 287 return std::move(sync_processor_wrapper_);
288 } 288 }
289 289
290 std::unique_ptr<syncer::SyncErrorFactory> 290 std::unique_ptr<syncer::SyncErrorFactory>
291 TemplateURLServiceSyncTest::CreateAndPassSyncErrorFactory() { 291 TemplateURLServiceSyncTest::CreateAndPassSyncErrorFactory() {
292 return std::unique_ptr<syncer::SyncErrorFactory>( 292 return std::unique_ptr<syncer::SyncErrorFactory>(
293 new syncer::SyncErrorFactoryMock()); 293 new syncer::SyncErrorFactoryMock());
294 } 294 }
295 295
296 TemplateURL* TemplateURLServiceSyncTest::CreateTestTemplateURL( 296 std::unique_ptr<TemplateURL> TemplateURLServiceSyncTest::CreateTestTemplateURL(
297 const base::string16& keyword, 297 const base::string16& keyword,
298 const std::string& url, 298 const std::string& url,
299 const std::string& guid, 299 const std::string& guid,
300 time_t last_mod, 300 time_t last_mod,
301 bool safe_for_autoreplace, 301 bool safe_for_autoreplace,
302 bool created_by_policy, 302 bool created_by_policy,
303 int prepopulate_id) const { 303 int prepopulate_id) const {
304 TemplateURLData data; 304 TemplateURLData data;
305 data.SetShortName(ASCIIToUTF16("unittest")); 305 data.SetShortName(ASCIIToUTF16("unittest"));
306 data.SetKeyword(keyword); 306 data.SetKeyword(keyword);
307 data.SetURL(url); 307 data.SetURL(url);
308 data.favicon_url = GURL("http://favicon.url"); 308 data.favicon_url = GURL("http://favicon.url");
309 data.safe_for_autoreplace = safe_for_autoreplace; 309 data.safe_for_autoreplace = safe_for_autoreplace;
310 data.date_created = Time::FromTimeT(100); 310 data.date_created = Time::FromTimeT(100);
311 data.last_modified = Time::FromTimeT(last_mod); 311 data.last_modified = Time::FromTimeT(last_mod);
312 data.created_by_policy = created_by_policy; 312 data.created_by_policy = created_by_policy;
313 data.prepopulate_id = prepopulate_id; 313 data.prepopulate_id = prepopulate_id;
314 if (!guid.empty()) 314 if (!guid.empty())
315 data.sync_guid = guid; 315 data.sync_guid = guid;
316 return new TemplateURL(data); 316 return base::MakeUnique<TemplateURL>(data);
317 } 317 }
318 318
319 void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected, 319 void TemplateURLServiceSyncTest::AssertEquals(const TemplateURL& expected,
320 const TemplateURL& actual) const { 320 const TemplateURL& actual) const {
321 ASSERT_EQ(expected.short_name(), actual.short_name()); 321 ASSERT_EQ(expected.short_name(), actual.short_name());
322 ASSERT_EQ(expected.keyword(), actual.keyword()); 322 ASSERT_EQ(expected.keyword(), actual.keyword());
323 ASSERT_EQ(expected.url(), actual.url()); 323 ASSERT_EQ(expected.url(), actual.url());
324 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url()); 324 ASSERT_EQ(expected.suggestions_url(), actual.suggestions_url());
325 ASSERT_EQ(expected.favicon_url(), actual.favicon_url()); 325 ASSERT_EQ(expected.favicon_url(), actual.favicon_url());
326 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list()); 326 ASSERT_EQ(expected.show_in_default_list(), actual.show_in_default_list());
(...skipping 16 matching lines...) Expand all
343 ASSERT_EQ(GetKeyword(iter1->second), GetKeyword(iter2->second)); 343 ASSERT_EQ(GetKeyword(iter1->second), GetKeyword(iter2->second));
344 ASSERT_EQ(GetURL(iter1->second), GetURL(iter2->second)); 344 ASSERT_EQ(GetURL(iter1->second), GetURL(iter2->second));
345 map2.erase(iter2); 345 map2.erase(iter2);
346 } 346 }
347 } 347 }
348 EXPECT_EQ(0U, map2.size()); 348 EXPECT_EQ(0U, map2.size());
349 } 349 }
350 350
351 syncer::SyncChange TemplateURLServiceSyncTest::CreateTestSyncChange( 351 syncer::SyncChange TemplateURLServiceSyncTest::CreateTestSyncChange(
352 syncer::SyncChange::SyncChangeType type, 352 syncer::SyncChange::SyncChangeType type,
353 TemplateURL* turl) const { 353 std::unique_ptr<TemplateURL> turl) const {
Nico 2016/09/02 16:22:22 (unrelated to your change, this feels like a somew
Avi (use Gerrit) 2016/09/02 17:25:38 So it can reuse CreateTestTemplateURL ?
Nico 2016/09/02 17:34:49 Sure, but that function is local to this file and
Peter Kasting 2016/09/02 19:22:22 Yeah, this probably should have taken a const Temp
354 // We take control of the TemplateURL so make sure it's cleaned up after
355 // we create data out of it.
356 std::unique_ptr<TemplateURL> scoped_turl(turl);
357 return syncer::SyncChange( 354 return syncer::SyncChange(
358 FROM_HERE, 355 FROM_HERE, type,
359 type, 356 TemplateURLService::CreateSyncDataFromTemplateURL(*turl));
360 TemplateURLService::CreateSyncDataFromTemplateURL(*scoped_turl));
361 } 357 }
362 358
363 syncer::SyncDataList TemplateURLServiceSyncTest::CreateInitialSyncData() const { 359 syncer::SyncDataList TemplateURLServiceSyncTest::CreateInitialSyncData() const {
364 syncer::SyncDataList list; 360 syncer::SyncDataList list;
365 361
366 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL( 362 std::unique_ptr<TemplateURL> turl = CreateTestTemplateURL(
367 ASCIIToUTF16("key1"), "http://key1.com", "key1", 90)); 363 ASCIIToUTF16("key1"), "http://key1.com", "key1", 90);
368 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); 364 list.push_back(
369 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", 365 TemplateURLService::CreateSyncDataFromTemplateURL(*turl.get()));
370 "key2", 90)); 366 turl = CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com", "key2",
371 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); 367 90);
372 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", 368 list.push_back(
373 "key3", 90)); 369 TemplateURLService::CreateSyncDataFromTemplateURL(*turl.get()));
374 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); 370 turl = CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com", "key3",
371 90);
372 list.push_back(
373 TemplateURLService::CreateSyncDataFromTemplateURL(*turl.get()));
375 374
376 return list; 375 return list;
377 } 376 }
378 377
379 std::unique_ptr<TemplateURL> TemplateURLServiceSyncTest::Deserialize( 378 std::unique_ptr<TemplateURL> TemplateURLServiceSyncTest::Deserialize(
380 const syncer::SyncData& sync_data) { 379 const syncer::SyncData& sync_data) {
381 syncer::SyncChangeList dummy; 380 syncer::SyncChangeList dummy;
382 TestTemplateURLServiceClient client; 381 TestTemplateURLServiceClient client;
383 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( 382 return TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData(
384 &client, NULL, SearchTermsData(), NULL, sync_data, &dummy); 383 &client, NULL, SearchTermsData(), NULL, sync_data, &dummy);
385 } 384 }
386 385
387 TemplateURL* TemplateURLServiceSyncTest::CopyTemplateURL( 386 std::unique_ptr<TemplateURL> TemplateURLServiceSyncTest::CopyTemplateURL(
388 const TemplateURLData* turl, 387 const TemplateURLData* turl,
389 const std::string& url, 388 const std::string& url,
390 const std::string& guid) { 389 const std::string& guid) {
391 TemplateURLData data = *turl; 390 TemplateURLData data = *turl;
392 data.SetURL(url); 391 data.SetURL(url);
393 data.date_created = Time::FromTimeT(100); 392 data.date_created = Time::FromTimeT(100);
394 data.last_modified = Time::FromTimeT(100); 393 data.last_modified = Time::FromTimeT(100);
395 data.sync_guid = guid; 394 data.sync_guid = guid;
396 return new TemplateURL(data); 395 return base::MakeUnique<TemplateURL>(data);
397 } 396 }
398 397
399 // Actual tests --------------------------------------------------------------- 398 // Actual tests ---------------------------------------------------------------
400 399
401 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) { 400 TEST_F(TemplateURLServiceSyncTest, SerializeDeserialize) {
402 // Create a TemplateURL and convert it into a sync specific type. 401 // Create a TemplateURL and convert it into a sync specific type.
403 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL( 402 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL(
404 ASCIIToUTF16("unittest"), "http://www.unittest.com/")); 403 ASCIIToUTF16("unittest"), "http://www.unittest.com/"));
405 syncer::SyncData sync_data = 404 syncer::SyncData sync_data =
406 TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 405 TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
445 std::string guid = GetGUID(*iter); 444 std::string guid = GetGUID(*iter);
446 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); 445 const TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
447 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); 446 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter));
448 AssertEquals(*service_turl, *deserialized); 447 AssertEquals(*service_turl, *deserialized);
449 } 448 }
450 } 449 }
451 450
452 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) { 451 TEST_F(TemplateURLServiceSyncTest, GetAllSyncDataNoManagedEngines) {
453 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); 452 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com"));
454 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); 453 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com"));
455 TemplateURL* managed_turl = CreateTestTemplateURL(ASCIIToUTF16("key3"), 454 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key3"), "http://key3.com",
456 "http://key3.com", std::string(), 100, false, true); 455 std::string(), 100, false, true));
457 model()->Add(managed_turl);
458 syncer::SyncDataList all_sync_data = 456 syncer::SyncDataList all_sync_data =
459 model()->GetAllSyncData(syncer::SEARCH_ENGINES); 457 model()->GetAllSyncData(syncer::SEARCH_ENGINES);
460 458
461 EXPECT_EQ(2U, all_sync_data.size()); 459 EXPECT_EQ(2U, all_sync_data.size());
462 460
463 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin(); 461 for (syncer::SyncDataList::const_iterator iter = all_sync_data.begin();
464 iter != all_sync_data.end(); ++iter) { 462 iter != all_sync_data.end(); ++iter) {
465 std::string guid = GetGUID(*iter); 463 std::string guid = GetGUID(*iter);
466 TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid); 464 TemplateURL* service_turl = model()->GetTemplateURLForGUID(guid);
467 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter)); 465 std::unique_ptr<TemplateURL> deserialized(Deserialize(*iter));
468 ASSERT_FALSE(service_turl->created_by_policy()); 466 ASSERT_FALSE(service_turl->created_by_policy());
469 AssertEquals(*service_turl, *deserialized); 467 AssertEquals(*service_turl, *deserialized);
470 } 468 }
471 } 469 }
472 470
473 TEST_F(TemplateURLServiceSyncTest, UniquifyKeyword) { 471 TEST_F(TemplateURLServiceSyncTest, UniquifyKeyword) {
474 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com")); 472 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com"));
475 // Create a key that conflicts with something in the model. 473 // Create a key that conflicts with something in the model.
476 std::unique_ptr<TemplateURL> turl( 474 std::unique_ptr<TemplateURL> turl =
477 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com", "xyz")); 475 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com", "xyz");
478 base::string16 new_keyword = model()->UniquifyKeyword(*turl, false); 476 base::string16 new_keyword = model()->UniquifyKeyword(*turl, false);
479 EXPECT_EQ(ASCIIToUTF16("new.com"), new_keyword); 477 EXPECT_EQ(ASCIIToUTF16("new.com"), new_keyword);
480 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); 478 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));
481 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("new.com"), "http://new.com", 479 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("new.com"), "http://new.com",
482 "xyz")); 480 "xyz"));
483 481
484 // Test a second collision. This time it should be resolved by actually 482 // Test a second collision. This time it should be resolved by actually
485 // modifying the original keyword, since the autogenerated keyword is already 483 // modifying the original keyword, since the autogenerated keyword is already
486 // used. 484 // used.
487 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com")); 485 turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com");
488 new_keyword = model()->UniquifyKeyword(*turl, false); 486 new_keyword = model()->UniquifyKeyword(*turl, false);
489 EXPECT_EQ(ASCIIToUTF16("key1_"), new_keyword); 487 EXPECT_EQ(ASCIIToUTF16("key1_"), new_keyword);
490 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); 488 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));
491 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1_"), "http://new.com")); 489 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1_"), "http://new.com"));
492 490
493 // Test a third collision. This should collide on both the autogenerated 491 // Test a third collision. This should collide on both the autogenerated
494 // keyword and the first uniquification attempt. 492 // keyword and the first uniquification attempt.
495 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com")); 493 turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://new.com");
496 new_keyword = model()->UniquifyKeyword(*turl, false); 494 new_keyword = model()->UniquifyKeyword(*turl, false);
497 EXPECT_EQ(ASCIIToUTF16("key1__"), new_keyword); 495 EXPECT_EQ(ASCIIToUTF16("key1__"), new_keyword);
498 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); 496 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));
499 497
500 // If we force the method, it should uniquify the keyword even if it is 498 // If we force the method, it should uniquify the keyword even if it is
501 // currently unique, and skip the host-based autogenerated keyword. 499 // currently unique, and skip the host-based autogenerated keyword.
502 turl.reset( 500 turl = CreateTestTemplateURL(ASCIIToUTF16("unique"), "http://unique.com");
503 CreateTestTemplateURL(ASCIIToUTF16("unique"), "http://unique.com"));
504 new_keyword = model()->UniquifyKeyword(*turl, true); 501 new_keyword = model()->UniquifyKeyword(*turl, true);
505 EXPECT_EQ(ASCIIToUTF16("unique_"), new_keyword); 502 EXPECT_EQ(ASCIIToUTF16("unique_"), new_keyword);
506 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); 503 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword));
507 } 504 }
508 505
509 TEST_F(TemplateURLServiceSyncTest, IsLocalTemplateURLBetter) { 506 TEST_F(TemplateURLServiceSyncTest, IsLocalTemplateURLBetter) {
510 // Test some edge cases of this function. 507 // Test some edge cases of this function.
511 const struct { 508 const struct {
512 time_t local_time; 509 time_t local_time;
513 time_t sync_time; 510 time_t sync_time;
514 bool local_is_default; 511 bool local_is_default;
515 bool local_created_by_policy; 512 bool local_created_by_policy;
516 bool expected_result; 513 bool expected_result;
517 } test_cases[] = { 514 } test_cases[] = {
518 // Sync is better by timestamp but local is Default. 515 // Sync is better by timestamp but local is Default.
519 {10, 100, true, false, true}, 516 {10, 100, true, false, true},
520 // Sync is better by timestamp but local is Create by Policy. 517 // Sync is better by timestamp but local is Create by Policy.
521 {10, 100, false, true, true}, 518 {10, 100, false, true, true},
522 // Tie. Sync wins. 519 // Tie. Sync wins.
523 {100, 100, false, false, false}, 520 {100, 100, false, false, false},
524 }; 521 };
525 522
526 for (size_t i = 0; i < arraysize(test_cases); ++i) { 523 for (size_t i = 0; i < arraysize(test_cases); ++i) {
527 TemplateURL* local_turl = CreateTestTemplateURL( 524 TemplateURL* local_turl = model()->Add(CreateTestTemplateURL(
528 ASCIIToUTF16("localkey"), "www.local.com", "localguid", 525 ASCIIToUTF16("localkey"), "www.local.com", "localguid",
529 test_cases[i].local_time, true, test_cases[i].local_created_by_policy); 526 test_cases[i].local_time, true, test_cases[i].local_created_by_policy));
530 model()->Add(local_turl);
531 if (test_cases[i].local_is_default) 527 if (test_cases[i].local_is_default)
532 model()->SetUserSelectedDefaultSearchProvider(local_turl); 528 model()->SetUserSelectedDefaultSearchProvider(local_turl);
533 529
534 std::unique_ptr<TemplateURL> sync_turl( 530 std::unique_ptr<TemplateURL> sync_turl(
535 CreateTestTemplateURL(ASCIIToUTF16("synckey"), "www.sync.com", 531 CreateTestTemplateURL(ASCIIToUTF16("synckey"), "www.sync.com",
536 "syncguid", test_cases[i].sync_time)); 532 "syncguid", test_cases[i].sync_time));
537 EXPECT_EQ(test_cases[i].expected_result, 533 EXPECT_EQ(test_cases[i].expected_result,
538 model()->IsLocalTemplateURLBetter(local_turl, sync_turl.get())); 534 model()->IsLocalTemplateURLBetter(local_turl, sync_turl.get()));
539 535
540 // Undo the changes. 536 // Undo the changes.
541 if (test_cases[i].local_is_default) 537 if (test_cases[i].local_is_default)
542 model()->SetUserSelectedDefaultSearchProvider(NULL); 538 model()->SetUserSelectedDefaultSearchProvider(NULL);
543 model()->Remove(local_turl); 539 model()->Remove(local_turl);
544 } 540 }
545 } 541 }
546 542
547 TEST_F(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict) { 543 TEST_F(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict) {
548 // This tests cases where neither the sync nor the local TemplateURL are 544 // This tests cases where neither the sync nor the local TemplateURL are
549 // marked safe_for_autoreplace. 545 // marked safe_for_autoreplace.
550 546
551 // Create a keyword that conflicts, and make it older. Sync keyword is 547 // Create a keyword that conflicts, and make it older. Sync keyword is
552 // uniquified, and a syncer::SyncChange is added. 548 // uniquified, and a syncer::SyncChange is added.
553 base::string16 original_turl_keyword = ASCIIToUTF16("key1"); 549 base::string16 original_turl_keyword = ASCIIToUTF16("key1");
554 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword, 550 TemplateURL* original_turl = model()->Add(CreateTestTemplateURL(
555 "http://key1.com", std::string(), 9000); 551 original_turl_keyword, "http://key1.com", std::string(), 9000));
556 model()->Add(original_turl);
557 std::unique_ptr<TemplateURL> sync_turl(CreateTestTemplateURL( 552 std::unique_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(
558 original_turl_keyword, "http://new.com", "remote", 8999)); 553 original_turl_keyword, "http://new.com", "remote", 8999));
559 syncer::SyncChangeList changes; 554 syncer::SyncChangeList changes;
560 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); 555 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
561 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); 556 EXPECT_NE(original_turl_keyword, sync_turl->keyword());
562 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); 557 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
563 ASSERT_EQ(1U, changes.size()); 558 ASSERT_EQ(1U, changes.size());
564 EXPECT_EQ("remote", GetGUID(changes[0].sync_data())); 559 EXPECT_EQ("remote", GetGUID(changes[0].sync_data()));
565 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); 560 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type());
566 changes.clear(); 561 changes.clear();
567 model()->Remove(original_turl); 562 model()->Remove(original_turl);
568 563
569 // Sync is newer. Original TemplateURL keyword is uniquified. A SyncChange 564 // Sync is newer. Original TemplateURL keyword is uniquified. A SyncChange
570 // is added (which in a normal run would be deleted by PruneSyncChanges() when 565 // is added (which in a normal run would be deleted by PruneSyncChanges() when
571 // the local GUID doesn't appear in the sync GUID list). Also ensure that 566 // the local GUID doesn't appear in the sync GUID list). Also ensure that
572 // this does not change the safe_for_autoreplace flag or the TemplateURLID in 567 // this does not change the safe_for_autoreplace flag or the TemplateURLID in
573 // the original. 568 // the original.
574 original_turl = CreateTestTemplateURL(original_turl_keyword, 569 original_turl = model()->Add(CreateTestTemplateURL(
575 "http://key1.com", "local", 9000); 570 original_turl_keyword, "http://key1.com", "local", 9000));
576 model()->Add(original_turl);
577 TemplateURLID original_id = original_turl->id(); 571 TemplateURLID original_id = original_turl->id();
578 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", 572 sync_turl = CreateTestTemplateURL(original_turl_keyword, "http://new.com",
579 std::string(), 9001)); 573 std::string(), 9001);
580 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); 574 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
581 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); 575 EXPECT_EQ(original_turl_keyword, sync_turl->keyword());
582 EXPECT_NE(original_turl_keyword, original_turl->keyword()); 576 EXPECT_NE(original_turl_keyword, original_turl->keyword());
583 EXPECT_FALSE(original_turl->safe_for_autoreplace()); 577 EXPECT_FALSE(original_turl->safe_for_autoreplace());
584 EXPECT_EQ(original_id, original_turl->id()); 578 EXPECT_EQ(original_id, original_turl->id());
585 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); 579 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword));
586 ASSERT_EQ(1U, changes.size()); 580 ASSERT_EQ(1U, changes.size());
587 EXPECT_EQ("local", GetGUID(changes[0].sync_data())); 581 EXPECT_EQ("local", GetGUID(changes[0].sync_data()));
588 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); 582 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type());
589 changes.clear(); 583 changes.clear();
590 model()->Remove(original_turl); 584 model()->Remove(original_turl);
591 585
592 // Equal times. Same result as above. Sync left alone, original uniquified so 586 // Equal times. Same result as above. Sync left alone, original uniquified so
593 // sync_turl can fit. 587 // sync_turl can fit.
594 original_turl = CreateTestTemplateURL(original_turl_keyword, 588 original_turl = model()->Add(CreateTestTemplateURL(
595 "http://key1.com", "local2", 9000); 589 original_turl_keyword, "http://key1.com", "local2", 9000));
596 model()->Add(original_turl); 590 sync_turl = CreateTestTemplateURL(original_turl_keyword, "http://new.com",
597 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", 591 std::string(), 9000);
598 std::string(), 9000));
599 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); 592 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
600 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); 593 EXPECT_EQ(original_turl_keyword, sync_turl->keyword());
601 EXPECT_NE(original_turl_keyword, original_turl->keyword()); 594 EXPECT_NE(original_turl_keyword, original_turl->keyword());
602 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); 595 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword));
603 ASSERT_EQ(1U, changes.size()); 596 ASSERT_EQ(1U, changes.size());
604 EXPECT_EQ("local2", GetGUID(changes[0].sync_data())); 597 EXPECT_EQ("local2", GetGUID(changes[0].sync_data()));
605 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); 598 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type());
606 changes.clear(); 599 changes.clear();
607 model()->Remove(original_turl); 600 model()->Remove(original_turl);
608 601
609 // Sync is newer, but original TemplateURL is created by policy, so it wins. 602 // Sync is newer, but original TemplateURL is created by policy, so it wins.
610 // Sync keyword is uniquified, and a syncer::SyncChange is added. 603 // Sync keyword is uniquified, and a syncer::SyncChange is added.
611 original_turl = CreateTestTemplateURL(original_turl_keyword, 604 original_turl = model()->Add(
612 "http://key1.com", std::string(), 9000, false, true); 605 CreateTestTemplateURL(original_turl_keyword, "http://key1.com",
613 model()->Add(original_turl); 606 std::string(), 9000, false, true));
614 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", 607 sync_turl = CreateTestTemplateURL(original_turl_keyword, "http://new.com",
615 "remote2", 9999)); 608 "remote2", 9999);
616 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); 609 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes);
617 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); 610 EXPECT_NE(original_turl_keyword, sync_turl->keyword());
618 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); 611 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
619 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); 612 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
620 ASSERT_EQ(1U, changes.size()); 613 ASSERT_EQ(1U, changes.size());
621 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data())); 614 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data()));
622 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); 615 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type());
623 changes.clear(); 616 changes.clear();
624 model()->Remove(original_turl); 617 model()->Remove(original_turl);
625 } 618 }
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 EXPECT_EQ(6, merge_result.num_items_after_association()); 696 EXPECT_EQ(6, merge_result.num_items_after_association());
704 } 697 }
705 698
706 TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) { 699 TEST_F(TemplateURLServiceSyncTest, MergeSyncIsTheSame) {
707 // The local data is the same as the sync data merged in. i.e. - There have 700 // The local data is the same as the sync data merged in. i.e. - There have
708 // been no changes since the last time we synced. Even the last_modified 701 // been no changes since the last time we synced. Even the last_modified
709 // timestamps are the same. 702 // timestamps are the same.
710 syncer::SyncDataList initial_data = CreateInitialSyncData(); 703 syncer::SyncDataList initial_data = CreateInitialSyncData();
711 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); 704 for (syncer::SyncDataList::const_iterator iter = initial_data.begin();
712 iter != initial_data.end(); ++iter) { 705 iter != initial_data.end(); ++iter) {
713 std::unique_ptr<TemplateURL> converted(Deserialize(*iter)); 706 model()->Add(Deserialize(*iter));
714 model()->Add(converted.release());
715 } 707 }
716 708
717 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 709 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
718 syncer::SEARCH_ENGINES, initial_data, 710 syncer::SEARCH_ENGINES, initial_data,
719 PassProcessor(), CreateAndPassSyncErrorFactory()); 711 PassProcessor(), CreateAndPassSyncErrorFactory());
720 712
721 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); 713 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
722 for (syncer::SyncDataList::const_iterator iter = initial_data.begin(); 714 for (syncer::SyncDataList::const_iterator iter = initial_data.begin();
723 iter != initial_data.end(); ++iter) { 715 iter != initial_data.end(); ++iter) {
724 std::string guid = GetGUID(*iter); 716 std::string guid = GetGUID(*iter);
725 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid)); 717 EXPECT_TRUE(model()->GetTemplateURLForGUID(guid));
726 } 718 }
727 EXPECT_EQ(0U, processor()->change_list_size()); 719 EXPECT_EQ(0U, processor()->change_list_size());
728 720
729 // Locally everything should remain the same. 721 // Locally everything should remain the same.
730 EXPECT_EQ(0, merge_result.num_items_added()); 722 EXPECT_EQ(0, merge_result.num_items_added());
731 EXPECT_EQ(0, merge_result.num_items_modified()); 723 EXPECT_EQ(0, merge_result.num_items_modified());
732 EXPECT_EQ(0, merge_result.num_items_deleted()); 724 EXPECT_EQ(0, merge_result.num_items_deleted());
733 EXPECT_EQ(3, merge_result.num_items_before_association()); 725 EXPECT_EQ(3, merge_result.num_items_before_association());
734 EXPECT_EQ(3, merge_result.num_items_after_association()); 726 EXPECT_EQ(3, merge_result.num_items_after_association());
735 } 727 }
736 728
737 TEST_F(TemplateURLServiceSyncTest, MergeUpdateFromSync) { 729 TEST_F(TemplateURLServiceSyncTest, MergeUpdateFromSync) {
738 // The local data is the same as the sync data merged in, but timestamps have 730 // The local data is the same as the sync data merged in, but timestamps have
739 // changed. Ensure the right fields are merged in. 731 // changed. Ensure the right fields are merged in.
740 syncer::SyncDataList initial_data; 732 syncer::SyncDataList initial_data;
741 TemplateURL* turl1 = CreateTestTemplateURL(ASCIIToUTF16("abc.com"), 733 TemplateURL* turl1 = model()->Add(CreateTestTemplateURL(
742 "http://abc.com", "abc", 9000); 734 ASCIIToUTF16("abc.com"), "http://abc.com", "abc", 9000));
743 model()->Add(turl1); 735 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("xyz.com"), "http://xyz.com",
744 TemplateURL* turl2 = CreateTestTemplateURL(ASCIIToUTF16("xyz.com"), 736 "xyz", 9000));
745 "http://xyz.com", "xyz", 9000);
746 model()->Add(turl2);
747 737
748 std::unique_ptr<TemplateURL> turl1_newer(CreateTestTemplateURL( 738 std::unique_ptr<TemplateURL> turl1_newer = CreateTestTemplateURL(
749 ASCIIToUTF16("abc.com"), "http://abc.ca", "abc", 9999)); 739 ASCIIToUTF16("abc.com"), "http://abc.ca", "abc", 9999);
750 initial_data.push_back( 740 initial_data.push_back(
751 TemplateURLService::CreateSyncDataFromTemplateURL(*turl1_newer)); 741 TemplateURLService::CreateSyncDataFromTemplateURL(*turl1_newer));
752 742
753 std::unique_ptr<TemplateURL> turl2_older(CreateTestTemplateURL( 743 std::unique_ptr<TemplateURL> turl2_older = CreateTestTemplateURL(
754 ASCIIToUTF16("xyz.com"), "http://xyz.ca", "xyz", 8888)); 744 ASCIIToUTF16("xyz.com"), "http://xyz.ca", "xyz", 8888);
755 initial_data.push_back( 745 initial_data.push_back(
756 TemplateURLService::CreateSyncDataFromTemplateURL(*turl2_older)); 746 TemplateURLService::CreateSyncDataFromTemplateURL(*turl2_older));
757 747
758 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 748 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
759 syncer::SEARCH_ENGINES, initial_data, 749 syncer::SEARCH_ENGINES, initial_data,
760 PassProcessor(), CreateAndPassSyncErrorFactory()); 750 PassProcessor(), CreateAndPassSyncErrorFactory());
761 751
762 // Both were local updates, so we expect the same count. 752 // Both were local updates, so we expect the same count.
763 EXPECT_EQ(2U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); 753 EXPECT_EQ(2U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
764 754
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
1059 } 1049 }
1060 1050
1061 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { 1051 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) {
1062 // Ensure that ProcessTemplateURLChange is called and pushes the correct 1052 // Ensure that ProcessTemplateURLChange is called and pushes the correct
1063 // changes to Sync whenever local changes are made to TemplateURLs. 1053 // changes to Sync whenever local changes are made to TemplateURLs.
1064 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, 1054 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES,
1065 CreateInitialSyncData(), PassProcessor(), 1055 CreateInitialSyncData(), PassProcessor(),
1066 CreateAndPassSyncErrorFactory()); 1056 CreateAndPassSyncErrorFactory());
1067 1057
1068 // Add a new search engine. 1058 // Add a new search engine.
1069 TemplateURL* new_turl = 1059 model()->Add(
1070 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"); 1060 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"));
1071 model()->Add(new_turl);
1072 EXPECT_EQ(1U, processor()->change_list_size()); 1061 EXPECT_EQ(1U, processor()->change_list_size());
1073 ASSERT_TRUE(processor()->contains_guid("new")); 1062 ASSERT_TRUE(processor()->contains_guid("new"));
1074 syncer::SyncChange change = processor()->change_for_guid("new"); 1063 syncer::SyncChange change = processor()->change_for_guid("new");
1075 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, change.change_type()); 1064 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, change.change_type());
1076 EXPECT_EQ("baidu", GetKeyword(change.sync_data())); 1065 EXPECT_EQ("baidu", GetKeyword(change.sync_data()));
1077 EXPECT_EQ("http://baidu.cn", GetURL(change.sync_data())); 1066 EXPECT_EQ("http://baidu.cn", GetURL(change.sync_data()));
1078 1067
1079 // Change a keyword. 1068 // Change a keyword.
1080 TemplateURL* existing_turl = model()->GetTemplateURLForGUID("key1"); 1069 TemplateURL* existing_turl = model()->GetTemplateURLForGUID("key1");
1081 model()->ResetTemplateURL(existing_turl, existing_turl->short_name(), 1070 model()->ResetTemplateURL(existing_turl, existing_turl->short_name(),
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1134 1123
1135 EXPECT_EQ(extension1, 1124 EXPECT_EQ(extension1,
1136 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1"))); 1125 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword1")));
1137 EXPECT_EQ(model()->GetTemplateURLForHost("bbb.com"), 1126 EXPECT_EQ(model()->GetTemplateURLForHost("bbb.com"),
1138 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2"))); 1127 model()->GetTemplateURLForKeyword(ASCIIToUTF16("keyword2")));
1139 } 1128 }
1140 1129
1141 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordMigrated) { 1130 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordMigrated) {
1142 // Create a couple of sync entries with autogenerated keywords. 1131 // Create a couple of sync entries with autogenerated keywords.
1143 syncer::SyncDataList initial_data; 1132 syncer::SyncDataList initial_data;
1144 std::unique_ptr<TemplateURL> turl( 1133 std::unique_ptr<TemplateURL> turl =
1145 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); 1134 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1");
1146 initial_data.push_back( 1135 initial_data.push_back(
1147 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); 1136 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1148 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), 1137 turl = CreateTestTemplateURL(
1149 "{google:baseURL}search?q={searchTerms}", "key2")); 1138 ASCIIToUTF16("key2"), "{google:baseURL}search?q={searchTerms}", "key2");
1150 initial_data.push_back( 1139 initial_data.push_back(
1151 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid(), 99)); 1140 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid(), 99));
1152 1141
1153 // Now try to sync the data locally. 1142 // Now try to sync the data locally.
1154 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1143 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1155 PassProcessor(), CreateAndPassSyncErrorFactory()); 1144 PassProcessor(), CreateAndPassSyncErrorFactory());
1156 1145
1157 // Both entries should have been added, with explicit keywords. 1146 // Both entries should have been added, with explicit keywords.
1158 TemplateURL* key1 = model()->GetTemplateURLForHost("key1.com"); 1147 TemplateURL* key1 = model()->GetTemplateURLForHost("key1.com");
1159 ASSERT_FALSE(key1 == NULL); 1148 ASSERT_FALSE(key1 == NULL);
(...skipping 16 matching lines...) Expand all
1176 EXPECT_EQ(google_keyword, UTF8ToUTF16(GetKeyword(key2_change.sync_data()))); 1165 EXPECT_EQ(google_keyword, UTF8ToUTF16(GetKeyword(key2_change.sync_data())));
1177 } 1166 }
1178 1167
1179 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordConflicts) { 1168 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordConflicts) {
1180 // Sync brings in some autogenerated keywords, but the generated keywords we 1169 // Sync brings in some autogenerated keywords, but the generated keywords we
1181 // try to create conflict with ones in the model. 1170 // try to create conflict with ones in the model.
1182 base::string16 google_keyword(url_formatter::StripWWWFromHost(GURL( 1171 base::string16 google_keyword(url_formatter::StripWWWFromHost(GURL(
1183 model()->search_terms_data().GoogleBaseURLValue()))); 1172 model()->search_terms_data().GoogleBaseURLValue())));
1184 const std::string local_google_url = 1173 const std::string local_google_url =
1185 "{google:baseURL}1/search?q={searchTerms}"; 1174 "{google:baseURL}1/search?q={searchTerms}";
1186 TemplateURL* google = CreateTestTemplateURL(google_keyword, local_google_url); 1175 TemplateURL* google =
1187 model()->Add(google); 1176 model()->Add(CreateTestTemplateURL(google_keyword, local_google_url));
1188 TemplateURL* other = 1177 TemplateURL* other = model()->Add(
1189 CreateTestTemplateURL(ASCIIToUTF16("other.com"), "http://other.com/foo"); 1178 CreateTestTemplateURL(ASCIIToUTF16("other.com"), "http://other.com/foo"));
1190 model()->Add(other);
1191 syncer::SyncDataList initial_data; 1179 syncer::SyncDataList initial_data;
1192 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL( 1180 std::unique_ptr<TemplateURL> turl = CreateTestTemplateURL(
1193 ASCIIToUTF16("sync1"), "{google:baseURL}2/search?q={searchTerms}", 1181 ASCIIToUTF16("sync1"), "{google:baseURL}2/search?q={searchTerms}",
1194 "sync1", 50)); 1182 "sync1", 50);
1195 initial_data.push_back( 1183 initial_data.push_back(
1196 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); 1184 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1197 const std::string synced_other_url = 1185 const std::string synced_other_url =
1198 "http://other.com/search?q={searchTerms}"; 1186 "http://other.com/search?q={searchTerms}";
1199 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("sync2"), 1187 turl = CreateTestTemplateURL(ASCIIToUTF16("sync2"), synced_other_url, "sync2",
1200 synced_other_url, "sync2", 150)); 1188 150);
1201 initial_data.push_back( 1189 initial_data.push_back(
1202 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); 1190 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1203 1191
1204 // Before we merge the data, grab the local sync_guids so we can ensure that 1192 // Before we merge the data, grab the local sync_guids so we can ensure that
1205 // they've been replaced. 1193 // they've been replaced.
1206 const std::string local_google_guid = google->sync_guid(); 1194 const std::string local_google_guid = google->sync_guid();
1207 const std::string local_other_guid = other->sync_guid(); 1195 const std::string local_other_guid = other->sync_guid();
1208 1196
1209 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1197 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1210 PassProcessor(), CreateAndPassSyncErrorFactory()); 1198 PassProcessor(), CreateAndPassSyncErrorFactory());
(...skipping 23 matching lines...) Expand all
1234 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, sync2_change.change_type()); 1222 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, sync2_change.change_type());
1235 EXPECT_EQ("other.com", GetKeyword(sync2_change.sync_data())); 1223 EXPECT_EQ("other.com", GetKeyword(sync2_change.sync_data()));
1236 EXPECT_EQ(synced_other_url, GetURL(sync2_change.sync_data())); 1224 EXPECT_EQ(synced_other_url, GetURL(sync2_change.sync_data()));
1237 } 1225 }
1238 1226
1239 TEST_F(TemplateURLServiceSyncTest, TwoAutogeneratedKeywordsUsingGoogleBaseURL) { 1227 TEST_F(TemplateURLServiceSyncTest, TwoAutogeneratedKeywordsUsingGoogleBaseURL) {
1240 // Sync brings in two autogenerated keywords and both use Google base URLs. 1228 // Sync brings in two autogenerated keywords and both use Google base URLs.
1241 // We make the first older so that it will get renamed once before the second 1229 // We make the first older so that it will get renamed once before the second
1242 // and then again once after (when we resolve conflicts for the second). 1230 // and then again once after (when we resolve conflicts for the second).
1243 syncer::SyncDataList initial_data; 1231 syncer::SyncDataList initial_data;
1244 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL( 1232 std::unique_ptr<TemplateURL> turl = CreateTestTemplateURL(
1245 ASCIIToUTF16("key1"), "{google:baseURL}1/search?q={searchTerms}", "key1", 1233 ASCIIToUTF16("key1"), "{google:baseURL}1/search?q={searchTerms}", "key1",
1246 50)); 1234 50);
1247 initial_data.push_back( 1235 initial_data.push_back(
1248 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); 1236 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1249 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), 1237 turl = CreateTestTemplateURL(
1250 "{google:baseURL}2/search?q={searchTerms}", "key2")); 1238 ASCIIToUTF16("key2"), "{google:baseURL}2/search?q={searchTerms}", "key2");
1251 initial_data.push_back( 1239 initial_data.push_back(
1252 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); 1240 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid()));
1253 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1241 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1254 PassProcessor(), CreateAndPassSyncErrorFactory()); 1242 PassProcessor(), CreateAndPassSyncErrorFactory());
1255 1243
1256 // We should still have coalesced the updates to one each. 1244 // We should still have coalesced the updates to one each.
1257 base::string16 google_keyword(url_formatter::StripWWWFromHost(GURL( 1245 base::string16 google_keyword(url_formatter::StripWWWFromHost(GURL(
1258 model()->search_terms_data().GoogleBaseURLValue()))); 1246 model()->search_terms_data().GoogleBaseURLValue())));
1259 TemplateURL* keyword1 = 1247 TemplateURL* keyword1 =
1260 model()->GetTemplateURLForKeyword(google_keyword + ASCIIToUTF16("_")); 1248 model()->GetTemplateURLForKeyword(google_keyword + ASCIIToUTF16("_"));
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
1528 model()->ProcessSyncChanges(FROM_HERE, changes2); 1516 model()->ProcessSyncChanges(FROM_HERE, changes2);
1529 1517
1530 EXPECT_EQ(5U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); 1518 EXPECT_EQ(5U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1531 ASSERT_NE(default_search, model()->GetDefaultSearchProvider()); 1519 ASSERT_NE(default_search, model()->GetDefaultSearchProvider());
1532 ASSERT_EQ("newdefault", model()->GetDefaultSearchProvider()->sync_guid()); 1520 ASSERT_EQ("newdefault", model()->GetDefaultSearchProvider()->sync_guid());
1533 } 1521 }
1534 1522
1535 TEST_F(TemplateURLServiceSyncTest, DefaultGuidDeletedBeforeNewDSPArrives) { 1523 TEST_F(TemplateURLServiceSyncTest, DefaultGuidDeletedBeforeNewDSPArrives) {
1536 syncer::SyncDataList initial_data; 1524 syncer::SyncDataList initial_data;
1537 // The default search provider should support replacement. 1525 // The default search provider should support replacement.
1538 std::unique_ptr<TemplateURL> turl1(CreateTestTemplateURL( 1526 std::unique_ptr<TemplateURL> turl1 = CreateTestTemplateURL(
1539 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "key1", 90)); 1527 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "key1", 90);
1540 // Create a second default search provider for the 1528 // Create a second default search provider for the
1541 // FindNewDefaultSearchProvider method to find. 1529 // FindNewDefaultSearchProvider method to find.
1542 TemplateURLData data; 1530 TemplateURLData data;
1543 data.SetShortName(ASCIIToUTF16("unittest")); 1531 data.SetShortName(ASCIIToUTF16("unittest"));
1544 data.SetKeyword(ASCIIToUTF16("key2")); 1532 data.SetKeyword(ASCIIToUTF16("key2"));
1545 data.SetURL("http://key2.com/{searchTerms}"); 1533 data.SetURL("http://key2.com/{searchTerms}");
1546 data.favicon_url = GURL("http://favicon.url"); 1534 data.favicon_url = GURL("http://favicon.url");
1547 data.safe_for_autoreplace = false; 1535 data.safe_for_autoreplace = false;
1548 data.date_created = Time::FromTimeT(100); 1536 data.date_created = Time::FromTimeT(100);
1549 data.last_modified = Time::FromTimeT(100); 1537 data.last_modified = Time::FromTimeT(100);
(...skipping 26 matching lines...) Expand all
1576 prefs::kSyncedDefaultSearchProviderGUID)); 1564 prefs::kSyncedDefaultSearchProviderGUID));
1577 1565
1578 // Simulate a situation where an ACTION_DELETE on the default arrives before 1566 // Simulate a situation where an ACTION_DELETE on the default arrives before
1579 // the new default search provider entry. This should fail to delete the 1567 // the new default search provider entry. This should fail to delete the
1580 // target entry, and instead send up an "undelete" to the server, after 1568 // target entry, and instead send up an "undelete" to the server, after
1581 // further uniquifying the keyword to avoid infinite sync loops. The synced 1569 // further uniquifying the keyword to avoid infinite sync loops. The synced
1582 // default GUID should not be changed so that when the expected default entry 1570 // default GUID should not be changed so that when the expected default entry
1583 // arrives, it can still be set as the default. 1571 // arrives, it can still be set as the default.
1584 syncer::SyncChangeList changes1; 1572 syncer::SyncChangeList changes1;
1585 changes1.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_DELETE, 1573 changes1.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_DELETE,
1586 turl1.release())); 1574 std::move(turl1)));
1587 model()->ProcessSyncChanges(FROM_HERE, changes1); 1575 model()->ProcessSyncChanges(FROM_HERE, changes1);
1588 1576
1589 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1_"))); 1577 EXPECT_TRUE(model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1_")));
1590 EXPECT_EQ(2U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); 1578 EXPECT_EQ(2U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1591 EXPECT_EQ("key1", model()->GetDefaultSearchProvider()->sync_guid()); 1579 EXPECT_EQ("key1", model()->GetDefaultSearchProvider()->sync_guid());
1592 EXPECT_EQ("newdefault", profile_a()->GetTestingPrefService()->GetString( 1580 EXPECT_EQ("newdefault", profile_a()->GetTestingPrefService()->GetString(
1593 prefs::kSyncedDefaultSearchProviderGUID)); 1581 prefs::kSyncedDefaultSearchProviderGUID));
1594 syncer::SyncChange undelete = processor()->change_for_guid("key1"); 1582 syncer::SyncChange undelete = processor()->change_for_guid("key1");
1595 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, undelete.change_type()); 1583 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, undelete.change_type());
1596 EXPECT_EQ("key1_", 1584 EXPECT_EQ("key1_",
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1729 const TemplateURL* expected_default = 1717 const TemplateURL* expected_default =
1730 model()->GetTemplateURLForGUID("newdefault"); 1718 model()->GetTemplateURLForGUID("newdefault");
1731 test_util_a_->RemoveManagedDefaultSearchPreferences(); 1719 test_util_a_->RemoveManagedDefaultSearchPreferences();
1732 1720
1733 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider()); 1721 EXPECT_EQ(expected_default, model()->GetDefaultSearchProvider());
1734 } 1722 }
1735 1723
1736 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) { 1724 TEST_F(TemplateURLServiceSyncTest, SyncMergeDeletesDefault) {
1737 // If the value from Sync is a duplicate of the local default and is newer, it 1725 // If the value from Sync is a duplicate of the local default and is newer, it
1738 // should safely replace the local value and set as the new default. 1726 // should safely replace the local value and set as the new default.
1739 TemplateURL* default_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), 1727 TemplateURL* default_turl = model()->Add(CreateTestTemplateURL(
1740 "http://key1.com/{searchTerms}", "whateverguid", 10); 1728 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "whateverguid",
1741 model()->Add(default_turl); 1729 10));
1742 model()->SetUserSelectedDefaultSearchProvider(default_turl); 1730 model()->SetUserSelectedDefaultSearchProvider(default_turl);
1743 1731
1744 syncer::SyncDataList initial_data = CreateInitialSyncData(); 1732 syncer::SyncDataList initial_data = CreateInitialSyncData();
1745 // The key1 entry should be a duplicate of the default. 1733 // The key1 entry should be a duplicate of the default.
1746 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL( 1734 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL(
1747 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "key1", 90)); 1735 ASCIIToUTF16("key1"), "http://key1.com/{searchTerms}", "key1", 90));
1748 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 1736 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
1749 1737
1750 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1738 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1751 PassProcessor(), CreateAndPassSyncErrorFactory()); 1739 PassProcessor(), CreateAndPassSyncErrorFactory());
1752 1740
1753 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); 1741 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1754 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); 1742 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid"));
1755 EXPECT_EQ(model()->GetDefaultSearchProvider(), 1743 EXPECT_EQ(model()->GetDefaultSearchProvider(),
1756 model()->GetTemplateURLForGUID("key1")); 1744 model()->GetTemplateURLForGUID("key1"));
1757 } 1745 }
1758 1746
1759 TEST_F(TemplateURLServiceSyncTest, LocalDefaultWinsConflict) { 1747 TEST_F(TemplateURLServiceSyncTest, LocalDefaultWinsConflict) {
1760 // We expect that the local default always wins keyword conflict resolution. 1748 // We expect that the local default always wins keyword conflict resolution.
1761 const base::string16 keyword(ASCIIToUTF16("key1")); 1749 const base::string16 keyword(ASCIIToUTF16("key1"));
1762 const std::string url("http://whatever.com/{searchTerms}"); 1750 const std::string url("http://whatever.com/{searchTerms}");
1763 TemplateURL* default_turl = CreateTestTemplateURL(keyword, 1751 TemplateURL* default_turl =
1764 url, 1752 model()->Add(CreateTestTemplateURL(keyword, url, "whateverguid", 10));
1765 "whateverguid",
1766 10);
1767 model()->Add(default_turl);
1768 model()->SetUserSelectedDefaultSearchProvider(default_turl); 1753 model()->SetUserSelectedDefaultSearchProvider(default_turl);
1769 1754
1770 syncer::SyncDataList initial_data = CreateInitialSyncData(); 1755 syncer::SyncDataList initial_data = CreateInitialSyncData();
1771 // The key1 entry should be different from the default but conflict in the 1756 // The key1 entry should be different from the default but conflict in the
1772 // keyword. 1757 // keyword.
1773 std::unique_ptr<TemplateURL> turl(CreateTestTemplateURL( 1758 std::unique_ptr<TemplateURL> turl = CreateTestTemplateURL(
1774 keyword, "http://key1.com/{searchTerms}", "key1", 90)); 1759 keyword, "http://key1.com/{searchTerms}", "key1", 90);
1775 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); 1760 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl);
1776 1761
1777 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1762 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1778 PassProcessor(), CreateAndPassSyncErrorFactory()); 1763 PassProcessor(), CreateAndPassSyncErrorFactory());
1779 1764
1780 // Since the local default was not yet synced, it should be merged with the 1765 // Since the local default was not yet synced, it should be merged with the
1781 // conflicting TemplateURL. However, its values should have been preserved 1766 // conflicting TemplateURL. However, its values should have been preserved
1782 // since it would have won conflict resolution due to being the default. 1767 // since it would have won conflict resolution due to being the default.
1783 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); 1768 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size());
1784 const TemplateURL* winner = model()->GetTemplateURLForGUID("key1"); 1769 const TemplateURL* winner = model()->GetTemplateURLForGUID("key1");
1785 ASSERT_TRUE(winner); 1770 ASSERT_TRUE(winner);
1786 EXPECT_EQ(model()->GetDefaultSearchProvider(), winner); 1771 EXPECT_EQ(model()->GetDefaultSearchProvider(), winner);
1787 EXPECT_EQ(keyword, winner->keyword()); 1772 EXPECT_EQ(keyword, winner->keyword());
1788 EXPECT_EQ(url, winner->url()); 1773 EXPECT_EQ(url, winner->url());
1789 ASSERT_TRUE(processor()->contains_guid("key1")); 1774 ASSERT_TRUE(processor()->contains_guid("key1"));
1790 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, 1775 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE,
1791 processor()->change_for_guid("key1").change_type()); 1776 processor()->change_for_guid("key1").change_type());
1792 EXPECT_EQ(url, GetURL(processor()->change_for_guid("key1").sync_data())); 1777 EXPECT_EQ(url, GetURL(processor()->change_for_guid("key1").sync_data()));
1793 1778
1794 // There is no loser, as the two were merged together. The local sync_guid 1779 // There is no loser, as the two were merged together. The local sync_guid
1795 // should no longer be found in the model. 1780 // should no longer be found in the model.
1796 const TemplateURL* loser = model()->GetTemplateURLForGUID("whateverguid"); 1781 const TemplateURL* loser = model()->GetTemplateURLForGUID("whateverguid");
1797 ASSERT_FALSE(loser); 1782 ASSERT_FALSE(loser);
1798 } 1783 }
1799 1784
1800 TEST_F(TemplateURLServiceSyncTest, DeleteBogusData) { 1785 TEST_F(TemplateURLServiceSyncTest, DeleteBogusData) {
1801 // Create a couple of bogus entries to sync. 1786 // Create a couple of bogus entries to sync.
1802 syncer::SyncDataList initial_data; 1787 syncer::SyncDataList initial_data;
1803 std::unique_ptr<TemplateURL> turl( 1788 std::unique_ptr<TemplateURL> turl =
1804 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); 1789 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1");
1805 initial_data.push_back( 1790 initial_data.push_back(
1806 CreateCustomSyncData(*turl, false, std::string(), turl->sync_guid())); 1791 CreateCustomSyncData(*turl, false, std::string(), turl->sync_guid()));
1807 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); 1792 turl = CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com");
1808 initial_data.push_back( 1793 initial_data.push_back(
1809 CreateCustomSyncData(*turl, false, turl->url(), std::string())); 1794 CreateCustomSyncData(*turl, false, turl->url(), std::string()));
1810 1795
1811 // Now try to sync the data locally. 1796 // Now try to sync the data locally.
1812 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, 1797 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data,
1813 PassProcessor(), CreateAndPassSyncErrorFactory()); 1798 PassProcessor(), CreateAndPassSyncErrorFactory());
1814 1799
1815 // Nothing should have been added, and both bogus entries should be marked for 1800 // Nothing should have been added, and both bogus entries should be marked for
1816 // deletion. 1801 // deletion.
1817 EXPECT_EQ(0U, model()->GetTemplateURLs().size()); 1802 EXPECT_EQ(0U, model()->GetTemplateURLs().size());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1870 ASSERT_FALSE(prepop_turls.empty()); 1855 ASSERT_FALSE(prepop_turls.empty());
1871 1856
1872 // Create a copy of the first TemplateURL with a really old timestamp and a 1857 // Create a copy of the first TemplateURL with a really old timestamp and a
1873 // new keyword. Add it to the model. 1858 // new keyword. Add it to the model.
1874 TemplateURLData data_copy(*prepop_turls[0]); 1859 TemplateURLData data_copy(*prepop_turls[0]);
1875 data_copy.last_modified = Time::FromTimeT(10); 1860 data_copy.last_modified = Time::FromTimeT(10);
1876 base::string16 original_keyword = data_copy.keyword(); 1861 base::string16 original_keyword = data_copy.keyword();
1877 data_copy.SetKeyword(ASCIIToUTF16(kNewKeyword)); 1862 data_copy.SetKeyword(ASCIIToUTF16(kNewKeyword));
1878 // Set safe_for_autoreplace to false so our keyword survives. 1863 // Set safe_for_autoreplace to false so our keyword survives.
1879 data_copy.safe_for_autoreplace = false; 1864 data_copy.safe_for_autoreplace = false;
1880 model()->Add(new TemplateURL(data_copy)); 1865 model()->Add(base::MakeUnique<TemplateURL>(data_copy));
1881 1866
1882 // Merge the prepopulate search engines. 1867 // Merge the prepopulate search engines.
1883 base::Time pre_merge_time = base::Time::Now(); 1868 base::Time pre_merge_time = base::Time::Now();
1884 base::RunLoop().RunUntilIdle(); 1869 base::RunLoop().RunUntilIdle();
1885 test_util_a_->ResetModel(true); 1870 test_util_a_->ResetModel(true);
1886 1871
1887 // The newly added search engine should have been safely merged, with an 1872 // The newly added search engine should have been safely merged, with an
1888 // updated time. 1873 // updated time.
1889 TemplateURL* added_turl = model()->GetTemplateURLForKeyword( 1874 TemplateURL* added_turl = model()->GetTemplateURLForKeyword(
1890 ASCIIToUTF16(kNewKeyword)); 1875 ASCIIToUTF16(kNewKeyword));
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 const time_t sync_last_modified = 2019 const time_t sync_last_modified =
2035 test_cases[i].conflict_winner == SYNC ? 110 : 90; 2020 test_cases[i].conflict_winner == SYNC ? 110 : 90;
2036 const std::string local_guid = "local_guid"; 2021 const std::string local_guid = "local_guid";
2037 const std::string sync_guid = "sync_guid"; 2022 const std::string sync_guid = "sync_guid";
2038 2023
2039 // Initialize expectations. 2024 // Initialize expectations.
2040 base::string16 expected_local_keyword = local_keyword; 2025 base::string16 expected_local_keyword = local_keyword;
2041 base::string16 expected_sync_keyword = sync_keyword; 2026 base::string16 expected_sync_keyword = sync_keyword;
2042 2027
2043 // Create the data and run the actual test. 2028 // Create the data and run the actual test.
2044 TemplateURL* local_turl = CreateTestTemplateURL( 2029 TemplateURL* local_turl = model()->Add(CreateTestTemplateURL(
2045 local_keyword, local_url, local_guid, local_last_modified); 2030 local_keyword, local_url, local_guid, local_last_modified));
2046 model()->Add(local_turl);
2047 std::unique_ptr<TemplateURL> sync_turl(CreateTestTemplateURL( 2031 std::unique_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(
2048 sync_keyword, sync_url, sync_guid, sync_last_modified)); 2032 sync_keyword, sync_url, sync_guid, sync_last_modified));
2049 2033
2050 SyncDataMap sync_data; 2034 SyncDataMap sync_data;
2051 if (test_cases[i].synced_at_start == SYNC || 2035 if (test_cases[i].synced_at_start == SYNC ||
2052 test_cases[i].synced_at_start == BOTH) { 2036 test_cases[i].synced_at_start == BOTH) {
2053 sync_data[sync_turl->sync_guid()] = 2037 sync_data[sync_turl->sync_guid()] =
2054 TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl); 2038 TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl);
2055 } 2039 }
2056 if (test_cases[i].synced_at_start == BOTH) { 2040 if (test_cases[i].synced_at_start == BOTH) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
2123 } // for 2107 } // for
2124 } 2108 }
2125 2109
2126 TEST_F(TemplateURLServiceSyncTest, MergePrepopulatedEngine) { 2110 TEST_F(TemplateURLServiceSyncTest, MergePrepopulatedEngine) {
2127 std::unique_ptr<TemplateURLData> default_turl( 2111 std::unique_ptr<TemplateURLData> default_turl(
2128 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); 2112 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL));
2129 2113
2130 // Merge with an initial list containing a prepopulated engine with a wrong 2114 // Merge with an initial list containing a prepopulated engine with a wrong
2131 // URL. 2115 // URL.
2132 syncer::SyncDataList list; 2116 syncer::SyncDataList list;
2133 std::unique_ptr<TemplateURL> sync_turl(CopyTemplateURL( 2117 std::unique_ptr<TemplateURL> sync_turl = CopyTemplateURL(
2134 default_turl.get(), "http://wrong.url.com?q={searchTerms}", "default")); 2118 default_turl.get(), "http://wrong.url.com?q={searchTerms}", "default");
2135 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl)); 2119 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl));
2136 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 2120 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
2137 syncer::SEARCH_ENGINES, list, PassProcessor(), 2121 syncer::SEARCH_ENGINES, list, PassProcessor(),
2138 CreateAndPassSyncErrorFactory()); 2122 CreateAndPassSyncErrorFactory());
2139 2123
2140 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default"); 2124 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default");
2141 EXPECT_TRUE(result_turl); 2125 EXPECT_TRUE(result_turl);
2142 EXPECT_EQ(default_turl->keyword(), result_turl->keyword()); 2126 EXPECT_EQ(default_turl->keyword(), result_turl->keyword());
2143 EXPECT_EQ(default_turl->short_name(), result_turl->short_name()); 2127 EXPECT_EQ(default_turl->short_name(), result_turl->short_name());
2144 EXPECT_EQ(default_turl->url(), result_turl->url()); 2128 EXPECT_EQ(default_turl->url(), result_turl->url());
2145 } 2129 }
2146 2130
2147 TEST_F(TemplateURLServiceSyncTest, AddPrepopulatedEngine) { 2131 TEST_F(TemplateURLServiceSyncTest, AddPrepopulatedEngine) {
2148 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 2132 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
2149 syncer::SEARCH_ENGINES, syncer::SyncDataList(), PassProcessor(), 2133 syncer::SEARCH_ENGINES, syncer::SyncDataList(), PassProcessor(),
2150 CreateAndPassSyncErrorFactory()); 2134 CreateAndPassSyncErrorFactory());
2151 2135
2152 std::unique_ptr<TemplateURLData> default_turl( 2136 std::unique_ptr<TemplateURLData> default_turl(
2153 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); 2137 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL));
2154 TemplateURL* sync_turl = CopyTemplateURL(default_turl.get(), 2138 std::unique_ptr<TemplateURL> sync_turl = CopyTemplateURL(
2155 "http://wrong.url.com?q={searchTerms}", "default"); 2139 default_turl.get(), "http://wrong.url.com?q={searchTerms}", "default");
2156 2140
2157 // Add a prepopulated engine with a wrong URL. 2141 // Add a prepopulated engine with a wrong URL.
2158 syncer::SyncChangeList changes; 2142 syncer::SyncChangeList changes;
2159 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD, 2143 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_ADD,
2160 sync_turl)); 2144 std::move(sync_turl)));
2161 model()->ProcessSyncChanges(FROM_HERE, changes); 2145 model()->ProcessSyncChanges(FROM_HERE, changes);
2162 2146
2163 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default"); 2147 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default");
2164 EXPECT_TRUE(result_turl); 2148 EXPECT_TRUE(result_turl);
2165 EXPECT_EQ(default_turl->keyword(), result_turl->keyword()); 2149 EXPECT_EQ(default_turl->keyword(), result_turl->keyword());
2166 EXPECT_EQ(default_turl->short_name(), result_turl->short_name()); 2150 EXPECT_EQ(default_turl->short_name(), result_turl->short_name());
2167 EXPECT_EQ(default_turl->url(), result_turl->url()); 2151 EXPECT_EQ(default_turl->url(), result_turl->url());
2168 } 2152 }
2169 2153
2170 TEST_F(TemplateURLServiceSyncTest, UpdatePrepopulatedEngine) { 2154 TEST_F(TemplateURLServiceSyncTest, UpdatePrepopulatedEngine) {
2171 std::unique_ptr<TemplateURLData> default_turl( 2155 std::unique_ptr<TemplateURLData> default_turl(
2172 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); 2156 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL));
2173 2157
2174 TemplateURLData data = *default_turl; 2158 TemplateURLData data = *default_turl;
2175 data.SetURL("http://old.wrong.url.com?q={searchTerms}"); 2159 data.SetURL("http://old.wrong.url.com?q={searchTerms}");
2176 data.sync_guid = "default"; 2160 data.sync_guid = "default";
2177 model()->Add(new TemplateURL(data)); 2161 model()->Add(base::MakeUnique<TemplateURL>(data));
2178 2162
2179 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 2163 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
2180 syncer::SEARCH_ENGINES, syncer::SyncDataList(), PassProcessor(), 2164 syncer::SEARCH_ENGINES, syncer::SyncDataList(), PassProcessor(),
2181 CreateAndPassSyncErrorFactory()); 2165 CreateAndPassSyncErrorFactory());
2182 2166
2183 TemplateURL* sync_turl = CopyTemplateURL(default_turl.get(), 2167 std::unique_ptr<TemplateURL> sync_turl =
2184 "http://new.wrong.url.com?q={searchTerms}", "default"); 2168 CopyTemplateURL(default_turl.get(),
2169 "http://new.wrong.url.com?q={searchTerms}", "default");
2185 2170
2186 // Update the engine in the model, which is prepopulated, with a new one. 2171 // Update the engine in the model, which is prepopulated, with a new one.
2187 // Both have wrong URLs, but it should still get corrected. 2172 // Both have wrong URLs, but it should still get corrected.
2188 syncer::SyncChangeList changes; 2173 syncer::SyncChangeList changes;
2189 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_UPDATE, 2174 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_UPDATE,
2190 sync_turl)); 2175 std::move(sync_turl)));
2191 model()->ProcessSyncChanges(FROM_HERE, changes); 2176 model()->ProcessSyncChanges(FROM_HERE, changes);
2192 2177
2193 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default"); 2178 const TemplateURL* result_turl = model()->GetTemplateURLForGUID("default");
2194 EXPECT_TRUE(result_turl); 2179 EXPECT_TRUE(result_turl);
2195 EXPECT_EQ(default_turl->keyword(), result_turl->keyword()); 2180 EXPECT_EQ(default_turl->keyword(), result_turl->keyword());
2196 EXPECT_EQ(default_turl->short_name(), result_turl->short_name()); 2181 EXPECT_EQ(default_turl->short_name(), result_turl->short_name());
2197 EXPECT_EQ(default_turl->url(), result_turl->url()); 2182 EXPECT_EQ(default_turl->url(), result_turl->url());
2198 } 2183 }
2199 2184
2200 TEST_F(TemplateURLServiceSyncTest, MergeEditedPrepopulatedEngine) { 2185 TEST_F(TemplateURLServiceSyncTest, MergeEditedPrepopulatedEngine) {
2201 std::unique_ptr<TemplateURLData> default_turl( 2186 std::unique_ptr<TemplateURLData> default_turl(
2202 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); 2187 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL));
2203 2188
2204 TemplateURLData data(*default_turl); 2189 TemplateURLData data(*default_turl);
2205 data.safe_for_autoreplace = false; 2190 data.safe_for_autoreplace = false;
2206 data.SetKeyword(ASCIIToUTF16("new_kw")); 2191 data.SetKeyword(ASCIIToUTF16("new_kw"));
2207 data.SetShortName(ASCIIToUTF16("my name")); 2192 data.SetShortName(ASCIIToUTF16("my name"));
2208 data.SetURL("http://wrong.url.com?q={searchTerms}"); 2193 data.SetURL("http://wrong.url.com?q={searchTerms}");
2209 data.date_created = Time::FromTimeT(50); 2194 data.date_created = Time::FromTimeT(50);
2210 data.last_modified = Time::FromTimeT(50); 2195 data.last_modified = Time::FromTimeT(50);
2211 data.sync_guid = "default"; 2196 data.sync_guid = "default";
2212 model()->Add(new TemplateURL(data)); 2197 model()->Add(base::MakeUnique<TemplateURL>(data));
2213 2198
2214 data.date_created = Time::FromTimeT(100); 2199 data.date_created = Time::FromTimeT(100);
2215 data.last_modified = Time::FromTimeT(100); 2200 data.last_modified = Time::FromTimeT(100);
2216 std::unique_ptr<TemplateURL> sync_turl(new TemplateURL(data)); 2201 std::unique_ptr<TemplateURL> sync_turl(new TemplateURL(data));
2217 syncer::SyncDataList list; 2202 syncer::SyncDataList list;
2218 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl)); 2203 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl));
2219 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 2204 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
2220 syncer::SEARCH_ENGINES, list, PassProcessor(), 2205 syncer::SEARCH_ENGINES, list, PassProcessor(),
2221 CreateAndPassSyncErrorFactory()); 2206 CreateAndPassSyncErrorFactory());
2222 2207
(...skipping 10 matching lines...) Expand all
2233 2218
2234 TemplateURLData data(*default_turl); 2219 TemplateURLData data(*default_turl);
2235 data.SetKeyword(ASCIIToUTF16("old_kw")); 2220 data.SetKeyword(ASCIIToUTF16("old_kw"));
2236 data.SetShortName(ASCIIToUTF16("my name")); 2221 data.SetShortName(ASCIIToUTF16("my name"));
2237 data.SetURL("http://wrong.url.com?q={searchTerms}"); 2222 data.SetURL("http://wrong.url.com?q={searchTerms}");
2238 data.safe_for_autoreplace = true; 2223 data.safe_for_autoreplace = true;
2239 data.date_created = Time::FromTimeT(50); 2224 data.date_created = Time::FromTimeT(50);
2240 data.last_modified = Time::FromTimeT(50); 2225 data.last_modified = Time::FromTimeT(50);
2241 data.prepopulate_id = 1; 2226 data.prepopulate_id = 1;
2242 data.sync_guid = "default"; 2227 data.sync_guid = "default";
2243 model()->Add(new TemplateURL(data)); 2228 model()->Add(base::MakeUnique<TemplateURL>(data));
2244 2229
2245 TemplateURLData new_data(*default_turl); 2230 TemplateURLData new_data(*default_turl);
2246 new_data.SetKeyword(ASCIIToUTF16("new_kw")); 2231 new_data.SetKeyword(ASCIIToUTF16("new_kw"));
2247 new_data.SetShortName(ASCIIToUTF16("my name")); 2232 new_data.SetShortName(ASCIIToUTF16("my name"));
2248 new_data.SetURL("http://wrong.url.com?q={searchTerms}"); 2233 new_data.SetURL("http://wrong.url.com?q={searchTerms}");
2249 new_data.safe_for_autoreplace = false; 2234 new_data.safe_for_autoreplace = false;
2250 new_data.date_created = Time::FromTimeT(100); 2235 new_data.date_created = Time::FromTimeT(100);
2251 new_data.last_modified = Time::FromTimeT(100); 2236 new_data.last_modified = Time::FromTimeT(100);
2252 new_data.prepopulate_id = 1; 2237 new_data.prepopulate_id = 1;
2253 new_data.sync_guid = "different_guid"; 2238 new_data.sync_guid = "different_guid";
(...skipping 16 matching lines...) Expand all
2270 2255
2271 // Reset the state of the service. 2256 // Reset the state of the service.
2272 model()->Remove(result_turl); 2257 model()->Remove(result_turl);
2273 model()->StopSyncing(syncer::SEARCH_ENGINES); 2258 model()->StopSyncing(syncer::SEARCH_ENGINES);
2274 sync_processor_wrapper_.reset( 2259 sync_processor_wrapper_.reset(
2275 new syncer::SyncChangeProcessorWrapperForTest(sync_processor_.get())); 2260 new syncer::SyncChangeProcessorWrapperForTest(sync_processor_.get()));
2276 2261
2277 // Now test that a remote TemplateURL can override the attributes of the local 2262 // Now test that a remote TemplateURL can override the attributes of the local
2278 // default search provider. 2263 // default search provider.
2279 TemplateURL* existing_default = new TemplateURL(data); 2264 TemplateURL* existing_default = new TemplateURL(data);
2280 model()->Add(existing_default); 2265 model()->Add(base::WrapUnique(existing_default));
2281 model()->SetUserSelectedDefaultSearchProvider(existing_default); 2266 model()->SetUserSelectedDefaultSearchProvider(existing_default);
2282 2267
2283 merge_result = model()->MergeDataAndStartSyncing( 2268 merge_result = model()->MergeDataAndStartSyncing(
2284 syncer::SEARCH_ENGINES, list, PassProcessor(), 2269 syncer::SEARCH_ENGINES, list, PassProcessor(),
2285 CreateAndPassSyncErrorFactory()); 2270 CreateAndPassSyncErrorFactory());
2286 2271
2287 result_turl = model()->GetDefaultSearchProvider(); 2272 result_turl = model()->GetDefaultSearchProvider();
2288 EXPECT_TRUE(result_turl); 2273 EXPECT_TRUE(result_turl);
2289 EXPECT_EQ(ASCIIToUTF16("new_kw"), result_turl->keyword()); 2274 EXPECT_EQ(ASCIIToUTF16("new_kw"), result_turl->keyword());
2290 EXPECT_EQ(ASCIIToUTF16("my name"), result_turl->short_name()); 2275 EXPECT_EQ(ASCIIToUTF16("my name"), result_turl->short_name());
2291 EXPECT_EQ(default_turl->url(), result_turl->url()); 2276 EXPECT_EQ(default_turl->url(), result_turl->url());
2292 } 2277 }
2293 2278
2294 TEST_F(TemplateURLServiceSyncTest, MergeNonEditedPrepopulatedEngine) { 2279 TEST_F(TemplateURLServiceSyncTest, MergeNonEditedPrepopulatedEngine) {
2295 std::unique_ptr<TemplateURLData> default_turl( 2280 std::unique_ptr<TemplateURLData> default_turl(
2296 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL)); 2281 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(NULL));
2297 2282
2298 TemplateURLData data(*default_turl); 2283 TemplateURLData data(*default_turl);
2299 data.safe_for_autoreplace = true; // Can be replaced with built-in values. 2284 data.safe_for_autoreplace = true; // Can be replaced with built-in values.
2300 data.SetKeyword(ASCIIToUTF16("new_kw")); 2285 data.SetKeyword(ASCIIToUTF16("new_kw"));
2301 data.SetShortName(ASCIIToUTF16("my name")); 2286 data.SetShortName(ASCIIToUTF16("my name"));
2302 data.SetURL("http://wrong.url.com?q={searchTerms}"); 2287 data.SetURL("http://wrong.url.com?q={searchTerms}");
2303 data.date_created = Time::FromTimeT(50); 2288 data.date_created = Time::FromTimeT(50);
2304 data.last_modified = Time::FromTimeT(50); 2289 data.last_modified = Time::FromTimeT(50);
2305 data.sync_guid = "default"; 2290 data.sync_guid = "default";
2306 model()->Add(new TemplateURL(data)); 2291 model()->Add(base::MakeUnique<TemplateURL>(data));
2307 2292
2308 data.date_created = Time::FromTimeT(100); 2293 data.date_created = Time::FromTimeT(100);
2309 data.last_modified = Time::FromTimeT(100); 2294 data.last_modified = Time::FromTimeT(100);
2310 std::unique_ptr<TemplateURL> sync_turl(new TemplateURL(data)); 2295 std::unique_ptr<TemplateURL> sync_turl(new TemplateURL(data));
2311 syncer::SyncDataList list; 2296 syncer::SyncDataList list;
2312 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl)); 2297 list.push_back(TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl));
2313 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing( 2298 syncer::SyncMergeResult merge_result = model()->MergeDataAndStartSyncing(
2314 syncer::SEARCH_ENGINES, list, PassProcessor(), 2299 syncer::SEARCH_ENGINES, list, PassProcessor(),
2315 CreateAndPassSyncErrorFactory()); 2300 CreateAndPassSyncErrorFactory());
2316 2301
(...skipping 28 matching lines...) Expand all
2345 2330
2346 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) { 2331 TEST_F(TemplateURLServiceSyncTest, NonAsciiKeywordDoesNotCrash) {
2347 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"), 2332 model()->Add(CreateTestTemplateURL(UTF8ToUTF16("\xf0\xaf\xa6\x8d"),
2348 "http://key1.com")); 2333 "http://key1.com"));
2349 syncer::SyncDataList initial_data = CreateInitialSyncData(); 2334 syncer::SyncDataList initial_data = CreateInitialSyncData();
2350 2335
2351 model()->MergeDataAndStartSyncing( 2336 model()->MergeDataAndStartSyncing(
2352 syncer::SEARCH_ENGINES, initial_data, PassProcessor(), 2337 syncer::SEARCH_ENGINES, initial_data, PassProcessor(),
2353 CreateAndPassSyncErrorFactory()); 2338 CreateAndPassSyncErrorFactory());
2354 } 2339 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698