Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // Unit tests for the SyncApi. Note that a lot of the underlying | 5 // Unit tests for the SyncApi. Note that a lot of the underlying |
| 6 // functionality is provided by the Syncable layer, which has its own | 6 // functionality is provided by the Syncable layer, which has its own |
| 7 // unit tests. We'll test SyncApi specific things in this harness. | 7 // unit tests. We'll test SyncApi specific things in this harness. |
| 8 | 8 |
| 9 #include <cstddef> | 9 #include <cstddef> |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/format_macros.h" | 14 #include "base/format_macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 17 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
| 18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
| 19 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 20 #include "base/tracked.h" | 20 #include "base/tracked.h" |
| 21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 22 #include "base/values.h" | 22 #include "base/values.h" |
| 23 #include "chrome/browser/password_manager/encryptor.h" | 23 #include "chrome/browser/password_manager/encryptor.h" |
| 24 #include "chrome/browser/sync/engine/model_safe_worker.h" | 24 #include "chrome/browser/sync/engine/model_safe_worker.h" |
| 25 #include "chrome/browser/sync/engine/nigori_util.h" | 25 #include "chrome/browser/sync/engine/nigori_util.h" |
| 26 #include "chrome/browser/sync/internal_api/change_record.h" | |
| 26 #include "chrome/browser/sync/internal_api/http_post_provider_factory.h" | 27 #include "chrome/browser/sync/internal_api/http_post_provider_factory.h" |
| 27 #include "chrome/browser/sync/internal_api/http_post_provider_interface.h" | 28 #include "chrome/browser/sync/internal_api/http_post_provider_interface.h" |
| 28 #include "chrome/browser/sync/internal_api/read_node.h" | 29 #include "chrome/browser/sync/internal_api/read_node.h" |
| 29 #include "chrome/browser/sync/internal_api/read_transaction.h" | 30 #include "chrome/browser/sync/internal_api/read_transaction.h" |
| 30 #include "chrome/browser/sync/internal_api/sync_manager.h" | 31 #include "chrome/browser/sync/internal_api/sync_manager.h" |
| 31 #include "chrome/browser/sync/internal_api/write_node.h" | 32 #include "chrome/browser/sync/internal_api/write_node.h" |
| 32 #include "chrome/browser/sync/internal_api/write_transaction.h" | 33 #include "chrome/browser/sync/internal_api/write_transaction.h" |
| 33 #include "chrome/browser/sync/js/js_arg_list.h" | 34 #include "chrome/browser/sync/js/js_arg_list.h" |
| 34 #include "chrome/browser/sync/js/js_backend.h" | 35 #include "chrome/browser/sync/js/js_backend.h" |
| 35 #include "chrome/browser/sync/js/js_event_handler.h" | 36 #include "chrome/browser/sync/js/js_event_handler.h" |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 549 scoped_ptr<DictionaryValue> details(node.GetDetailsAsValue()); | 550 scoped_ptr<DictionaryValue> details(node.GetDetailsAsValue()); |
| 550 if (details.get()) { | 551 if (details.get()) { |
| 551 CheckNodeValue(node, *details, true); | 552 CheckNodeValue(node, *details, true); |
| 552 } else { | 553 } else { |
| 553 ADD_FAILURE(); | 554 ADD_FAILURE(); |
| 554 } | 555 } |
| 555 } | 556 } |
| 556 | 557 |
| 557 namespace { | 558 namespace { |
| 558 | 559 |
| 559 void ExpectChangeRecordActionValue(SyncManager::ChangeRecord::Action | 560 void ExpectChangeRecordActionValue(ChangeRecord::Action |
| 560 expected_value, | 561 expected_value, |
|
Nicolas Zea
2011/09/16 21:20:26
can this fit on the previous line?
akalin
2011/09/16 21:50:36
Done.
| |
| 561 const DictionaryValue& value, | 562 const DictionaryValue& value, |
| 562 const std::string& key) { | 563 const std::string& key) { |
| 563 std::string str_value; | 564 std::string str_value; |
| 564 EXPECT_TRUE(value.GetString(key, &str_value)); | 565 EXPECT_TRUE(value.GetString(key, &str_value)); |
| 565 switch (expected_value) { | 566 switch (expected_value) { |
| 566 case SyncManager::ChangeRecord::ACTION_ADD: | 567 case ChangeRecord::ACTION_ADD: |
| 567 EXPECT_EQ("Add", str_value); | 568 EXPECT_EQ("Add", str_value); |
| 568 break; | 569 break; |
| 569 case SyncManager::ChangeRecord::ACTION_UPDATE: | 570 case ChangeRecord::ACTION_UPDATE: |
| 570 EXPECT_EQ("Update", str_value); | 571 EXPECT_EQ("Update", str_value); |
| 571 break; | 572 break; |
| 572 case SyncManager::ChangeRecord::ACTION_DELETE: | 573 case ChangeRecord::ACTION_DELETE: |
| 573 EXPECT_EQ("Delete", str_value); | 574 EXPECT_EQ("Delete", str_value); |
| 574 break; | 575 break; |
| 575 default: | 576 default: |
| 576 NOTREACHED(); | 577 NOTREACHED(); |
| 577 break; | 578 break; |
| 578 } | 579 } |
| 579 } | 580 } |
| 580 | 581 |
| 581 void CheckNonDeleteChangeRecordValue(const SyncManager::ChangeRecord& record, | 582 void CheckNonDeleteChangeRecordValue(const ChangeRecord& record, |
| 582 const DictionaryValue& value, | 583 const DictionaryValue& value, |
| 583 BaseTransaction* trans) { | 584 BaseTransaction* trans) { |
| 584 EXPECT_NE(SyncManager::ChangeRecord::ACTION_DELETE, record.action); | 585 EXPECT_NE(ChangeRecord::ACTION_DELETE, record.action); |
| 585 ExpectChangeRecordActionValue(record.action, value, "action"); | 586 ExpectChangeRecordActionValue(record.action, value, "action"); |
| 586 { | 587 { |
| 587 ReadNode node(trans); | 588 ReadNode node(trans); |
| 588 EXPECT_TRUE(node.InitByIdLookup(record.id)); | 589 EXPECT_TRUE(node.InitByIdLookup(record.id)); |
| 589 scoped_ptr<DictionaryValue> expected_details(node.GetDetailsAsValue()); | 590 scoped_ptr<DictionaryValue> expected_details(node.GetDetailsAsValue()); |
| 590 ExpectDictDictionaryValue(*expected_details, value, "node"); | 591 ExpectDictDictionaryValue(*expected_details, value, "node"); |
| 591 } | 592 } |
| 592 } | 593 } |
| 593 | 594 |
| 594 void CheckDeleteChangeRecordValue(const SyncManager::ChangeRecord& record, | 595 void CheckDeleteChangeRecordValue(const ChangeRecord& record, |
| 595 const DictionaryValue& value) { | 596 const DictionaryValue& value) { |
| 596 EXPECT_EQ(SyncManager::ChangeRecord::ACTION_DELETE, record.action); | 597 EXPECT_EQ(ChangeRecord::ACTION_DELETE, record.action); |
| 597 ExpectChangeRecordActionValue(record.action, value, "action"); | 598 ExpectChangeRecordActionValue(record.action, value, "action"); |
| 598 DictionaryValue* node_value = NULL; | 599 DictionaryValue* node_value = NULL; |
| 599 EXPECT_TRUE(value.GetDictionary("node", &node_value)); | 600 EXPECT_TRUE(value.GetDictionary("node", &node_value)); |
| 600 if (node_value) { | 601 if (node_value) { |
| 601 ExpectInt64Value(record.id, *node_value, "id"); | 602 ExpectInt64Value(record.id, *node_value, "id"); |
| 602 scoped_ptr<DictionaryValue> expected_specifics_value( | 603 scoped_ptr<DictionaryValue> expected_specifics_value( |
| 603 browser_sync::EntitySpecificsToValue(record.specifics)); | 604 browser_sync::EntitySpecificsToValue(record.specifics)); |
| 604 ExpectDictDictionaryValue(*expected_specifics_value, | 605 ExpectDictDictionaryValue(*expected_specifics_value, |
| 605 *node_value, "specifics"); | 606 *node_value, "specifics"); |
| 606 scoped_ptr<DictionaryValue> expected_extra_value; | 607 scoped_ptr<DictionaryValue> expected_extra_value; |
| 607 if (record.extra.get()) { | 608 if (record.extra.get()) { |
| 608 expected_extra_value.reset(record.extra->ToValue()); | 609 expected_extra_value.reset(record.extra->ToValue()); |
| 609 } | 610 } |
| 610 Value* extra_value = NULL; | 611 Value* extra_value = NULL; |
| 611 EXPECT_EQ(record.extra.get() != NULL, | 612 EXPECT_EQ(record.extra.get() != NULL, |
| 612 node_value->Get("extra", &extra_value)); | 613 node_value->Get("extra", &extra_value)); |
| 613 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); | 614 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); |
| 614 } | 615 } |
| 615 } | 616 } |
| 616 | 617 |
| 617 class MockExtraChangeRecordData | 618 class MockExtraChangeRecordData |
| 618 : public SyncManager::ExtraPasswordChangeRecordData { | 619 : public ExtraPasswordChangeRecordData { |
| 619 public: | 620 public: |
| 620 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); | 621 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); |
| 621 }; | 622 }; |
| 622 | 623 |
| 623 } // namespace | 624 } // namespace |
| 624 | 625 |
| 625 TEST_F(SyncApiTest, ChangeRecordToValue) { | 626 TEST_F(SyncApiTest, ChangeRecordToValue) { |
| 626 int64 child_id = MakeNode(test_user_share_.user_share(), | 627 int64 child_id = MakeNode(test_user_share_.user_share(), |
| 627 syncable::BOOKMARKS, "testtag"); | 628 syncable::BOOKMARKS, "testtag"); |
| 628 sync_pb::EntitySpecifics child_specifics; | 629 sync_pb::EntitySpecifics child_specifics; |
| 629 { | 630 { |
| 630 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 631 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 631 ReadNode node(&trans); | 632 ReadNode node(&trans); |
| 632 EXPECT_TRUE(node.InitByIdLookup(child_id)); | 633 EXPECT_TRUE(node.InitByIdLookup(child_id)); |
| 633 child_specifics = node.GetEntry()->Get(syncable::SPECIFICS); | 634 child_specifics = node.GetEntry()->Get(syncable::SPECIFICS); |
| 634 } | 635 } |
| 635 | 636 |
| 636 // Add | 637 // Add |
| 637 { | 638 { |
| 638 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 639 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 639 SyncManager::ChangeRecord record; | 640 ChangeRecord record; |
| 640 record.action = SyncManager::ChangeRecord::ACTION_ADD; | 641 record.action = ChangeRecord::ACTION_ADD; |
| 641 record.id = 1; | 642 record.id = 1; |
| 642 record.specifics = child_specifics; | 643 record.specifics = child_specifics; |
| 643 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); | 644 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); |
| 644 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); | 645 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); |
| 645 CheckNonDeleteChangeRecordValue(record, *value, &trans); | 646 CheckNonDeleteChangeRecordValue(record, *value, &trans); |
| 646 } | 647 } |
| 647 | 648 |
| 648 // Update | 649 // Update |
| 649 { | 650 { |
| 650 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 651 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 651 SyncManager::ChangeRecord record; | 652 ChangeRecord record; |
| 652 record.action = SyncManager::ChangeRecord::ACTION_UPDATE; | 653 record.action = ChangeRecord::ACTION_UPDATE; |
| 653 record.id = child_id; | 654 record.id = child_id; |
| 654 record.specifics = child_specifics; | 655 record.specifics = child_specifics; |
| 655 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); | 656 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); |
| 656 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); | 657 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); |
| 657 CheckNonDeleteChangeRecordValue(record, *value, &trans); | 658 CheckNonDeleteChangeRecordValue(record, *value, &trans); |
| 658 } | 659 } |
| 659 | 660 |
| 660 // Delete (no extra) | 661 // Delete (no extra) |
| 661 { | 662 { |
| 662 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 663 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 663 SyncManager::ChangeRecord record; | 664 ChangeRecord record; |
| 664 record.action = SyncManager::ChangeRecord::ACTION_DELETE; | 665 record.action = ChangeRecord::ACTION_DELETE; |
| 665 record.id = child_id + 1; | 666 record.id = child_id + 1; |
| 666 record.specifics = child_specifics; | 667 record.specifics = child_specifics; |
| 667 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); | 668 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); |
| 668 CheckDeleteChangeRecordValue(record, *value); | 669 CheckDeleteChangeRecordValue(record, *value); |
| 669 } | 670 } |
| 670 | 671 |
| 671 // Delete (with extra) | 672 // Delete (with extra) |
| 672 { | 673 { |
| 673 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 674 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 674 SyncManager::ChangeRecord record; | 675 ChangeRecord record; |
| 675 record.action = SyncManager::ChangeRecord::ACTION_DELETE; | 676 record.action = ChangeRecord::ACTION_DELETE; |
| 676 record.id = child_id + 1; | 677 record.id = child_id + 1; |
| 677 record.specifics = child_specifics; | 678 record.specifics = child_specifics; |
| 678 | 679 |
| 679 DictionaryValue extra_value; | 680 DictionaryValue extra_value; |
| 680 extra_value.SetString("foo", "bar"); | 681 extra_value.SetString("foo", "bar"); |
| 681 scoped_ptr<StrictMock<MockExtraChangeRecordData> > extra( | 682 scoped_ptr<StrictMock<MockExtraChangeRecordData> > extra( |
| 682 new StrictMock<MockExtraChangeRecordData>()); | 683 new StrictMock<MockExtraChangeRecordData>()); |
| 683 EXPECT_CALL(*extra, ToValue()).Times(2).WillRepeatedly( | 684 EXPECT_CALL(*extra, ToValue()).Times(2).WillRepeatedly( |
| 684 Invoke(&extra_value, &DictionaryValue::DeepCopy)); | 685 Invoke(&extra_value, &DictionaryValue::DeepCopy)); |
| 685 | 686 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 724 virtual HttpPostProviderInterface* Create() OVERRIDE { | 725 virtual HttpPostProviderInterface* Create() OVERRIDE { |
| 725 return new TestHttpPostProviderInterface(); | 726 return new TestHttpPostProviderInterface(); |
| 726 } | 727 } |
| 727 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { | 728 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { |
| 728 delete http; | 729 delete http; |
| 729 } | 730 } |
| 730 }; | 731 }; |
| 731 | 732 |
| 732 class SyncManagerObserverMock : public SyncManager::Observer { | 733 class SyncManagerObserverMock : public SyncManager::Observer { |
| 733 public: | 734 public: |
| 734 MOCK_METHOD4(OnChangesApplied, | 735 MOCK_METHOD3(OnChangesApplied, |
| 735 void(ModelType, | 736 void(ModelType, |
| 736 const BaseTransaction*, | 737 const BaseTransaction*, |
| 737 const SyncManager::ChangeRecord*, | 738 const ImmutableChangeRecordList&)); // NOLINT |
| 738 int)); // NOLINT | |
| 739 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT | 739 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT |
| 740 MOCK_METHOD1(OnSyncCycleCompleted, | 740 MOCK_METHOD1(OnSyncCycleCompleted, |
| 741 void(const SyncSessionSnapshot*)); // NOLINT | 741 void(const SyncSessionSnapshot*)); // NOLINT |
| 742 MOCK_METHOD2(OnInitializationComplete, | 742 MOCK_METHOD2(OnInitializationComplete, |
| 743 void(const WeakHandle<JsBackend>&, bool)); // NOLINT | 743 void(const WeakHandle<JsBackend>&, bool)); // NOLINT |
| 744 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT | 744 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT |
| 745 MOCK_METHOD1(OnPassphraseRequired, | 745 MOCK_METHOD1(OnPassphraseRequired, |
| 746 void(sync_api::PassphraseRequiredReason)); // NOLINT | 746 void(sync_api::PassphraseRequiredReason)); // NOLINT |
| 747 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT | 747 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT |
| 748 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT | 748 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT |
| 749 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT | 749 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT |
| 750 MOCK_METHOD1(OnMigrationNeededForTypes, void(const ModelTypeSet&)); | |
| 751 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT | 750 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT |
| 752 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT | 751 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT |
| 753 MOCK_METHOD1(OnEncryptionComplete, void(const ModelTypeSet&)); // NOLINT | 752 MOCK_METHOD1(OnEncryptionComplete, void(const ModelTypeSet&)); // NOLINT |
| 754 MOCK_METHOD1(OnActionableError, | 753 MOCK_METHOD1(OnActionableError, |
| 755 void(const browser_sync::SyncProtocolError&)); // NOLINT | 754 void(const browser_sync::SyncProtocolError&)); // NOLINT |
| 756 }; | 755 }; |
| 757 | 756 |
| 758 class SyncNotifierMock : public sync_notifier::SyncNotifier { | 757 class SyncNotifierMock : public sync_notifier::SyncNotifier { |
| 759 public: | 758 public: |
| 760 MOCK_METHOD1(AddObserver, void(sync_notifier::SyncNotifierObserver*)); | 759 MOCK_METHOD1(AddObserver, void(sync_notifier::SyncNotifierObserver*)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 821 | 820 |
| 822 EXPECT_TRUE(sync_notifier_observer_); | 821 EXPECT_TRUE(sync_notifier_observer_); |
| 823 EXPECT_TRUE(js_backend_.IsInitialized()); | 822 EXPECT_TRUE(js_backend_.IsInitialized()); |
| 824 | 823 |
| 825 EXPECT_EQ(1, update_enabled_types_call_count_); | 824 EXPECT_EQ(1, update_enabled_types_call_count_); |
| 826 | 825 |
| 827 ModelSafeRoutingInfo routes; | 826 ModelSafeRoutingInfo routes; |
| 828 GetModelSafeRoutingInfo(&routes); | 827 GetModelSafeRoutingInfo(&routes); |
| 829 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); | 828 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); |
| 830 ++i) { | 829 ++i) { |
| 831 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _, 1)) | 830 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _)) |
| 832 .RetiresOnSaturation(); | 831 .RetiresOnSaturation(); |
| 833 EXPECT_CALL(observer_, OnChangesComplete(i->first)) | 832 EXPECT_CALL(observer_, OnChangesComplete(i->first)) |
| 834 .RetiresOnSaturation(); | 833 .RetiresOnSaturation(); |
| 835 type_roots_[i->first] = MakeServerNodeForType( | 834 type_roots_[i->first] = MakeServerNodeForType( |
| 836 sync_manager_.GetUserShare(), i->first); | 835 sync_manager_.GetUserShare(), i->first); |
| 837 } | 836 } |
| 838 PumpLoop(); | 837 PumpLoop(); |
| 839 } | 838 } |
| 840 | 839 |
| 841 void TearDown() { | 840 void TearDown() { |
| (...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1549 ReadNode node(&trans); | 1548 ReadNode node(&trans); |
| 1550 EXPECT_TRUE(node.InitByIdLookup(node1)); | 1549 EXPECT_TRUE(node.InitByIdLookup(node1)); |
| 1551 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); | 1550 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); |
| 1552 EXPECT_EQ(title, node.GetTitle()); | 1551 EXPECT_EQ(title, node.GetTitle()); |
| 1553 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); | 1552 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); |
| 1554 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | 1553 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); |
| 1555 } | 1554 } |
| 1556 } | 1555 } |
| 1557 | 1556 |
| 1558 } // namespace browser_sync | 1557 } // namespace browser_sync |
| OLD | NEW |