sync_manager_impl_unittest.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/sync/engine/sync_manager_impl.h"
  5. #include <cstddef>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/compiler_specific.h"
  10. #include "base/files/scoped_temp_dir.h"
  11. #include "base/format_macros.h"
  12. #include "base/location.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/run_loop.h"
  15. #include "base/strings/string_number_conversions.h"
  16. #include "base/strings/utf_string_conversions.h"
  17. #include "base/test/gmock_move_support.h"
  18. #include "base/test/mock_callback.h"
  19. #include "base/test/task_environment.h"
  20. #include "base/test/values_test_util.h"
  21. #include "base/values.h"
  22. #include "components/sync/base/client_tag_hash.h"
  23. #include "components/sync/base/extensions_activity.h"
  24. #include "components/sync/base/model_type.h"
  25. #include "components/sync/base/model_type_test_util.h"
  26. #include "components/sync/engine/cancelation_signal.h"
  27. #include "components/sync/engine/cycle/sync_cycle.h"
  28. #include "components/sync/engine/events/protocol_event.h"
  29. #include "components/sync/engine/net/http_post_provider.h"
  30. #include "components/sync/engine/net/http_post_provider_factory.h"
  31. #include "components/sync/engine/nigori/key_derivation_params.h"
  32. #include "components/sync/engine/polling_constants.h"
  33. #include "components/sync/engine/sync_scheduler.h"
  34. #include "components/sync/protocol/encryption.pb.h"
  35. #include "components/sync/protocol/proto_value_conversions.h"
  36. #include "components/sync/protocol/sync_enums.pb.h"
  37. #include "components/sync/test/fake_sync_encryption_handler.h"
  38. #include "components/sync/test/fake_sync_scheduler.h"
  39. #include "components/sync/test/test_engine_components_factory.h"
  40. #include "services/network/test/test_network_connection_tracker.h"
  41. #include "testing/gmock/include/gmock/gmock.h"
  42. #include "testing/gtest/include/gtest/gtest.h"
  43. #include "third_party/protobuf/src/google/protobuf/io/coded_stream.h"
  44. #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h"
  45. #include "url/gurl.h"
  46. using testing::_;
  47. using testing::StrictMock;
  48. namespace syncer {
  49. namespace {
  50. class TestHttpPostProvider : public HttpPostProvider {
  51. public:
  52. void SetExtraRequestHeaders(const char* headers) override {}
  53. void SetURL(const GURL& url) override {}
  54. void SetPostPayload(const char* content_type,
  55. int content_length,
  56. const char* content) override {}
  57. void SetAllowBatching(bool allow_batching) override {}
  58. bool MakeSynchronousPost(int* net_error_code,
  59. int* http_status_code) override {
  60. return false;
  61. }
  62. int GetResponseContentLength() const override { return 0; }
  63. const char* GetResponseContent() const override { return ""; }
  64. const std::string GetResponseHeaderValue(
  65. const std::string& name) const override {
  66. return std::string();
  67. }
  68. void Abort() override {}
  69. private:
  70. ~TestHttpPostProvider() override = default;
  71. };
  72. class TestHttpPostProviderFactory : public HttpPostProviderFactory {
  73. public:
  74. ~TestHttpPostProviderFactory() override = default;
  75. scoped_refptr<HttpPostProvider> Create() override {
  76. return new TestHttpPostProvider();
  77. }
  78. };
  79. class SyncManagerObserverMock : public SyncManager::Observer {
  80. public:
  81. MOCK_METHOD(void,
  82. OnSyncCycleCompleted,
  83. (const SyncCycleSnapshot&),
  84. (override));
  85. MOCK_METHOD(void, OnConnectionStatusChange, (ConnectionStatus), (override));
  86. MOCK_METHOD(void, OnActionableError, (const SyncProtocolError&), (override));
  87. MOCK_METHOD(void, OnMigrationRequested, (ModelTypeSet), (override));
  88. MOCK_METHOD(void, OnProtocolEvent, (const ProtocolEvent&), (override));
  89. MOCK_METHOD(void, OnSyncStatusChanged, (const SyncStatus&), (override));
  90. };
  91. class SyncEncryptionHandlerObserverMock
  92. : public SyncEncryptionHandler::Observer {
  93. public:
  94. MOCK_METHOD(void,
  95. OnPassphraseRequired,
  96. (const KeyDerivationParams&, const sync_pb::EncryptedData&),
  97. (override));
  98. MOCK_METHOD(void, OnPassphraseAccepted, (), (override));
  99. MOCK_METHOD(void, OnTrustedVaultKeyRequired, (), (override));
  100. MOCK_METHOD(void, OnTrustedVaultKeyAccepted, (), (override));
  101. MOCK_METHOD(void, OnEncryptedTypesChanged, (ModelTypeSet, bool), (override));
  102. MOCK_METHOD(void,
  103. OnCryptographerStateChanged,
  104. (Cryptographer*, bool),
  105. (override));
  106. MOCK_METHOD(void,
  107. OnPassphraseTypeChanged,
  108. (PassphraseType, base::Time),
  109. (override));
  110. };
  111. class MockSyncScheduler : public FakeSyncScheduler {
  112. public:
  113. MockSyncScheduler() = default;
  114. ~MockSyncScheduler() override = default;
  115. MOCK_METHOD(void, Start, (SyncScheduler::Mode, base::Time), (override));
  116. MOCK_METHOD(void,
  117. ScheduleConfiguration,
  118. (sync_pb::SyncEnums::GetUpdatesOrigin origin,
  119. ModelTypeSet types_to_download,
  120. base::OnceClosure ready_task),
  121. (override));
  122. };
  123. class ComponentsFactory : public TestEngineComponentsFactory {
  124. public:
  125. explicit ComponentsFactory(std::unique_ptr<SyncScheduler> scheduler_to_use)
  126. : scheduler_to_use_(std::move(scheduler_to_use)) {}
  127. ~ComponentsFactory() override = default;
  128. std::unique_ptr<SyncScheduler> BuildScheduler(
  129. const std::string& name,
  130. SyncCycleContext* context,
  131. CancelationSignal* stop_handle,
  132. bool local_sync_backend_enabled) override {
  133. DCHECK(scheduler_to_use_);
  134. return std::move(scheduler_to_use_);
  135. }
  136. private:
  137. std::unique_ptr<SyncScheduler> scheduler_to_use_;
  138. };
  139. class SyncManagerImplTest : public testing::Test {
  140. protected:
  141. SyncManagerImplTest()
  142. : sync_manager_("Test sync manager",
  143. network::TestNetworkConnectionTracker::GetInstance()) {}
  144. ~SyncManagerImplTest() override = default;
  145. void SetUp() override {
  146. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  147. extensions_activity_ = new ExtensionsActivity();
  148. sync_manager_.AddObserver(&manager_observer_);
  149. // Save raw pointers to the objects that won't be owned by the fixture.
  150. auto encryption_observer =
  151. std::make_unique<StrictMock<SyncEncryptionHandlerObserverMock>>();
  152. encryption_observer_ = encryption_observer.get();
  153. auto scheduler = std::make_unique<MockSyncScheduler>();
  154. scheduler_ = scheduler.get();
  155. // This should be the only method called by the Init() in the observer.
  156. EXPECT_CALL(manager_observer_, OnSyncStatusChanged).Times(3);
  157. SyncManager::InitArgs args;
  158. args.service_url = GURL("https://example.com/");
  159. args.post_factory = std::make_unique<TestHttpPostProviderFactory>();
  160. args.encryption_observer_proxy = std::move(encryption_observer);
  161. args.extensions_activity = extensions_activity_.get();
  162. args.cache_guid = "fake_cache_guid";
  163. args.invalidator_client_id = "fake_invalidator_client_id";
  164. args.enable_local_sync_backend = false;
  165. args.local_sync_backend_folder = temp_dir_.GetPath();
  166. args.engine_components_factory =
  167. std::make_unique<ComponentsFactory>(std::move(scheduler));
  168. args.encryption_handler = &encryption_handler_;
  169. args.cancelation_signal = &cancelation_signal_;
  170. args.poll_interval = base::Minutes(60);
  171. sync_manager_.Init(&args);
  172. base::RunLoop().RunUntilIdle();
  173. }
  174. void TearDown() override {
  175. sync_manager_.RemoveObserver(&manager_observer_);
  176. sync_manager_.ShutdownOnSyncThread();
  177. base::RunLoop().RunUntilIdle();
  178. }
  179. SyncManagerImpl* sync_manager() { return &sync_manager_; }
  180. MockSyncScheduler* scheduler() { return scheduler_; }
  181. private:
  182. base::test::SingleThreadTaskEnvironment task_environment_;
  183. base::ScopedTempDir temp_dir_;
  184. scoped_refptr<ExtensionsActivity> extensions_activity_;
  185. FakeSyncEncryptionHandler encryption_handler_;
  186. SyncManagerImpl sync_manager_;
  187. CancelationSignal cancelation_signal_;
  188. StrictMock<SyncManagerObserverMock> manager_observer_;
  189. // Owned by |sync_manager_|.
  190. raw_ptr<StrictMock<SyncEncryptionHandlerObserverMock>> encryption_observer_ =
  191. nullptr;
  192. raw_ptr<MockSyncScheduler> scheduler_ = nullptr;
  193. };
  194. // Test that the configuration params are properly created and sent to
  195. // ScheduleConfigure. No callback should be invoked.
  196. TEST_F(SyncManagerImplTest, BasicConfiguration) {
  197. ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES);
  198. base::MockOnceClosure ready_task;
  199. EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE, _));
  200. EXPECT_CALL(*scheduler(),
  201. ScheduleConfiguration(sync_pb::SyncEnums::RECONFIGURATION,
  202. types_to_download, _));
  203. EXPECT_CALL(ready_task, Run).Times(0);
  204. sync_manager()->ConfigureSyncer(
  205. CONFIGURE_REASON_RECONFIGURATION, types_to_download,
  206. SyncManager::SyncFeatureState::ON, ready_task.Get());
  207. }
  208. } // namespace
  209. } // namespace syncer