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/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "content/public/test/test_utils.h"
12 #include "extensions/browser/extension_prefs.h"
13 #include "extensions/browser/extension_system.h"
14 #include "extensions/common/manifest.h"
16 using extensions::Extension
;
17 using extensions::ExtensionPrefs
;
18 using extensions::ExtensionSystem
;
22 const int kNumTestApps
= 2;
23 const char* kTestApps
[] = {
30 class EphemeralAppServiceBrowserTest
: public EphemeralAppTestBase
{
33 for (int i
= 0; i
< kNumTestApps
; ++i
) {
34 const Extension
* extension
= InstallEphemeralApp(kTestApps
[i
]);
35 ASSERT_TRUE(extension
);
36 app_ids_
.push_back(extension
->id());
39 ASSERT_EQ(kNumTestApps
, (int) app_ids_
.size());
42 void GarbageCollectEphemeralApps() {
43 EphemeralAppService
* ephemeral_service
= EphemeralAppService::Get(
44 browser()->profile());
45 ASSERT_TRUE(ephemeral_service
);
46 ephemeral_service
->GarbageCollectApps();
49 void InitEphemeralAppCount(EphemeralAppService
* ephemeral_service
) {
50 ephemeral_service
->InitEphemeralAppCount();
53 std::vector
<std::string
> app_ids_
;
56 // Verifies that inactive ephemeral apps are uninstalled and active apps are
57 // not removed. Extensive testing of the ephemeral app cache's replacement
58 // policies is done in the unit tests for EphemeralAppService. This is more
59 // like an integration test.
60 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest
,
61 GarbageCollectInactiveApps
) {
62 EphemeralAppService
* ephemeral_service
=
63 EphemeralAppService::Get(browser()->profile());
64 ASSERT_TRUE(ephemeral_service
);
65 InitEphemeralAppCount(ephemeral_service
);
69 const base::Time time_now
= base::Time::Now();
70 ExtensionPrefs
* prefs
= ExtensionPrefs::Get(browser()->profile());
73 // Set launch time for an inactive app.
74 std::string inactive_app_id
= app_ids_
[0];
75 base::Time inactive_launch
= time_now
-
76 base::TimeDelta::FromDays(EphemeralAppService::kAppInactiveThreshold
+ 1);
77 prefs
->SetLastLaunchTime(inactive_app_id
, inactive_launch
);
79 // Set launch time for an active app.
80 std::string active_app_id
= app_ids_
[1];
81 base::Time active_launch
= time_now
-
82 base::TimeDelta::FromDays(EphemeralAppService::kAppKeepThreshold
);
83 prefs
->SetLastLaunchTime(active_app_id
, active_launch
);
85 // Perform garbage collection.
86 content::WindowedNotificationObserver
uninstall_signal(
87 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED
,
88 content::Source
<Profile
>(browser()->profile()));
89 GarbageCollectEphemeralApps();
90 uninstall_signal
.Wait();
92 ExtensionService
* service
= ExtensionSystem::Get(browser()->profile())
93 ->extension_service();
95 EXPECT_FALSE(service
->GetInstalledExtension(inactive_app_id
));
96 EXPECT_TRUE(service
->GetInstalledExtension(active_app_id
));
98 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
101 // Verify that the count of ephemeral apps is maintained correctly.
102 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest
,
104 EphemeralAppService
* ephemeral_service
=
105 EphemeralAppService::Get(browser()->profile());
106 ASSERT_TRUE(ephemeral_service
);
107 InitEphemeralAppCount(ephemeral_service
);
109 // The count should not increase for regular installed apps.
110 EXPECT_TRUE(InstallPlatformApp("minimal"));
111 EXPECT_EQ(0, ephemeral_service
->ephemeral_app_count());
113 // The count should increase when an ephemeral app is added.
114 const Extension
* app
= InstallEphemeralApp(kMessagingReceiverApp
);
116 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
118 // The count should remain constant if the ephemeral app is updated.
119 const std::string app_id
= app
->id();
120 app
= UpdateEphemeralApp(
121 app_id
, GetTestPath(kMessagingReceiverAppV2
),
122 GetTestPath(kMessagingReceiverApp
).ReplaceExtension(
123 FILE_PATH_LITERAL(".pem")));
125 EXPECT_EQ(1, ephemeral_service
->ephemeral_app_count());
127 // The count should decrease when an ephemeral app is promoted to a regular
129 PromoteEphemeralApp(app
);
130 EXPECT_EQ(0, ephemeral_service
->ephemeral_app_count());