Ensure low-memory renderers retry failed loads correctly.
[chromium-blink-merge.git] / components / sync_driver / shared_change_processor_unittest.cc
blobb97ea3616ca47573aed5d08b54ab8c8eb9f7a278
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/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 {
33 namespace {
35 using ::testing::NiceMock;
36 using ::testing::StrictMock;
38 class TestSyncApiComponentFactory : public SyncApiComponentFactory {
39 public:
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>&
48 debug_info_listener,
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 {
53 return nullptr;
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 {
60 return nullptr;
62 scoped_ptr<sync_driver::LocalDeviceInfoProvider>
63 CreateLocalDeviceInfoProvider() override {
64 return nullptr;
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 :
88 public testing::Test,
89 public FakeSyncClient {
90 public:
91 SyncSharedChangeProcessorTest()
92 : FakeSyncClient(&factory_),
93 backend_thread_("dbthread"),
94 did_connect_(false),
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();
107 protected:
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(
113 FROM_HERE,
114 base::Bind(&SyncSharedChangeProcessorTest::SetUpDBSyncableService,
115 base::Unretained(this))));
118 void TearDown() override {
119 EXPECT_TRUE(backend_thread_.task_runner()->PostTask(
120 FROM_HERE,
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.
141 void Connect() {
142 EXPECT_TRUE(backend_thread_.task_runner()->PostTask(
143 FROM_HERE,
144 base::Bind(&SyncSharedChangeProcessorTest::ConnectOnDBThread,
145 base::Unretained(this), shared_change_processor_)));
148 void SetAttachmentStore() {
149 EXPECT_TRUE(backend_thread_.task_runner()->PostTask(
150 FROM_HERE,
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(
158 FROM_HERE,
159 base::Bind(
160 &SyncSharedChangeProcessorTest::CheckAttachmentServiceOnDBThread,
161 base::Unretained(this), base::Unretained(&event))));
162 event.Wait();
163 return has_attachment_service_;
166 private:
167 // Used by SetUp().
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
190 // (in TearDown()).
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>()));
198 did_connect_ = true;
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();
205 event->Signal();
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_;
217 bool did_connect_;
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) {
227 Connect();
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();
235 Connect();
236 EXPECT_TRUE(HasAttachmentService());
239 } // namespace
241 } // namespace sync_driver