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/message_loop/message_loop.h"
13 #include "base/threading/thread.h"
14 #include "components/sync_driver/data_type_error_handler_mock.h"
15 #include "components/sync_driver/generic_change_processor.h"
16 #include "components/sync_driver/generic_change_processor_factory.h"
17 #include "components/sync_driver/sync_api_component_factory.h"
18 #include "sync/api/fake_syncable_service.h"
19 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace sync_driver
{
27 using ::testing::NiceMock
;
28 using ::testing::StrictMock
;
30 class SyncSharedChangeProcessorTest
:
32 public SyncApiComponentFactory
{
34 SyncSharedChangeProcessorTest() : backend_thread_("dbthread"),
35 did_connect_(false) {}
37 virtual ~SyncSharedChangeProcessorTest() {
38 EXPECT_FALSE(db_syncable_service_
.get());
41 virtual base::WeakPtr
<syncer::SyncableService
> GetSyncableServiceForType(
42 syncer::ModelType type
) OVERRIDE
{
43 return db_syncable_service_
->AsWeakPtr();
46 virtual scoped_ptr
<syncer::AttachmentService
> CreateAttachmentService(
47 const syncer::UserShare
& user_share
,
48 syncer::AttachmentService::Delegate
* delegate
) OVERRIDE
{
49 return syncer::AttachmentServiceImpl::CreateForTest();
53 virtual void SetUp() OVERRIDE
{
54 shared_change_processor_
= new SharedChangeProcessor();
55 ASSERT_TRUE(backend_thread_
.Start());
56 ASSERT_TRUE(backend_thread_
.message_loop_proxy()->PostTask(
58 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService
,
59 base::Unretained(this))));
62 virtual void TearDown() OVERRIDE
{
63 EXPECT_TRUE(backend_thread_
.message_loop_proxy()->PostTask(
65 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService
,
66 base::Unretained(this))));
67 // This must happen before the DB thread is stopped since
68 // |shared_change_processor_| may post tasks to delete its members
69 // on the correct thread.
71 // TODO(akalin): Write deterministic tests for the destruction of
72 // |shared_change_processor_| on the UI and DB threads.
73 shared_change_processor_
= NULL
;
74 backend_thread_
.Stop();
76 // Note: Stop() joins the threads, and that barrier prevents this read
77 // from being moved (e.g by compiler optimization) in such a way that it
78 // would race with the write in ConnectOnDBThread (because by this time,
79 // everything that could have run on |backend_thread_| has done so).
80 ASSERT_TRUE(did_connect_
);
83 // Connect |shared_change_processor_| on the DB thread.
85 EXPECT_TRUE(backend_thread_
.message_loop_proxy()->PostTask(
87 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread
,
88 base::Unretained(this),
89 shared_change_processor_
)));
94 void SetUpDBSyncableService() {
95 DCHECK(backend_thread_
.message_loop_proxy()->BelongsToCurrentThread());
96 DCHECK(!db_syncable_service_
.get());
97 db_syncable_service_
.reset(new syncer::FakeSyncableService());
100 // Used by TearDown().
101 void TearDownDBSyncableService() {
102 DCHECK(backend_thread_
.message_loop_proxy()->BelongsToCurrentThread());
103 DCHECK(db_syncable_service_
.get());
104 db_syncable_service_
.reset();
107 // Used by Connect(). The SharedChangeProcessor is passed in
108 // because we modify |shared_change_processor_| on the main thread
110 void ConnectOnDBThread(
111 const scoped_refptr
<SharedChangeProcessor
>& shared_change_processor
) {
112 DCHECK(backend_thread_
.message_loop_proxy()->BelongsToCurrentThread());
113 syncer::UserShare share
;
114 EXPECT_TRUE(shared_change_processor
->Connect(
120 base::WeakPtr
<syncer::SyncMergeResult
>()));
124 base::MessageLoop frontend_loop_
;
125 base::Thread backend_thread_
;
127 scoped_refptr
<SharedChangeProcessor
> shared_change_processor_
;
128 StrictMock
<DataTypeErrorHandlerMock
> error_handler_
;
130 GenericChangeProcessorFactory processor_factory_
;
133 // Used only on DB thread.
134 scoped_ptr
<syncer::FakeSyncableService
> db_syncable_service_
;
137 // Simply connect the shared change processor. It should succeed, and
138 // nothing further should happen.
139 TEST_F(SyncSharedChangeProcessorTest
, Basic
) {
145 } // namespace sync_driver