OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 5 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
6 | 6 |
| 7 #include "base/callback.h" |
7 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" |
9 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/thread_task_runner_handle.h" |
10 #include "chrome/browser/apps/app_browsertest_util.h" | 13 #include "chrome/browser/apps/app_browsertest_util.h" |
11 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 14 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
12 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 15 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
13 #include "chrome/browser/chromeos/drive/file_system_util.h" | 16 #include "chrome/browser/chromeos/drive/file_system_util.h" |
14 #include "chrome/browser/chromeos/file_manager/volume_manager.h" | 17 #include "chrome/browser/chromeos/file_manager/volume_manager.h" |
15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | 18 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
17 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 20 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
18 #include "chrome/browser/drive/fake_drive_service.h" | 21 #include "chrome/browser/drive/fake_drive_service.h" |
19 #include "chrome/browser/extensions/component_loader.h" | 22 #include "chrome/browser/extensions/component_loader.h" |
20 #include "chrome/common/chrome_paths.h" | 23 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/extensions/api/file_system.h" |
21 #include "content/public/test/test_utils.h" | 25 #include "content/public/test/test_utils.h" |
| 26 #include "extensions/browser/event_router.h" |
22 #include "google_apis/drive/drive_api_parser.h" | 27 #include "google_apis/drive/drive_api_parser.h" |
23 #include "google_apis/drive/test_util.h" | 28 #include "google_apis/drive/test_util.h" |
24 #include "storage/browser/fileapi/external_mount_points.h" | 29 #include "storage/browser/fileapi/external_mount_points.h" |
25 #include "ui/base/ui_base_types.h" | 30 #include "ui/base/ui_base_types.h" |
26 | 31 |
27 using file_manager::VolumeManager; | 32 using file_manager::VolumeManager; |
28 | 33 |
29 namespace extensions { | 34 namespace extensions { |
30 namespace { | 35 namespace { |
31 | 36 |
32 // Mount point names for chrome.fileSystem.requestFileSystem() tests. | 37 // Mount point names for chrome.fileSystem.requestFileSystem() tests. |
33 const char kWritableMountPointName[] = "writable"; | 38 const char kWritableMountPointName[] = "writable"; |
34 const char kReadOnlyMountPointName[] = "read-only"; | 39 const char kReadOnlyMountPointName[] = "read-only"; |
35 | 40 |
36 // Child directory created in each of the mount points. | 41 // Child directory created in each of the mount points. |
37 const char kChildDirectory[] = "child-dir"; | 42 const char kChildDirectory[] = "child-dir"; |
38 | 43 |
| 44 // ID of a testing extension. |
| 45 const char kTestingExtensionId[] = "pkplfbidichfdicaijlchgnapepdginl"; |
| 46 |
39 } // namespace | 47 } // namespace |
40 | 48 |
41 // Skips the user consent dialog for chrome.fileSystem.requestFileSystem() and | 49 // Skips the user consent dialog for chrome.fileSystem.requestFileSystem() and |
42 // simulates clicking of the specified dialog button. | 50 // simulates clicking of the specified dialog button. |
43 class ScopedSkipRequestFileSystemDialog { | 51 class ScopedSkipRequestFileSystemDialog { |
44 public: | 52 public: |
45 explicit ScopedSkipRequestFileSystemDialog(ui::DialogButton button) { | 53 explicit ScopedSkipRequestFileSystemDialog(ui::DialogButton button) { |
46 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( | 54 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( |
47 button); | 55 button); |
48 } | 56 } |
49 ~ScopedSkipRequestFileSystemDialog() { | 57 ~ScopedSkipRequestFileSystemDialog() { |
50 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( | 58 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( |
51 ui::DIALOG_BUTTON_NONE); | 59 ui::DIALOG_BUTTON_NONE); |
52 } | 60 } |
53 | 61 |
54 private: | 62 private: |
55 DISALLOW_COPY_AND_ASSIGN(ScopedSkipRequestFileSystemDialog); | 63 DISALLOW_COPY_AND_ASSIGN(ScopedSkipRequestFileSystemDialog); |
56 }; | 64 }; |
57 | 65 |
| 66 // Observers adding a listener to the |event_name| event by |extension|, and |
| 67 // then fires the |callback|. |
| 68 class ScopedAddListenerObserver : public EventRouter::Observer { |
| 69 public: |
| 70 ScopedAddListenerObserver(Profile* profile, |
| 71 const std::string& event_name, |
| 72 const std::string& extension_id, |
| 73 const base::Closure& callback) |
| 74 : extension_id_(extension_id), |
| 75 callback_(callback), |
| 76 event_router_(EventRouter::EventRouter::Get(profile)) { |
| 77 DCHECK(profile); |
| 78 DCHECK(event_router_); |
| 79 event_router_->RegisterObserver(this, event_name); |
| 80 } |
| 81 |
| 82 ~ScopedAddListenerObserver() { event_router_->UnregisterObserver(this); } |
| 83 |
| 84 // EventRouter::Observer overrides. |
| 85 void OnListenerAdded(const EventListenerInfo& details) override { |
| 86 // Call the callback only once, as the listener may be added multiple times. |
| 87 if (details.extension_id == extension_id_ && !callback_.is_null()) { |
| 88 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, callback_); |
| 89 callback_ = base::Closure(); |
| 90 } |
| 91 } |
| 92 |
| 93 private: |
| 94 const std::string extension_id_; |
| 95 base::Closure callback_; |
| 96 EventRouter* const event_router_; |
| 97 |
| 98 DISALLOW_COPY_AND_ASSIGN(ScopedAddListenerObserver); |
| 99 }; |
| 100 |
58 // This class contains chrome.filesystem API test specific to Chrome OS, namely, | 101 // This class contains chrome.filesystem API test specific to Chrome OS, namely, |
59 // the integrated Google Drive support. | 102 // the integrated Google Drive support. |
60 class FileSystemApiTestForDrive : public PlatformAppBrowserTest { | 103 class FileSystemApiTestForDrive : public PlatformAppBrowserTest { |
61 public: | 104 public: |
62 FileSystemApiTestForDrive() | 105 FileSystemApiTestForDrive() |
63 : fake_drive_service_(NULL), | 106 : fake_drive_service_(NULL), |
64 integration_service_(NULL) { | 107 integration_service_(NULL) { |
65 } | 108 } |
66 | 109 |
67 // Sets up fake Drive service for tests (this has to be injected before the | 110 // Sets up fake Drive service for tests (this has to be injected before the |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 CreateTestingFileSystem(kReadOnlyMountPointName, true /* read_only */); | 217 CreateTestingFileSystem(kReadOnlyMountPointName, true /* read_only */); |
175 PlatformAppBrowserTest::SetUpOnMainThread(); | 218 PlatformAppBrowserTest::SetUpOnMainThread(); |
176 } | 219 } |
177 | 220 |
178 void TearDownOnMainThread() override { | 221 void TearDownOnMainThread() override { |
179 PlatformAppBrowserTest::TearDownOnMainThread(); | 222 PlatformAppBrowserTest::TearDownOnMainThread(); |
180 user_manager_enabler_.reset(); | 223 user_manager_enabler_.reset(); |
181 fake_user_manager_ = nullptr; | 224 fake_user_manager_ = nullptr; |
182 } | 225 } |
183 | 226 |
| 227 // Simulates mounting a removable volume. |
| 228 void MountFakeVolume() { |
| 229 VolumeManager* const volume_manager = |
| 230 VolumeManager::Get(browser()->profile()); |
| 231 ASSERT_TRUE(volume_manager); |
| 232 volume_manager->AddVolumeForTesting( |
| 233 base::FilePath("/a/b/c"), file_manager::VOLUME_TYPE_TESTING, |
| 234 chromeos::DEVICE_TYPE_UNKNOWN, false /* read_only */); |
| 235 } |
| 236 |
184 protected: | 237 protected: |
185 base::ScopedTempDir temp_dir_; | 238 base::ScopedTempDir temp_dir_; |
186 chromeos::FakeChromeUserManager* fake_user_manager_; | 239 chromeos::FakeChromeUserManager* fake_user_manager_; |
187 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | 240 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
188 | 241 |
189 // Creates a testing file system in a testing directory. | 242 // Creates a testing file system in a testing directory. |
190 void CreateTestingFileSystem(const std::string& mount_point_name, | 243 void CreateTestingFileSystem(const std::string& mount_point_name, |
191 bool read_only) { | 244 bool read_only) { |
192 const base::FilePath mount_point_path = | 245 const base::FilePath mount_point_path = |
193 temp_dir_.path().Append(mount_point_name); | 246 temp_dir_.path().Append(mount_point_name); |
194 LOG(ERROR) << mount_point_path.value(); | |
195 ASSERT_TRUE(base::CreateDirectory(mount_point_path)); | 247 ASSERT_TRUE(base::CreateDirectory(mount_point_path)); |
196 ASSERT_TRUE( | 248 ASSERT_TRUE( |
197 base::CreateDirectory(mount_point_path.Append(kChildDirectory))); | 249 base::CreateDirectory(mount_point_path.Append(kChildDirectory))); |
198 ASSERT_TRUE(content::BrowserContext::GetMountPoints(browser()->profile()) | 250 ASSERT_TRUE(content::BrowserContext::GetMountPoints(browser()->profile()) |
199 ->RegisterFileSystem( | 251 ->RegisterFileSystem( |
200 mount_point_name, storage::kFileSystemTypeNativeLocal, | 252 mount_point_name, storage::kFileSystemTypeNativeLocal, |
201 storage::FileSystemMountOption(), mount_point_path)); | 253 storage::FileSystemMountOption(), mount_point_path)); |
202 VolumeManager* const volume_manager = | 254 VolumeManager* const volume_manager = |
203 VolumeManager::Get(browser()->profile()); | 255 VolumeManager::Get(browser()->profile()); |
204 ASSERT_TRUE(volume_manager); | 256 ASSERT_TRUE(volume_manager); |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 << message_; | 468 << message_; |
417 } | 469 } |
418 | 470 |
419 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForRequestFileSystem, | 471 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForRequestFileSystem, |
420 GetVolumeList_NotKioskSession) { | 472 GetVolumeList_NotKioskSession) { |
421 ASSERT_TRUE(RunPlatformAppTest( | 473 ASSERT_TRUE(RunPlatformAppTest( |
422 "api_test/file_system/get_volume_list_not_kiosk_session")) | 474 "api_test/file_system/get_volume_list_not_kiosk_session")) |
423 << message_; | 475 << message_; |
424 } | 476 } |
425 | 477 |
| 478 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForRequestFileSystem, |
| 479 OnVolumeListChanged) { |
| 480 EnterKioskSession(); |
| 481 |
| 482 ScopedAddListenerObserver observer( |
| 483 profile(), extensions::api::file_system::OnVolumeListChanged::kEventName, |
| 484 kTestingExtensionId, |
| 485 base::Bind(&FileSystemApiTestForRequestFileSystem::MountFakeVolume, |
| 486 this)); |
| 487 |
| 488 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/on_volume_list_changed")) |
| 489 << message_; |
| 490 } |
| 491 |
426 } // namespace extensions | 492 } // namespace extensions |
OLD | NEW |