| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/session_length_limiter.h" | 5 #include "chrome/browser/chromeos/session_length_limiter.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/prefs/testing_pref_service.h" | 10 #include "base/prefs/testing_pref_service.h" |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 if (session_length_limiter_) | 165 if (session_length_limiter_) |
| 166 session_length_limiter_->OnUserActivity(NULL); | 166 session_length_limiter_->OnUserActivity(NULL); |
| 167 UpdateSessionStartTimeIfWaitingForUserActivity(); | 167 UpdateSessionStartTimeIfWaitingForUserActivity(); |
| 168 user_activity_seen_ = true; | 168 user_activity_seen_ = true; |
| 169 } | 169 } |
| 170 | 170 |
| 171 void SessionLengthLimiterTest:: | 171 void SessionLengthLimiterTest:: |
| 172 UpdateSessionStartTimeIfWaitingForUserActivity() { | 172 UpdateSessionStartTimeIfWaitingForUserActivity() { |
| 173 if (!user_activity_seen_ && | 173 if (!user_activity_seen_ && |
| 174 local_state_.GetBoolean(prefs::kSessionWaitForInitialUserActivity)) { | 174 local_state_.GetBoolean(prefs::kSessionWaitForInitialUserActivity)) { |
| 175 session_start_time_ = runner_->GetCurrentMockTime(); | 175 session_start_time_ = runner_->NowTicks(); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 void SessionLengthLimiterTest::ExpectStopSession() { | 179 void SessionLengthLimiterTest::ExpectStopSession() { |
| 180 Mock::VerifyAndClearExpectations(delegate_); | 180 Mock::VerifyAndClearExpectations(delegate_); |
| 181 EXPECT_CALL(*delegate_, StopSession()) | 181 EXPECT_CALL(*delegate_, StopSession()) |
| 182 .Times(1) | 182 .Times(1) |
| 183 .WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime)); | 183 .WillOnce(Invoke(this, &SessionLengthLimiterTest::SaveSessionStopTime)); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void SessionLengthLimiterTest::SaveSessionStopTime() { | 186 void SessionLengthLimiterTest::SaveSessionStopTime() { |
| 187 session_stop_time_ = runner_->GetCurrentMockTime(); | 187 session_stop_time_ = runner_->NowTicks(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 void SessionLengthLimiterTest::CreateSessionLengthLimiter( | 190 void SessionLengthLimiterTest::CreateSessionLengthLimiter( |
| 191 bool browser_restarted) { | 191 bool browser_restarted) { |
| 192 user_activity_seen_ = false; | 192 user_activity_seen_ = false; |
| 193 session_start_time_ = runner_->GetCurrentMockTime(); | 193 session_start_time_ = runner_->NowTicks(); |
| 194 | 194 |
| 195 EXPECT_FALSE(delegate_); | 195 EXPECT_FALSE(delegate_); |
| 196 delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>; | 196 delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>; |
| 197 ON_CALL(*delegate_, GetCurrentTime()) | 197 ON_CALL(*delegate_, GetCurrentTime()) |
| 198 .WillByDefault(Invoke( | 198 .WillByDefault( |
| 199 runner_.get(), &base::TestMockTimeTaskRunner::GetCurrentMockTime)); | 199 Invoke(runner_.get(), &base::TestMockTimeTaskRunner::NowTicks)); |
| 200 EXPECT_CALL(*delegate_, StopSession()).Times(0); | 200 EXPECT_CALL(*delegate_, StopSession()).Times(0); |
| 201 session_length_limiter_.reset( | 201 session_length_limiter_.reset( |
| 202 new SessionLengthLimiter(delegate_, browser_restarted)); | 202 new SessionLengthLimiter(delegate_, browser_restarted)); |
| 203 } | 203 } |
| 204 | 204 |
| 205 void SessionLengthLimiterTest::DestroySessionLengthLimiter() { | 205 void SessionLengthLimiterTest::DestroySessionLengthLimiter() { |
| 206 session_length_limiter_.reset(); | 206 session_length_limiter_.reset(); |
| 207 delegate_ = NULL; | 207 delegate_ = NULL; |
| 208 } | 208 } |
| 209 | 209 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 // Pref indicating user activity set. Session start time not set. | 277 // Pref indicating user activity set. Session start time not set. |
| 278 SetSessionUserActivitySeenPref(true); | 278 SetSessionUserActivitySeenPref(true); |
| 279 ClearSessionStartTimePref(); | 279 ClearSessionStartTimePref(); |
| 280 CreateSessionLengthLimiter(false); | 280 CreateSessionLengthLimiter(false); |
| 281 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 281 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 282 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 282 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 283 DestroySessionLengthLimiter(); | 283 DestroySessionLengthLimiter(); |
| 284 | 284 |
| 285 // Pref indicating user activity not set. Session start time in the future. | 285 // Pref indicating user activity not set. Session start time in the future. |
| 286 ClearSessionUserActivitySeenPref(); | 286 ClearSessionUserActivitySeenPref(); |
| 287 SetSessionStartTimePref( | 287 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2)); |
| 288 runner_->GetCurrentMockTime() + base::TimeDelta::FromHours(2)); | |
| 289 CreateSessionLengthLimiter(false); | 288 CreateSessionLengthLimiter(false); |
| 290 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 289 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 291 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 290 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 292 DestroySessionLengthLimiter(); | 291 DestroySessionLengthLimiter(); |
| 293 | 292 |
| 294 // Pref indicating user activity set. Session start time in the future. | 293 // Pref indicating user activity set. Session start time in the future. |
| 295 SetSessionUserActivitySeenPref(true); | 294 SetSessionUserActivitySeenPref(true); |
| 296 SetSessionStartTimePref( | 295 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2)); |
| 297 runner_->GetCurrentMockTime() + base::TimeDelta::FromHours(2)); | |
| 298 CreateSessionLengthLimiter(false); | 296 CreateSessionLengthLimiter(false); |
| 299 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 297 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 300 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 298 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 301 DestroySessionLengthLimiter(); | 299 DestroySessionLengthLimiter(); |
| 302 | 300 |
| 303 // Pref indicating user activity not set. Session start time valid. | 301 // Pref indicating user activity not set. Session start time valid. |
| 304 ClearSessionUserActivitySeenPref(); | 302 ClearSessionUserActivitySeenPref(); |
| 305 SetSessionStartTimePref( | 303 SetSessionStartTimePref(runner_->NowTicks() - base::TimeDelta::FromHours(2)); |
| 306 runner_->GetCurrentMockTime() - base::TimeDelta::FromHours(2)); | |
| 307 CreateSessionLengthLimiter(false); | 304 CreateSessionLengthLimiter(false); |
| 308 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 305 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 309 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 306 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 310 DestroySessionLengthLimiter(); | 307 DestroySessionLengthLimiter(); |
| 311 | 308 |
| 312 // Pref indicating user activity set. Session start time valid. | 309 // Pref indicating user activity set. Session start time valid. |
| 313 SetSessionUserActivitySeenPref(true); | 310 SetSessionUserActivitySeenPref(true); |
| 314 SetSessionStartTimePref( | 311 SetSessionStartTimePref(runner_->NowTicks() - base::TimeDelta::FromHours(2)); |
| 315 runner_->GetCurrentMockTime() - base::TimeDelta::FromHours(2)); | |
| 316 CreateSessionLengthLimiter(false); | 312 CreateSessionLengthLimiter(false); |
| 317 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 313 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 318 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 314 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 319 DestroySessionLengthLimiter(); | 315 DestroySessionLengthLimiter(); |
| 320 } | 316 } |
| 321 | 317 |
| 322 // Verifies that when not instructed to wait for initial user activity, local | 318 // Verifies that when not instructed to wait for initial user activity, local |
| 323 // state is correctly updated during restart after a crash: | 319 // state is correctly updated during restart after a crash: |
| 324 // * If no valid session start time is found in local state, the session start | 320 // * If no valid session start time is found in local state, the session start |
| 325 // time is set and the pref indicating user activity is cleared. | 321 // time is set and the pref indicating user activity is cleared. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 337 // Pref indicating user activity set. Session start time not set. | 333 // Pref indicating user activity set. Session start time not set. |
| 338 SetSessionUserActivitySeenPref(true); | 334 SetSessionUserActivitySeenPref(true); |
| 339 ClearSessionStartTimePref(); | 335 ClearSessionStartTimePref(); |
| 340 CreateSessionLengthLimiter(true); | 336 CreateSessionLengthLimiter(true); |
| 341 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 337 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 342 EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); | 338 EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); |
| 343 DestroySessionLengthLimiter(); | 339 DestroySessionLengthLimiter(); |
| 344 | 340 |
| 345 // Pref indicating user activity not set. Session start time in the future. | 341 // Pref indicating user activity not set. Session start time in the future. |
| 346 ClearSessionUserActivitySeenPref(); | 342 ClearSessionUserActivitySeenPref(); |
| 347 SetSessionStartTimePref( | 343 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2)); |
| 348 runner_->GetCurrentMockTime() + base::TimeDelta::FromHours(2)); | |
| 349 CreateSessionLengthLimiter(true); | 344 CreateSessionLengthLimiter(true); |
| 350 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 345 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 351 EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); | 346 EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); |
| 352 DestroySessionLengthLimiter(); | 347 DestroySessionLengthLimiter(); |
| 353 | 348 |
| 354 // Pref indicating user activity set. Session start time in the future. | 349 // Pref indicating user activity set. Session start time in the future. |
| 355 SetSessionUserActivitySeenPref(true); | 350 SetSessionUserActivitySeenPref(true); |
| 356 SetSessionStartTimePref( | 351 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2)); |
| 357 runner_->GetCurrentMockTime() + base::TimeDelta::FromHours(2)); | |
| 358 CreateSessionLengthLimiter(true); | 352 CreateSessionLengthLimiter(true); |
| 359 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 353 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 360 EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); | 354 EXPECT_EQ(session_start_time_, GetSessionStartTimePref()); |
| 361 DestroySessionLengthLimiter(); | 355 DestroySessionLengthLimiter(); |
| 362 | 356 |
| 363 const base::TimeTicks stored_session_start_time = | 357 const base::TimeTicks stored_session_start_time = |
| 364 runner_->GetCurrentMockTime() - base::TimeDelta::FromHours(2); | 358 runner_->NowTicks() - base::TimeDelta::FromHours(2); |
| 365 | 359 |
| 366 // Pref indicating user activity not set. Session start time valid. | 360 // Pref indicating user activity not set. Session start time valid. |
| 367 ClearSessionUserActivitySeenPref(); | 361 ClearSessionUserActivitySeenPref(); |
| 368 SetSessionStartTimePref(stored_session_start_time); | 362 SetSessionStartTimePref(stored_session_start_time); |
| 369 CreateSessionLengthLimiter(true); | 363 CreateSessionLengthLimiter(true); |
| 370 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 364 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 371 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); | 365 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); |
| 372 DestroySessionLengthLimiter(); | 366 DestroySessionLengthLimiter(); |
| 373 | 367 |
| 374 // Pref indicating user activity set. Session start time valid. | 368 // Pref indicating user activity set. Session start time valid. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 401 // Pref indicating user activity set. Session start time not set. | 395 // Pref indicating user activity set. Session start time not set. |
| 402 SetSessionUserActivitySeenPref(true); | 396 SetSessionUserActivitySeenPref(true); |
| 403 ClearSessionStartTimePref(); | 397 ClearSessionStartTimePref(); |
| 404 CreateSessionLengthLimiter(true); | 398 CreateSessionLengthLimiter(true); |
| 405 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 399 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 406 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 400 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 407 DestroySessionLengthLimiter(); | 401 DestroySessionLengthLimiter(); |
| 408 | 402 |
| 409 // Pref indicating user activity not set. Session start time in the future. | 403 // Pref indicating user activity not set. Session start time in the future. |
| 410 ClearSessionUserActivitySeenPref(); | 404 ClearSessionUserActivitySeenPref(); |
| 411 SetSessionStartTimePref( | 405 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2)); |
| 412 runner_->GetCurrentMockTime() + base::TimeDelta::FromHours(2)); | |
| 413 CreateSessionLengthLimiter(true); | 406 CreateSessionLengthLimiter(true); |
| 414 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 407 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 415 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 408 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 416 DestroySessionLengthLimiter(); | 409 DestroySessionLengthLimiter(); |
| 417 | 410 |
| 418 // Pref indicating user activity set. Session start time in the future. | 411 // Pref indicating user activity set. Session start time in the future. |
| 419 SetSessionUserActivitySeenPref(true); | 412 SetSessionUserActivitySeenPref(true); |
| 420 SetSessionStartTimePref( | 413 SetSessionStartTimePref(runner_->NowTicks() + base::TimeDelta::FromHours(2)); |
| 421 runner_->GetCurrentMockTime() + base::TimeDelta::FromHours(2)); | |
| 422 CreateSessionLengthLimiter(true); | 414 CreateSessionLengthLimiter(true); |
| 423 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 415 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 424 EXPECT_FALSE(IsSessionStartTimePrefSet()); | 416 EXPECT_FALSE(IsSessionStartTimePrefSet()); |
| 425 DestroySessionLengthLimiter(); | 417 DestroySessionLengthLimiter(); |
| 426 | 418 |
| 427 const base::TimeTicks stored_session_start_time = | 419 const base::TimeTicks stored_session_start_time = |
| 428 runner_->GetCurrentMockTime() - base::TimeDelta::FromHours(2); | 420 runner_->NowTicks() - base::TimeDelta::FromHours(2); |
| 429 | 421 |
| 430 // Pref indicating user activity not set. Session start time valid. | 422 // Pref indicating user activity not set. Session start time valid. |
| 431 ClearSessionUserActivitySeenPref(); | 423 ClearSessionUserActivitySeenPref(); |
| 432 SetSessionStartTimePref(stored_session_start_time); | 424 SetSessionStartTimePref(stored_session_start_time); |
| 433 CreateSessionLengthLimiter(true); | 425 CreateSessionLengthLimiter(true); |
| 434 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); | 426 EXPECT_FALSE(IsSessionUserActivitySeenPrefSet()); |
| 435 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); | 427 EXPECT_EQ(stored_session_start_time, GetSessionStartTimePref()); |
| 436 DestroySessionLengthLimiter(); | 428 DestroySessionLengthLimiter(); |
| 437 | 429 |
| 438 // Pref indicating user activity set. Session start time valid. | 430 // Pref indicating user activity set. Session start time valid. |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 739 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50)); | 731 runner_->FastForwardBy(base::TimeDelta::FromSeconds(50)); |
| 740 | 732 |
| 741 // Remove the session length limit. | 733 // Remove the session length limit. |
| 742 ClearSessionLengthLimitPref(); | 734 ClearSessionLengthLimitPref(); |
| 743 | 735 |
| 744 // Verify that no timer fires to terminate the session. | 736 // Verify that no timer fires to terminate the session. |
| 745 runner_->FastForwardUntilNoTasksRemain(); | 737 runner_->FastForwardUntilNoTasksRemain(); |
| 746 } | 738 } |
| 747 | 739 |
| 748 } // namespace chromeos | 740 } // namespace chromeos |
| OLD | NEW |