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

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

Issue 7826003: Retroactively addressed sky@'s comments from CL http://codereview.chromium.org/7566036/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Initial upload Created 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/template_url.h" 9 #include "chrome/browser/search_engines/template_url.h"
10 #include "chrome/browser/search_engines/template_url_service.h" 10 #include "chrome/browser/search_engines/template_url_service.h"
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 string16 original_turl_keyword = original_turl->keyword(); 316 string16 original_turl_keyword = original_turl->keyword();
317 model()->Add(original_turl); 317 model()->Add(original_turl);
318 318
319 // Create a key that does not conflict with something in the model. 319 // Create a key that does not conflict with something in the model.
320 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL("unique", 320 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL("unique",
321 "http://new.com")); 321 "http://new.com"));
322 string16 sync_keyword = sync_turl->keyword(); 322 string16 sync_keyword = sync_turl->keyword();
323 SyncChangeList changes; 323 SyncChangeList changes;
324 324
325 // No conflict, no TURLs changed, no changes. 325 // No conflict, no TURLs changed, no changes.
326 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), changes)); 326 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
327 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); 327 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
328 EXPECT_EQ(sync_keyword, sync_turl->keyword()); 328 EXPECT_EQ(sync_keyword, sync_turl->keyword());
329 EXPECT_EQ(0U, changes.size()); 329 EXPECT_EQ(0U, changes.size());
330 330
331 // Change sync keyword to something that conflicts, and make it older. 331 // Change sync keyword to something that conflicts, and make it older.
332 // Conflict, sync keyword is uniquified, and a SyncChange is added. 332 // Conflict, sync keyword is uniquified, and a SyncChange is added.
333 sync_turl->set_keyword(original_turl->keyword()); 333 sync_turl->set_keyword(original_turl->keyword());
334 sync_turl->set_last_modified(Time::FromTimeT(8999)); 334 sync_turl->set_last_modified(Time::FromTimeT(8999));
335 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), changes)); 335 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
336 EXPECT_NE(sync_keyword, sync_turl->keyword()); 336 EXPECT_NE(sync_keyword, sync_turl->keyword());
337 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); 337 EXPECT_EQ(original_turl_keyword, original_turl->keyword());
338 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); 338 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
339 EXPECT_EQ(1U, changes.size()); 339 EXPECT_EQ(1U, changes.size());
340 changes.clear(); 340 changes.clear();
341 341
342 // Sync is newer. Original TemplateURL keyword is uniquified, no SyncChange 342 // Sync is newer. Original TemplateURL keyword is uniquified, no SyncChange
343 // is added. 343 // is added.
344 sync_turl->set_keyword(original_turl->keyword()); 344 sync_turl->set_keyword(original_turl->keyword());
345 sync_keyword = sync_turl->keyword(); 345 sync_keyword = sync_turl->keyword();
346 sync_turl->set_last_modified(Time::FromTimeT(9001)); 346 sync_turl->set_last_modified(Time::FromTimeT(9001));
347 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), changes)); 347 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
348 EXPECT_EQ(sync_keyword, sync_turl->keyword()); 348 EXPECT_EQ(sync_keyword, sync_turl->keyword());
349 EXPECT_NE(original_turl_keyword, original_turl->keyword()); 349 EXPECT_NE(original_turl_keyword, original_turl->keyword());
350 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); 350 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
351 EXPECT_EQ(0U, changes.size()); 351 EXPECT_EQ(0U, changes.size());
352 352
353 // Equal times. Same result as above. Sync left alone, original uniquified so 353 // Equal times. Same result as above. Sync left alone, original uniquified so
354 // sync_turl can fit. 354 // sync_turl can fit.
355 sync_turl->set_keyword(original_turl->keyword()); 355 sync_turl->set_keyword(original_turl->keyword());
356 sync_keyword = sync_turl->keyword(); 356 sync_keyword = sync_turl->keyword();
357 // Note that we have to reset original_turl's last_modified time as it was 357 // Note that we have to reset original_turl's last_modified time as it was
358 // modified above. 358 // modified above.
359 original_turl->set_last_modified(Time::FromTimeT(9000)); 359 original_turl->set_last_modified(Time::FromTimeT(9000));
360 sync_turl->set_last_modified(Time::FromTimeT(9000)); 360 sync_turl->set_last_modified(Time::FromTimeT(9000));
361 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), changes)); 361 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), &changes));
362 EXPECT_EQ(sync_keyword, sync_turl->keyword()); 362 EXPECT_EQ(sync_keyword, sync_turl->keyword());
363 EXPECT_NE(original_turl_keyword, original_turl->keyword()); 363 EXPECT_NE(original_turl_keyword, original_turl->keyword());
364 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); 364 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword()));
365 EXPECT_EQ(0U, changes.size()); 365 EXPECT_EQ(0U, changes.size());
366 } 366 }
367 367
368 TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) { 368 TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) {
369 TemplateURL* original_turl = 369 TemplateURL* original_turl =
370 CreateTestTemplateURL("key1", "http://key1.com"); 370 CreateTestTemplateURL("key1", "http://key1.com");
371 model()->Add(original_turl); 371 model()->Add(original_turl);
(...skipping 24 matching lines...) Expand all
396 TEST_F(TemplateURLServiceSyncTest, MergeSyncAndLocalURLDuplicates) { 396 TEST_F(TemplateURLServiceSyncTest, MergeSyncAndLocalURLDuplicates) {
397 TemplateURL* original_turl = 397 TemplateURL* original_turl =
398 CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9000); 398 CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9000);
399 model()->Add(original_turl); 399 model()->Add(original_turl);
400 TemplateURL* sync_turl = 400 TemplateURL* sync_turl =
401 CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9001); 401 CreateTestTemplateURL("key1", "http://key1.com", std::string(), 9001);
402 SyncChangeList changes; 402 SyncChangeList changes;
403 403
404 // The sync TemplateURL is newer. It should replace the original TemplateURL. 404 // The sync TemplateURL is newer. It should replace the original TemplateURL.
405 // Note that MergeSyncAndLocalURLDuplicates takes ownership of sync_turl. 405 // Note that MergeSyncAndLocalURLDuplicates takes ownership of sync_turl.
406 model()->MergeSyncAndLocalURLDuplicates(sync_turl, original_turl, changes); 406 model()->MergeSyncAndLocalURLDuplicates(sync_turl, original_turl, &changes);
407 const TemplateURL* result = 407 const TemplateURL* result =
408 model()->GetTemplateURLForKeyword(UTF8ToUTF16("key1")); 408 model()->GetTemplateURLForKeyword(UTF8ToUTF16("key1"));
409 ASSERT_TRUE(result); 409 ASSERT_TRUE(result);
410 EXPECT_EQ(9001, result->last_modified().ToTimeT()); 410 EXPECT_EQ(9001, result->last_modified().ToTimeT());
411 EXPECT_EQ(0U, changes.size()); 411 EXPECT_EQ(0U, changes.size());
412 412
413 // The sync TemplateURL is older. The existing TemplateURL should win and a 413 // The sync TemplateURL is older. The existing TemplateURL should win and a
414 // SyncChange should be added to the list. 414 // SyncChange should be added to the list.
415 TemplateURL* sync_turl2 = 415 TemplateURL* sync_turl2 =
416 CreateTestTemplateURL("key1", "http://key1.com", std::string(), 8999); 416 CreateTestTemplateURL("key1", "http://key1.com", std::string(), 8999);
417 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, changes); 417 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes);
418 result = model()->GetTemplateURLForKeyword(UTF8ToUTF16("key1")); 418 result = model()->GetTemplateURLForKeyword(UTF8ToUTF16("key1"));
419 ASSERT_TRUE(result); 419 ASSERT_TRUE(result);
420 EXPECT_EQ(9001, result->last_modified().ToTimeT()); 420 EXPECT_EQ(9001, result->last_modified().ToTimeT());
421 EXPECT_EQ(1U, changes.size()); 421 EXPECT_EQ(1U, changes.size());
422 } 422 }
423 423
424 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) { 424 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) {
425 model()->MergeDataAndStartSyncing( 425 model()->MergeDataAndStartSyncing(
426 syncable::SEARCH_ENGINES, 426 syncable::SEARCH_ENGINES,
427 SyncDataList(), // Empty. 427 SyncDataList(), // Empty.
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after
972 ASSERT_FALSE(error.IsSet()); 972 ASSERT_FALSE(error.IsSet());
973 973
974 SyncChangeList changes; 974 SyncChangeList changes;
975 changes.push_back(CreateTestSyncChange( 975 changes.push_back(CreateTestSyncChange(
976 SyncChange::ACTION_UPDATE, 976 SyncChange::ACTION_UPDATE,
977 CreateTestTemplateURL("newkeyword", "http://new.com", "key2"))); 977 CreateTestTemplateURL("newkeyword", "http://new.com", "key2")));
978 processor()->set_erroneous(true); 978 processor()->set_erroneous(true);
979 error = model()->ProcessSyncChanges(FROM_HERE, changes); 979 error = model()->ProcessSyncChanges(FROM_HERE, changes);
980 EXPECT_TRUE(error.IsSet()); 980 EXPECT_TRUE(error.IsSet());
981 } 981 }
982
983 TEST_F(TemplateURLServiceSyncTest, MergeTwiceWithSameSyncData) {
984 // Ensure that a second merge with the same data as the first does not
985 // actually update the local data.
986 SyncDataList initial_data;
987 initial_data.push_back(CreateInitialSyncData().at(0));
988 TemplateURL* turl = TemplateURLService::CreateTemplateURLFromSyncData(
989 initial_data.at(0));
990 ASSERT_TRUE(turl);
991 turl->set_last_modified(Time::FromTimeT(10)); // earlier
992 model()->Add(turl);
993
994 SyncError error = model()->MergeDataAndStartSyncing(
995 syncable::SEARCH_ENGINES,
996 initial_data,
997 processor());
998 ASSERT_FALSE(error.IsSet());
999
1000 // We should have updated the original TemplateURL with Sync's version.
1001 // Keep a copy of it so we can compare it after we re-merge.
1002 ASSERT_TRUE(model()->GetTemplateURLForGUID("key1"));
1003 TemplateURL updated_turl(*model()->GetTemplateURLForGUID("key1"));
1004 EXPECT_EQ(Time::FromTimeT(90), updated_turl.last_modified());
1005
1006 // Modify a single field of the initial data. This should not be updated in
1007 // the second merge, as the last_modified timestamp remains the same.
1008 scoped_ptr<TemplateURL> temp_turl(
1009 TemplateURLService::CreateTemplateURLFromSyncData(initial_data.at(0)));
1010 temp_turl->set_short_name(UTF8ToUTF16("SomethingDifferent"));
1011 initial_data.clear();
1012 initial_data.push_back(
1013 TemplateURLService::CreateSyncDataFromTemplateURL(*temp_turl));
1014
1015 // Remerge the data again. This simulates shutting down and syncing again
1016 // at a different time, but the cloud data has not changed.
1017 model()->StopSyncing(syncable::SEARCH_ENGINES);
1018 error = model()->MergeDataAndStartSyncing(syncable::SEARCH_ENGINES,
1019 initial_data,
1020 processor());
1021 ASSERT_FALSE(error.IsSet());
1022
1023 // Check that the TemplateURL was not modified.
1024 const TemplateURL* reupdated_turl = model()->GetTemplateURLForGUID("key1");
1025 ASSERT_TRUE(reupdated_turl);
1026 AssertEquals(updated_turl, *reupdated_turl);
1027 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698