Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / drive_backend / sync_engine_unittest.cc
blobff6d5df28de6af638df6d28306b4afdf06fb8270
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/run_loop.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/drive/drive_uploader.h"
11 #include "chrome/browser/drive/fake_drive_service.h"
12 #include "chrome/browser/extensions/test_extension_service.h"
13 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
14 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h"
15 #include "chrome/browser/sync_file_system/sync_file_system_test_util.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/extension_builder.h"
19 #include "extensions/common/extension_set.h"
20 #include "extensions/common/value_builder.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace sync_file_system {
24 namespace drive_backend {
26 namespace {
28 const char kAppID[] = "app_id";
30 void EmptyTask(SyncStatusCode status, const SyncStatusCallback& callback) {
31 base::MessageLoop::current()->PostTask(
32 FROM_HERE, base::Bind(callback, status));
35 } // namespace
37 class MockSyncTask : public SyncTask {
38 public:
39 explicit MockSyncTask(bool used_network) {
40 set_used_network(used_network);
42 virtual ~MockSyncTask() {}
44 virtual void Run(const SyncStatusCallback& callback) OVERRIDE {
45 callback.Run(SYNC_STATUS_OK);
48 private:
49 DISALLOW_COPY_AND_ASSIGN(MockSyncTask);
52 class MockExtensionService : public TestExtensionService {
53 public:
54 MockExtensionService() {}
55 virtual ~MockExtensionService() {}
57 virtual const extensions::ExtensionSet* extensions() const OVERRIDE {
58 return &extensions_;
61 virtual const extensions::ExtensionSet* disabled_extensions() const OVERRIDE {
62 return &disabled_extensions_;
65 virtual void AddExtension(const extensions::Extension* extension) OVERRIDE {
66 extensions_.Insert(make_scoped_refptr(extension));
69 virtual const extensions::Extension* GetInstalledExtension(
70 const std::string& extension_id) const OVERRIDE {
71 return extensions_.GetByID(extension_id);
74 virtual bool IsExtensionEnabled(
75 const std::string& extension_id) const OVERRIDE {
76 return extensions_.Contains(extension_id) &&
77 !disabled_extensions_.Contains(extension_id);
80 void UninstallExtension(const std::string& extension_id) {
81 extensions_.Remove(extension_id);
82 disabled_extensions_.Remove(extension_id);
85 void DisableExtension(const std::string& extension_id) {
86 if (!IsExtensionEnabled(extension_id))
87 return;
88 const extensions::Extension* extension = extensions_.GetByID(extension_id);
89 disabled_extensions_.Insert(make_scoped_refptr(extension));
92 private:
93 extensions::ExtensionSet extensions_;
94 extensions::ExtensionSet disabled_extensions_;
96 DISALLOW_COPY_AND_ASSIGN(MockExtensionService);
99 class SyncEngineTest
100 : public testing::Test,
101 public base::SupportsWeakPtr<SyncEngineTest> {
102 public:
103 SyncEngineTest() {}
104 virtual ~SyncEngineTest() {}
106 virtual void SetUp() OVERRIDE {
107 ASSERT_TRUE(profile_dir_.CreateUniqueTempDir());
108 extension_service_.reset(new MockExtensionService);
109 scoped_ptr<drive::FakeDriveService> fake_drive_service(
110 new drive::FakeDriveService);
112 ASSERT_TRUE(fake_drive_service->LoadAccountMetadataForWapi(
113 "sync_file_system/account_metadata.json"));
114 ASSERT_TRUE(fake_drive_service->LoadResourceListForWapi(
115 "gdata/empty_feed.json"));
116 sync_engine_.reset(new drive_backend::SyncEngine(
117 profile_dir_.path(),
118 base::MessageLoopProxy::current(),
119 fake_drive_service.PassAs<drive::DriveServiceInterface>(),
120 scoped_ptr<drive::DriveUploaderInterface>(),
121 NULL /* notification_manager */,
122 extension_service_.get(),
123 NULL /* auth_token_service */));
124 sync_engine_->Initialize();
125 base::RunLoop().RunUntilIdle();
128 virtual void TearDown() OVERRIDE {
129 sync_engine_.reset();
130 extension_service_.reset();
131 base::RunLoop().RunUntilIdle();
134 MockExtensionService* extension_service() { return extension_service_.get(); }
135 SyncEngine* sync_engine() { return sync_engine_.get(); }
137 void UpdateRegisteredApps() {
138 sync_engine_->UpdateRegisteredApps();
141 SyncTaskManager* GetSyncEngineTaskManager() {
142 return sync_engine_->task_manager_.get();
145 void CheckServiceState(SyncStatusCode expected_sync_status,
146 RemoteServiceState expected_service_status,
147 SyncStatusCode sync_status) {
148 EXPECT_EQ(expected_sync_status, sync_status);
149 EXPECT_EQ(expected_service_status, sync_engine_->GetCurrentState());
152 private:
153 content::TestBrowserThreadBundle browser_threads_;
154 base::ScopedTempDir profile_dir_;
156 scoped_ptr<MockExtensionService> extension_service_;
157 scoped_ptr<drive_backend::SyncEngine> sync_engine_;
159 DISALLOW_COPY_AND_ASSIGN(SyncEngineTest);
162 TEST_F(SyncEngineTest, EnableOrigin) {
163 FileTracker tracker;
164 SyncStatusCode sync_status = SYNC_STATUS_UNKNOWN;
165 MetadataDatabase* metadata_database = sync_engine()->GetMetadataDatabase();
166 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(kAppID);
168 sync_engine()->RegisterOrigin(origin, CreateResultReceiver(&sync_status));
169 base::RunLoop().RunUntilIdle();
170 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
171 ASSERT_TRUE(metadata_database->FindAppRootTracker(kAppID, &tracker));
172 EXPECT_EQ(TRACKER_KIND_APP_ROOT, tracker.tracker_kind());
174 sync_engine()->DisableOrigin(origin, CreateResultReceiver(&sync_status));
175 base::RunLoop().RunUntilIdle();
176 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
177 ASSERT_TRUE(metadata_database->FindAppRootTracker(kAppID, &tracker));
178 EXPECT_EQ(TRACKER_KIND_DISABLED_APP_ROOT, tracker.tracker_kind());
180 sync_engine()->EnableOrigin(origin, CreateResultReceiver(&sync_status));
181 base::RunLoop().RunUntilIdle();
182 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
183 ASSERT_TRUE(metadata_database->FindAppRootTracker(kAppID, &tracker));
184 EXPECT_EQ(TRACKER_KIND_APP_ROOT, tracker.tracker_kind());
186 sync_engine()->UninstallOrigin(
187 origin,
188 RemoteFileSyncService::UNINSTALL_AND_KEEP_REMOTE,
189 CreateResultReceiver(&sync_status));
190 base::RunLoop().RunUntilIdle();
191 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
192 ASSERT_FALSE(metadata_database->FindAppRootTracker(kAppID, &tracker));
195 TEST_F(SyncEngineTest, UpdateRegisteredApps) {
196 SyncStatusCode sync_status = SYNC_STATUS_UNKNOWN;
197 for (int i = 0; i < 3; i++) {
198 scoped_refptr<const extensions::Extension> extension =
199 extensions::ExtensionBuilder()
200 .SetManifest(extensions::DictionaryBuilder()
201 .Set("name", "foo")
202 .Set("version", "1.0")
203 .Set("manifest_version", 2))
204 .SetID(base::StringPrintf("app_%d", i))
205 .Build();
206 extension_service()->AddExtension(extension.get());
207 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(
208 extension->id());
209 sync_status = SYNC_STATUS_UNKNOWN;
210 sync_engine()->RegisterOrigin(origin, CreateResultReceiver(&sync_status));
211 base::RunLoop().RunUntilIdle();
212 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
215 MetadataDatabase* metadata_database = sync_engine()->GetMetadataDatabase();
216 FileTracker tracker;
218 ASSERT_TRUE(metadata_database->FindAppRootTracker("app_0", &tracker));
219 EXPECT_EQ(TRACKER_KIND_APP_ROOT, tracker.tracker_kind());
221 ASSERT_TRUE(metadata_database->FindAppRootTracker("app_1", &tracker));
222 EXPECT_EQ(TRACKER_KIND_APP_ROOT, tracker.tracker_kind());
224 ASSERT_TRUE(metadata_database->FindAppRootTracker("app_2", &tracker));
225 EXPECT_EQ(TRACKER_KIND_APP_ROOT, tracker.tracker_kind());
227 extension_service()->DisableExtension("app_1");
228 extension_service()->UninstallExtension("app_2");
229 ASSERT_FALSE(extension_service()->GetInstalledExtension("app_2"));
230 UpdateRegisteredApps();
231 base::RunLoop().RunUntilIdle();
233 ASSERT_TRUE(metadata_database->FindAppRootTracker("app_0", &tracker));
234 EXPECT_EQ(TRACKER_KIND_APP_ROOT, tracker.tracker_kind());
236 ASSERT_TRUE(metadata_database->FindAppRootTracker("app_1", &tracker));
237 EXPECT_EQ(TRACKER_KIND_DISABLED_APP_ROOT, tracker.tracker_kind());
239 ASSERT_FALSE(metadata_database->FindAppRootTracker("app_2", &tracker));
242 TEST_F(SyncEngineTest, GetOriginStatusMap) {
243 FileTracker tracker;
244 SyncStatusCode sync_status = SYNC_STATUS_UNKNOWN;
245 GURL origin = extensions::Extension::GetBaseURLFromExtensionId(kAppID);
247 sync_engine()->RegisterOrigin(GURL("chrome-extension://app_0"),
248 CreateResultReceiver(&sync_status));
249 base::RunLoop().RunUntilIdle();
250 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
252 sync_engine()->RegisterOrigin(GURL("chrome-extension://app_1"),
253 CreateResultReceiver(&sync_status));
254 base::RunLoop().RunUntilIdle();
255 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
257 RemoteFileSyncService::OriginStatusMap status_map;
258 sync_engine()->GetOriginStatusMap(&status_map);
259 ASSERT_EQ(2u, status_map.size());
260 EXPECT_EQ("Enabled", status_map[GURL("chrome-extension://app_0")]);
261 EXPECT_EQ("Enabled", status_map[GURL("chrome-extension://app_1")]);
263 sync_engine()->DisableOrigin(GURL("chrome-extension://app_1"),
264 CreateResultReceiver(&sync_status));
265 base::RunLoop().RunUntilIdle();
266 EXPECT_EQ(SYNC_STATUS_OK, sync_status);
268 sync_engine()->GetOriginStatusMap(&status_map);
269 ASSERT_EQ(2u, status_map.size());
270 EXPECT_EQ("Enabled", status_map[GURL("chrome-extension://app_0")]);
271 EXPECT_EQ("Disabled", status_map[GURL("chrome-extension://app_1")]);
274 TEST_F(SyncEngineTest, UpdateServiceState) {
275 EXPECT_EQ(REMOTE_SERVICE_OK, sync_engine()->GetCurrentState());
277 GetSyncEngineTaskManager()->ScheduleTask(
278 base::Bind(&EmptyTask, SYNC_STATUS_AUTHENTICATION_FAILED),
279 base::Bind(&SyncEngineTest::CheckServiceState,
280 AsWeakPtr(),
281 SYNC_STATUS_AUTHENTICATION_FAILED,
282 REMOTE_SERVICE_AUTHENTICATION_REQUIRED));
284 GetSyncEngineTaskManager()->ScheduleTask(
285 base::Bind(&EmptyTask, SYNC_STATUS_ACCESS_FORBIDDEN),
286 base::Bind(&SyncEngineTest::CheckServiceState,
287 AsWeakPtr(),
288 SYNC_STATUS_ACCESS_FORBIDDEN,
289 REMOTE_SERVICE_AUTHENTICATION_REQUIRED));
291 GetSyncEngineTaskManager()->ScheduleTask(
292 base::Bind(&EmptyTask, SYNC_STATUS_SERVICE_TEMPORARILY_UNAVAILABLE),
293 base::Bind(&SyncEngineTest::CheckServiceState,
294 AsWeakPtr(),
295 SYNC_STATUS_SERVICE_TEMPORARILY_UNAVAILABLE,
296 REMOTE_SERVICE_TEMPORARY_UNAVAILABLE));
298 GetSyncEngineTaskManager()->ScheduleTask(
299 base::Bind(&EmptyTask, SYNC_STATUS_NETWORK_ERROR),
300 base::Bind(&SyncEngineTest::CheckServiceState,
301 AsWeakPtr(),
302 SYNC_STATUS_NETWORK_ERROR,
303 REMOTE_SERVICE_TEMPORARY_UNAVAILABLE));
305 GetSyncEngineTaskManager()->ScheduleTask(
306 base::Bind(&EmptyTask, SYNC_STATUS_ABORT),
307 base::Bind(&SyncEngineTest::CheckServiceState,
308 AsWeakPtr(),
309 SYNC_STATUS_ABORT,
310 REMOTE_SERVICE_TEMPORARY_UNAVAILABLE));
312 GetSyncEngineTaskManager()->ScheduleTask(
313 base::Bind(&EmptyTask, SYNC_STATUS_FAILED),
314 base::Bind(&SyncEngineTest::CheckServiceState,
315 AsWeakPtr(),
316 SYNC_STATUS_FAILED,
317 REMOTE_SERVICE_TEMPORARY_UNAVAILABLE));
319 GetSyncEngineTaskManager()->ScheduleTask(
320 base::Bind(&EmptyTask, SYNC_DATABASE_ERROR_CORRUPTION),
321 base::Bind(&SyncEngineTest::CheckServiceState,
322 AsWeakPtr(),
323 SYNC_DATABASE_ERROR_CORRUPTION,
324 REMOTE_SERVICE_DISABLED));
326 GetSyncEngineTaskManager()->ScheduleTask(
327 base::Bind(&EmptyTask, SYNC_DATABASE_ERROR_IO_ERROR),
328 base::Bind(&SyncEngineTest::CheckServiceState,
329 AsWeakPtr(),
330 SYNC_DATABASE_ERROR_IO_ERROR,
331 REMOTE_SERVICE_DISABLED));
333 GetSyncEngineTaskManager()->ScheduleTask(
334 base::Bind(&EmptyTask, SYNC_DATABASE_ERROR_FAILED),
335 base::Bind(&SyncEngineTest::CheckServiceState,
336 AsWeakPtr(),
337 SYNC_DATABASE_ERROR_FAILED,
338 REMOTE_SERVICE_DISABLED));
340 GetSyncEngineTaskManager()->ScheduleSyncTask(
341 scoped_ptr<SyncTask>(new MockSyncTask(false)),
342 base::Bind(&SyncEngineTest::CheckServiceState,
343 AsWeakPtr(),
344 SYNC_STATUS_OK,
345 REMOTE_SERVICE_DISABLED));
347 GetSyncEngineTaskManager()->ScheduleSyncTask(
348 scoped_ptr<SyncTask>(new MockSyncTask(true)),
349 base::Bind(&SyncEngineTest::CheckServiceState,
350 AsWeakPtr(),
351 SYNC_STATUS_OK,
352 REMOTE_SERVICE_OK));
354 base::RunLoop().RunUntilIdle();
357 } // namespace drive_backend
358 } // namespace sync_file_system