Update To 11.40.268.0
[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/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
18 namespace content {
19 class BrowserContext;
20 }
21
22 namespace extensions {
23
24 namespace {
25
26 class 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   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
46 void 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
53 void 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
61 const int kGraylistedPath = base::DIR_HOME;
62
63 }  // namespace
64
65 class FileSystemApiTest : public PlatformAppBrowserTest {
66  public:
67   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   void SetUpOnMainThread() override { ClearCommandLineArgs(); }
76
77   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         file_system_api::GetLastChooseEntryDirectory(prefs, extension_id);
131     if (filename.empty()) {
132       EXPECT_TRUE(stored_value.empty());
133     } else {
134       EXPECT_EQ(base::MakeAbsoluteFilePath(filename.DirName()),
135                 base::MakeAbsoluteFilePath(stored_value));
136     }
137   }
138
139   base::FilePath test_root_folder_;
140   base::ScopedTempDir temp_dir_;
141 };
142
143 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetDisplayPath) {
144   base::FilePath test_file = test_root_folder_.AppendASCII("gold.txt");
145   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
146       &test_file);
147   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/get_display_path"))
148       << message_;
149 }
150
151 #if defined(OS_WIN) || defined(OS_POSIX)
152 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetDisplayPathPrettify) {
153   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(base::DIR_HOME,
154       test_root_folder_, false, false));
155
156   base::FilePath test_file = test_root_folder_.AppendASCII("gold.txt");
157   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
158       &test_file);
159   ASSERT_TRUE(RunPlatformAppTest(
160       "api_test/file_system/get_display_path_prettify")) << message_;
161 }
162 #endif
163
164 #if defined(OS_MACOSX)
165 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
166     FileSystemApiGetDisplayPathPrettifyMac) {
167   // On Mac, "test.localized" will be localized into just "test".
168   base::FilePath test_path = TempFilePath("test.localized", false);
169   ASSERT_TRUE(base::CreateDirectory(test_path));
170
171   base::FilePath test_file = test_path.AppendASCII("gold.txt");
172   base::FilePath source = test_root_folder_.AppendASCII("gold.txt");
173   EXPECT_TRUE(base::CopyFile(source, test_file));
174
175   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
176       &test_file);
177   ASSERT_TRUE(RunPlatformAppTest(
178       "api_test/file_system/get_display_path_prettify_mac")) << message_;
179 }
180 #endif
181
182 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenExistingFileTest) {
183   base::FilePath test_file = TempFilePath("open_existing.txt", true);
184   ASSERT_FALSE(test_file.empty());
185   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
186       &test_file);
187   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
188       << message_;
189   CheckStoredDirectoryMatches(test_file);
190 }
191
192 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
193                        FileSystemApiOpenExistingFileUsingPreviousPathTest) {
194   base::FilePath test_file = TempFilePath("open_existing.txt", true);
195   ASSERT_FALSE(test_file.empty());
196   FileSystemChooseEntryFunction::
197       SkipPickerAndSelectSuggestedPathForTest();
198   {
199     AppLoadObserver observer(profile(),
200                              base::Bind(SetLastChooseEntryDirectory,
201                                         test_file.DirName(),
202                                         ExtensionPrefs::Get(profile())));
203     ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
204         << message_;
205   }
206   CheckStoredDirectoryMatches(test_file);
207 }
208
209 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
210     FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) {
211   base::FilePath test_file = TempFilePath("open_existing.txt", true);
212   ASSERT_FALSE(test_file.empty());
213   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
214       chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false, false));
215   FileSystemChooseEntryFunction::
216       SkipPickerAndSelectSuggestedPathForTest();
217   {
218     AppLoadObserver observer(
219         profile(),
220         base::Bind(SetLastChooseEntryDirectory,
221                    test_file.DirName().Append(base::FilePath::FromUTF8Unsafe(
222                        "fake_directory_does_not_exist")),
223                    ExtensionPrefs::Get(profile())));
224     ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
225         << message_;
226   }
227   CheckStoredDirectoryMatches(test_file);
228 }
229
230 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
231                        FileSystemApiOpenExistingFileDefaultPathTest) {
232   base::FilePath test_file = TempFilePath("open_existing.txt", true);
233   ASSERT_FALSE(test_file.empty());
234   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
235       chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false, false));
236   FileSystemChooseEntryFunction::
237       SkipPickerAndSelectSuggestedPathForTest();
238   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing"))
239       << message_;
240   CheckStoredDirectoryMatches(test_file);
241 }
242
243 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenMultipleSuggested) {
244   base::FilePath test_file = TempFilePath("open_existing.txt", true);
245   ASSERT_FALSE(test_file.empty());
246   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
247       chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false, false));
248   FileSystemChooseEntryFunction::SkipPickerAndSelectSuggestedPathForTest();
249   ASSERT_TRUE(RunPlatformAppTest(
250       "api_test/file_system/open_multiple_with_suggested_name"))
251       << message_;
252   CheckStoredDirectoryMatches(test_file);
253 }
254
255 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
256                        FileSystemApiOpenMultipleExistingFilesTest) {
257   std::vector<std::string> names;
258   names.push_back("open_existing1.txt");
259   names.push_back("open_existing2.txt");
260   std::vector<base::FilePath> test_files = TempFilePaths(names, true);
261   ASSERT_EQ(2u, test_files.size());
262   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
263       &test_files);
264   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_multiple_existing"))
265       << message_;
266 }
267
268 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenDirectoryTest) {
269   base::FilePath test_file = TempFilePath("open_existing.txt", true);
270   ASSERT_FALSE(test_file.empty());
271   base::FilePath test_directory = test_file.DirName();
272   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
273       &test_directory);
274   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
275       << message_;
276   CheckStoredDirectoryMatches(test_file);
277 }
278
279 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
280                        FileSystemApiOpenDirectoryWithWriteTest) {
281   base::FilePath test_file = TempFilePath("open_existing.txt", true);
282   ASSERT_FALSE(test_file.empty());
283   base::FilePath test_directory = test_file.DirName();
284   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
285       &test_directory);
286   ASSERT_TRUE(
287       RunPlatformAppTest("api_test/file_system/open_directory_with_write"))
288       << message_;
289   CheckStoredDirectoryMatches(test_file);
290 }
291
292 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
293                        FileSystemApiOpenDirectoryWithoutPermissionTest) {
294   base::FilePath test_file = TempFilePath("open_existing.txt", true);
295   ASSERT_FALSE(test_file.empty());
296   base::FilePath test_directory = test_file.DirName();
297   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
298       &test_directory);
299   ASSERT_TRUE(RunPlatformAppTest(
300       "api_test/file_system/open_directory_without_permission"))
301       << message_;
302   CheckStoredDirectoryMatches(base::FilePath());
303 }
304
305 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
306                        FileSystemApiOpenDirectoryWithOnlyWritePermissionTest) {
307   base::FilePath test_file = TempFilePath("open_existing.txt", true);
308   ASSERT_FALSE(test_file.empty());
309   base::FilePath test_directory = test_file.DirName();
310   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
311       &test_directory);
312   ASSERT_TRUE(RunPlatformAppTest(
313       "api_test/file_system/open_directory_with_only_write"))
314       << message_;
315   CheckStoredDirectoryMatches(base::FilePath());
316 }
317
318 #if defined(OS_WIN) || defined(OS_POSIX)
319 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
320                        FileSystemApiOpenDirectoryOnGraylistAndAllowTest) {
321   FileSystemChooseEntryFunction::SkipDirectoryConfirmationForTest();
322   base::FilePath test_file = TempFilePath("open_existing.txt", true);
323   ASSERT_FALSE(test_file.empty());
324   base::FilePath test_directory = test_file.DirName();
325   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
326       kGraylistedPath, test_directory, false, false));
327   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
328       &test_directory);
329   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
330       << message_;
331   CheckStoredDirectoryMatches(test_file);
332 }
333
334 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
335                        FileSystemApiOpenDirectoryOnGraylistTest) {
336   FileSystemChooseEntryFunction::AutoCancelDirectoryConfirmationForTest();
337   base::FilePath test_file = TempFilePath("open_existing.txt", true);
338   ASSERT_FALSE(test_file.empty());
339   base::FilePath test_directory = test_file.DirName();
340   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
341       kGraylistedPath, test_directory, false, false));
342   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
343       &test_directory);
344   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory_cancel"))
345       << message_;
346   CheckStoredDirectoryMatches(test_file);
347 }
348
349 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
350                        FileSystemApiOpenDirectoryContainingGraylistTest) {
351   FileSystemChooseEntryFunction::AutoCancelDirectoryConfirmationForTest();
352   base::FilePath test_file = TempFilePath("open_existing.txt", true);
353   ASSERT_FALSE(test_file.empty());
354   base::FilePath test_directory = test_file.DirName();
355   base::FilePath parent_directory = test_directory.DirName();
356   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
357       kGraylistedPath, test_directory, false, false));
358   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
359       &parent_directory);
360   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory_cancel"))
361       << message_;
362   CheckStoredDirectoryMatches(test_directory);
363 }
364
365 // Test that choosing a subdirectory of a path does not require confirmation.
366 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
367                        FileSystemApiOpenDirectorySubdirectoryOfGraylistTest) {
368   // If a dialog is erroneously displayed, auto cancel it, so that the test
369   // fails.
370   FileSystemChooseEntryFunction::AutoCancelDirectoryConfirmationForTest();
371   base::FilePath test_file = TempFilePath("open_existing.txt", true);
372   ASSERT_FALSE(test_file.empty());
373   base::FilePath test_directory = test_file.DirName();
374   base::FilePath parent_directory = test_directory.DirName();
375   ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded(
376       kGraylistedPath, parent_directory, false, false));
377   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
378       &test_directory);
379   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_directory"))
380       << message_;
381   CheckStoredDirectoryMatches(test_file);
382 }
383 #endif  // defined(OS_WIN) || defined(OS_POSIX)
384
385 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
386     FileSystemApiInvalidChooseEntryTypeTest) {
387   base::FilePath test_file = TempFilePath("open_existing.txt", true);
388   ASSERT_FALSE(test_file.empty());
389   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
390       &test_file);
391   ASSERT_TRUE(RunPlatformAppTest(
392       "api_test/file_system/invalid_choose_file_type")) << message_;
393   CheckStoredDirectoryMatches(base::FilePath());
394 }
395
396 // http://crbug.com/177163
397 #if defined(OS_WIN) && !defined(NDEBUG)
398 #define MAYBE_FileSystemApiOpenExistingFileWithWriteTest DISABLED_FileSystemApiOpenExistingFileWithWriteTest
399 #else
400 #define MAYBE_FileSystemApiOpenExistingFileWithWriteTest FileSystemApiOpenExistingFileWithWriteTest
401 #endif
402 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
403     MAYBE_FileSystemApiOpenExistingFileWithWriteTest) {
404   base::FilePath test_file = TempFilePath("open_existing.txt", true);
405   ASSERT_FALSE(test_file.empty());
406   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
407       &test_file);
408   ASSERT_TRUE(RunPlatformAppTest(
409       "api_test/file_system/open_existing_with_write")) << message_;
410   CheckStoredDirectoryMatches(test_file);
411 }
412
413 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
414     FileSystemApiOpenWritableExistingFileTest) {
415   base::FilePath test_file = TempFilePath("open_existing.txt", true);
416   ASSERT_FALSE(test_file.empty());
417   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
418       &test_file);
419   ASSERT_TRUE(RunPlatformAppTest(
420       "api_test/file_system/open_writable_existing")) << message_;
421   CheckStoredDirectoryMatches(base::FilePath());
422 }
423
424 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
425     FileSystemApiOpenWritableExistingFileWithWriteTest) {
426   base::FilePath test_file = TempFilePath("open_existing.txt", true);
427   ASSERT_FALSE(test_file.empty());
428   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
429       &test_file);
430   ASSERT_TRUE(RunPlatformAppTest(
431       "api_test/file_system/open_writable_existing_with_write")) << message_;
432   CheckStoredDirectoryMatches(test_file);
433 }
434
435 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
436                        FileSystemApiOpenMultipleWritableExistingFilesTest) {
437   std::vector<std::string> names;
438   names.push_back("open_existing1.txt");
439   names.push_back("open_existing2.txt");
440   std::vector<base::FilePath> test_files = TempFilePaths(names, true);
441   ASSERT_EQ(2u, test_files.size());
442   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
443       &test_files);
444   ASSERT_TRUE(RunPlatformAppTest(
445       "api_test/file_system/open_multiple_writable_existing_with_write"))
446       << message_;
447 }
448
449 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenCancelTest) {
450   FileSystemChooseEntryFunction::SkipPickerAndAlwaysCancelForTest();
451   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_cancel"))
452       << message_;
453   CheckStoredDirectoryMatches(base::FilePath());
454 }
455
456 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiOpenBackgroundTest) {
457   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_background"))
458       << message_;
459 }
460
461 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveNewFileTest) {
462   base::FilePath test_file = TempFilePath("save_new.txt", false);
463   ASSERT_FALSE(test_file.empty());
464   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
465       &test_file);
466   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_new"))
467       << message_;
468   CheckStoredDirectoryMatches(base::FilePath());
469 }
470
471 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveExistingFileTest) {
472   base::FilePath test_file = TempFilePath("save_existing.txt", true);
473   ASSERT_FALSE(test_file.empty());
474   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
475       &test_file);
476   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_existing"))
477       << message_;
478   CheckStoredDirectoryMatches(base::FilePath());
479 }
480
481 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
482     FileSystemApiSaveNewFileWithWriteTest) {
483   base::FilePath test_file = TempFilePath("save_new.txt", false);
484   ASSERT_FALSE(test_file.empty());
485   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
486       &test_file);
487   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_new_with_write"))
488       << message_;
489   CheckStoredDirectoryMatches(test_file);
490 }
491
492 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
493     FileSystemApiSaveExistingFileWithWriteTest) {
494   base::FilePath test_file = TempFilePath("save_existing.txt", true);
495   ASSERT_FALSE(test_file.empty());
496   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
497       &test_file);
498   ASSERT_TRUE(RunPlatformAppTest(
499       "api_test/file_system/save_existing_with_write")) << message_;
500   CheckStoredDirectoryMatches(test_file);
501 }
502
503 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveMultipleFilesTest) {
504   std::vector<std::string> names;
505   names.push_back("save1.txt");
506   names.push_back("save2.txt");
507   std::vector<base::FilePath> test_files = TempFilePaths(names, false);
508   ASSERT_EQ(2u, test_files.size());
509   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathsForTest(
510       &test_files);
511   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_multiple"))
512       << message_;
513 }
514
515 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveCancelTest) {
516   FileSystemChooseEntryFunction::SkipPickerAndAlwaysCancelForTest();
517   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_cancel"))
518       << message_;
519 }
520
521 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiSaveBackgroundTest) {
522   ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/save_background"))
523       << message_;
524 }
525
526 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiGetWritableTest) {
527   base::FilePath test_file = TempFilePath("writable.txt", true);
528   ASSERT_FALSE(test_file.empty());
529   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
530       &test_file);
531   ASSERT_TRUE(RunPlatformAppTest(
532       "api_test/file_system/get_writable_file_entry")) << message_;
533 }
534
535 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
536     FileSystemApiGetWritableWithWriteTest) {
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_with_write")) << message_;
543 }
544
545 IN_PROC_BROWSER_TEST_F(FileSystemApiTest,
546                        FileSystemApiGetWritableRootEntryTest) {
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_root_entry")) << 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   AppLoadObserver observer(profile(),
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   AppLoadObserver observer(profile(),
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