ephemeral_app_service_browsertest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/app_browsertest_util.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
16using extensions::PlatformAppBrowserTest;
17using extensions::Extension;
18using extensions::ExtensionPrefs;
19using extensions::ExtensionSystem;
20
21namespace {
22
23const int kNumTestApps = 2;
24const char* kTestApps[] = {
25  "platform_apps/shell_window/generic",
26  "platform_apps/minimal"
27};
28
29}  // namespace
30
31class EphemeralAppServiceBrowserTest : public PlatformAppBrowserTest {
32 protected:
33  void LoadApps() {
34    for (int i = 0; i < kNumTestApps; ++i) {
35      base::FilePath path = test_data_dir_.AppendASCII(kTestApps[i]);
36      const Extension* extension =
37          InstallExtensionWithSourceAndFlags(
38              path,
39              1,
40              extensions::Manifest::UNPACKED,
41              Extension::IS_EPHEMERAL);
42      app_ids_.push_back(extension->id());
43    }
44
45    ASSERT_EQ(kNumTestApps, (int) app_ids_.size());
46  }
47
48  void GarbageCollectEphemeralApps() {
49    EphemeralAppService* ephemeral_service = EphemeralAppService::Get(
50        browser()->profile());
51    ASSERT_TRUE(ephemeral_service);
52    ephemeral_service->GarbageCollectApps();
53  }
54
55  std::vector<std::string> app_ids_;
56};
57
58// Verifies that inactive ephemeral apps are uninstalled and active apps are
59// not removed. Extensive testing of the ephemeral app cache's replacement
60// policies is done in the unit tests for EphemeralAppService. This is more
61// like an integration test.
62IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest,
63                       GarbageCollectInactiveApps) {
64  LoadApps();
65
66  const base::Time time_now = base::Time::Now();
67  ExtensionPrefs* prefs = ExtensionPrefs::Get(browser()->profile());
68  ASSERT_TRUE(prefs);
69
70  // Set launch time for an inactive app.
71  std::string inactive_app_id = app_ids_[0];
72  base::Time inactive_launch = time_now -
73      base::TimeDelta::FromDays(EphemeralAppService::kAppInactiveThreshold + 1);
74  prefs->SetLastLaunchTime(inactive_app_id, inactive_launch);
75
76  // Set launch time for an active app.
77  std::string active_app_id = app_ids_[1];
78  base::Time active_launch = time_now -
79      base::TimeDelta::FromDays(EphemeralAppService::kAppKeepThreshold);
80  prefs->SetLastLaunchTime(active_app_id, active_launch);
81
82  // Perform garbage collection.
83  content::WindowedNotificationObserver uninstall_signal(
84      chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
85      content::Source<Profile>(browser()->profile()));
86  GarbageCollectEphemeralApps();
87  uninstall_signal.Wait();
88
89  ExtensionService* service = ExtensionSystem::Get(browser()->profile())
90      ->extension_service();
91  EXPECT_TRUE(service);
92  EXPECT_FALSE(service->GetInstalledExtension(inactive_app_id));
93  EXPECT_TRUE(service->GetInstalledExtension(active_app_id));
94}
95