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