| 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" | |
| 8 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/location.h" | |
| 11 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 12 #include "base/thread_task_runner_handle.h" | |
| 13 #include "chrome/browser/apps/app_browsertest_util.h" | 10 #include "chrome/browser/apps/app_browsertest_util.h" |
| 14 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 11 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
| 15 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 12 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 16 #include "chrome/browser/chromeos/drive/file_system_util.h" | 13 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 17 #include "chrome/browser/chromeos/file_manager/volume_manager.h" | 14 #include "chrome/browser/chromeos/file_manager/volume_manager.h" |
| 18 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | 15 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| 19 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| 20 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 17 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 21 #include "chrome/browser/drive/fake_drive_service.h" | 18 #include "chrome/browser/drive/fake_drive_service.h" |
| 22 #include "chrome/browser/extensions/component_loader.h" | 19 #include "chrome/browser/extensions/component_loader.h" |
| 23 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/extensions/api/file_system.h" | |
| 25 #include "content/public/test/test_utils.h" | 21 #include "content/public/test/test_utils.h" |
| 26 #include "extensions/browser/event_router.h" | |
| 27 #include "google_apis/drive/drive_api_parser.h" | 22 #include "google_apis/drive/drive_api_parser.h" |
| 28 #include "google_apis/drive/test_util.h" | 23 #include "google_apis/drive/test_util.h" |
| 29 #include "storage/browser/fileapi/external_mount_points.h" | 24 #include "storage/browser/fileapi/external_mount_points.h" |
| 30 #include "ui/base/ui_base_types.h" | 25 #include "ui/base/ui_base_types.h" |
| 31 | 26 |
| 32 using file_manager::VolumeManager; | 27 using file_manager::VolumeManager; |
| 33 | 28 |
| 34 namespace extensions { | 29 namespace extensions { |
| 35 namespace { | 30 namespace { |
| 36 | 31 |
| 37 // Mount point names for chrome.fileSystem.requestFileSystem() tests. | 32 // Mount point names for chrome.fileSystem.requestFileSystem() tests. |
| 38 const char kWritableMountPointName[] = "writable"; | 33 const char kWritableMountPointName[] = "writable"; |
| 39 const char kReadOnlyMountPointName[] = "read-only"; | 34 const char kReadOnlyMountPointName[] = "read-only"; |
| 40 | 35 |
| 41 // Child directory created in each of the mount points. | 36 // Child directory created in each of the mount points. |
| 42 const char kChildDirectory[] = "child-dir"; | 37 const char kChildDirectory[] = "child-dir"; |
| 43 | 38 |
| 44 // ID of a testing extension. | |
| 45 const char kTestingExtensionId[] = "pkplfbidichfdicaijlchgnapepdginl"; | |
| 46 | |
| 47 } // namespace | 39 } // namespace |
| 48 | 40 |
| 49 // Skips the user consent dialog for chrome.fileSystem.requestFileSystem() and | 41 // Skips the user consent dialog for chrome.fileSystem.requestFileSystem() and |
| 50 // simulates clicking of the specified dialog button. | 42 // simulates clicking of the specified dialog button. |
| 51 class ScopedSkipRequestFileSystemDialog { | 43 class ScopedSkipRequestFileSystemDialog { |
| 52 public: | 44 public: |
| 53 explicit ScopedSkipRequestFileSystemDialog(ui::DialogButton button) { | 45 explicit ScopedSkipRequestFileSystemDialog(ui::DialogButton button) { |
| 54 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( | 46 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( |
| 55 button); | 47 button); |
| 56 } | 48 } |
| 57 ~ScopedSkipRequestFileSystemDialog() { | 49 ~ScopedSkipRequestFileSystemDialog() { |
| 58 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( | 50 file_system_api::ConsentProviderDelegate::SetAutoDialogButtonForTest( |
| 59 ui::DIALOG_BUTTON_NONE); | 51 ui::DIALOG_BUTTON_NONE); |
| 60 } | 52 } |
| 61 | 53 |
| 62 private: | 54 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(ScopedSkipRequestFileSystemDialog); | 55 DISALLOW_COPY_AND_ASSIGN(ScopedSkipRequestFileSystemDialog); |
| 64 }; | 56 }; |
| 65 | 57 |
| 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 | |
| 101 // This class contains chrome.filesystem API test specific to Chrome OS, namely, | 58 // This class contains chrome.filesystem API test specific to Chrome OS, namely, |
| 102 // the integrated Google Drive support. | 59 // the integrated Google Drive support. |
| 103 class FileSystemApiTestForDrive : public PlatformAppBrowserTest { | 60 class FileSystemApiTestForDrive : public PlatformAppBrowserTest { |
| 104 public: | 61 public: |
| 105 FileSystemApiTestForDrive() | 62 FileSystemApiTestForDrive() |
| 106 : fake_drive_service_(NULL), | 63 : fake_drive_service_(NULL), |
| 107 integration_service_(NULL) { | 64 integration_service_(NULL) { |
| 108 } | 65 } |
| 109 | 66 |
| 110 // Sets up fake Drive service for tests (this has to be injected before the | 67 // 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... |
| 217 CreateTestingFileSystem(kReadOnlyMountPointName, true /* read_only */); | 174 CreateTestingFileSystem(kReadOnlyMountPointName, true /* read_only */); |
| 218 PlatformAppBrowserTest::SetUpOnMainThread(); | 175 PlatformAppBrowserTest::SetUpOnMainThread(); |
| 219 } | 176 } |
| 220 | 177 |
| 221 void TearDownOnMainThread() override { | 178 void TearDownOnMainThread() override { |
| 222 PlatformAppBrowserTest::TearDownOnMainThread(); | 179 PlatformAppBrowserTest::TearDownOnMainThread(); |
| 223 user_manager_enabler_.reset(); | 180 user_manager_enabler_.reset(); |
| 224 fake_user_manager_ = nullptr; | 181 fake_user_manager_ = nullptr; |
| 225 } | 182 } |
| 226 | 183 |
| 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 | |
| 237 protected: | 184 protected: |
| 238 base::ScopedTempDir temp_dir_; | 185 base::ScopedTempDir temp_dir_; |
| 239 chromeos::FakeChromeUserManager* fake_user_manager_; | 186 chromeos::FakeChromeUserManager* fake_user_manager_; |
| 240 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | 187 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| 241 | 188 |
| 242 // Creates a testing file system in a testing directory. | 189 // Creates a testing file system in a testing directory. |
| 243 void CreateTestingFileSystem(const std::string& mount_point_name, | 190 void CreateTestingFileSystem(const std::string& mount_point_name, |
| 244 bool read_only) { | 191 bool read_only) { |
| 245 const base::FilePath mount_point_path = | 192 const base::FilePath mount_point_path = |
| 246 temp_dir_.path().Append(mount_point_name); | 193 temp_dir_.path().Append(mount_point_name); |
| 194 LOG(ERROR) << mount_point_path.value(); |
| 247 ASSERT_TRUE(base::CreateDirectory(mount_point_path)); | 195 ASSERT_TRUE(base::CreateDirectory(mount_point_path)); |
| 248 ASSERT_TRUE( | 196 ASSERT_TRUE( |
| 249 base::CreateDirectory(mount_point_path.Append(kChildDirectory))); | 197 base::CreateDirectory(mount_point_path.Append(kChildDirectory))); |
| 250 ASSERT_TRUE(content::BrowserContext::GetMountPoints(browser()->profile()) | 198 ASSERT_TRUE(content::BrowserContext::GetMountPoints(browser()->profile()) |
| 251 ->RegisterFileSystem( | 199 ->RegisterFileSystem( |
| 252 mount_point_name, storage::kFileSystemTypeNativeLocal, | 200 mount_point_name, storage::kFileSystemTypeNativeLocal, |
| 253 storage::FileSystemMountOption(), mount_point_path)); | 201 storage::FileSystemMountOption(), mount_point_path)); |
| 254 VolumeManager* const volume_manager = | 202 VolumeManager* const volume_manager = |
| 255 VolumeManager::Get(browser()->profile()); | 203 VolumeManager::Get(browser()->profile()); |
| 256 ASSERT_TRUE(volume_manager); | 204 ASSERT_TRUE(volume_manager); |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 << message_; | 416 << message_; |
| 469 } | 417 } |
| 470 | 418 |
| 471 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForRequestFileSystem, | 419 IN_PROC_BROWSER_TEST_F(FileSystemApiTestForRequestFileSystem, |
| 472 GetVolumeList_NotKioskSession) { | 420 GetVolumeList_NotKioskSession) { |
| 473 ASSERT_TRUE(RunPlatformAppTest( | 421 ASSERT_TRUE(RunPlatformAppTest( |
| 474 "api_test/file_system/get_volume_list_not_kiosk_session")) | 422 "api_test/file_system/get_volume_list_not_kiosk_session")) |
| 475 << message_; | 423 << message_; |
| 476 } | 424 } |
| 477 | 425 |
| 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 | |
| 492 } // namespace extensions | 426 } // namespace extensions |
| OLD | NEW |