| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 | |
| 5 #include "components/sync_driver/shared_change_processor.h" | |
| 6 | |
| 7 #include <cstddef> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/compiler_specific.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/single_thread_task_runner.h" | |
| 15 #include "base/synchronization/waitable_event.h" | |
| 16 #include "base/threading/thread.h" | |
| 17 #include "components/sync/api/attachments/attachment_id.h" | |
| 18 #include "components/sync/api/attachments/attachment_store.h" | |
| 19 #include "components/sync/api/fake_syncable_service.h" | |
| 20 #include "components/sync/base/model_type.h" | |
| 21 #include "components/sync/core/attachments/attachment_service_impl.h" | |
| 22 #include "components/sync/core/test/data_type_error_handler_mock.h" | |
| 23 #include "components/sync/core/test/test_user_share.h" | |
| 24 #include "components/sync_driver/fake_sync_client.h" | |
| 25 #include "components/sync_driver/generic_change_processor.h" | |
| 26 #include "components/sync_driver/generic_change_processor_factory.h" | |
| 27 #include "components/sync_driver/local_device_info_provider.h" | |
| 28 #include "components/sync_driver/sync_api_component_factory.h" | |
| 29 #include "testing/gmock/include/gmock/gmock.h" | |
| 30 #include "testing/gtest/include/gtest/gtest.h" | |
| 31 | |
| 32 namespace sync_driver { | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 using ::testing::NiceMock; | |
| 37 using ::testing::StrictMock; | |
| 38 | |
| 39 class TestSyncApiComponentFactory : public SyncApiComponentFactory { | |
| 40 public: | |
| 41 TestSyncApiComponentFactory() {} | |
| 42 ~TestSyncApiComponentFactory() override {} | |
| 43 | |
| 44 // SyncApiComponentFactory implementation. | |
| 45 void RegisterDataTypes( | |
| 46 sync_driver::SyncService* sync_service, | |
| 47 const RegisterDataTypesMethod& register_platform_types_method) override {} | |
| 48 sync_driver::DataTypeManager* CreateDataTypeManager( | |
| 49 const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>& | |
| 50 debug_info_listener, | |
| 51 const sync_driver::DataTypeController::TypeMap* controllers, | |
| 52 const sync_driver::DataTypeEncryptionHandler* encryption_handler, | |
| 53 browser_sync::SyncBackendHost* backend, | |
| 54 sync_driver::DataTypeManagerObserver* observer) override { | |
| 55 return nullptr; | |
| 56 } | |
| 57 browser_sync::SyncBackendHost* CreateSyncBackendHost( | |
| 58 const std::string& name, | |
| 59 invalidation::InvalidationService* invalidator, | |
| 60 const base::WeakPtr<sync_driver::SyncPrefs>& sync_prefs, | |
| 61 const base::FilePath& sync_folder) override { | |
| 62 return nullptr; | |
| 63 } | |
| 64 std::unique_ptr<sync_driver::LocalDeviceInfoProvider> | |
| 65 CreateLocalDeviceInfoProvider() override { | |
| 66 return nullptr; | |
| 67 } | |
| 68 SyncApiComponentFactory::SyncComponents CreateBookmarkSyncComponents( | |
| 69 sync_driver::SyncService* sync_service, | |
| 70 syncer::DataTypeErrorHandler* error_handler) override { | |
| 71 return SyncApiComponentFactory::SyncComponents(nullptr, nullptr); | |
| 72 } | |
| 73 std::unique_ptr<syncer::AttachmentService> CreateAttachmentService( | |
| 74 std::unique_ptr<syncer::AttachmentStoreForSync> attachment_store, | |
| 75 const syncer::UserShare& user_share, | |
| 76 const std::string& store_birthday, | |
| 77 syncer::ModelType model_type, | |
| 78 syncer::AttachmentService::Delegate* delegate) override { | |
| 79 return syncer::AttachmentServiceImpl::CreateForTest(); | |
| 80 } | |
| 81 }; | |
| 82 | |
| 83 class SyncSharedChangeProcessorTest : | |
| 84 public testing::Test, | |
| 85 public FakeSyncClient { | |
| 86 public: | |
| 87 SyncSharedChangeProcessorTest() | |
| 88 : FakeSyncClient(&factory_), | |
| 89 backend_thread_("dbthread"), | |
| 90 did_connect_(false), | |
| 91 has_attachment_service_(false) {} | |
| 92 | |
| 93 ~SyncSharedChangeProcessorTest() override { | |
| 94 EXPECT_FALSE(db_syncable_service_.get()); | |
| 95 } | |
| 96 | |
| 97 // FakeSyncClient override. | |
| 98 base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType( | |
| 99 syncer::ModelType type) override { | |
| 100 return db_syncable_service_->AsWeakPtr(); | |
| 101 } | |
| 102 | |
| 103 protected: | |
| 104 void SetUp() override { | |
| 105 test_user_share_.SetUp(); | |
| 106 shared_change_processor_ = new SharedChangeProcessor(); | |
| 107 ASSERT_TRUE(backend_thread_.Start()); | |
| 108 ASSERT_TRUE(backend_thread_.task_runner()->PostTask( | |
| 109 FROM_HERE, | |
| 110 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService, | |
| 111 base::Unretained(this)))); | |
| 112 } | |
| 113 | |
| 114 void TearDown() override { | |
| 115 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( | |
| 116 FROM_HERE, | |
| 117 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService, | |
| 118 base::Unretained(this)))); | |
| 119 // This must happen before the DB thread is stopped since | |
| 120 // |shared_change_processor_| may post tasks to delete its members | |
| 121 // on the correct thread. | |
| 122 // | |
| 123 // TODO(akalin): Write deterministic tests for the destruction of | |
| 124 // |shared_change_processor_| on the UI and DB threads. | |
| 125 shared_change_processor_ = NULL; | |
| 126 backend_thread_.Stop(); | |
| 127 | |
| 128 // Note: Stop() joins the threads, and that barrier prevents this read | |
| 129 // from being moved (e.g by compiler optimization) in such a way that it | |
| 130 // would race with the write in ConnectOnDBThread (because by this time, | |
| 131 // everything that could have run on |backend_thread_| has done so). | |
| 132 ASSERT_TRUE(did_connect_); | |
| 133 test_user_share_.TearDown(); | |
| 134 } | |
| 135 | |
| 136 // Connect |shared_change_processor_| on the DB thread. | |
| 137 void Connect() { | |
| 138 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( | |
| 139 FROM_HERE, | |
| 140 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread, | |
| 141 base::Unretained(this), shared_change_processor_))); | |
| 142 } | |
| 143 | |
| 144 void SetAttachmentStore() { | |
| 145 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( | |
| 146 FROM_HERE, | |
| 147 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread, | |
| 148 base::Unretained(this)))); | |
| 149 } | |
| 150 | |
| 151 bool HasAttachmentService() { | |
| 152 base::WaitableEvent event(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 153 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 154 EXPECT_TRUE(backend_thread_.task_runner()->PostTask( | |
| 155 FROM_HERE, | |
| 156 base::Bind( | |
| 157 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread, | |
| 158 base::Unretained(this), base::Unretained(&event)))); | |
| 159 event.Wait(); | |
| 160 return has_attachment_service_; | |
| 161 } | |
| 162 | |
| 163 private: | |
| 164 // Used by SetUp(). | |
| 165 void SetUpDBSyncableService() { | |
| 166 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); | |
| 167 DCHECK(!db_syncable_service_.get()); | |
| 168 db_syncable_service_.reset(new syncer::FakeSyncableService()); | |
| 169 } | |
| 170 | |
| 171 // Used by TearDown(). | |
| 172 void TearDownDBSyncableService() { | |
| 173 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); | |
| 174 DCHECK(db_syncable_service_.get()); | |
| 175 db_syncable_service_.reset(); | |
| 176 } | |
| 177 | |
| 178 void SetAttachmentStoreOnDBThread() { | |
| 179 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); | |
| 180 DCHECK(db_syncable_service_.get()); | |
| 181 db_syncable_service_->set_attachment_store( | |
| 182 syncer::AttachmentStore::CreateInMemoryStore()); | |
| 183 } | |
| 184 | |
| 185 // Used by Connect(). The SharedChangeProcessor is passed in | |
| 186 // because we modify |shared_change_processor_| on the main thread | |
| 187 // (in TearDown()). | |
| 188 void ConnectOnDBThread( | |
| 189 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) { | |
| 190 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); | |
| 191 EXPECT_TRUE(shared_change_processor->Connect( | |
| 192 this, &processor_factory_, test_user_share_.user_share(), | |
| 193 &error_handler_, syncer::AUTOFILL, | |
| 194 base::WeakPtr<syncer::SyncMergeResult>())); | |
| 195 did_connect_ = true; | |
| 196 } | |
| 197 | |
| 198 void CheckAttachmentServiceOnDBThread(base::WaitableEvent* event) { | |
| 199 DCHECK(backend_thread_.task_runner()->BelongsToCurrentThread()); | |
| 200 DCHECK(db_syncable_service_.get()); | |
| 201 has_attachment_service_ = !!db_syncable_service_->attachment_service(); | |
| 202 event->Signal(); | |
| 203 } | |
| 204 | |
| 205 base::MessageLoop frontend_loop_; | |
| 206 base::Thread backend_thread_; | |
| 207 syncer::TestUserShare test_user_share_; | |
| 208 TestSyncApiComponentFactory factory_; | |
| 209 | |
| 210 scoped_refptr<SharedChangeProcessor> shared_change_processor_; | |
| 211 StrictMock<syncer::DataTypeErrorHandlerMock> error_handler_; | |
| 212 | |
| 213 GenericChangeProcessorFactory processor_factory_; | |
| 214 bool did_connect_; | |
| 215 bool has_attachment_service_; | |
| 216 | |
| 217 // Used only on DB thread. | |
| 218 std::unique_ptr<syncer::FakeSyncableService> db_syncable_service_; | |
| 219 }; | |
| 220 | |
| 221 // Simply connect the shared change processor. It should succeed, and | |
| 222 // nothing further should happen. | |
| 223 TEST_F(SyncSharedChangeProcessorTest, Basic) { | |
| 224 Connect(); | |
| 225 } | |
| 226 | |
| 227 // Connect the shared change processor to a syncable service with | |
| 228 // AttachmentStore. Verify that shared change processor implementation | |
| 229 // creates AttachmentService and passes it back to the syncable service. | |
| 230 TEST_F(SyncSharedChangeProcessorTest, ConnectWithAttachmentStore) { | |
| 231 SetAttachmentStore(); | |
| 232 Connect(); | |
| 233 EXPECT_TRUE(HasAttachmentService()); | |
| 234 } | |
| 235 | |
| 236 } // namespace | |
| 237 | |
| 238 } // namespace sync_driver | |
| OLD | NEW |