summaryrefslogtreecommitdiffstats
path: root/chrome/browser/apps/ephemeral_app_service_unittest.cc
blob: 6b40cfef5031458649120d3b13b14951e0da1e47 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// 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 <algorithm>

#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/apps/ephemeral_app_service.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

// Generate a time N number of days before the reference time.
// The generated time can be randomized.
base::Time GetPastTime(const base::Time& reference_time,
                       int days_before,
                       bool randomize_time = false) {
  base::Time generated_time =
      reference_time - base::TimeDelta::FromDays(days_before);

  // Add an hour so that the time is well within the number of days before.
  generated_time += base::TimeDelta::FromHours(1);

  // Add a random number of seconds between 0 - 10 hours.
  if (randomize_time)
    generated_time += base::TimeDelta::FromSeconds(base::RandInt(0, 36000));

  return generated_time;
}

}  // namespace

class EphemeralAppServiceTest : public testing::Test {
 protected:
  typedef EphemeralAppService::LaunchTimeAppMap LaunchTimeAppMap;

  EphemeralAppServiceTest() {}
  virtual ~EphemeralAppServiceTest() {}

  void RunTest(int ephemeral_app_count,
               const LaunchTimeAppMap& launch_times,
               const std::set<std::string>& expected_removed_ids) {
    std::set<std::string> remove_app_ids;
    EphemeralAppService::GetAppsToRemove(ephemeral_app_count,
                                         launch_times,
                                         &remove_app_ids);
    EXPECT_EQ(expected_removed_ids, remove_app_ids);
  }

  void RunTestCheckLRU(int ephemeral_app_count,
                       LaunchTimeAppMap& launch_times,
                       int expected_removed_count) {
    std::set<std::string> remove_app_ids;
    EphemeralAppService::GetAppsToRemove(ephemeral_app_count,
                                         launch_times,
                                         &remove_app_ids);
    EXPECT_EQ(expected_removed_count, (int) remove_app_ids.size());

    // Move the launch times of removed apps to another map.
    LaunchTimeAppMap removed_apps;
    for (LaunchTimeAppMap::iterator it = launch_times.begin();
         it != launch_times.end(); ) {
      if (remove_app_ids.find(it->second) != remove_app_ids.end()) {
        removed_apps.insert(*it);
        launch_times.erase(it++);
      } else {
        ++it;
      }
    }

    if (launch_times.empty())
      return;

    // Verify that the removed apps have launch times earlier than or equal to
    // all retained apps. We can actually just compare with the first entry in
    // |launch_times| but will make no implementation assumptions.
    for (LaunchTimeAppMap::const_iterator removed = removed_apps.begin();
         removed != removed_apps.end(); ++removed) {
      for (LaunchTimeAppMap::iterator retained = launch_times.begin();
         retained != launch_times.end(); ++retained) {
        EXPECT_LE(removed->first, retained->first);
      }
    }
  }

  // Generate X app launch times, N days before the reference time.
  // If |generated_ids| is not NULL, the generated app IDs will be added
  // to the set.
  void GenerateLaunchTimes(const base::Time& reference_time,
                           int days_before,
                           int count,
                           LaunchTimeAppMap* launch_times,
                           std::set<std::string>* generated_ids = NULL) {
    for (int i = 0; i < count; ++i) {
      std::string app_id = base::IntToString(launch_times->size());
      launch_times->insert(std::make_pair(
          GetPastTime(reference_time, days_before, true),
          app_id));

      if (generated_ids)
        generated_ids->insert(app_id);
    }
  }

  // Add inactive apps that should always be removed by garbage collection.
  void AddInactiveApps(const base::Time& reference_time,
                       int count,
                       LaunchTimeAppMap* launch_times,
                       std::set<std::string>* generated_ids = NULL) {
    GenerateLaunchTimes(reference_time,
                        EphemeralAppService::kAppInactiveThreshold + 1,
                        count,
                        launch_times,
                        generated_ids);
  }

  // Add recently launched apps that should NOT be removed by garbage
  // collection regardless of the number of cached ephemeral apps.
  void AddRecentlyLaunchedApps(const base::Time& reference_time,
                               int count,
                               LaunchTimeAppMap* launch_times,
                               std::set<std::string>* generated_ids = NULL) {
    GenerateLaunchTimes(reference_time,
                        EphemeralAppService::kAppKeepThreshold,
                        count,
                        launch_times,
                        generated_ids);
  }

  // Add apps launched between the kAppInactiveThreshold and kAppKeepThreshold,
  // which may or may not be removed by garbage collection depending on the
  // number of ephemeral apps in the cache.
  void AddIntermediateApps(const base::Time& reference_time,
                           int count,
                           LaunchTimeAppMap* launch_times,
                           std::set<std::string>* generated_ids = NULL) {
    int days_before = base::RandInt(EphemeralAppService::kAppKeepThreshold + 1,
                                    EphemeralAppService::kAppInactiveThreshold);
    GenerateLaunchTimes(reference_time,
                        days_before,
                        count,
                        launch_times,
                        generated_ids);
  }
};

// Verify that inactive apps are removed even if the cache has not reached
// capacity.
// Test case: | inactive |
// Expected output: All inactive apps removed.
TEST_F(EphemeralAppServiceTest, RemoveInactiveApps) {
  base::Time time_now = base::Time::Now();
  LaunchTimeAppMap launch_times;
  std::set<std::string> expected_removed_ids;

  AddInactiveApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount / 5,
      &launch_times,
      &expected_removed_ids);
  RunTest(launch_times.size(), launch_times, expected_removed_ids);
}

// Verify that inactive apps are removed even if the cache has not reached
// capacity.
// Test case: | inactive | intermediate | recently launched |
// Expected output: All inactive apps removed, other apps retained.
TEST_F(EphemeralAppServiceTest, RemoveInactiveAppsKeepOthers) {
  base::Time time_now = base::Time::Now();
  LaunchTimeAppMap launch_times;
  std::set<std::string> expected_removed_ids;

  AddInactiveApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount / 5,
      &launch_times,
      &expected_removed_ids);
  AddIntermediateApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount / 5,
      &launch_times);
  AddRecentlyLaunchedApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount / 5,
      &launch_times);
  RunTest(launch_times.size(), launch_times, expected_removed_ids);
}

// Verify that recently launched apps will not be removed, even when the cache
// overflows.
// Test case: | recently launched |
// Expected output: All recently launched apps retained.
TEST_F(EphemeralAppServiceTest, KeepRecentLaunch) {
  base::Time time_now = base::Time::Now();
  LaunchTimeAppMap launch_times;

  AddRecentlyLaunchedApps(
      time_now,
      3,
      &launch_times);
  RunTest(launch_times.size(), launch_times, std::set<std::string>());

  AddRecentlyLaunchedApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount,
      &launch_times); // overflow
  RunTest(launch_times.size(), launch_times, std::set<std::string>());
}

// Verify that recently launched apps will not be removed, even when the cache
// overflows.
// Test case: | intermediate (overflow) | recently launched (overflow) |
// Expected output: All recently launched apps retained, intermediate apps
// removed.
TEST_F(EphemeralAppServiceTest, KeepRecentLaunchRemoveOthers) {
  base::Time time_now = base::Time::Now();
  LaunchTimeAppMap launch_times;
  std::set<std::string> expected_removed_ids;

  AddRecentlyLaunchedApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount + 3,
      &launch_times); // overflow
  AddIntermediateApps(
      time_now,
      3,
      &launch_times,
      &expected_removed_ids); // overflow
  RunTest(launch_times.size(), launch_times, expected_removed_ids);
}

// Verify that the LRU algorithm is implemented correctly.
// Test case: | intermediate (overflow) |
// Expected output: The least recently launched apps are removed.
TEST_F(EphemeralAppServiceTest, RemoveOverflow) {
  base::Time time_now = base::Time::Now();
  LaunchTimeAppMap launch_times;

  const int kOverflow = 3;
  AddIntermediateApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount + kOverflow,
      &launch_times); // overflow
  RunTestCheckLRU(launch_times.size(), launch_times, kOverflow);
}

// Verify that GetAppsToRemove() takes into account the number of running apps,
// since they are not included in the launch times.
// Test case: | intermediate (overflow) | running apps |
// Expected output: The least recently launched apps are removed.
TEST_F(EphemeralAppServiceTest, RemoveOverflowWithRunningApps) {
  base::Time time_now = base::Time::Now();
  LaunchTimeAppMap launch_times;

  const int kRunningApps = 3;
  AddIntermediateApps(
      time_now,
      EphemeralAppService::kMaxEphemeralAppsCount,
      &launch_times);  // overflow
  RunTestCheckLRU(
      launch_times.size() + kRunningApps,
      launch_times,
      kRunningApps);
}