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