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