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