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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
|
// 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(const base::TimeDelta& time_delta);
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 SetSessionUserActivitySeenPref(bool user_activity_seen);
void ClearSessionUserActivitySeenPref();
bool IsSessionUserActivitySeenPrefSet();
bool GetSessionUserActivitySeenPref();
void SetSessionStartTimePref(const base::TimeTicks& session_start_time);
void ClearSessionStartTimePref();
bool IsSessionStartTimePrefSet();
base::TimeTicks GetSessionStartTimePref();
void SetSessionLengthLimitPref(const base::TimeDelta& session_length_limit);
void ClearSessionLengthLimitPref();
void SetWaitForInitialUserActivityPref(bool wait_for_initial_user_activity);
void SimulateUserActivity();
void UpdateSessionStartTimeIfWaitingForUserActivity();
void ExpectStopSession();
void SaveSessionStopTime();
// Clears the session state by resetting |user_activity_| and
// |session_start_time_| and creates a new SessionLengthLimiter.
void CreateSessionLengthLimiter(bool browser_restarted);
void DestroySessionLengthLimiter();
scoped_refptr<MockTimeSingleThreadTaskRunner> runner_;
base::TimeTicks session_start_time_;
base::TimeTicks session_stop_time_;
private:
TestingPrefServiceSimple local_state_;
bool user_activity_seen_;
MockSessionLengthLimiterDelegate* delegate_; // Owned by
// session_length_limiter_.
scoped_ptr<SessionLengthLimiter> session_length_limiter_;
};
MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner()
: now_(base::TimeTicks::FromInternalValue(1000)) {
}
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(
const base::TimeDelta& time_delta) {
const base::TimeTicks latest = now_ + time_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()
: user_activity_seen_(false),
delegate_(NULL) {
}
void SessionLengthLimiterTest::SetUp() {
TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
SessionLengthLimiter::RegisterPrefs(local_state_.registry());
runner_ = new MockTimeSingleThreadTaskRunner;
}
void SessionLengthLimiterTest::TearDown() {
session_length_limiter_.reset();
TestingBrowserProcess::GetGlobal()->SetLocalState(NULL);
}
void SessionLengthLimiterTest::SetSessionUserActivitySeenPref(
bool user_activity_seen) {
local_state_.SetUserPref(prefs::kSessionUserActivitySeen,
new base::FundamentalValue(user_activity_seen));
}
void SessionLengthLimiterTest::ClearSessionUserActivitySeenPref() {
local_state_.ClearPref(prefs::kSessionUserActivitySeen);
}
bool SessionLengthLimiterTest::IsSessionUserActivitySeenPrefSet() {
return local_state_.HasPrefPath(prefs::kSessionUserActivitySeen);
}
bool SessionLengthLimiterTest::GetSessionUserActivitySeenPref() {
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
return local_state_.GetBoolean(prefs::kSessionUserActivitySeen);
}
void SessionLengthLimiterTest::SetSessionStartTimePref(
const base::TimeTicks& session_start_time) {
local_state_.SetUserPref(
prefs::kSessionStartTime,
new base::StringValue(
base::Int64ToString(session_start_time.ToInternalValue())));
}
void SessionLengthLimiterTest::ClearSessionStartTimePref() {
local_state_.ClearPref(prefs::kSessionStartTime);
}
bool SessionLengthLimiterTest::IsSessionStartTimePrefSet() {
return local_state_.HasPrefPath(prefs::kSessionStartTime);
}
base::TimeTicks SessionLengthLimiterTest::GetSessionStartTimePref() {
EXPECT_TRUE(IsSessionStartTimePrefSet());
return base::TimeTicks::FromInternalValue(
local_state_.GetInt64(prefs::kSessionStartTime));
}
void SessionLengthLimiterTest::SetSessionLengthLimitPref(
const base::TimeDelta& session_length_limit) {
local_state_.SetUserPref(prefs::kSessionLengthLimit,
new base::FundamentalValue(
static_cast<int>(session_length_limit.InMilliseconds())));
UpdateSessionStartTimeIfWaitingForUserActivity();
}
void SessionLengthLimiterTest::ClearSessionLengthLimitPref() {
local_state_.RemoveUserPref(prefs::kSessionLengthLimit);
UpdateSessionStartTimeIfWaitingForUserActivity();
}
void SessionLengthLimiterTest::SetWaitForInitialUserActivityPref(
bool wait_for_initial_user_activity) {
UpdateSessionStartTimeIfWaitingForUserActivity();
local_state_.SetUserPref(
prefs::kSessionWaitForInitialUserActivity,
new base::FundamentalValue(wait_for_initial_user_activity));
}
void SessionLengthLimiterTest::SimulateUserActivity() {
if (session_length_limiter_)
session_length_limiter_->OnUserActivity(NULL);
UpdateSessionStartTimeIfWaitingForUserActivity();
user_activity_seen_ = true;
}
void SessionLengthLimiterTest::
UpdateSessionStartTimeIfWaitingForUserActivity() {
if (!user_activity_seen_ &&
local_state_.GetBoolean(prefs::kSessionWaitForInitialUserActivity)) {
session_start_time_ = runner_->GetCurrentTime();
}
}
void SessionLengthLimiterTest::ExpectStopSession() {
Mock::VerifyAndClearExpectations(delegate_);
EXPECT_CALL(*delegate_, StopSession())
.Times(1)
.WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime));
}
void SessionLengthLimiterTest::SaveSessionStopTime() {
session_stop_time_ = runner_->GetCurrentTime();
}
void SessionLengthLimiterTest::CreateSessionLengthLimiter(
bool browser_restarted) {
user_activity_seen_ = false;
session_start_time_ = runner_->GetCurrentTime();
EXPECT_FALSE(delegate_);
delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>;
ON_CALL(*delegate_, GetCurrentTime())
.WillByDefault(Invoke(runner_.get(),
&MockTimeSingleThreadTaskRunner::GetCurrentTime));
EXPECT_CALL(*delegate_, StopSession()).Times(0);
session_length_limiter_.reset(
new SessionLengthLimiter(delegate_, browser_restarted));
}
void SessionLengthLimiterTest::DestroySessionLengthLimiter() {
session_length_limiter_.reset();
delegate_ = NULL;
}
// Verifies that when not instructed to wait for initial user activity, the
// session start time is set and the pref indicating user activity is cleared
// in local state during login.
TEST_F(SessionLengthLimiterTest, StartDoNotWaitForInitialUserActivity) {
// Pref indicating user activity not set. Session start time not set.
ClearSessionUserActivitySeenPref();
ClearSessionStartTimePref();
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time not set.
SetSessionUserActivitySeenPref(true);
ClearSessionStartTimePref();
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity not set. Session start time in the future.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time in the future.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(session_start_time_ + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity not set. Session start time valid.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time valid.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(session_start_time_ - base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
}
// Verifies that when instructed to wait for initial user activity, the session
// start time and the pref indicating user activity are cleared in local state
// during login.
TEST_F(SessionLengthLimiterTest, StartWaitForInitialUserActivity) {
SetWaitForInitialUserActivityPref(true);
// Pref indicating user activity not set. Session start time not set.
ClearSessionUserActivitySeenPref();
ClearSessionStartTimePref();
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time not set.
SetSessionUserActivitySeenPref(true);
ClearSessionStartTimePref();
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity not set. Session start time in the future.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(
runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time in the future.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(
runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity not set. Session start time valid.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(
runner_->GetCurrentTime() - base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time valid.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(
runner_->GetCurrentTime() - base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
}
// Verifies that when not instructed to wait for initial user activity, local
// state is correctly updated during restart after a crash:
// * If no valid session start time is found in local state, the session start
// time is set and the pref indicating user activity is cleared.
// * If a valid session start time is found in local state, the session start
// time and the pref indicating user activity are *not* modified.
TEST_F(SessionLengthLimiterTest, RestartDoNotWaitForInitialUserActivity) {
// Pref indicating user activity not set. Session start time not set.
ClearSessionUserActivitySeenPref();
ClearSessionStartTimePref();
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time not set.
SetSessionUserActivitySeenPref(true);
ClearSessionStartTimePref();
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity not set. Session start time in the future.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(
runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time in the future.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(
runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
DestroySessionLengthLimiter();
const base::TimeTicks stored_session_start_time =
runner_->GetCurrentTime() - base::TimeDelta::FromHours(2);
// Pref indicating user activity not set. Session start time valid.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(stored_session_start_time);
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time valid.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(stored_session_start_time);
CreateSessionLengthLimiter(true);
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
DestroySessionLengthLimiter();
}
// Verifies that when instructed to wait for initial user activity, local state
// is correctly updated during restart after a crash:
// * If no valid session start time is found in local state, the session start
// time and the pref indicating user activity are cleared.
// * If a valid session start time is found in local state, the session start
// time and the pref indicating user activity are *not* modified.
TEST_F(SessionLengthLimiterTest, RestartWaitForInitialUserActivity) {
SetWaitForInitialUserActivityPref(true);
// Pref indicating user activity not set. Session start time not set.
ClearSessionUserActivitySeenPref();
ClearSessionStartTimePref();
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time not set.
SetSessionUserActivitySeenPref(true);
ClearSessionStartTimePref();
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity not set. Session start time in the future.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(
runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time in the future.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(
runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
DestroySessionLengthLimiter();
const base::TimeTicks stored_session_start_time =
runner_->GetCurrentTime() - base::TimeDelta::FromHours(2);
// Pref indicating user activity not set. Session start time valid.
ClearSessionUserActivitySeenPref();
SetSessionStartTimePref(stored_session_start_time);
CreateSessionLengthLimiter(true);
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
DestroySessionLengthLimiter();
// Pref indicating user activity set. Session start time valid.
SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(stored_session_start_time);
CreateSessionLengthLimiter(true);
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
DestroySessionLengthLimiter();
}
// Verifies that local state is correctly updated when waiting for initial user
// activity is toggled and no user activity has occurred yet.
TEST_F(SessionLengthLimiterTest, ToggleWaitForInitialUserActivity) {
CreateSessionLengthLimiter(false);
// Verify that the pref indicating user activity was not set and the session
// start time was set.
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Enable waiting for initial user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SetWaitForInitialUserActivityPref(true);
// Verify that the session start time was cleared and the pref indicating user
// activity was not set.
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
// Disable waiting for initial user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SetWaitForInitialUserActivityPref(false);
// Verify that the pref indicating user activity was not set and the session
// start time was.
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
}
// Verifies that local state is correctly updated when instructed not to wait
// for initial user activity and user activity occurs. Also verifies that once
// initial user activity has occurred, neither the session start time nor the
// pref indicating user activity change in local state anymore.
TEST_F(SessionLengthLimiterTest, UserActivityWhileNotWaiting) {
CreateSessionLengthLimiter(false);
// Verify that the pref indicating user activity was not set and the session
// start time was set.
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Simulate user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SimulateUserActivity();
// Verify that the pref indicating user activity and the session start time
// were set.
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Simulate user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SimulateUserActivity();
// Verify that the pref indicating user activity and the session start time
// were not changed.
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Enable waiting for initial user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SetWaitForInitialUserActivityPref(true);
// Verify that the pref indicating user activity and the session start time
// were not changed.
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
}
// Verifies that local state is correctly updated when instructed to wait for
// initial user activity and user activity occurs. Also verifies that once
// initial user activity has occurred, neither the session start time nor the
// pref indicating user activity change in local state anymore.
TEST_F(SessionLengthLimiterTest, UserActivityWhileWaiting) {
SetWaitForInitialUserActivityPref(true);
CreateSessionLengthLimiter(false);
// Verify that the pref indicating user activity and the session start time
// were not set.
EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
EXPECT_FALSE(IsSessionStartTimePrefSet());
// Simulate user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SimulateUserActivity();
// Verify that the pref indicating user activity and the session start time
// were set.
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Simulate user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SimulateUserActivity();
// Verify that the pref indicating user activity and the session start time
// were not changed.
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Disable waiting for initial user activity.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(1));
SetWaitForInitialUserActivityPref(false);
// Verify that the pref indicating user activity and the session start time
// were not changed.
EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
EXPECT_TRUE(GetSessionUserActivitySeenPref());
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
}
// Creates a SessionLengthLimiter without setting a limit. Verifies that the
// limiter does not start a timer.
TEST_F(SessionLengthLimiterTest, RunWithoutLimit) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
CreateSessionLengthLimiter(false);
// Verify that no timer fires to terminate the session.
runner_->FastForwardUntilNoTasksRemain();
}
// Creates a SessionLengthLimiter after setting a limit and instructs it not to
// wait for user activity. Verifies that the limiter starts a timer even if no
// user activity occurs and that when the session length reaches the limit, the
// session is terminated.
TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileNotWaiting) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
// Set a 60 second session time limit.
SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Verify that the timer fires and the session is terminated when the session
// length limit is reached.
ExpectStopSession();
runner_->FastForwardUntilNoTasksRemain();
EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
session_stop_time_);
}
// Creates a SessionLengthLimiter after setting a limit and instructs it to wait
// for initial user activity. Verifies that if no user activity occurs, the
// limiter does not start a timer.
TEST_F(SessionLengthLimiterTest, RunWithoutUserActivityWhileWaiting) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
SetWaitForInitialUserActivityPref(true);
// Set a 60 second session time limit.
SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionStartTimePrefSet());
// Verify that no timer fires to terminate the session.
runner_->FastForwardUntilNoTasksRemain();
}
// Creates a SessionLengthLimiter after setting a limit and instructs it not to
// wait for user activity. Verifies that the limiter starts a timer and that
// when the session length reaches the limit, the session is terminated. Also
// verifies that user activity does not affect the timer.
TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileNotWaiting) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
// Set a 60 second session time limit.
SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Simulate user activity after 20 seconds.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
SimulateUserActivity();
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Verify that the timer fires and the session is terminated when the session
// length limit is reached.
ExpectStopSession();
runner_->FastForwardUntilNoTasksRemain();
EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
session_stop_time_);
}
// Creates a SessionLengthLimiter after setting a limit and instructs it to wait
// for initial user activity. Verifies that once user activity occurs, the
// limiter starts a timer and that when the session length reaches the limit,
// the session is terminated. Also verifies that further user activity does not
// affect the timer.
TEST_F(SessionLengthLimiterTest, RunWithUserActivityWhileWaiting) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
SetWaitForInitialUserActivityPref(true);
// Set a 60 second session time limit.
SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
EXPECT_FALSE(IsSessionStartTimePrefSet());
// Simulate user activity after 20 seconds.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
SimulateUserActivity();
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Simulate user activity after 20 seconds.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(20));
SimulateUserActivity();
EXPECT_EQ(session_start_time_, GetSessionStartTimePref());
// Verify that the timer fires and the session is terminated when the session
// length limit is reached.
ExpectStopSession();
runner_->FastForwardUntilNoTasksRemain();
EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(60),
session_stop_time_);
}
// 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(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
// Fast forward the time by 50 seconds, verifying that no timer fires to
// terminate the session.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
// Increase the session length limit to 90 seconds.
SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(90));
// Verify that the the timer fires and the session is terminated when the
// session length limit is reached.
ExpectStopSession();
runner_->FastForwardUntilNoTasksRemain();
EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(90),
session_stop_time_);
}
// 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(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
// Fast forward the time by 50 seconds, verifying that no timer fires to
// terminate the session.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
// Verify that reducing the session length limit below the 50 seconds that
// have already elapsed causes the session to be terminated immediately.
ExpectStopSession();
SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(40));
EXPECT_EQ(session_start_time_ + base::TimeDelta::FromSeconds(50),
session_stop_time_);
}
// 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(base::TimeDelta::FromSeconds(60));
CreateSessionLengthLimiter(false);
// Fast forward the time by 50 seconds, verifying that no timer fires to
// terminate the session.
runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
// Remove the session length limit.
ClearSessionLengthLimitPref();
// Verify that no timer fires to terminate the session.
runner_->FastForwardUntilNoTasksRemain();
}
} // namespace chromeos
|