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 "base/files/file.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/memory/scoped_vector.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/time/time.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/chromeos/file_system_provider/notification_manager_interface.h"
12 #include "chrome/browser/chromeos/file_system_provider/observer.h"
13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info.h"
14 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_interface.h"
15 #include "chrome/browser/chromeos/file_system_provider/request_manager.h"
16 #include "chrome/browser/chromeos/file_system_provider/request_value.h"
17 #include "chrome/browser/chromeos/file_system_provider/service.h"
18 #include "chrome/browser/extensions/extension_apitest.h"
19 #include "ui/base/ui_base_types.h"
20 #include "ui/message_center/message_center.h"
22 namespace extensions
{
25 using chromeos::file_system_provider::MountContext
;
26 using chromeos::file_system_provider::NotificationManagerInterface
;
27 using chromeos::file_system_provider::Observer
;
28 using chromeos::file_system_provider::ProvidedFileSystemInterface
;
29 using chromeos::file_system_provider::ProvidedFileSystemInfo
;
30 using chromeos::file_system_provider::RequestManager
;
31 using chromeos::file_system_provider::RequestType
;
32 using chromeos::file_system_provider::RequestValue
;
33 using chromeos::file_system_provider::Service
;
35 // Clicks the default button on the notification as soon as request timeouts
36 // and a unresponsiveness notification is shown.
37 class NotificationButtonClicker
: public RequestManager::Observer
{
39 explicit NotificationButtonClicker(
40 const ProvidedFileSystemInfo
& file_system_info
)
41 : file_system_info_(file_system_info
) {}
42 ~NotificationButtonClicker() override
{}
44 // RequestManager::Observer overrides.
45 void OnRequestCreated(int request_id
, RequestType type
) override
{}
46 void OnRequestDestroyed(int request_id
) override
{}
47 void OnRequestExecuted(int request_id
) override
{}
48 void OnRequestFulfilled(int request_id
,
49 const RequestValue
& result
,
50 bool has_more
) override
{}
51 void OnRequestRejected(int request_id
,
52 const RequestValue
& result
,
53 base::File::Error error
) override
{}
54 void OnRequestTimeouted(int request_id
) override
{
55 // Call asynchronously so the notification is setup is completed.
56 base::ThreadTaskRunnerHandle::Get()->PostTask(
57 FROM_HERE
, base::Bind(&NotificationButtonClicker::ClickButton
,
58 base::Unretained(this)));
63 g_browser_process
->message_center()->ClickOnNotificationButton(
64 file_system_info_
.mount_path().value(), ui::DIALOG_BUTTON_OK
);
67 ProvidedFileSystemInfo file_system_info_
;
68 DISALLOW_COPY_AND_ASSIGN(NotificationButtonClicker
);
71 // Simulates clicking on the unresponsive notification's abort button. Also,
72 // sets the timeout delay to 0 ms, so the notification is shown faster.
73 class AbortOnUnresponsivePerformer
: public Observer
{
75 explicit AbortOnUnresponsivePerformer(Profile
* profile
)
76 : service_(Service::Get(profile
)) {
79 service_
->AddObserver(this);
82 ~AbortOnUnresponsivePerformer() override
{ service_
->RemoveObserver(this); }
84 // Observer overrides.
85 void OnProvidedFileSystemMount(const ProvidedFileSystemInfo
& file_system_info
,
87 base::File::Error error
) override
{
88 if (error
!= base::File::FILE_OK
)
91 ProvidedFileSystemInterface
* const file_system
=
92 service_
->GetProvidedFileSystem(file_system_info
.extension_id(),
93 file_system_info
.file_system_id());
95 file_system
->GetRequestManager()->SetTimeoutForTesting(base::TimeDelta());
97 scoped_ptr
<NotificationButtonClicker
> clicker(
98 new NotificationButtonClicker(file_system
->GetFileSystemInfo()));
100 file_system
->GetRequestManager()->AddObserver(clicker
.get());
101 clickers_
.push_back(clicker
.Pass());
104 void OnProvidedFileSystemUnmount(
105 const ProvidedFileSystemInfo
& file_system_info
,
106 base::File::Error error
) override
{}
109 Service
* service_
; // Not owned.
110 ScopedVector
<NotificationButtonClicker
> clickers_
;
111 DISALLOW_COPY_AND_ASSIGN(AbortOnUnresponsivePerformer
);
116 class FileSystemProviderApiTest
: public ExtensionApiTest
{
118 FileSystemProviderApiTest() {}
120 // Loads a helper testing extension.
121 void SetUpOnMainThread() override
{
122 ExtensionApiTest::SetUpOnMainThread();
123 const extensions::Extension
* extension
= LoadExtensionWithFlags(
124 test_data_dir_
.AppendASCII("file_system_provider/test_util"),
125 kFlagEnableIncognito
);
126 ASSERT_TRUE(extension
);
130 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Mount
) {
131 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/mount",
132 kFlagLoadAsComponent
))
136 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Unmount
) {
137 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/unmount",
138 kFlagLoadAsComponent
))
142 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, GetAll
) {
143 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/get_all",
144 kFlagLoadAsComponent
))
148 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, GetMetadata
) {
149 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/get_metadata",
150 kFlagLoadAsComponent
))
154 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, ReadDirectory
) {
155 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/read_directory",
156 kFlagLoadAsComponent
))
160 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, ReadFile
) {
161 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/read_file",
162 kFlagLoadAsComponent
))
166 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, BigFile
) {
167 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/big_file",
168 kFlagLoadAsComponent
))
172 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Evil
) {
173 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/evil",
174 kFlagLoadAsComponent
))
178 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, MimeType
) {
179 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/mime_type",
180 kFlagLoadAsComponent
))
184 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, CreateDirectory
) {
185 ASSERT_TRUE(RunPlatformAppTestWithFlags(
186 "file_system_provider/create_directory", kFlagLoadAsComponent
))
190 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, DeleteEntry
) {
191 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/delete_entry",
192 kFlagLoadAsComponent
))
196 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, CreateFile
) {
197 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/create_file",
198 kFlagLoadAsComponent
))
202 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, CopyEntry
) {
203 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/copy_entry",
204 kFlagLoadAsComponent
))
208 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, MoveEntry
) {
209 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/move_entry",
210 kFlagLoadAsComponent
))
214 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Truncate
) {
215 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/truncate",
216 kFlagLoadAsComponent
))
220 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, WriteFile
) {
221 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/write_file",
222 kFlagLoadAsComponent
))
226 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Extension
) {
227 ASSERT_TRUE(RunComponentExtensionTest("file_system_provider/extension"))
231 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Thumbnail
) {
232 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/thumbnail",
233 kFlagLoadAsComponent
))
237 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, AddWatcher
) {
238 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/add_watcher",
239 kFlagLoadAsComponent
))
243 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, RemoveWatcher
) {
244 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/remove_watcher",
245 kFlagLoadAsComponent
))
249 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Notify
) {
250 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/notify",
251 kFlagLoadAsComponent
))
255 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Configure
) {
256 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/configure",
257 kFlagLoadAsComponent
))
261 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, GetActions
) {
262 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/get_actions",
263 kFlagLoadAsComponent
))
267 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, ExecuteAction
) {
268 ASSERT_TRUE(RunPlatformAppTestWithFlags("file_system_provider/execute_action",
269 kFlagLoadAsComponent
))
273 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Unresponsive_Extension
) {
274 AbortOnUnresponsivePerformer
performer(browser()->profile());
276 RunComponentExtensionTest("file_system_provider/unresponsive_extension"))
280 IN_PROC_BROWSER_TEST_F(FileSystemProviderApiTest
, Unresponsive_App
) {
281 AbortOnUnresponsivePerformer
performer(browser()->profile());
282 ASSERT_TRUE(RunPlatformAppTestWithFlags(
283 "file_system_provider/unresponsive_app", kFlagLoadAsComponent
))
287 } // namespace extensions