summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/session_length_limiter_unittest.cc
blob: 39495a456d7bdb8a972c706d1696cda5b860aa78 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2012 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 "chrome/browser/chromeos/session_length_limiter.h"

#include <queue>
#include <utility>
#include <vector>

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/testing_pref_service.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_number_conversions.h"
#include "base/thread_task_runner_handle.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::Invoke;
using ::testing::Mock;
using ::testing::NiceMock;

namespace chromeos {

namespace {

class MockSessionLengthLimiterDelegate : public SessionLengthLimiter::Delegate {
 public:
  MOCK_CONST_METHOD0(GetCurrentTime, const base::TimeTicks(void));
  MOCK_METHOD0(StopSession, void(void));
};

// A SingleThreadTaskRunner that mocks the current time and allows it to be
// fast-forwarded.
class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner {
 public:
  MockTimeSingleThreadTaskRunner();

  // base::SingleThreadTaskRunner:
  virtual bool RunsTasksOnCurrentThread() const OVERRIDE;
  virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
                               const base::Closure& task,
                               base::TimeDelta delay) OVERRIDE;
  virtual bool PostNonNestableDelayedTask(
      const tracked_objects::Location& from_here,
      const base::Closure& task,
      base::TimeDelta delay) OVERRIDE;

  const base::TimeTicks& GetCurrentTime() const;

  void FastForwardBy(int64 milliseconds);
  void FastForwardUntilNoTasksRemain();

 private:
  // Strict weak temporal ordering of tasks.
  class TemporalOrder {
   public:
    bool operator()(
        const std::pair<base::TimeTicks, base::Closure>& first_task,
        const std::pair<base::TimeTicks, base::Closure>& second_task) const;
  };

  virtual ~MockTimeSingleThreadTaskRunner();

  base::TimeTicks now_;
  std::priority_queue<std::pair<base::TimeTicks, base::Closure>,
                      std::vector<std::pair<base::TimeTicks, base::Closure> >,
                      TemporalOrder> tasks_;
};

}  // namespace

class SessionLengthLimiterTest : public testing::Test {
 protected:
  SessionLengthLimiterTest();

  // testing::Test:
  virtual void SetUp() OVERRIDE;
  virtual void TearDown() OVERRIDE;

  void SetSessionStartTimePref(int64 session_start_time);
  void VerifySessionStartTimePref();
  void SetSessionLengthLimitPref(int64 session_length_limit);

  void ExpectStopSession();
  void CheckStopSessionTime();

  void CreateSessionLengthLimiter(bool browser_restarted);

  TestingPrefServiceSimple local_state_;
  scoped_refptr<MockTimeSingleThreadTaskRunner> runner_;
  base::TimeTicks session_start_time_;
  base::TimeTicks session_end_time_;

  MockSessionLengthLimiterDelegate* delegate_;  // Owned by
                                                // session_length_limiter_.
  scoped_ptr<SessionLengthLimiter> session_length_limiter_;
};

MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() {
}

bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const {
  return true;
}

bool MockTimeSingleThreadTaskRunner::PostDelayedTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task,
    base::TimeDelta delay) {
  tasks_.push(std::pair<base::TimeTicks, base::Closure>(now_ + delay, task));
  return true;
}

bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask(
    const tracked_objects::Location& from_here,
    const base::Closure& task,
    base::TimeDelta delay) {
  NOTREACHED();
  return false;
}

const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const {
  return now_;
}

void MockTimeSingleThreadTaskRunner::FastForwardBy(int64 delta) {
  const base::TimeTicks latest =
      now_ + base::TimeDelta::FromMilliseconds(delta);
  while (!tasks_.empty() && tasks_.top().first <= latest) {
    now_ = tasks_.top().first;
    base::Closure task = tasks_.top().second;
    tasks_.pop();
    task.Run();
  }
  now_ = latest;
}

void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() {
  while (!tasks_.empty()) {
    now_ = tasks_.top().first;
    base::Closure task = tasks_.top().second;
    tasks_.pop();
    task.Run();
  }
}

bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()(
    const std::pair<base::TimeTicks, base::Closure>& first_task,
    const std::pair<base::TimeTicks, base::Closure>& second_task) const {
  return first_task.first >= second_task.first;
}

MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() {
}

SessionLengthLimiterTest::SessionLengthLimiterTest() : delegate_(NULL) {
}

void SessionLengthLimiterTest::SetUp() {
  TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
  SessionLengthLimiter::RegisterPrefs(local_state_.registry());
  runner_ = new MockTimeSingleThreadTaskRunner;
  session_start_time_ = runner_->GetCurrentTime();

  delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>;
  ON_CALL(*delegate_, GetCurrentTime())
      .WillByDefault(Invoke(runner_.get(),
                            &MockTimeSingleThreadTaskRunner::GetCurrentTime));
  EXPECT_CALL(*delegate_, StopSession()).Times(0);
}

void SessionLengthLimiterTest::TearDown() {
  TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
}

void SessionLengthLimiterTest::SetSessionStartTimePref(
    int64 session_start_time) {
  local_state_.SetUserPref(prefs::kSessionStartTime,
                           base::Value::CreateStringValue(
                               base::Int64ToString(session_start_time)));
}

void SessionLengthLimiterTest::VerifySessionStartTimePref() {
  base::TimeTicks session_start_time(base::TimeTicks::FromInternalValue(
      local_state_.GetInt64(prefs::kSessionStartTime)));
  EXPECT_EQ(session_start_time_, session_start_time);
}

void SessionLengthLimiterTest::SetSessionLengthLimitPref(
    int64 session_length_limit) {
  session_end_time_ = session_start_time_ +
      base::TimeDelta::FromMilliseconds(session_length_limit);
  // If the new session end time has passed already, the session should end now.
  if (session_end_time_ < runner_->GetCurrentTime())
    session_end_time_ = runner_->GetCurrentTime();
  local_state_.SetUserPref(prefs::kSessionLengthLimit,
                           base::Value::CreateIntegerValue(
                               session_length_limit));
}

void SessionLengthLimiterTest::ExpectStopSession() {
  Mock::VerifyAndClearExpectations(delegate_);
  EXPECT_CALL(*delegate_, StopSession())
    .Times(1)
    .WillOnce(Invoke(this, &SessionLengthLimiterTest::CheckStopSessionTime));
}

void SessionLengthLimiterTest::CheckStopSessionTime() {
  EXPECT_EQ(session_end_time_, runner_->GetCurrentTime());
}

void SessionLengthLimiterTest::CreateSessionLengthLimiter(
    bool browser_restarted) {
  session_length_limiter_.reset(
      new SessionLengthLimiter(delegate_, browser_restarted));
}
// Verifies that the session start time in local state is updated during login
// if no session start time has been stored before.
TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeUnset) {
  CreateSessionLengthLimiter(false);
  VerifySessionStartTimePref();
}

// Verifies that the session start time in local state is updated during login
// if a session start time lying in the future has been stored before.
TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeFuture) {
  SetSessionStartTimePref(
      (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue());
  CreateSessionLengthLimiter(false);
  VerifySessionStartTimePref();
}

// Verifies that the session start time in local state is updated during login
// if a valid session start time has been stored before.
TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeValid) {
  SetSessionStartTimePref(
      (session_start_time_ - base::TimeDelta::FromHours(2)).ToInternalValue());
  CreateSessionLengthLimiter(false);
  VerifySessionStartTimePref();
}

// Verifies that the session start time in local state is updated during restart
// after a crash if no session start time has been stored before.
TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeUnset) {
  CreateSessionLengthLimiter(true);
  VerifySessionStartTimePref();
}

// Verifies that the session start time in local state is updated during restart
// after a crash if a session start time lying in the future has been stored
// before.
TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeFuture) {
  SetSessionStartTimePref(
      (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue());
  CreateSessionLengthLimiter(true);
  VerifySessionStartTimePref();
}

// Verifies that the session start time in local state is *not* updated during
// restart after a crash if a valid session start time has been stored before.
TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeValid) {
  session_start_time_ -= base::TimeDelta::FromHours(2);
  SetSessionStartTimePref(session_start_time_.ToInternalValue());
  CreateSessionLengthLimiter(true);
  VerifySessionStartTimePref();
}

// Creates a SessionLengthLimiter without setting a limit. Verifies that the
// limiter does not start a timer.
TEST_F(SessionLengthLimiterTest, RunWithoutSessionLengthLimit) {
  base::ThreadTaskRunnerHandle runner_handler(runner_);

  // Create a SessionLengthLimiter.
  CreateSessionLengthLimiter(false);

  // Verify that no timer fires to terminate the session.
  runner_->FastForwardUntilNoTasksRemain();
}

// Creates a SessionLengthLimiter after setting a limit. Verifies that the
// limiter starts a timer and that when the session length reaches the limit,
// the session is terminated.
TEST_F(SessionLengthLimiterTest, RunWithSessionLengthLimit) {
  base::ThreadTaskRunnerHandle runner_handler(runner_);

  // Set a 60 second session time limit.
  SetSessionLengthLimitPref(60 * 1000);  // 60 seconds.

  // Create a SessionLengthLimiter.
  CreateSessionLengthLimiter(false);

  // Verify that the timer fires and the session is terminated when the session
  // length limit is reached.
  ExpectStopSession();
  runner_->FastForwardUntilNoTasksRemain();
}

// Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
// seconds of session time to pass, then increases the limit to 90 seconds.
// Verifies that when the session time reaches the new 90 second limit, the
// session is terminated.
TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) {
  base::ThreadTaskRunnerHandle runner_handler(runner_);

  // Set a 60 second session time limit.
  SetSessionLengthLimitPref(60 * 1000);  // 60 seconds.

  // Create a SessionLengthLimiter.
  CreateSessionLengthLimiter(false);

  // Fast forward the time by 50 seconds, verifying that no timer fires to
  // terminate the session.
  runner_->FastForwardBy(50 * 1000);  // 50 seconds.

  // Increase the session length limit to 90 seconds.
  SetSessionLengthLimitPref(90 * 1000);  // 90 seconds.

  // Verify that the the timer fires and the session is terminated when the
  // session length limit is reached.
  ExpectStopSession();
  runner_->FastForwardUntilNoTasksRemain();
}

// Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
// seconds of session time to pass, then decreases the limit to 40 seconds.
// Verifies that when the limit is decreased to 40 seconds after 50 seconds of
// session time have passed, the next timer tick causes the session to be
// terminated.
TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) {
  base::ThreadTaskRunnerHandle runner_handler(runner_);

  // Set a 60 second session time limit.
  SetSessionLengthLimitPref(60 * 1000);  // 60 seconds.

  // Create a SessionLengthLimiter.
  CreateSessionLengthLimiter(false);

  // Fast forward the time by 50 seconds, verifying that no timer fires to
  // terminate the session.
  runner_->FastForwardBy(50 * 1000);  // 50 seconds.

  // Verify that reducing the session length limit below the 50 seconds that
  // have already elapsed causes the session to be terminated immediately.
  ExpectStopSession();
  SetSessionLengthLimitPref(40 * 1000);  // 40 seconds.
}

// Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
// seconds of session time to pass, then removes the limit. Verifies that after
// the limit is removed, the session is not terminated when the session time
// reaches the original 60 second limit.
TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) {
  base::ThreadTaskRunnerHandle runner_handler(runner_);

  // Set a 60 second session time limit.
  SetSessionLengthLimitPref(60 * 1000);  // 60 seconds.

  // Create a SessionLengthLimiter.
  CreateSessionLengthLimiter(false);

  // Fast forward the time by 50 seconds, verifying that no timer fires to
  // terminate the session.
  runner_->FastForwardBy(50 * 1000);  // 50 seconds.

  // Remove the session length limit.
  local_state_.RemoveUserPref(prefs::kSessionLengthLimit);

  // Verify that no timer fires to terminate the session.
  runner_->FastForwardUntilNoTasksRemain();
}

}  // namespace chromeos