Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/memory/scoped_vector.h" | 6 #include "base/memory/scoped_vector.h" |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/time.h" | 8 #include "base/time.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/search_engines/search_terms_data.h" | 10 #include "chrome/browser/search_engines/search_terms_data.h" |
| (...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 | 464 |
| 465 // If we force the method, it should uniquify the keyword even if it is | 465 // If we force the method, it should uniquify the keyword even if it is |
| 466 // currently unique, and skip the host-based autogenerated keyword. | 466 // currently unique, and skip the host-based autogenerated keyword. |
| 467 turl.reset( | 467 turl.reset( |
| 468 CreateTestTemplateURL(ASCIIToUTF16("unique"), "http://unique.com")); | 468 CreateTestTemplateURL(ASCIIToUTF16("unique"), "http://unique.com")); |
| 469 new_keyword = model()->UniquifyKeyword(*turl, true); | 469 new_keyword = model()->UniquifyKeyword(*turl, true); |
| 470 EXPECT_EQ(ASCIIToUTF16("unique_"), new_keyword); | 470 EXPECT_EQ(ASCIIToUTF16("unique_"), new_keyword); |
| 471 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); | 471 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(new_keyword)); |
| 472 } | 472 } |
| 473 | 473 |
| 474 TEST_F(TemplateURLServiceSyncTest, SyncKeywordConflictNeitherAutoreplace) { | 474 TEST_F(TemplateURLServiceSyncTest, IsLocalTemplateURLBetter) { |
| 475 // Test some edge cases of this function. | |
| 476 const struct { | |
| 477 time_t local_time; | |
| 478 time_t sync_time; | |
| 479 bool local_is_default; | |
| 480 bool local_created_by_policy; | |
| 481 bool expected_result; | |
| 482 } test_cases[] = { | |
| 483 // Sync is better by timestamp but local is Default. | |
| 484 {10, 100, true, false, true}, | |
| 485 // Sync is better by timestamp but local is Create by Policy. | |
| 486 {10, 100, false, true, true}, | |
| 487 // Tie. Sync wins. | |
| 488 {100, 100, false, false, false}, | |
| 489 }; | |
| 490 | |
| 491 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | |
| 492 TemplateURL* local_turl = CreateTestTemplateURL( | |
| 493 ASCIIToUTF16("localkey"), "www.local.com", "localguid", | |
| 494 test_cases[i].local_time, true, test_cases[i].local_created_by_policy); | |
| 495 model()->Add(local_turl); | |
| 496 if (test_cases[i].local_is_default) | |
| 497 model()->SetDefaultSearchProvider(local_turl); | |
| 498 | |
| 499 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL( | |
| 500 ASCIIToUTF16("synckey"), "www.sync.com", "syncguid", | |
| 501 test_cases[i].sync_time)); | |
| 502 EXPECT_EQ(test_cases[i].expected_result, | |
| 503 model()->IsLocalTemplateURLBetter(local_turl, sync_turl.get())); | |
| 504 | |
| 505 // Undo the changes. | |
| 506 if (test_cases[i].local_is_default) | |
| 507 model()->SetDefaultSearchProvider(NULL); | |
| 508 model()->Remove(local_turl); | |
| 509 } | |
| 510 } | |
| 511 | |
| 512 TEST_F(TemplateURLServiceSyncTest, ResolveSyncKeywordConflict) { | |
| 475 // This tests cases where neither the sync nor the local TemplateURL are | 513 // This tests cases where neither the sync nor the local TemplateURL are |
| 476 // marked safe_for_autoreplace. | 514 // marked safe_for_autoreplace. |
| 477 | 515 |
| 478 // Create a keyword that conflicts, and make it older. Sync keyword is | 516 // Create a keyword that conflicts, and make it older. Sync keyword is |
| 479 // uniquified, and a syncer::SyncChange is added. | 517 // uniquified, and a syncer::SyncChange is added. |
| 480 string16 original_turl_keyword = ASCIIToUTF16("key1"); | 518 string16 original_turl_keyword = ASCIIToUTF16("key1"); |
| 481 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword, | 519 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword, |
| 482 "http://key1.com", std::string(), 9000); | 520 "http://key1.com", std::string(), 9000); |
| 483 model()->Add(original_turl); | 521 model()->Add(original_turl); |
| 484 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(original_turl_keyword, | 522 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(original_turl_keyword, |
| 485 "http://new.com", "remote", 8999)); | 523 "http://new.com", "remote", 8999)); |
| 486 syncer::SyncChangeList changes; | 524 syncer::SyncChangeList changes; |
| 487 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | 525 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); |
| 488 original_turl, &changes)); | |
| 489 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); | 526 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); |
| 490 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); | 527 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); |
| 491 ASSERT_EQ(1U, changes.size()); | 528 ASSERT_EQ(1U, changes.size()); |
| 492 EXPECT_EQ("remote", GetGUID(changes[0].sync_data())); | 529 EXPECT_EQ("remote", GetGUID(changes[0].sync_data())); |
| 493 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); | 530 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); |
| 494 changes.clear(); | 531 changes.clear(); |
| 495 model()->Remove(original_turl); | 532 model()->Remove(original_turl); |
| 496 | 533 |
| 497 // Sync is newer. Original TemplateURL keyword is uniquified. A SyncChange | 534 // Sync is newer. Original TemplateURL keyword is uniquified. A SyncChange |
| 498 // is added (which in a normal run would be deleted by PruneSyncChanges() when | 535 // is added (which in a normal run would be deleted by PruneSyncChanges() when |
| 499 // the local GUID doesn't appear in the sync GUID list). Also ensure that | 536 // the local GUID doesn't appear in the sync GUID list). Also ensure that |
| 500 // this does not change the safe_for_autoreplace flag or the TemplateURLID in | 537 // this does not change the safe_for_autoreplace flag or the TemplateURLID in |
| 501 // the original. | 538 // the original. |
| 502 original_turl = CreateTestTemplateURL(original_turl_keyword, | 539 original_turl = CreateTestTemplateURL(original_turl_keyword, |
| 503 "http://key1.com", "local", 9000); | 540 "http://key1.com", "local", 9000); |
| 504 model()->Add(original_turl); | 541 model()->Add(original_turl); |
| 505 TemplateURLID original_id = original_turl->id(); | 542 TemplateURLID original_id = original_turl->id(); |
| 506 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | 543 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", |
| 507 std::string(), 9001)); | 544 std::string(), 9001)); |
| 508 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | 545 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); |
| 509 original_turl, &changes)); | |
| 510 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); | 546 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); |
| 511 EXPECT_NE(original_turl_keyword, original_turl->keyword()); | 547 EXPECT_NE(original_turl_keyword, original_turl->keyword()); |
| 512 EXPECT_FALSE(original_turl->safe_for_autoreplace()); | 548 EXPECT_FALSE(original_turl->safe_for_autoreplace()); |
| 513 EXPECT_EQ(original_id, original_turl->id()); | 549 EXPECT_EQ(original_id, original_turl->id()); |
| 514 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); | 550 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); |
| 515 ASSERT_EQ(1U, changes.size()); | 551 ASSERT_EQ(1U, changes.size()); |
| 516 EXPECT_EQ("local", GetGUID(changes[0].sync_data())); | 552 EXPECT_EQ("local", GetGUID(changes[0].sync_data())); |
| 517 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); | 553 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); |
| 518 changes.clear(); | 554 changes.clear(); |
| 519 model()->Remove(original_turl); | 555 model()->Remove(original_turl); |
| 520 | 556 |
| 521 // Equal times. Same result as above. Sync left alone, original uniquified so | 557 // Equal times. Same result as above. Sync left alone, original uniquified so |
| 522 // sync_turl can fit. | 558 // sync_turl can fit. |
| 523 original_turl = CreateTestTemplateURL(original_turl_keyword, | 559 original_turl = CreateTestTemplateURL(original_turl_keyword, |
| 524 "http://key1.com", "local2", 9000); | 560 "http://key1.com", "local2", 9000); |
| 525 model()->Add(original_turl); | 561 model()->Add(original_turl); |
| 526 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | 562 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", |
| 527 std::string(), 9000)); | 563 std::string(), 9000)); |
| 528 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | 564 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); |
| 529 original_turl, &changes)); | |
| 530 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); | 565 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); |
| 531 EXPECT_NE(original_turl_keyword, original_turl->keyword()); | 566 EXPECT_NE(original_turl_keyword, original_turl->keyword()); |
| 532 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); | 567 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(original_turl_keyword)); |
| 533 ASSERT_EQ(1U, changes.size()); | 568 ASSERT_EQ(1U, changes.size()); |
| 534 EXPECT_EQ("local2", GetGUID(changes[0].sync_data())); | 569 EXPECT_EQ("local2", GetGUID(changes[0].sync_data())); |
| 535 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); | 570 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); |
| 536 changes.clear(); | 571 changes.clear(); |
| 537 model()->Remove(original_turl); | 572 model()->Remove(original_turl); |
| 538 | 573 |
| 539 // Sync is newer, but original TemplateURL is created by policy, so it wins. | 574 // Sync is newer, but original TemplateURL is created by policy, so it wins. |
| 540 // Sync keyword is uniquified, and a syncer::SyncChange is added. | 575 // Sync keyword is uniquified, and a syncer::SyncChange is added. |
| 541 original_turl = CreateTestTemplateURL(original_turl_keyword, | 576 original_turl = CreateTestTemplateURL(original_turl_keyword, |
| 542 "http://key1.com", std::string(), 9000, false, true); | 577 "http://key1.com", std::string(), 9000, false, true); |
| 543 model()->Add(original_turl); | 578 model()->Add(original_turl); |
| 544 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | 579 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", |
| 545 "remote2", 9999)); | 580 "remote2", 9999)); |
| 546 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | 581 model()->ResolveSyncKeywordConflict(sync_turl.get(), original_turl, &changes); |
| 547 original_turl, &changes)); | |
| 548 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); | 582 EXPECT_NE(original_turl_keyword, sync_turl->keyword()); |
| 549 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); | 583 EXPECT_EQ(original_turl_keyword, original_turl->keyword()); |
| 550 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); | 584 EXPECT_EQ(NULL, model()->GetTemplateURLForKeyword(sync_turl->keyword())); |
| 551 ASSERT_EQ(1U, changes.size()); | 585 ASSERT_EQ(1U, changes.size()); |
| 552 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data())); | 586 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data())); |
| 553 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); | 587 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, changes[0].change_type()); |
| 554 changes.clear(); | 588 changes.clear(); |
| 555 model()->Remove(original_turl); | 589 model()->Remove(original_turl); |
| 556 } | 590 } |
| 557 | 591 |
| 558 TEST_F(TemplateURLServiceSyncTest, SyncKeywordConflictBothAutoreplace) { | |
| 559 // This tests cases where both the sync and the local TemplateURL are marked | |
| 560 // safe_for_autoreplace. | |
| 561 | |
| 562 // Create a keyword that conflicts, and make it older. SyncChange is added, | |
| 563 // function returns false. | |
| 564 string16 original_turl_keyword = ASCIIToUTF16("key1"); | |
| 565 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 566 "http://key1.com", std::string(), 9000, true); | |
| 567 model()->Add(original_turl); | |
| 568 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(original_turl_keyword, | |
| 569 "http://new.com", "remote", 8999, true)); | |
| 570 syncer::SyncChangeList changes; | |
| 571 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 572 original_turl, &changes)); | |
| 573 EXPECT_EQ(original_turl, | |
| 574 model()->GetTemplateURLForKeyword(original_turl_keyword)); | |
| 575 ASSERT_EQ(1U, changes.size()); | |
| 576 EXPECT_EQ("remote", GetGUID(changes[0].sync_data())); | |
| 577 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 578 changes.clear(); | |
| 579 model()->Remove(original_turl); | |
| 580 | |
| 581 // Sync is newer. Original TemplateURL is removed from the model. A | |
| 582 // syncer::SyncChange is added (which in a normal run would be deleted by | |
| 583 // PruneSyncChanges() when the local GUID doesn't appear in the sync GUID | |
| 584 // list). | |
| 585 original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 586 "http://key1.com", "local", 9000, true); | |
| 587 model()->Add(original_turl); | |
| 588 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | |
| 589 std::string(), 9001, true)); | |
| 590 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 591 original_turl, &changes)); | |
| 592 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); | |
| 593 EXPECT_TRUE(model()->GetTemplateURLs().empty()); | |
| 594 ASSERT_EQ(1U, changes.size()); | |
| 595 EXPECT_EQ("local", GetGUID(changes[0].sync_data())); | |
| 596 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 597 changes.clear(); | |
| 598 | |
| 599 // Equal times. Same result as above. Sync left alone, original removed so | |
| 600 // sync_turl can fit. | |
| 601 original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 602 "http://key1.com", "local2", 9000, true); | |
| 603 model()->Add(original_turl); | |
| 604 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | |
| 605 std::string(), 9000, true)); | |
| 606 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 607 original_turl, &changes)); | |
| 608 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); | |
| 609 EXPECT_TRUE(model()->GetTemplateURLs().empty()); | |
| 610 ASSERT_EQ(1U, changes.size()); | |
| 611 EXPECT_EQ("local2", GetGUID(changes[0].sync_data())); | |
| 612 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 613 changes.clear(); | |
| 614 | |
| 615 // Sync is newer, but original TemplateURL is created by policy, so it wins. | |
| 616 // syncer::SyncChange is added, function returns false. | |
| 617 original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 618 "http://key1.com", std::string(), 9000, true, true); | |
| 619 model()->Add(original_turl); | |
| 620 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | |
| 621 "remote2", 9999, true)); | |
| 622 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 623 original_turl, &changes)); | |
| 624 EXPECT_EQ(original_turl, | |
| 625 model()->GetTemplateURLForKeyword(original_turl_keyword)); | |
| 626 ASSERT_EQ(1U, changes.size()); | |
| 627 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data())); | |
| 628 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 629 changes.clear(); | |
| 630 model()->Remove(original_turl); | |
| 631 } | |
| 632 | |
| 633 TEST_F(TemplateURLServiceSyncTest, SyncKeywordConflictOneAutoreplace) { | |
| 634 // This tests cases where either the sync or the local TemplateURL is marked | |
| 635 // safe_for_autoreplace, but the other is not. Basically, we run the same | |
| 636 // tests as in SyncKeywordConflictBothAutoreplace, but mark the keywords so as | |
| 637 // to reverse the outcome of each test. | |
| 638 | |
| 639 // Create a keyword that conflicts, and make it older. Normally the local | |
| 640 // TemplateURL would win this. | |
| 641 string16 original_turl_keyword = ASCIIToUTF16("key1"); | |
| 642 TemplateURL* original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 643 "http://key1.com", "local", 9000, true); | |
| 644 model()->Add(original_turl); | |
| 645 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(original_turl_keyword, | |
| 646 "http://new.com", std::string(), 8999)); | |
| 647 syncer::SyncChangeList changes; | |
| 648 EXPECT_TRUE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 649 original_turl, &changes)); | |
| 650 EXPECT_EQ(original_turl_keyword, sync_turl->keyword()); | |
| 651 EXPECT_TRUE(model()->GetTemplateURLs().empty()); | |
| 652 ASSERT_EQ(1U, changes.size()); | |
| 653 EXPECT_EQ("local", GetGUID(changes[0].sync_data())); | |
| 654 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 655 changes.clear(); | |
| 656 | |
| 657 // Sync is newer. Normally the sync TemplateURL would win this. | |
| 658 original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 659 "http://key1.com", std::string(), 9000); | |
| 660 model()->Add(original_turl); | |
| 661 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | |
| 662 "remote", 9001, true)); | |
| 663 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 664 original_turl, &changes)); | |
| 665 EXPECT_EQ(original_turl, | |
| 666 model()->GetTemplateURLForKeyword(original_turl_keyword)); | |
| 667 ASSERT_EQ(1U, changes.size()); | |
| 668 EXPECT_EQ("remote", GetGUID(changes[0].sync_data())); | |
| 669 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 670 changes.clear(); | |
| 671 model()->Remove(original_turl); | |
| 672 | |
| 673 // Equal times. Same result as above. | |
| 674 original_turl = CreateTestTemplateURL(original_turl_keyword, | |
| 675 "http://key1.com", std::string(), 9000); | |
| 676 model()->Add(original_turl); | |
| 677 sync_turl.reset(CreateTestTemplateURL(original_turl_keyword, "http://new.com", | |
| 678 "remote2", 9000, true)); | |
| 679 EXPECT_FALSE(model()->ResolveSyncKeywordConflict(sync_turl.get(), | |
| 680 original_turl, &changes)); | |
| 681 EXPECT_EQ(original_turl, | |
| 682 model()->GetTemplateURLForKeyword(original_turl_keyword)); | |
| 683 ASSERT_EQ(1U, changes.size()); | |
| 684 EXPECT_EQ("remote2", GetGUID(changes[0].sync_data())); | |
| 685 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 686 changes.clear(); | |
| 687 model()->Remove(original_turl); | |
| 688 | |
| 689 // We don't run the "created by policy" test since URLs created by policy are | |
| 690 // never safe_for_autoreplace. | |
| 691 } | |
| 692 | |
| 693 TEST_F(TemplateURLServiceSyncTest, FindDuplicateOfSyncTemplateURL) { | |
| 694 TemplateURL* original_turl = | |
| 695 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com"); | |
| 696 model()->Add(original_turl); | |
| 697 | |
| 698 // No matches at all. | |
| 699 scoped_ptr<TemplateURL> sync_turl(CreateTestTemplateURL(ASCIIToUTF16("key2"), | |
| 700 "http://key2.com")); | |
| 701 EXPECT_EQ(NULL, model()->FindDuplicateOfSyncTemplateURL(*sync_turl)); | |
| 702 | |
| 703 // URL matches, but not keyword. No dupe. | |
| 704 sync_turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), | |
| 705 "http://key1.com")); | |
| 706 EXPECT_EQ(NULL, model()->FindDuplicateOfSyncTemplateURL(*sync_turl)); | |
| 707 | |
| 708 // Keyword matches, but not URL. No dupe. | |
| 709 sync_turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key1"), | |
| 710 "http://key2.com")); | |
| 711 EXPECT_EQ(NULL, model()->FindDuplicateOfSyncTemplateURL(*sync_turl)); | |
| 712 | |
| 713 // Duplicate. | |
| 714 sync_turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key1"), | |
| 715 "http://key1.com")); | |
| 716 const TemplateURL* dupe_turl = | |
| 717 model()->FindDuplicateOfSyncTemplateURL(*sync_turl); | |
| 718 ASSERT_TRUE(dupe_turl); | |
| 719 EXPECT_EQ(dupe_turl->keyword(), sync_turl->keyword()); | |
| 720 EXPECT_EQ(dupe_turl->url(), sync_turl->url()); | |
| 721 } | |
| 722 | |
| 723 TEST_F(TemplateURLServiceSyncTest, MergeSyncAndLocalURLDuplicates) { | |
| 724 TemplateURL* original_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), | |
| 725 "http://key1.com", std::string(), 9000); | |
| 726 model()->Add(original_turl); | |
| 727 TemplateURL* sync_turl = CreateTestTemplateURL(ASCIIToUTF16("key1"), | |
| 728 "http://key1.com", std::string(), 9001); | |
| 729 std::string original_guid = original_turl->sync_guid(); | |
| 730 syncer::SyncChangeList changes; | |
| 731 | |
| 732 // The sync TemplateURL is newer. It should replace the original TemplateURL | |
| 733 // and a syncer::SyncChange should be added to the list. | |
| 734 // Note that MergeSyncAndLocalURLDuplicates takes ownership of sync_turl. | |
| 735 model()->MergeSyncAndLocalURLDuplicates(sync_turl, original_turl, &changes); | |
| 736 TemplateURL* result = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); | |
| 737 ASSERT_TRUE(result); | |
| 738 EXPECT_EQ(9001, result->last_modified().ToTimeT()); | |
| 739 EXPECT_EQ(1U, changes.size()); | |
| 740 // We expect a change to delete the local entry. | |
| 741 syncer::SyncChange change = changes.at(0); | |
| 742 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, change.change_type()); | |
| 743 EXPECT_EQ(original_guid, | |
| 744 change.sync_data().GetSpecifics().search_engine().sync_guid()); | |
| 745 changes.clear(); | |
| 746 | |
| 747 // The sync TemplateURL is older. The existing TemplateURL should win and a | |
| 748 // syncer::SyncChange should be added to the list. | |
| 749 TemplateURL* sync_turl2 = CreateTestTemplateURL(ASCIIToUTF16("key1"), | |
| 750 "http://key1.com", std::string(), 8999); | |
| 751 std::string sync_guid = sync_turl2->sync_guid(); | |
| 752 model()->MergeSyncAndLocalURLDuplicates(sync_turl2, sync_turl, &changes); | |
| 753 result = model()->GetTemplateURLForKeyword(ASCIIToUTF16("key1")); | |
| 754 ASSERT_TRUE(result); | |
| 755 EXPECT_EQ(9001, result->last_modified().ToTimeT()); | |
| 756 EXPECT_EQ(1U, changes.size()); | |
| 757 // We expect a change to update the sync entry. | |
| 758 change = changes.at(0); | |
| 759 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, change.change_type()); | |
| 760 EXPECT_EQ(sync_guid, | |
| 761 change.sync_data().GetSpecifics().search_engine().sync_guid()); | |
| 762 } | |
| 763 | |
| 764 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) { | 592 TEST_F(TemplateURLServiceSyncTest, StartSyncEmpty) { |
| 765 model()->MergeDataAndStartSyncing( | 593 model()->MergeDataAndStartSyncing( |
| 766 syncer::SEARCH_ENGINES, syncer::SyncDataList(), | 594 syncer::SEARCH_ENGINES, syncer::SyncDataList(), |
| 767 PassProcessor(), CreateAndPassSyncErrorFactory()); | 595 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 768 | 596 |
| 769 EXPECT_EQ(0U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 597 EXPECT_EQ(0U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 770 EXPECT_EQ(0U, processor()->change_list_size()); | 598 EXPECT_EQ(0U, processor()->change_list_size()); |
| 771 } | 599 } |
| 772 | 600 |
| 773 TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) { | 601 TEST_F(TemplateURLServiceSyncTest, MergeIntoEmpty) { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 891 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), | 719 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), |
| 892 "http://expected.com", "bbb", 100)); // keyword conflict | 720 "http://expected.com", "bbb", 100)); // keyword conflict |
| 893 | 721 |
| 894 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), | 722 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), |
| 895 "http://unique.com", "ccc")); // add | 723 "http://unique.com", "ccc")); // add |
| 896 | 724 |
| 897 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, | 725 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, |
| 898 CreateInitialSyncData(), PassProcessor(), | 726 CreateInitialSyncData(), PassProcessor(), |
| 899 CreateAndPassSyncErrorFactory()); | 727 CreateAndPassSyncErrorFactory()); |
| 900 | 728 |
| 901 // The dupe results in a merge. The other two should be added to the model. | 729 // The dupe and conflict results in merges, as local values are always merged |
| 902 EXPECT_EQ(5U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 730 // with sync values if there is a keyword conflict. The unique keyword should |
| 731 // be added. | |
| 732 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | |
| 903 | 733 |
| 904 // The key1 duplicate results in the local copy winning. Ensure that Sync's | 734 // The key1 duplicate results in the local copy winning. Ensure that Sync's |
| 905 // copy was not added, and the local copy is pushed upstream to Sync as an | 735 // copy was not added, and the local copy is pushed upstream to Sync as an |
| 906 // update. The local copy should have received the sync data's GUID. | 736 // update. The local copy should have received the sync data's GUID. |
| 907 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); | 737 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); |
| 908 // Check changes for the UPDATE. | 738 // Check changes for the UPDATE. |
| 909 ASSERT_TRUE(processor()->contains_guid("key1")); | 739 ASSERT_TRUE(processor()->contains_guid("key1")); |
| 910 syncer::SyncChange key1_change = processor()->change_for_guid("key1"); | 740 syncer::SyncChange key1_change = processor()->change_for_guid("key1"); |
| 911 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key1_change.change_type()); | 741 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key1_change.change_type()); |
| 742 // The local sync_guid should no longer be found. | |
| 912 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); | 743 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); |
| 913 | 744 |
| 914 // The key2 keyword conflict results in the local copy winning, so ensure it | 745 // The key2 keyword conflict results in a merge, with the values of the local |
| 915 // retains the original keyword, and that an update to the sync copy is pushed | 746 // copy winning, so ensure it retains the original URL, and that an update to |
| 916 // upstream to Sync. Both TemplateURLs should be found locally, however. | 747 // the sync guid is pushed upstream to Sync. |
| 917 const TemplateURL* key2 = model()->GetTemplateURLForGUID("bbb"); | 748 const TemplateURL* key2 = model()->GetTemplateURLForGUID("key2"); |
| 918 EXPECT_TRUE(key2); | 749 ASSERT_TRUE(key2); |
| 919 EXPECT_EQ(ASCIIToUTF16("key2"), key2->keyword()); | 750 EXPECT_EQ(ASCIIToUTF16("key2"), key2->keyword()); |
| 920 EXPECT_TRUE(model()->GetTemplateURLForGUID("key2")); | 751 EXPECT_EQ("http://expected.com", key2->url()); |
| 921 // Check changes for the UPDATE. | 752 // Check changes for the UPDATE. |
| 922 ASSERT_TRUE(processor()->contains_guid("key2")); | 753 ASSERT_TRUE(processor()->contains_guid("key2")); |
| 923 syncer::SyncChange key2_change = processor()->change_for_guid("key2"); | 754 syncer::SyncChange key2_change = processor()->change_for_guid("key2"); |
| 924 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key2_change.change_type()); | 755 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key2_change.change_type()); |
| 925 EXPECT_EQ("key2.com", GetKeyword(key2_change.sync_data())); | 756 EXPECT_EQ("key2", GetKeyword(key2_change.sync_data())); |
| 757 EXPECT_EQ("http://expected.com", GetURL(key2_change.sync_data())); | |
| 758 // The local sync_guid should no longer be found. | |
| 759 EXPECT_FALSE(model()->GetTemplateURLForGUID("bbb")); | |
| 926 | 760 |
| 927 // The last TemplateURL should have had no conflicts and was just added. It | 761 // The last TemplateURL should have had no conflicts and was just added. It |
| 928 // should not have replaced the third local TemplateURL. | 762 // should not have replaced the third local TemplateURL. |
| 929 EXPECT_TRUE(model()->GetTemplateURLForGUID("ccc")); | 763 EXPECT_TRUE(model()->GetTemplateURLForGUID("ccc")); |
| 930 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); | 764 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); |
| 931 | 765 |
| 932 // Two UPDATEs and two ADDs. | 766 // Two UPDATEs and one ADD. |
| 933 EXPECT_EQ(4U, processor()->change_list_size()); | 767 EXPECT_EQ(3U, processor()->change_list_size()); |
| 934 // Two ADDs should be pushed up to Sync. | 768 // One ADDs should be pushed up to Sync. |
| 935 ASSERT_TRUE(processor()->contains_guid("bbb")); | |
| 936 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, | |
| 937 processor()->change_for_guid("bbb").change_type()); | |
| 938 ASSERT_TRUE(processor()->contains_guid("ccc")); | 769 ASSERT_TRUE(processor()->contains_guid("ccc")); |
| 939 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, | 770 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, |
| 940 processor()->change_for_guid("ccc").change_type()); | 771 processor()->change_for_guid("ccc").change_type()); |
| 941 } | 772 } |
| 942 | 773 |
| 943 TEST_F(TemplateURLServiceSyncTest, MergeAddFromNewerSyncData) { | 774 TEST_F(TemplateURLServiceSyncTest, MergeAddFromNewerSyncData) { |
| 944 // GUIDs all differ, so this is data to be added from Sync, but the timestamps | 775 // GUIDs all differ, so Sync may overtake some entries, but the timestamps |
| 945 // from Sync are newer. Set up the local data so that one is a dupe, one has a | 776 // from Sync are newer. Set up the local data so that one is a dupe, one has a |
| 946 // conflicting keyword, and the last has no conflicts (a clean ADD). | 777 // conflicting keyword, and the last has no conflicts (a clean ADD). |
| 947 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", | 778 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", |
| 948 "aaa", 10)); // dupe | 779 "aaa", 10)); // dupe |
| 949 | 780 |
| 950 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), | 781 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("key2"), |
| 951 "http://expected.com", "bbb", 10)); // keyword conflict | 782 "http://expected.com", "bbb", 10)); // keyword conflict |
| 952 | 783 |
| 953 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), | 784 model()->Add(CreateTestTemplateURL(ASCIIToUTF16("unique"), |
| 954 "http://unique.com", "ccc", 10)); // add | 785 "http://unique.com", "ccc", 10)); // add |
| 955 | 786 |
| 956 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, | 787 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, |
| 957 CreateInitialSyncData(), PassProcessor(), | 788 CreateInitialSyncData(), PassProcessor(), |
| 958 CreateAndPassSyncErrorFactory()); | 789 CreateAndPassSyncErrorFactory()); |
| 959 | 790 |
| 960 // The dupe results in a merge. The other two should be added to the model. | 791 // The dupe and keyword conflict results in merges. The unique keyword be |
| 961 EXPECT_EQ(5U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 792 // added to the model. |
| 793 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | |
| 962 | 794 |
| 963 // The key1 duplicate results in Sync's copy winning. Ensure that Sync's | 795 // The key1 duplicate results in Sync's copy winning. Ensure that Sync's |
| 964 // copy replaced the local copy. | 796 // copy replaced the local copy. |
| 965 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); | 797 EXPECT_TRUE(model()->GetTemplateURLForGUID("key1")); |
| 966 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); | 798 EXPECT_FALSE(model()->GetTemplateURLForGUID("aaa")); |
| 799 EXPECT_FALSE(processor()->contains_guid("key1")); | |
| 800 EXPECT_FALSE(processor()->contains_guid("aaa")); | |
| 967 | 801 |
| 968 // The key2 keyword conflict results in Sync's copy winning, so ensure it | 802 // The key2 keyword conflict results in Sync's copy winning, so ensure it |
| 969 // retains the original keyword. The local copy should get a uniquified | 803 // retains the original keyword and is added. The local copy should be |
| 970 // keyword. Both TemplateURLs should be found locally. | 804 // removed. |
| 971 const TemplateURL* key2_sync = model()->GetTemplateURLForGUID("key2"); | 805 const TemplateURL* key2_sync = model()->GetTemplateURLForGUID("key2"); |
| 972 ASSERT_TRUE(key2_sync); | 806 ASSERT_TRUE(key2_sync); |
| 973 EXPECT_EQ(ASCIIToUTF16("key2"), key2_sync->keyword()); | 807 EXPECT_EQ(ASCIIToUTF16("key2"), key2_sync->keyword()); |
| 974 const TemplateURL* key2_local = model()->GetTemplateURLForGUID("bbb"); | 808 EXPECT_FALSE(model()->GetTemplateURLForGUID("bbb")); |
| 975 ASSERT_TRUE(key2_local); | |
| 976 EXPECT_EQ(ASCIIToUTF16("expected.com"), key2_local->keyword()); | |
| 977 | 809 |
| 978 // The last TemplateURL should have had no conflicts and was just added. It | 810 // The last TemplateURL should have had no conflicts and was just added. It |
| 979 // should not have replaced the third local TemplateURL. | 811 // should not have replaced the third local TemplateURL. |
| 980 EXPECT_TRUE(model()->GetTemplateURLForGUID("ccc")); | 812 EXPECT_TRUE(model()->GetTemplateURLForGUID("ccc")); |
| 981 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); | 813 EXPECT_TRUE(model()->GetTemplateURLForGUID("key3")); |
| 982 | 814 |
| 983 // Two ADDs. | 815 // One ADD. |
| 984 EXPECT_EQ(2U, processor()->change_list_size()); | 816 EXPECT_EQ(1U, processor()->change_list_size()); |
| 985 // Two ADDs should be pushed up to Sync. | 817 // One ADDs should be pushed up to Sync. |
| 986 ASSERT_TRUE(processor()->contains_guid("bbb")); | |
| 987 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, | |
| 988 processor()->change_for_guid("bbb").change_type()); | |
| 989 ASSERT_TRUE(processor()->contains_guid("ccc")); | 818 ASSERT_TRUE(processor()->contains_guid("ccc")); |
| 990 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, | 819 EXPECT_EQ(syncer::SyncChange::ACTION_ADD, |
| 991 processor()->change_for_guid("ccc").change_type()); | 820 processor()->change_for_guid("ccc").change_type()); |
| 992 } | 821 } |
| 993 | 822 |
| 994 TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) { | 823 TEST_F(TemplateURLServiceSyncTest, ProcessChangesEmptyModel) { |
| 995 // We initially have no data. | 824 // We initially have no data. |
| 996 model()->MergeDataAndStartSyncing( | 825 model()->MergeDataAndStartSyncing( |
| 997 syncer::SEARCH_ENGINES, syncer::SyncDataList(), | 826 syncer::SEARCH_ENGINES, syncer::SyncDataList(), |
| 998 PassProcessor(), CreateAndPassSyncErrorFactory()); | 827 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1128 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3"))); | 957 model()->GetTemplateURLForKeyword(ASCIIToUTF16("key3"))); |
| 1129 | 958 |
| 1130 ASSERT_TRUE(processor()->contains_guid("aaa")); | 959 ASSERT_TRUE(processor()->contains_guid("aaa")); |
| 1131 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, | 960 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, |
| 1132 processor()->change_for_guid("aaa").change_type()); | 961 processor()->change_for_guid("aaa").change_type()); |
| 1133 ASSERT_TRUE(processor()->contains_guid("key1")); | 962 ASSERT_TRUE(processor()->contains_guid("key1")); |
| 1134 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, | 963 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, |
| 1135 processor()->change_for_guid("key1").change_type()); | 964 processor()->change_for_guid("key1").change_type()); |
| 1136 } | 965 } |
| 1137 | 966 |
| 1138 TEST_F(TemplateURLServiceSyncTest, RemoveUpdatedURLOnConflict) { | |
| 1139 // Updating a local replaceable URL to have the same keyword as a local | |
| 1140 // non-replaceable URL should result in the former being removed from the | |
| 1141 // model entirely. | |
| 1142 syncer::SyncDataList initial_data; | |
| 1143 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("sync"), | |
| 1144 "http://sync.com", "sync", 100, true)); | |
| 1145 initial_data.push_back( | |
| 1146 TemplateURLService::CreateSyncDataFromTemplateURL(*turl)); | |
| 1147 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | |
| 1148 PassProcessor(), CreateAndPassSyncErrorFactory()); | |
| 1149 | |
| 1150 TemplateURL* new_turl = | |
| 1151 CreateTestTemplateURL(ASCIIToUTF16("local"), "http://local.com", "local"); | |
| 1152 model()->Add(new_turl); | |
| 1153 | |
| 1154 syncer::SyncChangeList changes; | |
| 1155 changes.push_back(CreateTestSyncChange(syncer::SyncChange::ACTION_UPDATE, | |
| 1156 CreateTestTemplateURL(ASCIIToUTF16("local"), "http://sync.com", "sync", | |
| 1157 110, true))); | |
| 1158 model()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1159 | |
| 1160 EXPECT_EQ(1U, model()->GetTemplateURLs().size()); | |
| 1161 EXPECT_EQ("local", | |
| 1162 model()->GetTemplateURLForKeyword(ASCIIToUTF16("local"))->sync_guid()); | |
| 1163 EXPECT_EQ(1U, processor()->change_list_size()); | |
| 1164 ASSERT_TRUE(processor()->contains_guid("sync")); | |
| 1165 EXPECT_EQ(syncer::SyncChange::ACTION_DELETE, | |
| 1166 processor()->change_for_guid("sync").change_type()); | |
| 1167 } | |
| 1168 | |
| 1169 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { | 967 TEST_F(TemplateURLServiceSyncTest, ProcessTemplateURLChange) { |
| 1170 // Ensure that ProcessTemplateURLChange is called and pushes the correct | 968 // Ensure that ProcessTemplateURLChange is called and pushes the correct |
| 1171 // changes to Sync whenever local changes are made to TemplateURLs. | 969 // changes to Sync whenever local changes are made to TemplateURLs. |
| 1172 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, | 970 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, |
| 1173 CreateInitialSyncData(), PassProcessor(), | 971 CreateInitialSyncData(), PassProcessor(), |
| 1174 CreateAndPassSyncErrorFactory()); | 972 CreateAndPassSyncErrorFactory()); |
| 1175 | 973 |
| 1176 // Add a new search engine. | 974 // Add a new search engine. |
| 1177 TemplateURL* new_turl = | 975 TemplateURL* new_turl = |
| 1178 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"); | 976 CreateTestTemplateURL(ASCIIToUTF16("baidu"), "http://baidu.cn", "new"); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1284 syncer::SyncChange key2_change = processor()->change_for_guid("key2"); | 1082 syncer::SyncChange key2_change = processor()->change_for_guid("key2"); |
| 1285 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key2_change.change_type()); | 1083 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key2_change.change_type()); |
| 1286 EXPECT_EQ(google_keyword, UTF8ToUTF16(GetKeyword(key2_change.sync_data()))); | 1084 EXPECT_EQ(google_keyword, UTF8ToUTF16(GetKeyword(key2_change.sync_data()))); |
| 1287 } | 1085 } |
| 1288 | 1086 |
| 1289 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordConflicts) { | 1087 TEST_F(TemplateURLServiceSyncTest, AutogeneratedKeywordConflicts) { |
| 1290 // Sync brings in some autogenerated keywords, but the generated keywords we | 1088 // Sync brings in some autogenerated keywords, but the generated keywords we |
| 1291 // try to create conflict with ones in the model. | 1089 // try to create conflict with ones in the model. |
| 1292 string16 google_keyword(net::StripWWW(ASCIIToUTF16(GURL( | 1090 string16 google_keyword(net::StripWWW(ASCIIToUTF16(GURL( |
| 1293 UIThreadSearchTermsData(profile_a()).GoogleBaseURLValue()).host()))); | 1091 UIThreadSearchTermsData(profile_a()).GoogleBaseURLValue()).host()))); |
| 1294 TemplateURL* google = CreateTestTemplateURL(google_keyword, | 1092 const std::string local_google_url = |
| 1295 "{google:baseURL}1/search?q={searchTerms}"); | 1093 "{google:baseURL}1/search?q={searchTerms}"; |
| 1094 TemplateURL* google = CreateTestTemplateURL(google_keyword, local_google_url); | |
| 1296 model()->Add(google); | 1095 model()->Add(google); |
| 1297 TemplateURL* other = | 1096 TemplateURL* other = |
| 1298 CreateTestTemplateURL(ASCIIToUTF16("other.com"), "http://other.com/foo"); | 1097 CreateTestTemplateURL(ASCIIToUTF16("other.com"), "http://other.com/foo"); |
| 1299 model()->Add(other); | 1098 model()->Add(other); |
| 1300 syncer::SyncDataList initial_data; | 1099 syncer::SyncDataList initial_data; |
| 1301 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("sync1"), | 1100 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("sync1"), |
| 1302 "{google:baseURL}2/search?q={searchTerms}", "sync1", 50)); | 1101 "{google:baseURL}2/search?q={searchTerms}", "sync1", 50)); |
| 1303 initial_data.push_back( | 1102 initial_data.push_back( |
| 1304 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); | 1103 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); |
| 1104 const std::string synced_other_url = | |
| 1105 "http://other.com/search?q={searchTerms}"; | |
| 1305 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("sync2"), | 1106 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("sync2"), |
| 1306 "http://other.com/search?q={searchTerms}", "sync2", 150)); | 1107 synced_other_url, "sync2", 150)); |
| 1307 initial_data.push_back( | 1108 initial_data.push_back( |
| 1308 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); | 1109 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); |
| 1110 | |
| 1111 // Before we merge the data, grab the local sync_guids so we can ensure that | |
| 1112 // they've been replaced. | |
| 1113 const std::string local_google_guid = google->sync_guid(); | |
| 1114 const std::string local_other_guid = other->sync_guid(); | |
| 1115 | |
| 1309 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | 1116 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1310 PassProcessor(), CreateAndPassSyncErrorFactory()); | 1117 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 1311 | 1118 |
| 1312 // In this case, the conflicts should be handled just like any other keyword | 1119 // In this case, the conflicts should be handled just like any other keyword |
| 1313 // conflicts -- the later-modified TemplateURL is assumed to be authoritative. | 1120 // conflicts -- the later-modified TemplateURL is assumed to be authoritative. |
| 1314 EXPECT_EQ(google_keyword, google->keyword()); | 1121 // Since the initial TemplateURLs were local only, they should be merged with |
| 1315 EXPECT_EQ(google_keyword + ASCIIToUTF16("_"), | 1122 // the sync TemplateURLs (GUIDs transferred over). |
| 1316 model()->GetTemplateURLForGUID("sync1")->keyword()); | 1123 EXPECT_FALSE(model()->GetTemplateURLForGUID(local_google_guid)); |
| 1317 EXPECT_EQ(ASCIIToUTF16("other.com_"), other->keyword()); | 1124 ASSERT_TRUE(model()->GetTemplateURLForGUID("sync1")); |
| 1125 EXPECT_EQ(google_keyword, model()->GetTemplateURLForGUID("sync1")->keyword()); | |
| 1126 EXPECT_FALSE(model()->GetTemplateURLForGUID(local_other_guid)); | |
| 1127 ASSERT_TRUE(model()->GetTemplateURLForGUID("sync2")); | |
| 1318 EXPECT_EQ(ASCIIToUTF16("other.com"), | 1128 EXPECT_EQ(ASCIIToUTF16("other.com"), |
| 1319 model()->GetTemplateURLForGUID("sync2")->keyword()); | 1129 model()->GetTemplateURLForGUID("sync2")->keyword()); |
| 1320 | 1130 |
| 1321 // Both synced URLs should have associated UPDATEs, since both needed their | 1131 // Both synced URLs should have associated UPDATEs, since both needed their |
| 1322 // keywords to be generated (and sync1 needed conflict resolution as well). | 1132 // keywords to be generated. |
| 1323 EXPECT_GE(processor()->change_list_size(), 2U); | 1133 EXPECT_EQ(processor()->change_list_size(), 2U); |
| 1324 ASSERT_TRUE(processor()->contains_guid("sync1")); | 1134 ASSERT_TRUE(processor()->contains_guid("sync1")); |
| 1325 syncer::SyncChange sync1_change = processor()->change_for_guid("sync1"); | 1135 syncer::SyncChange sync1_change = processor()->change_for_guid("sync1"); |
| 1326 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, sync1_change.change_type()); | 1136 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, sync1_change.change_type()); |
| 1327 EXPECT_EQ(google_keyword + ASCIIToUTF16("_"), | 1137 EXPECT_EQ(google_keyword, UTF8ToUTF16(GetKeyword(sync1_change.sync_data()))); |
| 1328 UTF8ToUTF16(GetKeyword(sync1_change.sync_data()))); | 1138 EXPECT_EQ(local_google_url, GetURL(sync1_change.sync_data())); |
| 1329 ASSERT_TRUE(processor()->contains_guid("sync2")); | 1139 ASSERT_TRUE(processor()->contains_guid("sync2")); |
| 1330 syncer::SyncChange sync2_change = processor()->change_for_guid("sync2"); | 1140 syncer::SyncChange sync2_change = processor()->change_for_guid("sync2"); |
| 1331 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, sync2_change.change_type()); | 1141 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, sync2_change.change_type()); |
| 1332 EXPECT_EQ("other.com", GetKeyword(sync2_change.sync_data())); | 1142 EXPECT_EQ("other.com", GetKeyword(sync2_change.sync_data())); |
| 1143 EXPECT_EQ(synced_other_url, GetURL(sync2_change.sync_data())); | |
| 1333 } | 1144 } |
| 1334 | 1145 |
| 1335 TEST_F(TemplateURLServiceSyncTest, TwoAutogeneratedKeywordsUsingGoogleBaseURL) { | 1146 TEST_F(TemplateURLServiceSyncTest, TwoAutogeneratedKeywordsUsingGoogleBaseURL) { |
| 1336 // Sync brings in two autogenerated keywords and both use Google base URLs. | 1147 // Sync brings in two autogenerated keywords and both use Google base URLs. |
| 1337 // We make the first older so that it will get renamed once before the second | 1148 // We make the first older so that it will get renamed once before the second |
| 1338 // and then again once after (when we resolve conflicts for the second). | 1149 // and then again once after (when we resolve conflicts for the second). |
| 1339 syncer::SyncDataList initial_data; | 1150 syncer::SyncDataList initial_data; |
| 1340 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"), | 1151 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(ASCIIToUTF16("key1"), |
| 1341 "{google:baseURL}1/search?q={searchTerms}", "key1", 50)); | 1152 "{google:baseURL}1/search?q={searchTerms}", "key1", 50)); |
| 1342 initial_data.push_back( | 1153 initial_data.push_back( |
| 1343 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); | 1154 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); |
| 1344 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), | 1155 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), |
| 1345 "{google:baseURL}2/search?q={searchTerms}", "key2")); | 1156 "{google:baseURL}2/search?q={searchTerms}", "key2")); |
| 1346 initial_data.push_back( | 1157 initial_data.push_back( |
| 1347 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); | 1158 CreateCustomSyncData(*turl, true, turl->url(), turl->sync_guid())); |
| 1348 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | 1159 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1349 PassProcessor(), CreateAndPassSyncErrorFactory()); | 1160 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 1350 | 1161 |
| 1351 // We should still have coalesced the updates to one each. | 1162 // We should still have coalesced the updates to one each. |
| 1352 string16 google_keyword(net::StripWWW(ASCIIToUTF16(GURL( | 1163 string16 google_keyword(net::StripWWW(ASCIIToUTF16(GURL( |
| 1353 UIThreadSearchTermsData(profile_a()).GoogleBaseURLValue()).host()))); | 1164 UIThreadSearchTermsData(profile_a()).GoogleBaseURLValue()).host()))); |
| 1354 TemplateURL* keyword1 = | 1165 TemplateURL* keyword1 = |
| 1355 model()->GetTemplateURLForKeyword(google_keyword + ASCIIToUTF16("_")); | 1166 model()->GetTemplateURLForKeyword(google_keyword + ASCIIToUTF16("_")); |
| 1356 ASSERT_FALSE(keyword1 == NULL); | 1167 ASSERT_FALSE(keyword1 == NULL); |
| 1357 EXPECT_EQ("key1", keyword1->sync_guid()); | 1168 EXPECT_EQ("key1", keyword1->sync_guid()); |
| 1358 TemplateURL* keyword2 = model()->GetTemplateURLForKeyword(google_keyword); | 1169 TemplateURL* keyword2 = model()->GetTemplateURLForKeyword(google_keyword); |
| 1359 ASSERT_FALSE(keyword2 == NULL); | 1170 ASSERT_FALSE(keyword2 == NULL); |
| 1360 EXPECT_EQ("key2", keyword2->sync_guid()); | 1171 EXPECT_EQ("key2", keyword2->sync_guid()); |
| 1172 | |
| 1361 EXPECT_GE(processor()->change_list_size(), 2U); | 1173 EXPECT_GE(processor()->change_list_size(), 2U); |
| 1362 ASSERT_TRUE(processor()->contains_guid("key1")); | 1174 ASSERT_TRUE(processor()->contains_guid("key1")); |
| 1363 syncer::SyncChange key1_change = processor()->change_for_guid("key1"); | 1175 syncer::SyncChange key1_change = processor()->change_for_guid("key1"); |
| 1364 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key1_change.change_type()); | 1176 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key1_change.change_type()); |
| 1365 EXPECT_EQ(keyword1->keyword(), | 1177 EXPECT_EQ(keyword1->keyword(), |
| 1366 UTF8ToUTF16(GetKeyword(key1_change.sync_data()))); | 1178 UTF8ToUTF16(GetKeyword(key1_change.sync_data()))); |
| 1367 ASSERT_TRUE(processor()->contains_guid("key2")); | 1179 ASSERT_TRUE(processor()->contains_guid("key2")); |
| 1368 syncer::SyncChange key2_change = processor()->change_for_guid("key2"); | 1180 syncer::SyncChange key2_change = processor()->change_for_guid("key2"); |
| 1369 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key2_change.change_type()); | 1181 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, key2_change.change_type()); |
| 1370 EXPECT_EQ(keyword2->keyword(), | 1182 EXPECT_EQ(keyword2->keyword(), |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1829 | 1641 |
| 1830 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 1642 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1831 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); | 1643 EXPECT_FALSE(model()->GetTemplateURLForGUID("whateverguid")); |
| 1832 EXPECT_EQ(model()->GetDefaultSearchProvider(), | 1644 EXPECT_EQ(model()->GetDefaultSearchProvider(), |
| 1833 model()->GetTemplateURLForGUID("key1")); | 1645 model()->GetTemplateURLForGUID("key1")); |
| 1834 } | 1646 } |
| 1835 | 1647 |
| 1836 TEST_F(TemplateURLServiceSyncTest, LocalDefaultWinsConflict) { | 1648 TEST_F(TemplateURLServiceSyncTest, LocalDefaultWinsConflict) { |
| 1837 // We expect that the local default always wins keyword conflict resolution. | 1649 // We expect that the local default always wins keyword conflict resolution. |
| 1838 const string16 keyword(ASCIIToUTF16("key1")); | 1650 const string16 keyword(ASCIIToUTF16("key1")); |
| 1651 const std::string url("http://whatever.com/{searchTerms}"); | |
| 1839 TemplateURL* default_turl = CreateTestTemplateURL(keyword, | 1652 TemplateURL* default_turl = CreateTestTemplateURL(keyword, |
| 1840 "http://whatever.com/{searchTerms}", "whateverguid", 10); | 1653 url, |
| 1654 "whateverguid", | |
| 1655 10); | |
| 1841 model()->Add(default_turl); | 1656 model()->Add(default_turl); |
| 1842 model()->SetDefaultSearchProvider(default_turl); | 1657 model()->SetDefaultSearchProvider(default_turl); |
| 1843 | 1658 |
| 1844 syncer::SyncDataList initial_data = CreateInitialSyncData(); | 1659 syncer::SyncDataList initial_data = CreateInitialSyncData(); |
| 1845 // The key1 entry should be different from the default but conflict in the | 1660 // The key1 entry should be different from the default but conflict in the |
| 1846 // keyword. | 1661 // keyword. |
| 1847 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(keyword, | 1662 scoped_ptr<TemplateURL> turl(CreateTestTemplateURL(keyword, |
| 1848 "http://key1.com/{searchTerms}", "key1", 90)); | 1663 "http://key1.com/{searchTerms}", "key1", 90)); |
| 1849 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); | 1664 initial_data[0] = TemplateURLService::CreateSyncDataFromTemplateURL(*turl); |
| 1850 | 1665 |
| 1851 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, | 1666 model()->MergeDataAndStartSyncing(syncer::SEARCH_ENGINES, initial_data, |
| 1852 PassProcessor(), CreateAndPassSyncErrorFactory()); | 1667 PassProcessor(), CreateAndPassSyncErrorFactory()); |
| 1853 | 1668 |
| 1854 // The conflicting TemplateURL should be added, but it should have lost | 1669 // Since the local default was not yet synced, it should be merged with the |
| 1855 // conflict resolution against the default. | 1670 // conflicting TemplateURL. However, its values should have been preserved |
| 1856 EXPECT_EQ(4U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); | 1671 // since it would have won conflict resolution due to being the default. |
| 1857 const TemplateURL* winner = model()->GetTemplateURLForGUID("whateverguid"); | 1672 EXPECT_EQ(3U, model()->GetAllSyncData(syncer::SEARCH_ENGINES).size()); |
| 1673 const TemplateURL* winner = model()->GetTemplateURLForGUID("key1"); | |
| 1858 ASSERT_TRUE(winner); | 1674 ASSERT_TRUE(winner); |
| 1859 EXPECT_EQ(model()->GetDefaultSearchProvider(), winner); | 1675 EXPECT_EQ(model()->GetDefaultSearchProvider(), winner); |
| 1860 EXPECT_EQ(keyword, winner->keyword()); | 1676 EXPECT_EQ(keyword, winner->keyword()); |
| 1861 const TemplateURL* loser = model()->GetTemplateURLForGUID("key1"); | 1677 EXPECT_EQ(url, winner->url()); |
| 1862 ASSERT_TRUE(loser); | 1678 ASSERT_TRUE(processor()->contains_guid("key1")); |
| 1863 EXPECT_EQ(ASCIIToUTF16("key1.com"), loser->keyword()); | 1679 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, |
| 1680 processor()->change_for_guid("key1").change_type()); | |
| 1681 EXPECT_EQ(url, GetURL(processor()->change_for_guid("key1").sync_data())); | |
| 1682 | |
| 1683 // There is no loser, as the two were merged together. The local sync_guid | |
| 1684 // should no longer be found in the model. | |
| 1685 const TemplateURL* loser = model()->GetTemplateURLForGUID("whateverguid"); | |
| 1686 ASSERT_FALSE(loser); | |
| 1864 } | 1687 } |
| 1865 | 1688 |
| 1866 TEST_F(TemplateURLServiceSyncTest, DeleteBogusData) { | 1689 TEST_F(TemplateURLServiceSyncTest, DeleteBogusData) { |
| 1867 // Create a couple of bogus entries to sync. | 1690 // Create a couple of bogus entries to sync. |
| 1868 syncer::SyncDataList initial_data; | 1691 syncer::SyncDataList initial_data; |
| 1869 scoped_ptr<TemplateURL> turl( | 1692 scoped_ptr<TemplateURL> turl( |
| 1870 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); | 1693 CreateTestTemplateURL(ASCIIToUTF16("key1"), "http://key1.com", "key1")); |
| 1871 initial_data.push_back( | 1694 initial_data.push_back( |
| 1872 CreateCustomSyncData(*turl, false, std::string(), turl->sync_guid())); | 1695 CreateCustomSyncData(*turl, false, std::string(), turl->sync_guid())); |
| 1873 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); | 1696 turl.reset(CreateTestTemplateURL(ASCIIToUTF16("key2"), "http://key2.com")); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2005 // A local change to the Google base URL should update the keyword and | 1828 // A local change to the Google base URL should update the keyword and |
| 2006 // generate a sync change. | 1829 // generate a sync change. |
| 2007 test_util_a_.SetGoogleBaseURL(GURL("http://google.co.in/")); | 1830 test_util_a_.SetGoogleBaseURL(GURL("http://google.co.in/")); |
| 2008 EXPECT_EQ(ASCIIToUTF16("google.co.in"), synced_turl->keyword()); | 1831 EXPECT_EQ(ASCIIToUTF16("google.co.in"), synced_turl->keyword()); |
| 2009 EXPECT_EQ(1U, processor()->change_list_size()); | 1832 EXPECT_EQ(1U, processor()->change_list_size()); |
| 2010 ASSERT_TRUE(processor()->contains_guid("guid")); | 1833 ASSERT_TRUE(processor()->contains_guid("guid")); |
| 2011 syncer::SyncChange change(processor()->change_for_guid("guid")); | 1834 syncer::SyncChange change(processor()->change_for_guid("guid")); |
| 2012 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, change.change_type()); | 1835 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, change.change_type()); |
| 2013 EXPECT_EQ("google.co.in", GetKeyword(change.sync_data())); | 1836 EXPECT_EQ("google.co.in", GetKeyword(change.sync_data())); |
| 2014 } | 1837 } |
| 1838 | |
| 1839 TEST_F(TemplateURLServiceSyncTest, MergeInSyncTemplateURL) { | |
| 1840 // An enumeration used to indicate which TemplateURL test value is expected | |
| 1841 // for a particular test result. | |
| 1842 enum ExpectedTemplateURL { | |
| 1843 LOCAL, | |
| 1844 SYNC, | |
| 1845 BOTH, | |
| 1846 NEITHER, | |
| 1847 }; | |
| 1848 | |
| 1849 // Sets up and executes a MergeInSyncTemplateURL test given a number of | |
| 1850 // expected start and end states: | |
| 1851 // * |conflict_winner| denotes which TemplateURL should win the | |
| 1852 // conflict. | |
| 1853 // * |synced_at_start| denotes which of the TemplateURLs should known | |
| 1854 // to Sync. | |
| 1855 // * |update_sent| denotes which TemplateURL should have an | |
| 1856 // ACTION_UPDATE sent to the server after the merge. | |
| 1857 // * |turl_uniquified| denotes which TemplateURL should have its | |
| 1858 // keyword updated after the merge. | |
| 1859 // * |present_in_model| denotes which TemplateURL should be found in | |
| 1860 // the model after the merge. | |
| 1861 // * If |keywords_conflict| is true, the TemplateURLs are set up with | |
| 1862 // the same keyword. | |
| 1863 const struct { | |
| 1864 ExpectedTemplateURL conflict_winner; | |
| 1865 ExpectedTemplateURL synced_at_start; | |
| 1866 ExpectedTemplateURL update_sent; | |
| 1867 ExpectedTemplateURL turl_uniquified; | |
| 1868 ExpectedTemplateURL present_in_model; | |
| 1869 bool keywords_conflict; | |
| 1870 } test_cases[] = { | |
| 1871 // Both are synced and the new sync entry is better: Local is uniquified and | |
| 1872 // UPDATE sent. Sync is added. | |
| 1873 {SYNC, BOTH, LOCAL, LOCAL, BOTH, true}, | |
| 1874 // Both are synced and the local entry is better: Sync is uniquified and | |
| 1875 // added to the model, and UPDATE for it is sent. | |
|
Nicolas Zea
2012/08/10 17:34:09
and an
SteveT
2012/08/10 19:07:06
Done.
| |
| 1876 {LOCAL, BOTH, SYNC, SYNC, BOTH, true}, | |
| 1877 // Local was not known to Sync and the new sync entry is better: Sync is | |
| 1878 // added. Local is removed. No updates. | |
| 1879 {SYNC, SYNC, NEITHER, NEITHER, SYNC, true}, | |
| 1880 // Local was not known to sync and the local entry is better: Local is | |
| 1881 // updated with sync GUID, Sync is not added. UPDATE sent for Sync. | |
| 1882 {LOCAL, SYNC, SYNC, NEITHER, SYNC, true}, | |
| 1883 // No conflicting keyword. Both should be added with their original | |
| 1884 // keywords, with no updates sent. Note that MergeDataAndStartSyncing is | |
| 1885 // responsible for creating the ACTION_ADD for the local TemplateURL. | |
| 1886 {NEITHER, SYNC, NEITHER, NEITHER, BOTH, false}, | |
| 1887 }; | |
| 1888 | |
| 1889 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | |
| 1890 // Assert all the valid states of ExpectedTemplateURLs. | |
| 1891 DCHECK_NE(test_cases[i].conflict_winner, BOTH); | |
| 1892 DCHECK_NE(test_cases[i].synced_at_start, NEITHER); | |
| 1893 DCHECK_NE(test_cases[i].synced_at_start, LOCAL); | |
| 1894 DCHECK_NE(test_cases[i].update_sent, BOTH); | |
| 1895 DCHECK_NE(test_cases[i].turl_uniquified, BOTH); | |
| 1896 DCHECK_NE(test_cases[i].present_in_model, NEITHER); | |
| 1897 | |
| 1898 const string16 local_keyword = ASCIIToUTF16("localkeyword"); | |
| 1899 const string16 sync_keyword = test_cases[i].keywords_conflict ? | |
| 1900 local_keyword : ASCIIToUTF16("synckeyword"); | |
| 1901 const std::string local_url = "www.localurl.com"; | |
| 1902 const std::string sync_url = "www.syncurl.com"; | |
| 1903 const time_t local_last_modified = 100; | |
| 1904 const time_t sync_last_modified = | |
| 1905 test_cases[i].conflict_winner == SYNC ? 110 : 90; | |
| 1906 const std::string local_guid = "local_guid"; | |
| 1907 const std::string sync_guid = "sync_guid"; | |
| 1908 | |
| 1909 // Initialize expectations. | |
| 1910 string16 expected_local_keyword = local_keyword; | |
| 1911 string16 expected_sync_keyword = sync_keyword; | |
| 1912 | |
| 1913 // Create the data and run the actual test. | |
| 1914 TemplateURL* local_turl = CreateTestTemplateURL( | |
| 1915 local_keyword, local_url, local_guid, local_last_modified); | |
| 1916 model()->Add(local_turl); | |
| 1917 TemplateURL* sync_turl = CreateTestTemplateURL( | |
| 1918 sync_keyword, sync_url, sync_guid, sync_last_modified); | |
| 1919 | |
| 1920 SyncDataMap sync_data; | |
| 1921 if (test_cases[i].synced_at_start == SYNC || | |
| 1922 test_cases[i].synced_at_start == BOTH) { | |
| 1923 sync_data[sync_turl->sync_guid()] = | |
| 1924 TemplateURLService::CreateSyncDataFromTemplateURL(*sync_turl); | |
| 1925 } | |
| 1926 if (test_cases[i].synced_at_start == BOTH) { | |
| 1927 sync_data[local_turl->sync_guid()] = | |
| 1928 TemplateURLService::CreateSyncDataFromTemplateURL(*local_turl); | |
| 1929 } | |
| 1930 SyncDataMap initial_data; | |
| 1931 initial_data[local_turl->sync_guid()] = | |
| 1932 TemplateURLService::CreateSyncDataFromTemplateURL(*local_turl); | |
| 1933 | |
| 1934 syncer::SyncChangeList change_list; | |
| 1935 model()->MergeInSyncTemplateURL(sync_turl, sync_data, &change_list, | |
| 1936 &initial_data); | |
| 1937 | |
| 1938 // Check for expected updates, if any. | |
| 1939 std::string expected_update_guid; | |
| 1940 if (test_cases[i].update_sent == LOCAL) | |
| 1941 expected_update_guid = local_guid; | |
| 1942 else if (test_cases[i].update_sent == SYNC) | |
| 1943 expected_update_guid = sync_guid; | |
| 1944 if (!expected_update_guid.empty()) { | |
| 1945 ASSERT_EQ(1U, change_list.size()); | |
| 1946 EXPECT_EQ(expected_update_guid, GetGUID(change_list[0].sync_data())); | |
| 1947 EXPECT_EQ(syncer::SyncChange::ACTION_UPDATE, | |
| 1948 change_list[0].change_type()); | |
| 1949 } else { | |
| 1950 EXPECT_EQ(0U, change_list.size()); | |
| 1951 } | |
| 1952 | |
| 1953 // Adjust the expectations based on the expectation enums. | |
| 1954 if (test_cases[i].turl_uniquified == LOCAL) { | |
| 1955 DCHECK(test_cases[i].keywords_conflict); | |
| 1956 expected_local_keyword = ASCIIToUTF16("localkeyword_"); | |
| 1957 } | |
| 1958 if (test_cases[i].turl_uniquified == SYNC) { | |
| 1959 DCHECK(test_cases[i].keywords_conflict); | |
| 1960 expected_sync_keyword = ASCIIToUTF16("localkeyword_"); | |
| 1961 } | |
| 1962 | |
| 1963 // Check for TemplateURLs expected in the model. Note that this is checked | |
| 1964 // by GUID rather than the initial pointer, as a merge could occur (the | |
| 1965 // Sync TemplateURL overtakes the local one). Also remove the present | |
| 1966 // TemplateURL when done so the next test case starts with a clean slate. | |
| 1967 if (test_cases[i].present_in_model == LOCAL || | |
| 1968 test_cases[i].present_in_model == BOTH) { | |
| 1969 ASSERT_TRUE(model()->GetTemplateURLForGUID(local_guid)); | |
| 1970 EXPECT_EQ(expected_local_keyword, local_turl->keyword()); | |
| 1971 EXPECT_EQ(local_url, local_turl->url()); | |
| 1972 EXPECT_EQ(local_last_modified, local_turl->last_modified().ToTimeT()); | |
| 1973 model()->Remove(model()->GetTemplateURLForGUID(local_guid)); | |
| 1974 } | |
| 1975 if (test_cases[i].present_in_model == SYNC || | |
| 1976 test_cases[i].present_in_model == BOTH) { | |
| 1977 ASSERT_TRUE(model()->GetTemplateURLForGUID(sync_guid)); | |
| 1978 EXPECT_EQ(expected_sync_keyword, sync_turl->keyword()); | |
| 1979 EXPECT_EQ(sync_url, sync_turl->url()); | |
| 1980 EXPECT_EQ(sync_last_modified, sync_turl->last_modified().ToTimeT()); | |
| 1981 model()->Remove(model()->GetTemplateURLForGUID(sync_guid)); | |
| 1982 } | |
| 1983 } // for | |
| 1984 } | |
| OLD | NEW |