Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / chromeos / session_length_limiter_unittest.cc
blob60252e3bc63e0d9a5aab16ee23cf818944b186e1
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/chromeos/session_length_limiter.h"
7 #include <queue>
8 #include <utility>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/location.h"
14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/prefs/testing_pref_service.h"
18 #include "base/single_thread_task_runner.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/thread_task_runner_handle.h"
21 #include "base/values.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using ::testing::Invoke;
28 using ::testing::Mock;
29 using ::testing::NiceMock;
31 namespace chromeos {
33 namespace {
35 class MockSessionLengthLimiterDelegate : public SessionLengthLimiter::Delegate {
36 public:
37 MOCK_CONST_METHOD0(GetCurrentTime, const base::TimeTicks(void));
38 MOCK_METHOD0(StopSession, void(void));
41 // A SingleThreadTaskRunner that mocks the current time and allows it to be
42 // fast-forwarded.
43 class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner {
44 public:
45 MockTimeSingleThreadTaskRunner();
47 // base::SingleThreadTaskRunner:
48 virtual bool RunsTasksOnCurrentThread() const override;
49 virtual bool PostDelayedTask(const tracked_objects::Location& from_here,
50 const base::Closure& task,
51 base::TimeDelta delay) override;
52 virtual bool PostNonNestableDelayedTask(
53 const tracked_objects::Location& from_here,
54 const base::Closure& task,
55 base::TimeDelta delay) override;
57 const base::TimeTicks& GetCurrentTime() const;
59 void FastForwardBy(const base::TimeDelta& time_delta);
60 void FastForwardUntilNoTasksRemain();
62 private:
63 // Strict weak temporal ordering of tasks.
64 class TemporalOrder {
65 public:
66 bool operator()(
67 const std::pair<base::TimeTicks, base::Closure>& first_task,
68 const std::pair<base::TimeTicks, base::Closure>& second_task) const;
71 virtual ~MockTimeSingleThreadTaskRunner();
73 base::TimeTicks now_;
74 std::priority_queue<std::pair<base::TimeTicks, base::Closure>,
75 std::vector<std::pair<base::TimeTicks, base::Closure> >,
76 TemporalOrder> tasks_;
79 } // namespace
81 class SessionLengthLimiterTest : public testing::Test {
82 protected:
83 SessionLengthLimiterTest();
85 // testing::Test:
86 virtual void SetUp() override;
87 virtual void TearDown() override;
89 void SetSessionUserActivitySeenPref(bool user_activity_seen);
90 void ClearSessionUserActivitySeenPref();
91 bool IsSessionUserActivitySeenPrefSet();
92 bool GetSessionUserActivitySeenPref();
94 void SetSessionStartTimePref(const base::TimeTicks& session_start_time);
95 void ClearSessionStartTimePref();
96 bool IsSessionStartTimePrefSet();
97 base::TimeTicks GetSessionStartTimePref();
99 void SetSessionLengthLimitPref(const base::TimeDelta& session_length_limit);
100 void ClearSessionLengthLimitPref();
102 void SetWaitForInitialUserActivityPref(bool wait_for_initial_user_activity);
104 void SimulateUserActivity();
106 void UpdateSessionStartTimeIfWaitingForUserActivity();
108 void ExpectStopSession();
109 void SaveSessionStopTime();
111 // Clears the session state by resetting |user_activity_| and
112 // |session_start_time_| and creates a new SessionLengthLimiter.
113 void CreateSessionLengthLimiter(bool browser_restarted);
115 void DestroySessionLengthLimiter();
117 scoped_refptr<MockTimeSingleThreadTaskRunner> runner_;
118 base::TimeTicks session_start_time_;
119 base::TimeTicks session_stop_time_;
121 private:
122 TestingPrefServiceSimple local_state_;
123 bool user_activity_seen_;
125 MockSessionLengthLimiterDelegate* delegate_; // Owned by
126 // session_length_limiter_.
127 scoped_ptr<SessionLengthLimiter> session_length_limiter_;
130 MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner()
131 : now_(base::TimeTicks::FromInternalValue(1000)) {
134 bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const {
135 return true;
138 bool MockTimeSingleThreadTaskRunner::PostDelayedTask(
139 const tracked_objects::Location& from_here,
140 const base::Closure& task,
141 base::TimeDelta delay) {
142 tasks_.push(std::pair<base::TimeTicks, base::Closure>(now_ + delay, task));
143 return true;
146 bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask(
147 const tracked_objects::Location& from_here,
148 const base::Closure& task,
149 base::TimeDelta delay) {
150 NOTREACHED();
151 return false;
154 const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const {
155 return now_;
158 void MockTimeSingleThreadTaskRunner::FastForwardBy(
159 const base::TimeDelta& time_delta) {
160 const base::TimeTicks latest = now_ + time_delta;
161 while (!tasks_.empty() && tasks_.top().first <= latest) {
162 now_ = tasks_.top().first;
163 base::Closure task = tasks_.top().second;
164 tasks_.pop();
165 task.Run();
167 now_ = latest;
170 void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() {
171 while (!tasks_.empty()) {
172 now_ = tasks_.top().first;
173 base::Closure task = tasks_.top().second;
174 tasks_.pop();
175 task.Run();
179 bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()(
180 const std::pair<base::TimeTicks, base::Closure>& first_task,
181 const std::pair<base::TimeTicks, base::Closure>& second_task) const {
182 return first_task.first > second_task.first;
185 MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() {
188 SessionLengthLimiterTest::SessionLengthLimiterTest()
189 : user_activity_seen_(false),
190 delegate_(NULL) {
193 void SessionLengthLimiterTest::SetUp() {
194 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
195 SessionLengthLimiter::RegisterPrefs(local_state_.registry());
196 runner_ = new MockTimeSingleThreadTaskRunner;
199 void SessionLengthLimiterTest::TearDown() {
200 session_length_limiter_.reset();
201 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
204 void SessionLengthLimiterTest::SetSessionUserActivitySeenPref(
205 bool user_activity_seen) {
206 local_state_.SetUserPref(prefs::kSessionUserActivitySeen,
207 new base::FundamentalValue(user_activity_seen));
210 void SessionLengthLimiterTest::ClearSessionUserActivitySeenPref() {
211 local_state_.ClearPref(prefs::kSessionUserActivitySeen);
214 bool SessionLengthLimiterTest::IsSessionUserActivitySeenPrefSet() {
215 return local_state_.HasPrefPath(prefs::kSessionUserActivitySeen);
218 bool SessionLengthLimiterTest::GetSessionUserActivitySeenPref() {
219 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
220 return local_state_.GetBoolean(prefs::kSessionUserActivitySeen);
223 void SessionLengthLimiterTest::SetSessionStartTimePref(
224 const base::TimeTicks& session_start_time) {
225 local_state_.SetUserPref(
226 prefs::kSessionStartTime,
227 new base::StringValue(
228 base::Int64ToString(session_start_time.ToInternalValue())));
231 void SessionLengthLimiterTest::ClearSessionStartTimePref() {
232 local_state_.ClearPref(prefs::kSessionStartTime);
235 bool SessionLengthLimiterTest::IsSessionStartTimePrefSet() {
236 return local_state_.HasPrefPath(prefs::kSessionStartTime);
239 base::TimeTicks SessionLengthLimiterTest::GetSessionStartTimePref() {
240 EXPECT_TRUE(IsSessionStartTimePrefSet());
241 return base::TimeTicks::FromInternalValue(
242 local_state_.GetInt64(prefs::kSessionStartTime));
245 void SessionLengthLimiterTest::SetSessionLengthLimitPref(
246 const base::TimeDelta& session_length_limit) {
247 local_state_.SetUserPref(prefs::kSessionLengthLimit,
248 new base::FundamentalValue(
249 static_cast<int>(session_length_limit.InMilliseconds())));
250 UpdateSessionStartTimeIfWaitingForUserActivity();
253 void SessionLengthLimiterTest::ClearSessionLengthLimitPref() {
254 local_state_.RemoveUserPref(prefs::kSessionLengthLimit);
255 UpdateSessionStartTimeIfWaitingForUserActivity();
258 void SessionLengthLimiterTest::SetWaitForInitialUserActivityPref(
259 bool wait_for_initial_user_activity) {
260 UpdateSessionStartTimeIfWaitingForUserActivity();
261 local_state_.SetUserPref(
262 prefs::kSessionWaitForInitialUserActivity,
263 new base::FundamentalValue(wait_for_initial_user_activity));
266 void SessionLengthLimiterTest::SimulateUserActivity() {
267 if (session_length_limiter_)
268 session_length_limiter_->OnUserActivity(NULL);
269 UpdateSessionStartTimeIfWaitingForUserActivity();
270 user_activity_seen_ = true;
273 void SessionLengthLimiterTest::
274 UpdateSessionStartTimeIfWaitingForUserActivity() {
275 if (!user_activity_seen_ &&
276 local_state_.GetBoolean(prefs::kSessionWaitForInitialUserActivity)) {
277 session_start_time_ = runner_->GetCurrentTime();
281 void SessionLengthLimiterTest::ExpectStopSession() {
282 Mock::VerifyAndClearExpectations(delegate_);
283 EXPECT_CALL(*delegate_, StopSession())
284 .Times(1)
285 .WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime));
288 void SessionLengthLimiterTest::SaveSessionStopTime() {
289 session_stop_time_ = runner_->GetCurrentTime();
292 void SessionLengthLimiterTest::CreateSessionLengthLimiter(
293 bool browser_restarted) {
294 user_activity_seen_ = false;
295 session_start_time_ = runner_->GetCurrentTime();
297 EXPECT_FALSE(delegate_);
298 delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>;
299 ON_CALL(*delegate_, GetCurrentTime())
300 .WillByDefault(Invoke(runner_.get(),
301 &MockTimeSingleThreadTaskRunner::GetCurrentTime));
302 EXPECT_CALL(*delegate_, StopSession()).Times(0);
303 session_length_limiter_.reset(
304 new SessionLengthLimiter(delegate_, browser_restarted));
307 void SessionLengthLimiterTest::DestroySessionLengthLimiter() {
308 session_length_limiter_.reset();
309 delegate_ = NULL;
312 // Verifies that when not instructed to wait for initial user activity, the
313 // session start time is set and the pref indicating user activity is cleared
314 // in local state during login.
315 TEST_F(SessionLengthLimiterTest, StartDoNotWaitForInitialUserActivity) {
316 // Pref indicating user activity not set. Session start time not set.
317 ClearSessionUserActivitySeenPref();
318 ClearSessionStartTimePref();
319 CreateSessionLengthLimiter(false);
320 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
321 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
322 DestroySessionLengthLimiter();
324 // Pref indicating user activity set. Session start time not set.
325 SetSessionUserActivitySeenPref(true);
326 ClearSessionStartTimePref();
327 CreateSessionLengthLimiter(false);
328 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
329 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
330 DestroySessionLengthLimiter();
332 // Pref indicating user activity not set. Session start time in the future.
333 ClearSessionUserActivitySeenPref();
334 SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2));
335 CreateSessionLengthLimiter(false);
336 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
337 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
338 DestroySessionLengthLimiter();
340 // Pref indicating user activity set. Session start time in the future.
341 SetSessionUserActivitySeenPref(true);
342 SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2));
343 CreateSessionLengthLimiter(false);
344 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
345 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
346 DestroySessionLengthLimiter();
348 // Pref indicating user activity not set. Session start time valid.
349 ClearSessionUserActivitySeenPref();
350 SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2));
351 CreateSessionLengthLimiter(false);
352 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
353 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
354 DestroySessionLengthLimiter();
356 // Pref indicating user activity set. Session start time valid.
357 SetSessionUserActivitySeenPref(true);
358 SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2));
359 CreateSessionLengthLimiter(false);
360 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
361 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
362 DestroySessionLengthLimiter();
365 // Verifies that when instructed to wait for initial user activity, the session
366 // start time and the pref indicating user activity are cleared in local state
367 // during login.
368 TEST_F(SessionLengthLimiterTest, StartWaitForInitialUserActivity) {
369 SetWaitForInitialUserActivityPref(true);
371 // Pref indicating user activity not set. Session start time not set.
372 ClearSessionUserActivitySeenPref();
373 ClearSessionStartTimePref();
374 CreateSessionLengthLimiter(false);
375 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
376 EXPECT_FALSE(IsSessionStartTimePrefSet());
377 DestroySessionLengthLimiter();
379 // Pref indicating user activity set. Session start time not set.
380 SetSessionUserActivitySeenPref(true);
381 ClearSessionStartTimePref();
382 CreateSessionLengthLimiter(false);
383 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
384 EXPECT_FALSE(IsSessionStartTimePrefSet());
385 DestroySessionLengthLimiter();
387 // Pref indicating user activity not set. Session start time in the future.
388 ClearSessionUserActivitySeenPref();
389 SetSessionStartTimePref(
390 runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
391 CreateSessionLengthLimiter(false);
392 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
393 EXPECT_FALSE(IsSessionStartTimePrefSet());
394 DestroySessionLengthLimiter();
396 // Pref indicating user activity set. Session start time in the future.
397 SetSessionUserActivitySeenPref(true);
398 SetSessionStartTimePref(
399 runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
400 CreateSessionLengthLimiter(false);
401 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
402 EXPECT_FALSE(IsSessionStartTimePrefSet());
403 DestroySessionLengthLimiter();
405 // Pref indicating user activity not set. Session start time valid.
406 ClearSessionUserActivitySeenPref();
407 SetSessionStartTimePref(
408 runner_->GetCurrentTime() - base::TimeDelta::FromHours(2));
409 CreateSessionLengthLimiter(false);
410 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
411 EXPECT_FALSE(IsSessionStartTimePrefSet());
412 DestroySessionLengthLimiter();
414 // Pref indicating user activity set. Session start time valid.
415 SetSessionUserActivitySeenPref(true);
416 SetSessionStartTimePref(
417 runner_->GetCurrentTime() - base::TimeDelta::FromHours(2));
418 CreateSessionLengthLimiter(false);
419 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
420 EXPECT_FALSE(IsSessionStartTimePrefSet());
421 DestroySessionLengthLimiter();
424 // Verifies that when not instructed to wait for initial user activity, local
425 // state is correctly updated during restart after a crash:
426 // * If no valid session start time is found in local state, the session start
427 // time is set and the pref indicating user activity is cleared.
428 // * If a valid session start time is found in local state, the session start
429 // time and the pref indicating user activity are *not* modified.
430 TEST_F(SessionLengthLimiterTest, RestartDoNotWaitForInitialUserActivity) {
431 // Pref indicating user activity not set. Session start time not set.
432 ClearSessionUserActivitySeenPref();
433 ClearSessionStartTimePref();
434 CreateSessionLengthLimiter(true);
435 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
436 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
437 DestroySessionLengthLimiter();
439 // Pref indicating user activity set. Session start time not set.
440 SetSessionUserActivitySeenPref(true);
441 ClearSessionStartTimePref();
442 CreateSessionLengthLimiter(true);
443 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
444 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
445 DestroySessionLengthLimiter();
447 // Pref indicating user activity not set. Session start time in the future.
448 ClearSessionUserActivitySeenPref();
449 SetSessionStartTimePref(
450 runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
451 CreateSessionLengthLimiter(true);
452 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
453 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
454 DestroySessionLengthLimiter();
456 // Pref indicating user activity set. Session start time in the future.
457 SetSessionUserActivitySeenPref(true);
458 SetSessionStartTimePref(
459 runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
460 CreateSessionLengthLimiter(true);
461 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
462 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
463 DestroySessionLengthLimiter();
465 const base::TimeTicks stored_session_start_time =
466 runner_->GetCurrentTime() - base::TimeDelta::FromHours(2);
468 // Pref indicating user activity not set. Session start time valid.
469 ClearSessionUserActivitySeenPref();
470 SetSessionStartTimePref(stored_session_start_time);
471 CreateSessionLengthLimiter(true);
472 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
473 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
474 DestroySessionLengthLimiter();
476 // Pref indicating user activity set. Session start time valid.
477 SetSessionUserActivitySeenPref(true);
478 SetSessionStartTimePref(stored_session_start_time);
479 CreateSessionLengthLimiter(true);
480 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
481 EXPECT_TRUE(GetSessionUserActivitySeenPref());
482 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
483 DestroySessionLengthLimiter();
486 // Verifies that when instructed to wait for initial user activity, local state
487 // is correctly updated during restart after a crash:
488 // * If no valid session start time is found in local state, the session start
489 // time and the pref indicating user activity are cleared.
490 // * If a valid session start time is found in local state, the session start
491 // time and the pref indicating user activity are *not* modified.
492 TEST_F(SessionLengthLimiterTest, RestartWaitForInitialUserActivity) {
493 SetWaitForInitialUserActivityPref(true);
495 // Pref indicating user activity not set. Session start time not set.
496 ClearSessionUserActivitySeenPref();
497 ClearSessionStartTimePref();
498 CreateSessionLengthLimiter(true);
499 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
500 EXPECT_FALSE(IsSessionStartTimePrefSet());
501 DestroySessionLengthLimiter();
503 // Pref indicating user activity set. Session start time not set.
504 SetSessionUserActivitySeenPref(true);
505 ClearSessionStartTimePref();
506 CreateSessionLengthLimiter(true);
507 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
508 EXPECT_FALSE(IsSessionStartTimePrefSet());
509 DestroySessionLengthLimiter();
511 // Pref indicating user activity not set. Session start time in the future.
512 ClearSessionUserActivitySeenPref();
513 SetSessionStartTimePref(
514 runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
515 CreateSessionLengthLimiter(true);
516 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
517 EXPECT_FALSE(IsSessionStartTimePrefSet());
518 DestroySessionLengthLimiter();
520 // Pref indicating user activity set. Session start time in the future.
521 SetSessionUserActivitySeenPref(true);
522 SetSessionStartTimePref(
523 runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
524 CreateSessionLengthLimiter(true);
525 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
526 EXPECT_FALSE(IsSessionStartTimePrefSet());
527 DestroySessionLengthLimiter();
529 const base::TimeTicks stored_session_start_time =
530 runner_->GetCurrentTime() - base::TimeDelta::FromHours(2);
532 // Pref indicating user activity not set. Session start time valid.
533 ClearSessionUserActivitySeenPref();
534 SetSessionStartTimePref(stored_session_start_time);
535 CreateSessionLengthLimiter(true);
536 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
537 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
538 DestroySessionLengthLimiter();
540 // Pref indicating user activity set. Session start time valid.
541 SetSessionUserActivitySeenPref(true);
542 SetSessionStartTimePref(stored_session_start_time);
543 CreateSessionLengthLimiter(true);
544 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
545 EXPECT_TRUE(GetSessionUserActivitySeenPref());
546 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
547 DestroySessionLengthLimiter();
550 // Verifies that local state is correctly updated when waiting for initial user
551 // activity is toggled and no user activity has occurred yet.
552 TEST_F(SessionLengthLimiterTest, ToggleWaitForInitialUserActivity) {
553 CreateSessionLengthLimiter(false);
555 // Verify that the pref indicating user activity was not set and the session
556 // start time was set.
557 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
558 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
560 // Enable waiting for initial user activity.
561 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
562 SetWaitForInitialUserActivityPref(true);
564 // Verify that the session start time was cleared and the pref indicating user
565 // activity was not set.
566 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
567 EXPECT_FALSE(IsSessionStartTimePrefSet());
569 // Disable waiting for initial user activity.
570 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
571 SetWaitForInitialUserActivityPref(false);
573 // Verify that the pref indicating user activity was not set and the session
574 // start time was.
575 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
576 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
579 // Verifies that local state is correctly updated when instructed not to wait
580 // for initial user activity and user activity occurs. Also verifies that once
581 // initial user activity has occurred, neither the session start time nor the
582 // pref indicating user activity change in local state anymore.
583 TEST_F(SessionLengthLimiterTest, UserActivityWhileNotWaiting) {
584 CreateSessionLengthLimiter(false);
586 // Verify that the pref indicating user activity was not set and the session
587 // start time was set.
588 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
589 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
591 // Simulate user activity.
592 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
593 SimulateUserActivity();
595 // Verify that the pref indicating user activity and the session start time
596 // were set.
597 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
598 EXPECT_TRUE(GetSessionUserActivitySeenPref());
599 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
601 // Simulate user activity.
602 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
603 SimulateUserActivity();
605 // Verify that the pref indicating user activity and the session start time
606 // were not changed.
607 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
608 EXPECT_TRUE(GetSessionUserActivitySeenPref());
609 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
611 // Enable waiting for initial user activity.
612 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
613 SetWaitForInitialUserActivityPref(true);
615 // Verify that the pref indicating user activity and the session start time
616 // were not changed.
617 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
618 EXPECT_TRUE(GetSessionUserActivitySeenPref());
619 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
622 // Verifies that local state is correctly updated when instructed to wait for
623 // initial user activity and user activity occurs. Also verifies that once
624 // initial user activity has occurred, neither the session start time nor the
625 // pref indicating user activity change in local state anymore.
626 TEST_F(SessionLengthLimiterTest, UserActivityWhileWaiting) {
627 SetWaitForInitialUserActivityPref(true);
629 CreateSessionLengthLimiter(false);
631 // Verify that the pref indicating user activity and the session start time
632 // were not set.
633 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
634 EXPECT_FALSE(IsSessionStartTimePrefSet());
636 // Simulate user activity.
637 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
638 SimulateUserActivity();
640 // Verify that the pref indicating user activity and the session start time
641 // were set.
642 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
643 EXPECT_TRUE(GetSessionUserActivitySeenPref());
644 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
646 // Simulate user activity.
647 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
648 SimulateUserActivity();
650 // Verify that the pref indicating user activity and the session start time
651 // were not changed.
652 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
653 EXPECT_TRUE(GetSessionUserActivitySeenPref());
654 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
656 // Disable waiting for initial user activity.
657 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
658 SetWaitForInitialUserActivityPref(false);
660 // Verify that the pref indicating user activity and the session start time
661 // were not changed.
662 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
663 EXPECT_TRUE(GetSessionUserActivitySeenPref());
664 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
667 // Creates a SessionLengthLimiter without setting a limit. Verifies that the
668 // limiter does not start a timer.
669 TEST_F(SessionLengthLimiterTest, RunWithoutLimit) {
670 base::ThreadTaskRunnerHandle runner_handler(runner_);
672 CreateSessionLengthLimiter(false);
674 // Verify that no timer fires to terminate the session.
675 runner_->FastForwardUntilNoTasksRemain();
678 // Creates a SessionLengthLimiter after setting a limit and instructs it not to
679 // wait for user activity. Verifies that the limiter starts a timer even if no
680 // user activity occurs and that when the session length reaches the limit, the
681 // session is terminated.
682 TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileNotWaiting) {
683 base::ThreadTaskRunnerHandle runner_handler(runner_);
685 // Set a 60 second session time limit.
686 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
688 CreateSessionLengthLimiter(false);
689 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
691 // Verify that the timer fires and the session is terminated when the session
692 // length limit is reached.
693 ExpectStopSession();
694 runner_->FastForwardUntilNoTasksRemain();
695 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
696 session_stop_time_);
699 // Creates a SessionLengthLimiter after setting a limit and instructs it to wait
700 // for initial user activity. Verifies that if no user activity occurs, the
701 // limiter does not start a timer.
702 TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileWaiting) {
703 base::ThreadTaskRunnerHandle runner_handler(runner_);
704 SetWaitForInitialUserActivityPref(true);
706 // Set a 60 second session time limit.
707 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
709 CreateSessionLengthLimiter(false);
710 EXPECT_FALSE(IsSessionStartTimePrefSet());
712 // Verify that no timer fires to terminate the session.
713 runner_->FastForwardUntilNoTasksRemain();
716 // Creates a SessionLengthLimiter after setting a limit and instructs it not to
717 // wait for user activity. Verifies that the limiter starts a timer and that
718 // when the session length reaches the limit, the session is terminated. Also
719 // verifies that user activity does not affect the timer.
720 TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileNotWaiting) {
721 base::ThreadTaskRunnerHandle runner_handler(runner_);
723 // Set a 60 second session time limit.
724 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
726 CreateSessionLengthLimiter(false);
727 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
729 // Simulate user activity after 20 seconds.
730 runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
731 SimulateUserActivity();
732 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
734 // Verify that the timer fires and the session is terminated when the session
735 // length limit is reached.
736 ExpectStopSession();
737 runner_->FastForwardUntilNoTasksRemain();
738 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
739 session_stop_time_);
742 // Creates a SessionLengthLimiter after setting a limit and instructs it to wait
743 // for initial user activity. Verifies that once user activity occurs, the
744 // limiter starts a timer and that when the session length reaches the limit,
745 // the session is terminated. Also verifies that further user activity does not
746 // affect the timer.
747 TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileWaiting) {
748 base::ThreadTaskRunnerHandle runner_handler(runner_);
749 SetWaitForInitialUserActivityPref(true);
751 // Set a 60 second session time limit.
752 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
754 CreateSessionLengthLimiter(false);
755 EXPECT_FALSE(IsSessionStartTimePrefSet());
757 // Simulate user activity after 20 seconds.
758 runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
759 SimulateUserActivity();
760 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
762 // Simulate user activity after 20 seconds.
763 runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
764 SimulateUserActivity();
765 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
767 // Verify that the timer fires and the session is terminated when the session
768 // length limit is reached.
769 ExpectStopSession();
770 runner_->FastForwardUntilNoTasksRemain();
771 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
772 session_stop_time_);
775 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
776 // seconds of session time to pass, then increases the limit to 90 seconds.
777 // Verifies that when the session time reaches the new 90 second limit, the
778 // session is terminated.
779 TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) {
780 base::ThreadTaskRunnerHandle runner_handler(runner_);
782 // Set a 60 second session time limit.
783 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
785 CreateSessionLengthLimiter(false);
787 // Fast forward the time by 50 seconds, verifying that no timer fires to
788 // terminate the session.
789 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
791 // Increase the session length limit to 90 seconds.
792 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(90));
794 // Verify that the the timer fires and the session is terminated when the
795 // session length limit is reached.
796 ExpectStopSession();
797 runner_->FastForwardUntilNoTasksRemain();
798 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(90),
799 session_stop_time_);
802 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
803 // seconds of session time to pass, then decreases the limit to 40 seconds.
804 // Verifies that when the limit is decreased to 40 seconds after 50 seconds of
805 // session time have passed, the next timer tick causes the session to be
806 // terminated.
807 TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) {
808 base::ThreadTaskRunnerHandle runner_handler(runner_);
810 // Set a 60 second session time limit.
811 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
813 CreateSessionLengthLimiter(false);
815 // Fast forward the time by 50 seconds, verifying that no timer fires to
816 // terminate the session.
817 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
819 // Verify that reducing the session length limit below the 50 seconds that
820 // have already elapsed causes the session to be terminated immediately.
821 ExpectStopSession();
822 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(40));
823 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(50),
824 session_stop_time_);
827 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
828 // seconds of session time to pass, then removes the limit. Verifies that after
829 // the limit is removed, the session is not terminated when the session time
830 // reaches the original 60 second limit.
831 TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) {
832 base::ThreadTaskRunnerHandle runner_handler(runner_);
834 // Set a 60 second session time limit.
835 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
837 CreateSessionLengthLimiter(false);
839 // Fast forward the time by 50 seconds, verifying that no timer fires to
840 // terminate the session.
841 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
843 // Remove the session length limit.
844 ClearSessionLengthLimitPref();
846 // Verify that no timer fires to terminate the session.
847 runner_->FastForwardUntilNoTasksRemain();
850 } // namespace chromeos