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

Side by Side Diff: sync/internal_api/syncapi_unittest.cc

Issue 10483015: [Sync] Refactor sync configuration logic. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments + rebase Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 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 // 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/callback.h" 13 #include "base/callback.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/format_macros.h" 15 #include "base/format_macros.h"
16 #include "base/location.h" 16 #include "base/location.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop.h" 18 #include "base/message_loop.h"
19 #include "base/message_loop_proxy.h" 19 #include "base/message_loop_proxy.h"
20 #include "base/scoped_temp_dir.h" 20 #include "base/scoped_temp_dir.h"
21 #include "base/string_number_conversions.h" 21 #include "base/string_number_conversions.h"
22 #include "base/stringprintf.h" 22 #include "base/stringprintf.h"
23 #include "base/test/values_test_util.h" 23 #include "base/test/values_test_util.h"
24 #include "base/utf_string_conversions.h" 24 #include "base/utf_string_conversions.h"
25 #include "base/values.h" 25 #include "base/values.h"
26 #include "sync/engine/nigori_util.h" 26 #include "sync/engine/nigori_util.h"
27 #include "sync/engine/sync_scheduler.h"
27 #include "sync/internal_api/change_record.h" 28 #include "sync/internal_api/change_record.h"
28 #include "sync/internal_api/http_post_provider_factory.h" 29 #include "sync/internal_api/http_post_provider_factory.h"
29 #include "sync/internal_api/http_post_provider_interface.h" 30 #include "sync/internal_api/http_post_provider_interface.h"
30 #include "sync/internal_api/public/engine/model_safe_worker.h" 31 #include "sync/internal_api/public/engine/model_safe_worker.h"
31 #include "sync/internal_api/public/engine/polling_constants.h" 32 #include "sync/internal_api/public/engine/polling_constants.h"
32 #include "sync/internal_api/public/syncable/model_type_test_util.h" 33 #include "sync/internal_api/public/syncable/model_type_test_util.h"
33 #include "sync/internal_api/read_node.h" 34 #include "sync/internal_api/read_node.h"
34 #include "sync/internal_api/read_transaction.h" 35 #include "sync/internal_api/read_transaction.h"
35 #include "sync/internal_api/sync_manager.h" 36 #include "sync/internal_api/sync_manager.h"
36 #include "sync/internal_api/syncapi_internal.h" 37 #include "sync/internal_api/syncapi_internal.h"
(...skipping 10 matching lines...) Expand all
47 #include "sync/protocol/bookmark_specifics.pb.h" 48 #include "sync/protocol/bookmark_specifics.pb.h"
48 #include "sync/protocol/encryption.pb.h" 49 #include "sync/protocol/encryption.pb.h"
49 #include "sync/protocol/extension_specifics.pb.h" 50 #include "sync/protocol/extension_specifics.pb.h"
50 #include "sync/protocol/password_specifics.pb.h" 51 #include "sync/protocol/password_specifics.pb.h"
51 #include "sync/protocol/preference_specifics.pb.h" 52 #include "sync/protocol/preference_specifics.pb.h"
52 #include "sync/protocol/proto_value_conversions.h" 53 #include "sync/protocol/proto_value_conversions.h"
53 #include "sync/protocol/sync.pb.h" 54 #include "sync/protocol/sync.pb.h"
54 #include "sync/sessions/sync_session.h" 55 #include "sync/sessions/sync_session.h"
55 #include "sync/syncable/syncable.h" 56 #include "sync/syncable/syncable.h"
56 #include "sync/syncable/syncable_id.h" 57 #include "sync/syncable/syncable_id.h"
58 #include "sync/test/callback_counter.h"
57 #include "sync/test/fake_encryptor.h" 59 #include "sync/test/fake_encryptor.h"
58 #include "sync/test/fake_extensions_activity_monitor.h" 60 #include "sync/test/fake_extensions_activity_monitor.h"
59 #include "sync/util/cryptographer.h" 61 #include "sync/util/cryptographer.h"
60 #include "sync/util/extensions_activity_monitor.h" 62 #include "sync/util/extensions_activity_monitor.h"
61 #include "sync/util/test_unrecoverable_error_handler.h" 63 #include "sync/util/test_unrecoverable_error_handler.h"
62 #include "sync/util/time.h" 64 #include "sync/util/time.h"
63 #include "testing/gmock/include/gmock/gmock.h" 65 #include "testing/gmock/include/gmock/gmock.h"
64 #include "testing/gtest/include/gtest/gtest.h" 66 #include "testing/gtest/include/gtest/gtest.h"
65 67
66 using base::ExpectDictStringValue; 68 using base::ExpectDictStringValue;
69 using browser_sync::CallbackCounter;
67 using browser_sync::Cryptographer; 70 using browser_sync::Cryptographer;
68 using browser_sync::FakeEncryptor; 71 using browser_sync::FakeEncryptor;
69 using browser_sync::FakeExtensionsActivityMonitor; 72 using browser_sync::FakeExtensionsActivityMonitor;
70 using browser_sync::HasArgsAsList; 73 using browser_sync::HasArgsAsList;
71 using browser_sync::HasDetailsAsDictionary; 74 using browser_sync::HasDetailsAsDictionary;
72 using browser_sync::KeyParams; 75 using browser_sync::KeyParams;
73 using browser_sync::kNigoriTag; 76 using browser_sync::kNigoriTag;
74 using browser_sync::JsArgList; 77 using browser_sync::JsArgList;
75 using browser_sync::JsBackend; 78 using browser_sync::JsBackend;
76 using browser_sync::JsEventHandler; 79 using browser_sync::JsEventHandler;
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*)); 716 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*));
714 MOCK_METHOD1(SetUniqueId, void(const std::string&)); 717 MOCK_METHOD1(SetUniqueId, void(const std::string&));
715 MOCK_METHOD1(SetStateDeprecated, void(const std::string&)); 718 MOCK_METHOD1(SetStateDeprecated, void(const std::string&));
716 MOCK_METHOD2(UpdateCredentials, 719 MOCK_METHOD2(UpdateCredentials,
717 void(const std::string&, const std::string&)); 720 void(const std::string&, const std::string&));
718 MOCK_METHOD1(UpdateEnabledTypes, 721 MOCK_METHOD1(UpdateEnabledTypes,
719 void(syncable::ModelTypeSet)); 722 void(syncable::ModelTypeSet));
720 MOCK_METHOD1(SendNotification, void(syncable::ModelTypeSet)); 723 MOCK_METHOD1(SendNotification, void(syncable::ModelTypeSet));
721 }; 724 };
722 725
726 class TestSyncManager : public SyncManager {
727 public:
728 TestSyncManager()
729 : SyncManager("Test sync manager"),
730 result_(true) {}
731
732 browser_sync::ConfigurationParams get_config_params() const {
733 return params_;
734 }
735 void set_config_result(bool result) { result_ = result; }
736
737 protected:
738 // SyncManager implementation.
739 virtual bool DoConfigureSyncer(
tim (not reviewing) 2012/06/15 14:59:37 Ugh. I see the corner you're in here. I'm really
Nicolas Zea 2012/06/15 21:23:36 I see what you're saying, but given that that's ki
tim (not reviewing) 2012/06/15 21:58:24 As we discussed offline, I think we can special ca
740 const browser_sync::ConfigurationParams& params) OVERRIDE {
741 params_ = params;
742 if (params.ready_task.is_null()) {
743 ADD_FAILURE();
tim (not reviewing) 2012/06/15 14:59:37 nit, in general something like ADD_FAILURE() << "N
Nicolas Zea 2012/06/15 21:23:36 Done.
744 return result_;
745 }
746
747 if (result_)
748 params.ready_task.Run();
749 return result_;
750 }
751
752 private:
753 browser_sync::ConfigurationParams params_;
754 bool result_;
755 };
756
723 } // namespace 757 } // namespace
724 758
725 class SyncManagerTest : public testing::Test, 759 class SyncManagerTest : public testing::Test,
726 public SyncManager::ChangeDelegate { 760 public SyncManager::ChangeDelegate {
727 protected: 761 protected:
728 enum NigoriStatus { 762 enum NigoriStatus {
729 DONT_WRITE_NIGORI, 763 DONT_WRITE_NIGORI,
730 WRITE_TO_NIGORI 764 WRITE_TO_NIGORI
731 }; 765 };
732 766
733 enum EncryptionStatus { 767 enum EncryptionStatus {
734 UNINITIALIZED, 768 UNINITIALIZED,
735 DEFAULT_ENCRYPTION, 769 DEFAULT_ENCRYPTION,
736 FULL_ENCRYPTION 770 FULL_ENCRYPTION
737 }; 771 };
738 772
739 SyncManagerTest() 773 SyncManagerTest()
740 : sync_notifier_mock_(NULL), 774 : sync_notifier_mock_(NULL),
741 sync_manager_("Test sync manager"),
742 sync_notifier_observer_(NULL), 775 sync_notifier_observer_(NULL),
743 update_enabled_types_call_count_(0) {} 776 update_enabled_types_call_count_(0) {}
744 777
745 virtual ~SyncManagerTest() { 778 virtual ~SyncManagerTest() {
746 EXPECT_FALSE(sync_notifier_mock_); 779 EXPECT_FALSE(sync_notifier_mock_);
747 } 780 }
748 781
749 // Test implementation. 782 // Test implementation.
750 void SetUp() { 783 void SetUp() {
751 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 784 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 // Needed by |sync_manager_|. 965 // Needed by |sync_manager_|.
933 ScopedTempDir temp_dir_; 966 ScopedTempDir temp_dir_;
934 // Sync Id's for the roots of the enabled datatypes. 967 // Sync Id's for the roots of the enabled datatypes.
935 std::map<ModelType, int64> type_roots_; 968 std::map<ModelType, int64> type_roots_;
936 FakeExtensionsActivityMonitor extensions_activity_monitor_; 969 FakeExtensionsActivityMonitor extensions_activity_monitor_;
937 StrictMock<SyncNotifierMock>* sync_notifier_mock_; 970 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
938 971
939 protected: 972 protected:
940 FakeEncryptor encryptor_; 973 FakeEncryptor encryptor_;
941 TestUnrecoverableErrorHandler handler_; 974 TestUnrecoverableErrorHandler handler_;
942 SyncManager sync_manager_; 975 TestSyncManager sync_manager_;
943 WeakHandle<JsBackend> js_backend_; 976 WeakHandle<JsBackend> js_backend_;
944 StrictMock<SyncManagerObserverMock> observer_; 977 StrictMock<SyncManagerObserverMock> observer_;
945 sync_notifier::SyncNotifierObserver* sync_notifier_observer_; 978 sync_notifier::SyncNotifierObserver* sync_notifier_observer_;
946 int update_enabled_types_call_count_; 979 int update_enabled_types_call_count_;
947 }; 980 };
948 981
949 TEST_F(SyncManagerTest, UpdateEnabledTypes) { 982 TEST_F(SyncManagerTest, UpdateEnabledTypes) {
950 EXPECT_EQ(0, update_enabled_types_call_count_); 983 EXPECT_EQ(0, update_enabled_types_call_count_);
951 984
952 ModelSafeRoutingInfo routes; 985 ModelSafeRoutingInfo routes;
(...skipping 1547 matching lines...) Expand 10 before | Expand all | Expand 10 after
2500 node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag)); 2533 node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
2501 EXPECT_EQ(title, node.GetTitle()); 2534 EXPECT_EQ(title, node.GetTitle());
2502 EXPECT_EQ(GURL(url2), node.GetURL()); 2535 EXPECT_EQ(GURL(url2), node.GetURL());
2503 const syncable::Entry* node_entry = node.GetEntry(); 2536 const syncable::Entry* node_entry = node.GetEntry();
2504 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME)); 2537 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
2505 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS); 2538 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
2506 EXPECT_TRUE(specifics.has_encrypted()); 2539 EXPECT_TRUE(specifics.has_encrypted());
2507 } 2540 }
2508 } 2541 }
2509 2542
2543 // Test that the configuration params are properly sent to DoConfigureSyncer.
2544 TEST_F(SyncManagerTest, BasicConfiguration) {
2545 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2546 syncable::ModelTypeSet types_to_download(syncable::BOOKMARKS,
2547 syncable::PREFERENCES);
2548 browser_sync::ModelSafeRoutingInfo new_routing_info;
2549 GetModelSafeRoutingInfo(&new_routing_info);
2550 sync_manager_.set_config_result(true);
2551
2552 CallbackCounter ready_task_counter, retry_task_counter;
2553 sync_manager_.ConfigureSyncer(
2554 reason,
2555 types_to_download,
2556 new_routing_info,
2557 base::Bind(&CallbackCounter::Callback,
2558 base::Unretained(&ready_task_counter)),
2559 base::Bind(&CallbackCounter::Callback,
2560 base::Unretained(&retry_task_counter)));
2561 EXPECT_EQ(1, ready_task_counter.times_called());
2562 EXPECT_EQ(0, retry_task_counter.times_called());
2563 browser_sync::ConfigurationParams params = sync_manager_.get_config_params();
2564 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2565 params.source);
2566 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2567 EXPECT_EQ(new_routing_info, params.routing_info);
2568 }
2569
2570 // Test that the retry callback is invoked on configuration failure.
2571 TEST_F(SyncManagerTest, ConfigurationRetry) {
2572 ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2573 syncable::ModelTypeSet types_to_download(syncable::BOOKMARKS,
2574 syncable::PREFERENCES);
2575 browser_sync::ModelSafeRoutingInfo new_routing_info;
2576 GetModelSafeRoutingInfo(&new_routing_info);
2577 sync_manager_.set_config_result(false);
2578
2579 CallbackCounter ready_task_counter, retry_task_counter;
2580 sync_manager_.ConfigureSyncer(
2581 reason,
2582 types_to_download,
2583 new_routing_info,
2584 base::Bind(&CallbackCounter::Callback,
2585 base::Unretained(&ready_task_counter)),
2586 base::Bind(&CallbackCounter::Callback,
2587 base::Unretained(&retry_task_counter)));
2588 EXPECT_EQ(0, ready_task_counter.times_called());
2589 EXPECT_EQ(1, retry_task_counter.times_called());
2590 browser_sync::ConfigurationParams params = sync_manager_.get_config_params();
2591 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2592 params.source);
2593 EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2594 EXPECT_EQ(new_routing_info, params.routing_info);
2595 }
2596
2510 } // namespace browser_sync 2597 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698