Upload upstream chromium 67.0.3396
[platform/framework/web/chromium-efl.git] / apps / app_restore_service_browsertest.cc
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"
7 #include "apps/saved_files_service.h"
8 #include "base/threading/thread_restrictions.h"
9 #include "chrome/browser/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"
19
20 using extensions::Extension;
21 using extensions::ExtensionPrefs;
22 using extensions::ExtensionSystem;
23 using extensions::FileSystemChooseEntryFunction;
24 using extensions::SavedFileEntry;
25
26 // TODO(benwells): Move PlatformAppBrowserTest to apps namespace in apps
27 // component.
28 using extensions::PlatformAppBrowserTest;
29
30 namespace apps {
31
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());
37
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());
42
43   // App is running.
44   ASSERT_TRUE(extension_prefs->IsExtensionRunning(extension->id()));
45
46   // Wait for the extension to get suspended.
47   extension_suspended.Wait();
48
49   // App isn't running because it got suspended.
50   ASSERT_FALSE(extension_prefs->IsExtensionRunning(extension->id()));
51
52   // Pretend that the app is supposed to be running.
53   extension_prefs->SetExtensionRunning(extension->id(), true);
54
55   ExtensionTestMessageListener restart_listener("onRestarted", false);
56   apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
57       ->HandleStartup(true);
58   EXPECT_TRUE(restart_listener.WaitUntilSatisfied());
59 }
60
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());
70
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()));
76
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()));
85
86   // Close the window, then open a hidden window and check the app is not
87   // marked active.
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()));
95
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()));
101
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()));
107
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");
113 }
114
115 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, FileAccessIsSavedToPrefs) {
116   content::WindowedNotificationObserver extension_suspended(
117       extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
118       content::NotificationService::AllSources());
119
120   base::ScopedAllowBlockingForTesting allow_blocking;
121   base::ScopedTempDir temp_directory;
122   ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
123   base::FilePath temp_file;
124   ASSERT_TRUE(
125       base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
126
127   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
128       &temp_file);
129   FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
130       "temp", temp_directory.GetPath());
131
132   const Extension* extension = LoadAndLaunchPlatformApp(
133       "file_access_saved_to_prefs_test", "fileWritten");
134   ASSERT_TRUE(extension);
135
136   SavedFilesService* saved_files_service = SavedFilesService::Get(profile());
137
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());
142
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());
147 }
148
149 // Flaky: crbug.com/269613
150 #if defined(OS_LINUX) || defined(OS_WIN)
151 #define MAYBE_FileAccessIsRestored DISABLED_FileAccessIsRestored
152 #else
153 #define MAYBE_FileAccessIsRestored FileAccessIsRestored
154 #endif
155
156 IN_PROC_BROWSER_TEST_F(PlatformAppBrowserTest, MAYBE_FileAccessIsRestored) {
157   content::WindowedNotificationObserver extension_suspended(
158       extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
159       content::NotificationService::AllSources());
160
161   base::ScopedAllowBlockingForTesting allow_blocking;
162   base::ScopedTempDir temp_directory;
163   ASSERT_TRUE(temp_directory.CreateUniqueTempDir());
164   base::FilePath temp_file;
165   ASSERT_TRUE(
166       base::CreateTemporaryFileInDir(temp_directory.GetPath(), &temp_file));
167
168   FileSystemChooseEntryFunction::SkipPickerAndAlwaysSelectPathForTest(
169       &temp_file);
170   FileSystemChooseEntryFunction::RegisterTempExternalFileSystemForTest(
171       "temp", temp_directory.GetPath());
172
173   ExtensionTestMessageListener access_ok_listener(
174       "restartedFileAccessOK", false);
175
176   const Extension* extension =
177       LoadAndLaunchPlatformApp("file_access_restored_test", "fileWritten");
178   ASSERT_TRUE(extension);
179
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();
186
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);
194   }
195
196   apps::AppRestoreServiceFactory::GetForBrowserContext(browser()->profile())
197       ->HandleStartup(true);
198
199   EXPECT_TRUE(access_ok_listener.WaitUntilSatisfied());
200 }
201
202 }  // namespace apps