ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / session_length_limiter_unittest.cc
blob9421c787d0c934fed1844bb740876fcea4444c10
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 "base/compiler_specific.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/test/test_mock_time_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/values.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/testing_browser_process.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::Invoke;
21 using ::testing::Mock;
22 using ::testing::NiceMock;
24 namespace chromeos {
26 namespace {
28 class MockSessionLengthLimiterDelegate : public SessionLengthLimiter::Delegate {
29 public:
30 MOCK_CONST_METHOD0(GetCurrentTime, const base::TimeTicks(void));
31 MOCK_METHOD0(StopSession, void(void));
34 } // namespace
36 class SessionLengthLimiterTest : public testing::Test {
37 protected:
38 SessionLengthLimiterTest();
40 // testing::Test:
41 void SetUp() override;
42 void TearDown() override;
44 void SetSessionUserActivitySeenPref(bool user_activity_seen);
45 void ClearSessionUserActivitySeenPref();
46 bool IsSessionUserActivitySeenPrefSet();
47 bool GetSessionUserActivitySeenPref();
49 void SetSessionStartTimePref(const base::TimeTicks& session_start_time);
50 void ClearSessionStartTimePref();
51 bool IsSessionStartTimePrefSet();
52 base::TimeTicks GetSessionStartTimePref();
54 void SetSessionLengthLimitPref(const base::TimeDelta& session_length_limit);
55 void ClearSessionLengthLimitPref();
57 void SetWaitForInitialUserActivityPref(bool wait_for_initial_user_activity);
59 void SimulateUserActivity();
61 void UpdateSessionStartTimeIfWaitingForUserActivity();
63 void ExpectStopSession();
64 void SaveSessionStopTime();
66 // Clears the session state by resetting |user_activity_| and
67 // |session_start_time_| and creates a new SessionLengthLimiter.
68 void CreateSessionLengthLimiter(bool browser_restarted);
70 void DestroySessionLengthLimiter();
72 scoped_refptr<base::TestMockTimeTaskRunner> runner_;
73 base::TimeTicks session_start_time_;
74 base::TimeTicks session_stop_time_;
76 private:
77 TestingPrefServiceSimple local_state_;
78 bool user_activity_seen_;
80 MockSessionLengthLimiterDelegate* delegate_; // Owned by
81 // session_length_limiter_.
82 scoped_ptr<SessionLengthLimiter> session_length_limiter_;
85 SessionLengthLimiterTest::SessionLengthLimiterTest()
86 : user_activity_seen_(false),
87 delegate_(NULL) {
90 void SessionLengthLimiterTest::SetUp() {
91 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
92 SessionLengthLimiter::RegisterPrefs(local_state_.registry());
93 runner_ = new base::TestMockTimeTaskRunner;
94 runner_->FastForwardBy(base::TimeDelta::FromInternalValue(1000));
97 void SessionLengthLimiterTest::TearDown() {
98 session_length_limiter_.reset();
99 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
102 void SessionLengthLimiterTest::SetSessionUserActivitySeenPref(
103 bool user_activity_seen) {
104 local_state_.SetUserPref(prefs::kSessionUserActivitySeen,
105 new base::FundamentalValue(user_activity_seen));
108 void SessionLengthLimiterTest::ClearSessionUserActivitySeenPref() {
109 local_state_.ClearPref(prefs::kSessionUserActivitySeen);
112 bool SessionLengthLimiterTest::IsSessionUserActivitySeenPrefSet() {
113 return local_state_.HasPrefPath(prefs::kSessionUserActivitySeen);
116 bool SessionLengthLimiterTest::GetSessionUserActivitySeenPref() {
117 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
118 return local_state_.GetBoolean(prefs::kSessionUserActivitySeen);
121 void SessionLengthLimiterTest::SetSessionStartTimePref(
122 const base::TimeTicks& session_start_time) {
123 local_state_.SetUserPref(
124 prefs::kSessionStartTime,
125 new base::StringValue(
126 base::Int64ToString(session_start_time.ToInternalValue())));
129 void SessionLengthLimiterTest::ClearSessionStartTimePref() {
130 local_state_.ClearPref(prefs::kSessionStartTime);
133 bool SessionLengthLimiterTest::IsSessionStartTimePrefSet() {
134 return local_state_.HasPrefPath(prefs::kSessionStartTime);
137 base::TimeTicks SessionLengthLimiterTest::GetSessionStartTimePref() {
138 EXPECT_TRUE(IsSessionStartTimePrefSet());
139 return base::TimeTicks::FromInternalValue(
140 local_state_.GetInt64(prefs::kSessionStartTime));
143 void SessionLengthLimiterTest::SetSessionLengthLimitPref(
144 const base::TimeDelta& session_length_limit) {
145 local_state_.SetUserPref(prefs::kSessionLengthLimit,
146 new base::FundamentalValue(
147 static_cast<int>(session_length_limit.InMilliseconds())));
148 UpdateSessionStartTimeIfWaitingForUserActivity();
151 void SessionLengthLimiterTest::ClearSessionLengthLimitPref() {
152 local_state_.RemoveUserPref(prefs::kSessionLengthLimit);
153 UpdateSessionStartTimeIfWaitingForUserActivity();
156 void SessionLengthLimiterTest::SetWaitForInitialUserActivityPref(
157 bool wait_for_initial_user_activity) {
158 UpdateSessionStartTimeIfWaitingForUserActivity();
159 local_state_.SetUserPref(
160 prefs::kSessionWaitForInitialUserActivity,
161 new base::FundamentalValue(wait_for_initial_user_activity));
164 void SessionLengthLimiterTest::SimulateUserActivity() {
165 if (session_length_limiter_)
166 session_length_limiter_->OnUserActivity(NULL);
167 UpdateSessionStartTimeIfWaitingForUserActivity();
168 user_activity_seen_ = true;
171 void SessionLengthLimiterTest::
172 UpdateSessionStartTimeIfWaitingForUserActivity() {
173 if (!user_activity_seen_ &&
174 local_state_.GetBoolean(prefs::kSessionWaitForInitialUserActivity)) {
175 session_start_time_ = runner_->NowTicks();
179 void SessionLengthLimiterTest::ExpectStopSession() {
180 Mock::VerifyAndClearExpectations(delegate_);
181 EXPECT_CALL(*delegate_, StopSession())
182 .Times(1)
183 .WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime));
186 void SessionLengthLimiterTest::SaveSessionStopTime() {
187 session_stop_time_ = runner_->NowTicks();
190 void SessionLengthLimiterTest::CreateSessionLengthLimiter(
191 bool browser_restarted) {
192 user_activity_seen_ = false;
193 session_start_time_ = runner_->NowTicks();
195 EXPECT_FALSE(delegate_);
196 delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>;
197 ON_CALL(*delegate_, GetCurrentTime())
198 .WillByDefault(
199 Invoke(runner_.get(), &base::TestMockTimeTaskRunner::NowTicks));
200 EXPECT_CALL(*delegate_, StopSession()).Times(0);
201 session_length_limiter_.reset(
202 new SessionLengthLimiter(delegate_, browser_restarted));
205 void SessionLengthLimiterTest::DestroySessionLengthLimiter() {
206 session_length_limiter_.reset();
207 delegate_ = NULL;
210 // Verifies that when not instructed to wait for initial user activity, the
211 // session start time is set and the pref indicating user activity is cleared
212 // in local state during login.
213 TEST_F(SessionLengthLimiterTest, StartDoNotWaitForInitialUserActivity) {
214 // Pref indicating user activity not set. Session start time not set.
215 ClearSessionUserActivitySeenPref();
216 ClearSessionStartTimePref();
217 CreateSessionLengthLimiter(false);
218 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
219 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
220 DestroySessionLengthLimiter();
222 // Pref indicating user activity set. Session start time not set.
223 SetSessionUserActivitySeenPref(true);
224 ClearSessionStartTimePref();
225 CreateSessionLengthLimiter(false);
226 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
227 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
228 DestroySessionLengthLimiter();
230 // Pref indicating user activity not set. Session start time in the future.
231 ClearSessionUserActivitySeenPref();
232 SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2));
233 CreateSessionLengthLimiter(false);
234 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
235 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
236 DestroySessionLengthLimiter();
238 // Pref indicating user activity set. Session start time in the future.
239 SetSessionUserActivitySeenPref(true);
240 SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2));
241 CreateSessionLengthLimiter(false);
242 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
243 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
244 DestroySessionLengthLimiter();
246 // Pref indicating user activity not set. Session start time valid.
247 ClearSessionUserActivitySeenPref();
248 SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2));
249 CreateSessionLengthLimiter(false);
250 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
251 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
252 DestroySessionLengthLimiter();
254 // Pref indicating user activity set. Session start time valid.
255 SetSessionUserActivitySeenPref(true);
256 SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2));
257 CreateSessionLengthLimiter(false);
258 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
259 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
260 DestroySessionLengthLimiter();
263 // Verifies that when instructed to wait for initial user activity, the session
264 // start time and the pref indicating user activity are cleared in local state
265 // during login.
266 TEST_F(SessionLengthLimiterTest, StartWaitForInitialUserActivity) {
267 SetWaitForInitialUserActivityPref(true);
269 // Pref indicating user activity not set. Session start time not set.
270 ClearSessionUserActivitySeenPref();
271 ClearSessionStartTimePref();
272 CreateSessionLengthLimiter(false);
273 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
274 EXPECT_FALSE(IsSessionStartTimePrefSet());
275 DestroySessionLengthLimiter();
277 // Pref indicating user activity set. Session start time not set.
278 SetSessionUserActivitySeenPref(true);
279 ClearSessionStartTimePref();
280 CreateSessionLengthLimiter(false);
281 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
282 EXPECT_FALSE(IsSessionStartTimePrefSet());
283 DestroySessionLengthLimiter();
285 // Pref indicating user activity not set. Session start time in the future.
286 ClearSessionUserActivitySeenPref();
287 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2));
288 CreateSessionLengthLimiter(false);
289 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
290 EXPECT_FALSE(IsSessionStartTimePrefSet());
291 DestroySessionLengthLimiter();
293 // Pref indicating user activity set. Session start time in the future.
294 SetSessionUserActivitySeenPref(true);
295 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2));
296 CreateSessionLengthLimiter(false);
297 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
298 EXPECT_FALSE(IsSessionStartTimePrefSet());
299 DestroySessionLengthLimiter();
301 // Pref indicating user activity not set. Session start time valid.
302 ClearSessionUserActivitySeenPref();
303 SetSessionStartTimePref(runner_->NowTicks() - base::TimeDelta::FromHours(2));
304 CreateSessionLengthLimiter(false);
305 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
306 EXPECT_FALSE(IsSessionStartTimePrefSet());
307 DestroySessionLengthLimiter();
309 // Pref indicating user activity set. Session start time valid.
310 SetSessionUserActivitySeenPref(true);
311 SetSessionStartTimePref(runner_->NowTicks() - base::TimeDelta::FromHours(2));
312 CreateSessionLengthLimiter(false);
313 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
314 EXPECT_FALSE(IsSessionStartTimePrefSet());
315 DestroySessionLengthLimiter();
318 // Verifies that when not instructed to wait for initial user activity, local
319 // state is correctly updated during restart after a crash:
320 // * If no valid session start time is found in local state, the session start
321 // time is set and the pref indicating user activity is cleared.
322 // * If a valid session start time is found in local state, the session start
323 // time and the pref indicating user activity are *not* modified.
324 TEST_F(SessionLengthLimiterTest, RestartDoNotWaitForInitialUserActivity) {
325 // Pref indicating user activity not set. Session start time not set.
326 ClearSessionUserActivitySeenPref();
327 ClearSessionStartTimePref();
328 CreateSessionLengthLimiter(true);
329 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
330 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
331 DestroySessionLengthLimiter();
333 // Pref indicating user activity set. Session start time not set.
334 SetSessionUserActivitySeenPref(true);
335 ClearSessionStartTimePref();
336 CreateSessionLengthLimiter(true);
337 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
338 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
339 DestroySessionLengthLimiter();
341 // Pref indicating user activity not set. Session start time in the future.
342 ClearSessionUserActivitySeenPref();
343 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2));
344 CreateSessionLengthLimiter(true);
345 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
346 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
347 DestroySessionLengthLimiter();
349 // Pref indicating user activity set. Session start time in the future.
350 SetSessionUserActivitySeenPref(true);
351 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2));
352 CreateSessionLengthLimiter(true);
353 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
354 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
355 DestroySessionLengthLimiter();
357 const base::TimeTicks stored_session_start_time =
358 runner_->NowTicks() - base::TimeDelta::FromHours(2);
360 // Pref indicating user activity not set. Session start time valid.
361 ClearSessionUserActivitySeenPref();
362 SetSessionStartTimePref(stored_session_start_time);
363 CreateSessionLengthLimiter(true);
364 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
365 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
366 DestroySessionLengthLimiter();
368 // Pref indicating user activity set. Session start time valid.
369 SetSessionUserActivitySeenPref(true);
370 SetSessionStartTimePref(stored_session_start_time);
371 CreateSessionLengthLimiter(true);
372 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
373 EXPECT_TRUE(GetSessionUserActivitySeenPref());
374 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
375 DestroySessionLengthLimiter();
378 // Verifies that when instructed to wait for initial user activity, local state
379 // is correctly updated during restart after a crash:
380 // * If no valid session start time is found in local state, the session start
381 // time and the pref indicating user activity are cleared.
382 // * If a valid session start time is found in local state, the session start
383 // time and the pref indicating user activity are *not* modified.
384 TEST_F(SessionLengthLimiterTest, RestartWaitForInitialUserActivity) {
385 SetWaitForInitialUserActivityPref(true);
387 // Pref indicating user activity not set. Session start time not set.
388 ClearSessionUserActivitySeenPref();
389 ClearSessionStartTimePref();
390 CreateSessionLengthLimiter(true);
391 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
392 EXPECT_FALSE(IsSessionStartTimePrefSet());
393 DestroySessionLengthLimiter();
395 // Pref indicating user activity set. Session start time not set.
396 SetSessionUserActivitySeenPref(true);
397 ClearSessionStartTimePref();
398 CreateSessionLengthLimiter(true);
399 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
400 EXPECT_FALSE(IsSessionStartTimePrefSet());
401 DestroySessionLengthLimiter();
403 // Pref indicating user activity not set. Session start time in the future.
404 ClearSessionUserActivitySeenPref();
405 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2));
406 CreateSessionLengthLimiter(true);
407 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
408 EXPECT_FALSE(IsSessionStartTimePrefSet());
409 DestroySessionLengthLimiter();
411 // Pref indicating user activity set. Session start time in the future.
412 SetSessionUserActivitySeenPref(true);
413 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2));
414 CreateSessionLengthLimiter(true);
415 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
416 EXPECT_FALSE(IsSessionStartTimePrefSet());
417 DestroySessionLengthLimiter();
419 const base::TimeTicks stored_session_start_time =
420 runner_->NowTicks() - base::TimeDelta::FromHours(2);
422 // Pref indicating user activity not set. Session start time valid.
423 ClearSessionUserActivitySeenPref();
424 SetSessionStartTimePref(stored_session_start_time);
425 CreateSessionLengthLimiter(true);
426 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
427 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
428 DestroySessionLengthLimiter();
430 // Pref indicating user activity set. Session start time valid.
431 SetSessionUserActivitySeenPref(true);
432 SetSessionStartTimePref(stored_session_start_time);
433 CreateSessionLengthLimiter(true);
434 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
435 EXPECT_TRUE(GetSessionUserActivitySeenPref());
436 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
437 DestroySessionLengthLimiter();
440 // Verifies that local state is correctly updated when waiting for initial user
441 // activity is toggled and no user activity has occurred yet.
442 TEST_F(SessionLengthLimiterTest, ToggleWaitForInitialUserActivity) {
443 CreateSessionLengthLimiter(false);
445 // Verify that the pref indicating user activity was not set and the session
446 // start time was set.
447 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
448 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
450 // Enable waiting for initial user activity.
451 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
452 SetWaitForInitialUserActivityPref(true);
454 // Verify that the session start time was cleared and the pref indicating user
455 // activity was not set.
456 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
457 EXPECT_FALSE(IsSessionStartTimePrefSet());
459 // Disable waiting for initial user activity.
460 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
461 SetWaitForInitialUserActivityPref(false);
463 // Verify that the pref indicating user activity was not set and the session
464 // start time was.
465 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
466 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
469 // Verifies that local state is correctly updated when instructed not to wait
470 // for initial user activity and user activity occurs. Also verifies that once
471 // initial user activity has occurred, neither the session start time nor the
472 // pref indicating user activity change in local state anymore.
473 TEST_F(SessionLengthLimiterTest, UserActivityWhileNotWaiting) {
474 CreateSessionLengthLimiter(false);
476 // Verify that the pref indicating user activity was not set and the session
477 // start time was set.
478 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
479 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
481 // Simulate user activity.
482 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
483 SimulateUserActivity();
485 // Verify that the pref indicating user activity and the session start time
486 // were set.
487 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
488 EXPECT_TRUE(GetSessionUserActivitySeenPref());
489 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
491 // Simulate user activity.
492 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
493 SimulateUserActivity();
495 // Verify that the pref indicating user activity and the session start time
496 // were not changed.
497 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
498 EXPECT_TRUE(GetSessionUserActivitySeenPref());
499 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
501 // Enable waiting for initial user activity.
502 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
503 SetWaitForInitialUserActivityPref(true);
505 // Verify that the pref indicating user activity and the session start time
506 // were not changed.
507 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
508 EXPECT_TRUE(GetSessionUserActivitySeenPref());
509 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
512 // Verifies that local state is correctly updated when instructed to wait for
513 // initial user activity and user activity occurs. Also verifies that once
514 // initial user activity has occurred, neither the session start time nor the
515 // pref indicating user activity change in local state anymore.
516 TEST_F(SessionLengthLimiterTest, UserActivityWhileWaiting) {
517 SetWaitForInitialUserActivityPref(true);
519 CreateSessionLengthLimiter(false);
521 // Verify that the pref indicating user activity and the session start time
522 // were not set.
523 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
524 EXPECT_FALSE(IsSessionStartTimePrefSet());
526 // Simulate user activity.
527 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
528 SimulateUserActivity();
530 // Verify that the pref indicating user activity and the session start time
531 // were set.
532 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
533 EXPECT_TRUE(GetSessionUserActivitySeenPref());
534 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
536 // Simulate user activity.
537 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
538 SimulateUserActivity();
540 // Verify that the pref indicating user activity and the session start time
541 // were not changed.
542 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
543 EXPECT_TRUE(GetSessionUserActivitySeenPref());
544 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
546 // Disable waiting for initial user activity.
547 runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
548 SetWaitForInitialUserActivityPref(false);
550 // Verify that the pref indicating user activity and the session start time
551 // were not changed.
552 EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
553 EXPECT_TRUE(GetSessionUserActivitySeenPref());
554 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
557 // Creates a SessionLengthLimiter without setting a limit. Verifies that the
558 // limiter does not start a timer.
559 TEST_F(SessionLengthLimiterTest, RunWithoutLimit) {
560 base::ThreadTaskRunnerHandle runner_handler(runner_);
562 CreateSessionLengthLimiter(false);
564 // Verify that no timer fires to terminate the session.
565 runner_->FastForwardUntilNoTasksRemain();
568 // Creates a SessionLengthLimiter after setting a limit and instructs it not to
569 // wait for user activity. Verifies that the limiter starts a timer even if no
570 // user activity occurs and that when the session length reaches the limit, the
571 // session is terminated.
572 TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileNotWaiting) {
573 base::ThreadTaskRunnerHandle runner_handler(runner_);
575 // Set a 60 second session time limit.
576 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
578 CreateSessionLengthLimiter(false);
579 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
581 // Verify that the timer fires and the session is terminated when the session
582 // length limit is reached.
583 ExpectStopSession();
584 runner_->FastForwardUntilNoTasksRemain();
585 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
586 session_stop_time_);
589 // Creates a SessionLengthLimiter after setting a limit and instructs it to wait
590 // for initial user activity. Verifies that if no user activity occurs, the
591 // limiter does not start a timer.
592 TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileWaiting) {
593 base::ThreadTaskRunnerHandle runner_handler(runner_);
594 SetWaitForInitialUserActivityPref(true);
596 // Set a 60 second session time limit.
597 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
599 CreateSessionLengthLimiter(false);
600 EXPECT_FALSE(IsSessionStartTimePrefSet());
602 // Verify that no timer fires to terminate the session.
603 runner_->FastForwardUntilNoTasksRemain();
606 // Creates a SessionLengthLimiter after setting a limit and instructs it not to
607 // wait for user activity. Verifies that the limiter starts a timer and that
608 // when the session length reaches the limit, the session is terminated. Also
609 // verifies that user activity does not affect the timer.
610 TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileNotWaiting) {
611 base::ThreadTaskRunnerHandle runner_handler(runner_);
613 // Set a 60 second session time limit.
614 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
616 CreateSessionLengthLimiter(false);
617 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
619 // Simulate user activity after 20 seconds.
620 runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
621 SimulateUserActivity();
622 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
624 // Verify that the timer fires and the session is terminated when the session
625 // length limit is reached.
626 ExpectStopSession();
627 runner_->FastForwardUntilNoTasksRemain();
628 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
629 session_stop_time_);
632 // Creates a SessionLengthLimiter after setting a limit and instructs it to wait
633 // for initial user activity. Verifies that once user activity occurs, the
634 // limiter starts a timer and that when the session length reaches the limit,
635 // the session is terminated. Also verifies that further user activity does not
636 // affect the timer.
637 TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileWaiting) {
638 base::ThreadTaskRunnerHandle runner_handler(runner_);
639 SetWaitForInitialUserActivityPref(true);
641 // Set a 60 second session time limit.
642 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
644 CreateSessionLengthLimiter(false);
645 EXPECT_FALSE(IsSessionStartTimePrefSet());
647 // Simulate user activity after 20 seconds.
648 runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
649 SimulateUserActivity();
650 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
652 // Simulate user activity after 20 seconds.
653 runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
654 SimulateUserActivity();
655 EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
657 // Verify that the timer fires and the session is terminated when the session
658 // length limit is reached.
659 ExpectStopSession();
660 runner_->FastForwardUntilNoTasksRemain();
661 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
662 session_stop_time_);
665 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
666 // seconds of session time to pass, then increases the limit to 90 seconds.
667 // Verifies that when the session time reaches the new 90 second limit, the
668 // session is terminated.
669 TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) {
670 base::ThreadTaskRunnerHandle runner_handler(runner_);
672 // Set a 60 second session time limit.
673 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
675 CreateSessionLengthLimiter(false);
677 // Fast forward the time by 50 seconds, verifying that no timer fires to
678 // terminate the session.
679 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
681 // Increase the session length limit to 90 seconds.
682 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(90));
684 // Verify that the the timer fires and the session is terminated when the
685 // session length limit is reached.
686 ExpectStopSession();
687 runner_->FastForwardUntilNoTasksRemain();
688 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(90),
689 session_stop_time_);
692 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
693 // seconds of session time to pass, then decreases the limit to 40 seconds.
694 // Verifies that when the limit is decreased to 40 seconds after 50 seconds of
695 // session time have passed, the next timer tick causes the session to be
696 // terminated.
697 TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) {
698 base::ThreadTaskRunnerHandle runner_handler(runner_);
700 // Set a 60 second session time limit.
701 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
703 CreateSessionLengthLimiter(false);
705 // Fast forward the time by 50 seconds, verifying that no timer fires to
706 // terminate the session.
707 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
709 // Verify that reducing the session length limit below the 50 seconds that
710 // have already elapsed causes the session to be terminated immediately.
711 ExpectStopSession();
712 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(40));
713 EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(50),
714 session_stop_time_);
717 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50
718 // seconds of session time to pass, then removes the limit. Verifies that after
719 // the limit is removed, the session is not terminated when the session time
720 // reaches the original 60 second limit.
721 TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) {
722 base::ThreadTaskRunnerHandle runner_handler(runner_);
724 // Set a 60 second session time limit.
725 SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
727 CreateSessionLengthLimiter(false);
729 // Fast forward the time by 50 seconds, verifying that no timer fires to
730 // terminate the session.
731 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
733 // Remove the session length limit.
734 ClearSessionLengthLimitPref();
736 // Verify that no timer fires to terminate the session.
737 runner_->FastForwardUntilNoTasksRemain();
740 } // namespace chromeos