blob: 017b97c7b86aa9527acf52a306f7eae3560dded1 [file] [log] [blame]
[email protected]c2e2b6d2013-01-22 02:23:291// 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.
4
5#include "apps/app_restore_service.h"
6#include "apps/app_restore_service_factory.h"
[email protected]961745f2013-05-25 14:09:247#include "apps/saved_files_service.h"
jam3f2d3932017-04-26 20:28:518#include "base/threading/thread_restrictions.h"
Dominick Ng6ff51052018-07-06 05:30:209#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
michaelpg4d80e562017-04-04 01:48:1410#include "content/public/browser/browser_context.h"
[email protected]5a0613d32013-06-17 20:06:5311#include "content/public/browser/notification_service.h"
Peter Kasting919ce652020-05-07 10:22:3612#include "content/public/test/browser_test.h"
[email protected]c2e2b6d2013-01-22 02:23:2913#include "content/public/test/test_utils.h"
michaelpg56c27b32017-07-14 01:35:2914#include "extensions/browser/api/file_system/file_system_api.h"
michaelpg868a94be2017-06-26 16:55:2515#include "extensions/browser/api/file_system/saved_file_entry.h"
[email protected]489db0842014-01-22 18:20:0316#include "extensions/browser/extension_prefs.h"
[email protected]adf5a102014-07-31 12:44:0617#include "extensions/browser/notification_types.h"
[email protected]e4452d32013-11-15 23:07:4118#include "extensions/common/extension.h"
lfg910f2f92014-09-19 05:31:0919#include "extensions/test/extension_test_message_listener.h"
[email protected]c2e2b6d2013-01-22 02:23:2920
21using extensions::Extension;
22using extensions::ExtensionPrefs;
23using extensions::ExtensionSystem;
[email protected]b897ff82013-02-27 19:50:1324using extensions::FileSystemChooseEntryFunction;
michaelpg868a94be2017-06-26 16:55:2525using extensions::SavedFileEntry;
[email protected]b897ff82013-02-27 19:50:1326
[email protected]c2e2b6d2013-01-22 02:23:2927// TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
28// component.
29using extensions::PlatformAppBrowserTest;
30
31namespace apps {
32
33// Tests that a running app is recorded in the preferences as such.
34IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
35 content::WindowedNotificationObserver extension_suspended(
[email protected]adf5a102014-07-31 12:44:0636 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
[email protected]c2e2b6d2013-01-22 02:23:2937 content::NotificationService::AllSources());
38
39 const Extension* extension = LoadExtension(
40 test_data_dir_.AppendASCII("platform_apps/restart_test"));
41 ASSERT_TRUE(extension);
[email protected]836e2982013-05-16 08:07:4242 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
[email protected]c2e2b6d2013-01-22 02:23:2943
44 // App is running.
45 ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
46
47 // Wait for the extension to get suspended.
48 extension_suspended.Wait();
49
50 // App isn't running because it got suspended.
51 ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
52
53 // Pretend that the app is supposed to be running.
54 extension_prefs->SetExtensionRunning(extension->id(), true);
55
56 ExtensionTestMessageListener restart_listener("onRestarted", false);
michaelpg4d80e562017-04-04 01:48:1457 apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
58 ->HandleStartup(true);
Devlin Croninf34c8ee2018-04-05 01:29:2159 EXPECT_TRUE(restart_listener.WaitUntilSatisfied());
[email protected]c2e2b6d2013-01-22 02:23:2960}
61
[email protected]7aadf69a42014-05-15 07:15:5062// Tests that apps are recorded in the preferences as active when and only when
63// they have visible windows.
64IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ActiveAppsAreRecorded) {
65 ExtensionTestMessageListener ready_listener("ready", true);
66 const Extension* extension =
67 LoadExtension(test_data_dir_.AppendASCII("platform_apps/active_test"));
68 ASSERT_TRUE(extension);
69 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
70 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
71
72 // Open a visible window and check the app is marked active.
73 ready_listener.Reply("create");
74 ready_listener.Reset();
75 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
76 ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
77
78 // Close the window, then open a minimized window and check the app is active.
79 ready_listener.Reply("closeLastWindow");
80 ready_listener.Reset();
81 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
82 ready_listener.Reply("createMinimized");
83 ready_listener.Reset();
84 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
85 ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
86
87 // Close the window, then open a hidden window and check the app is not
88 // marked active.
89 ready_listener.Reply("closeLastWindow");
90 ready_listener.Reset();
91 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
92 ready_listener.Reply("createHidden");
93 ready_listener.Reset();
94 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
95 ASSERT_FALSE(extension_prefs->IsActive(extension->id()));
96
97 // Open another window and check the app is marked active.
98 ready_listener.Reply("create");
99 ready_listener.Reset();
100 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
101 ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
102
103 // Close the visible window and check the app has been marked inactive.
104 ready_listener.Reply("closeLastWindow");
105 ready_listener.Reset();
106 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
107 ASSERT_FALSE(extension_prefs->IsActive(extension->id()));
108
109 // Close the last window and exit.
110 ready_listener.Reply("closeLastWindow");
111 ready_listener.Reset();
112 ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
113 ready_listener.Reply("exit");
114}
115
[email protected]b897ff82013-02-27 19:50:13116IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) {
117 content::WindowedNotificationObserver extension_suspended(
[email protected]adf5a102014-07-31 12:44:06118 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
[email protected]b897ff82013-02-27 19:50:13119 content::NotificationService::AllSources());
120
Francois Doraye6fb2d02017-10-18 21:29:13121 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]b897ff82013-02-27 19:50:13122 base::ScopedTempDir temp_directory;
123 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
124 base::FilePath temp_file;
vabr16e5f602016-09-15 18:14:00125 ASSERT_TRUE(
126 base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
[email protected]b897ff82013-02-27 19:50:13127
128 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
129 &temp_file);
[email protected]5025efd2013-03-25 14:15:53130 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
vabr16e5f602016-09-15 18:14:00131 "temp", temp_directory.GetPath());
[email protected]b897ff82013-02-27 19:50:13132
[email protected]599f9822014-06-17 09:41:27133 const Extension* extension = LoadAndLaunchPlatformApp(
134 "file_access_saved_to_prefs_test", "fileWritten");
[email protected]b897ff82013-02-27 19:50:13135 ASSERT_TRUE(extension);
[email protected]b897ff82013-02-27 19:50:13136
[email protected]961745f2013-05-25 14:09:24137 SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
[email protected]b897ff82013-02-27 19:50:13138
[email protected]961745f2013-05-25 14:09:24139 std::vector<SavedFileEntry> file_entries =
140 saved_files_service->GetAllFileEntries(extension->id());
[email protected]b897ff82013-02-27 19:50:13141 // One for the read-only file entry and one for the writable file entry.
142 ASSERT_EQ(2u, file_entries.size());
143
144 extension_suspended.Wait();
[email protected]961745f2013-05-25 14:09:24145 file_entries = saved_files_service->GetAllFileEntries(extension->id());
[email protected]b897ff82013-02-27 19:50:13146 // File entries should be cleared when the extension is suspended.
147 ASSERT_TRUE(file_entries.empty());
148}
149
[email protected]5a1eca32013-08-07 20:55:48150// Flaky: crbug.com/269613
[email protected]4f46f972013-08-29 17:40:51151#if defined(OS_LINUX) || defined(OS_WIN)
[email protected]5a1eca32013-08-07 20:55:48152#define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
153#else
154#define MAYBE_FileAccessIsRestored FileAccessIsRestored
155#endif
156
157IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
[email protected]fc2a40f2013-03-13 13:14:57158 content::WindowedNotificationObserver extension_suspended(
[email protected]adf5a102014-07-31 12:44:06159 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
[email protected]fc2a40f2013-03-13 13:14:57160 content::NotificationService::AllSources());
161
Francois Doraye6fb2d02017-10-18 21:29:13162 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]fc2a40f2013-03-13 13:14:57163 base::ScopedTempDir temp_directory;
164 ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
165 base::FilePath temp_file;
vabr16e5f602016-09-15 18:14:00166 ASSERT_TRUE(
167 base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
[email protected]fc2a40f2013-03-13 13:14:57168
169 FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
170 &temp_file);
[email protected]5025efd2013-03-25 14:15:53171 FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
vabr16e5f602016-09-15 18:14:00172 "temp", temp_directory.GetPath());
[email protected]fc2a40f2013-03-13 13:14:57173
[email protected]fc2a40f2013-03-13 13:14:57174 ExtensionTestMessageListener access_ok_listener(
175 "restartedFileAccessOK", false);
176
177 const Extension* extension =
[email protected]599f9822014-06-17 09:41:27178 LoadAndLaunchPlatformApp("file_access_restored_test", "fileWritten");
[email protected]fc2a40f2013-03-13 13:14:57179 ASSERT_TRUE(extension);
[email protected]fc2a40f2013-03-13 13:14:57180
[email protected]de0f8b42013-05-08 20:59:51181 ExtensionPrefs* extension_prefs =
[email protected]836e2982013-05-16 08:07:42182 ExtensionPrefs::Get(browser()->profile());
[email protected]961745f2013-05-25 14:09:24183 SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
184 std::vector<SavedFileEntry> file_entries =
185 saved_files_service->GetAllFileEntries(extension->id());
[email protected]fc2a40f2013-03-13 13:14:57186 extension_suspended.Wait();
187
188 // Simulate a restart by populating the preferences as if the browser didn't
189 // get time to clean itself up.
190 extension_prefs->SetExtensionRunning(extension->id(), true);
191 for (std::vector<SavedFileEntry>::const_iterator it = file_entries.begin();
192 it != file_entries.end(); ++it) {
[email protected]6b7ecdd2013-08-29 14:16:24193 saved_files_service->RegisterFileEntry(
194 extension->id(), it->id, it->path, it->is_directory);
[email protected]fc2a40f2013-03-13 13:14:57195 }
196
michaelpg4d80e562017-04-04 01:48:14197 apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
198 ->HandleStartup(true);
[email protected]fc2a40f2013-03-13 13:14:57199
Devlin Croninf34c8ee2018-04-05 01:29:21200 EXPECT_TRUE(access_ok_listener.WaitUntilSatisfied());
[email protected]fc2a40f2013-03-13 13:14:57201}
202
[email protected]c2e2b6d2013-01-22 02:23:29203} // namespace apps