Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: chrome/browser/policy/device_status_collector_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698