file_system_apitest.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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
3// found in the LICENSE file.
4
5#include "apps/saved_files_service.h"
6#include "base/file_util.h"
7#include "base/path_service.h"
8#include "build/build_config.h"
9#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/extensions/api/file_system/file_system_api.h"
11#include "chrome/browser/extensions/extension_prefs.h"
12#include "chrome/browser/extensions/platform_app_browsertest_util.h"
13#include "chrome/common/chrome_paths.h"
14#include "content/public/browser/notification_observer.h"
15#include "content/public/browser/notification_service.h"
16
17namespace extensions {
18
19namespace {
20
21class AppInstallObserver : public content::NotificationObserver {
22 public:
23  AppInstallObserver(
24      base::Callback<void(const Extension*)> callback)
25      : callback_(callback) {
26    registrar_.Add(this,
27                   chrome::NOTIFICATION_EXTENSION_LOADED,
28                   content::NotificationService::AllSources());
29  }
30
31  virtual void Observe(int type,
32                       const content::NotificationSource& source,
33                       const content::NotificationDetails& details) OVERRIDE {
34    EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED, type);
35    callback_.Run(content::Details<const Extension>(details).ptr());
36  }
37
38 private:
39  content::NotificationRegistrar registrar_;
40  base::Callback<void(const Extension*)> callback_;
41  DISALLOW_COPY_AND_ASSIGN(AppInstallObserver);
42};
43
44void SetLastChooseEntryDirectory(const base::FilePath& choose_entry_directory,
45                                 ExtensionPrefs* prefs,
46                                 const Extension* extension) {
47  file_system_api::SetLastChooseEntryDirectory(
48      prefs, extension->id(), choose_entry_directory);
49}
50
51void SetLastChooseEntryDirectoryToAppDirectory(
52    ExtensionPrefs* prefs,
53    const Extension* extension) {
54  file_system_api::SetLastChooseEntryDirectory(
55      prefs, extension->id(), extension->path());
56}
57
58void AddSavedEntry(const base::FilePath& path_to_save,
59                   apps::SavedFilesService* service,
60                   const Extension* extension) {
61  service->RegisterFileEntry(
62      extension->id(), "magic id", path_to_save, /* writable */ true);
63}
64
65}  // namespace
66
67class FileSystemApiTest : public PlatformAppBrowserTest {
68 public:
69  virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
70    PlatformAppBrowserTest::SetUpCommandLine(command_line);
71    test_root_folder_ = test_data_dir_.AppendASCII("api_test")
72        .AppendASCII("file_system");
73    FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
74        "test_root", test_root_folder_);
75  }
76
77  virtual void TearDown() OVERRIDE {
78    FileSystemChooseEntryFunction::StopSkippingPickerForTest();
79    PlatformAppBrowserTest::TearDown();
80  };
81
82 protected:
83  base::FilePath TempFilePath(const std::string& destination_name,
84                              bool copy_gold) {
85    if (!temp_dir_.CreateUniqueTempDir()) {
86      ADD_FAILURE() << "CreateUniqueTempDir failed";
87      return base::FilePath();
88    }
89    FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
90        "test_temp", temp_dir_.path());
91
92    base::FilePath destination = temp_dir_.path().AppendASCII(destination_name);
93    if (copy_gold) {
94      base::FilePath source = test_root_folder_.AppendASCII("gold.txt");
95      EXPECT_TRUE(base::CopyFile(source, destination));
96    }
97    return destination;
98  }
99
100  std::vector<base::FilePath> TempFilePaths(
101      const std::vector<std::string>& destination_names,
102      bool copy_gold) {
103    if (!temp_dir_.CreateUniqueTempDir()) {
104      ADD_FAILURE() << "CreateUniqueTempDir failed";
105      return std::vector<base::FilePath>();
106    }
107    FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
108        "test_temp", temp_dir_.path());
109
110    std::vector<base::FilePath> result;
111    for (std::vector<std::string>::const_iterator it =
112             destination_names.begin();
113         it != destination_names.end(); ++it) {
114      base::FilePath destination = temp_dir_.path().AppendASCII(*it);
115      if (copy_gold) {
116        base::FilePath source = test_root_folder_.AppendASCII("gold.txt");
117        EXPECT_TRUE(base::CopyFile(source, destination));
118      }
119      result.push_back(destination);
120    }
121    return result;
122  }
123
124  void CheckStoredDirectoryMatches(const base::FilePath& filename) {
125    const Extension* extension = GetSingleLoadedExtension();
126    ASSERT_TRUE(extension);
127    std::string extension_id = extension->id();
128    ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
129    base::FilePath stored_value;
130    if (filename.empty()) {
131      EXPECT_FALSE(file_system_api::GetLastChooseEntryDirectory(
132          prefs, extension_id, &stored_value));
133    } else {
134      EXPECT_TRUE(file_system_api::GetLastChooseEntryDirectory(
135          prefs, extension_id, &stored_value));
136      EXPECT_EQ(base::MakeAbsoluteFilePath(filename.DirName()),
137                base::MakeAbsoluteFilePath(stored_value));
138    }
139  }
140
141  base::FilePath test_root_folder_;
142  base::ScopedTempDir temp_dir_;
143};
144
145IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetDisplayPath) {
146  base::FilePath test_file = test_root_folder_.AppendASCII("gold.txt");
147  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
148      &test_file);
149  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/get_display_path"))
150      << message_;
151}
152
153#if defined(OS_WIN) || defined(OS_POSIX)
154IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetDisplayPathPrettify) {
155#if defined(OS_WIN)
156  int override = base::DIR_PROFILE;
157#elif defined(OS_POSIX)
158  int override = base::DIR_HOME;
159#endif
160  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(override,
161      test_root_folder_, false));
162
163  base::FilePath test_file = test_root_folder_.AppendASCII("gold.txt");
164  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
165      &test_file);
166  ASSERT_TRUE(RunPlatformAppTest(
167      "api_test/file_system/get_display_path_prettify")) << message_;
168}
169#endif
170
171#if defined(OS_MACOSX)
172IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
173    FileSystemApiGetDisplayPathPrettifyMac) {
174  // On Mac, "test.localized" will be localized into just "test".
175  base::FilePath test_path = TempFilePath("test.localized", false);
176  ASSERT_TRUE(file_util::CreateDirectory(test_path));
177
178  base::FilePath test_file = test_path.AppendASCII("gold.txt");
179  base::FilePath source = test_root_folder_.AppendASCII("gold.txt");
180  EXPECT_TRUE(base::CopyFile(source, test_file));
181
182  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
183      &test_file);
184  ASSERT_TRUE(RunPlatformAppTest(
185      "api_test/file_system/get_display_path_prettify_mac")) << message_;
186}
187#endif
188
189IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenExistingFileTest) {
190  base::FilePath test_file = TempFilePath("open_existing.txt", true);
191  ASSERT_FALSE(test_file.empty());
192  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
193      &test_file);
194  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
195      << message_;
196  CheckStoredDirectoryMatches(test_file);
197}
198
199IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
200                       FileSystemApiOpenExistingFileUsingPreviousPathTest) {
201  base::FilePath test_file = TempFilePath("open_existing.txt", true);
202  ASSERT_FALSE(test_file.empty());
203  FileSystemChooseEntryFunction::
204      SkipPickerAndSelectSuggestedPathForTest();
205  {
206    AppInstallObserver observer(
207        base::Bind(SetLastChooseEntryDirectory,
208                   test_file.DirName(),
209                   ExtensionPrefs::Get(profile())));
210    ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
211        << message_;
212  }
213  CheckStoredDirectoryMatches(test_file);
214}
215
216IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
217    FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) {
218  base::FilePath test_file = TempFilePath("open_existing.txt", true);
219  ASSERT_FALSE(test_file.empty());
220  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
221      chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false));
222  FileSystemChooseEntryFunction::
223      SkipPickerAndSelectSuggestedPathForTest();
224  {
225    AppInstallObserver observer(base::Bind(
226        SetLastChooseEntryDirectory,
227        test_file.DirName().Append(
228            base::FilePath::FromUTF8Unsafe("fake_directory_does_not_exist")),
229        ExtensionPrefs::Get(profile())));
230    ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
231        << message_;
232  }
233  CheckStoredDirectoryMatches(test_file);
234}
235
236IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
237                       FileSystemApiOpenExistingFileDefaultPathTest) {
238  base::FilePath test_file = TempFilePath("open_existing.txt", true);
239  ASSERT_FALSE(test_file.empty());
240  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
241      chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false));
242  FileSystemChooseEntryFunction::
243      SkipPickerAndSelectSuggestedPathForTest();
244  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
245      << message_;
246  CheckStoredDirectoryMatches(test_file);
247}
248
249IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenMultipleSuggested) {
250  base::FilePath test_file = TempFilePath("open_existing.txt", true);
251  ASSERT_FALSE(test_file.empty());
252  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
253      chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false));
254  FileSystemChooseEntryFunction::SkipPickerAndSelectSuggestedPathForTest();
255  ASSERT_TRUE(RunPlatformAppTest(
256      "api_test/file_system/open_multiple_with_suggested_name"))
257      << message_;
258  CheckStoredDirectoryMatches(test_file);
259}
260
261IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
262                       FileSystemApiOpenMultipleExistingFilesTest) {
263  std::vector<std::string> names;
264  names.push_back("open_existing1.txt");
265  names.push_back("open_existing2.txt");
266  std::vector<base::FilePath> test_files = TempFilePaths(names, true);
267  ASSERT_EQ(2u, test_files.size());
268  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
269      &test_files);
270  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_multiple_existing"))
271      << message_;
272}
273
274IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
275    FileSystemApiInvalidChooseEntryTypeTest) {
276  base::FilePath test_file = TempFilePath("open_existing.txt", true);
277  ASSERT_FALSE(test_file.empty());
278  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
279      &test_file);
280  ASSERT_TRUE(RunPlatformAppTest(
281      "api_test/file_system/invalid_choose_file_type")) << message_;
282  CheckStoredDirectoryMatches(base::FilePath());
283}
284
285// http://crbug.com/177163
286#if defined(OS_WIN) && !defined(NDEBUG)
287#define MAYBE_FileSystemApiOpenExistingFileWithWriteTest DISABLED_FileSystemApiOpenExistingFileWithWriteTest
288#else
289#define MAYBE_FileSystemApiOpenExistingFileWithWriteTest FileSystemApiOpenExistingFileWithWriteTest
290#endif
291IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
292    MAYBE_FileSystemApiOpenExistingFileWithWriteTest) {
293  base::FilePath test_file = TempFilePath("open_existing.txt", true);
294  ASSERT_FALSE(test_file.empty());
295  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
296      &test_file);
297  ASSERT_TRUE(RunPlatformAppTest(
298      "api_test/file_system/open_existing_with_write")) << message_;
299  CheckStoredDirectoryMatches(test_file);
300}
301
302IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
303    FileSystemApiOpenWritableExistingFileTest) {
304  base::FilePath test_file = TempFilePath("open_existing.txt", true);
305  ASSERT_FALSE(test_file.empty());
306  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
307      &test_file);
308  ASSERT_TRUE(RunPlatformAppTest(
309      "api_test/file_system/open_writable_existing")) << message_;
310  CheckStoredDirectoryMatches(base::FilePath());
311}
312
313IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
314    FileSystemApiOpenWritableExistingFileWithWriteTest) {
315  base::FilePath test_file = TempFilePath("open_existing.txt", true);
316  ASSERT_FALSE(test_file.empty());
317  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
318      &test_file);
319  ASSERT_TRUE(RunPlatformAppTest(
320      "api_test/file_system/open_writable_existing_with_write")) << message_;
321  CheckStoredDirectoryMatches(test_file);
322}
323
324IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
325                       FileSystemApiOpenMultipleWritableExistingFilesTest) {
326  std::vector<std::string> names;
327  names.push_back("open_existing1.txt");
328  names.push_back("open_existing2.txt");
329  std::vector<base::FilePath> test_files = TempFilePaths(names, true);
330  ASSERT_EQ(2u, test_files.size());
331  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
332      &test_files);
333  ASSERT_TRUE(RunPlatformAppTest(
334      "api_test/file_system/open_multiple_writable_existing_with_write"))
335      << message_;
336}
337
338IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenCancelTest) {
339  FileSystemChooseEntryFunction::SkipPickerAndAlwaysCancelForTest();
340  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_cancel"))
341      << message_;
342  CheckStoredDirectoryMatches(base::FilePath());
343}
344
345IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenBackgroundTest) {
346  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_background"))
347      << message_;
348}
349
350IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveNewFileTest) {
351  base::FilePath test_file = TempFilePath("save_new.txt", false);
352  ASSERT_FALSE(test_file.empty());
353  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
354      &test_file);
355  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_new"))
356      << message_;
357  CheckStoredDirectoryMatches(base::FilePath());
358}
359
360IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveExistingFileTest) {
361  base::FilePath test_file = TempFilePath("save_existing.txt", true);
362  ASSERT_FALSE(test_file.empty());
363  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
364      &test_file);
365  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_existing"))
366      << message_;
367  CheckStoredDirectoryMatches(base::FilePath());
368}
369
370IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
371    FileSystemApiSaveNewFileWithWriteTest) {
372  base::FilePath test_file = TempFilePath("save_new.txt", false);
373  ASSERT_FALSE(test_file.empty());
374  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
375      &test_file);
376  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_new_with_write"))
377      << message_;
378  CheckStoredDirectoryMatches(test_file);
379}
380
381IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
382    FileSystemApiSaveExistingFileWithWriteTest) {
383  base::FilePath test_file = TempFilePath("save_existing.txt", true);
384  ASSERT_FALSE(test_file.empty());
385  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
386      &test_file);
387  ASSERT_TRUE(RunPlatformAppTest(
388      "api_test/file_system/save_existing_with_write")) << message_;
389  CheckStoredDirectoryMatches(test_file);
390}
391
392IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveMultipleFilesTest) {
393  std::vector<std::string> names;
394  names.push_back("save1.txt");
395  names.push_back("save2.txt");
396  std::vector<base::FilePath> test_files = TempFilePaths(names, false);
397  ASSERT_EQ(2u, test_files.size());
398  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
399      &test_files);
400  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_multiple"))
401      << message_;
402}
403
404IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveCancelTest) {
405  FileSystemChooseEntryFunction::SkipPickerAndAlwaysCancelForTest();
406  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_cancel"))
407      << message_;
408}
409
410IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveBackgroundTest) {
411  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_background"))
412      << message_;
413}
414
415IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetWritableTest) {
416  base::FilePath test_file = TempFilePath("writable.txt", true);
417  ASSERT_FALSE(test_file.empty());
418  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
419      &test_file);
420  ASSERT_TRUE(RunPlatformAppTest(
421      "api_test/file_system/get_writable_file_entry")) << message_;
422}
423
424IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
425    FileSystemApiGetWritableWithWriteTest) {
426  base::FilePath test_file = TempFilePath("writable.txt", true);
427  ASSERT_FALSE(test_file.empty());
428  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
429      &test_file);
430  ASSERT_TRUE(RunPlatformAppTest(
431      "api_test/file_system/get_writable_file_entry_with_write")) << message_;
432}
433
434IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
435                       FileSystemApiGetWritableInUserDataDirTest) {
436  base::FilePath test_file =
437      base::MakeAbsoluteFilePath(TempFilePath("test.js", true));
438  ASSERT_FALSE(test_file.empty());
439  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
440      &test_file);
441  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
442      chrome::DIR_USER_DATA, test_file.DirName(), false));
443  ASSERT_TRUE(RunPlatformAppTest(
444      "api_test/file_system/get_writable_file_entry_non_writable_file"))
445      << message_;
446}
447
448IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
449                       FileSystemApiGetWritableInChromeDirTest) {
450  base::FilePath test_file =
451      base::MakeAbsoluteFilePath(TempFilePath("test.js", true));
452  ASSERT_FALSE(test_file.empty());
453  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
454      &test_file);
455  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
456      chrome::DIR_APP, test_file.DirName(), false));
457  ASSERT_TRUE(RunPlatformAppTest(
458      "api_test/file_system/get_writable_file_entry_non_writable_file"))
459      << message_;
460}
461
462IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
463                       FileSystemApiGetWritableInAppDirectory) {
464  FileSystemChooseEntryFunction::SkipPickerAndSelectSuggestedPathForTest();
465  {
466    AppInstallObserver observer(
467        base::Bind(SetLastChooseEntryDirectoryToAppDirectory,
468                   ExtensionPrefs::Get(profile())));
469    ASSERT_TRUE(RunPlatformAppTest(
470        "api_test/file_system/get_writable_file_entry_non_writable_file"))
471        << message_;
472  }
473}
474
475IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiIsWritableTest) {
476  base::FilePath test_file = TempFilePath("writable.txt", true);
477  ASSERT_FALSE(test_file.empty());
478  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
479      &test_file);
480  ASSERT_TRUE(RunPlatformAppTest(
481      "api_test/file_system/is_writable_file_entry")) << message_;
482}
483
484IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRetainEntry) {
485  base::FilePath test_file = TempFilePath("writable.txt", true);
486  ASSERT_FALSE(test_file.empty());
487  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
488      &test_file);
489  ASSERT_TRUE(RunPlatformAppTest(
490      "api_test/file_system/retain_entry")) << message_;
491  std::vector<apps::SavedFileEntry> file_entries = apps::SavedFilesService::Get(
492      profile())->GetAllFileEntries(GetSingleLoadedExtension()->id());
493  ASSERT_EQ(1u, file_entries.size());
494  EXPECT_EQ(test_file, file_entries[0].path);
495  EXPECT_EQ(1, file_entries[0].sequence_number);
496  EXPECT_FALSE(file_entries[0].writable);
497}
498
499IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreEntry) {
500  base::FilePath test_file = TempFilePath("writable.txt", true);
501  ASSERT_FALSE(test_file.empty());
502  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
503      &test_file);
504  {
505    AppInstallObserver observer(base::Bind(
506        AddSavedEntry, test_file, apps::SavedFilesService::Get(profile())));
507    ASSERT_TRUE(RunPlatformAppTest(
508        "api_test/file_system/restore_entry")) << message_;
509  }
510}
511
512IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
513                       FileSystemApiOpenNonWritableFileForRead) {
514  base::FilePath test_file = TempFilePath("open_existing.txt", true);
515  ASSERT_FALSE(test_file.empty());
516  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
517      chrome::DIR_USER_DATA, test_file.DirName(), false));
518  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
519      &test_file);
520  ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
521      << message_;
522}
523
524IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
525                       FileSystemApiOpenInUserDataDirForWrite) {
526  base::FilePath test_file =
527      base::MakeAbsoluteFilePath(TempFilePath("open_existing.txt", true));
528  ASSERT_FALSE(test_file.empty());
529  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
530      chrome::DIR_USER_DATA, test_file.DirName(), false));
531  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
532      &test_file);
533  ASSERT_TRUE(RunPlatformAppTest(
534      "api_test/file_system/open_writable_existing_non_writable")) << message_;
535}
536
537#if defined(OS_CHROMEOS)
538// In Chrome OS the download directory is whitelisted for write.
539IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
540                       FileSystemApiOpenInDownloadDirForWrite) {
541  base::FilePath test_file =
542      base::MakeAbsoluteFilePath(TempFilePath("writable.txt", true));
543  ASSERT_FALSE(test_file.empty());
544  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
545      chrome::DIR_USER_DATA, test_file.DirName(), false));
546  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
547      chrome::DIR_DEFAULT_DOWNLOADS_SAFE, test_file.DirName(), false));
548  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
549      &test_file);
550  ASSERT_TRUE(RunPlatformAppTest(
551      "api_test/file_system/is_writable_file_entry")) << message_;
552}
553#endif
554
555IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
556                       FileSystemApiOpenInChromeDirForWrite) {
557  base::FilePath test_file =
558      base::MakeAbsoluteFilePath(TempFilePath("open_existing.txt", true));
559  ASSERT_FALSE(test_file.empty());
560  ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
561      chrome::DIR_APP, test_file.DirName(), false));
562  FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
563      &test_file);
564  ASSERT_TRUE(RunPlatformAppTest(
565      "api_test/file_system/open_writable_existing_non_writable")) << message_;
566}
567
568IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
569                       FileSystemApiOpenInAppDirectoryForWrite) {
570  FileSystemChooseEntryFunction::SkipPickerAndSelectSuggestedPathForTest();
571  {
572    AppInstallObserver observer(
573        base::Bind(SetLastChooseEntryDirectoryToAppDirectory,
574                   ExtensionPrefs::Get(profile())));
575    ASSERT_TRUE(RunPlatformAppTest(
576        "api_test/file_system/open_writable_existing_non_writable"))
577        << message_;
578  }
579}
580
581}  // namespace extensions
582