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/environment.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
11 #include "chrome/browser/chromeos/settings/cros_settings.h" | 12 #include "chrome/browser/chromeos/settings/cros_settings.h" |
12 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | 13 #include "chrome/browser/chromeos/settings/cros_settings_names.h" |
13 #include "chrome/browser/chromeos/settings/cros_settings_provider.h" | 14 #include "chrome/browser/chromeos/settings/cros_settings_provider.h" |
14 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 15 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
15 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" | 16 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" |
16 #include "chrome/browser/chromeos/system/statistics_provider.h" | 17 #include "chrome/browser/chromeos/system/statistics_provider.h" |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 88 |
88 // Reset the baseline time. | 89 // Reset the baseline time. |
89 void SetBaselineTime(Time time) { | 90 void SetBaselineTime(Time time) { |
90 baseline_time_ = time; | 91 baseline_time_ = time; |
91 baseline_offset_periods_ = 0; | 92 baseline_offset_periods_ = 0; |
92 } | 93 } |
93 | 94 |
94 protected: | 95 protected: |
95 virtual void CheckIdleState() OVERRIDE { | 96 virtual void CheckIdleState() OVERRIDE { |
96 // This should never be called in testing, as it results in a dbus call. | 97 // This should never be called in testing, as it results in a dbus call. |
97 NOTREACHED(); | 98 ADD_FAILURE(); |
98 } | 99 } |
99 | 100 |
100 // Each time this is called, returns a time that is a fixed increment | 101 // Each time this is called, returns a time that is a fixed increment |
101 // later than the previous time. | 102 // later than the previous time. |
102 virtual Time GetCurrentTime() OVERRIDE { | 103 virtual Time GetCurrentTime() OVERRIDE { |
103 int poll_interval = policy::DeviceStatusCollector::kIdlePollIntervalSeconds; | 104 int poll_interval = policy::DeviceStatusCollector::kIdlePollIntervalSeconds; |
104 return baseline_time_ + | 105 return baseline_time_ + |
105 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++); | 106 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++); |
106 } | 107 } |
107 | 108 |
(...skipping 12 matching lines...) Expand all Loading... |
120 for (int i = 0; i < status.active_period_size(); i++) { | 121 for (int i = 0; i < status.active_period_size(); i++) { |
121 active_milliseconds += status.active_period(i).active_duration(); | 122 active_milliseconds += status.active_period(i).active_duration(); |
122 } | 123 } |
123 return active_milliseconds; | 124 return active_milliseconds; |
124 } | 125 } |
125 | 126 |
126 } // namespace | 127 } // namespace |
127 | 128 |
128 namespace policy { | 129 namespace policy { |
129 | 130 |
| 131 // Though it is a unit test, this test is linked with browser_tests so that it |
| 132 // runs in a separate process. The intention is to avoid overriding the timezone |
| 133 // environment variable for other tests. |
130 class DeviceStatusCollectorTest : public testing::Test { | 134 class DeviceStatusCollectorTest : public testing::Test { |
131 public: | 135 public: |
132 DeviceStatusCollectorTest() | 136 DeviceStatusCollectorTest() |
133 : message_loop_(MessageLoop::TYPE_UI), | 137 : message_loop_(MessageLoop::TYPE_UI), |
134 ui_thread_(content::BrowserThread::UI, &message_loop_), | 138 ui_thread_(content::BrowserThread::UI, &message_loop_), |
135 file_thread_(content::BrowserThread::FILE, &message_loop_), | 139 file_thread_(content::BrowserThread::FILE, &message_loop_), |
136 io_thread_(content::BrowserThread::IO, &message_loop_) { | 140 io_thread_(content::BrowserThread::IO, &message_loop_) { |
| 141 // Run this test with a well-known timezone so that Time::LocalMidnight() |
| 142 // returns the same values on all machines. |
| 143 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 144 env->SetVar("TZ", "UTC"); |
| 145 |
137 TestingDeviceStatusCollector::RegisterPrefs(&prefs_); | 146 TestingDeviceStatusCollector::RegisterPrefs(&prefs_); |
138 | 147 |
139 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) | 148 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) |
140 .WillRepeatedly(Return(false)); | 149 .WillRepeatedly(Return(false)); |
141 | 150 |
142 // Remove the real DeviceSettingsProvider and replace it with a stub. | 151 // Remove the real DeviceSettingsProvider and replace it with a stub. |
143 cros_settings_ = chromeos::CrosSettings::Get(); | 152 cros_settings_ = chromeos::CrosSettings::Get(); |
144 device_settings_provider_ = | 153 device_settings_provider_ = |
145 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); | 154 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); |
146 EXPECT_TRUE(device_settings_provider_ != NULL); | 155 EXPECT_TRUE(device_settings_provider_ != NULL); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 IDLE_STATE_IDLE, | 340 IDLE_STATE_IDLE, |
332 IDLE_STATE_IDLE | 341 IDLE_STATE_IDLE |
333 }; | 342 }; |
334 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 343 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
335 status_collector_->Simulate(test_states, | 344 status_collector_->Simulate(test_states, |
336 sizeof(test_states) / sizeof(IdleState)); | 345 sizeof(test_states) / sizeof(IdleState)); |
337 GetStatus(); | 346 GetStatus(); |
338 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 347 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
339 } | 348 } |
340 | 349 |
341 // Fails after after WebKit roll [132375:132450] | 350 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { |
342 // http://crbug.com/157848 | |
343 TEST_F(DeviceStatusCollectorTest, DISABLED_MaxStoredPeriods) { | |
344 IdleState test_states[] = { | 351 IdleState test_states[] = { |
345 IDLE_STATE_ACTIVE, | 352 IDLE_STATE_ACTIVE, |
346 IDLE_STATE_IDLE | 353 IDLE_STATE_IDLE |
347 }; | 354 }; |
348 unsigned int max_days = 10; | 355 int max_days = 10; |
349 | 356 |
350 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 357 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
351 status_collector_->set_max_stored_past_activity_days(max_days - 1); | 358 status_collector_->set_max_stored_past_activity_days(max_days - 1); |
352 status_collector_->set_max_stored_future_activity_days(1); | 359 status_collector_->set_max_stored_future_activity_days(1); |
353 Time baseline = Time::Now().LocalMidnight(); | 360 Time baseline = Time::Now().LocalMidnight(); |
354 | 361 |
355 // Simulate 12 active periods. | 362 // Simulate 12 active periods. |
356 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { | 363 for (int i = 0; i < max_days + 2; i++) { |
357 status_collector_->Simulate(test_states, | 364 status_collector_->Simulate(test_states, |
358 sizeof(test_states) / sizeof(IdleState)); | 365 sizeof(test_states) / sizeof(IdleState)); |
359 // Advance the simulated clock by a day. | 366 // Advance the simulated clock by a day. |
360 baseline += TimeDelta::FromDays(1); | 367 baseline += TimeDelta::FromDays(1); |
361 status_collector_->SetBaselineTime(baseline); | 368 status_collector_->SetBaselineTime(baseline); |
362 } | 369 } |
363 | 370 |
364 // Check that we don't exceed the max number of periods. | 371 // Check that we don't exceed the max number of periods. |
365 GetStatus(); | 372 GetStatus(); |
366 EXPECT_EQ(static_cast<int>(max_days), status_.active_period_size()); | 373 EXPECT_EQ(max_days, status_.active_period_size()); |
367 | 374 |
368 // Simulate some future times. | 375 // Simulate some future times. |
369 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { | 376 for (int i = 0; i < max_days + 2; i++) { |
370 status_collector_->Simulate(test_states, | 377 status_collector_->Simulate(test_states, |
371 sizeof(test_states) / sizeof(IdleState)); | 378 sizeof(test_states) / sizeof(IdleState)); |
372 // Advance the simulated clock by a day. | 379 // Advance the simulated clock by a day. |
373 baseline += TimeDelta::FromDays(1); | 380 baseline += TimeDelta::FromDays(1); |
374 status_collector_->SetBaselineTime(baseline); | 381 status_collector_->SetBaselineTime(baseline); |
375 } | 382 } |
376 // Set the clock back so the previous simulated times are in the future. | 383 // Set the clock back so the previous simulated times are in the future. |
377 baseline -= TimeDelta::FromDays(20); | 384 baseline -= TimeDelta::FromDays(20); |
378 status_collector_->SetBaselineTime(baseline); | 385 status_collector_->SetBaselineTime(baseline); |
379 | 386 |
380 // Collect one more data point to trigger pruning. | 387 // Collect one more data point to trigger pruning. |
381 status_collector_->Simulate(test_states, 1); | 388 status_collector_->Simulate(test_states, 1); |
382 | 389 |
383 // Check that we don't exceed the max number of periods. | 390 // Check that we don't exceed the max number of periods. |
384 status_.clear_active_period(); | 391 status_.clear_active_period(); |
385 GetStatus(); | 392 GetStatus(); |
386 EXPECT_LT(status_.active_period_size(), static_cast<int>(max_days)); | 393 EXPECT_LT(status_.active_period_size(), max_days); |
387 } | 394 } |
388 | 395 |
389 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { | 396 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { |
390 // If the pref for collecting device activity times isn't explicitly turned | 397 // If the pref for collecting device activity times isn't explicitly turned |
391 // on, no data on activity times should be reported. | 398 // on, no data on activity times should be reported. |
392 | 399 |
393 IdleState test_states[] = { | 400 IdleState test_states[] = { |
394 IDLE_STATE_ACTIVE, | 401 IDLE_STATE_ACTIVE, |
395 IDLE_STATE_ACTIVE, | 402 IDLE_STATE_ACTIVE, |
396 IDLE_STATE_ACTIVE | 403 IDLE_STATE_ACTIVE |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 // Check that after enabling location reporting again, an error is reported | 540 // Check that after enabling location reporting again, an error is reported |
534 // if no valid fix is available. | 541 // if no valid fix is available. |
535 SetMockPositionToReturnNext(invalid_fix); | 542 SetMockPositionToReturnNext(invalid_fix); |
536 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); | 543 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); |
537 // Allow the new pref to propagate to the status collector. | 544 // Allow the new pref to propagate to the status collector. |
538 message_loop_.RunUntilIdle(); | 545 message_loop_.RunUntilIdle(); |
539 CheckThatALocationErrorIsReported(); | 546 CheckThatALocationErrorIsReported(); |
540 } | 547 } |
541 | 548 |
542 } // namespace policy | 549 } // namespace policy |
OLD | NEW |