Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(637)

Side by Side Diff: chrome/browser/policy/device_status_collector_unittest.cc

Issue 9348105: Aggregate device activity, and report per-day activity in device status reports. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add a few more unit tests. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/policy/device_status_collector.h" 5 #include "chrome/browser/policy/device_status_collector.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/time.h" 8 #include "base/time.h"
9 #include "chrome/browser/idle.h" 9 #include "chrome/browser/idle.h"
10 #include "chrome/browser/chromeos/cros_settings.h" 10 #include "chrome/browser/chromeos/cros_settings.h"
(...skipping 14 matching lines...) Expand all
25 namespace em = enterprise_management; 25 namespace em = enterprise_management;
26 26
27 namespace { 27 namespace {
28 28
29 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { 29 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector {
30 public: 30 public:
31 TestingDeviceStatusCollector( 31 TestingDeviceStatusCollector(
32 PrefService* local_state, 32 PrefService* local_state,
33 chromeos::system::StatisticsProvider* provider) 33 chromeos::system::StatisticsProvider* provider)
34 : policy::DeviceStatusCollector(local_state, provider), 34 : policy::DeviceStatusCollector(local_state, provider),
35 local_state_(local_state), 35 local_state_(local_state) {
36 baseline_time_(Time::Now()) { 36 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness
37 // due to a single activity period spanning two days.
38 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1));
37 } 39 }
38 40
39 void Simulate(IdleState* states, int len) { 41 void Simulate(IdleState* states, int len) {
40 for (int i = 0; i < len; i++) 42 for (int i = 0; i < len; i++)
41 IdleStateCallback(states[i]); 43 IdleStateCallback(states[i]);
42 } 44 }
43 45
44 void SimulateWithSleep(IdleState* states, int len, int ) { 46 void set_max_stored_past_activity_days(unsigned int value) {
45 for (int i = 0; i < len; i++) 47 max_stored_past_activity_days_ = value;
46 IdleStateCallback(states[i]);
47 } 48 }
48 49
49 void set_max_stored_active_periods(unsigned int value) { 50 void set_max_stored_future_activity_days(unsigned int value) {
50 max_stored_active_periods_ = value; 51 max_stored_future_activity_days_ = value;
52 }
53
54 // Reset the baseline time.
55 void SetBaselineTime(Time time) {
56 baseline_time_ = time;
57 baseline_offset_periods_ = 0;
51 } 58 }
52 59
53 protected: 60 protected:
54 virtual void CheckIdleState() OVERRIDE { 61 virtual void CheckIdleState() OVERRIDE {
55 // This should never be called in testing, as it results in a dbus call. 62 // This should never be called in testing, as it results in a dbus call.
56 NOTREACHED(); 63 NOTREACHED();
57 } 64 }
58 65
59 // Each time this is called, returns a time that is a fixed increment 66 // Each time this is called, returns a time that is a fixed increment
60 // later than the previous time. 67 // later than the previous time.
61 virtual Time GetCurrentTime() OVERRIDE { 68 virtual Time GetCurrentTime() OVERRIDE {
62 static int call_count = 0; 69 int poll_interval = policy::DeviceStatusCollector::kPollIntervalSeconds;
63 return baseline_time_ + TimeDelta::FromSeconds( 70 return baseline_time_ +
64 policy::DeviceStatusCollector::kPollIntervalSeconds * call_count++); 71 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++);
65 } 72 }
66 73
67 private: 74 private:
68 PrefService* local_state_; 75 PrefService* local_state_;
69 76
70 // Baseline time for the fake times returned from GetCurrentTime(). 77 // Baseline time for the fake times returned from GetCurrentTime().
71 // It doesn't really matter what this is, as long as it stays the same over
72 // the lifetime of the object.
73 Time baseline_time_; 78 Time baseline_time_;
79
80 // The number of simulated periods since the baseline time.
81 int baseline_offset_periods_;
74 }; 82 };
75 83
76 // Return the total number of active milliseconds contained in a device 84 // Return the total number of active milliseconds contained in a device
77 // status report. 85 // status report.
78 int64 GetActiveMilliseconds(em::DeviceStatusReportRequest& status) { 86 int64 GetActiveMilliseconds(em::DeviceStatusReportRequest& status) {
79 int64 active_milliseconds = 0; 87 int64 active_milliseconds = 0;
80 for (int i = 0; i < status.active_time_size(); i++) { 88 for (int i = 0; i < status.active_period_size(); i++) {
81 const em::TimePeriod& period = status.active_time(i); 89 active_milliseconds += status.active_period(i).active_duration();
82 active_milliseconds += period.end_timestamp() - period.start_timestamp();
83 } 90 }
84 return active_milliseconds; 91 return active_milliseconds;
85 } 92 }
86 93
87 } // namespace 94 } // namespace
88 95
89 namespace policy { 96 namespace policy {
90 97
91 using ::testing::_; 98 using ::testing::_;
92 using ::testing::NotNull; 99 using ::testing::NotNull;
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 TEST_F(DeviceStatusCollectorTest, AllIdle) { 152 TEST_F(DeviceStatusCollectorTest, AllIdle) {
146 IdleState test_states[] = { 153 IdleState test_states[] = {
147 IDLE_STATE_IDLE, 154 IDLE_STATE_IDLE,
148 IDLE_STATE_IDLE, 155 IDLE_STATE_IDLE,
149 IDLE_STATE_IDLE 156 IDLE_STATE_IDLE
150 }; 157 };
151 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 158 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
152 159
153 // Test reporting with no data. 160 // Test reporting with no data.
154 status_collector_.GetStatus(&status_); 161 status_collector_.GetStatus(&status_);
155 EXPECT_EQ(0, status_.active_time_size()); 162 EXPECT_EQ(0, status_.active_period_size());
156 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 163 EXPECT_EQ(0, GetActiveMilliseconds(status_));
157 164
158 // Test reporting with a single idle sample. 165 // Test reporting with a single idle sample.
159 status_collector_.Simulate(test_states, 1); 166 status_collector_.Simulate(test_states, 1);
160 status_collector_.GetStatus(&status_); 167 status_collector_.GetStatus(&status_);
161 EXPECT_EQ(0, status_.active_time_size()); 168 EXPECT_EQ(0, status_.active_period_size());
162 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 169 EXPECT_EQ(0, GetActiveMilliseconds(status_));
163 170
164 // Test reporting with multiple consecutive idle samples. 171 // Test reporting with multiple consecutive idle samples.
165 status_collector_.Simulate(test_states, 172 status_collector_.Simulate(test_states,
166 sizeof(test_states) / sizeof(IdleState)); 173 sizeof(test_states) / sizeof(IdleState));
167 status_collector_.GetStatus(&status_); 174 status_collector_.GetStatus(&status_);
168 EXPECT_EQ(0, status_.active_time_size()); 175 EXPECT_EQ(0, status_.active_period_size());
169 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 176 EXPECT_EQ(0, GetActiveMilliseconds(status_));
170 } 177 }
171 178
172 TEST_F(DeviceStatusCollectorTest, AllActive) { 179 TEST_F(DeviceStatusCollectorTest, AllActive) {
173 IdleState test_states[] = { 180 IdleState test_states[] = {
174 IDLE_STATE_ACTIVE, 181 IDLE_STATE_ACTIVE,
175 IDLE_STATE_ACTIVE, 182 IDLE_STATE_ACTIVE,
176 IDLE_STATE_ACTIVE 183 IDLE_STATE_ACTIVE
177 }; 184 };
178 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 185 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
179 186
180 // Test a single active sample. 187 // Test a single active sample.
181 status_collector_.Simulate(test_states, 1); 188 status_collector_.Simulate(test_states, 1);
182 status_collector_.GetStatus(&status_); 189 status_collector_.GetStatus(&status_);
183 EXPECT_EQ(1, status_.active_time_size()); 190 EXPECT_EQ(1, status_.active_period_size());
184 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 191 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
185 status_.clear_active_time(); // Clear the result protobuf. 192 status_.clear_active_period(); // Clear the result protobuf.
186 193
187 // Test multiple consecutive active samples -- they should be coalesced 194 // Test multiple consecutive active samples.
188 // into a single active period.
189 status_collector_.Simulate(test_states, 195 status_collector_.Simulate(test_states,
190 sizeof(test_states) / sizeof(IdleState)); 196 sizeof(test_states) / sizeof(IdleState));
191 status_collector_.GetStatus(&status_); 197 status_collector_.GetStatus(&status_);
192 EXPECT_EQ(1, status_.active_time_size()); 198 EXPECT_EQ(1, status_.active_period_size());
193 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 199 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
194 } 200 }
195 201
196 TEST_F(DeviceStatusCollectorTest, MixedStates) { 202 TEST_F(DeviceStatusCollectorTest, MixedStates) {
197 IdleState test_states[] = { 203 IdleState test_states[] = {
198 IDLE_STATE_ACTIVE, 204 IDLE_STATE_ACTIVE,
199 IDLE_STATE_IDLE, 205 IDLE_STATE_IDLE,
200 IDLE_STATE_ACTIVE, 206 IDLE_STATE_ACTIVE,
201 IDLE_STATE_ACTIVE, 207 IDLE_STATE_ACTIVE,
202 IDLE_STATE_IDLE, 208 IDLE_STATE_IDLE,
203 IDLE_STATE_IDLE, 209 IDLE_STATE_IDLE,
204 IDLE_STATE_ACTIVE 210 IDLE_STATE_ACTIVE
205 }; 211 };
206 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 212 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
207 status_collector_.Simulate(test_states, 213 status_collector_.Simulate(test_states,
208 sizeof(test_states) / sizeof(IdleState)); 214 sizeof(test_states) / sizeof(IdleState));
209 status_collector_.GetStatus(&status_); 215 status_collector_.GetStatus(&status_);
210 EXPECT_EQ(3, status_.active_time_size());
211 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 216 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
212 } 217 }
213 218
214 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { 219 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) {
215 IdleState test_states[] = { 220 IdleState test_states[] = {
216 IDLE_STATE_ACTIVE, 221 IDLE_STATE_ACTIVE,
217 IDLE_STATE_IDLE, 222 IDLE_STATE_IDLE,
218 IDLE_STATE_ACTIVE, 223 IDLE_STATE_ACTIVE,
219 IDLE_STATE_ACTIVE, 224 IDLE_STATE_ACTIVE,
220 IDLE_STATE_IDLE, 225 IDLE_STATE_IDLE,
221 IDLE_STATE_IDLE 226 IDLE_STATE_IDLE
222 }; 227 };
223 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 228 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
224 status_collector_.Simulate(test_states, 229 status_collector_.Simulate(test_states,
225 sizeof(test_states) / sizeof(IdleState)); 230 sizeof(test_states) / sizeof(IdleState));
226 231
227 // Process the list a second time with a different collector. 232 // Process the list a second time with a different collector.
228 // It should be able to count the active periods found by the first 233 // It should be able to count the active periods found by the first
229 // collector, because the results are stored in a pref. 234 // collector, because the results are stored in a pref.
230 TestingDeviceStatusCollector second_collector(&prefs_, 235 TestingDeviceStatusCollector second_collector(&prefs_,
231 &statistics_provider_); 236 &statistics_provider_);
232 second_collector.Simulate(test_states, 237 second_collector.Simulate(test_states,
233 sizeof(test_states) / sizeof(IdleState)); 238 sizeof(test_states) / sizeof(IdleState));
234 239
235 second_collector.GetStatus(&status_); 240 second_collector.GetStatus(&status_);
236 EXPECT_EQ(4, status_.active_time_size());
237 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 241 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
238 } 242 }
239 243
240 TEST_F(DeviceStatusCollectorTest, Times) { 244 TEST_F(DeviceStatusCollectorTest, Times) {
241 IdleState test_states[] = { 245 IdleState test_states[] = {
242 IDLE_STATE_ACTIVE, 246 IDLE_STATE_ACTIVE,
243 IDLE_STATE_IDLE, 247 IDLE_STATE_IDLE,
244 IDLE_STATE_ACTIVE, 248 IDLE_STATE_ACTIVE,
245 IDLE_STATE_ACTIVE, 249 IDLE_STATE_ACTIVE,
246 IDLE_STATE_IDLE, 250 IDLE_STATE_IDLE,
247 IDLE_STATE_IDLE 251 IDLE_STATE_IDLE
248 }; 252 };
249 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 253 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
250 status_collector_.Simulate(test_states, 254 status_collector_.Simulate(test_states,
251 sizeof(test_states) / sizeof(IdleState)); 255 sizeof(test_states) / sizeof(IdleState));
252 status_collector_.GetStatus(&status_); 256 status_collector_.GetStatus(&status_);
253 EXPECT_EQ(2, status_.active_time_size());
254
255 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 257 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
256 } 258 }
257 259
258 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { 260 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) {
259 IdleState test_states[] = { 261 IdleState test_states[] = {
260 IDLE_STATE_ACTIVE, 262 IDLE_STATE_ACTIVE,
261 IDLE_STATE_IDLE 263 IDLE_STATE_IDLE
262 }; 264 };
263 unsigned int max_periods = 10; 265 unsigned int max_days = 10;
264 266
265 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 267 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
266 status_collector_.set_max_stored_active_periods(max_periods); 268 status_collector_.set_max_stored_past_activity_days(max_days - 1);
269 status_collector_.set_max_stored_future_activity_days(1);
270 Time baseline = Time::Now().LocalMidnight();
267 271
268 // Simulate 12 active periods. 272 // Simulate 12 active periods.
269 for (int i = 0; i < 12; i++) { 273 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) {
270 status_collector_.Simulate(test_states, 274 status_collector_.Simulate(test_states,
271 sizeof(test_states) / sizeof(IdleState)); 275 sizeof(test_states) / sizeof(IdleState));
276 // Advance the simulated clock by a day.
277 baseline += TimeDelta::FromDays(1);
278 status_collector_.SetBaselineTime(baseline);
272 } 279 }
273 280
274 // Check that we don't exceed the max number of periods. 281 // Check that we don't exceed the max number of periods.
275 status_collector_.GetStatus(&status_); 282 status_collector_.GetStatus(&status_);
276 EXPECT_EQ(static_cast<int>(max_periods), status_.active_time_size()); 283 EXPECT_EQ(static_cast<int>(max_days), status_.active_period_size());
284
285 // Simulate some future times.
286 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) {
287 status_collector_.Simulate(test_states,
288 sizeof(test_states) / sizeof(IdleState));
289 // Advance the simulated clock by a day.
290 baseline += TimeDelta::FromDays(1);
291 status_collector_.SetBaselineTime(baseline);
292 }
293 // Set the clock back so the previous simulated times are in the future.
294 baseline -= TimeDelta::FromDays(20);
295 status_collector_.SetBaselineTime(baseline);
296
297 // Collect one more data point to trigger pruning.
298 status_collector_.Simulate(test_states, 1);
299
300 // Check that we don't exceed the max number of periods.
301 status_.clear_active_period();
302 status_collector_.GetStatus(&status_);
303 EXPECT_LT(status_.active_period_size(), static_cast<int>(max_days));
277 } 304 }
278 305
279 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { 306 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) {
280 // If the pref for collecting device activity times isn't explicitly turned 307 // If the pref for collecting device activity times isn't explicitly turned
281 // on, no data on activity times should be reported. 308 // on, no data on activity times should be reported.
282 309
283 IdleState test_states[] = { 310 IdleState test_states[] = {
284 IDLE_STATE_ACTIVE, 311 IDLE_STATE_ACTIVE,
285 IDLE_STATE_ACTIVE, 312 IDLE_STATE_ACTIVE,
286 IDLE_STATE_ACTIVE 313 IDLE_STATE_ACTIVE
287 }; 314 };
288 status_collector_.Simulate(test_states, 315 status_collector_.Simulate(test_states,
289 sizeof(test_states) / sizeof(IdleState)); 316 sizeof(test_states) / sizeof(IdleState));
290 status_collector_.GetStatus(&status_); 317 status_collector_.GetStatus(&status_);
291 EXPECT_EQ(0, status_.active_time_size()); 318 EXPECT_EQ(0, status_.active_period_size());
292 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 319 EXPECT_EQ(0, GetActiveMilliseconds(status_));
293 } 320 }
294 321
322 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) {
323 IdleState test_states[] = {
324 IDLE_STATE_ACTIVE
325 };
326 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
327 status_collector_.SetBaselineTime(
328 Time::Now().LocalMidnight() + TimeDelta::FromSeconds(10));
329
330 status_collector_.Simulate(test_states, 1);
331 status_collector_.GetStatus(&status_);
332 EXPECT_EQ(2, status_.active_period_size());
Mattias Nissler (ping if slow) 2012/02/15 14:56:21 We should check that we get correct times for both
Patrick Dubroy 2012/02/15 15:37:25 Done.
333 }
334
295 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { 335 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) {
296 // Test that boot mode data is not reported if the pref is not turned on. 336 // Test that boot mode data is not reported if the pref is not turned on.
297 status_collector_.GetStatus(&status_); 337 status_collector_.GetStatus(&status_);
298 EXPECT_EQ(false, status_.has_boot_mode()); 338 EXPECT_EQ(false, status_.has_boot_mode());
299 339
300 EXPECT_CALL(statistics_provider_, 340 EXPECT_CALL(statistics_provider_,
301 GetMachineStatistic("devsw_boot", NotNull())) 341 GetMachineStatistic("devsw_boot", NotNull()))
302 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); 342 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true)));
303 EXPECT_EQ(false, status_.has_boot_mode()); 343 EXPECT_EQ(false, status_.has_boot_mode());
304 344
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 EXPECT_EQ(true, status_.has_os_version()); 385 EXPECT_EQ(true, status_.has_os_version());
346 EXPECT_EQ(true, status_.has_firmware_version()); 386 EXPECT_EQ(true, status_.has_firmware_version());
347 387
348 // Check that the browser version is not empty. OS version & firmware 388 // Check that the browser version is not empty. OS version & firmware
349 // don't have any reasonable values inside the unit test, so those 389 // don't have any reasonable values inside the unit test, so those
350 // aren't checked. 390 // aren't checked.
351 EXPECT_NE("", status_.browser_version()); 391 EXPECT_NE("", status_.browser_version());
352 } 392 }
353 393
354 } // namespace policy 394 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698