1 // Copyright 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.
7 #include "chrome/browser/apps/ephemeral_app_browsertest.h"
8 #include "chrome/browser/apps/ephemeral_app_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/test/test_utils.h"
11 #include "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/test_extension_registry_observer.h"
15 using extensions::Extension
;
16 using extensions::ExtensionPrefs
;
17 using extensions::ExtensionRegistry
;
21 const int kNumTestApps
= 2;
22 const char* kTestApps
[] = {
29 class EphemeralAppServiceBrowserTest
: public EphemeralAppTestBase
{
32 for (int i
= 0; i
< kNumTestApps
; ++i
) {
33 const Extension
* extension
= InstallEphemeralApp(kTestApps
[i
]);
34 ASSERT_TRUE(extension
);
35 app_ids_
.push_back(extension
->id());
38 ASSERT_EQ(kNumTestApps
, (int) app_ids_
.size());
41 void GarbageCollectEphemeralApps() {
42 EphemeralAppService
* ephemeral_service
= EphemeralAppService::Get(
43 browser()->profile());
44 ASSERT_TRUE(ephemeral_service
);
45 ephemeral_service
->GarbageCollectApps();
48 void InitEphemeralAppCount(EphemeralAppService
* ephemeral_service
) {
49 ephemeral_service
->InitEphemeralAppCount();
52 void DisableEphemeralAppsOnStartup() {
53 EphemeralAppService
* ephemeral_service
=
54 EphemeralAppService::Get(browser()->profile());
55 ASSERT_TRUE(ephemeral_service
);
56 ephemeral_service
->DisableEphemeralAppsOnStartup();
59 std::vector
<std::string
> app_ids_
;
62 // Verifies that inactive ephemeral apps are uninstalled and active apps are
63 // not removed. Extensive testing of the ephemeral app cache's replacement
64 // policies is done in the unit tests for EphemeralAppService. This is more
65 // like an integration test.
66 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest
,
67 GarbageCollectInactiveApps
) {
68 EphemeralAppService
* ephemeral_service
=
69 EphemeralAppService::Get(browser()->profile());
70 ASSERT_TRUE(ephemeral_service
);
71 InitEphemeralAppCount(ephemeral_service
);
75 const base::Time time_now
= base::Time::Now();
76 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(browser()->profile());
79 // Set launch time for an inactive app.
80 std::string inactive_app_id
= app_ids_
[0];
81 base::Time inactive_launch
= time_now
-
82 base::TimeDelta::FromDays(EphemeralAppService::kAppInactiveThreshold
+ 1);
83 prefs
->SetLastLaunchTime(inactive_app_id
, inactive_launch
);
85 // Set launch time for an active app.
86 std::string active_app_id
= app_ids_
[1];
87 base::Time active_launch
= time_now
-
88 base::TimeDelta::FromDays(EphemeralAppService::kAppKeepThreshold
);
89 prefs
->SetLastLaunchTime(active_app_id
, active_launch
);
91 // Perform garbage collection.
92 extensions::TestExtensionRegistryObserver
observer(
93 ExtensionRegistry::Get(browser()->profile()));
94 GarbageCollectEphemeralApps();
95 observer
.WaitForExtensionUninstalled();
97 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
98 ASSERT_TRUE(registry
);
99 EXPECT_FALSE(registry
->GetExtensionById(inactive_app_id
,
100 ExtensionRegistry::EVERYTHING
));
102 registry
->GetExtensionById(active_app_id
, ExtensionRegistry::EVERYTHING
));
104 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
107 // Verify that the count of ephemeral apps is maintained correctly.
108 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest
, EphemeralAppCount
) {
109 EphemeralAppService
* ephemeral_service
=
110 EphemeralAppService::Get(browser()->profile());
111 ASSERT_TRUE(ephemeral_service
);
112 InitEphemeralAppCount(ephemeral_service
);
114 // The count should not increase for regular installed apps.
115 EXPECT_TRUE(InstallPlatformApp("minimal"));
116 EXPECT_EQ(0, ephemeral_service
->ephemeral_app_count());
118 // The count should increase when an ephemeral app is added.
119 const Extension
* app
= InstallEphemeralApp(kMessagingReceiverApp
);
121 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
123 // The count should remain constant if the ephemeral app is updated.
124 const std::string app_id
= app
->id();
125 app
= UpdateEphemeralApp(
126 app_id
, GetTestPath(kMessagingReceiverAppV2
),
127 GetTestPath(kMessagingReceiverApp
).ReplaceExtension(
128 FILE_PATH_LITERAL(".pem")));
130 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
132 // The count should decrease when an ephemeral app is promoted to a regular
134 PromoteEphemeralApp(app
);
135 EXPECT_EQ(0, ephemeral_service
->ephemeral_app_count());
138 // Verify that the cache of ephemeral apps is correctly cleared. Running apps
139 // should not be removed.
140 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest
, ClearCachedApps
) {
141 const Extension
* running_app
=
142 InstallAndLaunchEphemeralApp(kMessagingReceiverApp
);
143 const Extension
* inactive_app
=
144 InstallAndLaunchEphemeralApp(kDispatchEventTestApp
);
145 std::string inactive_app_id
= inactive_app
->id();
146 std::string running_app_id
= running_app
->id();
147 CloseAppWaitForUnload(inactive_app_id
);
149 EphemeralAppService
* ephemeral_service
=
150 EphemeralAppService::Get(browser()->profile());
151 ASSERT_TRUE(ephemeral_service
);
152 EXPECT_EQ(2, ephemeral_service
->ephemeral_app_count());
154 ephemeral_service
->ClearCachedApps();
156 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
157 ASSERT_TRUE(registry
);
158 EXPECT_FALSE(registry
->GetExtensionById(inactive_app_id
,
159 ExtensionRegistry::EVERYTHING
));
160 EXPECT_TRUE(registry
->GetExtensionById(running_app_id
,
161 ExtensionRegistry::EVERYTHING
));
163 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
166 // Verify that the service will unload and disable ephemeral apps on startup.
167 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest
,
168 DisableEphemeralAppsOnStartup
) {
169 const Extension
* installed_app
= InstallPlatformApp(kNotificationsTestApp
);
170 const Extension
* running_app
=
171 InstallAndLaunchEphemeralApp(kMessagingReceiverApp
);
172 const Extension
* inactive_app
= InstallEphemeralApp(kDispatchEventTestApp
);
173 const Extension
* disabled_app
= InstallEphemeralApp(kFileSystemTestApp
);
174 ASSERT_TRUE(installed_app
);
175 ASSERT_TRUE(running_app
);
176 ASSERT_TRUE(inactive_app
);
177 ASSERT_TRUE(disabled_app
);
178 DisableEphemeralApp(disabled_app
, Extension::DISABLE_PERMISSIONS_INCREASE
);
180 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
181 ASSERT_TRUE(registry
);
182 EXPECT_TRUE(registry
->enabled_extensions().Contains(installed_app
->id()));
183 EXPECT_TRUE(registry
->enabled_extensions().Contains(running_app
->id()));
184 EXPECT_TRUE(registry
->enabled_extensions().Contains(inactive_app
->id()));
185 EXPECT_TRUE(registry
->disabled_extensions().Contains(disabled_app
->id()));
187 DisableEphemeralAppsOnStartup();
189 // Verify that the inactive app is disabled.
190 EXPECT_TRUE(registry
->enabled_extensions().Contains(installed_app
->id()));
191 EXPECT_TRUE(registry
->enabled_extensions().Contains(running_app
->id()));
192 EXPECT_TRUE(registry
->disabled_extensions().Contains(inactive_app
->id()));
193 EXPECT_TRUE(registry
->disabled_extensions().Contains(disabled_app
->id()));
195 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(profile());
197 EXPECT_FALSE(prefs
->HasDisableReason(
198 installed_app
->id(), Extension::DISABLE_INACTIVE_EPHEMERAL_APP
));
199 EXPECT_FALSE(prefs
->HasDisableReason(
200 running_app
->id(), Extension::DISABLE_INACTIVE_EPHEMERAL_APP
));
201 EXPECT_TRUE(prefs
->HasDisableReason(
202 inactive_app
->id(), Extension::DISABLE_INACTIVE_EPHEMERAL_APP
));
203 EXPECT_TRUE(prefs
->HasDisableReason(
204 disabled_app
->id(), Extension::DISABLE_INACTIVE_EPHEMERAL_APP
));
205 EXPECT_TRUE(prefs
->HasDisableReason(
206 disabled_app
->id(), Extension::DISABLE_PERMISSIONS_INCREASE
));