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