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