OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <vector> | 5 #include <vector> |
6 | 6 |
7 #include "chrome/browser/apps/ephemeral_app_browsertest.h" | 7 #include "chrome/browser/apps/ephemeral_app_browsertest.h" |
8 #include "chrome/browser/apps/ephemeral_app_service.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" | 9 #include "extensions/browser/extension_registry.h" |
13 #include "extensions/browser/test_extension_registry_observer.h" | |
14 | 10 |
15 using extensions::Extension; | 11 using extensions::Extension; |
16 using extensions::ExtensionPrefs; | |
17 using extensions::ExtensionRegistry; | 12 using extensions::ExtensionRegistry; |
18 | 13 |
19 namespace { | 14 class EphemeralAppServiceBrowserTest : public EphemeralAppTestBase {}; |
20 | 15 |
21 const int kNumTestApps = 2; | 16 // Verify that the cache of ephemeral apps is correctly cleared. All ephemeral |
22 const char* kTestApps[] = { | 17 // apps should be removed. |
23 "app_window/generic", | |
24 "minimal" | |
25 }; | |
26 | |
27 } // namespace | |
28 | |
29 class EphemeralAppServiceBrowserTest : public EphemeralAppTestBase { | |
30 protected: | |
31 void LoadApps() { | |
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()); | |
36 } | |
37 | |
38 ASSERT_EQ(kNumTestApps, (int) app_ids_.size()); | |
39 } | |
40 | |
41 void GarbageCollectEphemeralApps() { | |
42 EphemeralAppService* ephemeral_service = EphemeralAppService::Get( | |
43 browser()->profile()); | |
44 ASSERT_TRUE(ephemeral_service); | |
45 ephemeral_service->GarbageCollectApps(); | |
46 } | |
47 | |
48 void InitEphemeralAppCount(EphemeralAppService* ephemeral_service) { | |
49 ephemeral_service->InitEphemeralAppCount(); | |
50 } | |
51 | |
52 void DisableEphemeralAppsOnStartup() { | |
53 EphemeralAppService* ephemeral_service = | |
54 EphemeralAppService::Get(browser()->profile()); | |
55 ASSERT_TRUE(ephemeral_service); | |
56 ephemeral_service->DisableEphemeralAppsOnStartup(); | |
57 } | |
58 | |
59 std::vector<std::string> app_ids_; | |
60 }; | |
61 | |
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); | |
72 | |
73 LoadApps(); | |
74 | |
75 const base::Time time_now = base::Time::Now(); | |
76 ExtensionPrefs* prefs = ExtensionPrefs::Get(browser()->profile()); | |
77 ASSERT_TRUE(prefs); | |
78 | |
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); | |
84 | |
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); | |
90 | |
91 // Perform garbage collection. | |
92 extensions::TestExtensionRegistryObserver observer( | |
93 ExtensionRegistry::Get(browser()->profile())); | |
94 GarbageCollectEphemeralApps(); | |
95 observer.WaitForExtensionUninstalled(); | |
96 | |
97 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | |
98 ASSERT_TRUE(registry); | |
99 EXPECT_FALSE(registry->GetExtensionById(inactive_app_id, | |
100 ExtensionRegistry::EVERYTHING)); | |
101 EXPECT_TRUE( | |
102 registry->GetExtensionById(active_app_id, ExtensionRegistry::EVERYTHING)); | |
103 | |
104 EXPECT_EQ(1, ephemeral_service->ephemeral_app_count()); | |
105 } | |
106 | |
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); | |
113 | |
114 // The count should not increase for regular installed apps. | |
115 EXPECT_TRUE(InstallPlatformApp("minimal")); | |
116 EXPECT_EQ(0, ephemeral_service->ephemeral_app_count()); | |
117 | |
118 // The count should increase when an ephemeral app is added. | |
119 const Extension* app = InstallEphemeralApp(kMessagingReceiverApp); | |
120 ASSERT_TRUE(app); | |
121 EXPECT_EQ(1, ephemeral_service->ephemeral_app_count()); | |
122 | |
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"))); | |
129 ASSERT_TRUE(app); | |
130 EXPECT_EQ(1, ephemeral_service->ephemeral_app_count()); | |
131 | |
132 // The count should decrease when an ephemeral app is promoted to a regular | |
133 // installed app. | |
134 PromoteEphemeralApp(app); | |
135 EXPECT_EQ(0, ephemeral_service->ephemeral_app_count()); | |
136 } | |
137 | |
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) { | 18 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest, ClearCachedApps) { |
141 const Extension* running_app = | 19 const Extension* running_app = |
142 InstallAndLaunchEphemeralApp(kMessagingReceiverApp); | 20 InstallAndLaunchEphemeralApp(kMessagingReceiverApp); |
143 const Extension* inactive_app = | 21 const Extension* inactive_app = |
144 InstallAndLaunchEphemeralApp(kDispatchEventTestApp); | 22 InstallAndLaunchEphemeralApp(kDispatchEventTestApp); |
145 std::string inactive_app_id = inactive_app->id(); | 23 std::string inactive_app_id = inactive_app->id(); |
146 std::string running_app_id = running_app->id(); | 24 std::string running_app_id = running_app->id(); |
147 CloseAppWaitForUnload(inactive_app_id); | 25 CloseAppWaitForUnload(inactive_app_id); |
148 | 26 |
149 EphemeralAppService* ephemeral_service = | 27 EphemeralAppService* ephemeral_service = |
150 EphemeralAppService::Get(browser()->profile()); | 28 EphemeralAppService::Get(browser()->profile()); |
151 ASSERT_TRUE(ephemeral_service); | 29 ASSERT_TRUE(ephemeral_service); |
152 EXPECT_EQ(2, ephemeral_service->ephemeral_app_count()); | 30 |
| 31 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
| 32 ASSERT_TRUE(registry); |
| 33 |
| 34 int extension_count = registry->GenerateInstalledExtensionsSet()->size(); |
153 | 35 |
154 ephemeral_service->ClearCachedApps(); | 36 ephemeral_service->ClearCachedApps(); |
155 | 37 |
156 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | |
157 ASSERT_TRUE(registry); | |
158 EXPECT_FALSE(registry->GetExtensionById(inactive_app_id, | 38 EXPECT_FALSE(registry->GetExtensionById(inactive_app_id, |
159 ExtensionRegistry::EVERYTHING)); | 39 ExtensionRegistry::EVERYTHING)); |
160 EXPECT_TRUE(registry->GetExtensionById(running_app_id, | 40 EXPECT_FALSE(registry->GetExtensionById(running_app_id, |
161 ExtensionRegistry::EVERYTHING)); | 41 ExtensionRegistry::EVERYTHING)); |
162 | 42 |
163 EXPECT_EQ(1, ephemeral_service->ephemeral_app_count()); | 43 int new_extension_count = registry->GenerateInstalledExtensionsSet()->size(); |
| 44 EXPECT_EQ(2, extension_count - new_extension_count); |
164 } | 45 } |
165 | |
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); | |
179 | |
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())); | |
186 | |
187 DisableEphemeralAppsOnStartup(); | |
188 | |
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())); | |
194 | |
195 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile()); | |
196 ASSERT_TRUE(prefs); | |
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)); | |
207 } | |
OLD | NEW |