Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / ephemeral_app_service_browsertest.cc
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.
4
5 #include <vector>
6
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"
15
16 using extensions::Extension;
17 using extensions::ExtensionPrefs;
18 using extensions::ExtensionSystem;
19
20 namespace {
21
22 const int kNumTestApps = 2;
23 const char* kTestApps[] = {
24   "app_window/generic",
25   "minimal"
26 };
27
28 }  // namespace
29
30 class EphemeralAppServiceBrowserTest : public EphemeralAppTestBase {
31  protected:
32   void LoadApps() {
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());
37     }
38
39     ASSERT_EQ(kNumTestApps, (int) app_ids_.size());
40   }
41
42   void GarbageCollectEphemeralApps() {
43     EphemeralAppService* ephemeral_service = EphemeralAppService::Get(
44         browser()->profile());
45     ASSERT_TRUE(ephemeral_service);
46     ephemeral_service->GarbageCollectApps();
47   }
48
49   void InitEphemeralAppCount(EphemeralAppService* ephemeral_service) {
50     ephemeral_service->InitEphemeralAppCount();
51   }
52
53   std::vector<std::string> app_ids_;
54 };
55
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);
66
67   LoadApps();
68
69   const base::Time time_now = base::Time::Now();
70   ExtensionPrefs* prefs = ExtensionPrefs::Get(browser()->profile());
71   ASSERT_TRUE(prefs);
72
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);
78
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);
84
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();
91
92   ExtensionService* service = ExtensionSystem::Get(browser()->profile())
93       ->extension_service();
94   ASSERT_TRUE(service);
95   EXPECT_FALSE(service->GetInstalledExtension(inactive_app_id));
96   EXPECT_TRUE(service->GetInstalledExtension(active_app_id));
97
98   EXPECT_EQ(1, ephemeral_service->ephemeral_app_count());
99 }
100
101 // Verify that the count of ephemeral apps is maintained correctly.
102 IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest,
103                        EphemeralAppCount) {
104   EphemeralAppService* ephemeral_service =
105       EphemeralAppService::Get(browser()->profile());
106   ASSERT_TRUE(ephemeral_service);
107   InitEphemeralAppCount(ephemeral_service);
108
109   // The count should not increase for regular installed apps.
110   EXPECT_TRUE(InstallPlatformApp("minimal"));
111   EXPECT_EQ(0, ephemeral_service->ephemeral_app_count());
112
113   // The count should increase when an ephemeral app is added.
114   const Extension* app = InstallEphemeralApp(kMessagingReceiverApp);
115   ASSERT_TRUE(app);
116   EXPECT_EQ(1, ephemeral_service->ephemeral_app_count());
117
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")));
124   ASSERT_TRUE(app);
125   EXPECT_EQ(1, ephemeral_service->ephemeral_app_count());
126
127   // The count should decrease when an ephemeral app is promoted to a regular
128   // installed app.
129   PromoteEphemeralApp(app);
130   EXPECT_EQ(0, ephemeral_service->ephemeral_app_count());
131 }