| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 8 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 9 #include "chrome/browser/extensions/api/file_system/saved_files_service.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 11 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/extensions/platform_app_browsertest_util.h" | 12 #include "chrome/browser/extensions/platform_app_browsertest_util.h" |
| 12 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 13 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 14 | 15 |
| 15 using extensions::FileSystemChooseEntryFunction; | 16 using extensions::FileSystemChooseEntryFunction; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 class AppInstallObserver : public content::NotificationObserver { | 20 class SetLastChooseEntryDirectoryObserver |
| 21 : public content::NotificationObserver { |
| 20 public: | 22 public: |
| 21 AppInstallObserver(const base::FilePath& choose_entry_directory, | 23 SetLastChooseEntryDirectoryObserver( |
| 22 extensions::ExtensionPrefs* prefs) | 24 const base::FilePath& choose_entry_directory, |
| 25 extensions::ExtensionPrefs* prefs) |
| 23 : path_(choose_entry_directory), | 26 : path_(choose_entry_directory), |
| 24 prefs_(prefs) { | 27 prefs_(prefs) { |
| 25 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 28 registrar_.Add(this, |
| 29 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 26 content::NotificationService::AllSources()); | 30 content::NotificationService::AllSources()); |
| 27 } | 31 } |
| 28 | 32 |
| 29 virtual void Observe(int type, | 33 virtual void Observe(int type, |
| 30 const content::NotificationSource& source, | 34 const content::NotificationSource& source, |
| 31 const content::NotificationDetails& details) OVERRIDE { | 35 const content::NotificationDetails& details) OVERRIDE { |
| 32 EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED, type); | 36 EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED, type); |
| 33 std::string extension_id = content::Details<const extensions::Extension>( | 37 std::string extension_id = |
| 34 details).ptr()->id(); | 38 content::Details<const extensions::Extension>(details).ptr()->id(); |
| 35 prefs_->SetLastChooseEntryDirectory(extension_id, path_); | 39 prefs_->SetLastChooseEntryDirectory(extension_id, path_); |
| 36 } | 40 } |
| 37 | 41 |
| 38 private: | 42 private: |
| 39 content::NotificationRegistrar registrar_; | 43 content::NotificationRegistrar registrar_; |
| 40 const base::FilePath path_; | 44 const base::FilePath path_; |
| 41 extensions::ExtensionPrefs* prefs_; | 45 extensions::ExtensionPrefs* prefs_; |
| 42 DISALLOW_COPY_AND_ASSIGN(AppInstallObserver); | 46 DISALLOW_COPY_AND_ASSIGN(SetLastChooseEntryDirectoryObserver); |
| 47 }; |
| 48 |
| 49 class AddSavedEntryObserver : public content::NotificationObserver { |
| 50 public: |
| 51 AddSavedEntryObserver(const base::FilePath& path_to_save, |
| 52 extensions::SavedFilesService* service) |
| 53 : path_(path_to_save), |
| 54 service_(service) { |
| 55 registrar_.Add(this, |
| 56 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 57 content::NotificationService::AllSources()); |
| 58 } |
| 59 |
| 60 virtual void Observe(int type, |
| 61 const content::NotificationSource& source, |
| 62 const content::NotificationDetails& details) OVERRIDE { |
| 63 EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED, type); |
| 64 std::string extension_id = |
| 65 content::Details<const extensions::Extension>(details).ptr()->id(); |
| 66 service_->AddFileEntry(extension_id, "magic id", path_, true); |
| 67 } |
| 68 |
| 69 private: |
| 70 content::NotificationRegistrar registrar_; |
| 71 const base::FilePath path_; |
| 72 extensions::SavedFilesService* service_; |
| 73 DISALLOW_COPY_AND_ASSIGN(AddSavedEntryObserver); |
| 43 }; | 74 }; |
| 44 | 75 |
| 45 } // namespace | 76 } // namespace |
| 46 | 77 |
| 47 class FileSystemApiTest : public extensions::PlatformAppBrowserTest { | 78 class FileSystemApiTest : public extensions::PlatformAppBrowserTest { |
| 48 public: | 79 public: |
| 49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 80 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 50 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); | 81 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); |
| 51 test_root_folder_ = test_data_dir_.AppendASCII("api_test") | 82 test_root_folder_ = test_data_dir_.AppendASCII("api_test") |
| 52 .AppendASCII("file_system"); | 83 .AppendASCII("file_system"); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 CheckStoredDirectoryMatches(test_file); | 184 CheckStoredDirectoryMatches(test_file); |
| 154 } | 185 } |
| 155 | 186 |
| 156 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 187 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
| 157 FileSystemApiOpenExistingFileUsingPreviousPathTest) { | 188 FileSystemApiOpenExistingFileUsingPreviousPathTest) { |
| 158 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 189 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
| 159 ASSERT_FALSE(test_file.empty()); | 190 ASSERT_FALSE(test_file.empty()); |
| 160 FileSystemChooseEntryFunction:: | 191 FileSystemChooseEntryFunction:: |
| 161 SkipPickerAndSelectSuggestedPathForTest(); | 192 SkipPickerAndSelectSuggestedPathForTest(); |
| 162 { | 193 { |
| 163 AppInstallObserver observer( | 194 SetLastChooseEntryDirectoryObserver observer( |
| 164 test_file.DirName(), | 195 test_file.DirName(), |
| 165 extensions::ExtensionSystem::Get( | 196 extensions::ExtensionSystem::Get( |
| 166 profile())->extension_service()->extension_prefs()); | 197 profile())->extension_service()->extension_prefs()); |
| 167 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) | 198 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) |
| 168 << message_; | 199 << message_; |
| 169 } | 200 } |
| 170 CheckStoredDirectoryMatches(test_file); | 201 CheckStoredDirectoryMatches(test_file); |
| 171 } | 202 } |
| 172 | 203 |
| 173 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 204 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
| 174 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { | 205 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { |
| 175 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 206 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
| 176 ASSERT_FALSE(test_file.empty()); | 207 ASSERT_FALSE(test_file.empty()); |
| 177 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( | 208 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( |
| 178 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false)); | 209 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false)); |
| 179 FileSystemChooseEntryFunction:: | 210 FileSystemChooseEntryFunction:: |
| 180 SkipPickerAndSelectSuggestedPathForTest(); | 211 SkipPickerAndSelectSuggestedPathForTest(); |
| 181 { | 212 { |
| 182 AppInstallObserver observer( | 213 SetLastChooseEntryDirectoryObserver observer( |
| 183 test_file.DirName().Append(base::FilePath::FromUTF8Unsafe( | 214 test_file.DirName().Append(base::FilePath::FromUTF8Unsafe( |
| 184 "fake_directory_does_not_exist")), | 215 "fake_directory_does_not_exist")), |
| 185 extensions::ExtensionSystem::Get( | 216 extensions::ExtensionSystem::Get( |
| 186 profile())->extension_service()->extension_prefs()); | 217 profile())->extension_service()->extension_prefs()); |
| 187 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) | 218 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) |
| 188 << message_; | 219 << message_; |
| 189 } | 220 } |
| 190 CheckStoredDirectoryMatches(test_file); | 221 CheckStoredDirectoryMatches(test_file); |
| 191 } | 222 } |
| 192 | 223 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 | 364 |
| 334 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) { | 365 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) { |
| 335 base::FilePath test_file = TempFilePath("writable.txt", true); | 366 base::FilePath test_file = TempFilePath("writable.txt", true); |
| 336 ASSERT_FALSE(test_file.empty()); | 367 ASSERT_FALSE(test_file.empty()); |
| 337 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( | 368 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 338 &test_file); | 369 &test_file); |
| 339 ASSERT_TRUE(RunPlatformAppTest( | 370 ASSERT_TRUE(RunPlatformAppTest( |
| 340 "api_test/file_system/is_writable_file_entry")) << message_; | 371 "api_test/file_system/is_writable_file_entry")) << message_; |
| 341 } | 372 } |
| 342 | 373 |
| 343 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetEntryId) { | 374 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRetainEntry) { |
| 344 base::FilePath test_file = TempFilePath("writable.txt", true); | 375 base::FilePath test_file = TempFilePath("writable.txt", true); |
| 345 ASSERT_FALSE(test_file.empty()); | 376 ASSERT_FALSE(test_file.empty()); |
| 346 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( | 377 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 347 &test_file); | 378 &test_file); |
| 348 ASSERT_TRUE(RunPlatformAppTest( | 379 ASSERT_TRUE(RunPlatformAppTest( |
| 349 "api_test/file_system/get_entry_id")) << message_; | 380 "api_test/file_system/retain_entry")) << message_; |
| 381 std::vector<extensions::SavedFileEntry> file_entries; |
| 382 extensions::SavedFilesService::Get(profile())->GetFileEntries( |
| 383 GetSingleLoadedExtension()->id(), &file_entries); |
| 384 ASSERT_EQ(1u, file_entries.size()); |
| 385 EXPECT_EQ(test_file, file_entries[0].path); |
| 386 EXPECT_EQ(1, file_entries[0].sequence_number); |
| 387 EXPECT_FALSE(file_entries[0].writable); |
| 350 } | 388 } |
| 389 |
| 390 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreEntry) { |
| 391 base::FilePath test_file = TempFilePath("writable.txt", true); |
| 392 ASSERT_FALSE(test_file.empty()); |
| 393 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 394 &test_file); |
| 395 { |
| 396 AddSavedEntryObserver observer( |
| 397 test_file, extensions::SavedFilesService::Get(profile())); |
| 398 ASSERT_TRUE(RunPlatformAppTest( |
| 399 "api_test/file_system/restore_entry")) << message_; |
| 400 } |
| 401 } |
| OLD | NEW |