Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / components / sync_driver / shared_change_processor_unittest.cc
blob1b50458dd9960f48506d33d1613650fe5027a273
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/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 {
25 namespace {
27 using ::testing::NiceMock;
28 using ::testing::StrictMock;
30 class SyncSharedChangeProcessorTest :
31 public testing::Test,
32 public SyncApiComponentFactory {
33 public:
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 scoped_refptr<syncer::AttachmentStore>& attachment_store,
48 const syncer::UserShare& user_share,
49 const std::string& store_birthday,
50 syncer::AttachmentService::Delegate* delegate) override {
51 return syncer::AttachmentServiceImpl::CreateForTest();
54 protected:
55 virtual void SetUp() override {
56 shared_change_processor_ = new SharedChangeProcessor();
57 ASSERT_TRUE(backend_thread_.Start());
58 ASSERT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
59 FROM_HERE,
60 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
61 base::Unretained(this))));
64 virtual void TearDown() override {
65 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
66 FROM_HERE,
67 base::Bind(&SyncSharedChangeProcessorTest::TearDownDBSyncableService,
68 base::Unretained(this))));
69 // This must happen before the DB thread is stopped since
70 // |shared_change_processor_| may post tasks to delete its members
71 // on the correct thread.
73 // TODO(akalin): Write deterministic tests for the destruction of
74 // |shared_change_processor_| on the UI and DB threads.
75 shared_change_processor_ = NULL;
76 backend_thread_.Stop();
78 // Note: Stop() joins the threads, and that barrier prevents this read
79 // from being moved (e.g by compiler optimization) in such a way that it
80 // would race with the write in ConnectOnDBThread (because by this time,
81 // everything that could have run on |backend_thread_| has done so).
82 ASSERT_TRUE(did_connect_);
85 // Connect |shared_change_processor_| on the DB thread.
86 void Connect() {
87 EXPECT_TRUE(backend_thread_.message_loop_proxy()->PostTask(
88 FROM_HERE,
89 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
90 base::Unretained(this),
91 shared_change_processor_)));
94 private:
95 // Used by SetUp().
96 void SetUpDBSyncableService() {
97 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
98 DCHECK(!db_syncable_service_.get());
99 db_syncable_service_.reset(new syncer::FakeSyncableService());
102 // Used by TearDown().
103 void TearDownDBSyncableService() {
104 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
105 DCHECK(db_syncable_service_.get());
106 db_syncable_service_.reset();
109 // Used by Connect(). The SharedChangeProcessor is passed in
110 // because we modify |shared_change_processor_| on the main thread
111 // (in TearDown()).
112 void ConnectOnDBThread(
113 const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
114 DCHECK(backend_thread_.message_loop_proxy()->BelongsToCurrentThread());
115 syncer::UserShare share;
116 EXPECT_TRUE(shared_change_processor->Connect(
117 this,
118 &processor_factory_,
119 &share,
120 &error_handler_,
121 syncer::AUTOFILL,
122 base::WeakPtr<syncer::SyncMergeResult>()));
123 did_connect_ = true;
126 base::MessageLoop frontend_loop_;
127 base::Thread backend_thread_;
129 scoped_refptr<SharedChangeProcessor> shared_change_processor_;
130 StrictMock<DataTypeErrorHandlerMock> error_handler_;
132 GenericChangeProcessorFactory processor_factory_;
133 bool did_connect_;
135 // Used only on DB thread.
136 scoped_ptr<syncer::FakeSyncableService> db_syncable_service_;
139 // Simply connect the shared change processor. It should succeed, and
140 // nothing further should happen.
141 TEST_F(SyncSharedChangeProcessorTest, Basic) {
142 Connect();
145 } // namespace
147 } // namespace sync_driver