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 "base/threading/thread_restrictions.h"
9 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/test/test_utils.h"
13 #include "extensions/browser/api/file_system/file_system_api.h"
14 #include "extensions/browser/api/file_system/saved_file_entry.h"
15 #include "extensions/browser/extension_prefs.h"
16 #include "extensions/browser/notification_types.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/test/extension_test_message_listener.h"
20 using extensions::Extension;
21 using extensions::ExtensionPrefs;
22 using extensions::ExtensionSystem;
23 using extensions::FileSystemChooseEntryFunction;
24 using extensions::SavedFileEntry;
26 // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
28 using extensions::PlatformAppBrowserTest;
32 // Tests that a running app is recorded in the preferences as such.
33 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
34 content::WindowedNotificationObserver extension_suspended(
35 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
36 content::NotificationService::AllSources());
38 const Extension* extension = LoadExtension(
39 test_data_dir_.AppendASCII("platform_apps/restart_test"));
40 ASSERT_TRUE(extension);
41 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
44 ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
46 // Wait for the extension to get suspended.
47 extension_suspended.Wait();
49 // App isn't running because it got suspended.
50 ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
52 // Pretend that the app is supposed to be running.
53 extension_prefs->SetExtensionRunning(extension->id(), true);
55 ExtensionTestMessageListener restart_listener("onRestarted", false);
56 apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
57 ->HandleStartup(true);
58 EXPECT_TRUE(restart_listener.WaitUntilSatisfied());
61 // Tests that apps are recorded in the preferences as active when and only when
62 // they have visible windows.
63 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ActiveAppsAreRecorded) {
64 ExtensionTestMessageListener ready_listener("ready", true);
65 const Extension* extension =
66 LoadExtension(test_data_dir_.AppendASCII("platform_apps/active_test"));
67 ASSERT_TRUE(extension);
68 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
69 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
71 // Open a visible window and check the app is marked active.
72 ready_listener.Reply("create");
73 ready_listener.Reset();
74 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
75 ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
77 // Close the window, then open a minimized window and check the app is active.
78 ready_listener.Reply("closeLastWindow");
79 ready_listener.Reset();
80 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
81 ready_listener.Reply("createMinimized");
82 ready_listener.Reset();
83 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
84 ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
86 // Close the window, then open a hidden window and check the app is not
88 ready_listener.Reply("closeLastWindow");
89 ready_listener.Reset();
90 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
91 ready_listener.Reply("createHidden");
92 ready_listener.Reset();
93 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
94 ASSERT_FALSE(extension_prefs->IsActive(extension->id()));
96 // Open another window and check the app is marked active.
97 ready_listener.Reply("create");
98 ready_listener.Reset();
99 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
100 ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
102 // Close the visible window and check the app has been marked inactive.
103 ready_listener.Reply("closeLastWindow");
104 ready_listener.Reset();
105 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
106 ASSERT_FALSE(extension_prefs->IsActive(extension->id()));
108 // Close the last window and exit.
109 ready_listener.Reply("closeLastWindow");
110 ready_listener.Reset();
111 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
112 ready_listener.Reply("exit");
115 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) {
116 content::WindowedNotificationObserver extension_suspended(
117 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
118 content::NotificationService::AllSources());
120 base::ScopedAllowBlockingForTesting allow_blocking;
121 base::ScopedTempDir temp_directory;
122 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
123 base::FilePath temp_file;
125 base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
127 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
129 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
130 "temp", temp_directory.GetPath());
132 const Extension* extension = LoadAndLaunchPlatformApp(
133 "file_access_saved_to_prefs_test", "fileWritten");
134 ASSERT_TRUE(extension);
136 SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
138 std::vector<SavedFileEntry> file_entries =
139 saved_files_service->GetAllFileEntries(extension->id());
140 // One for the read-only file entry and one for the writable file entry.
141 ASSERT_EQ(2u, file_entries.size());
143 extension_suspended.Wait();
144 file_entries = saved_files_service->GetAllFileEntries(extension->id());
145 // File entries should be cleared when the extension is suspended.
146 ASSERT_TRUE(file_entries.empty());
149 // Flaky: crbug.com/269613
150 #if defined(OS_LINUX) || defined(OS_WIN)
151 #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
153 #define MAYBE_FileAccessIsRestored FileAccessIsRestored
156 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
157 content::WindowedNotificationObserver extension_suspended(
158 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
159 content::NotificationService::AllSources());
161 base::ScopedAllowBlockingForTesting allow_blocking;
162 base::ScopedTempDir temp_directory;
163 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
164 base::FilePath temp_file;
166 base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
168 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
170 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
171 "temp", temp_directory.GetPath());
173 ExtensionTestMessageListener access_ok_listener(
174 "restartedFileAccessOK", false);
176 const Extension* extension =
177 LoadAndLaunchPlatformApp("file_access_restored_test", "fileWritten");
178 ASSERT_TRUE(extension);
180 ExtensionPrefs* extension_prefs =
181 ExtensionPrefs::Get(browser()->profile());
182 SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
183 std::vector<SavedFileEntry> file_entries =
184 saved_files_service->GetAllFileEntries(extension->id());
185 extension_suspended.Wait();
187 // Simulate a restart by populating the preferences as if the browser didn't
188 // get time to clean itself up.
189 extension_prefs->SetExtensionRunning(extension->id(), true);
190 for (std::vector<SavedFileEntry>::const_iterator it = file_entries.begin();
191 it != file_entries.end(); ++it) {
192 saved_files_service->RegisterFileEntry(
193 extension->id(), it->id, it->path, it->is_directory);
196 apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
197 ->HandleStartup(true);
199 EXPECT_TRUE(access_ok_listener.WaitUntilSatisfied());