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 // Tests that apps are recorded in the preferences as active when and only when
59 // they have visible windows.
60 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, ActiveAppsAreRecorded
) {
61 ExtensionTestMessageListener
ready_listener("ready", true);
62 const Extension
* extension
=
63 LoadExtension(test_data_dir_
.AppendASCII("platform_apps/active_test"));
64 ASSERT_TRUE(extension
);
65 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(browser()->profile());
66 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
68 // Open a visible window and check the app is marked active.
69 ready_listener
.Reply("create");
70 ready_listener
.Reset();
71 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
72 ASSERT_TRUE(extension_prefs
->IsActive(extension
->id()));
74 // Close the window, then open a minimized window and check the app is active.
75 ready_listener
.Reply("closeLastWindow");
76 ready_listener
.Reset();
77 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
78 ready_listener
.Reply("createMinimized");
79 ready_listener
.Reset();
80 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
81 ASSERT_TRUE(extension_prefs
->IsActive(extension
->id()));
83 // Close the window, then open a hidden window and check the app is not
85 ready_listener
.Reply("closeLastWindow");
86 ready_listener
.Reset();
87 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
88 ready_listener
.Reply("createHidden");
89 ready_listener
.Reset();
90 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
91 ASSERT_FALSE(extension_prefs
->IsActive(extension
->id()));
93 // Open another window and check the app is marked active.
94 ready_listener
.Reply("create");
95 ready_listener
.Reset();
96 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
97 ASSERT_TRUE(extension_prefs
->IsActive(extension
->id()));
99 // Close the visible window and check the app has been marked inactive.
100 ready_listener
.Reply("closeLastWindow");
101 ready_listener
.Reset();
102 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
103 ASSERT_FALSE(extension_prefs
->IsActive(extension
->id()));
105 // Close the last window and exit.
106 ready_listener
.Reply("closeLastWindow");
107 ready_listener
.Reset();
108 ASSERT_TRUE(ready_listener
.WaitUntilSatisfied());
109 ready_listener
.Reply("exit");
112 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, FileAccessIsSavedToPrefs
) {
113 content::WindowedNotificationObserver
extension_suspended(
114 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
115 content::NotificationService::AllSources());
117 base::ScopedTempDir temp_directory
;
118 ASSERT_TRUE(temp_directory
.CreateUniqueTempDir());
119 base::FilePath temp_file
;
120 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory
.path(),
123 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
125 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
126 "temp", temp_directory
.path());
128 ExtensionTestMessageListener
file_written_listener("fileWritten", false);
129 ExtensionTestMessageListener
access_ok_listener(
130 "restartedFileAccessOK", false);
132 const Extension
* extension
=
133 LoadAndLaunchPlatformApp("file_access_saved_to_prefs_test");
134 ASSERT_TRUE(extension
);
135 file_written_listener
.WaitUntilSatisfied();
137 SavedFilesService
* saved_files_service
= SavedFilesService::Get(profile());
139 std::vector
<SavedFileEntry
> file_entries
=
140 saved_files_service
->GetAllFileEntries(extension
->id());
141 // One for the read-only file entry and one for the writable file entry.
142 ASSERT_EQ(2u, file_entries
.size());
144 extension_suspended
.Wait();
145 file_entries
= saved_files_service
->GetAllFileEntries(extension
->id());
146 // File entries should be cleared when the extension is suspended.
147 ASSERT_TRUE(file_entries
.empty());
150 // Flaky: crbug.com/269613
151 #if defined(OS_LINUX) || defined(OS_WIN)
152 #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
154 #define MAYBE_FileAccessIsRestored FileAccessIsRestored
157 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, MAYBE_FileAccessIsRestored
) {
158 content::WindowedNotificationObserver
extension_suspended(
159 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
160 content::NotificationService::AllSources());
162 base::ScopedTempDir temp_directory
;
163 ASSERT_TRUE(temp_directory
.CreateUniqueTempDir());
164 base::FilePath temp_file
;
165 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_directory
.path(),
168 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
170 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
171 "temp", temp_directory
.path());
173 ExtensionTestMessageListener
file_written_listener("fileWritten", false);
174 ExtensionTestMessageListener
access_ok_listener(
175 "restartedFileAccessOK", false);
177 const Extension
* extension
=
178 LoadAndLaunchPlatformApp("file_access_restored_test");
179 ASSERT_TRUE(extension
);
180 file_written_listener
.WaitUntilSatisfied();
182 ExtensionPrefs
* extension_prefs
=
183 ExtensionPrefs::Get(browser()->profile());
184 SavedFilesService
* saved_files_service
= SavedFilesService::Get(profile());
185 std::vector
<SavedFileEntry
> file_entries
=
186 saved_files_service
->GetAllFileEntries(extension
->id());
187 extension_suspended
.Wait();
189 // Simulate a restart by populating the preferences as if the browser didn't
190 // get time to clean itself up.
191 extension_prefs
->SetExtensionRunning(extension
->id(), true);
192 for (std::vector
<SavedFileEntry
>::const_iterator it
= file_entries
.begin();
193 it
!= file_entries
.end(); ++it
) {
194 saved_files_service
->RegisterFileEntry(
195 extension
->id(), it
->id
, it
->path
, it
->is_directory
);
198 apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
201 access_ok_listener
.WaitUntilSatisfied();