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/policy/device_status_collector.h" | 5 #include "chrome/browser/policy/device_status_collector.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "chrome/browser/chromeos/settings/cros_settings.h" | 10 #include "chrome/browser/chromeos/settings/cros_settings.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 using base::Time; | 31 using base::Time; |
32 | 32 |
33 namespace em = enterprise_management; | 33 namespace em = enterprise_management; |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; | 37 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; |
38 | 38 |
39 scoped_ptr<content::Geoposition> mock_position_to_return_next; | 39 scoped_ptr<content::Geoposition> mock_position_to_return_next; |
40 | 40 |
41 Time TestBaselineTime() { | |
42 return Time::UnixEpoch() + TimeDelta::FromDays(42); | |
43 } | |
44 | |
41 void SetMockPositionToReturnNext(const content::Geoposition &position) { | 45 void SetMockPositionToReturnNext(const content::Geoposition &position) { |
42 mock_position_to_return_next.reset(new content::Geoposition(position)); | 46 mock_position_to_return_next.reset(new content::Geoposition(position)); |
43 } | 47 } |
44 | 48 |
45 void MockPositionUpdateRequester( | 49 void MockPositionUpdateRequester( |
46 const content::GeolocationUpdateCallback& callback) { | 50 const content::GeolocationUpdateCallback& callback) { |
47 if (!mock_position_to_return_next.get()) | 51 if (!mock_position_to_return_next.get()) |
48 return; | 52 return; |
49 | 53 |
50 // If the fix is invalid, the DeviceStatusCollector will immediately request | 54 // If the fix is invalid, the DeviceStatusCollector will immediately request |
51 // another update when it receives the callback. This is desirable and safe in | 55 // another update when it receives the callback. This is desirable and safe in |
52 // real life where geolocation updates arrive asynchronously. In this testing | 56 // real life where geolocation updates arrive asynchronously. In this testing |
53 // harness, the callback is invoked synchronously upon request, leading to a | 57 // harness, the callback is invoked synchronously upon request, leading to a |
54 // request-callback loop. The loop is broken by returning the mock position | 58 // request-callback loop. The loop is broken by returning the mock position |
55 // only once. | 59 // only once. |
56 scoped_ptr<content::Geoposition> position( | 60 scoped_ptr<content::Geoposition> position( |
57 mock_position_to_return_next.release()); | 61 mock_position_to_return_next.release()); |
58 callback.Run(*position); | 62 callback.Run(*position); |
59 } | 63 } |
60 | 64 |
61 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { | 65 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { |
62 public: | 66 public: |
63 TestingDeviceStatusCollector( | 67 TestingDeviceStatusCollector( |
64 PrefService* local_state, | 68 PrefService* local_state, |
65 chromeos::system::StatisticsProvider* provider) | 69 chromeos::system::StatisticsProvider* provider) |
66 : policy::DeviceStatusCollector(local_state, | 70 : policy::DeviceStatusCollector(local_state, |
67 provider, | 71 provider, |
68 &MockPositionUpdateRequester) { | 72 &MockPositionUpdateRequester) { |
69 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness | 73 // Set the baseline time to a fixed value to prevent test flakiness |
70 // due to a single activity period spanning two days. | 74 // due to a single activity period spanning two days. |
71 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1)); | 75 SetBaselineTime(TestBaselineTime()); |
76 | |
77 // Initialize the collector after setting the baseline time. | |
78 Init(); | |
72 } | 79 } |
73 | 80 |
74 void Simulate(IdleState* states, int len) { | 81 void Simulate(IdleState* states, int len) { |
75 for (int i = 0; i < len; i++) | 82 for (int i = 0; i < len; i++) |
76 IdleStateCallback(states[i]); | 83 IdleStateCallback(states[i]); |
77 } | 84 } |
78 | 85 |
79 void set_max_stored_past_activity_days(unsigned int value) { | 86 void set_max_stored_past_activity_days(unsigned int value) { |
80 max_stored_past_activity_days_ = value; | 87 max_stored_past_activity_days_ = value; |
81 } | 88 } |
82 | 89 |
83 void set_max_stored_future_activity_days(unsigned int value) { | 90 void set_max_stored_future_activity_days(unsigned int value) { |
84 max_stored_future_activity_days_ = value; | 91 max_stored_future_activity_days_ = value; |
85 } | 92 } |
86 | 93 |
87 // Reset the baseline time. | 94 // Reset the baseline time. |
88 void SetBaselineTime(Time time) { | 95 void SetBaselineTime(Time time) { |
89 baseline_time_ = time; | 96 baseline_time_ = time; |
90 baseline_offset_periods_ = 0; | 97 baseline_offset_periods_ = 0; |
91 } | 98 } |
92 | 99 |
93 protected: | 100 protected: |
94 virtual void CheckIdleState() OVERRIDE { | 101 virtual void CheckIdleState() OVERRIDE { |
95 // This should never be called in testing, as it results in a dbus call. | 102 // This should never be called in testing, as it results in a dbus call. |
96 NOTREACHED(); | 103 ADD_FAILURE(); |
97 } | 104 } |
98 | 105 |
99 // Each time this is called, returns a time that is a fixed increment | 106 // Each time this is called, returns a time that is a fixed increment |
100 // later than the previous time. | 107 // later than the previous time. |
101 virtual Time GetCurrentTime() OVERRIDE { | 108 virtual Time GetCurrentTime() OVERRIDE { |
102 int poll_interval = policy::DeviceStatusCollector::kIdlePollIntervalSeconds; | 109 int poll_interval = policy::DeviceStatusCollector::kIdlePollIntervalSeconds; |
103 return baseline_time_ + | 110 return baseline_time_ + |
104 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++); | 111 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++); |
105 } | 112 } |
106 | 113 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 EXPECT_TRUE(location.has_timestamp()); | 193 EXPECT_TRUE(location.has_timestamp()); |
187 EXPECT_FALSE(location.has_altitude()); | 194 EXPECT_FALSE(location.has_altitude()); |
188 EXPECT_FALSE(location.has_altitude_accuracy()); | 195 EXPECT_FALSE(location.has_altitude_accuracy()); |
189 EXPECT_FALSE(location.has_heading()); | 196 EXPECT_FALSE(location.has_heading()); |
190 EXPECT_FALSE(location.has_speed()); | 197 EXPECT_FALSE(location.has_speed()); |
191 EXPECT_FALSE(location.has_error_message()); | 198 EXPECT_FALSE(location.has_error_message()); |
192 EXPECT_DOUBLE_EQ(4.3, location.latitude()); | 199 EXPECT_DOUBLE_EQ(4.3, location.latitude()); |
193 EXPECT_DOUBLE_EQ(-7.8, location.longitude()); | 200 EXPECT_DOUBLE_EQ(-7.8, location.longitude()); |
194 EXPECT_DOUBLE_EQ(3., location.accuracy()); | 201 EXPECT_DOUBLE_EQ(3., location.accuracy()); |
195 // Check that the timestamp is not older than ten minutes. | 202 // Check that the timestamp is not older than ten minutes. |
196 EXPECT_TRUE(Time::Now() - Time::FromDoubleT(location.timestamp() / 1000.) < | 203 Time timestamp = Time::FromDoubleT(location.timestamp() / 1000.0); |
197 TimeDelta::FromMinutes(10)); | 204 EXPECT_TRUE(TestBaselineTime() - timestamp < TimeDelta::FromMinutes(10)); |
Patrick Dubroy
2012/10/26 09:36:46
Isn't the delta always going to be negative here?
Joao da Silva
2012/11/15 13:25:48
The timestamp is expected to be some time in the p
Patrick Dubroy
2012/11/15 13:53:01
Ah, I see.
| |
198 } | 205 } |
199 | 206 |
200 void CheckThatALocationErrorIsReported() { | 207 void CheckThatALocationErrorIsReported() { |
201 GetStatus(); | 208 GetStatus(); |
202 EXPECT_TRUE(status_.has_device_location()); | 209 EXPECT_TRUE(status_.has_device_location()); |
203 em::DeviceLocation location = status_.device_location(); | 210 em::DeviceLocation location = status_.device_location(); |
204 EXPECT_TRUE(location.has_error_code()); | 211 EXPECT_TRUE(location.has_error_code()); |
205 EXPECT_EQ(em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE, | 212 EXPECT_EQ(em::DeviceLocation::ERROR_CODE_POSITION_UNAVAILABLE, |
206 location.error_code()); | 213 location.error_code()); |
207 } | 214 } |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
326 IDLE_STATE_IDLE, | 333 IDLE_STATE_IDLE, |
327 IDLE_STATE_IDLE | 334 IDLE_STATE_IDLE |
328 }; | 335 }; |
329 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 336 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
330 status_collector_->Simulate(test_states, | 337 status_collector_->Simulate(test_states, |
331 sizeof(test_states) / sizeof(IdleState)); | 338 sizeof(test_states) / sizeof(IdleState)); |
332 GetStatus(); | 339 GetStatus(); |
333 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 340 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
334 } | 341 } |
335 | 342 |
336 // Fails after after WebKit roll [132375:132450] | 343 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { |
337 // http://crbug.com/157848 | |
338 TEST_F(DeviceStatusCollectorTest, DISABLED_MaxStoredPeriods) { | |
339 IdleState test_states[] = { | 344 IdleState test_states[] = { |
340 IDLE_STATE_ACTIVE, | 345 IDLE_STATE_ACTIVE, |
341 IDLE_STATE_IDLE | 346 IDLE_STATE_IDLE |
342 }; | 347 }; |
343 unsigned int max_days = 10; | 348 unsigned int max_days = 10; |
344 | 349 |
345 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 350 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
346 status_collector_->set_max_stored_past_activity_days(max_days - 1); | 351 status_collector_->set_max_stored_past_activity_days(max_days - 1); |
347 status_collector_->set_max_stored_future_activity_days(1); | 352 status_collector_->set_max_stored_future_activity_days(1); |
348 Time baseline = Time::Now().LocalMidnight(); | 353 Time baseline = TestBaselineTime(); |
349 | 354 |
350 // Simulate 12 active periods. | 355 // Simulate 12 active periods. |
351 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { | 356 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { |
352 status_collector_->Simulate(test_states, | 357 status_collector_->Simulate(test_states, |
353 sizeof(test_states) / sizeof(IdleState)); | 358 sizeof(test_states) / sizeof(IdleState)); |
354 // Advance the simulated clock by a day. | 359 // Advance the simulated clock by a day. |
355 baseline += TimeDelta::FromDays(1); | 360 baseline += TimeDelta::FromDays(1); |
356 status_collector_->SetBaselineTime(baseline); | 361 status_collector_->SetBaselineTime(baseline); |
357 } | 362 } |
358 | 363 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
398 } | 403 } |
399 | 404 |
400 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) { | 405 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) { |
401 IdleState test_states[] = { | 406 IdleState test_states[] = { |
402 IDLE_STATE_ACTIVE | 407 IDLE_STATE_ACTIVE |
403 }; | 408 }; |
404 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 409 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
405 | 410 |
406 // Set the baseline time to 10 seconds after midnight. | 411 // Set the baseline time to 10 seconds after midnight. |
407 status_collector_->SetBaselineTime( | 412 status_collector_->SetBaselineTime( |
408 Time::Now().LocalMidnight() + TimeDelta::FromSeconds(10)); | 413 TestBaselineTime().LocalMidnight() + TimeDelta::FromSeconds(10)); |
409 | 414 |
410 status_collector_->Simulate(test_states, 1); | 415 status_collector_->Simulate(test_states, 1); |
411 GetStatus(); | 416 GetStatus(); |
412 ASSERT_EQ(2, status_.active_period_size()); | 417 ASSERT_EQ(2, status_.active_period_size()); |
413 | 418 |
414 em::ActiveTimePeriod period0 = status_.active_period(0); | 419 em::ActiveTimePeriod period0 = status_.active_period(0); |
415 em::ActiveTimePeriod period1 = status_.active_period(1); | 420 em::ActiveTimePeriod period1 = status_.active_period(1); |
416 EXPECT_EQ(ActivePeriodMilliseconds() - 10000, period0.active_duration()); | 421 EXPECT_EQ(ActivePeriodMilliseconds() - 10000, period0.active_duration()); |
417 EXPECT_EQ(10000, period1.active_duration()); | 422 EXPECT_EQ(10000, period1.active_duration()); |
418 | 423 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 // don't have any reasonable values inside the unit test, so those | 488 // don't have any reasonable values inside the unit test, so those |
484 // aren't checked. | 489 // aren't checked. |
485 EXPECT_NE("", status_.browser_version()); | 490 EXPECT_NE("", status_.browser_version()); |
486 } | 491 } |
487 | 492 |
488 TEST_F(DeviceStatusCollectorTest, Location) { | 493 TEST_F(DeviceStatusCollectorTest, Location) { |
489 content::Geoposition valid_fix; | 494 content::Geoposition valid_fix; |
490 valid_fix.latitude = 4.3; | 495 valid_fix.latitude = 4.3; |
491 valid_fix.longitude = -7.8; | 496 valid_fix.longitude = -7.8; |
492 valid_fix.accuracy = 3.; | 497 valid_fix.accuracy = 3.; |
493 valid_fix.timestamp = Time::Now(); | 498 valid_fix.timestamp = TestBaselineTime() - TimeDelta::FromMinutes(5); |
494 | 499 |
495 content::Geoposition invalid_fix; | 500 content::Geoposition invalid_fix; |
496 invalid_fix.error_code = | 501 invalid_fix.error_code = |
497 content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 502 content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
498 invalid_fix.timestamp = Time::Now(); | 503 invalid_fix.timestamp = TestBaselineTime() - TimeDelta::FromMinutes(4); |
499 | 504 |
500 // Check that when device location reporting is disabled, no location is | 505 // Check that when device location reporting is disabled, no location is |
501 // reported. | 506 // reported. |
502 SetMockPositionToReturnNext(valid_fix); | 507 SetMockPositionToReturnNext(valid_fix); |
503 CheckThatNoLocationIsReported(); | 508 CheckThatNoLocationIsReported(); |
504 | 509 |
505 // Check that when device location reporting is enabled and a valid fix is | 510 // Check that when device location reporting is enabled and a valid fix is |
506 // available, the location is reported and is stored in local state. | 511 // available, the location is reported and is stored in local state. |
507 SetMockPositionToReturnNext(valid_fix); | 512 SetMockPositionToReturnNext(valid_fix); |
508 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); | 513 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); |
(...skipping 19 matching lines...) Expand all Loading... | |
528 // Check that after enabling location reporting again, an error is reported | 533 // Check that after enabling location reporting again, an error is reported |
529 // if no valid fix is available. | 534 // if no valid fix is available. |
530 SetMockPositionToReturnNext(invalid_fix); | 535 SetMockPositionToReturnNext(invalid_fix); |
531 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); | 536 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); |
532 // Allow the new pref to propagate to the status collector. | 537 // Allow the new pref to propagate to the status collector. |
533 message_loop_.RunAllPending(); | 538 message_loop_.RunAllPending(); |
534 CheckThatALocationErrorIsReported(); | 539 CheckThatALocationErrorIsReported(); |
535 } | 540 } |
536 | 541 |
537 } // namespace policy | 542 } // namespace policy |
OLD | NEW |