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 "base/json/json_reader.h"
6 #include "base/run_loop.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "base/values.h"
9 #include "chrome/browser/apps/app_browsertest_util.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/signin/fake_signin_manager_builder.h"
12 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h"
13 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h"
14 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
15 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
16 #include "components/drive/service/fake_drive_service.h"
17 #include "extensions/test/extension_test_message_listener.h"
18 #include "extensions/test/result_catcher.h"
19 #include "storage/browser/quota/quota_manager.h"
20 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
21 #include "third_party/leveldatabase/src/include/leveldb/env.h"
23 namespace sync_file_system
{
27 class FakeDriveServiceFactory
28 : public drive_backend::SyncEngine::DriveServiceFactory
{
30 explicit FakeDriveServiceFactory(
31 drive::FakeDriveService::ChangeObserver
* change_observer
)
32 : change_observer_(change_observer
) {}
33 ~FakeDriveServiceFactory() override
{}
35 scoped_ptr
<drive::DriveServiceInterface
> CreateDriveService(
36 OAuth2TokenService
* oauth2_token_service
,
37 net::URLRequestContextGetter
* url_request_context_getter
,
38 base::SequencedTaskRunner
* blocking_task_runner
) override
{
39 scoped_ptr
<drive::FakeDriveService
> drive_service(
40 new drive::FakeDriveService
);
41 drive_service
->AddChangeObserver(change_observer_
);
42 return drive_service
.Pass();
46 drive::FakeDriveService::ChangeObserver
* change_observer_
;
48 DISALLOW_COPY_AND_ASSIGN(FakeDriveServiceFactory
);
53 class SyncFileSystemTest
: public extensions::PlatformAppBrowserTest
,
54 public drive::FakeDriveService::ChangeObserver
{
57 : fake_drive_service_(NULL
),
59 remote_service_(NULL
) {
62 void SetUpInProcessBrowserTestFixture() override
{
63 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
64 real_minimum_preserved_space_
=
65 storage::QuotaManager::kMinimumPreserveForSystem
;
66 storage::QuotaManager::kMinimumPreserveForSystem
= 0;
69 void TearDownInProcessBrowserTestFixture() override
{
70 storage::QuotaManager::kMinimumPreserveForSystem
=
71 real_minimum_preserved_space_
;
72 ExtensionApiTest::TearDownInProcessBrowserTestFixture();
75 scoped_refptr
<base::SequencedTaskRunner
> MakeSequencedTaskRunner() {
76 scoped_refptr
<base::SequencedWorkerPool
> worker_pool
=
77 content::BrowserThread::GetBlockingPool();
79 return worker_pool
->GetSequencedTaskRunnerWithShutdownBehavior(
80 worker_pool
->GetSequenceToken(),
81 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
);
84 void SetUpOnMainThread() override
{
85 ASSERT_TRUE(base_dir_
.CreateUniqueTempDir());
87 SyncFileSystemServiceFactory
* factory
=
88 SyncFileSystemServiceFactory::GetInstance();
90 content::BrowserContext
* context
= browser()->profile();
91 ExtensionServiceInterface
* extension_service
=
92 extensions::ExtensionSystem::Get(context
)->extension_service();
94 scoped_ptr
<drive_backend::SyncEngine::DriveServiceFactory
>
95 drive_service_factory(new FakeDriveServiceFactory(this));
97 fake_signin_manager_
.reset(new FakeSigninManagerForTesting(
98 browser()->profile()));
100 remote_service_
= new drive_backend::SyncEngine(
101 base::ThreadTaskRunnerHandle::Get(), // ui_task_runner
102 MakeSequencedTaskRunner(),
103 MakeSequencedTaskRunner(),
104 content::BrowserThread::GetBlockingPool(),
107 NULL
, // notification_manager
109 fake_signin_manager_
.get(), // signin_manager
110 NULL
, // token_service
111 NULL
, // request_context
112 drive_service_factory
.Pass(),
113 in_memory_env_
.get());
114 remote_service_
->SetSyncEnabled(true);
115 factory
->set_mock_remote_file_service(
116 scoped_ptr
<RemoteFileSyncService
>(remote_service_
));
119 // drive::FakeDriveService::ChangeObserver override.
120 void OnNewChangeAvailable() override
{
121 sync_engine()->OnNotificationReceived();
124 SyncFileSystemService
* sync_file_system_service() {
125 return SyncFileSystemServiceFactory::GetForProfile(browser()->profile());
128 drive_backend::SyncEngine
* sync_engine() {
129 return static_cast<drive_backend::SyncEngine
*>(
130 sync_file_system_service()->remote_service_
.get());
133 LocalFileSyncService
* local_file_sync_service() {
134 return sync_file_system_service()->local_service_
.get();
138 fake_signin_manager_
->SetAuthenticatedAccountInfo("12345", "tester");
139 sync_engine()->GoogleSigninSucceeded("12345", "tester", "password");
142 void SetSyncEnabled(bool enabled
) {
143 sync_file_system_service()->SetSyncEnabledForTesting(enabled
);
146 void WaitUntilIdle() {
147 base::RunLoop run_loop
;
148 sync_file_system_service()->CallOnIdleForTesting(run_loop
.QuitClosure());
153 base::ScopedTempDir base_dir_
;
154 scoped_ptr
<leveldb::Env
> in_memory_env_
;
156 scoped_ptr
<FakeSigninManagerForTesting
> fake_signin_manager_
;
158 drive::FakeDriveService
* fake_drive_service_
;
159 LocalFileSyncService
* local_service_
;
160 drive_backend::SyncEngine
* remote_service_
;
162 int64 real_minimum_preserved_space_
;
164 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemTest
);
167 IN_PROC_BROWSER_TEST_F(SyncFileSystemTest
, AuthorizationTest
) {
168 ExtensionTestMessageListener
open_failure(
169 "checkpoint: Failed to get syncfs", true);
170 ExtensionTestMessageListener
bar_created(
171 "checkpoint: \"/bar\" created", true);
172 ExtensionTestMessageListener
foo_created(
173 "checkpoint: \"/foo\" created", true);
174 extensions::ResultCatcher catcher
;
176 LoadAndLaunchPlatformApp("sync_file_system/authorization_test", "Launched");
178 // Application sync is disabled at the initial state. Thus first
179 // syncFilesystem.requestFileSystem call should fail.
180 ASSERT_TRUE(open_failure
.WaitUntilSatisfied());
182 // Enable Application sync and let the app retry.
184 SetSyncEnabled(true);
186 open_failure
.Reply("resume");
188 ASSERT_TRUE(foo_created
.WaitUntilSatisfied());
190 // The app creates a file "/foo", that should successfully sync to the remote
191 // service. Wait for the completion and resume the app.
194 sync_engine()->GoogleSignedOut("test_account", std::string());
195 foo_created
.Reply("resume");
197 ASSERT_TRUE(bar_created
.WaitUntilSatisfied());
199 // The app creates anohter file "/bar". Since the user signed out from chrome
200 // The synchronization should fail and the service state should be
201 // AUTHENTICATION_REQUIRED.
204 EXPECT_EQ(REMOTE_SERVICE_AUTHENTICATION_REQUIRED
,
205 sync_engine()->GetCurrentState());
207 sync_engine()->GoogleSigninSucceeded("test_account", "tester", "testing");
210 bar_created
.Reply("resume");
212 EXPECT_TRUE(catcher
.GetNextResult());
215 } // namespace sync_file_system