| 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/message_loop.h" | 7 #include "base/memory/singleton.h" |
| 8 #include "base/time.h" | 8 #include "base/synchronization/waitable_event.h" |
| 9 #include "chrome/browser/idle.h" | |
| 10 #include "chrome/browser/chromeos/cros_settings.h" | 9 #include "chrome/browser/chromeos/cros_settings.h" |
| 11 #include "chrome/browser/chromeos/cros_settings_names.h" | |
| 12 #include "chrome/browser/chromeos/cros_settings_provider.h" | |
| 13 #include "chrome/browser/chromeos/stub_cros_settings_provider.h" | 10 #include "chrome/browser/chromeos/stub_cros_settings_provider.h" |
| 14 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" | 11 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" |
| 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 12 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/common/pref_names.h" |
| 17 #include "chrome/test/base/testing_pref_service.h" | 14 #include "chrome/test/base/testing_pref_service.h" |
| 15 #include "content/browser/geolocation/arbitrator_dependency_factory.h" |
| 16 #include "content/browser/geolocation/fake_access_token_store.h" |
| 17 #include "content/browser/geolocation/geolocation_provider.h" |
| 18 #include "content/browser/geolocation/location_arbitrator.h" |
| 19 #include "content/browser/geolocation/mock_location_provider.h" |
| 18 #include "content/test/test_browser_thread.h" | 20 #include "content/test/test_browser_thread.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | 21 |
| 22 using ::testing::_; |
| 23 using ::testing::DoAll; |
| 24 using ::testing::Invoke; |
| 25 using ::testing::WithoutArgs; |
| 22 using base::TimeDelta; | 26 using base::TimeDelta; |
| 23 using base::Time; | 27 using base::Time; |
| 24 | 28 |
| 25 namespace em = enterprise_management; | 29 namespace em = enterprise_management; |
| 26 | 30 |
| 27 namespace { | 31 namespace { |
| 28 | 32 |
| 29 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; | 33 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; |
| 30 | 34 |
| 35 // Mock geolocation stack, part 1: |
| 36 // A factory that provides the GeolocationArbitrator with a mock access token |
| 37 // store and a mock network location provider which, depending on the value of |
| 38 // |has_valid_location|, simulates success or failure in acquiring a location |
| 39 // fix. |
| 40 class TestingDependencyFactory |
| 41 : public DefaultGeolocationArbitratorDependencyFactory { |
| 42 public: |
| 43 explicit TestingDependencyFactory(bool has_valid_location) |
| 44 : DefaultGeolocationArbitratorDependencyFactory(), |
| 45 has_valid_location_(has_valid_location) { } |
| 46 |
| 47 virtual content::AccessTokenStore* NewAccessTokenStore() OVERRIDE { |
| 48 content::FakeAccessTokenStore* store = new content::FakeAccessTokenStore(); |
| 49 EXPECT_CALL(*store, LoadAccessTokens(_)) |
| 50 .WillRepeatedly(DoAll( |
| 51 Invoke(store, |
| 52 &content::FakeAccessTokenStore::DefaultLoadAccessTokens), |
| 53 WithoutArgs( |
| 54 Invoke(store, |
| 55 &content::FakeAccessTokenStore:: |
| 56 NotifyDelegateTokensLoaded)))); |
| 57 return store; |
| 58 } |
| 59 |
| 60 virtual LocationProviderBase* NewNetworkLocationProvider( |
| 61 content::AccessTokenStore* access_token_store, |
| 62 net::URLRequestContextGetter* context, |
| 63 const GURL& url, |
| 64 const string16& access_token) OVERRIDE { |
| 65 if (has_valid_location_) |
| 66 return NewAutoSuccessMockNetworkLocationProvider(); |
| 67 else |
| 68 return NewAutoFailMockNetworkLocationProvider(); |
| 69 } |
| 70 |
| 71 virtual LocationProviderBase* NewSystemLocationProvider() OVERRIDE { |
| 72 return NULL; |
| 73 } |
| 74 |
| 75 bool has_valid_location_; |
| 76 }; |
| 77 |
| 78 // Mock geolocation stack, part 2: |
| 79 // A wrapper around the regular GeolocationProvider that signals when it has |
| 80 // received a location update. |
| 81 class TestingGeolocationProvider : public GeolocationProvider { |
| 82 public: |
| 83 explicit TestingGeolocationProvider(base::WaitableEvent* event) |
| 84 : GeolocationProvider(), |
| 85 event_(event) { } |
| 86 |
| 87 virtual void OnLocationUpdate(const Geoposition& position) OVERRIDE { |
| 88 GeolocationProvider::OnLocationUpdate(position); |
| 89 event_->Signal(); |
| 90 } |
| 91 |
| 92 private: |
| 93 base::WaitableEvent* event_; |
| 94 }; |
| 95 |
| 31 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { | 96 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { |
| 32 public: | 97 public: |
| 33 TestingDeviceStatusCollector( | 98 TestingDeviceStatusCollector( |
| 34 PrefService* local_state, | 99 PrefService* local_state, |
| 35 chromeos::system::StatisticsProvider* provider) | 100 chromeos::system::StatisticsProvider* statistics_provider, |
| 36 : policy::DeviceStatusCollector(local_state, provider), | 101 GeolocationProvider* geolocation_provider) |
| 37 local_state_(local_state) { | 102 : policy::DeviceStatusCollector(local_state, statistics_provider) { |
| 38 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness | 103 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness |
| 39 // due to a single activity period spanning two days. | 104 // due to a single activity period spanning two days. |
| 40 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1)); | 105 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1)); |
| 106 |
| 107 SetGeolocationProviderForTest(geolocation_provider); |
| 41 } | 108 } |
| 42 | 109 |
| 43 void Simulate(IdleState* states, int len) { | 110 void Simulate(IdleState* states, int len) { |
| 44 for (int i = 0; i < len; i++) | 111 for (int i = 0; i < len; i++) |
| 45 IdleStateCallback(states[i]); | 112 IdleStateCallback(states[i]); |
| 46 } | 113 } |
| 47 | 114 |
| 48 void set_max_stored_past_activity_days(unsigned int value) { | 115 void set_max_stored_past_activity_days(unsigned int value) { |
| 49 max_stored_past_activity_days_ = value; | 116 max_stored_past_activity_days_ = value; |
| 50 } | 117 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 67 | 134 |
| 68 // Each time this is called, returns a time that is a fixed increment | 135 // Each time this is called, returns a time that is a fixed increment |
| 69 // later than the previous time. | 136 // later than the previous time. |
| 70 virtual Time GetCurrentTime() OVERRIDE { | 137 virtual Time GetCurrentTime() OVERRIDE { |
| 71 int poll_interval = policy::DeviceStatusCollector::kPollIntervalSeconds; | 138 int poll_interval = policy::DeviceStatusCollector::kPollIntervalSeconds; |
| 72 return baseline_time_ + | 139 return baseline_time_ + |
| 73 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++); | 140 TimeDelta::FromSeconds(poll_interval * baseline_offset_periods_++); |
| 74 } | 141 } |
| 75 | 142 |
| 76 private: | 143 private: |
| 77 PrefService* local_state_; | |
| 78 | |
| 79 // Baseline time for the fake times returned from GetCurrentTime(). | 144 // Baseline time for the fake times returned from GetCurrentTime(). |
| 80 Time baseline_time_; | 145 Time baseline_time_; |
| 81 | 146 |
| 82 // The number of simulated periods since the baseline time. | 147 // The number of simulated periods since the baseline time. |
| 83 int baseline_offset_periods_; | 148 int baseline_offset_periods_; |
| 84 }; | 149 }; |
| 85 | 150 |
| 86 // Return the total number of active milliseconds contained in a device | 151 // Return the total number of active milliseconds contained in a device |
| 87 // status report. | 152 // status report. |
| 88 int64 GetActiveMilliseconds(em::DeviceStatusReportRequest& status) { | 153 int64 GetActiveMilliseconds(em::DeviceStatusReportRequest& status) { |
| 89 int64 active_milliseconds = 0; | 154 int64 active_milliseconds = 0; |
| 90 for (int i = 0; i < status.active_period_size(); i++) { | 155 for (int i = 0; i < status.active_period_size(); i++) { |
| 91 active_milliseconds += status.active_period(i).active_duration(); | 156 active_milliseconds += status.active_period(i).active_duration(); |
| 92 } | 157 } |
| 93 return active_milliseconds; | 158 return active_milliseconds; |
| 94 } | 159 } |
| 95 | 160 |
| 96 } // namespace | 161 } // namespace |
| 97 | 162 |
| 98 namespace policy { | 163 namespace policy { |
| 99 | 164 |
| 100 using ::testing::_; | |
| 101 using ::testing::NotNull; | 165 using ::testing::NotNull; |
| 102 using ::testing::Return; | 166 using ::testing::Return; |
| 103 using ::testing::SetArgPointee; | 167 using ::testing::SetArgPointee; |
| 104 | 168 |
| 105 class DeviceStatusCollectorTest : public testing::Test { | 169 class DeviceStatusCollectorTest : public testing::Test { |
| 106 public: | 170 public: |
| 107 DeviceStatusCollectorTest() | 171 DeviceStatusCollectorTest() |
| 108 : message_loop_(MessageLoop::TYPE_UI), | 172 : message_loop_(MessageLoop::TYPE_UI), |
| 109 ui_thread_(content::BrowserThread::UI, &message_loop_), | 173 ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 110 file_thread_(content::BrowserThread::FILE, &message_loop_), | 174 file_thread_(content::BrowserThread::FILE, &message_loop_), |
| 111 status_collector_(&prefs_, &statistics_provider_) { | 175 io_thread_(content::BrowserThread::IO, &message_loop_), |
| 176 event_(false, false) { |
| 177 TestingDeviceStatusCollector::RegisterPrefs(&prefs_); |
| 112 | 178 |
| 113 DeviceStatusCollector::RegisterPrefs(&prefs_); | |
| 114 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) | 179 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) |
| 115 .WillRepeatedly(Return(false)); | 180 .WillRepeatedly(Return(false)); |
| 116 | 181 |
| 182 // Remove the real DeviceSettingsProvider and replace it with a stub. |
| 117 cros_settings_ = chromeos::CrosSettings::Get(); | 183 cros_settings_ = chromeos::CrosSettings::Get(); |
| 118 | |
| 119 // Remove the real DeviceSettingsProvider and replace it with a stub. | |
| 120 device_settings_provider_ = | 184 device_settings_provider_ = |
| 121 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); | 185 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); |
| 122 EXPECT_TRUE(device_settings_provider_ != NULL); | 186 EXPECT_TRUE(device_settings_provider_ != NULL); |
| 123 EXPECT_TRUE( | 187 EXPECT_TRUE( |
| 124 cros_settings_->RemoveSettingsProvider(device_settings_provider_)); | 188 cros_settings_->RemoveSettingsProvider(device_settings_provider_)); |
| 125 cros_settings_->AddSettingsProvider(&stub_settings_provider_); | 189 cros_settings_->AddSettingsProvider(&stub_settings_provider_); |
| 190 |
| 191 StartStatusCollector(true); |
| 126 } | 192 } |
| 127 | 193 |
| 128 ~DeviceStatusCollectorTest() { | 194 ~DeviceStatusCollectorTest() { |
| 195 StopStatusCollector(); |
| 196 |
| 129 // Restore the real DeviceSettingsProvider. | 197 // Restore the real DeviceSettingsProvider. |
| 130 EXPECT_TRUE( | 198 EXPECT_TRUE( |
| 131 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_)); | 199 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_)); |
| 132 cros_settings_->AddSettingsProvider(device_settings_provider_); | 200 cros_settings_->AddSettingsProvider(device_settings_provider_); |
| 133 } | 201 } |
| 134 | 202 |
| 203 void GetStatus() { |
| 204 status_.Clear(); |
| 205 status_collector_->GetStatus(&status_); |
| 206 } |
| 207 |
| 208 void StartStatusCollector(bool has_valid_fix) { |
| 209 dependency_factory_ = new TestingDependencyFactory(has_valid_fix); |
| 210 GeolocationArbitrator::SetDependencyFactoryForTest( |
| 211 dependency_factory_.get()); |
| 212 event_.Reset(); |
| 213 geolocation_provider_ = new TestingGeolocationProvider(&event_); |
| 214 status_collector_ = new TestingDeviceStatusCollector( |
| 215 &prefs_, |
| 216 &statistics_provider_, |
| 217 geolocation_provider_); |
| 218 } |
| 219 |
| 220 void StopStatusCollector() { |
| 221 delete status_collector_; |
| 222 status_collector_ = 0; |
| 223 // Allow the DeviceStatusLocationHelper's shutdown sequence to run. |
| 224 message_loop_.RunAllPending(); |
| 225 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); |
| 226 dependency_factory_ = NULL; |
| 227 DefaultSingletonTraits<GeolocationProvider>::Delete(geolocation_provider_); |
| 228 geolocation_provider_ = 0; |
| 229 } |
| 230 |
| 231 void RestartStatusCollector(bool has_valid_fix) { |
| 232 StopStatusCollector(); |
| 233 StartStatusCollector(has_valid_fix); |
| 234 } |
| 235 |
| 236 void CheckThatNoLocationIsReported() { |
| 237 GetStatus(); |
| 238 EXPECT_FALSE(status_.has_device_location()); |
| 239 } |
| 240 |
| 241 void CheckThatAValidLocationIsReported() { |
| 242 // Checks that a location is being reported which matches the fix returned |
| 243 // by the AutoMockLocationProvider. |
| 244 GetStatus(); |
| 245 EXPECT_TRUE(status_.has_device_location()); |
| 246 em::DeviceLocation location = status_.device_location(); |
| 247 if (location.has_error_code()) |
| 248 EXPECT_EQ(0, location.error_code()); |
| 249 EXPECT_TRUE(location.has_latitude()); |
| 250 EXPECT_TRUE(location.has_longitude()); |
| 251 EXPECT_TRUE(location.has_accuracy()); |
| 252 EXPECT_TRUE(location.has_timestamp()); |
| 253 EXPECT_FALSE(location.has_altitude()); |
| 254 EXPECT_FALSE(location.has_altitude_accuracy()); |
| 255 EXPECT_FALSE(location.has_heading()); |
| 256 EXPECT_FALSE(location.has_speed()); |
| 257 EXPECT_FALSE(location.has_error_message()); |
| 258 EXPECT_EQ(4.3, location.latitude()); |
| 259 EXPECT_EQ(-7.8, location.longitude()); |
| 260 EXPECT_EQ(3., location.accuracy()); |
| 261 // Check that the timestamp is not older than ten minutes. |
| 262 EXPECT_TRUE(Time::Time::Now() - |
| 263 base::Time::FromDoubleT(location.timestamp() / 1000.) < |
| 264 base::TimeDelta::FromMinutes(10)); |
| 265 } |
| 266 |
| 267 void CheckThatALocationErrorIsReported() { |
| 268 GetStatus(); |
| 269 EXPECT_TRUE(status_.has_device_location()); |
| 270 em::DeviceLocation location = status_.device_location(); |
| 271 EXPECT_TRUE(location.has_error_code()); |
| 272 EXPECT_EQ(1, location.error_code()); |
| 273 } |
| 274 |
| 135 protected: | 275 protected: |
| 136 // Convenience method. | 276 // Convenience method. |
| 137 int64 ActivePeriodMilliseconds() { | 277 int64 ActivePeriodMilliseconds() { |
| 138 return policy::DeviceStatusCollector::kPollIntervalSeconds * 1000; | 278 return policy::DeviceStatusCollector::kPollIntervalSeconds * 1000; |
| 139 } | 279 } |
| 140 | 280 |
| 141 MessageLoop message_loop_; | 281 MessageLoop message_loop_; |
| 142 content::TestBrowserThread ui_thread_; | 282 content::TestBrowserThread ui_thread_; |
| 143 content::TestBrowserThread file_thread_; | 283 content::TestBrowserThread file_thread_; |
| 284 content::TestBrowserThread io_thread_; |
| 285 |
| 286 scoped_refptr<TestingDependencyFactory> dependency_factory_; |
| 287 base::WaitableEvent event_; |
| 288 TestingGeolocationProvider* geolocation_provider_; |
| 144 | 289 |
| 145 TestingPrefService prefs_; | 290 TestingPrefService prefs_; |
| 146 chromeos::system::MockStatisticsProvider statistics_provider_; | 291 chromeos::system::MockStatisticsProvider statistics_provider_; |
| 147 TestingDeviceStatusCollector status_collector_; | 292 TestingDeviceStatusCollector* status_collector_; |
| 148 em::DeviceStatusReportRequest status_; | 293 em::DeviceStatusReportRequest status_; |
| 149 chromeos::CrosSettings* cros_settings_; | 294 chromeos::CrosSettings* cros_settings_; |
| 150 chromeos::CrosSettingsProvider* device_settings_provider_; | 295 chromeos::CrosSettingsProvider* device_settings_provider_; |
| 151 chromeos::StubCrosSettingsProvider stub_settings_provider_; | 296 chromeos::StubCrosSettingsProvider stub_settings_provider_; |
| 152 }; | 297 }; |
| 153 | 298 |
| 154 TEST_F(DeviceStatusCollectorTest, AllIdle) { | 299 TEST_F(DeviceStatusCollectorTest, AllIdle) { |
| 155 IdleState test_states[] = { | 300 IdleState test_states[] = { |
| 156 IDLE_STATE_IDLE, | 301 IDLE_STATE_IDLE, |
| 157 IDLE_STATE_IDLE, | 302 IDLE_STATE_IDLE, |
| 158 IDLE_STATE_IDLE | 303 IDLE_STATE_IDLE |
| 159 }; | 304 }; |
| 160 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 305 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 161 | 306 |
| 162 // Test reporting with no data. | 307 // Test reporting with no data. |
| 163 status_collector_.GetStatus(&status_); | 308 GetStatus(); |
| 164 EXPECT_EQ(0, status_.active_period_size()); | 309 EXPECT_EQ(0, status_.active_period_size()); |
| 165 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 310 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
| 166 | 311 |
| 167 // Test reporting with a single idle sample. | 312 // Test reporting with a single idle sample. |
| 168 status_collector_.Simulate(test_states, 1); | 313 status_collector_->Simulate(test_states, 1); |
| 169 status_collector_.GetStatus(&status_); | 314 GetStatus(); |
| 170 EXPECT_EQ(0, status_.active_period_size()); | 315 EXPECT_EQ(0, status_.active_period_size()); |
| 171 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 316 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
| 172 | 317 |
| 173 // Test reporting with multiple consecutive idle samples. | 318 // Test reporting with multiple consecutive idle samples. |
| 174 status_collector_.Simulate(test_states, | 319 status_collector_->Simulate(test_states, |
| 175 sizeof(test_states) / sizeof(IdleState)); | 320 sizeof(test_states) / sizeof(IdleState)); |
| 176 status_collector_.GetStatus(&status_); | 321 GetStatus(); |
| 177 EXPECT_EQ(0, status_.active_period_size()); | 322 EXPECT_EQ(0, status_.active_period_size()); |
| 178 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 323 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
| 179 } | 324 } |
| 180 | 325 |
| 181 TEST_F(DeviceStatusCollectorTest, AllActive) { | 326 TEST_F(DeviceStatusCollectorTest, AllActive) { |
| 182 IdleState test_states[] = { | 327 IdleState test_states[] = { |
| 183 IDLE_STATE_ACTIVE, | 328 IDLE_STATE_ACTIVE, |
| 184 IDLE_STATE_ACTIVE, | 329 IDLE_STATE_ACTIVE, |
| 185 IDLE_STATE_ACTIVE | 330 IDLE_STATE_ACTIVE |
| 186 }; | 331 }; |
| 187 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 332 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 188 | 333 |
| 189 // Test a single active sample. | 334 // Test a single active sample. |
| 190 status_collector_.Simulate(test_states, 1); | 335 status_collector_->Simulate(test_states, 1); |
| 191 status_collector_.GetStatus(&status_); | 336 GetStatus(); |
| 192 EXPECT_EQ(1, status_.active_period_size()); | 337 EXPECT_EQ(1, status_.active_period_size()); |
| 193 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 338 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
| 194 status_.clear_active_period(); // Clear the result protobuf. | 339 status_.clear_active_period(); // Clear the result protobuf. |
| 195 | 340 |
| 196 // Test multiple consecutive active samples. | 341 // Test multiple consecutive active samples. |
| 197 status_collector_.Simulate(test_states, | 342 status_collector_->Simulate(test_states, |
| 198 sizeof(test_states) / sizeof(IdleState)); | 343 sizeof(test_states) / sizeof(IdleState)); |
| 199 status_collector_.GetStatus(&status_); | 344 GetStatus(); |
| 200 EXPECT_EQ(1, status_.active_period_size()); | 345 EXPECT_EQ(1, status_.active_period_size()); |
| 201 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 346 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
| 202 } | 347 } |
| 203 | 348 |
| 204 TEST_F(DeviceStatusCollectorTest, MixedStates) { | 349 TEST_F(DeviceStatusCollectorTest, MixedStates) { |
| 205 IdleState test_states[] = { | 350 IdleState test_states[] = { |
| 206 IDLE_STATE_ACTIVE, | 351 IDLE_STATE_ACTIVE, |
| 207 IDLE_STATE_IDLE, | 352 IDLE_STATE_IDLE, |
| 208 IDLE_STATE_ACTIVE, | 353 IDLE_STATE_ACTIVE, |
| 209 IDLE_STATE_ACTIVE, | 354 IDLE_STATE_ACTIVE, |
| 210 IDLE_STATE_IDLE, | 355 IDLE_STATE_IDLE, |
| 211 IDLE_STATE_IDLE, | 356 IDLE_STATE_IDLE, |
| 212 IDLE_STATE_ACTIVE | 357 IDLE_STATE_ACTIVE |
| 213 }; | 358 }; |
| 214 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 359 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 215 status_collector_.Simulate(test_states, | 360 status_collector_->Simulate(test_states, |
| 216 sizeof(test_states) / sizeof(IdleState)); | 361 sizeof(test_states) / sizeof(IdleState)); |
| 217 status_collector_.GetStatus(&status_); | 362 GetStatus(); |
| 218 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 363 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
| 219 } | 364 } |
| 220 | 365 |
| 221 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { | 366 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { |
| 222 IdleState test_states[] = { | 367 IdleState test_states[] = { |
| 223 IDLE_STATE_ACTIVE, | 368 IDLE_STATE_ACTIVE, |
| 224 IDLE_STATE_IDLE, | 369 IDLE_STATE_IDLE, |
| 225 IDLE_STATE_ACTIVE, | 370 IDLE_STATE_ACTIVE, |
| 226 IDLE_STATE_ACTIVE, | 371 IDLE_STATE_ACTIVE, |
| 227 IDLE_STATE_IDLE, | 372 IDLE_STATE_IDLE, |
| 228 IDLE_STATE_IDLE | 373 IDLE_STATE_IDLE |
| 229 }; | 374 }; |
| 230 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 375 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 231 status_collector_.Simulate(test_states, | 376 status_collector_->Simulate(test_states, |
| 232 sizeof(test_states) / sizeof(IdleState)); | 377 sizeof(test_states) / sizeof(IdleState)); |
| 233 | 378 |
| 234 // Process the list a second time with a different collector. | 379 // Process the list a second time after restarting the collector. It should be |
| 235 // It should be able to count the active periods found by the first | 380 // able to count the active periods found by the original collector, because |
| 236 // collector, because the results are stored in a pref. | 381 // the results are stored in a pref. |
| 237 TestingDeviceStatusCollector second_collector(&prefs_, | 382 RestartStatusCollector(false); |
| 238 &statistics_provider_); | 383 status_collector_->Simulate(test_states, |
| 239 second_collector.Simulate(test_states, | 384 sizeof(test_states) / sizeof(IdleState)); |
| 240 sizeof(test_states) / sizeof(IdleState)); | |
| 241 | 385 |
| 242 second_collector.GetStatus(&status_); | 386 GetStatus(); |
| 243 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 387 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
| 244 } | 388 } |
| 245 | 389 |
| 246 TEST_F(DeviceStatusCollectorTest, Times) { | 390 TEST_F(DeviceStatusCollectorTest, Times) { |
| 247 IdleState test_states[] = { | 391 IdleState test_states[] = { |
| 248 IDLE_STATE_ACTIVE, | 392 IDLE_STATE_ACTIVE, |
| 249 IDLE_STATE_IDLE, | 393 IDLE_STATE_IDLE, |
| 250 IDLE_STATE_ACTIVE, | 394 IDLE_STATE_ACTIVE, |
| 251 IDLE_STATE_ACTIVE, | 395 IDLE_STATE_ACTIVE, |
| 252 IDLE_STATE_IDLE, | 396 IDLE_STATE_IDLE, |
| 253 IDLE_STATE_IDLE | 397 IDLE_STATE_IDLE |
| 254 }; | 398 }; |
| 255 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 399 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 256 status_collector_.Simulate(test_states, | 400 status_collector_->Simulate(test_states, |
| 257 sizeof(test_states) / sizeof(IdleState)); | 401 sizeof(test_states) / sizeof(IdleState)); |
| 258 status_collector_.GetStatus(&status_); | 402 GetStatus(); |
| 259 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 403 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
| 260 } | 404 } |
| 261 | 405 |
| 262 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { | 406 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { |
| 263 IdleState test_states[] = { | 407 IdleState test_states[] = { |
| 264 IDLE_STATE_ACTIVE, | 408 IDLE_STATE_ACTIVE, |
| 265 IDLE_STATE_IDLE | 409 IDLE_STATE_IDLE |
| 266 }; | 410 }; |
| 267 unsigned int max_days = 10; | 411 unsigned int max_days = 10; |
| 268 | 412 |
| 269 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 413 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 270 status_collector_.set_max_stored_past_activity_days(max_days - 1); | 414 status_collector_->set_max_stored_past_activity_days(max_days - 1); |
| 271 status_collector_.set_max_stored_future_activity_days(1); | 415 status_collector_->set_max_stored_future_activity_days(1); |
| 272 Time baseline = Time::Now().LocalMidnight(); | 416 Time baseline = Time::Now().LocalMidnight(); |
| 273 | 417 |
| 274 // Simulate 12 active periods. | 418 // Simulate 12 active periods. |
| 275 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { | 419 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { |
| 276 status_collector_.Simulate(test_states, | 420 status_collector_->Simulate(test_states, |
| 277 sizeof(test_states) / sizeof(IdleState)); | 421 sizeof(test_states) / sizeof(IdleState)); |
| 278 // Advance the simulated clock by a day. | 422 // Advance the simulated clock by a day. |
| 279 baseline += TimeDelta::FromDays(1); | 423 baseline += TimeDelta::FromDays(1); |
| 280 status_collector_.SetBaselineTime(baseline); | 424 status_collector_->SetBaselineTime(baseline); |
| 281 } | 425 } |
| 282 | 426 |
| 283 // Check that we don't exceed the max number of periods. | 427 // Check that we don't exceed the max number of periods. |
| 284 status_collector_.GetStatus(&status_); | 428 GetStatus(); |
| 285 EXPECT_EQ(static_cast<int>(max_days), status_.active_period_size()); | 429 EXPECT_EQ(static_cast<int>(max_days), status_.active_period_size()); |
| 286 | 430 |
| 287 // Simulate some future times. | 431 // Simulate some future times. |
| 288 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { | 432 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { |
| 289 status_collector_.Simulate(test_states, | 433 status_collector_->Simulate(test_states, |
| 290 sizeof(test_states) / sizeof(IdleState)); | 434 sizeof(test_states) / sizeof(IdleState)); |
| 291 // Advance the simulated clock by a day. | 435 // Advance the simulated clock by a day. |
| 292 baseline += TimeDelta::FromDays(1); | 436 baseline += TimeDelta::FromDays(1); |
| 293 status_collector_.SetBaselineTime(baseline); | 437 status_collector_->SetBaselineTime(baseline); |
| 294 } | 438 } |
| 295 // Set the clock back so the previous simulated times are in the future. | 439 // Set the clock back so the previous simulated times are in the future. |
| 296 baseline -= TimeDelta::FromDays(20); | 440 baseline -= TimeDelta::FromDays(20); |
| 297 status_collector_.SetBaselineTime(baseline); | 441 status_collector_->SetBaselineTime(baseline); |
| 298 | 442 |
| 299 // Collect one more data point to trigger pruning. | 443 // Collect one more data point to trigger pruning. |
| 300 status_collector_.Simulate(test_states, 1); | 444 status_collector_->Simulate(test_states, 1); |
| 301 | 445 |
| 302 // Check that we don't exceed the max number of periods. | 446 // Check that we don't exceed the max number of periods. |
| 303 status_.clear_active_period(); | 447 status_.clear_active_period(); |
| 304 status_collector_.GetStatus(&status_); | 448 GetStatus(); |
| 305 EXPECT_LT(status_.active_period_size(), static_cast<int>(max_days)); | 449 EXPECT_LT(status_.active_period_size(), static_cast<int>(max_days)); |
| 306 } | 450 } |
| 307 | 451 |
| 308 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { | 452 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { |
| 309 // If the pref for collecting device activity times isn't explicitly turned | 453 // If the pref for collecting device activity times isn't explicitly turned |
| 310 // on, no data on activity times should be reported. | 454 // on, no data on activity times should be reported. |
| 311 | 455 |
| 312 IdleState test_states[] = { | 456 IdleState test_states[] = { |
| 313 IDLE_STATE_ACTIVE, | 457 IDLE_STATE_ACTIVE, |
| 314 IDLE_STATE_ACTIVE, | 458 IDLE_STATE_ACTIVE, |
| 315 IDLE_STATE_ACTIVE | 459 IDLE_STATE_ACTIVE |
| 316 }; | 460 }; |
| 317 status_collector_.Simulate(test_states, | 461 status_collector_->Simulate(test_states, |
| 318 sizeof(test_states) / sizeof(IdleState)); | 462 sizeof(test_states) / sizeof(IdleState)); |
| 319 status_collector_.GetStatus(&status_); | 463 GetStatus(); |
| 320 EXPECT_EQ(0, status_.active_period_size()); | 464 EXPECT_EQ(0, status_.active_period_size()); |
| 321 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 465 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
| 322 } | 466 } |
| 323 | 467 |
| 324 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) { | 468 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) { |
| 325 IdleState test_states[] = { | 469 IdleState test_states[] = { |
| 326 IDLE_STATE_ACTIVE | 470 IDLE_STATE_ACTIVE |
| 327 }; | 471 }; |
| 328 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 472 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 329 | 473 |
| 330 // Set the baseline time to 10 seconds after midnight. | 474 // Set the baseline time to 10 seconds after midnight. |
| 331 status_collector_.SetBaselineTime( | 475 status_collector_->SetBaselineTime( |
| 332 Time::Now().LocalMidnight() + TimeDelta::FromSeconds(10)); | 476 Time::Now().LocalMidnight() + TimeDelta::FromSeconds(10)); |
| 333 | 477 |
| 334 status_collector_.Simulate(test_states, 1); | 478 status_collector_->Simulate(test_states, 1); |
| 335 status_collector_.GetStatus(&status_); | 479 GetStatus(); |
| 336 ASSERT_EQ(2, status_.active_period_size()); | 480 ASSERT_EQ(2, status_.active_period_size()); |
| 337 | 481 |
| 338 em::ActiveTimePeriod period0 = status_.active_period(0); | 482 em::ActiveTimePeriod period0 = status_.active_period(0); |
| 339 em::ActiveTimePeriod period1 = status_.active_period(1); | 483 em::ActiveTimePeriod period1 = status_.active_period(1); |
| 340 EXPECT_EQ(ActivePeriodMilliseconds() - 10000, period0.active_duration()); | 484 EXPECT_EQ(ActivePeriodMilliseconds() - 10000, period0.active_duration()); |
| 341 EXPECT_EQ(10000, period1.active_duration()); | 485 EXPECT_EQ(10000, period1.active_duration()); |
| 342 | 486 |
| 343 em::TimePeriod time_period0 = period0.time_period(); | 487 em::TimePeriod time_period0 = period0.time_period(); |
| 344 em::TimePeriod time_period1 = period1.time_period(); | 488 em::TimePeriod time_period1 = period1.time_period(); |
| 345 | 489 |
| 346 EXPECT_EQ(time_period0.end_timestamp(), time_period1.start_timestamp()); | 490 EXPECT_EQ(time_period0.end_timestamp(), time_period1.start_timestamp()); |
| 347 | 491 |
| 348 // Ensure that the start and end times for the period are a day apart. | 492 // Ensure that the start and end times for the period are a day apart. |
| 349 EXPECT_EQ(time_period0.end_timestamp() - time_period0.start_timestamp(), | 493 EXPECT_EQ(time_period0.end_timestamp() - time_period0.start_timestamp(), |
| 350 kMillisecondsPerDay); | 494 kMillisecondsPerDay); |
| 351 EXPECT_EQ(time_period1.end_timestamp() - time_period1.start_timestamp(), | 495 EXPECT_EQ(time_period1.end_timestamp() - time_period1.start_timestamp(), |
| 352 kMillisecondsPerDay); | 496 kMillisecondsPerDay); |
| 353 } | 497 } |
| 354 | 498 |
| 355 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { | 499 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { |
| 356 // Test that boot mode data is not reported if the pref is not turned on. | 500 // Test that boot mode data is not reported if the pref is not turned on. |
| 357 status_collector_.GetStatus(&status_); | |
| 358 EXPECT_EQ(false, status_.has_boot_mode()); | |
| 359 | |
| 360 EXPECT_CALL(statistics_provider_, | 501 EXPECT_CALL(statistics_provider_, |
| 361 GetMachineStatistic("devsw_boot", NotNull())) | 502 GetMachineStatistic("devsw_boot", NotNull())) |
| 362 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); | 503 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); |
| 363 EXPECT_EQ(false, status_.has_boot_mode()); | 504 GetStatus(); |
| 505 EXPECT_FALSE(status_.has_boot_mode()); |
| 364 | 506 |
| 365 // Turn the pref on, and check that the status is reported iff the | 507 // Turn the pref on, and check that the status is reported iff the |
| 366 // statistics provider returns valid data. | 508 // statistics provider returns valid data. |
| 367 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true); | 509 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true); |
| 368 | 510 |
| 369 EXPECT_CALL(statistics_provider_, | 511 EXPECT_CALL(statistics_provider_, |
| 370 GetMachineStatistic("devsw_boot", NotNull())) | 512 GetMachineStatistic("devsw_boot", NotNull())) |
| 371 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); | 513 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); |
| 372 status_collector_.GetStatus(&status_); | 514 GetStatus(); |
| 373 EXPECT_EQ(false, status_.has_boot_mode()); | 515 EXPECT_FALSE(status_.has_boot_mode()); |
| 374 | 516 |
| 375 EXPECT_CALL(statistics_provider_, | 517 EXPECT_CALL(statistics_provider_, |
| 376 GetMachineStatistic("devsw_boot", NotNull())) | 518 GetMachineStatistic("devsw_boot", NotNull())) |
| 377 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); | 519 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); |
| 378 status_collector_.GetStatus(&status_); | 520 GetStatus(); |
| 379 EXPECT_EQ(false, status_.has_boot_mode()); | 521 EXPECT_FALSE(status_.has_boot_mode()); |
| 380 | 522 |
| 381 EXPECT_CALL(statistics_provider_, | 523 EXPECT_CALL(statistics_provider_, |
| 382 GetMachineStatistic("devsw_boot", NotNull())) | 524 GetMachineStatistic("devsw_boot", NotNull())) |
| 383 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); | 525 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); |
| 384 status_collector_.GetStatus(&status_); | 526 GetStatus(); |
| 385 EXPECT_EQ("Verified", status_.boot_mode()); | 527 EXPECT_EQ("Verified", status_.boot_mode()); |
| 386 | 528 |
| 387 EXPECT_CALL(statistics_provider_, | 529 EXPECT_CALL(statistics_provider_, |
| 388 GetMachineStatistic("devsw_boot", NotNull())) | 530 GetMachineStatistic("devsw_boot", NotNull())) |
| 389 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); | 531 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); |
| 390 status_collector_.GetStatus(&status_); | 532 GetStatus(); |
| 391 EXPECT_EQ("Dev", status_.boot_mode()); | 533 EXPECT_EQ("Dev", status_.boot_mode()); |
| 392 } | 534 } |
| 393 | 535 |
| 394 TEST_F(DeviceStatusCollectorTest, VersionInfo) { | 536 TEST_F(DeviceStatusCollectorTest, VersionInfo) { |
| 395 // When the pref to collect this data is not enabled, expect that none of | 537 // When the pref to collect this data is not enabled, expect that none of |
| 396 // the fields are present in the protobuf. | 538 // the fields are present in the protobuf. |
| 397 status_collector_.GetStatus(&status_); | 539 GetStatus(); |
| 398 EXPECT_EQ(false, status_.has_browser_version()); | 540 EXPECT_FALSE(status_.has_browser_version()); |
| 399 EXPECT_EQ(false, status_.has_os_version()); | 541 EXPECT_FALSE(status_.has_os_version()); |
| 400 EXPECT_EQ(false, status_.has_firmware_version()); | 542 EXPECT_FALSE(status_.has_firmware_version()); |
| 401 | 543 |
| 402 cros_settings_->SetBoolean(chromeos::kReportDeviceVersionInfo, true); | 544 cros_settings_->SetBoolean(chromeos::kReportDeviceVersionInfo, true); |
| 403 status_collector_.GetStatus(&status_); | 545 GetStatus(); |
| 404 EXPECT_EQ(true, status_.has_browser_version()); | 546 EXPECT_TRUE(status_.has_browser_version()); |
| 405 EXPECT_EQ(true, status_.has_os_version()); | 547 EXPECT_TRUE(status_.has_os_version()); |
| 406 EXPECT_EQ(true, status_.has_firmware_version()); | 548 EXPECT_TRUE(status_.has_firmware_version()); |
| 407 | 549 |
| 408 // Check that the browser version is not empty. OS version & firmware | 550 // Check that the browser version is not empty. OS version & firmware |
| 409 // don't have any reasonable values inside the unit test, so those | 551 // don't have any reasonable values inside the unit test, so those |
| 410 // aren't checked. | 552 // aren't checked. |
| 411 EXPECT_NE("", status_.browser_version()); | 553 EXPECT_NE("", status_.browser_version()); |
| 412 } | 554 } |
| 413 | 555 |
| 556 |
| 557 TEST_F(DeviceStatusCollectorTest, Location) { |
| 558 // Check that when device location reporting is disabled, no location is |
| 559 // reported. |
| 560 CheckThatNoLocationIsReported(); |
| 561 |
| 562 // Check that when device location reporting is enabled and the geolocation |
| 563 // stack returns a valid fix, the location is reported. |
| 564 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); |
| 565 // Allow the geolocation stack to start up. |
| 566 message_loop_.RunAllPending(); |
| 567 // Wait for success in acquiring a location fix to be returned on the |
| 568 // geolocation thread. |
| 569 event_.Wait(); |
| 570 // Allow the result to propagate to the UI thread. |
| 571 message_loop_.RunAllPending(); |
| 572 |
| 573 CheckThatAValidLocationIsReported(); |
| 574 |
| 575 // Restart the status collector with a mock GeolocationProvider that returns |
| 576 // no valid fix. Check that the last known location was stored in local state |
| 577 // and is reported instead. |
| 578 RestartStatusCollector(false); |
| 579 CheckThatAValidLocationIsReported(); |
| 580 |
| 581 // Check that after disabling location reporting again, the last known |
| 582 // location has been celared from local state and is no longer reported. |
| 583 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, false); |
| 584 // Allow the new pref to propagate to the status collector. |
| 585 message_loop_.RunAllPending(); |
| 586 |
| 587 EXPECT_TRUE(prefs_.GetDictionary(prefs::kDeviceLocation)->empty()); |
| 588 |
| 589 CheckThatNoLocationIsReported(); |
| 590 |
| 591 // Check that after enabling location reporting again, an error is reported |
| 592 // since the location is unknown. |
| 593 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true); |
| 594 // Allow the new pref to propagate to the status collector and the geolocation |
| 595 // stack to start up.. |
| 596 message_loop_.RunAllPending(); |
| 597 // Wait for failure in acquiring a location fix to be returned on the |
| 598 // geolocation thread. |
| 599 event_.Wait(); |
| 600 // Allow the result to propagate to the UI thread. |
| 601 message_loop_.RunAllPending(); |
| 602 |
| 603 CheckThatALocationErrorIsReported(); |
| 604 } |
| 605 |
| 414 } // namespace policy | 606 } // namespace policy |
| OLD | NEW |