OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "apps/saved_files_service.h" | 5 #include "apps/saved_files_service.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 8 #include "base/scoped_observer.h" |
8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
9 #include "chrome/browser/apps/app_browsertest_util.h" | 10 #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" | 11 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/chrome_paths.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 #include "extensions/browser/extension_prefs.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 } |
17 | 21 |
18 namespace extensions { | 22 namespace extensions { |
19 | 23 |
20 namespace { | 24 namespace { |
21 | 25 |
22 class AppInstallObserver : public content::NotificationObserver { | 26 class AppLoadObserver : public ExtensionRegistryObserver { |
23 public: | 27 public: |
24 AppInstallObserver( | 28 AppLoadObserver(content::BrowserContext* browser_context, |
25 base::Callback<void(const Extension*)> callback) | 29 base::Callback<void(const Extension*)> callback) |
26 : callback_(callback) { | 30 : callback_(callback), extension_registry_observer_(this) { |
27 registrar_.Add(this, | 31 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); |
28 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
29 content::NotificationService::AllSources()); | |
30 } | 32 } |
31 | 33 |
32 virtual void Observe(int type, | 34 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, |
33 const content::NotificationSource& source, | 35 const Extension* extension) OVERRIDE { |
34 const content::NotificationDetails& details) OVERRIDE { | 36 callback_.Run(extension); |
35 EXPECT_EQ(chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, type); | |
36 callback_.Run(content::Details<const Extension>(details).ptr()); | |
37 } | 37 } |
38 | 38 |
39 private: | 39 private: |
40 content::NotificationRegistrar registrar_; | |
41 base::Callback<void(const Extension*)> callback_; | 40 base::Callback<void(const Extension*)> callback_; |
42 DISALLOW_COPY_AND_ASSIGN(AppInstallObserver); | 41 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 42 extension_registry_observer_; |
| 43 DISALLOW_COPY_AND_ASSIGN(AppLoadObserver); |
43 }; | 44 }; |
44 | 45 |
45 void SetLastChooseEntryDirectory(const base::FilePath& choose_entry_directory, | 46 void SetLastChooseEntryDirectory(const base::FilePath& choose_entry_directory, |
46 ExtensionPrefs* prefs, | 47 ExtensionPrefs* prefs, |
47 const Extension* extension) { | 48 const Extension* extension) { |
48 file_system_api::SetLastChooseEntryDirectory( | 49 file_system_api::SetLastChooseEntryDirectory( |
49 prefs, extension->id(), choose_entry_directory); | 50 prefs, extension->id(), choose_entry_directory); |
50 } | 51 } |
51 | 52 |
52 void AddSavedEntry(const base::FilePath& path_to_save, | 53 void AddSavedEntry(const base::FilePath& path_to_save, |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 CheckStoredDirectoryMatches(test_file); | 191 CheckStoredDirectoryMatches(test_file); |
191 } | 192 } |
192 | 193 |
193 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 194 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
194 FileSystemApiOpenExistingFileUsingPreviousPathTest) { | 195 FileSystemApiOpenExistingFileUsingPreviousPathTest) { |
195 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 196 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
196 ASSERT_FALSE(test_file.empty()); | 197 ASSERT_FALSE(test_file.empty()); |
197 FileSystemChooseEntryFunction:: | 198 FileSystemChooseEntryFunction:: |
198 SkipPickerAndSelectSuggestedPathForTest(); | 199 SkipPickerAndSelectSuggestedPathForTest(); |
199 { | 200 { |
200 AppInstallObserver observer( | 201 AppLoadObserver observer(profile(), |
201 base::Bind(SetLastChooseEntryDirectory, | 202 base::Bind(SetLastChooseEntryDirectory, |
202 test_file.DirName(), | 203 test_file.DirName(), |
203 ExtensionPrefs::Get(profile()))); | 204 ExtensionPrefs::Get(profile()))); |
204 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) | 205 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) |
205 << message_; | 206 << message_; |
206 } | 207 } |
207 CheckStoredDirectoryMatches(test_file); | 208 CheckStoredDirectoryMatches(test_file); |
208 } | 209 } |
209 | 210 |
210 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 211 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
211 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { | 212 FileSystemApiOpenExistingFilePreviousPathDoesNotExistTest) { |
212 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 213 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
213 ASSERT_FALSE(test_file.empty()); | 214 ASSERT_FALSE(test_file.empty()); |
214 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( | 215 ASSERT_TRUE(PathService::OverrideAndCreateIfNeeded( |
215 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false, false)); | 216 chrome::DIR_USER_DOCUMENTS, test_file.DirName(), false, false)); |
216 FileSystemChooseEntryFunction:: | 217 FileSystemChooseEntryFunction:: |
217 SkipPickerAndSelectSuggestedPathForTest(); | 218 SkipPickerAndSelectSuggestedPathForTest(); |
218 { | 219 { |
219 AppInstallObserver observer(base::Bind( | 220 AppLoadObserver observer( |
220 SetLastChooseEntryDirectory, | 221 profile(), |
221 test_file.DirName().Append( | 222 base::Bind(SetLastChooseEntryDirectory, |
222 base::FilePath::FromUTF8Unsafe("fake_directory_does_not_exist")), | 223 test_file.DirName().Append(base::FilePath::FromUTF8Unsafe( |
223 ExtensionPrefs::Get(profile()))); | 224 "fake_directory_does_not_exist")), |
| 225 ExtensionPrefs::Get(profile()))); |
224 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) | 226 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/open_existing")) |
225 << message_; | 227 << message_; |
226 } | 228 } |
227 CheckStoredDirectoryMatches(test_file); | 229 CheckStoredDirectoryMatches(test_file); |
228 } | 230 } |
229 | 231 |
230 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, | 232 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, |
231 FileSystemApiOpenExistingFileDefaultPathTest) { | 233 FileSystemApiOpenExistingFileDefaultPathTest) { |
232 base::FilePath test_file = TempFilePath("open_existing.txt", true); | 234 base::FilePath test_file = TempFilePath("open_existing.txt", true); |
233 ASSERT_FALSE(test_file.empty()); | 235 ASSERT_FALSE(test_file.empty()); |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 EXPECT_EQ(test_directory, file_entries[0].path); | 603 EXPECT_EQ(test_directory, file_entries[0].path); |
602 EXPECT_EQ(1, file_entries[0].sequence_number); | 604 EXPECT_EQ(1, file_entries[0].sequence_number); |
603 EXPECT_TRUE(file_entries[0].is_directory); | 605 EXPECT_TRUE(file_entries[0].is_directory); |
604 } | 606 } |
605 | 607 |
606 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreEntry) { | 608 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreEntry) { |
607 base::FilePath test_file = TempFilePath("writable.txt", true); | 609 base::FilePath test_file = TempFilePath("writable.txt", true); |
608 ASSERT_FALSE(test_file.empty()); | 610 ASSERT_FALSE(test_file.empty()); |
609 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( | 611 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
610 &test_file); | 612 &test_file); |
611 AppInstallObserver observer( | 613 AppLoadObserver observer(profile(), |
612 base::Bind(AddSavedEntry, | 614 base::Bind(AddSavedEntry, |
613 test_file, | 615 test_file, |
614 false, | 616 false, |
615 apps::SavedFilesService::Get(profile()))); | 617 apps::SavedFilesService::Get(profile()))); |
616 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/restore_entry")) | 618 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/restore_entry")) |
617 << message_; | 619 << message_; |
618 } | 620 } |
619 | 621 |
620 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreDirectoryEntry) { | 622 IN_PROC_BROWSER_TEST_F(FileSystemApiTest, FileSystemApiRestoreDirectoryEntry) { |
621 base::FilePath test_file = TempFilePath("writable.txt", true); | 623 base::FilePath test_file = TempFilePath("writable.txt", true); |
622 ASSERT_FALSE(test_file.empty()); | 624 ASSERT_FALSE(test_file.empty()); |
623 base::FilePath test_directory = test_file.DirName(); | 625 base::FilePath test_directory = test_file.DirName(); |
624 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( | 626 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
625 &test_file); | 627 &test_file); |
626 AppInstallObserver observer( | 628 AppLoadObserver observer(profile(), |
627 base::Bind(AddSavedEntry, | 629 base::Bind(AddSavedEntry, |
628 test_directory, | 630 test_directory, |
629 true, | 631 true, |
630 apps::SavedFilesService::Get(profile()))); | 632 apps::SavedFilesService::Get(profile()))); |
631 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/restore_directory")) | 633 ASSERT_TRUE(RunPlatformAppTest("api_test/file_system/restore_directory")) |
632 << message_; | 634 << message_; |
633 } | 635 } |
634 | 636 |
635 } // namespace extensions | 637 } // namespace extensions |
OLD | NEW |