Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1059)

Side by Side Diff: chrome/browser/extensions/api/file_system/file_system_apitest.cc

Issue 14607023: Add support for persistent file access in apps. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: rebase Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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(const base::FilePath& choose_entry_directory,
44 extensions::ExtensionPrefs* prefs,
45 const extensions::Extension* extension) {
46 prefs->SetLastChooseEntryDirectory(extension->id(), choose_entry_directory);
47 }
48
49 void AddSavedEntry(const base::FilePath& path_to_save,
50 apps::SavedFilesService* service,
51 const extensions::Extension* extension) {
52 service->RegisterFileEntry(
53 extension->id(), "magic id", path_to_save, /* writable */ true);
54 }
55
45 } // namespace 56 } // namespace
46 57
47 class FileSystemApiTest : public extensions::PlatformAppBrowserTest { 58 class FileSystemApiTest : public extensions::PlatformAppBrowserTest {
48 public: 59 public:
49 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 60 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
50 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line); 61 extensions::PlatformAppBrowserTest::SetUpCommandLine(command_line);
51 test_root_folder_ = test_data_dir_.AppendASCII("api_test") 62 test_root_folder_ = test_data_dir_.AppendASCII("api_test")
52 .AppendASCII("file_system"); 63 .AppendASCII("file_system");
53 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest( 64 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
54 "test_root", test_root_folder_); 65 "test_root", test_root_folder_);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 } 165 }
155 166
156 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, 167 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
157 FileSystemApiOpenExistingFileUsingPreviousPathTest) { 168 FileSystemApiOpenExistingFileUsingPreviousPathTest) {
158 base::FilePath test_file = TempFilePath("open_existing.txt", true); 169 base::FilePath test_file = TempFilePath("open_existing.txt", true);
159 ASSERT_FALSE(test_file.empty()); 170 ASSERT_FALSE(test_file.empty());
160 FileSystemChooseEntryFunction:: 171 FileSystemChooseEntryFunction::
161 SkipPickerAndSelectSuggestedPathForTest(); 172 SkipPickerAndSelectSuggestedPathForTest();
162 { 173 {
163 AppInstallObserver observer( 174 AppInstallObserver observer(
164 test_file.DirName(), 175 base::Bind(SetLastChooseEntryDirectory,
165 extensions::ExtensionSystem::Get( 176 test_file.DirName(),
166 profile())->extension_service()->extension_prefs()); 177 extensions::ExtensionSystem::Get(
178 profile())->extension_service()->extension_prefs()));
167 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) 179 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
168 << message_; 180 << message_;
169 } 181 }
170 CheckStoredDirectoryMatches(test_file); 182 CheckStoredDirectoryMatches(test_file);
171 } 183 }
172 184
173 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, 185 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
174 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { 186 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) {
175 base::FilePath test_file = TempFilePath("open_existing.txt", true); 187 base::FilePath test_file = TempFilePath("open_existing.txt", true);
176 ASSERT_FALSE(test_file.empty()); 188 ASSERT_FALSE(test_file.empty());
177 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( 189 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
178 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false)); 190 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false));
179 FileSystemChooseEntryFunction:: 191 FileSystemChooseEntryFunction::
180 SkipPickerAndSelectSuggestedPathForTest(); 192 SkipPickerAndSelectSuggestedPathForTest();
181 { 193 {
182 AppInstallObserver observer( 194 AppInstallObserver observer(base::Bind(
183 test_file.DirName().Append(base::FilePath::FromUTF8Unsafe( 195 SetLastChooseEntryDirectory,
184 "fake_directory_does_not_exist")), 196 test_file.DirName().Append(
197 base::FilePath::FromUTF8Unsafe("fake_directory_does_not_exist")),
185 extensions::ExtensionSystem::Get( 198 extensions::ExtensionSystem::Get(
186 profile())->extension_service()->extension_prefs()); 199 profile())->extension_service()->extension_prefs()));
187 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) 200 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
188 << message_; 201 << message_;
189 } 202 }
190 CheckStoredDirectoryMatches(test_file); 203 CheckStoredDirectoryMatches(test_file);
191 } 204 }
192 205
193 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, 206 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
194 FileSystemApiOpenExistingFileDefaultPathTest) { 207 FileSystemApiOpenExistingFileDefaultPathTest) {
195 base::FilePath test_file = TempFilePath("open_existing.txt", true); 208 base::FilePath test_file = TempFilePath("open_existing.txt", true);
196 ASSERT_FALSE(test_file.empty()); 209 ASSERT_FALSE(test_file.empty());
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 346
334 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) { 347 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) {
335 base::FilePath test_file = TempFilePath("writable.txt", true); 348 base::FilePath test_file = TempFilePath("writable.txt", true);
336 ASSERT_FALSE(test_file.empty()); 349 ASSERT_FALSE(test_file.empty());
337 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( 350 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
338 &test_file); 351 &test_file);
339 ASSERT_TRUE(RunPlatformAppTest( 352 ASSERT_TRUE(RunPlatformAppTest(
340 "api_test/file_system/is_writable_file_entry")) << message_; 353 "api_test/file_system/is_writable_file_entry")) << message_;
341 } 354 }
342 355
343 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetEntryId) { 356 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRetainEntry) {
344 base::FilePath test_file = TempFilePath("writable.txt", true); 357 base::FilePath test_file = TempFilePath("writable.txt", true);
345 ASSERT_FALSE(test_file.empty()); 358 ASSERT_FALSE(test_file.empty());
346 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( 359 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
347 &test_file); 360 &test_file);
348 ASSERT_TRUE(RunPlatformAppTest( 361 ASSERT_TRUE(RunPlatformAppTest(
349 "api_test/file_system/get_entry_id")) << message_; 362 "api_test/file_system/retain_entry")) << message_;
363 std::vector<apps::SavedFileEntry> file_entries = apps::SavedFilesService::Get(
364 profile())->GetAllFileEntries(GetSingleLoadedExtension()->id());
365 ASSERT_EQ(1u, file_entries.size());
366 EXPECT_EQ(test_file, file_entries[0].path);
367 EXPECT_EQ(1, file_entries[0].sequence_number);
368 EXPECT_FALSE(file_entries[0].writable);
350 } 369 }
370
371 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreEntry) {
372 base::FilePath test_file = TempFilePath("writable.txt", true);
373 ASSERT_FALSE(test_file.empty());
374 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
375 &test_file);
376 {
377 AppInstallObserver observer(base::Bind(
378 AddSavedEntry, test_file, apps::SavedFilesService::Get(profile())));
379 ASSERT_TRUE(RunPlatformAppTest(
380 "api_test/file_system/restore_entry")) << message_;
381 }
382 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698