OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
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/drive/fake_drive_service.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/extension_test_message_listener.h" | |
13 #include "chrome/browser/signin/fake_signin_manager.h" | |
14 #include "chrome/browser/sync_file_system/drive_backend/sync_engine.h" | |
15 #include "chrome/browser/sync_file_system/local/local_file_sync_service.h" | |
16 #include "chrome/browser/sync_file_system/sync_file_system_service.h" | |
17 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h" | |
18 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h" | |
19 #include "third_party/leveldatabase/src/include/leveldb/env.h" | |
20 #include "webkit/browser/quota/quota_manager.h" | |
21 | |
22 namespace sync_file_system { | |
23 | |
24 typedef drive::FakeDriveService::ChangeObserver ChangeObserver; | |
25 | |
26 namespace { | |
27 | |
28 class FakeDriveServiceFactory | |
29 : public drive_backend::SyncEngine::DriveServiceFactory { | |
30 public: | |
31 explicit FakeDriveServiceFactory(ChangeObserver* change_observer) | |
32 : change_observer_(change_observer) {} | |
33 virtual ~FakeDriveServiceFactory() {} | |
34 | |
35 virtual 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.PassAs<drive::DriveServiceInterface>(); | |
43 } | |
44 | |
45 private: | |
46 ChangeObserver* change_observer_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(FakeDriveServiceFactory); | |
49 }; | |
50 | |
51 } // namespace | |
52 | |
53 class SyncFileSystemTest : public extensions::PlatformAppBrowserTest, | |
54 public ChangeObserver { | |
peria
2014/09/05 04:55:35
(optional) Would you use the original name "drive:
tzik
2014/09/08 07:00:37
Done.
| |
55 public: | |
56 SyncFileSystemTest() | |
57 : fake_drive_service_(NULL), | |
58 local_service_(NULL), | |
59 remote_service_(NULL) { | |
60 } | |
61 | |
62 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
63 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | |
64 real_minimum_preserved_space_ = | |
65 storage::QuotaManager::kMinimumPreserveForSystem; | |
66 storage::QuotaManager::kMinimumPreserveForSystem = 0; | |
67 } | |
68 | |
69 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
70 storage::QuotaManager::kMinimumPreserveForSystem = | |
71 real_minimum_preserved_space_; | |
72 ExtensionApiTest::TearDownInProcessBrowserTestFixture(); | |
73 } | |
74 | |
75 scoped_refptr<base::SequencedTaskRunner> MakeSequencedTaskRunner() { | |
76 scoped_refptr<base::SequencedWorkerPool> worker_pool = | |
77 content::BrowserThread::GetBlockingPool(); | |
78 | |
79 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
80 worker_pool->GetSequenceToken(), | |
81 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
82 } | |
83 | |
84 virtual void SetUpOnMainThread() OVERRIDE { | |
85 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | |
86 | |
87 SyncFileSystemServiceFactory* factory = | |
88 SyncFileSystemServiceFactory::GetInstance(); | |
89 | |
90 content::BrowserContext* context = browser()->profile(); | |
91 ExtensionServiceInterface* extension_service = | |
92 extensions::ExtensionSystem::Get(context)->extension_service(); | |
93 | |
94 scoped_ptr<FakeDriveServiceFactory> drive_service_factory( | |
peria
2014/09/05 04:55:35
Can we rewrite the type to
scoped_ptr<drive_backen
tzik
2014/09/08 07:00:37
Done.
| |
95 new FakeDriveServiceFactory(this)); | |
96 | |
97 fake_signin_manager_.reset(new FakeSigninManagerForTesting( | |
98 browser()->profile())); | |
99 | |
100 remote_service_ = new drive_backend::SyncEngine( | |
101 base::ThreadTaskRunnerHandle::Get(), // ui_task_runner | |
102 MakeSequencedTaskRunner(), | |
103 MakeSequencedTaskRunner(), | |
104 base_dir_.path(), | |
105 NULL, // task_logger | |
106 NULL, // notification_manager | |
107 extension_service, | |
108 fake_signin_manager_.get(), // signin_manager | |
109 NULL, // token_service | |
110 NULL, // request_context | |
111 drive_service_factory.PassAs< | |
112 drive_backend::SyncEngine::DriveServiceFactory>(), | |
113 in_memory_env_.get()); | |
114 remote_service_->SetSyncEnabled(true); | |
115 factory->set_mock_remote_file_service( | |
116 scoped_ptr<RemoteFileSyncService>(remote_service_)); | |
117 } | |
118 | |
119 // drive::FakeDriveService::ChangeObserver override. | |
120 virtual void OnNewChangeAvailable() OVERRIDE { | |
121 sync_engine()->OnNotificationReceived(); | |
122 } | |
123 | |
124 SyncFileSystemService* sync_file_system_service() { | |
125 return SyncFileSystemServiceFactory::GetForProfile(browser()->profile()); | |
126 } | |
127 | |
128 drive_backend::SyncEngine* sync_engine() { | |
129 return static_cast<drive_backend::SyncEngine*>( | |
130 sync_file_system_service()->remote_service_.get()); | |
131 } | |
132 | |
133 LocalFileSyncService* local_file_sync_service() { | |
134 return sync_file_system_service()->local_service_.get(); | |
135 } | |
136 | |
137 void SignIn() { | |
138 fake_signin_manager_->SetAuthenticatedUsername("tester"); | |
139 sync_engine()->GoogleSigninSucceeded("test_account", "tester", "testing"); | |
140 } | |
141 | |
142 void SetSyncEnabled(bool enabled) { | |
143 sync_file_system_service()->SetSyncEnabledForTesting(enabled); | |
144 } | |
145 | |
146 void WaitUntilIdle() { | |
147 base::RunLoop run_loop; | |
148 sync_file_system_service()->CallOnIdleForTesting(run_loop.QuitClosure()); | |
149 run_loop.Run(); | |
150 } | |
151 | |
152 private: | |
153 base::ScopedTempDir base_dir_; | |
154 scoped_ptr<leveldb::Env> in_memory_env_; | |
155 | |
156 scoped_ptr<FakeSigninManagerForTesting> fake_signin_manager_; | |
157 | |
158 drive::FakeDriveService* fake_drive_service_; | |
159 LocalFileSyncService* local_service_; | |
160 drive_backend::SyncEngine* remote_service_; | |
161 | |
162 int64 real_minimum_preserved_space_; | |
163 | |
164 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemTest); | |
165 }; | |
166 | |
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 ResultCatcher catcher; | |
175 | |
176 LoadAndLaunchPlatformApp("sync_file_system/authorization_test", "Launched"); | |
177 | |
178 // Application sync is disabled at the initial state. Thus first | |
179 // syncFilesystem.requestFileSystem call should fail. | |
180 ASSERT_TRUE(open_failure.WaitUntilSatisfied()); | |
181 | |
182 // Enable Application sync and let the app retry. | |
183 SignIn(); | |
184 SetSyncEnabled(true); | |
185 | |
186 open_failure.Reply("resume"); | |
187 | |
188 ASSERT_TRUE(foo_created.WaitUntilSatisfied()); | |
189 | |
190 // The app creates a file "/foo", that should successfully sync to the remote | |
191 // service. Wait for the completion and resume the app. | |
192 WaitUntilIdle(); | |
193 | |
194 sync_engine()->GoogleSignedOut("test_account", std::string()); | |
195 foo_created.Reply("resume"); | |
196 | |
197 ASSERT_TRUE(bar_created.WaitUntilSatisfied()); | |
198 | |
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. | |
202 | |
203 WaitUntilIdle(); | |
204 EXPECT_EQ(REMOTE_SERVICE_AUTHENTICATION_REQUIRED, | |
205 sync_engine()->GetCurrentState()); | |
206 | |
207 sync_engine()->GoogleSigninSucceeded("test_account", "tester", "testing"); | |
208 WaitUntilIdle(); | |
209 | |
210 bar_created.Reply("resume"); | |
211 | |
212 EXPECT_TRUE(catcher.GetNextResult()); | |
213 } | |
214 | |
215 } // namespace sync_file_system | |
OLD | NEW |