summaryrefslogtreecommitdiffstats
path: root/chrome/browser/apps/ephemeral_app_service_browsertest.cc
blob: 9ecf7a8ad18c4aa5be0251e0854a97b1a28df3ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <vector>

#include "chrome/browser/apps/ephemeral_app_browsertest.h"
#include "chrome/browser/apps/ephemeral_app_service.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/test/test_utils.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/manifest.h"

using extensions::Extension;
using extensions::ExtensionPrefs;
using extensions::ExtensionSystem;

namespace {

const int kNumTestApps = 2;
const char* kTestApps[] = {
  "app_window/generic",
  "minimal"
};

}  // namespace

class EphemeralAppServiceBrowserTest : public EphemeralAppTestBase {
 protected:
  void LoadApps() {
    for (int i = 0; i < kNumTestApps; ++i) {
      const Extension* extension = InstallEphemeralApp(kTestApps[i]);
      ASSERT_TRUE(extension);
      app_ids_.push_back(extension->id());
    }

    ASSERT_EQ(kNumTestApps, (int) app_ids_.size());
  }

  void GarbageCollectEphemeralApps() {
    EphemeralAppService* ephemeral_service = EphemeralAppService::Get(
        browser()->profile());
    ASSERT_TRUE(ephemeral_service);
    ephemeral_service->GarbageCollectApps();
  }

  void InitEphemeralAppCount(EphemeralAppService* ephemeral_service) {
    ephemeral_service->InitEphemeralAppCount();
  }

  std::vector<std::string> app_ids_;
};

// Verifies that inactive ephemeral apps are uninstalled and active apps are
// not removed. Extensive testing of the ephemeral app cache's replacement
// policies is done in the unit tests for EphemeralAppService. This is more
// like an integration test.
IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest,
                       GarbageCollectInactiveApps) {
  EphemeralAppService* ephemeral_service =
      EphemeralAppService::Get(browser()->profile());
  ASSERT_TRUE(ephemeral_service);
  InitEphemeralAppCount(ephemeral_service);

  LoadApps();

  const base::Time time_now = base::Time::Now();
  ExtensionPrefs* prefs = ExtensionPrefs::Get(browser()->profile());
  ASSERT_TRUE(prefs);

  // Set launch time for an inactive app.
  std::string inactive_app_id = app_ids_[0];
  base::Time inactive_launch = time_now -
      base::TimeDelta::FromDays(EphemeralAppService::kAppInactiveThreshold + 1);
  prefs->SetLastLaunchTime(inactive_app_id, inactive_launch);

  // Set launch time for an active app.
  std::string active_app_id = app_ids_[1];
  base::Time active_launch = time_now -
      base::TimeDelta::FromDays(EphemeralAppService::kAppKeepThreshold);
  prefs->SetLastLaunchTime(active_app_id, active_launch);

  // Perform garbage collection.
  content::WindowedNotificationObserver uninstall_signal(
      chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
      content::Source<Profile>(browser()->profile()));
  GarbageCollectEphemeralApps();
  uninstall_signal.Wait();

  ExtensionService* service = ExtensionSystem::Get(browser()->profile())
      ->extension_service();
  ASSERT_TRUE(service);
  EXPECT_FALSE(service->GetInstalledExtension(inactive_app_id));
  EXPECT_TRUE(service->GetInstalledExtension(active_app_id));

  EXPECT_EQ(1, ephemeral_service->ephemeral_app_count());
}

// Verify that the count of ephemeral apps is maintained correctly.
IN_PROC_BROWSER_TEST_F(EphemeralAppServiceBrowserTest,
                       EphemeralAppCount) {
  EphemeralAppService* ephemeral_service =
      EphemeralAppService::Get(browser()->profile());
  ASSERT_TRUE(ephemeral_service);
  InitEphemeralAppCount(ephemeral_service);

  // The count should not increase for regular installed apps.
  EXPECT_TRUE(InstallPlatformApp("minimal"));
  EXPECT_EQ(0, ephemeral_service->ephemeral_app_count());

  // The count should increase when an ephemeral app is added.
  const Extension* app = InstallEphemeralApp(kMessagingReceiverApp);
  ASSERT_TRUE(app);
  EXPECT_EQ(1, ephemeral_service->ephemeral_app_count());

  // The count should remain constant if the ephemeral app is updated.
  const std::string app_id = app->id();
  app = UpdateEphemeralApp(
      app_id, GetTestPath(kMessagingReceiverAppV2),
      GetTestPath(kMessagingReceiverApp).ReplaceExtension(
          FILE_PATH_LITERAL(".pem")));
  ASSERT_TRUE(app);
  EXPECT_EQ(1, ephemeral_service->ephemeral_app_count());

  // The count should decrease when an ephemeral app is promoted to a regular
  // installed app.
  PromoteEphemeralApp(app);
  EXPECT_EQ(0, ephemeral_service->ephemeral_app_count());
}