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

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: New implementation on top of a new content geolocation API. Created 8 years, 7 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/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
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 fix returned
177 // by the AutoMockLocationProvider.
Joao da Silva 2012/05/03 19:04:08 AutoMockLocationProvider -> MockPositionUpdateRequ
bartfab (slow) 2012/05/04 09:14:57 Actually, SetMockPositionToReturnNext(). This comm
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(0, location.error_code());
Joao da Silva 2012/05/03 19:04:08 nit: use the constant instead of 0?
bartfab (slow) 2012/05/04 09:14:57 Done.
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_EQ(4.3, location.latitude());
193 EXPECT_EQ(-7.8, location.longitude());
194 EXPECT_EQ(3., location.accuracy());
Joao da Silva 2012/05/03 19:04:08 Use EXPECT_DOUBLE_EQ for these 3
bartfab (slow) 2012/05/04 09:14:57 Done.
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(1, location.error_code());
Joao da Silva 2012/05/03 19:04:08 nit: use the constant instead of 1?
bartfab (slow) 2012/05/04 09:14:57 Done.
206 }
207
135 protected: 208 protected:
136 // Convenience method. 209 // Convenience method.
137 int64 ActivePeriodMilliseconds() { 210 int64 ActivePeriodMilliseconds() {
138 return policy::DeviceStatusCollector::kPollIntervalSeconds * 1000; 211 return policy::DeviceStatusCollector::kIdlePollIntervalSeconds * 1000;
139 } 212 }
140 213
141 MessageLoop message_loop_; 214 MessageLoop message_loop_;
142 content::TestBrowserThread ui_thread_; 215 content::TestBrowserThread ui_thread_;
143 content::TestBrowserThread file_thread_; 216 content::TestBrowserThread file_thread_;
217 content::TestBrowserThread io_thread_;
144 218
145 TestingPrefService prefs_; 219 TestingPrefService prefs_;
146 chromeos::system::MockStatisticsProvider statistics_provider_; 220 chromeos::system::MockStatisticsProvider statistics_provider_;
147 TestingDeviceStatusCollector status_collector_; 221 scoped_ptr<TestingDeviceStatusCollector> status_collector_;
148 em::DeviceStatusReportRequest status_; 222 em::DeviceStatusReportRequest status_;
149 chromeos::CrosSettings* cros_settings_; 223 chromeos::CrosSettings* cros_settings_;
150 chromeos::CrosSettingsProvider* device_settings_provider_; 224 chromeos::CrosSettingsProvider* device_settings_provider_;
151 chromeos::StubCrosSettingsProvider stub_settings_provider_; 225 chromeos::StubCrosSettingsProvider stub_settings_provider_;
152 }; 226 };
153 227
154 TEST_F(DeviceStatusCollectorTest, AllIdle) { 228 TEST_F(DeviceStatusCollectorTest, AllIdle) {
155 IdleState test_states[] = { 229 IdleState test_states[] = {
156 IDLE_STATE_IDLE, 230 IDLE_STATE_IDLE,
157 IDLE_STATE_IDLE, 231 IDLE_STATE_IDLE,
158 IDLE_STATE_IDLE 232 IDLE_STATE_IDLE
159 }; 233 };
160 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 234 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
161 235
162 // Test reporting with no data. 236 // Test reporting with no data.
163 status_collector_.GetStatus(&status_); 237 GetStatus();
164 EXPECT_EQ(0, status_.active_period_size()); 238 EXPECT_EQ(0, status_.active_period_size());
165 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 239 EXPECT_EQ(0, GetActiveMilliseconds(status_));
166 240
167 // Test reporting with a single idle sample. 241 // Test reporting with a single idle sample.
168 status_collector_.Simulate(test_states, 1); 242 status_collector_->Simulate(test_states, 1);
169 status_collector_.GetStatus(&status_); 243 GetStatus();
170 EXPECT_EQ(0, status_.active_period_size()); 244 EXPECT_EQ(0, status_.active_period_size());
171 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 245 EXPECT_EQ(0, GetActiveMilliseconds(status_));
172 246
173 // Test reporting with multiple consecutive idle samples. 247 // Test reporting with multiple consecutive idle samples.
174 status_collector_.Simulate(test_states, 248 status_collector_->Simulate(test_states,
175 sizeof(test_states) / sizeof(IdleState)); 249 sizeof(test_states) / sizeof(IdleState));
176 status_collector_.GetStatus(&status_); 250 GetStatus();
177 EXPECT_EQ(0, status_.active_period_size()); 251 EXPECT_EQ(0, status_.active_period_size());
178 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 252 EXPECT_EQ(0, GetActiveMilliseconds(status_));
179 } 253 }
180 254
181 TEST_F(DeviceStatusCollectorTest, AllActive) { 255 TEST_F(DeviceStatusCollectorTest, AllActive) {
182 IdleState test_states[] = { 256 IdleState test_states[] = {
183 IDLE_STATE_ACTIVE, 257 IDLE_STATE_ACTIVE,
184 IDLE_STATE_ACTIVE, 258 IDLE_STATE_ACTIVE,
185 IDLE_STATE_ACTIVE 259 IDLE_STATE_ACTIVE
186 }; 260 };
187 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 261 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
188 262
189 // Test a single active sample. 263 // Test a single active sample.
190 status_collector_.Simulate(test_states, 1); 264 status_collector_->Simulate(test_states, 1);
191 status_collector_.GetStatus(&status_); 265 GetStatus();
192 EXPECT_EQ(1, status_.active_period_size()); 266 EXPECT_EQ(1, status_.active_period_size());
193 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 267 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
194 status_.clear_active_period(); // Clear the result protobuf. 268 status_.clear_active_period(); // Clear the result protobuf.
195 269
196 // Test multiple consecutive active samples. 270 // Test multiple consecutive active samples.
197 status_collector_.Simulate(test_states, 271 status_collector_->Simulate(test_states,
198 sizeof(test_states) / sizeof(IdleState)); 272 sizeof(test_states) / sizeof(IdleState));
199 status_collector_.GetStatus(&status_); 273 GetStatus();
200 EXPECT_EQ(1, status_.active_period_size()); 274 EXPECT_EQ(1, status_.active_period_size());
201 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 275 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
202 } 276 }
203 277
204 TEST_F(DeviceStatusCollectorTest, MixedStates) { 278 TEST_F(DeviceStatusCollectorTest, MixedStates) {
205 IdleState test_states[] = { 279 IdleState test_states[] = {
206 IDLE_STATE_ACTIVE, 280 IDLE_STATE_ACTIVE,
207 IDLE_STATE_IDLE, 281 IDLE_STATE_IDLE,
208 IDLE_STATE_ACTIVE, 282 IDLE_STATE_ACTIVE,
209 IDLE_STATE_ACTIVE, 283 IDLE_STATE_ACTIVE,
210 IDLE_STATE_IDLE, 284 IDLE_STATE_IDLE,
211 IDLE_STATE_IDLE, 285 IDLE_STATE_IDLE,
212 IDLE_STATE_ACTIVE 286 IDLE_STATE_ACTIVE
213 }; 287 };
214 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 288 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
215 status_collector_.Simulate(test_states, 289 status_collector_->Simulate(test_states,
216 sizeof(test_states) / sizeof(IdleState)); 290 sizeof(test_states) / sizeof(IdleState));
217 status_collector_.GetStatus(&status_); 291 GetStatus();
218 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 292 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
219 } 293 }
220 294
221 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { 295 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) {
222 IdleState test_states[] = { 296 IdleState test_states[] = {
223 IDLE_STATE_ACTIVE, 297 IDLE_STATE_ACTIVE,
224 IDLE_STATE_IDLE, 298 IDLE_STATE_IDLE,
225 IDLE_STATE_ACTIVE, 299 IDLE_STATE_ACTIVE,
226 IDLE_STATE_ACTIVE, 300 IDLE_STATE_ACTIVE,
227 IDLE_STATE_IDLE, 301 IDLE_STATE_IDLE,
228 IDLE_STATE_IDLE 302 IDLE_STATE_IDLE
229 }; 303 };
230 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 304 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
231 status_collector_.Simulate(test_states, 305 status_collector_->Simulate(test_states,
232 sizeof(test_states) / sizeof(IdleState)); 306 sizeof(test_states) / sizeof(IdleState));
233 307
234 // Process the list a second time with a different collector. 308 // 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 309 // able to count the active periods found by the original collector, because
236 // collector, because the results are stored in a pref. 310 // the results are stored in a pref.
237 TestingDeviceStatusCollector second_collector(&prefs_, 311 RestartStatusCollector();
238 &statistics_provider_); 312 status_collector_->Simulate(test_states,
239 second_collector.Simulate(test_states, 313 sizeof(test_states) / sizeof(IdleState));
240 sizeof(test_states) / sizeof(IdleState));
241 314
242 second_collector.GetStatus(&status_); 315 GetStatus();
243 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 316 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
244 } 317 }
245 318
246 TEST_F(DeviceStatusCollectorTest, Times) { 319 TEST_F(DeviceStatusCollectorTest, Times) {
247 IdleState test_states[] = { 320 IdleState test_states[] = {
248 IDLE_STATE_ACTIVE, 321 IDLE_STATE_ACTIVE,
249 IDLE_STATE_IDLE, 322 IDLE_STATE_IDLE,
250 IDLE_STATE_ACTIVE, 323 IDLE_STATE_ACTIVE,
251 IDLE_STATE_ACTIVE, 324 IDLE_STATE_ACTIVE,
252 IDLE_STATE_IDLE, 325 IDLE_STATE_IDLE,
253 IDLE_STATE_IDLE 326 IDLE_STATE_IDLE
254 }; 327 };
255 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 328 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
256 status_collector_.Simulate(test_states, 329 status_collector_->Simulate(test_states,
257 sizeof(test_states) / sizeof(IdleState)); 330 sizeof(test_states) / sizeof(IdleState));
258 status_collector_.GetStatus(&status_); 331 GetStatus();
259 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); 332 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_));
260 } 333 }
261 334
262 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { 335 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) {
263 IdleState test_states[] = { 336 IdleState test_states[] = {
264 IDLE_STATE_ACTIVE, 337 IDLE_STATE_ACTIVE,
265 IDLE_STATE_IDLE 338 IDLE_STATE_IDLE
266 }; 339 };
267 unsigned int max_days = 10; 340 unsigned int max_days = 10;
268 341
269 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 342 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
270 status_collector_.set_max_stored_past_activity_days(max_days - 1); 343 status_collector_->set_max_stored_past_activity_days(max_days - 1);
271 status_collector_.set_max_stored_future_activity_days(1); 344 status_collector_->set_max_stored_future_activity_days(1);
272 Time baseline = Time::Now().LocalMidnight(); 345 Time baseline = Time::Now().LocalMidnight();
273 346
274 // Simulate 12 active periods. 347 // Simulate 12 active periods.
275 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { 348 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) {
276 status_collector_.Simulate(test_states, 349 status_collector_->Simulate(test_states,
277 sizeof(test_states) / sizeof(IdleState)); 350 sizeof(test_states) / sizeof(IdleState));
278 // Advance the simulated clock by a day. 351 // Advance the simulated clock by a day.
279 baseline += TimeDelta::FromDays(1); 352 baseline += TimeDelta::FromDays(1);
280 status_collector_.SetBaselineTime(baseline); 353 status_collector_->SetBaselineTime(baseline);
281 } 354 }
282 355
283 // Check that we don't exceed the max number of periods. 356 // Check that we don't exceed the max number of periods.
284 status_collector_.GetStatus(&status_); 357 GetStatus();
285 EXPECT_EQ(static_cast<int>(max_days), status_.active_period_size()); 358 EXPECT_EQ(static_cast<int>(max_days), status_.active_period_size());
286 359
287 // Simulate some future times. 360 // Simulate some future times.
288 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) { 361 for (int i = 0; i < static_cast<int>(max_days) + 2; i++) {
289 status_collector_.Simulate(test_states, 362 status_collector_->Simulate(test_states,
290 sizeof(test_states) / sizeof(IdleState)); 363 sizeof(test_states) / sizeof(IdleState));
291 // Advance the simulated clock by a day. 364 // Advance the simulated clock by a day.
292 baseline += TimeDelta::FromDays(1); 365 baseline += TimeDelta::FromDays(1);
293 status_collector_.SetBaselineTime(baseline); 366 status_collector_->SetBaselineTime(baseline);
294 } 367 }
295 // Set the clock back so the previous simulated times are in the future. 368 // Set the clock back so the previous simulated times are in the future.
296 baseline -= TimeDelta::FromDays(20); 369 baseline -= TimeDelta::FromDays(20);
297 status_collector_.SetBaselineTime(baseline); 370 status_collector_->SetBaselineTime(baseline);
298 371
299 // Collect one more data point to trigger pruning. 372 // Collect one more data point to trigger pruning.
300 status_collector_.Simulate(test_states, 1); 373 status_collector_->Simulate(test_states, 1);
301 374
302 // Check that we don't exceed the max number of periods. 375 // Check that we don't exceed the max number of periods.
303 status_.clear_active_period(); 376 status_.clear_active_period();
304 status_collector_.GetStatus(&status_); 377 GetStatus();
305 EXPECT_LT(status_.active_period_size(), static_cast<int>(max_days)); 378 EXPECT_LT(status_.active_period_size(), static_cast<int>(max_days));
306 } 379 }
307 380
308 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { 381 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) {
309 // If the pref for collecting device activity times isn't explicitly turned 382 // If the pref for collecting device activity times isn't explicitly turned
310 // on, no data on activity times should be reported. 383 // on, no data on activity times should be reported.
311 384
312 IdleState test_states[] = { 385 IdleState test_states[] = {
313 IDLE_STATE_ACTIVE, 386 IDLE_STATE_ACTIVE,
314 IDLE_STATE_ACTIVE, 387 IDLE_STATE_ACTIVE,
315 IDLE_STATE_ACTIVE 388 IDLE_STATE_ACTIVE
316 }; 389 };
317 status_collector_.Simulate(test_states, 390 status_collector_->Simulate(test_states,
318 sizeof(test_states) / sizeof(IdleState)); 391 sizeof(test_states) / sizeof(IdleState));
319 status_collector_.GetStatus(&status_); 392 GetStatus();
320 EXPECT_EQ(0, status_.active_period_size()); 393 EXPECT_EQ(0, status_.active_period_size());
321 EXPECT_EQ(0, GetActiveMilliseconds(status_)); 394 EXPECT_EQ(0, GetActiveMilliseconds(status_));
322 } 395 }
323 396
324 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) { 397 TEST_F(DeviceStatusCollectorTest, ActivityCrossingMidnight) {
325 IdleState test_states[] = { 398 IdleState test_states[] = {
326 IDLE_STATE_ACTIVE 399 IDLE_STATE_ACTIVE
327 }; 400 };
328 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); 401 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true);
329 402
330 // Set the baseline time to 10 seconds after midnight. 403 // Set the baseline time to 10 seconds after midnight.
331 status_collector_.SetBaselineTime( 404 status_collector_->SetBaselineTime(
332 Time::Now().LocalMidnight() + TimeDelta::FromSeconds(10)); 405 Time::Now().LocalMidnight() + TimeDelta::FromSeconds(10));
333 406
334 status_collector_.Simulate(test_states, 1); 407 status_collector_->Simulate(test_states, 1);
335 status_collector_.GetStatus(&status_); 408 GetStatus();
336 ASSERT_EQ(2, status_.active_period_size()); 409 ASSERT_EQ(2, status_.active_period_size());
337 410
338 em::ActiveTimePeriod period0 = status_.active_period(0); 411 em::ActiveTimePeriod period0 = status_.active_period(0);
339 em::ActiveTimePeriod period1 = status_.active_period(1); 412 em::ActiveTimePeriod period1 = status_.active_period(1);
340 EXPECT_EQ(ActivePeriodMilliseconds() - 10000, period0.active_duration()); 413 EXPECT_EQ(ActivePeriodMilliseconds() - 10000, period0.active_duration());
341 EXPECT_EQ(10000, period1.active_duration()); 414 EXPECT_EQ(10000, period1.active_duration());
342 415
343 em::TimePeriod time_period0 = period0.time_period(); 416 em::TimePeriod time_period0 = period0.time_period();
344 em::TimePeriod time_period1 = period1.time_period(); 417 em::TimePeriod time_period1 = period1.time_period();
345 418
346 EXPECT_EQ(time_period0.end_timestamp(), time_period1.start_timestamp()); 419 EXPECT_EQ(time_period0.end_timestamp(), time_period1.start_timestamp());
347 420
348 // Ensure that the start and end times for the period are a day apart. 421 // 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(), 422 EXPECT_EQ(time_period0.end_timestamp() - time_period0.start_timestamp(),
350 kMillisecondsPerDay); 423 kMillisecondsPerDay);
351 EXPECT_EQ(time_period1.end_timestamp() - time_period1.start_timestamp(), 424 EXPECT_EQ(time_period1.end_timestamp() - time_period1.start_timestamp(),
352 kMillisecondsPerDay); 425 kMillisecondsPerDay);
353 } 426 }
354 427
355 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { 428 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) {
356 // Test that boot mode data is not reported if the pref is not turned on. 429 // 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_, 430 EXPECT_CALL(statistics_provider_,
361 GetMachineStatistic("devsw_boot", NotNull())) 431 GetMachineStatistic("devsw_boot", NotNull()))
362 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); 432 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true)));
363 EXPECT_EQ(false, status_.has_boot_mode()); 433 GetStatus();
434 EXPECT_FALSE(status_.has_boot_mode());
364 435
365 // Turn the pref on, and check that the status is reported iff the 436 // Turn the pref on, and check that the status is reported iff the
366 // statistics provider returns valid data. 437 // statistics provider returns valid data.
367 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true); 438 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true);
368 439
369 EXPECT_CALL(statistics_provider_, 440 EXPECT_CALL(statistics_provider_,
370 GetMachineStatistic("devsw_boot", NotNull())) 441 GetMachineStatistic("devsw_boot", NotNull()))
371 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); 442 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true)));
372 status_collector_.GetStatus(&status_); 443 GetStatus();
373 EXPECT_EQ(false, status_.has_boot_mode()); 444 EXPECT_FALSE(status_.has_boot_mode());
374 445
375 EXPECT_CALL(statistics_provider_, 446 EXPECT_CALL(statistics_provider_,
376 GetMachineStatistic("devsw_boot", NotNull())) 447 GetMachineStatistic("devsw_boot", NotNull()))
377 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); 448 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true)));
378 status_collector_.GetStatus(&status_); 449 GetStatus();
379 EXPECT_EQ(false, status_.has_boot_mode()); 450 EXPECT_FALSE(status_.has_boot_mode());
380 451
381 EXPECT_CALL(statistics_provider_, 452 EXPECT_CALL(statistics_provider_,
382 GetMachineStatistic("devsw_boot", NotNull())) 453 GetMachineStatistic("devsw_boot", NotNull()))
383 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); 454 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true)));
384 status_collector_.GetStatus(&status_); 455 GetStatus();
385 EXPECT_EQ("Verified", status_.boot_mode()); 456 EXPECT_EQ("Verified", status_.boot_mode());
386 457
387 EXPECT_CALL(statistics_provider_, 458 EXPECT_CALL(statistics_provider_,
388 GetMachineStatistic("devsw_boot", NotNull())) 459 GetMachineStatistic("devsw_boot", NotNull()))
389 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); 460 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true)));
390 status_collector_.GetStatus(&status_); 461 GetStatus();
391 EXPECT_EQ("Dev", status_.boot_mode()); 462 EXPECT_EQ("Dev", status_.boot_mode());
392 } 463 }
393 464
394 TEST_F(DeviceStatusCollectorTest, VersionInfo) { 465 TEST_F(DeviceStatusCollectorTest, VersionInfo) {
395 // When the pref to collect this data is not enabled, expect that none of 466 // When the pref to collect this data is not enabled, expect that none of
396 // the fields are present in the protobuf. 467 // the fields are present in the protobuf.
397 status_collector_.GetStatus(&status_); 468 GetStatus();
398 EXPECT_EQ(false, status_.has_browser_version()); 469 EXPECT_FALSE(status_.has_browser_version());
399 EXPECT_EQ(false, status_.has_os_version()); 470 EXPECT_FALSE(status_.has_os_version());
400 EXPECT_EQ(false, status_.has_firmware_version()); 471 EXPECT_FALSE(status_.has_firmware_version());
401 472
402 cros_settings_->SetBoolean(chromeos::kReportDeviceVersionInfo, true); 473 cros_settings_->SetBoolean(chromeos::kReportDeviceVersionInfo, true);
403 status_collector_.GetStatus(&status_); 474 GetStatus();
404 EXPECT_EQ(true, status_.has_browser_version()); 475 EXPECT_TRUE(status_.has_browser_version());
405 EXPECT_EQ(true, status_.has_os_version()); 476 EXPECT_TRUE(status_.has_os_version());
406 EXPECT_EQ(true, status_.has_firmware_version()); 477 EXPECT_TRUE(status_.has_firmware_version());
407 478
408 // Check that the browser version is not empty. OS version & firmware 479 // Check that the browser version is not empty. OS version & firmware
409 // don't have any reasonable values inside the unit test, so those 480 // don't have any reasonable values inside the unit test, so those
410 // aren't checked. 481 // aren't checked.
411 EXPECT_NE("", status_.browser_version()); 482 EXPECT_NE("", status_.browser_version());
412 } 483 }
413 484
485 TEST_F(DeviceStatusCollectorTest, Location) {
486 content::Geoposition valid_fix;
487 valid_fix.latitude = 4.3;
488 valid_fix.longitude = -7.8;
489 valid_fix.accuracy = 3.;
490 valid_fix.timestamp = Time::Now();
491
492 content::Geoposition invalid_fix;
493 invalid_fix.error_code =
494 content::Geoposition::ERROR_CODE_POSITION_UNAVAILABLE;
495 invalid_fix.timestamp = Time::Now();
496
497 // Check that when device location reporting is disabled, no location is
498 // reported.
499 SetMockPositionToReturnNext(valid_fix);
500 CheckThatNoLocationIsReported();
501
502 // Check that when device location reporting is enabled and a valid fix is
503 // available, the location is reported and is stored in local state.
504 SetMockPositionToReturnNext(valid_fix);
505 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true);
506 EXPECT_FALSE(prefs_.GetDictionary(prefs::kDeviceLocation)->empty());
507 CheckThatAValidLocationIsReported();
508
509 // Restart the status collector. Check that the last known location has been
510 // retrieved from local state without requesting a geolocation update.
511 SetMockPositionToReturnNext(valid_fix);
512 RestartStatusCollector();
513 CheckThatAValidLocationIsReported();
514 EXPECT_TRUE(mock_position_to_return_next.get());
515
516 // Check that after disabling location reporting again, the last known
517 // location has been cleared from local state and is no longer reported.
518 SetMockPositionToReturnNext(valid_fix);
519 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, false);
520 // Allow the new pref to propagate to the status collector.
521 message_loop_.RunAllPending();
522 EXPECT_TRUE(prefs_.GetDictionary(prefs::kDeviceLocation)->empty());
523 CheckThatNoLocationIsReported();
524
525 // Check that after enabling location reporting again, an error is reported
526 // if no valid fix is available.
527 SetMockPositionToReturnNext(invalid_fix);
528 cros_settings_->SetBoolean(chromeos::kReportDeviceLocation, true);
529 // Allow the new pref to propagate to the status collector.
530 message_loop_.RunAllPending();
531 CheckThatALocationErrorIsReported();
532 }
533
414 } // namespace policy 534 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698