1 // Copyright 2013 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 "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/macros.h"
9 #include "base/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/sync_file_system/drive_backend/callback_helper.h"
12 #include "chrome/browser/sync_file_system/drive_backend/fake_sync_worker.h"
13 #include "chrome/browser/sync_file_system/drive_backend/sync_worker_interface.h"
14 #include "chrome/browser/sync_file_system/remote_file_sync_service.h"
15 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
16 #include "components/drive/drive_uploader.h"
17 #include "components/drive/service/fake_drive_service.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace sync_file_system
{
24 namespace drive_backend
{
26 class SyncEngineTest
: public testing::Test
,
27 public base::SupportsWeakPtr
<SyncEngineTest
> {
29 typedef RemoteFileSyncService::OriginStatusMap RemoteOriginStatusMap
;
32 ~SyncEngineTest() override
{}
34 void SetUp() override
{
35 ASSERT_TRUE(profile_dir_
.CreateUniqueTempDir());
37 scoped_ptr
<drive::DriveServiceInterface
>
38 fake_drive_service(new drive::FakeDriveService
);
40 worker_pool_
= new base::SequencedWorkerPool(1, "Worker");
41 scoped_refptr
<base::SingleThreadTaskRunner
> ui_task_runner
=
42 base::ThreadTaskRunnerHandle::Get();
44 worker_pool_
->GetSequencedTaskRunnerWithShutdownBehavior(
45 worker_pool_
->GetSequenceToken(),
46 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
48 sync_engine_
.reset(new drive_backend::SyncEngine(
50 worker_task_runner_
.get(),
51 nullptr /* drive_task_runner */,
54 nullptr /* task_logger */,
55 nullptr /* notification_manager */,
56 nullptr /* extension_service */,
57 nullptr /* signin_manager */,
58 nullptr /* token_service */,
59 nullptr /* request_context */,
60 nullptr /* drive_service_factory */,
61 nullptr /* in_memory_env */));
63 sync_engine_
->InitializeForTesting(
64 fake_drive_service
.Pass(),
65 nullptr, // drive_uploader
66 scoped_ptr
<SyncWorkerInterface
>(new FakeSyncWorker
));
67 sync_engine_
->SetSyncEnabled(true);
68 sync_engine_
->OnReadyToSendRequests();
70 WaitForWorkerTaskRunner();
73 void TearDown() override
{
75 WaitForWorkerTaskRunner();
76 worker_pool_
->Shutdown();
78 worker_task_runner_
= nullptr;
79 worker_pool_
= nullptr;
81 base::RunLoop().RunUntilIdle();
84 bool FindOriginStatus(const GURL
& origin
, std::string
* status
) {
85 scoped_ptr
<RemoteOriginStatusMap
> status_map
;
86 sync_engine()->GetOriginStatusMap(CreateResultReceiver(&status_map
));
87 WaitForWorkerTaskRunner();
89 RemoteOriginStatusMap::const_iterator itr
= status_map
->find(origin
);
90 if (itr
== status_map
->end())
93 *status
= itr
->second
;
94 // If an origin is uninstalled, it should not be found actually.
95 if (*status
== "Uninstalled")
100 void PostUpdateServiceState(RemoteServiceState state
,
101 const std::string
& description
) {
102 worker_task_runner_
->PostTask(
104 base::Bind(&FakeSyncWorker::UpdateServiceState
,
105 base::Unretained(fake_sync_worker()),
108 WaitForWorkerTaskRunner();
111 void WaitForWorkerTaskRunner() {
112 DCHECK(worker_task_runner_
.get());
114 base::RunLoop run_loop
;
115 worker_task_runner_
->PostTask(
117 RelayCallbackToCurrentThread(
118 FROM_HERE
, run_loop
.QuitClosure()));
123 SyncEngine
* sync_engine() { return sync_engine_
.get(); }
125 FakeSyncWorker
* fake_sync_worker() {
126 return static_cast<FakeSyncWorker
*>(sync_engine_
->sync_worker_
.get());
130 content::TestBrowserThreadBundle browser_threads_
;
131 base::ScopedTempDir profile_dir_
;
132 scoped_ptr
<drive_backend::SyncEngine
> sync_engine_
;
134 scoped_refptr
<base::SequencedWorkerPool
> worker_pool_
;
135 scoped_refptr
<base::SequencedTaskRunner
> worker_task_runner_
;
137 DISALLOW_COPY_AND_ASSIGN(SyncEngineTest
);
140 TEST_F(SyncEngineTest
, OriginTest
) {
141 GURL
origin("chrome-extension://app_0");
143 SyncStatusCode sync_status
;
146 sync_engine()->RegisterOrigin(
148 CreateResultReceiver(&sync_status
));
149 WaitForWorkerTaskRunner();
150 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
151 ASSERT_TRUE(FindOriginStatus(origin
, &status
));
152 EXPECT_EQ("Registered", status
);
154 sync_engine()->DisableOrigin(
156 CreateResultReceiver(&sync_status
));
157 WaitForWorkerTaskRunner();
158 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
159 ASSERT_TRUE(FindOriginStatus(origin
, &status
));
160 EXPECT_EQ("Disabled", status
);
162 sync_engine()->EnableOrigin(
164 CreateResultReceiver(&sync_status
));
165 WaitForWorkerTaskRunner();
166 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
167 ASSERT_TRUE(FindOriginStatus(origin
, &status
));
168 EXPECT_EQ("Enabled", status
);
170 sync_engine()->UninstallOrigin(
172 RemoteFileSyncService::UNINSTALL_AND_KEEP_REMOTE
,
173 CreateResultReceiver(&sync_status
));
174 WaitForWorkerTaskRunner();
175 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
176 EXPECT_FALSE(FindOriginStatus(origin
, &status
));
177 EXPECT_EQ("Uninstalled", status
);
180 TEST_F(SyncEngineTest
, GetOriginStatusMap
) {
181 SyncStatusCode sync_status
= SYNC_STATUS_UNKNOWN
;
183 sync_engine()->RegisterOrigin(GURL("chrome-extension://app_0"),
184 CreateResultReceiver(&sync_status
));
185 WaitForWorkerTaskRunner();
186 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
188 sync_engine()->RegisterOrigin(GURL("chrome-extension://app_1"),
189 CreateResultReceiver(&sync_status
));
190 WaitForWorkerTaskRunner();
191 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
193 scoped_ptr
<RemoteOriginStatusMap
> status_map
;
194 sync_engine()->GetOriginStatusMap(CreateResultReceiver(&status_map
));
195 WaitForWorkerTaskRunner();
196 ASSERT_EQ(2u, status_map
->size());
197 EXPECT_EQ("Registered", (*status_map
)[GURL("chrome-extension://app_0")]);
198 EXPECT_EQ("Registered", (*status_map
)[GURL("chrome-extension://app_1")]);
200 sync_engine()->DisableOrigin(GURL("chrome-extension://app_1"),
201 CreateResultReceiver(&sync_status
));
202 WaitForWorkerTaskRunner();
203 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
205 sync_engine()->GetOriginStatusMap(CreateResultReceiver(&status_map
));
206 WaitForWorkerTaskRunner();
207 ASSERT_EQ(2u, status_map
->size());
208 EXPECT_EQ("Registered", (*status_map
)[GURL("chrome-extension://app_0")]);
209 EXPECT_EQ("Disabled", (*status_map
)[GURL("chrome-extension://app_1")]);
212 TEST_F(SyncEngineTest
, UpdateServiceState
) {
214 RemoteServiceState state
;
215 const char* description
;
217 {REMOTE_SERVICE_OK
, "OK"},
218 {REMOTE_SERVICE_TEMPORARY_UNAVAILABLE
, "TEMPORARY_UNAVAILABLE"},
219 {REMOTE_SERVICE_AUTHENTICATION_REQUIRED
, "AUTHENTICATION_REQUIRED"},
220 {REMOTE_SERVICE_DISABLED
, "DISABLED"},
223 for (size_t i
= 0; i
< arraysize(test_data
); ++i
) {
224 PostUpdateServiceState(test_data
[i
].state
, test_data
[i
].description
);
225 EXPECT_EQ(test_data
[i
].state
, sync_engine()->GetCurrentState())
226 << "Expected state: REMOTE_SERVICE_" << test_data
[i
].description
;
230 TEST_F(SyncEngineTest
, ProcessRemoteChange
) {
231 SyncStatusCode sync_status
;
232 storage::FileSystemURL url
;
233 sync_engine()->ProcessRemoteChange(CreateResultReceiver(&sync_status
, &url
));
234 WaitForWorkerTaskRunner();
235 EXPECT_EQ(SYNC_STATUS_OK
, sync_status
);
238 } // namespace drive_backend
239 } // namespace sync_file_system