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 "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
8 #include "chrome/browser/extensions/api/file_system/file_system_api.h"
9 #include "chrome/browser/extensions/extension_prefs.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_system.h"
12 #include "chrome/browser/extensions/extension_test_message_listener.h"
13 #include "chrome/browser/extensions/platform_app_browsertest_util.h"
14 #include "chrome/common/extensions/extension.h"
15 #include "content/public/test/test_utils.h"
17 using extensions::app_file_handler_util::SavedFileEntry
;
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 ExtensionService
* extension_service
=
39 ExtensionSystem::Get(browser()->profile())->extension_service();
40 ExtensionPrefs
* extension_prefs
= extension_service
->extension_prefs();
43 ASSERT_TRUE(extension_prefs
->IsExtensionRunning(extension
->id()));
45 // Wait for the extension to get suspended.
46 extension_suspended
.Wait();
48 // App isn't running because it got suspended.
49 ASSERT_FALSE(extension_prefs
->IsExtensionRunning(extension
->id()));
51 // Pretend that the app is supposed to be running.
52 extension_prefs
->SetExtensionRunning(extension
->id(), true);
54 ExtensionTestMessageListener
restart_listener("onRestarted", false);
55 apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
57 restart_listener
.WaitUntilSatisfied();
60 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, FileAccessIsSavedToPrefs
) {
61 content::WindowedNotificationObserver
extension_suspended(
62 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
63 content::NotificationService::AllSources());
65 base::ScopedTempDir temp_directory
;
66 ASSERT_TRUE(temp_directory
.CreateUniqueTempDir());
67 base::FilePath temp_file
;
68 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_directory
.path(),
71 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
73 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
74 "temp", temp_directory
.path());
76 ExtensionTestMessageListener
file_written_listener("fileWritten", false);
77 ExtensionTestMessageListener
access_ok_listener(
78 "restartedFileAccessOK", false);
80 const Extension
* extension
=
81 LoadAndLaunchPlatformApp("file_access_saved_to_prefs_test");
82 ASSERT_TRUE(extension
);
83 file_written_listener
.WaitUntilSatisfied();
85 ExtensionService
* extension_service
=
86 ExtensionSystem::Get(browser()->profile())->extension_service();
87 ExtensionPrefs
* extension_prefs
= extension_service
->extension_prefs();
89 // Record the file entries in prefs because when the app gets suspended it
90 // will have them all cleared.
91 std::vector
<SavedFileEntry
> file_entries
;
92 extension_prefs
->GetSavedFileEntries(extension
->id(), &file_entries
);
93 // One for the read-only file entry and one for the writable file entry.
94 ASSERT_EQ(2u, file_entries
.size());
96 extension_suspended
.Wait();
98 extension_prefs
->GetSavedFileEntries(extension
->id(), &file_entries
);
99 // File entries should be cleared when the extension is suspended.
100 ASSERT_TRUE(file_entries
.empty());
103 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest
, 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(file_util::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 ExtensionService
* extension_service
=
129 ExtensionSystem::Get(browser()->profile())->extension_service();
130 ExtensionPrefs
* extension_prefs
= extension_service
->extension_prefs();
132 // Record the file entries in prefs because when the app gets suspended it
133 // will have them all cleared.
134 std::vector
<SavedFileEntry
> file_entries
;
135 extension_prefs
->GetSavedFileEntries(extension
->id(), &file_entries
);
136 extension_suspended
.Wait();
138 // Simulate a restart by populating the preferences as if the browser didn't
139 // get time to clean itself up.
140 extension_prefs
->SetExtensionRunning(extension
->id(), true);
141 for (std::vector
<SavedFileEntry
>::const_iterator it
= file_entries
.begin();
142 it
!= file_entries
.end(); ++it
) {
143 extension_prefs
->AddSavedFileEntry(
144 extension
->id(), it
->id
, it
->path
, it
->writable
);
147 apps::AppRestoreServiceFactory::GetForProfile(browser()->profile())->
150 access_ok_listener
.WaitUntilSatisfied();