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/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/time.h" | 8 #include "base/time.h" |
9 #include "chrome/browser/idle.h" | 9 #include "chrome/browser/idle.h" |
10 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" | 10 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" |
11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
13 #include "chrome/common/pref_names.h" | |
13 #include "chrome/test/base/testing_pref_service.h" | 14 #include "chrome/test/base/testing_pref_service.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using base::TimeDelta; | 18 using base::TimeDelta; |
18 using base::Time; | 19 using base::Time; |
19 | 20 |
20 namespace em = enterprise_management; | 21 namespace em = enterprise_management; |
21 | 22 |
22 namespace { | 23 namespace { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 TestingDeviceStatusCollector status_collector_; | 111 TestingDeviceStatusCollector status_collector_; |
111 em::DeviceStatusReportRequest status_; | 112 em::DeviceStatusReportRequest status_; |
112 }; | 113 }; |
113 | 114 |
114 TEST_F(DeviceStatusCollectorTest, AllIdle) { | 115 TEST_F(DeviceStatusCollectorTest, AllIdle) { |
115 IdleState test_states[] = { | 116 IdleState test_states[] = { |
116 IDLE_STATE_IDLE, | 117 IDLE_STATE_IDLE, |
117 IDLE_STATE_IDLE, | 118 IDLE_STATE_IDLE, |
118 IDLE_STATE_IDLE | 119 IDLE_STATE_IDLE |
119 }; | 120 }; |
121 prefs_.SetBoolean(prefs::kReportDeviceActivityTimes, true); | |
pastarmovj
2012/01/26 09:52:11
Those tests will then need some more mocking to wo
Patrick Dubroy
2012/01/27 15:20:29
Done.
| |
122 | |
120 // Test reporting with no data. | 123 // Test reporting with no data. |
121 status_collector_.GetStatus(&status_); | 124 status_collector_.GetStatus(&status_); |
122 EXPECT_EQ(0, status_.active_time_size()); | 125 EXPECT_EQ(0, status_.active_time_size()); |
123 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 126 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
124 | 127 |
125 // Test reporting with a single idle sample. | 128 // Test reporting with a single idle sample. |
126 status_collector_.Simulate(test_states, 1); | 129 status_collector_.Simulate(test_states, 1); |
127 status_collector_.GetStatus(&status_); | 130 status_collector_.GetStatus(&status_); |
128 EXPECT_EQ(0, status_.active_time_size()); | 131 EXPECT_EQ(0, status_.active_time_size()); |
129 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 132 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
130 | 133 |
131 // Test reporting with multiple consecutive idle samples. | 134 // Test reporting with multiple consecutive idle samples. |
132 status_collector_.Simulate(test_states, | 135 status_collector_.Simulate(test_states, |
133 sizeof(test_states) / sizeof(IdleState)); | 136 sizeof(test_states) / sizeof(IdleState)); |
134 status_collector_.GetStatus(&status_); | 137 status_collector_.GetStatus(&status_); |
135 EXPECT_EQ(0, status_.active_time_size()); | 138 EXPECT_EQ(0, status_.active_time_size()); |
136 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 139 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
137 } | 140 } |
138 | 141 |
139 TEST_F(DeviceStatusCollectorTest, AllActive) { | 142 TEST_F(DeviceStatusCollectorTest, AllActive) { |
140 IdleState test_states[] = { | 143 IdleState test_states[] = { |
141 IDLE_STATE_ACTIVE, | 144 IDLE_STATE_ACTIVE, |
142 IDLE_STATE_ACTIVE, | 145 IDLE_STATE_ACTIVE, |
143 IDLE_STATE_ACTIVE | 146 IDLE_STATE_ACTIVE |
144 }; | 147 }; |
148 prefs_.SetBoolean(prefs::kReportDeviceActivityTimes, true); | |
149 | |
145 // Test a single active sample. | 150 // Test a single active sample. |
146 status_collector_.Simulate(test_states, 1); | 151 status_collector_.Simulate(test_states, 1); |
147 status_collector_.GetStatus(&status_); | 152 status_collector_.GetStatus(&status_); |
148 EXPECT_EQ(1, status_.active_time_size()); | 153 EXPECT_EQ(1, status_.active_time_size()); |
149 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 154 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
150 status_.clear_active_time(); // Clear the result protobuf. | 155 status_.clear_active_time(); // Clear the result protobuf. |
151 | 156 |
152 // Test multiple consecutive active samples -- they should be coalesced | 157 // Test multiple consecutive active samples -- they should be coalesced |
153 // into a single active period. | 158 // into a single active period. |
154 status_collector_.Simulate(test_states, | 159 status_collector_.Simulate(test_states, |
155 sizeof(test_states) / sizeof(IdleState)); | 160 sizeof(test_states) / sizeof(IdleState)); |
156 status_collector_.GetStatus(&status_); | 161 status_collector_.GetStatus(&status_); |
157 EXPECT_EQ(1, status_.active_time_size()); | 162 EXPECT_EQ(1, status_.active_time_size()); |
158 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 163 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
159 } | 164 } |
160 | 165 |
161 TEST_F(DeviceStatusCollectorTest, MixedStates) { | 166 TEST_F(DeviceStatusCollectorTest, MixedStates) { |
162 IdleState test_states[] = { | 167 IdleState test_states[] = { |
163 IDLE_STATE_ACTIVE, | 168 IDLE_STATE_ACTIVE, |
164 IDLE_STATE_IDLE, | 169 IDLE_STATE_IDLE, |
165 IDLE_STATE_ACTIVE, | 170 IDLE_STATE_ACTIVE, |
166 IDLE_STATE_ACTIVE, | 171 IDLE_STATE_ACTIVE, |
167 IDLE_STATE_IDLE, | 172 IDLE_STATE_IDLE, |
168 IDLE_STATE_IDLE, | 173 IDLE_STATE_IDLE, |
169 IDLE_STATE_ACTIVE | 174 IDLE_STATE_ACTIVE |
170 }; | 175 }; |
176 prefs_.SetBoolean(prefs::kReportDeviceActivityTimes, true); | |
171 status_collector_.Simulate(test_states, | 177 status_collector_.Simulate(test_states, |
172 sizeof(test_states) / sizeof(IdleState)); | 178 sizeof(test_states) / sizeof(IdleState)); |
173 status_collector_.GetStatus(&status_); | 179 status_collector_.GetStatus(&status_); |
174 EXPECT_EQ(3, status_.active_time_size()); | 180 EXPECT_EQ(3, status_.active_time_size()); |
175 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 181 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
176 } | 182 } |
177 | 183 |
178 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { | 184 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { |
179 IdleState test_states[] = { | 185 IdleState test_states[] = { |
180 IDLE_STATE_ACTIVE, | 186 IDLE_STATE_ACTIVE, |
181 IDLE_STATE_IDLE, | 187 IDLE_STATE_IDLE, |
182 IDLE_STATE_ACTIVE, | 188 IDLE_STATE_ACTIVE, |
183 IDLE_STATE_ACTIVE, | 189 IDLE_STATE_ACTIVE, |
184 IDLE_STATE_IDLE, | 190 IDLE_STATE_IDLE, |
185 IDLE_STATE_IDLE | 191 IDLE_STATE_IDLE |
186 }; | 192 }; |
193 prefs_.SetBoolean(prefs::kReportDeviceActivityTimes, true); | |
187 status_collector_.Simulate(test_states, | 194 status_collector_.Simulate(test_states, |
188 sizeof(test_states) / sizeof(IdleState)); | 195 sizeof(test_states) / sizeof(IdleState)); |
189 | 196 |
190 // Process the list a second time with a different collector. | 197 // Process the list a second time with a different collector. |
191 // It should be able to count the active periods found by the first | 198 // It should be able to count the active periods found by the first |
192 // collector, because the results are stored in a pref. | 199 // collector, because the results are stored in a pref. |
193 TestingDeviceStatusCollector second_collector(&prefs_, | 200 TestingDeviceStatusCollector second_collector(&prefs_, |
194 &statistics_provider_); | 201 &statistics_provider_); |
195 second_collector.Simulate(test_states, | 202 second_collector.Simulate(test_states, |
196 sizeof(test_states) / sizeof(IdleState)); | 203 sizeof(test_states) / sizeof(IdleState)); |
197 | 204 |
198 second_collector.GetStatus(&status_); | 205 second_collector.GetStatus(&status_); |
199 EXPECT_EQ(4, status_.active_time_size()); | 206 EXPECT_EQ(4, status_.active_time_size()); |
200 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 207 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
201 } | 208 } |
202 | 209 |
203 TEST_F(DeviceStatusCollectorTest, Times) { | 210 TEST_F(DeviceStatusCollectorTest, Times) { |
204 IdleState test_states[] = { | 211 IdleState test_states[] = { |
205 IDLE_STATE_ACTIVE, | 212 IDLE_STATE_ACTIVE, |
206 IDLE_STATE_IDLE, | 213 IDLE_STATE_IDLE, |
207 IDLE_STATE_ACTIVE, | 214 IDLE_STATE_ACTIVE, |
208 IDLE_STATE_ACTIVE, | 215 IDLE_STATE_ACTIVE, |
209 IDLE_STATE_IDLE, | 216 IDLE_STATE_IDLE, |
210 IDLE_STATE_IDLE | 217 IDLE_STATE_IDLE |
211 }; | 218 }; |
219 prefs_.SetBoolean(prefs::kReportDeviceActivityTimes, true); | |
212 status_collector_.Simulate(test_states, | 220 status_collector_.Simulate(test_states, |
213 sizeof(test_states) / sizeof(IdleState)); | 221 sizeof(test_states) / sizeof(IdleState)); |
214 status_collector_.GetStatus(&status_); | 222 status_collector_.GetStatus(&status_); |
215 EXPECT_EQ(2, status_.active_time_size()); | 223 EXPECT_EQ(2, status_.active_time_size()); |
216 | 224 |
217 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 225 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
218 } | 226 } |
219 | 227 |
220 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { | 228 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { |
221 IdleState test_states[] = { | 229 IdleState test_states[] = { |
222 IDLE_STATE_ACTIVE, | 230 IDLE_STATE_ACTIVE, |
223 IDLE_STATE_IDLE | 231 IDLE_STATE_IDLE |
224 }; | 232 }; |
225 unsigned int max_periods = 10; | 233 unsigned int max_periods = 10; |
226 | 234 |
235 prefs_.SetBoolean(prefs::kReportDeviceActivityTimes, true); | |
227 status_collector_.set_max_stored_active_periods(max_periods); | 236 status_collector_.set_max_stored_active_periods(max_periods); |
228 | 237 |
229 // Simulate 12 active periods. | 238 // Simulate 12 active periods. |
230 for (int i = 0; i < 12; i++) { | 239 for (int i = 0; i < 12; i++) { |
231 status_collector_.Simulate(test_states, | 240 status_collector_.Simulate(test_states, |
232 sizeof(test_states) / sizeof(IdleState)); | 241 sizeof(test_states) / sizeof(IdleState)); |
233 } | 242 } |
234 | 243 |
235 // Check that we don't exceed the max number of periods. | 244 // Check that we don't exceed the max number of periods. |
236 status_collector_.GetStatus(&status_); | 245 status_collector_.GetStatus(&status_); |
237 EXPECT_EQ(static_cast<int>(max_periods), status_.active_time_size()); | 246 EXPECT_EQ(static_cast<int>(max_periods), status_.active_time_size()); |
238 } | 247 } |
239 | 248 |
249 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { | |
250 // If the pref for collecting device activity times isn't explicitly turned | |
251 // on, no data on activity times should be reported. | |
252 | |
253 IdleState test_states[] = { | |
254 IDLE_STATE_ACTIVE, | |
255 IDLE_STATE_ACTIVE, | |
256 IDLE_STATE_ACTIVE | |
257 }; | |
258 status_collector_.Simulate(test_states, | |
259 sizeof(test_states) / sizeof(IdleState)); | |
260 status_collector_.GetStatus(&status_); | |
261 EXPECT_EQ(0, status_.active_time_size()); | |
262 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | |
263 } | |
264 | |
240 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { | 265 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { |
266 // Test that boot mode data is not report if the pref is not turned on. | |
241 status_collector_.GetStatus(&status_); | 267 status_collector_.GetStatus(&status_); |
242 EXPECT_EQ(false, status_.has_boot_mode()); | 268 EXPECT_EQ(false, status_.has_boot_mode()); |
243 | 269 |
244 EXPECT_CALL(statistics_provider_, | 270 EXPECT_CALL(statistics_provider_, |
245 GetMachineStatistic("devsw_boot", NotNull())) | 271 GetMachineStatistic("devsw_boot", NotNull())) |
272 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); | |
273 EXPECT_EQ(false, status_.has_boot_mode()); | |
274 | |
275 // Turn the pref on, and check that the status is reported iff the | |
276 // statistics provider returns valid data. | |
277 prefs_.SetBoolean(prefs::kReportDeviceBootMode, true); | |
278 | |
279 EXPECT_CALL(statistics_provider_, | |
280 GetMachineStatistic("devsw_boot", NotNull())) | |
246 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); | 281 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); |
247 status_collector_.GetStatus(&status_); | 282 status_collector_.GetStatus(&status_); |
248 EXPECT_EQ(false, status_.has_boot_mode()); | 283 EXPECT_EQ(false, status_.has_boot_mode()); |
249 | 284 |
250 EXPECT_CALL(statistics_provider_, | 285 EXPECT_CALL(statistics_provider_, |
251 GetMachineStatistic("devsw_boot", NotNull())) | 286 GetMachineStatistic("devsw_boot", NotNull())) |
252 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); | 287 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); |
253 status_collector_.GetStatus(&status_); | 288 status_collector_.GetStatus(&status_); |
254 EXPECT_EQ(false, status_.has_boot_mode()); | 289 EXPECT_EQ(false, status_.has_boot_mode()); |
255 | 290 |
256 EXPECT_CALL(statistics_provider_, | 291 EXPECT_CALL(statistics_provider_, |
257 GetMachineStatistic("devsw_boot", NotNull())) | 292 GetMachineStatistic("devsw_boot", NotNull())) |
258 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); | 293 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); |
259 status_collector_.GetStatus(&status_); | 294 status_collector_.GetStatus(&status_); |
260 EXPECT_EQ("Verified", status_.boot_mode()); | 295 EXPECT_EQ("Verified", status_.boot_mode()); |
261 | 296 |
262 EXPECT_CALL(statistics_provider_, | 297 EXPECT_CALL(statistics_provider_, |
263 GetMachineStatistic("devsw_boot", NotNull())) | 298 GetMachineStatistic("devsw_boot", NotNull())) |
264 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); | 299 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); |
265 status_collector_.GetStatus(&status_); | 300 status_collector_.GetStatus(&status_); |
266 EXPECT_EQ("Dev", status_.boot_mode()); | 301 EXPECT_EQ("Dev", status_.boot_mode()); |
267 } | 302 } |
268 | 303 |
304 TEST_F(DeviceStatusCollectorTest, VersionInfo) { | |
305 // When the pref to collect this data is not enabled, expect that none of | |
306 // the fields are present in the protobuf. | |
307 status_collector_.GetStatus(&status_); | |
308 EXPECT_EQ(false, status_.has_browser_version()); | |
309 EXPECT_EQ(false, status_.has_os_version()); | |
310 EXPECT_EQ(false, status_.has_firmware_version()); | |
311 | |
312 prefs_.SetBoolean(prefs::kReportDeviceVersionInfo, true); | |
313 status_collector_.GetStatus(&status_); | |
314 EXPECT_EQ(true, status_.has_browser_version()); | |
315 EXPECT_EQ(true, status_.has_os_version()); | |
316 EXPECT_EQ(true, status_.has_firmware_version()); | |
317 | |
318 // Check that the browser version is not empty. OS version & firmware | |
319 // don't have any reasonable values inside the unit test, so those | |
320 // aren't checked. | |
321 EXPECT_NE("", status_.browser_version()); | |
322 } | |
323 | |
269 } // namespace policy | 324 } // namespace policy |
OLD | NEW |