app_restore_service_browsertest.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "apps/app_restore_service.h"
  5. #include "apps/app_restore_service_factory.h"
  6. #include "apps/saved_files_service.h"
  7. #include "base/files/file_util.h"
  8. #include "base/threading/thread_restrictions.h"
  9. #include "build/build_config.h"
  10. #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
  11. #include "content/public/browser/browser_context.h"
  12. #include "content/public/test/browser_test.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_host_test_helper.h"
  16. #include "extensions/browser/extension_prefs.h"
  17. #include "extensions/common/extension.h"
  18. #include "extensions/test/extension_test_message_listener.h"
  19. using extensions::Extension;
  20. using extensions::ExtensionPrefs;
  21. using extensions::ExtensionSystem;
  22. using extensions::FileSystemChooseEntryFunction;
  23. using extensions::SavedFileEntry;
  24. // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
  25. // component.
  26. using extensions::PlatformAppBrowserTest;
  27. namespace apps {
  28. // Tests that a running app is recorded in the preferences as such.
  29. IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, RunningAppsAreRecorded) {
  30. extensions::ExtensionHostTestHelper host_helper(profile());
  31. const Extension* extension = LoadExtension(
  32. test_data_dir_.AppendASCII("platform_apps/restart_test"));
  33. ASSERT_TRUE(extension);
  34. ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
  35. // App is running.
  36. ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
  37. // Wait for the extension to get suspended.
  38. host_helper.WaitForHostDestroyed();
  39. // App isn't running because it got suspended.
  40. ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
  41. // Pretend that the app is supposed to be running.
  42. extension_prefs->SetExtensionRunning(extension->id(), true);
  43. ExtensionTestMessageListener restart_listener("onRestarted");
  44. apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
  45. ->HandleStartup(true);
  46. EXPECT_TRUE(restart_listener.WaitUntilSatisfied());
  47. }
  48. // Tests that apps are recorded in the preferences as active when and only when
  49. // they have visible windows.
  50. IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, ActiveAppsAreRecorded) {
  51. ExtensionTestMessageListener ready_listener("ready",
  52. ReplyBehavior::kWillReply);
  53. const Extension* extension =
  54. LoadExtension(test_data_dir_.AppendASCII("platform_apps/active_test"));
  55. ASSERT_TRUE(extension);
  56. ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser()->profile());
  57. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  58. // Open a visible window and check the app is marked active.
  59. ready_listener.Reply("create");
  60. ready_listener.Reset();
  61. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  62. ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
  63. // Close the window, then open a minimized window and check the app is active.
  64. ready_listener.Reply("closeLastWindow");
  65. ready_listener.Reset();
  66. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  67. ready_listener.Reply("createMinimized");
  68. ready_listener.Reset();
  69. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  70. ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
  71. // Close the window, then open a hidden window and check the app is not
  72. // marked active.
  73. ready_listener.Reply("closeLastWindow");
  74. ready_listener.Reset();
  75. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  76. ready_listener.Reply("createHidden");
  77. ready_listener.Reset();
  78. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  79. ASSERT_FALSE(extension_prefs->IsActive(extension->id()));
  80. // Open another window and check the app is marked active.
  81. ready_listener.Reply("create");
  82. ready_listener.Reset();
  83. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  84. ASSERT_TRUE(extension_prefs->IsActive(extension->id()));
  85. // Close the visible window and check the app has been marked inactive.
  86. ready_listener.Reply("closeLastWindow");
  87. ready_listener.Reset();
  88. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  89. ASSERT_FALSE(extension_prefs->IsActive(extension->id()));
  90. // Close the last window and exit.
  91. ready_listener.Reply("closeLastWindow");
  92. ready_listener.Reset();
  93. ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
  94. ready_listener.Reply("exit");
  95. }
  96. IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) {
  97. base::ScopedAllowBlockingForTesting allow_blocking;
  98. base::ScopedTempDir temp_directory;
  99. ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  100. base::FilePath temp_file;
  101. ASSERT_TRUE(
  102. base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
  103. FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest picker(
  104. temp_file);
  105. FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
  106. "temp", temp_directory.GetPath());
  107. extensions::ExtensionHostTestHelper host_helper(profile());
  108. const Extension* extension = LoadAndLaunchPlatformApp(
  109. "file_access_saved_to_prefs_test", "fileWritten");
  110. ASSERT_TRUE(extension);
  111. SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
  112. std::vector<SavedFileEntry> file_entries =
  113. saved_files_service->GetAllFileEntries(extension->id());
  114. // One for the read-only file entry and one for the writable file entry.
  115. ASSERT_EQ(2u, file_entries.size());
  116. host_helper.WaitForHostDestroyed();
  117. file_entries = saved_files_service->GetAllFileEntries(extension->id());
  118. // File entries should be cleared when the extension is suspended.
  119. ASSERT_TRUE(file_entries.empty());
  120. }
  121. // Flaky: crbug.com/269613
  122. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
  123. #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
  124. #else
  125. #define MAYBE_FileAccessIsRestored FileAccessIsRestored
  126. #endif
  127. IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
  128. base::ScopedAllowBlockingForTesting allow_blocking;
  129. base::ScopedTempDir temp_directory;
  130. ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
  131. base::FilePath temp_file;
  132. ASSERT_TRUE(
  133. base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
  134. FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest picker(
  135. temp_file);
  136. FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
  137. "temp", temp_directory.GetPath());
  138. extensions::ExtensionHostTestHelper host_helper(profile());
  139. ExtensionTestMessageListener access_ok_listener("restartedFileAccessOK");
  140. const Extension* extension =
  141. LoadAndLaunchPlatformApp("file_access_restored_test", "fileWritten");
  142. ASSERT_TRUE(extension);
  143. ExtensionPrefs* extension_prefs =
  144. ExtensionPrefs::Get(browser()->profile());
  145. SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
  146. std::vector<SavedFileEntry> file_entries =
  147. saved_files_service->GetAllFileEntries(extension->id());
  148. host_helper.WaitForHostDestroyed();
  149. // Simulate a restart by populating the preferences as if the browser didn't
  150. // get time to clean itself up.
  151. extension_prefs->SetExtensionRunning(extension->id(), true);
  152. for (std::vector<SavedFileEntry>::const_iterator it = file_entries.begin();
  153. it != file_entries.end(); ++it) {
  154. saved_files_service->RegisterFileEntry(
  155. extension->id(), it->id, it->path, it->is_directory);
  156. }
  157. apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
  158. ->HandleStartup(true);
  159. EXPECT_TRUE(access_ok_listener.WaitUntilSatisfied());
  160. }
  161. } // namespace apps