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 namespace { | |
25 | |
26 class FakeDriveServiceFactory | |
27 : public drive_backend::SyncEngine::DriveServiceFactory { | |
28 public: | |
29 explicit FakeDriveServiceFactory( | |
30 drive::FakeDriveService::ChangeObserver* change_observer) | |
31 : change_observer_(change_observer) {} | |
32 virtual ~FakeDriveServiceFactory() {} | |
33 | |
34 virtual scoped_ptr<drive::DriveServiceInterface> CreateDriveService( | |
35 OAuth2TokenService* oauth2_token_service, | |
36 net::URLRequestContextGetter* url_request_context_getter, | |
37 base::SequencedTaskRunner* blocking_task_runner) OVERRIDE { | |
38 scoped_ptr<drive::FakeDriveService> drive_service( | |
39 new drive::FakeDriveService); | |
40 drive_service->AddChangeObserver(change_observer_); | |
41 return drive_service.PassAs<drive::DriveServiceInterface>(); | |
42 } | |
43 | |
44 private: | |
45 drive::FakeDriveService::ChangeObserver* change_observer_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(FakeDriveServiceFactory); | |
48 }; | |
49 | |
50 } // namespace | |
51 | |
52 class SyncFileSystemTest : public extensions::PlatformAppBrowserTest, | |
53 public drive::FakeDriveService::ChangeObserver { | |
54 public: | |
55 SyncFileSystemTest() | |
56 : fake_drive_service_(NULL), | |
57 local_service_(NULL), | |
58 remote_service_(NULL) { | |
59 } | |
60 | |
61 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | |
62 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); | |
63 real_minimum_preserved_space_ = | |
64 storage::QuotaManager::kMinimumPreserveForSystem; | |
65 storage::QuotaManager::kMinimumPreserveForSystem = 0; | |
66 } | |
67 | |
68 virtual void TearDownInProcessBrowserTestFixture() OVERRIDE { | |
69 storage::QuotaManager::kMinimumPreserveForSystem = | |
70 real_minimum_preserved_space_; | |
71 ExtensionApiTest::TearDownInProcessBrowserTestFixture(); | |
72 } | |
73 | |
74 scoped_refptr<base::SequencedTaskRunner> MakeSequencedTaskRunner() { | |
75 scoped_refptr<base::SequencedWorkerPool> worker_pool = | |
76 content::BrowserThread::GetBlockingPool(); | |
77 | |
78 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
79 worker_pool->GetSequenceToken(), | |
80 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN); | |
81 } | |
82 | |
83 virtual void SetUpOnMainThread() OVERRIDE { | |
84 ASSERT_TRUE(base_dir_.CreateUniqueTempDir()); | |
85 | |
86 SyncFileSystemServiceFactory* factory = | |
87 SyncFileSystemServiceFactory::GetInstance(); | |
88 | |
89 content::BrowserContext* context = browser()->profile(); | |
90 ExtensionServiceInterface* extension_service = | |
91 extensions::ExtensionSystem::Get(context)->extension_service(); | |
92 | |
93 scoped_ptr<drive_backend::SyncEngine::DriveServiceFactory> | |
94 drive_service_factory( | |
peria
2014/09/08 07:05:04
nit: connect with the next line, if possible
tzik
2014/09/08 07:30:22
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.Pass(), | |
112 in_memory_env_.get()); | |
113 remote_service_->SetSyncEnabled(true); | |
114 factory->set_mock_remote_file_service( | |
115 scoped_ptr<RemoteFileSyncService>(remote_service_)); | |
116 } | |
117 | |
118 // drive::FakeDriveService::ChangeObserver override. | |
119 virtual void OnNewChangeAvailable() OVERRIDE { | |
120 sync_engine()->OnNotificationReceived(); | |
121 } | |
122 | |
123 SyncFileSystemService* sync_file_system_service() { | |
124 return SyncFileSystemServiceFactory::GetForProfile(browser()->profile()); | |
125 } | |
126 | |
127 drive_backend::SyncEngine* sync_engine() { | |
128 return static_cast<drive_backend::SyncEngine*>( | |
129 sync_file_system_service()->remote_service_.get()); | |
130 } | |
131 | |
132 LocalFileSyncService* local_file_sync_service() { | |
133 return sync_file_system_service()->local_service_.get(); | |
134 } | |
135 | |
136 void SignIn() { | |
137 fake_signin_manager_->SetAuthenticatedUsername("tester"); | |
138 sync_engine()->GoogleSigninSucceeded("test_account", "tester", "testing"); | |
139 } | |
140 | |
141 void SetSyncEnabled(bool enabled) { | |
142 sync_file_system_service()->SetSyncEnabledForTesting(enabled); | |
143 } | |
144 | |
145 void WaitUntilIdle() { | |
146 base::RunLoop run_loop; | |
147 sync_file_system_service()->CallOnIdleForTesting(run_loop.QuitClosure()); | |
148 run_loop.Run(); | |
149 } | |
150 | |
151 private: | |
152 base::ScopedTempDir base_dir_; | |
153 scoped_ptr<leveldb::Env> in_memory_env_; | |
154 | |
155 scoped_ptr<FakeSigninManagerForTesting> fake_signin_manager_; | |
156 | |
157 drive::FakeDriveService* fake_drive_service_; | |
158 LocalFileSyncService* local_service_; | |
159 drive_backend::SyncEngine* remote_service_; | |
160 | |
161 int64 real_minimum_preserved_space_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(SyncFileSystemTest); | |
164 }; | |
165 | |
166 IN_PROC_BROWSER_TEST_F(SyncFileSystemTest, AuthorizationTest) { | |
167 ExtensionTestMessageListener open_failure( | |
168 "checkpoint: Failed to get syncfs", true); | |
169 ExtensionTestMessageListener bar_created( | |
170 "checkpoint: \"/bar\" created", true); | |
171 ExtensionTestMessageListener foo_created( | |
172 "checkpoint: \"/foo\" created", true); | |
173 ResultCatcher catcher; | |
174 | |
175 LoadAndLaunchPlatformApp("sync_file_system/authorization_test", "Launched"); | |
176 | |
177 // Application sync is disabled at the initial state. Thus first | |
178 // syncFilesystem.requestFileSystem call should fail. | |
179 ASSERT_TRUE(open_failure.WaitUntilSatisfied()); | |
180 | |
181 // Enable Application sync and let the app retry. | |
182 SignIn(); | |
183 SetSyncEnabled(true); | |
184 | |
185 open_failure.Reply("resume"); | |
186 | |
187 ASSERT_TRUE(foo_created.WaitUntilSatisfied()); | |
188 | |
189 // The app creates a file "/foo", that should successfully sync to the remote | |
190 // service. Wait for the completion and resume the app. | |
191 WaitUntilIdle(); | |
192 | |
193 sync_engine()->GoogleSignedOut("test_account", std::string()); | |
194 foo_created.Reply("resume"); | |
195 | |
196 ASSERT_TRUE(bar_created.WaitUntilSatisfied()); | |
197 | |
198 // The app creates anohter file "/bar". Since the user signed out from chrome | |
199 // The synchronization should fail and the service state should be | |
200 // AUTHENTICATION_REQUIRED. | |
201 | |
202 WaitUntilIdle(); | |
203 EXPECT_EQ(REMOTE_SERVICE_AUTHENTICATION_REQUIRED, | |
204 sync_engine()->GetCurrentState()); | |
205 | |
206 sync_engine()->GoogleSigninSucceeded("test_account", "tester", "testing"); | |
207 WaitUntilIdle(); | |
208 | |
209 bar_created.Reply("resume"); | |
210 | |
211 EXPECT_TRUE(catcher.GetNextResult()); | |
212 } | |
213 | |
214 } // namespace sync_file_system | |
OLD | NEW |