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.
5 #include "components/sync_driver/shared_change_processor.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/location.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "base/threading/thread.h"
16 #include "components/sync_driver/data_type_error_handler_mock.h"
17 #include "components/sync_driver/fake_sync_client.h"
18 #include "components/sync_driver/generic_change_processor.h"
19 #include "components/sync_driver/generic_change_processor_factory.h"
20 #include "components/sync_driver/local_device_info_provider.h"
21 #include "components/sync_driver/sync_api_component_factory.h"
22 #include "sync/api/attachments/attachment_id.h"
23 #include "sync/api/attachments/attachment_store.h"
24 #include "sync/api/fake_syncable_service.h"
25 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
26 #include "sync/internal_api/public/base/model_type.h"
27 #include "sync/internal_api/public/test/test_user_share.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 namespace sync_driver
{
35 using ::testing::NiceMock
;
36 using ::testing::StrictMock
;
38 class TestSyncApiComponentFactory
: public SyncApiComponentFactory
{
40 TestSyncApiComponentFactory() {}
41 ~TestSyncApiComponentFactory() override
{}
43 // SyncApiComponentFactory implementation.
44 void Initialize(sync_driver::SyncService
* pss
) override
{}
45 void RegisterDataTypes() override
{}
46 sync_driver::DataTypeManager
* CreateDataTypeManager(
47 const syncer::WeakHandle
<syncer::DataTypeDebugInfoListener
>&
49 const sync_driver::DataTypeController::TypeMap
* controllers
,
50 const sync_driver::DataTypeEncryptionHandler
* encryption_handler
,
51 browser_sync::SyncBackendHost
* backend
,
52 sync_driver::DataTypeManagerObserver
* observer
) override
{
55 browser_sync::SyncBackendHost
* CreateSyncBackendHost(
56 const std::string
& name
,
57 invalidation::InvalidationService
* invalidator
,
58 const base::WeakPtr
<sync_driver::SyncPrefs
>& sync_prefs
,
59 const base::FilePath
& sync_folder
) override
{
62 scoped_ptr
<sync_driver::LocalDeviceInfoProvider
>
63 CreateLocalDeviceInfoProvider() override
{
66 SyncApiComponentFactory::SyncComponents
CreateBookmarkSyncComponents(
67 sync_driver::SyncService
* sync_service
,
68 sync_driver::DataTypeErrorHandler
* error_handler
) override
{
69 return SyncApiComponentFactory::SyncComponents(nullptr, nullptr);
71 SyncApiComponentFactory::SyncComponents
CreateTypedUrlSyncComponents(
72 sync_driver::SyncService
* sync_service
,
73 history::HistoryBackend
* history_backend
,
74 sync_driver::DataTypeErrorHandler
* error_handler
) override
{
75 return SyncApiComponentFactory::SyncComponents(nullptr, nullptr);
77 scoped_ptr
<syncer::AttachmentService
> CreateAttachmentService(
78 scoped_ptr
<syncer::AttachmentStoreForSync
> attachment_store
,
79 const syncer::UserShare
& user_share
,
80 const std::string
& store_birthday
,
81 syncer::ModelType model_type
,
82 syncer::AttachmentService::Delegate
* delegate
) override
{
83 return syncer::AttachmentServiceImpl::CreateForTest();
87 class SyncSharedChangeProcessorTest
:
89 public FakeSyncClient
{
91 SyncSharedChangeProcessorTest()
92 : FakeSyncClient(&factory_
),
93 backend_thread_("dbthread"),
95 has_attachment_service_(false) {}
97 ~SyncSharedChangeProcessorTest() override
{
98 EXPECT_FALSE(db_syncable_service_
.get());
101 // FakeSyncClient override.
102 base::WeakPtr
<syncer::SyncableService
> GetSyncableServiceForType(
103 syncer::ModelType type
) override
{
104 return db_syncable_service_
->AsWeakPtr();
108 void SetUp() override
{
109 test_user_share_
.SetUp();
110 shared_change_processor_
= new SharedChangeProcessor();
111 ASSERT_TRUE(backend_thread_
.Start());
112 ASSERT_TRUE(backend_thread_
.task_runner()->PostTask(
114 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService
,
115 base::Unretained(this))));
118 void TearDown() override
{
119 EXPECT_TRUE(backend_thread_
.task_runner()->PostTask(
121 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService
,
122 base::Unretained(this))));
123 // This must happen before the DB thread is stopped since
124 // |shared_change_processor_| may post tasks to delete its members
125 // on the correct thread.
127 // TODO(akalin): Write deterministic tests for the destruction of
128 // |shared_change_processor_| on the UI and DB threads.
129 shared_change_processor_
= NULL
;
130 backend_thread_
.Stop();
132 // Note: Stop() joins the threads, and that barrier prevents this read
133 // from being moved (e.g by compiler optimization) in such a way that it
134 // would race with the write in ConnectOnDBThread (because by this time,
135 // everything that could have run on |backend_thread_| has done so).
136 ASSERT_TRUE(did_connect_
);
137 test_user_share_
.TearDown();
140 // Connect |shared_change_processor_| on the DB thread.
142 EXPECT_TRUE(backend_thread_
.task_runner()->PostTask(
144 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread
,
145 base::Unretained(this), shared_change_processor_
)));
148 void SetAttachmentStore() {
149 EXPECT_TRUE(backend_thread_
.task_runner()->PostTask(
151 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread
,
152 base::Unretained(this))));
155 bool HasAttachmentService() {
156 base::WaitableEvent
event(false, false);
157 EXPECT_TRUE(backend_thread_
.task_runner()->PostTask(
160 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread
,
161 base::Unretained(this), base::Unretained(&event
))));
163 return has_attachment_service_
;
168 void SetUpDBSyncableService() {
169 DCHECK(backend_thread_
.task_runner()->BelongsToCurrentThread());
170 DCHECK(!db_syncable_service_
.get());
171 db_syncable_service_
.reset(new syncer::FakeSyncableService());
174 // Used by TearDown().
175 void TearDownDBSyncableService() {
176 DCHECK(backend_thread_
.task_runner()->BelongsToCurrentThread());
177 DCHECK(db_syncable_service_
.get());
178 db_syncable_service_
.reset();
181 void SetAttachmentStoreOnDBThread() {
182 DCHECK(backend_thread_
.task_runner()->BelongsToCurrentThread());
183 DCHECK(db_syncable_service_
.get());
184 db_syncable_service_
->set_attachment_store(
185 syncer::AttachmentStore::CreateInMemoryStore());
188 // Used by Connect(). The SharedChangeProcessor is passed in
189 // because we modify |shared_change_processor_| on the main thread
191 void ConnectOnDBThread(
192 const scoped_refptr
<SharedChangeProcessor
>& shared_change_processor
) {
193 DCHECK(backend_thread_
.task_runner()->BelongsToCurrentThread());
194 EXPECT_TRUE(shared_change_processor
->Connect(
195 this, &processor_factory_
, test_user_share_
.user_share(),
196 &error_handler_
, syncer::AUTOFILL
,
197 base::WeakPtr
<syncer::SyncMergeResult
>()));
201 void CheckAttachmentServiceOnDBThread(base::WaitableEvent
* event
) {
202 DCHECK(backend_thread_
.task_runner()->BelongsToCurrentThread());
203 DCHECK(db_syncable_service_
.get());
204 has_attachment_service_
= !!db_syncable_service_
->attachment_service();
208 base::MessageLoop frontend_loop_
;
209 base::Thread backend_thread_
;
210 syncer::TestUserShare test_user_share_
;
211 TestSyncApiComponentFactory factory_
;
213 scoped_refptr
<SharedChangeProcessor
> shared_change_processor_
;
214 StrictMock
<DataTypeErrorHandlerMock
> error_handler_
;
216 GenericChangeProcessorFactory processor_factory_
;
218 bool has_attachment_service_
;
220 // Used only on DB thread.
221 scoped_ptr
<syncer::FakeSyncableService
> db_syncable_service_
;
224 // Simply connect the shared change processor. It should succeed, and
225 // nothing further should happen.
226 TEST_F(SyncSharedChangeProcessorTest
, Basic
) {
230 // Connect the shared change processor to a syncable service with
231 // AttachmentStore. Verify that shared change processor implementation
232 // creates AttachmentService and passes it back to the syncable service.
233 TEST_F(SyncSharedChangeProcessorTest
, ConnectWithAttachmentStore
) {
234 SetAttachmentStore();
236 EXPECT_TRUE(HasAttachmentService());
241 } // namespace sync_driver