1 // Copyright (c) 2013 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.
5 #include "apps/app_restore_service.h"
6 #include "apps/app_restore_service_factory.h"
7 #include "apps/saved_files_service.h"
8 #include "chrome/browser/apps/app_browsertest_util.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
11 #include "chrome/browser/extensions/extension_test_message_listener.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/notification_service.h"
14 #include "content/public/test/test_utils.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/common/extension.h"
18 using extensions::Extension
;
19 using extensions::ExtensionPrefs
;
20 using extensions::ExtensionSystem
;
21 using extensions::FileSystemChooseEntryFunction
;
23 // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
25 using extensions::PlatformAppBrowserTest
;
29 // Tests that a running app is recorded in the preferences as such.
30 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, RunningAppsAreRecorded
) {
31 content::WindowedNotificationObserver
extension_suspended(
32 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
33 content::NotificationService::AllSources());
35 const Extension
* extension
= LoadExtension(
36 test_data_dir_
.AppendASCII("platform_apps/restart_test"));
37 ASSERT_TRUE(extension
);
38 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(browser()->profile());
41 ASSERT_TRUE(extension_prefs
->IsExtensionRunning(extension
->id()));
43 // Wait for the extension to get suspended.
44 extension_suspended
.Wait();
46 // App isn't running because it got suspended.
47 ASSERT_FALSE(extension_prefs
->IsExtensionRunning(extension
->id()));
49 // Pretend that the app is supposed to be running.
50 extension_prefs
->SetExtensionRunning(extension
->id(), true);
52 ExtensionTestMessageListener
restart_listener("onRestarted", false);
53 apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
55 restart_listener
.WaitUntilSatisfied();
58 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, FileAccessIsSavedToPrefs
) {
59 content::WindowedNotificationObserver
extension_suspended(
60 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
61 content::NotificationService::AllSources());
63 base::ScopedTempDir temp_directory
;
64 ASSERT_TRUE(temp_directory
.CreateUniqueTempDir());
65 base::FilePath temp_file
;
66 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory
.path(),
69 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
71 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
72 "temp", temp_directory
.path());
74 ExtensionTestMessageListener
file_written_listener("fileWritten", false);
75 ExtensionTestMessageListener
access_ok_listener(
76 "restartedFileAccessOK", false);
78 const Extension
* extension
=
79 LoadAndLaunchPlatformApp("file_access_saved_to_prefs_test");
80 ASSERT_TRUE(extension
);
81 file_written_listener
.WaitUntilSatisfied();
83 SavedFilesService
* saved_files_service
= SavedFilesService::Get(profile());
85 std::vector
<SavedFileEntry
> file_entries
=
86 saved_files_service
->GetAllFileEntries(extension
->id());
87 // One for the read-only file entry and one for the writable file entry.
88 ASSERT_EQ(2u, file_entries
.size());
90 extension_suspended
.Wait();
91 file_entries
= saved_files_service
->GetAllFileEntries(extension
->id());
92 // File entries should be cleared when the extension is suspended.
93 ASSERT_TRUE(file_entries
.empty());
96 // Flaky: crbug.com/269613
97 #if defined(OS_LINUX) || defined(OS_WIN)
98 #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
100 #define MAYBE_FileAccessIsRestored FileAccessIsRestored
103 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, MAYBE_FileAccessIsRestored
) {
104 content::WindowedNotificationObserver
extension_suspended(
105 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
106 content::NotificationService::AllSources());
108 base::ScopedTempDir temp_directory
;
109 ASSERT_TRUE(temp_directory
.CreateUniqueTempDir());
110 base::FilePath temp_file
;
111 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory
.path(),
114 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
116 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
117 "temp", temp_directory
.path());
119 ExtensionTestMessageListener
file_written_listener("fileWritten", false);
120 ExtensionTestMessageListener
access_ok_listener(
121 "restartedFileAccessOK", false);
123 const Extension
* extension
=
124 LoadAndLaunchPlatformApp("file_access_restored_test");
125 ASSERT_TRUE(extension
);
126 file_written_listener
.WaitUntilSatisfied();
128 ExtensionPrefs
* extension_prefs
=
129 ExtensionPrefs::Get(browser()->profile());
130 SavedFilesService
* saved_files_service
= SavedFilesService::Get(profile());
131 std::vector
<SavedFileEntry
> file_entries
=
132 saved_files_service
->GetAllFileEntries(extension
->id());
133 extension_suspended
.Wait();
135 // Simulate a restart by populating the preferences as if the browser didn't
136 // get time to clean itself up.
137 extension_prefs
->SetExtensionRunning(extension
->id(), true);
138 for (std::vector
<SavedFileEntry
>::const_iterator it
= file_entries
.begin();
139 it
!= file_entries
.end(); ++it
) {
140 saved_files_service
->RegisterFileEntry(
141 extension
->id(), it
->id
, it
->path
, it
->is_directory
);
144 apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
147 access_ok_listener
.WaitUntilSatisfied();