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/power/session_length_limiter.h" | 5 #include "chrome/browser/chromeos/power/session_length_limiter.h" |
6 | 6 |
7 #include <deque> | 7 #include <queue> |
8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/location.h" | 13 #include "base/location.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
19 #include "base/time.h" | |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "chrome/browser/browser_process.h" | |
22 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
23 #include "chrome/test/base/testing_browser_process.h" | 22 #include "chrome/test/base/testing_browser_process.h" |
24 #include "chrome/test/base/testing_pref_service.h" | 23 #include "chrome/test/base/testing_pref_service.h" |
25 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
27 | 26 |
28 using ::testing::_; | |
29 using ::testing::Invoke; | 27 using ::testing::Invoke; |
30 using ::testing::Mock; | 28 using ::testing::Mock; |
31 using ::testing::NiceMock; | 29 using ::testing::NiceMock; |
32 | 30 |
33 namespace chromeos { | 31 namespace chromeos { |
34 | 32 |
35 namespace { | 33 namespace { |
36 | 34 |
37 // The interval at which the SessionLengthLimiter checks whether the remaining | |
38 // session time has reachzed zero. | |
39 const base::TimeDelta kSessionLengthLimitTimerInterval( | |
40 base::TimeDelta::FromSeconds(1)); | |
41 | |
42 const base::TimeDelta kZeroTimeDelta; | |
43 const base::TimeDelta kTenSeconds(base::TimeDelta::FromSeconds(10)); | |
44 | |
45 class MockSessionLengthLimiterDelegate : public SessionLengthLimiter::Delegate { | 35 class MockSessionLengthLimiterDelegate : public SessionLengthLimiter::Delegate { |
46 public: | 36 public: |
47 MOCK_CONST_METHOD0(GetCurrentTime, const base::Time(void)); | 37 MOCK_CONST_METHOD0(GetCurrentTime, const base::Time(void)); |
48 MOCK_METHOD0(StopSession, void(void)); | 38 MOCK_METHOD0(StopSession, void(void)); |
49 }; | 39 }; |
50 | 40 |
51 // A SingleThreadTaskRunner that allows the task queue to be inspected and | 41 // A SingleThreadTaskRunner that mocks the current time and allows it to be |
52 // delayed tasks to be run without waiting for the actual delays to expire. | 42 // fast-forwarded. |
53 class ImmediateSingleThreadTaskRunner : public base::SingleThreadTaskRunner { | 43 class MockTimeSingleThreadTaskRunner : public base::SingleThreadTaskRunner { |
54 public: | 44 public: |
55 virtual bool RunsTasksOnCurrentThread() const OVERRIDE { | 45 MockTimeSingleThreadTaskRunner(); |
56 return true; | |
57 } | |
58 | 46 |
| 47 // base::SingleThreadTaskRunner: |
| 48 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
59 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 49 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
60 const base::Closure& task, | 50 const base::Closure& task, |
61 base::TimeDelta delay) OVERRIDE { | 51 base::TimeDelta delay) OVERRIDE; |
62 tasks_.push_back(std::pair<base::TimeDelta, base::Closure>(delay, task)); | |
63 return true; | |
64 } | |
65 | |
66 virtual bool PostNonNestableDelayedTask( | 52 virtual bool PostNonNestableDelayedTask( |
67 const tracked_objects::Location& from_here, | 53 const tracked_objects::Location& from_here, |
68 const base::Closure& task, | 54 const base::Closure& task, |
69 base::TimeDelta delay) OVERRIDE { | 55 base::TimeDelta delay) OVERRIDE; |
70 NOTREACHED(); | |
71 return false; | |
72 } | |
73 | 56 |
74 void RunTasks() { | 57 const base::Time& GetCurrentTime() const; |
75 std::deque<std::pair<base::TimeDelta, base::Closure> > tasks; | |
76 tasks.swap(tasks_); | |
77 for (std::deque<std::pair<base::TimeDelta, base::Closure> >::iterator | |
78 it = tasks.begin(); it != tasks.end(); ++it) { | |
79 it->second.Run(); | |
80 } | |
81 } | |
82 | 58 |
83 const std::deque<std::pair<base::TimeDelta, base::Closure> >& tasks() const { | 59 void FastForwardBy(int64 milliseconds); |
84 return tasks_; | 60 void FastForwardUntilNoTasksRemain(); |
85 } | |
86 | 61 |
87 private: | 62 private: |
88 std::deque<std::pair<base::TimeDelta, base::Closure> > tasks_; | 63 // Strict weak temporal ordering of tasks. |
| 64 class TemporalOrder { |
| 65 public: |
| 66 bool operator()( |
| 67 const std::pair<base::Time, base::Closure>& first_task, |
| 68 const std::pair<base::Time, base::Closure>& second_task) const; |
| 69 }; |
89 | 70 |
90 virtual ~ImmediateSingleThreadTaskRunner() {} | 71 virtual ~MockTimeSingleThreadTaskRunner(); |
| 72 |
| 73 base::Time now_; |
| 74 std::priority_queue<std::pair<base::Time, base::Closure>, |
| 75 std::vector<std::pair<base::Time, base::Closure> >, |
| 76 TemporalOrder> tasks_; |
91 }; | 77 }; |
92 | 78 |
93 } // namespace | 79 } // namespace |
94 | 80 |
95 class SessionLengthLimiterTest : public testing::Test { | 81 class SessionLengthLimiterTest : public testing::Test { |
96 protected: | 82 protected: |
97 SessionLengthLimiterTest() : delegate_(NULL) { | 83 SessionLengthLimiterTest(); |
98 } | |
99 | 84 |
100 virtual void SetUp() { | 85 // testing::Test: |
101 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); | 86 virtual void SetUp() OVERRIDE; |
102 SessionLengthLimiter::RegisterPrefs(local_state_.registry()); | 87 virtual void TearDown() OVERRIDE; |
103 | 88 |
104 delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>; | 89 void SetSessionStartTimePref(int64 session_start_time); |
105 ON_CALL(*delegate_, GetCurrentTime()) | 90 void VerifySessionStartTimePref(); |
106 .WillByDefault(Invoke(this, &SessionLengthLimiterTest::GetCurrentTime)); | 91 void SetSessionLengthLimitPref(int64 session_length_limit); |
107 EXPECT_CALL(*delegate_, StopSession()).Times(0); | |
108 runner_ = new ImmediateSingleThreadTaskRunner; | |
109 | 92 |
110 // Initialize the mock clock to a fixed value, ensuring that timezone | 93 void ExpectStopSession(); |
111 // differences or DST changes do not affect the test. | 94 void CheckStopSessionTime(); |
112 now_ = base::Time::UnixEpoch() + base::TimeDelta::FromDays(40 * 365); | |
113 session_start_time_ = now_; | |
114 } | |
115 | 95 |
116 virtual void TearDown() { | 96 void CreateSessionLengthLimiter(bool browser_restarted); |
117 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); | |
118 } | |
119 | |
120 void SetSessionStartTimePref(int64 session_start_time) { | |
121 local_state_.SetUserPref(prefs::kSessionStartTime, | |
122 base::Value::CreateStringValue( | |
123 base::Int64ToString(session_start_time))); | |
124 } | |
125 | |
126 void VerifySessionStartTimePref() { | |
127 base::Time session_start_time(base::Time::FromInternalValue( | |
128 local_state_.GetInt64(prefs::kSessionStartTime))); | |
129 EXPECT_EQ(session_start_time_, session_start_time); | |
130 } | |
131 | |
132 void SetSessionLengthLimitPref(int64 session_length_limit) { | |
133 local_state_.SetUserPref(prefs::kSessionLengthLimit, | |
134 base::Value::CreateIntegerValue( | |
135 session_length_limit)); | |
136 base::TimeDelta remaining( | |
137 base::TimeDelta::FromMilliseconds(session_length_limit) - | |
138 (now_ - session_start_time_)); | |
139 if (remaining < kZeroTimeDelta) | |
140 remaining = kZeroTimeDelta; | |
141 remaining_.reset(new base::TimeDelta(remaining)); | |
142 } | |
143 | |
144 void ExpectStopSession() { | |
145 Mock::VerifyAndClearExpectations(delegate_); | |
146 EXPECT_CALL(*delegate_, StopSession()).Times(1); | |
147 } | |
148 | |
149 void CreateSessionLengthLimiter(bool browser_restarted) { | |
150 session_length_limiter_.reset( | |
151 new SessionLengthLimiter(delegate_, browser_restarted)); | |
152 } | |
153 | |
154 void VerifyNoTimerTickIsEnqueued() { | |
155 EXPECT_TRUE(runner_->tasks().empty()); | |
156 } | |
157 | |
158 void VerifyTimerTickIsEnqueued() { | |
159 ASSERT_EQ(1U, runner_->tasks().size()); | |
160 EXPECT_EQ(kSessionLengthLimitTimerInterval, | |
161 runner_->tasks().front().first); | |
162 } | |
163 | |
164 void VerifyTimerTick() { | |
165 VerifyTimerTickIsEnqueued(); | |
166 runner_->RunTasks(); | |
167 | |
168 now_ += kSessionLengthLimitTimerInterval; | |
169 *remaining_ -= kSessionLengthLimitTimerInterval; | |
170 if (*remaining_ < kZeroTimeDelta) | |
171 remaining_.reset(new base::TimeDelta(kZeroTimeDelta)); | |
172 } | |
173 | |
174 base::Time GetCurrentTime() const { | |
175 return now_; | |
176 } | |
177 | 97 |
178 TestingPrefServiceSimple local_state_; | 98 TestingPrefServiceSimple local_state_; |
| 99 scoped_refptr<MockTimeSingleThreadTaskRunner> runner_; |
| 100 base::Time session_start_time_; |
| 101 base::Time session_end_time_; |
| 102 |
179 MockSessionLengthLimiterDelegate* delegate_; // Owned by | 103 MockSessionLengthLimiterDelegate* delegate_; // Owned by |
180 // session_length_limiter_. | 104 // session_length_limiter_. |
181 scoped_refptr<ImmediateSingleThreadTaskRunner> runner_; | |
182 | |
183 base::Time session_start_time_; | |
184 base::Time now_; | |
185 scoped_ptr<base::TimeDelta> remaining_; | |
186 | |
187 scoped_ptr<SessionLengthLimiter> session_length_limiter_; | 105 scoped_ptr<SessionLengthLimiter> session_length_limiter_; |
188 }; | 106 }; |
189 | 107 |
| 108 MockTimeSingleThreadTaskRunner::MockTimeSingleThreadTaskRunner() |
| 109 // Initialize the mock clock to a fixed value, ensuring that timezone |
| 110 // differences or DST changes do not affect the test. |
| 111 : now_(base::Time::UnixEpoch() + base::TimeDelta::FromDays(40 * 365)) { |
| 112 } |
| 113 |
| 114 bool MockTimeSingleThreadTaskRunner::RunsTasksOnCurrentThread() const { |
| 115 return true; |
| 116 } |
| 117 |
| 118 bool MockTimeSingleThreadTaskRunner::PostDelayedTask( |
| 119 const tracked_objects::Location& from_here, |
| 120 const base::Closure& task, |
| 121 base::TimeDelta delay) { |
| 122 tasks_.push(std::pair<base::Time, base::Closure>(now_ + delay, task)); |
| 123 return true; |
| 124 } |
| 125 |
| 126 bool MockTimeSingleThreadTaskRunner::PostNonNestableDelayedTask( |
| 127 const tracked_objects::Location& from_here, |
| 128 const base::Closure& task, |
| 129 base::TimeDelta delay) { |
| 130 NOTREACHED(); |
| 131 return false; |
| 132 } |
| 133 |
| 134 const base::Time& MockTimeSingleThreadTaskRunner::GetCurrentTime() const { |
| 135 return now_; |
| 136 } |
| 137 |
| 138 void MockTimeSingleThreadTaskRunner::FastForwardBy(int64 delta) { |
| 139 const base::Time latest = now_ + base::TimeDelta::FromMilliseconds(delta); |
| 140 while (!tasks_.empty() && tasks_.top().first <= latest) { |
| 141 now_ = tasks_.top().first; |
| 142 tasks_.top().second.Run(); |
| 143 tasks_.pop(); |
| 144 } |
| 145 now_ = latest; |
| 146 } |
| 147 |
| 148 void MockTimeSingleThreadTaskRunner::FastForwardUntilNoTasksRemain() { |
| 149 while (!tasks_.empty()) { |
| 150 now_ = tasks_.top().first; |
| 151 tasks_.top().second.Run(); |
| 152 tasks_.pop(); |
| 153 } |
| 154 } |
| 155 |
| 156 bool MockTimeSingleThreadTaskRunner::TemporalOrder::operator()( |
| 157 const std::pair<base::Time, base::Closure>& first_task, |
| 158 const std::pair<base::Time, base::Closure>& second_task) const { |
| 159 return first_task.first < second_task.first; |
| 160 } |
| 161 |
| 162 MockTimeSingleThreadTaskRunner::~MockTimeSingleThreadTaskRunner() { |
| 163 } |
| 164 |
| 165 SessionLengthLimiterTest::SessionLengthLimiterTest() : delegate_(NULL) { |
| 166 } |
| 167 |
| 168 void SessionLengthLimiterTest::SetUp() { |
| 169 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); |
| 170 SessionLengthLimiter::RegisterPrefs(local_state_.registry()); |
| 171 runner_ = new MockTimeSingleThreadTaskRunner; |
| 172 session_start_time_ = runner_->GetCurrentTime(); |
| 173 |
| 174 delegate_ = new NiceMock<MockSessionLengthLimiterDelegate>; |
| 175 ON_CALL(*delegate_, GetCurrentTime()) |
| 176 .WillByDefault(Invoke(runner_.get(), |
| 177 &MockTimeSingleThreadTaskRunner::GetCurrentTime)); |
| 178 EXPECT_CALL(*delegate_, StopSession()).Times(0); |
| 179 } |
| 180 |
| 181 void SessionLengthLimiterTest::TearDown() { |
| 182 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); |
| 183 } |
| 184 |
| 185 void SessionLengthLimiterTest::SetSessionStartTimePref( |
| 186 int64 session_start_time) { |
| 187 local_state_.SetUserPref(prefs::kSessionStartTime, |
| 188 base::Value::CreateStringValue( |
| 189 base::Int64ToString(session_start_time))); |
| 190 } |
| 191 |
| 192 void SessionLengthLimiterTest::VerifySessionStartTimePref() { |
| 193 base::Time session_start_time(base::Time::FromInternalValue( |
| 194 local_state_.GetInt64(prefs::kSessionStartTime))); |
| 195 EXPECT_EQ(session_start_time_, session_start_time); |
| 196 } |
| 197 |
| 198 void SessionLengthLimiterTest::SetSessionLengthLimitPref( |
| 199 int64 session_length_limit) { |
| 200 session_end_time_ = session_start_time_ + |
| 201 base::TimeDelta::FromMilliseconds(session_length_limit); |
| 202 // If the new session end time has passed already, the session should end now. |
| 203 if (session_end_time_ < runner_->GetCurrentTime()) |
| 204 session_end_time_ = runner_->GetCurrentTime(); |
| 205 local_state_.SetUserPref(prefs::kSessionLengthLimit, |
| 206 base::Value::CreateIntegerValue( |
| 207 session_length_limit)); |
| 208 } |
| 209 |
| 210 void SessionLengthLimiterTest::ExpectStopSession() { |
| 211 Mock::VerifyAndClearExpectations(delegate_); |
| 212 EXPECT_CALL(*delegate_, StopSession()) |
| 213 .Times(1) |
| 214 .WillOnce(Invoke(this, &SessionLengthLimiterTest::CheckStopSessionTime)); |
| 215 } |
| 216 |
| 217 void SessionLengthLimiterTest::CheckStopSessionTime() { |
| 218 EXPECT_EQ(session_end_time_, runner_->GetCurrentTime()); |
| 219 } |
| 220 |
| 221 void SessionLengthLimiterTest::CreateSessionLengthLimiter( |
| 222 bool browser_restarted) { |
| 223 session_length_limiter_.reset( |
| 224 new SessionLengthLimiter(delegate_, browser_restarted)); |
| 225 } |
190 // Verifies that the session start time in local state is updated during login | 226 // Verifies that the session start time in local state is updated during login |
191 // if no session start time has been stored before. | 227 // if no session start time has been stored before. |
192 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeUnset) { | 228 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeUnset) { |
193 CreateSessionLengthLimiter(false); | 229 CreateSessionLengthLimiter(false); |
194 VerifySessionStartTimePref(); | 230 VerifySessionStartTimePref(); |
195 } | 231 } |
196 | 232 |
197 // Verifies that the session start time in local state is updated during login | 233 // Verifies that the session start time in local state is updated during login |
198 // if an invalid session start time has been stored before. | 234 // if an invalid session start time has been stored before. |
199 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeInvalid) { | 235 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeInvalid) { |
200 SetSessionStartTimePref(0); | 236 SetSessionStartTimePref(0); |
201 CreateSessionLengthLimiter(false); | 237 CreateSessionLengthLimiter(false); |
202 VerifySessionStartTimePref(); | 238 VerifySessionStartTimePref(); |
203 } | 239 } |
204 | 240 |
205 // Verifies that the session start time in local state is updated during login | 241 // Verifies that the session start time in local state is updated during login |
206 // if a session start time lying in the future has been stored before. | 242 // if a session start time lying in the future has been stored before. |
207 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeFuture) { | 243 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeFuture) { |
208 SetSessionStartTimePref( | 244 SetSessionStartTimePref( |
209 (now_ + base::TimeDelta::FromHours(2)).ToInternalValue()); | 245 (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue()); |
210 CreateSessionLengthLimiter(false); | 246 CreateSessionLengthLimiter(false); |
211 VerifySessionStartTimePref(); | 247 VerifySessionStartTimePref(); |
212 } | 248 } |
213 | 249 |
214 // Verifies that the session start time in local state is updated during login | 250 // Verifies that the session start time in local state is updated during login |
215 // if a valid session start time has been stored before. | 251 // if a valid session start time has been stored before. |
216 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeValid) { | 252 TEST_F(SessionLengthLimiterTest, StartWithSessionStartTimeValid) { |
217 const base::Time previous_start_time = now_ - base::TimeDelta::FromHours(2); | 253 SetSessionStartTimePref( |
218 SetSessionStartTimePref(previous_start_time.ToInternalValue()); | 254 (session_start_time_ - base::TimeDelta::FromHours(2)).ToInternalValue()); |
219 CreateSessionLengthLimiter(false); | 255 CreateSessionLengthLimiter(false); |
220 VerifySessionStartTimePref(); | 256 VerifySessionStartTimePref(); |
221 } | 257 } |
222 | 258 |
223 // Verifies that the session start time in local state is updated during restart | 259 // Verifies that the session start time in local state is updated during restart |
224 // after a crash if no session start time has been stored before. | 260 // after a crash if no session start time has been stored before. |
225 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeUnset) { | 261 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeUnset) { |
226 CreateSessionLengthLimiter(true); | 262 CreateSessionLengthLimiter(true); |
227 VerifySessionStartTimePref(); | 263 VerifySessionStartTimePref(); |
228 } | 264 } |
229 | 265 |
230 // Verifies that the session start time in local state is updated during restart | 266 // Verifies that the session start time in local state is updated during restart |
231 // after a crash if an invalid session start time has been stored before. | 267 // after a crash if an invalid session start time has been stored before. |
232 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeInvalid) { | 268 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeInvalid) { |
233 SetSessionStartTimePref(0); | 269 SetSessionStartTimePref(0); |
234 CreateSessionLengthLimiter(true); | 270 CreateSessionLengthLimiter(true); |
235 VerifySessionStartTimePref(); | 271 VerifySessionStartTimePref(); |
236 } | 272 } |
237 | 273 |
238 // Verifies that the session start time in local state is updated during restart | 274 // Verifies that the session start time in local state is updated during restart |
239 // after a crash if a session start time lying in the future has been stored | 275 // after a crash if a session start time lying in the future has been stored |
240 // before. | 276 // before. |
241 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeFuture) { | 277 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeFuture) { |
242 SetSessionStartTimePref( | 278 SetSessionStartTimePref( |
243 (now_ + base::TimeDelta::FromHours(2)).ToInternalValue()); | 279 (session_start_time_ + base::TimeDelta::FromHours(2)).ToInternalValue()); |
244 CreateSessionLengthLimiter(true); | 280 CreateSessionLengthLimiter(true); |
245 VerifySessionStartTimePref(); | 281 VerifySessionStartTimePref(); |
246 } | 282 } |
247 | 283 |
248 // Verifies that the session start time in local state is *not* updated during | 284 // Verifies that the session start time in local state is *not* updated during |
249 // restart after a crash if a valid session start time has been stored before. | 285 // restart after a crash if a valid session start time has been stored before. |
250 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeValid) { | 286 TEST_F(SessionLengthLimiterTest, RestartWithSessionStartTimeValid) { |
251 session_start_time_ -= base::TimeDelta::FromHours(2); | 287 session_start_time_ -= base::TimeDelta::FromHours(2); |
252 SetSessionStartTimePref(session_start_time_.ToInternalValue()); | 288 SetSessionStartTimePref(session_start_time_.ToInternalValue()); |
253 CreateSessionLengthLimiter(true); | 289 CreateSessionLengthLimiter(true); |
254 VerifySessionStartTimePref(); | 290 VerifySessionStartTimePref(); |
255 } | 291 } |
256 | 292 |
257 // Creates a SessionLengthLimiter without setting a limit. Verifies that the | 293 // Creates a SessionLengthLimiter without setting a limit. Verifies that the |
258 // limiter does not start a timer. | 294 // limiter does not start a timer. |
259 TEST_F(SessionLengthLimiterTest, RunWithoutSessionLengthLimit) { | 295 TEST_F(SessionLengthLimiterTest, RunWithoutSessionLengthLimit) { |
260 base::ThreadTaskRunnerHandle runner_handler(runner_); | 296 base::ThreadTaskRunnerHandle runner_handler(runner_); |
261 | 297 |
262 // Create a SessionLengthLimiter. | 298 // Create a SessionLengthLimiter. |
263 CreateSessionLengthLimiter(false); | 299 CreateSessionLengthLimiter(false); |
264 | 300 |
265 // Verify that no timer tick has been enqueued. | 301 // Verify that no timer fires to terminate the session. |
266 VerifyNoTimerTickIsEnqueued(); | 302 runner_->FastForwardUntilNoTasksRemain(); |
267 } | 303 } |
268 | 304 |
269 // Creates a SessionLengthLimiter after setting a limit. Verifies that the | 305 // Creates a SessionLengthLimiter after setting a limit. Verifies that the |
270 // limiter starts a timer and that when the session length reaches the limit, | 306 // limiter starts a timer and that when the session length reaches the limit, |
271 // the session is terminated. | 307 // the session is terminated. |
272 TEST_F(SessionLengthLimiterTest, RunWithSessionLengthLimit) { | 308 TEST_F(SessionLengthLimiterTest, RunWithSessionLengthLimit) { |
273 base::ThreadTaskRunnerHandle runner_handler(runner_); | 309 base::ThreadTaskRunnerHandle runner_handler(runner_); |
274 | 310 |
275 // Set a 60 second session time limit. | 311 // Set a 60 second session time limit. |
276 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. | 312 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. |
277 | 313 |
278 // Create a SessionLengthLimiter. | 314 // Create a SessionLengthLimiter. |
279 CreateSessionLengthLimiter(false); | 315 CreateSessionLengthLimiter(false); |
280 | 316 |
281 // Check timer ticks until the remaining session time reaches zero. | 317 // Verify that the timer fires and the session is terminated when the session |
282 while (*remaining_ > kZeroTimeDelta) | 318 // length limit is reached. |
283 VerifyTimerTick(); | |
284 | |
285 // Check that the next timer tick leads to the session being terminated. | |
286 ExpectStopSession(); | 319 ExpectStopSession(); |
287 VerifyTimerTick(); | 320 runner_->FastForwardUntilNoTasksRemain(); |
288 } | 321 } |
289 | 322 |
290 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 | 323 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 |
291 // seconds of session time to pass, then increases the limit to 90 seconds. | 324 // seconds of session time to pass, then increases the limit to 90 seconds. |
292 // Verifies that when the session time reaches the new 90 second limit, the | 325 // Verifies that when the session time reaches the new 90 second limit, the |
293 // session is terminated. | 326 // session is terminated. |
294 TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) { | 327 TEST_F(SessionLengthLimiterTest, RunAndIncreaseSessionLengthLimit) { |
295 base::ThreadTaskRunnerHandle runner_handler(runner_); | 328 base::ThreadTaskRunnerHandle runner_handler(runner_); |
296 | 329 |
297 // Set a 60 second session time limit. | 330 // Set a 60 second session time limit. |
298 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. | 331 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. |
299 | 332 |
300 // Create a SessionLengthLimiter. | 333 // Create a SessionLengthLimiter. |
301 CreateSessionLengthLimiter(false); | 334 CreateSessionLengthLimiter(false); |
302 | 335 |
303 // Check timer ticks for 50 seconds of session time. | 336 // Fast forward the time by 50 seconds, verifying that no timer fires to |
304 while (*remaining_ > kTenSeconds) | 337 // terminate the session. |
305 VerifyTimerTick(); | 338 runner_->FastForwardBy(50 * 1000); // 50 seconds. |
306 | 339 |
307 // Increase the session length limit to 90 seconds. | 340 // Increase the session length limit to 90 seconds. |
308 SetSessionLengthLimitPref(90 * 1000); // 90 seconds. | 341 SetSessionLengthLimitPref(90 * 1000); // 90 seconds. |
309 | 342 |
310 // Check timer ticks until the remaining session time reaches zero. | 343 // Verify that the the timer fires and the session is terminated when the |
311 while (*remaining_ > kZeroTimeDelta) | 344 // session length limit is reached. |
312 VerifyTimerTick(); | |
313 | |
314 // Check that the next timer tick leads to the session being terminated. | |
315 ExpectStopSession(); | 345 ExpectStopSession(); |
316 VerifyTimerTick(); | 346 runner_->FastForwardUntilNoTasksRemain(); |
317 } | 347 } |
318 | 348 |
319 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 | 349 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 |
320 // seconds of session time to pass, then decreases the limit to 40 seconds. | 350 // seconds of session time to pass, then decreases the limit to 40 seconds. |
321 // Verifies that when the limit is decreased to 40 seconds after 50 seconds of | 351 // Verifies that when the limit is decreased to 40 seconds after 50 seconds of |
322 // session time have passed, the next timer tick causes the session to be | 352 // session time have passed, the next timer tick causes the session to be |
323 // terminated. | 353 // terminated. |
324 TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) { | 354 TEST_F(SessionLengthLimiterTest, RunAndDecreaseSessionLengthLimit) { |
325 base::ThreadTaskRunnerHandle runner_handler(runner_); | 355 base::ThreadTaskRunnerHandle runner_handler(runner_); |
326 | 356 |
327 // Set a 60 second session time limit. | 357 // Set a 60 second session time limit. |
328 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. | 358 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. |
329 | 359 |
330 // Create a SessionLengthLimiter. | 360 // Create a SessionLengthLimiter. |
331 CreateSessionLengthLimiter(false); | 361 CreateSessionLengthLimiter(false); |
332 | 362 |
333 // Check timer ticks for 50 seconds of session time. | 363 // Fast forward the time by 50 seconds, verifying that no timer fires to |
334 while (*remaining_ > kTenSeconds) | 364 // terminate the session. |
335 VerifyTimerTick(); | 365 runner_->FastForwardBy(50 * 1000); // 50 seconds. |
336 | 366 |
337 // Reduce the session length limit below the 50 seconds that have already | 367 // Verify that reducing the session length limit below the 50 seconds that |
338 // elapsed. | 368 // have already elapsed causes the session to be terminated immediately. |
| 369 ExpectStopSession(); |
339 SetSessionLengthLimitPref(40 * 1000); // 40 seconds. | 370 SetSessionLengthLimitPref(40 * 1000); // 40 seconds. |
340 | |
341 // Check that the next timer tick causes the session to be terminated. | |
342 ExpectStopSession(); | |
343 VerifyTimerTick(); | |
344 } | 371 } |
345 | 372 |
346 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 | 373 // Creates a SessionLengthLimiter after setting a 60 second limit, allows 50 |
347 // seconds of session time to pass, then removes the limit. Verifies that after | 374 // seconds of session time to pass, then removes the limit. Verifies that after |
348 // the limit is removed, the session is not terminated when the session time | 375 // the limit is removed, the session is not terminated when the session time |
349 // reaches the original 60 second limit. | 376 // reaches the original 60 second limit. |
350 TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) { | 377 TEST_F(SessionLengthLimiterTest, RunAndRemoveSessionLengthLimit) { |
351 base::ThreadTaskRunnerHandle runner_handler(runner_); | 378 base::ThreadTaskRunnerHandle runner_handler(runner_); |
352 | 379 |
353 // Set a 60 second session time limit. | 380 // Set a 60 second session time limit. |
354 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. | 381 SetSessionLengthLimitPref(60 * 1000); // 60 seconds. |
355 | 382 |
356 // Create a SessionLengthLimiter. | 383 // Create a SessionLengthLimiter. |
357 CreateSessionLengthLimiter(false); | 384 CreateSessionLengthLimiter(false); |
358 | 385 |
359 // Check timer ticks for 50 seconds of session time. | 386 // Fast forward the time by 50 seconds, verifying that no timer fires to |
360 while (*remaining_ > kTenSeconds) | 387 // terminate the session. |
361 VerifyTimerTick(); | 388 runner_->FastForwardBy(50 * 1000); // 50 seconds. |
362 | 389 |
363 // Remove the session length limit. | 390 // Remove the session length limit. |
364 local_state_.RemoveUserPref(prefs::kSessionLengthLimit); | 391 local_state_.RemoveUserPref(prefs::kSessionLengthLimit); |
365 | 392 |
366 // Continue advancing the session time until it reaches the original 60 second | 393 // Verify that no timer fires to terminate the session. |
367 // limit. | 394 runner_->FastForwardUntilNoTasksRemain(); |
368 while (*remaining_ > kZeroTimeDelta) { | |
369 runner_->RunTasks(); | |
370 | |
371 now_ += kSessionLengthLimitTimerInterval; | |
372 *remaining_ -= kSessionLengthLimitTimerInterval; | |
373 if (*remaining_ < kZeroTimeDelta) | |
374 remaining_.reset(new base::TimeDelta(kZeroTimeDelta)); | |
375 } | |
376 | |
377 // Check that the next timer tick does not lead to the session being | |
378 // terminated. | |
379 now_ += kSessionLengthLimitTimerInterval; | |
380 runner_->RunTasks(); | |
381 } | 395 } |
382 | 396 |
383 } // namespace chromeos | 397 } // namespace chromeos |
OLD | NEW |