Remove dependency on //chrome from bookmark_undo_utils
[chromium-blink-merge.git] / components / sync_driver / shared_change_processor_unittest.cc
blobd65fb54d528d26d19be24025aaaf2d8806157e67
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"
7 #include <cstddef>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/thread.h"
15 #include "components/sync_driver/data_type_error_handler_mock.h"
16 #include "components/sync_driver/generic_change_processor.h"
17 #include "components/sync_driver/generic_change_processor_factory.h"
18 #include "components/sync_driver/sync_api_component_factory.h"
19 #include "sync/api/attachments/attachment_id.h"
20 #include "sync/api/attachments/attachment_store.h"
21 #include "sync/api/fake_syncable_service.h"
22 #include "sync/internal_api/public/attachments/attachment_service_impl.h"
23 #include "sync/internal_api/public/base/model_type.h"
24 #include "sync/internal_api/public/test/test_user_share.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 namespace sync_driver {
30 namespace {
32 using ::testing::NiceMock;
33 using ::testing::StrictMock;
35 class SyncSharedChangeProcessorTest :
36 public testing::Test,
37 public SyncApiComponentFactory {
38 public:
39 SyncSharedChangeProcessorTest()
40 : backend_thread_("dbthread"),
41 did_connect_(false),
42 has_attachment_service_(false) {}
44 virtual ~SyncSharedChangeProcessorTest() {
45 EXPECT_FALSE(db_syncable_service_.get());
48 virtual base::WeakPtr<syncer::SyncableService> GetSyncableServiceForType(
49 syncer::ModelType type) override {
50 return db_syncable_service_->AsWeakPtr();
53 virtual scoped_ptr<syncer::AttachmentService> CreateAttachmentService(
54 scoped_ptr<syncer::AttachmentStoreForSync> attachment_store,
55 const syncer::UserShare& user_share,
56 const std::string& store_birthday,
57 syncer::ModelType model_type,
58 syncer::AttachmentService::Delegate* delegate) override {
59 return syncer::AttachmentServiceImpl::CreateForTest();
62 protected:
63 virtual void SetUp() override {
64 test_user_share_.SetUp();
65 shared_change_processor_ = new SharedChangeProcessor();
66 ASSERT_TRUE(backend_thread_.Start());
67 ASSERT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
68 FROM_HERE,
69 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
70 base::Unretained(this))));
73 virtual void TearDown() override {
74 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
75 FROM_HERE,
76 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService,
77 base::Unretained(this))));
78 // This must happen before the DB thread is stopped since
79 // |shared_change_processor_| may post tasks to delete its members
80 // on the correct thread.
82 // TODO(akalin): Write deterministic tests for the destruction of
83 // |shared_change_processor_| on the UI and DB threads.
84 shared_change_processor_ = NULL;
85 backend_thread_.Stop();
87 // Note: Stop() joins the threads, and that barrier prevents this read
88 // from being moved (e.g by compiler optimization) in such a way that it
89 // would race with the write in ConnectOnDBThread (because by this time,
90 // everything that could have run on |backend_thread_| has done so).
91 ASSERT_TRUE(did_connect_);
92 test_user_share_.TearDown();
95 // Connect |shared_change_processor_| on the DB thread.
96 void Connect() {
97 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
98 FROM_HERE,
99 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
100 base::Unretained(this),
101 shared_change_processor_)));
104 void SetAttachmentStore() {
105 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
106 FROM_HERE,
107 base::Bind(&SyncSharedChangeProcessorTest::SetAttachmentStoreOnDBThread,
108 base::Unretained(this))));
111 bool HasAttachmentService() {
112 base::WaitableEvent event(false, false);
113 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
114 FROM_HERE,
115 base::Bind(
116 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread,
117 base::Unretained(this), base::Unretained(&event))));
118 event.Wait();
119 return has_attachment_service_;
122 private:
123 // Used by SetUp().
124 void SetUpDBSyncableService() {
125 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
126 DCHECK(!db_syncable_service_.get());
127 db_syncable_service_.reset(new syncer::FakeSyncableService());
130 // Used by TearDown().
131 void TearDownDBSyncableService() {
132 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
133 DCHECK(db_syncable_service_.get());
134 db_syncable_service_.reset();
137 void SetAttachmentStoreOnDBThread() {
138 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
139 DCHECK(db_syncable_service_.get());
140 db_syncable_service_->set_attachment_store(
141 syncer::AttachmentStore::CreateInMemoryStore());
144 // Used by Connect(). The SharedChangeProcessor is passed in
145 // because we modify |shared_change_processor_| on the main thread
146 // (in TearDown()).
147 void ConnectOnDBThread(
148 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
149 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
150 EXPECT_TRUE(shared_change_processor->Connect(
151 this, &processor_factory_, test_user_share_.user_share(),
152 &error_handler_, syncer::AUTOFILL,
153 base::WeakPtr<syncer::SyncMergeResult>()));
154 did_connect_ = true;
157 void CheckAttachmentServiceOnDBThread(base::WaitableEvent* event) {
158 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
159 DCHECK(db_syncable_service_.get());
160 has_attachment_service_ = !!db_syncable_service_->attachment_service();
161 event->Signal();
164 base::MessageLoop frontend_loop_;
165 base::Thread backend_thread_;
166 syncer::TestUserShare test_user_share_;
168 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
169 StrictMock<DataTypeErrorHandlerMock> error_handler_;
171 GenericChangeProcessorFactory processor_factory_;
172 bool did_connect_;
173 bool has_attachment_service_;
175 // Used only on DB thread.
176 scoped_ptr<syncer::FakeSyncableService> db_syncable_service_;
179 // Simply connect the shared change processor. It should succeed, and
180 // nothing further should happen.
181 TEST_F(SyncSharedChangeProcessorTest, Basic) {
182 Connect();
185 // Connect the shared change processor to a syncable service with
186 // AttachmentStore. Verify that shared change processor implementation
187 // creates AttachmentService and passes it back to the syncable service.
188 TEST_F(SyncSharedChangeProcessorTest, ConnectWithAttachmentStore) {
189 SetAttachmentStore();
190 Connect();
191 EXPECT_TRUE(HasAttachmentService());
194 } // namespace
196 } // namespace sync_driver