| 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 "apps/saved_files_service.h" |
| 5 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 7 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 8 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 9 #include "chrome/browser/extensions/api/file_system/file_system_api.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 AppInstallObserver : public content::NotificationObserver { |
| 20 public: | 21 public: |
| 21 AppInstallObserver(const base::FilePath& choose_entry_directory, | 22 AppInstallObserver( |
| 22 extensions::ExtensionPrefs* prefs) | 23 base::Callback<void(const extensions::Extension*)> callback) |
| 23 : path_(choose_entry_directory), | 24 : callback_(callback) { |
| 24 prefs_(prefs) { | 25 registrar_.Add(this, |
| 25 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 26 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 26 content::NotificationService::AllSources()); | 27 content::NotificationService::AllSources()); |
| 27 } | 28 } |
| 28 | 29 |
| 29 virtual void Observe(int type, | 30 virtual void Observe(int type, |
| 30 const content::NotificationSource& source, | 31 const content::NotificationSource& source, |
| 31 const content::NotificationDetails& details) OVERRIDE { | 32 const content::NotificationDetails& details) OVERRIDE { |
| 32 EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED, type); | 33 EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED, type); |
| 33 std::string extension_id = content::Details<const extensions::Extension>( | 34 callback_.Run(content::Details<const extensions::Extension>(details).ptr()); |
| 34 details).ptr()->id(); | |
| 35 prefs_->SetLastChooseEntryDirectory(extension_id, path_); | |
| 36 } | 35 } |
| 37 | 36 |
| 38 private: | 37 private: |
| 39 content::NotificationRegistrar registrar_; | 38 content::NotificationRegistrar registrar_; |
| 40 const base::FilePath path_; | 39 base::Callback<void(const extensions::Extension*)> callback_; |
| 41 extensions::ExtensionPrefs* prefs_; | |
| 42 DISALLOW_COPY_AND_ASSIGN(AppInstallObserver); | 40 DISALLOW_COPY_AND_ASSIGN(AppInstallObserver); |
| 43 }; | 41 }; |
| 44 | 42 |
| 43 void SetLastChooseEntryDirectory( |
| 44 const base::FilePath& choose_entry_directory, |
| 45 extensions::ExtensionPrefs* prefs, |
| 46 const extensions::Extension* extension) { |
| 47 prefs->SetLastChooseEntryDirectory(extension->id(), choose_entry_directory); |
| 48 } |
| 49 |
| 50 void AddSavedEntry( |
| 51 const base::FilePath& path_to_save, |
| 52 apps::SavedFilesService* service, |
| 53 const extensions::Extension* extension) { |
| 54 service->RetainFileEntry(extension->id(), "magic id", path_to_save, true); |
| 55 } |
| 56 |
| 45 } // namespace | 57 } // namespace |
| 46 | 58 |
| 47 class FileSystemApiTest : public extensions::PlatformAppBrowserTest { | 59 class FileSystemApiTest : public extensions::PlatformAppBrowserTest { |
| 48 public: | 60 public: |
| 49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | 61 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 50 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); | 62 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); |
| 51 test_root_folder_ = test_data_dir_.AppendASCII("api_test") | 63 test_root_folder_ = test_data_dir_.AppendASCII("api_test") |
| 52 .AppendASCII("file_system"); | 64 .AppendASCII("file_system"); |
| 53 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest( | 65 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest( |
| 54 "test_root", test_root_folder_); | 66 "test_root", test_root_folder_); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 166 } |
| 155 | 167 |
| 156 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 168 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
| 157 FileSystemApiOpenExistingFileUsingPreviousPathTest) { | 169 FileSystemApiOpenExistingFileUsingPreviousPathTest) { |
| 158 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 170 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
| 159 ASSERT_FALSE(test_file.empty()); | 171 ASSERT_FALSE(test_file.empty()); |
| 160 FileSystemChooseEntryFunction:: | 172 FileSystemChooseEntryFunction:: |
| 161 SkipPickerAndSelectSuggestedPathForTest(); | 173 SkipPickerAndSelectSuggestedPathForTest(); |
| 162 { | 174 { |
| 163 AppInstallObserver observer( | 175 AppInstallObserver observer( |
| 164 test_file.DirName(), | 176 base::Bind(SetLastChooseEntryDirectory, |
| 165 extensions::ExtensionSystem::Get( | 177 test_file.DirName(), |
| 166 profile())->extension_service()->extension_prefs()); | 178 extensions::ExtensionSystem::Get( |
| 179 profile())->extension_service()->extension_prefs())); |
| 167 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) | 180 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) |
| 168 << message_; | 181 << message_; |
| 169 } | 182 } |
| 170 CheckStoredDirectoryMatches(test_file); | 183 CheckStoredDirectoryMatches(test_file); |
| 171 } | 184 } |
| 172 | 185 |
| 173 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 186 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
| 174 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { | 187 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { |
| 175 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 188 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
| 176 ASSERT_FALSE(test_file.empty()); | 189 ASSERT_FALSE(test_file.empty()); |
| 177 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( | 190 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( |
| 178 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false)); | 191 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false)); |
| 179 FileSystemChooseEntryFunction:: | 192 FileSystemChooseEntryFunction:: |
| 180 SkipPickerAndSelectSuggestedPathForTest(); | 193 SkipPickerAndSelectSuggestedPathForTest(); |
| 181 { | 194 { |
| 182 AppInstallObserver observer( | 195 AppInstallObserver observer(base::Bind( |
| 183 test_file.DirName().Append(base::FilePath::FromUTF8Unsafe( | 196 SetLastChooseEntryDirectory, |
| 184 "fake_directory_does_not_exist")), | 197 test_file.DirName().Append( |
| 198 base::FilePath::FromUTF8Unsafe("fake_directory_does_not_exist")), |
| 185 extensions::ExtensionSystem::Get( | 199 extensions::ExtensionSystem::Get( |
| 186 profile())->extension_service()->extension_prefs()); | 200 profile())->extension_service()->extension_prefs())); |
| 187 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) | 201 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) |
| 188 << message_; | 202 << message_; |
| 189 } | 203 } |
| 190 CheckStoredDirectoryMatches(test_file); | 204 CheckStoredDirectoryMatches(test_file); |
| 191 } | 205 } |
| 192 | 206 |
| 193 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 207 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
| 194 FileSystemApiOpenExistingFileDefaultPathTest) { | 208 FileSystemApiOpenExistingFileDefaultPathTest) { |
| 195 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 209 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
| 196 ASSERT_FALSE(test_file.empty()); | 210 ASSERT_FALSE(test_file.empty()); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 | 347 |
| 334 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) { | 348 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) { |
| 335 base::FilePath test_file = TempFilePath("writable.txt", true); | 349 base::FilePath test_file = TempFilePath("writable.txt", true); |
| 336 ASSERT_FALSE(test_file.empty()); | 350 ASSERT_FALSE(test_file.empty()); |
| 337 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( | 351 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 338 &test_file); | 352 &test_file); |
| 339 ASSERT_TRUE(RunPlatformAppTest( | 353 ASSERT_TRUE(RunPlatformAppTest( |
| 340 "api_test/file_system/is_writable_file_entry")) << message_; | 354 "api_test/file_system/is_writable_file_entry")) << message_; |
| 341 } | 355 } |
| 342 | 356 |
| 343 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetEntryId) { | 357 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRetainEntry) { |
| 344 base::FilePath test_file = TempFilePath("writable.txt", true); | 358 base::FilePath test_file = TempFilePath("writable.txt", true); |
| 345 ASSERT_FALSE(test_file.empty()); | 359 ASSERT_FALSE(test_file.empty()); |
| 346 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( | 360 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 347 &test_file); | 361 &test_file); |
| 348 ASSERT_TRUE(RunPlatformAppTest( | 362 ASSERT_TRUE(RunPlatformAppTest( |
| 349 "api_test/file_system/get_entry_id")) << message_; | 363 "api_test/file_system/retain_entry")) << message_; |
| 364 std::vector<apps::SavedFileEntry> file_entries = apps::SavedFilesService::Get( |
| 365 profile())->GetAllFileEntries(GetSingleLoadedExtension()->id()); |
| 366 ASSERT_EQ(1u, file_entries.size()); |
| 367 EXPECT_EQ(test_file, file_entries[0].path); |
| 368 EXPECT_EQ(1, file_entries[0].sequence_number); |
| 369 EXPECT_FALSE(file_entries[0].writable); |
| 350 } | 370 } |
| 371 |
| 372 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreEntry) { |
| 373 base::FilePath test_file = TempFilePath("writable.txt", true); |
| 374 ASSERT_FALSE(test_file.empty()); |
| 375 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 376 &test_file); |
| 377 { |
| 378 AppInstallObserver observer(base::Bind( |
| 379 AddSavedEntry, test_file, apps::SavedFilesService::Get(profile()))); |
| 380 ASSERT_TRUE(RunPlatformAppTest( |
| 381 "api_test/file_system/restore_entry")) << message_; |
| 382 } |
| 383 } |
| OLD | NEW |