1 // Copyright 2013 The Chromium Authors
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"
7 #include "apps/app_restore_service_factory.h"
8 #include "apps/saved_files_service.h"
9 #include "base/files/file_util.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
13 #include "content/public/browser/browser_context.h"
14 #include "content/public/test/browser_test.h"
15 #include "extensions/browser/api/file_system/file_system_api.h"
16 #include "extensions/browser/api/file_system/saved_file_entry.h"
17 #include "extensions/browser/extension_host_test_helper.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/test/extension_test_message_listener.h"
22 using extensions::Extension;
23 using extensions::ExtensionPrefs;
24 using extensions::ExtensionSystem;
25 using extensions::FileSystemChooseEntryFunction;
26 using extensions::SavedFileEntry;
28 // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
30 using extensions::PlatformAppBrowserTest;
34 // Tests that a running app is recorded in the preferences as such.
35 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
36 extensions::ExtensionHostTestHelper host_helper(profile());
37 const Extension* extension = LoadExtension(
38 test_data_dir_.AppendASCII("platform_apps/restart_test"));
39 ASSERT_TRUE(extension);
40 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
43 ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
45 // Wait for the extension to get suspended.
46 host_helper.WaitForHostDestroyed();
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");
55 apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
56 ->HandleStartup(true);
57 EXPECT_TRUE(restart_listener.WaitUntilSatisfied());
60 // Tests that apps are recorded in the preferences as active when and only when
61 // they have visible windows.
62 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ActiveAppsAreRecorded) {
63 ExtensionTestMessageListener ready_listener("ready",
64 ReplyBehavior::kWillReply);
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 base::ScopedAllowBlockingForTesting allow_blocking;
117 base::ScopedTempDir temp_directory;
118 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
119 base::FilePath temp_file;
121 base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
123 const FileSystemChooseEntryFunction::TestOptions test_options{
124 .path_to_be_picked = &temp_file};
126 FileSystemChooseEntryFunction::SetOptionsForTesting(test_options);
127 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
128 "temp", temp_directory.GetPath());
130 extensions::ExtensionHostTestHelper host_helper(profile());
131 const Extension* extension = LoadAndLaunchPlatformApp(
132 "file_access_saved_to_prefs_test", "fileWritten");
133 ASSERT_TRUE(extension);
135 SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
137 std::vector<SavedFileEntry> file_entries =
138 saved_files_service->GetAllFileEntries(extension->id());
139 // One for the read-only file entry and one for the writable file entry.
140 ASSERT_EQ(2u, file_entries.size());
142 host_helper.WaitForHostDestroyed();
143 file_entries = saved_files_service->GetAllFileEntries(extension->id());
144 // File entries should be cleared when the extension is suspended.
145 ASSERT_TRUE(file_entries.empty());
148 // Flaky: crbug.com/269613
149 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
150 #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
152 #define MAYBE_FileAccessIsRestored FileAccessIsRestored
155 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
156 base::ScopedAllowBlockingForTesting allow_blocking;
157 base::ScopedTempDir temp_directory;
158 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
159 base::FilePath temp_file;
161 base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
163 const FileSystemChooseEntryFunction::TestOptions test_options{
164 .path_to_be_picked = &temp_file};
166 FileSystemChooseEntryFunction::SetOptionsForTesting(test_options);
167 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
168 "temp", temp_directory.GetPath());
170 extensions::ExtensionHostTestHelper host_helper(profile());
171 ExtensionTestMessageListener access_ok_listener("restartedFileAccessOK");
172 const Extension* extension =
173 LoadAndLaunchPlatformApp("file_access_restored_test", "fileWritten");
174 ASSERT_TRUE(extension);
176 ExtensionPrefs* extension_prefs =
177 ExtensionPrefs::Get(browser()->profile());
178 SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
179 std::vector<SavedFileEntry> file_entries =
180 saved_files_service->GetAllFileEntries(extension->id());
181 host_helper.WaitForHostDestroyed();
183 // Simulate a restart by populating the preferences as if the browser didn't
184 // get time to clean itself up.
185 extension_prefs->SetExtensionRunning(extension->id(), true);
186 for (std::vector<SavedFileEntry>::const_iterator it = file_entries.begin();
187 it != file_entries.end(); ++it) {
188 saved_files_service->RegisterFileEntry(
189 extension->id(), it->id, it->path, it->is_directory);
192 apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
193 ->HandleStartup(true);
195 EXPECT_TRUE(access_ok_listener.WaitUntilSatisfied());