[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "apps/app_restore_service.h" |
| 6 | #include "apps/app_restore_service_factory.h" |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 7 | #include "apps/saved_files_service.h" |
jam | 3f2d393 | 2017-04-26 20:28:51 | [diff] [blame] | 8 | #include "base/threading/thread_restrictions.h" |
Dominick Ng | 6ff5105 | 2018-07-06 05:30:20 | [diff] [blame] | 9 | #include "chrome/browser/apps/platform_apps/app_browsertest_util.h" |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
[email protected] | 5a0613d3 | 2013-06-17 20:06:53 | [diff] [blame] | 11 | #include "content/public/browser/notification_service.h" |
Peter Kasting | 919ce65 | 2020-05-07 10:22:36 | [diff] [blame] | 12 | #include "content/public/test/browser_test.h" |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 13 | #include "content/public/test/test_utils.h" |
michaelpg | 56c27b3 | 2017-07-14 01:35:29 | [diff] [blame] | 14 | #include "extensions/browser/api/file_system/file_system_api.h" |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 15 | #include "extensions/browser/api/file_system/saved_file_entry.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 16 | #include "extensions/browser/extension_prefs.h" |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 17 | #include "extensions/browser/notification_types.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 18 | #include "extensions/common/extension.h" |
lfg | 910f2f9 | 2014-09-19 05:31:09 | [diff] [blame] | 19 | #include "extensions/test/extension_test_message_listener.h" |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 20 | |
| 21 | using extensions::Extension; |
| 22 | using extensions::ExtensionPrefs; |
| 23 | using extensions::ExtensionSystem; |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 24 | using extensions::FileSystemChooseEntryFunction; |
michaelpg | 868a94be | 2017-06-26 16:55:25 | [diff] [blame] | 25 | using extensions::SavedFileEntry; |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 26 | |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 27 | // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps |
| 28 | // component. |
| 29 | using extensions::PlatformAppBrowserTest; |
| 30 | |
| 31 | namespace apps { |
| 32 | |
| 33 | // Tests that a running app is recorded in the preferences as such. |
| 34 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) { |
| 35 | content::WindowedNotificationObserver extension_suspended( |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 36 | extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 37 | content::NotificationService::AllSources()); |
| 38 | |
| 39 | const Extension* extension = LoadExtension( |
| 40 | test_data_dir_.AppendASCII("platform_apps/restart_test")); |
| 41 | ASSERT_TRUE(extension); |
[email protected] | 836e298 | 2013-05-16 08:07:42 | [diff] [blame] | 42 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile()); |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 43 | |
| 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); |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 57 | apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile()) |
| 58 | ->HandleStartup(true); |
Devlin Cronin | f34c8ee | 2018-04-05 01:29:21 | [diff] [blame] | 59 | EXPECT_TRUE(restart_listener.WaitUntilSatisfied()); |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 60 | } |
| 61 | |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 62 | // Tests that apps are recorded in the preferences as active when and only when |
| 63 | // they have visible windows. |
| 64 | IN_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] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 116 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) { |
| 117 | content::WindowedNotificationObserver extension_suspended( |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 118 | extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 119 | content::NotificationService::AllSources()); |
| 120 | |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 121 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 122 | base::ScopedTempDir temp_directory; |
| 123 | ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 124 | base::FilePath temp_file; |
vabr | 16e5f60 | 2016-09-15 18:14:00 | [diff] [blame] | 125 | ASSERT_TRUE( |
| 126 | base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file)); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 127 | |
| 128 | FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 129 | &temp_file); |
[email protected] | 5025efd | 2013-03-25 14:15:53 | [diff] [blame] | 130 | FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest( |
vabr | 16e5f60 | 2016-09-15 18:14:00 | [diff] [blame] | 131 | "temp", temp_directory.GetPath()); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 132 | |
[email protected] | 599f982 | 2014-06-17 09:41:27 | [diff] [blame] | 133 | const Extension* extension = LoadAndLaunchPlatformApp( |
| 134 | "file_access_saved_to_prefs_test", "fileWritten"); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 135 | ASSERT_TRUE(extension); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 136 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 137 | SavedFilesService* saved_files_service = SavedFilesService::Get(profile()); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 138 | |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 139 | std::vector<SavedFileEntry> file_entries = |
| 140 | saved_files_service->GetAllFileEntries(extension->id()); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 141 | // 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] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 145 | file_entries = saved_files_service->GetAllFileEntries(extension->id()); |
[email protected] | b897ff8 | 2013-02-27 19:50:13 | [diff] [blame] | 146 | // File entries should be cleared when the extension is suspended. |
| 147 | ASSERT_TRUE(file_entries.empty()); |
| 148 | } |
| 149 | |
[email protected] | 5a1eca3 | 2013-08-07 20:55:48 | [diff] [blame] | 150 | // Flaky: crbug.com/269613 |
[email protected] | 4f46f97 | 2013-08-29 17:40:51 | [diff] [blame] | 151 | #if defined(OS_LINUX) || defined(OS_WIN) |
[email protected] | 5a1eca3 | 2013-08-07 20:55:48 | [diff] [blame] | 152 | #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored |
| 153 | #else |
| 154 | #define MAYBE_FileAccessIsRestored FileAccessIsRestored |
| 155 | #endif |
| 156 | |
| 157 | IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) { |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 158 | content::WindowedNotificationObserver extension_suspended( |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 159 | extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 160 | content::NotificationService::AllSources()); |
| 161 | |
Francois Doray | e6fb2d0 | 2017-10-18 21:29:13 | [diff] [blame] | 162 | base::ScopedAllowBlockingForTesting allow_blocking; |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 163 | base::ScopedTempDir temp_directory; |
| 164 | ASSERT_TRUE(temp_directory.CreateUniqueTempDir()); |
| 165 | base::FilePath temp_file; |
vabr | 16e5f60 | 2016-09-15 18:14:00 | [diff] [blame] | 166 | ASSERT_TRUE( |
| 167 | base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file)); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 168 | |
| 169 | FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest( |
| 170 | &temp_file); |
[email protected] | 5025efd | 2013-03-25 14:15:53 | [diff] [blame] | 171 | FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest( |
vabr | 16e5f60 | 2016-09-15 18:14:00 | [diff] [blame] | 172 | "temp", temp_directory.GetPath()); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 173 | |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 174 | ExtensionTestMessageListener access_ok_listener( |
| 175 | "restartedFileAccessOK", false); |
| 176 | |
| 177 | const Extension* extension = |
[email protected] | 599f982 | 2014-06-17 09:41:27 | [diff] [blame] | 178 | LoadAndLaunchPlatformApp("file_access_restored_test", "fileWritten"); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 179 | ASSERT_TRUE(extension); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 180 | |
[email protected] | de0f8b4 | 2013-05-08 20:59:51 | [diff] [blame] | 181 | ExtensionPrefs* extension_prefs = |
[email protected] | 836e298 | 2013-05-16 08:07:42 | [diff] [blame] | 182 | ExtensionPrefs::Get(browser()->profile()); |
[email protected] | 961745f | 2013-05-25 14:09:24 | [diff] [blame] | 183 | SavedFilesService* saved_files_service = SavedFilesService::Get(profile()); |
| 184 | std::vector<SavedFileEntry> file_entries = |
| 185 | saved_files_service->GetAllFileEntries(extension->id()); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 186 | 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] | 6b7ecdd | 2013-08-29 14:16:24 | [diff] [blame] | 193 | saved_files_service->RegisterFileEntry( |
| 194 | extension->id(), it->id, it->path, it->is_directory); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 195 | } |
| 196 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 197 | apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile()) |
| 198 | ->HandleStartup(true); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 199 | |
Devlin Cronin | f34c8ee | 2018-04-05 01:29:21 | [diff] [blame] | 200 | EXPECT_TRUE(access_ok_listener.WaitUntilSatisfied()); |
[email protected] | fc2a40f | 2013-03-13 13:14:57 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | c2e2b6d | 2013-01-22 02:23:29 | [diff] [blame] | 203 | } // namespace apps |