| 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 expected_value, |
| 560 expected_value, | |
| 561 const DictionaryValue& value, | 561 const DictionaryValue& value, |
| 562 const std::string& key) { | 562 const std::string& key) { |
| 563 std::string str_value; | 563 std::string str_value; |
| 564 EXPECT_TRUE(value.GetString(key, &str_value)); | 564 EXPECT_TRUE(value.GetString(key, &str_value)); |
| 565 switch (expected_value) { | 565 switch (expected_value) { |
| 566 case SyncManager::ChangeRecord::ACTION_ADD: | 566 case ChangeRecord::ACTION_ADD: |
| 567 EXPECT_EQ("Add", str_value); | 567 EXPECT_EQ("Add", str_value); |
| 568 break; | 568 break; |
| 569 case SyncManager::ChangeRecord::ACTION_UPDATE: | 569 case ChangeRecord::ACTION_UPDATE: |
| 570 EXPECT_EQ("Update", str_value); | 570 EXPECT_EQ("Update", str_value); |
| 571 break; | 571 break; |
| 572 case SyncManager::ChangeRecord::ACTION_DELETE: | 572 case ChangeRecord::ACTION_DELETE: |
| 573 EXPECT_EQ("Delete", str_value); | 573 EXPECT_EQ("Delete", str_value); |
| 574 break; | 574 break; |
| 575 default: | 575 default: |
| 576 NOTREACHED(); | 576 NOTREACHED(); |
| 577 break; | 577 break; |
| 578 } | 578 } |
| 579 } | 579 } |
| 580 | 580 |
| 581 void CheckNonDeleteChangeRecordValue(const SyncManager::ChangeRecord& record, | 581 void CheckNonDeleteChangeRecordValue(const ChangeRecord& record, |
| 582 const DictionaryValue& value, | 582 const DictionaryValue& value, |
| 583 BaseTransaction* trans) { | 583 BaseTransaction* trans) { |
| 584 EXPECT_NE(SyncManager::ChangeRecord::ACTION_DELETE, record.action); | 584 EXPECT_NE(ChangeRecord::ACTION_DELETE, record.action); |
| 585 ExpectChangeRecordActionValue(record.action, value, "action"); | 585 ExpectChangeRecordActionValue(record.action, value, "action"); |
| 586 { | 586 { |
| 587 ReadNode node(trans); | 587 ReadNode node(trans); |
| 588 EXPECT_TRUE(node.InitByIdLookup(record.id)); | 588 EXPECT_TRUE(node.InitByIdLookup(record.id)); |
| 589 scoped_ptr<DictionaryValue> expected_details(node.GetDetailsAsValue()); | 589 scoped_ptr<DictionaryValue> expected_details(node.GetDetailsAsValue()); |
| 590 ExpectDictDictionaryValue(*expected_details, value, "node"); | 590 ExpectDictDictionaryValue(*expected_details, value, "node"); |
| 591 } | 591 } |
| 592 } | 592 } |
| 593 | 593 |
| 594 void CheckDeleteChangeRecordValue(const SyncManager::ChangeRecord& record, | 594 void CheckDeleteChangeRecordValue(const ChangeRecord& record, |
| 595 const DictionaryValue& value) { | 595 const DictionaryValue& value) { |
| 596 EXPECT_EQ(SyncManager::ChangeRecord::ACTION_DELETE, record.action); | 596 EXPECT_EQ(ChangeRecord::ACTION_DELETE, record.action); |
| 597 ExpectChangeRecordActionValue(record.action, value, "action"); | 597 ExpectChangeRecordActionValue(record.action, value, "action"); |
| 598 DictionaryValue* node_value = NULL; | 598 DictionaryValue* node_value = NULL; |
| 599 EXPECT_TRUE(value.GetDictionary("node", &node_value)); | 599 EXPECT_TRUE(value.GetDictionary("node", &node_value)); |
| 600 if (node_value) { | 600 if (node_value) { |
| 601 ExpectInt64Value(record.id, *node_value, "id"); | 601 ExpectInt64Value(record.id, *node_value, "id"); |
| 602 scoped_ptr<DictionaryValue> expected_specifics_value( | 602 scoped_ptr<DictionaryValue> expected_specifics_value( |
| 603 browser_sync::EntitySpecificsToValue(record.specifics)); | 603 browser_sync::EntitySpecificsToValue(record.specifics)); |
| 604 ExpectDictDictionaryValue(*expected_specifics_value, | 604 ExpectDictDictionaryValue(*expected_specifics_value, |
| 605 *node_value, "specifics"); | 605 *node_value, "specifics"); |
| 606 scoped_ptr<DictionaryValue> expected_extra_value; | 606 scoped_ptr<DictionaryValue> expected_extra_value; |
| 607 if (record.extra.get()) { | 607 if (record.extra.get()) { |
| 608 expected_extra_value.reset(record.extra->ToValue()); | 608 expected_extra_value.reset(record.extra->ToValue()); |
| 609 } | 609 } |
| 610 Value* extra_value = NULL; | 610 Value* extra_value = NULL; |
| 611 EXPECT_EQ(record.extra.get() != NULL, | 611 EXPECT_EQ(record.extra.get() != NULL, |
| 612 node_value->Get("extra", &extra_value)); | 612 node_value->Get("extra", &extra_value)); |
| 613 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); | 613 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); |
| 614 } | 614 } |
| 615 } | 615 } |
| 616 | 616 |
| 617 class MockExtraChangeRecordData | 617 class MockExtraChangeRecordData |
| 618 : public SyncManager::ExtraPasswordChangeRecordData { | 618 : public ExtraPasswordChangeRecordData { |
| 619 public: | 619 public: |
| 620 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); | 620 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); |
| 621 }; | 621 }; |
| 622 | 622 |
| 623 } // namespace | 623 } // namespace |
| 624 | 624 |
| 625 TEST_F(SyncApiTest, ChangeRecordToValue) { | 625 TEST_F(SyncApiTest, ChangeRecordToValue) { |
| 626 int64 child_id = MakeNode(test_user_share_.user_share(), | 626 int64 child_id = MakeNode(test_user_share_.user_share(), |
| 627 syncable::BOOKMARKS, "testtag"); | 627 syncable::BOOKMARKS, "testtag"); |
| 628 sync_pb::EntitySpecifics child_specifics; | 628 sync_pb::EntitySpecifics child_specifics; |
| 629 { | 629 { |
| 630 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 630 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 631 ReadNode node(&trans); | 631 ReadNode node(&trans); |
| 632 EXPECT_TRUE(node.InitByIdLookup(child_id)); | 632 EXPECT_TRUE(node.InitByIdLookup(child_id)); |
| 633 child_specifics = node.GetEntry()->Get(syncable::SPECIFICS); | 633 child_specifics = node.GetEntry()->Get(syncable::SPECIFICS); |
| 634 } | 634 } |
| 635 | 635 |
| 636 // Add | 636 // Add |
| 637 { | 637 { |
| 638 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 638 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 639 SyncManager::ChangeRecord record; | 639 ChangeRecord record; |
| 640 record.action = SyncManager::ChangeRecord::ACTION_ADD; | 640 record.action = ChangeRecord::ACTION_ADD; |
| 641 record.id = 1; | 641 record.id = 1; |
| 642 record.specifics = child_specifics; | 642 record.specifics = child_specifics; |
| 643 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); | 643 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); |
| 644 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); | 644 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); |
| 645 CheckNonDeleteChangeRecordValue(record, *value, &trans); | 645 CheckNonDeleteChangeRecordValue(record, *value, &trans); |
| 646 } | 646 } |
| 647 | 647 |
| 648 // Update | 648 // Update |
| 649 { | 649 { |
| 650 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 650 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 651 SyncManager::ChangeRecord record; | 651 ChangeRecord record; |
| 652 record.action = SyncManager::ChangeRecord::ACTION_UPDATE; | 652 record.action = ChangeRecord::ACTION_UPDATE; |
| 653 record.id = child_id; | 653 record.id = child_id; |
| 654 record.specifics = child_specifics; | 654 record.specifics = child_specifics; |
| 655 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); | 655 record.extra.reset(new StrictMock<MockExtraChangeRecordData>()); |
| 656 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); | 656 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); |
| 657 CheckNonDeleteChangeRecordValue(record, *value, &trans); | 657 CheckNonDeleteChangeRecordValue(record, *value, &trans); |
| 658 } | 658 } |
| 659 | 659 |
| 660 // Delete (no extra) | 660 // Delete (no extra) |
| 661 { | 661 { |
| 662 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 662 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 663 SyncManager::ChangeRecord record; | 663 ChangeRecord record; |
| 664 record.action = SyncManager::ChangeRecord::ACTION_DELETE; | 664 record.action = ChangeRecord::ACTION_DELETE; |
| 665 record.id = child_id + 1; | 665 record.id = child_id + 1; |
| 666 record.specifics = child_specifics; | 666 record.specifics = child_specifics; |
| 667 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); | 667 scoped_ptr<DictionaryValue> value(record.ToValue(&trans)); |
| 668 CheckDeleteChangeRecordValue(record, *value); | 668 CheckDeleteChangeRecordValue(record, *value); |
| 669 } | 669 } |
| 670 | 670 |
| 671 // Delete (with extra) | 671 // Delete (with extra) |
| 672 { | 672 { |
| 673 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); | 673 ReadTransaction trans(FROM_HERE, test_user_share_.user_share()); |
| 674 SyncManager::ChangeRecord record; | 674 ChangeRecord record; |
| 675 record.action = SyncManager::ChangeRecord::ACTION_DELETE; | 675 record.action = ChangeRecord::ACTION_DELETE; |
| 676 record.id = child_id + 1; | 676 record.id = child_id + 1; |
| 677 record.specifics = child_specifics; | 677 record.specifics = child_specifics; |
| 678 | 678 |
| 679 DictionaryValue extra_value; | 679 DictionaryValue extra_value; |
| 680 extra_value.SetString("foo", "bar"); | 680 extra_value.SetString("foo", "bar"); |
| 681 scoped_ptr<StrictMock<MockExtraChangeRecordData> > extra( | 681 scoped_ptr<StrictMock<MockExtraChangeRecordData> > extra( |
| 682 new StrictMock<MockExtraChangeRecordData>()); | 682 new StrictMock<MockExtraChangeRecordData>()); |
| 683 EXPECT_CALL(*extra, ToValue()).Times(2).WillRepeatedly( | 683 EXPECT_CALL(*extra, ToValue()).Times(2).WillRepeatedly( |
| 684 Invoke(&extra_value, &DictionaryValue::DeepCopy)); | 684 Invoke(&extra_value, &DictionaryValue::DeepCopy)); |
| 685 | 685 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 virtual HttpPostProviderInterface* Create() OVERRIDE { | 724 virtual HttpPostProviderInterface* Create() OVERRIDE { |
| 725 return new TestHttpPostProviderInterface(); | 725 return new TestHttpPostProviderInterface(); |
| 726 } | 726 } |
| 727 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { | 727 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { |
| 728 delete http; | 728 delete http; |
| 729 } | 729 } |
| 730 }; | 730 }; |
| 731 | 731 |
| 732 class SyncManagerObserverMock : public SyncManager::Observer { | 732 class SyncManagerObserverMock : public SyncManager::Observer { |
| 733 public: | 733 public: |
| 734 MOCK_METHOD4(OnChangesApplied, | 734 MOCK_METHOD3(OnChangesApplied, |
| 735 void(ModelType, | 735 void(ModelType, |
| 736 const BaseTransaction*, | 736 const BaseTransaction*, |
| 737 const SyncManager::ChangeRecord*, | 737 const ImmutableChangeRecordList&)); // NOLINT |
| 738 int)); // NOLINT | |
| 739 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT | 738 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT |
| 740 MOCK_METHOD1(OnSyncCycleCompleted, | 739 MOCK_METHOD1(OnSyncCycleCompleted, |
| 741 void(const SyncSessionSnapshot*)); // NOLINT | 740 void(const SyncSessionSnapshot*)); // NOLINT |
| 742 MOCK_METHOD2(OnInitializationComplete, | 741 MOCK_METHOD2(OnInitializationComplete, |
| 743 void(const WeakHandle<JsBackend>&, bool)); // NOLINT | 742 void(const WeakHandle<JsBackend>&, bool)); // NOLINT |
| 744 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT | 743 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT |
| 745 MOCK_METHOD1(OnPassphraseRequired, | 744 MOCK_METHOD1(OnPassphraseRequired, |
| 746 void(sync_api::PassphraseRequiredReason)); // NOLINT | 745 void(sync_api::PassphraseRequiredReason)); // NOLINT |
| 747 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT | 746 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT |
| 748 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT | 747 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT |
| 749 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT | 748 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT |
| 750 MOCK_METHOD1(OnMigrationNeededForTypes, void(const ModelTypeSet&)); | |
| 751 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT | 749 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT |
| 752 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT | 750 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT |
| 753 MOCK_METHOD1(OnEncryptionComplete, void(const ModelTypeSet&)); // NOLINT | 751 MOCK_METHOD1(OnEncryptionComplete, void(const ModelTypeSet&)); // NOLINT |
| 754 MOCK_METHOD1(OnActionableError, | 752 MOCK_METHOD1(OnActionableError, |
| 755 void(const browser_sync::SyncProtocolError&)); // NOLINT | 753 void(const browser_sync::SyncProtocolError&)); // NOLINT |
| 756 }; | 754 }; |
| 757 | 755 |
| 758 class SyncNotifierMock : public sync_notifier::SyncNotifier { | 756 class SyncNotifierMock : public sync_notifier::SyncNotifier { |
| 759 public: | 757 public: |
| 760 MOCK_METHOD1(AddObserver, void(sync_notifier::SyncNotifierObserver*)); | 758 MOCK_METHOD1(AddObserver, void(sync_notifier::SyncNotifierObserver*)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 | 819 |
| 822 EXPECT_TRUE(sync_notifier_observer_); | 820 EXPECT_TRUE(sync_notifier_observer_); |
| 823 EXPECT_TRUE(js_backend_.IsInitialized()); | 821 EXPECT_TRUE(js_backend_.IsInitialized()); |
| 824 | 822 |
| 825 EXPECT_EQ(1, update_enabled_types_call_count_); | 823 EXPECT_EQ(1, update_enabled_types_call_count_); |
| 826 | 824 |
| 827 ModelSafeRoutingInfo routes; | 825 ModelSafeRoutingInfo routes; |
| 828 GetModelSafeRoutingInfo(&routes); | 826 GetModelSafeRoutingInfo(&routes); |
| 829 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); | 827 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); |
| 830 ++i) { | 828 ++i) { |
| 831 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _, 1)) | 829 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _)) |
| 832 .RetiresOnSaturation(); | 830 .RetiresOnSaturation(); |
| 833 EXPECT_CALL(observer_, OnChangesComplete(i->first)) | 831 EXPECT_CALL(observer_, OnChangesComplete(i->first)) |
| 834 .RetiresOnSaturation(); | 832 .RetiresOnSaturation(); |
| 835 type_roots_[i->first] = MakeServerNodeForType( | 833 type_roots_[i->first] = MakeServerNodeForType( |
| 836 sync_manager_.GetUserShare(), i->first); | 834 sync_manager_.GetUserShare(), i->first); |
| 837 } | 835 } |
| 838 PumpLoop(); | 836 PumpLoop(); |
| 839 } | 837 } |
| 840 | 838 |
| 841 void TearDown() { | 839 void TearDown() { |
| (...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1549 ReadNode node(&trans); | 1547 ReadNode node(&trans); |
| 1550 EXPECT_TRUE(node.InitByIdLookup(node1)); | 1548 EXPECT_TRUE(node.InitByIdLookup(node1)); |
| 1551 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); | 1549 EXPECT_EQ(syncable::BOOKMARKS, node.GetModelType()); |
| 1552 EXPECT_EQ(title, node.GetTitle()); | 1550 EXPECT_EQ(title, node.GetTitle()); |
| 1553 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); | 1551 EXPECT_EQ(title, node.GetBookmarkSpecifics().title()); |
| 1554 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); | 1552 EXPECT_EQ(url, node.GetBookmarkSpecifics().url()); |
| 1555 } | 1553 } |
| 1556 } | 1554 } |
| 1557 | 1555 |
| 1558 } // namespace browser_sync | 1556 } // namespace browser_sync |
| OLD | NEW |