summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/session_length_limiter_unittest.cc
diff options
context:
space:
mode:
authorbartfab@chromium.org <bartfab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-28 15:38:09 +0000
committerbartfab@chromium.org <bartfab@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-10-28 15:38:09 +0000
commit8e0ba06bd3a63a6d5ae3a342bc1321146b6e18b7 (patch)
tree3c02b16e484c093b1d93d7a36df5587bb83fcc5c /chrome/browser/chromeos/session_length_limiter_unittest.cc
parent820d0d94af4eb2b96fc416de874e477feafae3d9 (diff)
downloadchromium_src-8e0ba06bd3a63a6d5ae3a342bc1321146b6e18b7.zip
chromium_src-8e0ba06bd3a63a6d5ae3a342bc1321146b6e18b7.tar.gz
chromium_src-8e0ba06bd3a63a6d5ae3a342bc1321146b6e18b7.tar.bz2
Add WaitForInitialUserActivity policy
This CL adds a user policy that controls whether power management delays and the session length limit should only start running after initial user activity in a session. The SessionLengthLimiter is modified to obey the new policy. Power management will hooked up to the new policy in a follow-up CL. It would technically be possible to add separate policies that determine whether power management delays on the one hand and the session length limit on the other hand should wait for initial user activity. However, it does not make sense for one of these to start running on session start while the other waits for user activity. Therefore, a single policy is added for both. BUG=310176 TEST=Updated unit and browser tests Review URL: https://codereview.chromium.org/35943002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231332 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos/session_length_limiter_unittest.cc')
-rw-r--r--chrome/browser/chromeos/session_length_limiter_unittest.cc645
1 files changed, 556 insertions, 89 deletions
diff --git a/chrome/browser/chromeos/session_length_limiter_unittest.cc b/chrome/browser/chromeos/session_length_limiter_unittest.cc
index 39495a4..625cb94 100644
--- a/chrome/browser/chromeos/session_length_limiter_unittest.cc
+++ b/chrome/browser/chromeos/session_length_limiter_unittest.cc
@@ -56,7 +56,7 @@ class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner {
const base::TimeTicks& GetCurrentTime() const;
- void FastForwardBy(int64 milliseconds);
+ void FastForwardBy(const base::TimeDelta& time_delta);
void FastForwardUntilNoTasksRemain();
private:
@@ -86,26 +86,49 @@ class SessionLengthLimiterTest : public testing::Test {
virtual void SetUp() OVERRIDE;
virtual void TearDown() OVERRIDE;
- void SetSessionStartTimePref(int64 session_start_time);
- void VerifySessionStartTimePref();
- void SetSessionLengthLimitPref(int64 session_length_limit);
+ void 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 CheckStopSessionTime();
+ void SaveSessionStopTime();
+ // Clears the session state by resetting |user_activity_| and
+ // |session_start_time_| and creates a new SessionLengthLimiter.
void CreateSessionLengthLimiter(bool browser_restarted);
- TestingPrefServiceSimple local_state_;
+ void DestroySessionLengthLimiter();
+
scoped_refptr<MockTimeSingleThreadTaskRunner> runner_;
base::TimeTicks session_start_time_;
- base::TimeTicks session_end_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() {
+MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner()
+ : now_(base::TimeTicks::FromInternalValue(1000)) {
}
bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const {
@@ -132,9 +155,9 @@ const base::TimeTicks& MockTimeSingleThreadTaskRunner::GetCurrentTime() const {
return now_;
}
-void MockTimeSingleThreadTaskRunner::FastForwardBy(int64 delta) {
- const base::TimeTicks latest =
- now_ + base::TimeDelta::FromMilliseconds(delta);
+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;
@@ -169,139 +192,582 @@ void SessionLengthLimiterTest::SetUp() {
TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
SessionLengthLimiter::RegisterPrefs(local_state_.registry());
runner_ = new MockTimeSingleThreadTaskRunner;
- session_start_time_ = runner_->GetCurrentTime();
-
- delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>;
- ON_CALL(*delegate_, GetCurrentTime())
- .WillByDefault(Invoke(runner_.get(),
- &MockTimeSingleThreadTaskRunner::GetCurrentTime));
- EXPECT_CALL(*delegate_, StopSession()).Times(0);
}
void SessionLengthLimiterTest::TearDown() {
+ 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(
- int64 session_start_time) {
- local_state_.SetUserPref(prefs::kSessionStartTime,
- base::Value::CreateStringValue(
- base::Int64ToString(session_start_time)));
+ 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);
}
-void SessionLengthLimiterTest::VerifySessionStartTimePref() {
- base::TimeTicks session_start_time(base::TimeTicks::FromInternalValue(
- local_state_.GetInt64(prefs::kSessionStartTime)));
- EXPECT_EQ(session_start_time_, session_start_time);
+base::TimeTicks SessionLengthLimiterTest::GetSessionStartTimePref() {
+ EXPECT_TRUE(IsSessionStartTimePrefSet());
+ return base::TimeTicks::FromInternalValue(
+ local_state_.GetInt64(prefs::kSessionStartTime));
}
void SessionLengthLimiterTest::SetSessionLengthLimitPref(
- int64 session_length_limit) {
- session_end_time_ = session_start_time_ +
- base::TimeDelta::FromMilliseconds(session_length_limit);
- // If the new session end time has passed already, the session should end now.
- if (session_end_time_ < runner_->GetCurrentTime())
- session_end_time_ = runner_->GetCurrentTime();
+ const base::TimeDelta& session_length_limit) {
local_state_.SetUserPref(prefs::kSessionLengthLimit,
- base::Value::CreateIntegerValue(
- session_length_limit));
+ 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::CheckStopSessionTime));
+ .Times(1)
+ .WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime));
}
-void SessionLengthLimiterTest::CheckStopSessionTime() {
- EXPECT_EQ(session_end_time_, runner_->GetCurrentTime());
+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));
}
-// Verifies that the session start time in local state is updated during login
-// if no session start time has been stored before.
-TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeUnset) {
+
+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);
- VerifySessionStartTimePref();
+ 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 the session start time in local state is updated during login
-// if a session start time lying in the future has been stored before.
-TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeFuture) {
+// 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(
- (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue());
+ runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
- VerifySessionStartTimePref();
-}
+ EXPECT_FALSE(IsSessionUserActivitySeenPrefSet());
+ EXPECT_FALSE(IsSessionStartTimePrefSet());
+ DestroySessionLengthLimiter();
-// Verifies that the session start time in local state is updated during login
-// if a valid session start time has been stored before.
-TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeValid) {
+ // Pref indicating user activity set. Session start time in the future.
+ SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(
- (session_start_time_ - base::TimeDelta::FromHours(2)).ToInternalValue());
+ runner_->GetCurrentTime() + base::TimeDelta::FromHours(2));
CreateSessionLengthLimiter(false);
- VerifySessionStartTimePref();
+ 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 the session start time in local state is updated during restart
-// after a crash if no session start time has been stored before.
-TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeUnset) {
+// 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);
- VerifySessionStartTimePref();
-}
+ 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();
-// Verifies that the session start time in local state is updated during restart
-// after a crash if a session start time lying in the future has been stored
-// before.
-TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeFuture) {
+ // Pref indicating user activity set. Session start time in the future.
+ SetSessionUserActivitySeenPref(true);
SetSessionStartTimePref(
- (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue());
+ 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);
- VerifySessionStartTimePref();
+ EXPECT_TRUE(IsSessionUserActivitySeenPrefSet());
+ EXPECT_TRUE(GetSessionUserActivitySeenPref());
+ EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref());
+ DestroySessionLengthLimiter();
}
-// Verifies that the session start time in local state is *not* updated during
-// restart after a crash if a valid session start time has been stored before.
-TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeValid) {
- session_start_time_ -= base::TimeDelta::FromHours(2);
- SetSessionStartTimePref(session_start_time_.ToInternalValue());
+// 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);
- VerifySessionStartTimePref();
+ 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, RunWithoutSessionLengthLimit) {
+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));
- // Create a SessionLengthLimiter.
CreateSessionLengthLimiter(false);
+ EXPECT_FALSE(IsSessionStartTimePrefSet());
// Verify that no timer fires to terminate the session.
runner_->FastForwardUntilNoTasksRemain();
}
-// Creates a SessionLengthLimiter after setting a limit. Verifies that the
+// 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.
-TEST_F(SessionLengthLimiterTest, RunWithSessionLengthLimit) {
+// 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(60 * 1000); // 60 seconds.
+ SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
- // Create a SessionLengthLimiter.
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
@@ -312,22 +778,23 @@ TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
// Set a 60 second session time limit.
- SetSessionLengthLimitPref(60 * 1000); // 60 seconds.
+ SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
- // Create a SessionLengthLimiter.
CreateSessionLengthLimiter(false);
// Fast forward the time by 50 seconds, verifying that no timer fires to
// terminate the session.
- runner_->FastForwardBy(50 * 1000); // 50 seconds.
+ runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
// Increase the session length limit to 90 seconds.
- SetSessionLengthLimitPref(90 * 1000); // 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
@@ -339,19 +806,20 @@ TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
// Set a 60 second session time limit.
- SetSessionLengthLimitPref(60 * 1000); // 60 seconds.
+ SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
- // Create a SessionLengthLimiter.
CreateSessionLengthLimiter(false);
// Fast forward the time by 50 seconds, verifying that no timer fires to
// terminate the session.
- runner_->FastForwardBy(50 * 1000); // 50 seconds.
+ 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(40 * 1000); // 40 seconds.
+ 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
@@ -362,17 +830,16 @@ TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) {
base::ThreadTaskRunnerHandle runner_handler(runner_);
// Set a 60 second session time limit.
- SetSessionLengthLimitPref(60 * 1000); // 60 seconds.
+ SetSessionLengthLimitPref(base::TimeDelta::FromSeconds(60));
- // Create a SessionLengthLimiter.
CreateSessionLengthLimiter(false);
// Fast forward the time by 50 seconds, verifying that no timer fires to
// terminate the session.
- runner_->FastForwardBy(50 * 1000); // 50 seconds.
+ runner_->FastForwardBy(base::TimeDelta::FromSeconds(50));
// Remove the session length limit.
- local_state_.RemoveUserPref(prefs::kSessionLengthLimit);
+ ClearSessionLengthLimitPref();
// Verify that no timer fires to terminate the session.
runner_->FastForwardUntilNoTasksRemain();