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/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" | |
10 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" | 14 #include "chrome/browser/chromeos/system/mock_statistics_provider.h" |
11 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 15 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
17 #include "chrome/common/pref_names.h" | |
pastarmovj
2012/01/27 15:39:03
Do you need this one?
Patrick Dubroy
2012/01/27 16:18:12
Done.
| |
13 #include "chrome/test/base/testing_pref_service.h" | 18 #include "chrome/test/base/testing_pref_service.h" |
19 #include "content/test/test_browser_thread.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
16 | 22 |
17 using base::TimeDelta; | 23 using base::TimeDelta; |
18 using base::Time; | 24 using base::Time; |
19 | 25 |
20 namespace em = enterprise_management; | 26 namespace em = enterprise_management; |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 namespace policy { | 90 namespace policy { |
85 | 91 |
86 using ::testing::_; | 92 using ::testing::_; |
87 using ::testing::NotNull; | 93 using ::testing::NotNull; |
88 using ::testing::Return; | 94 using ::testing::Return; |
89 using ::testing::SetArgPointee; | 95 using ::testing::SetArgPointee; |
90 | 96 |
91 class DeviceStatusCollectorTest : public testing::Test { | 97 class DeviceStatusCollectorTest : public testing::Test { |
92 public: | 98 public: |
93 DeviceStatusCollectorTest() | 99 DeviceStatusCollectorTest() |
94 : prefs_(), | 100 : message_loop_(MessageLoop::TYPE_UI), |
95 status_collector_(&prefs_, &statistics_provider_) { | 101 ui_thread_(content::BrowserThread::UI, &message_loop_), |
102 file_thread_(content::BrowserThread::FILE, &message_loop_), | |
103 status_collector_(&prefs_, &statistics_provider_) { | |
104 | |
96 DeviceStatusCollector::RegisterPrefs(&prefs_); | 105 DeviceStatusCollector::RegisterPrefs(&prefs_); |
97 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) | 106 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) |
98 .WillRepeatedly(Return(false)); | 107 .WillRepeatedly(Return(false)); |
108 | |
109 cros_settings_ = chromeos::CrosSettings::Get(); | |
110 | |
111 // Remove the real DeviceSettingsProvider and replace it with a stub. | |
112 device_settings_provider_ = | |
113 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); | |
114 EXPECT_TRUE(device_settings_provider_ != NULL); | |
115 EXPECT_TRUE( | |
116 cros_settings_->RemoveSettingsProvider(device_settings_provider_)); | |
117 cros_settings_->AddSettingsProvider(&stub_settings_provider_); | |
118 } | |
119 | |
120 ~DeviceStatusCollectorTest() { | |
121 // Restore the real DeviceSettingsProvider. | |
122 EXPECT_TRUE( | |
123 cros_settings_->RemoveSettingsProvider(&stub_settings_provider_)); | |
124 cros_settings_->AddSettingsProvider(device_settings_provider_); | |
99 } | 125 } |
100 | 126 |
101 protected: | 127 protected: |
102 // Convenience method. | 128 // Convenience method. |
103 int64 ActivePeriodMilliseconds() { | 129 int64 ActivePeriodMilliseconds() { |
104 return policy::DeviceStatusCollector::kPollIntervalSeconds * 1000; | 130 return policy::DeviceStatusCollector::kPollIntervalSeconds * 1000; |
105 } | 131 } |
106 | 132 |
107 MessageLoop message_loop_; | 133 MessageLoop message_loop_; |
134 content::TestBrowserThread ui_thread_; | |
135 content::TestBrowserThread file_thread_; | |
136 | |
108 TestingPrefService prefs_; | 137 TestingPrefService prefs_; |
109 chromeos::system::MockStatisticsProvider statistics_provider_; | 138 chromeos::system::MockStatisticsProvider statistics_provider_; |
110 TestingDeviceStatusCollector status_collector_; | 139 TestingDeviceStatusCollector status_collector_; |
111 em::DeviceStatusReportRequest status_; | 140 em::DeviceStatusReportRequest status_; |
141 chromeos::CrosSettings* cros_settings_; | |
142 chromeos::CrosSettingsProvider* device_settings_provider_; | |
143 chromeos::StubCrosSettingsProvider stub_settings_provider_; | |
112 }; | 144 }; |
113 | 145 |
114 TEST_F(DeviceStatusCollectorTest, AllIdle) { | 146 TEST_F(DeviceStatusCollectorTest, AllIdle) { |
115 IdleState test_states[] = { | 147 IdleState test_states[] = { |
116 IDLE_STATE_IDLE, | 148 IDLE_STATE_IDLE, |
117 IDLE_STATE_IDLE, | 149 IDLE_STATE_IDLE, |
118 IDLE_STATE_IDLE | 150 IDLE_STATE_IDLE |
119 }; | 151 }; |
152 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | |
153 | |
120 // Test reporting with no data. | 154 // Test reporting with no data. |
121 status_collector_.GetStatus(&status_); | 155 status_collector_.GetStatus(&status_); |
122 EXPECT_EQ(0, status_.active_time_size()); | 156 EXPECT_EQ(0, status_.active_time_size()); |
123 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 157 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
124 | 158 |
125 // Test reporting with a single idle sample. | 159 // Test reporting with a single idle sample. |
126 status_collector_.Simulate(test_states, 1); | 160 status_collector_.Simulate(test_states, 1); |
127 status_collector_.GetStatus(&status_); | 161 status_collector_.GetStatus(&status_); |
128 EXPECT_EQ(0, status_.active_time_size()); | 162 EXPECT_EQ(0, status_.active_time_size()); |
129 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 163 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
130 | 164 |
131 // Test reporting with multiple consecutive idle samples. | 165 // Test reporting with multiple consecutive idle samples. |
132 status_collector_.Simulate(test_states, | 166 status_collector_.Simulate(test_states, |
133 sizeof(test_states) / sizeof(IdleState)); | 167 sizeof(test_states) / sizeof(IdleState)); |
134 status_collector_.GetStatus(&status_); | 168 status_collector_.GetStatus(&status_); |
135 EXPECT_EQ(0, status_.active_time_size()); | 169 EXPECT_EQ(0, status_.active_time_size()); |
136 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | 170 EXPECT_EQ(0, GetActiveMilliseconds(status_)); |
137 } | 171 } |
138 | 172 |
139 TEST_F(DeviceStatusCollectorTest, AllActive) { | 173 TEST_F(DeviceStatusCollectorTest, AllActive) { |
140 IdleState test_states[] = { | 174 IdleState test_states[] = { |
141 IDLE_STATE_ACTIVE, | 175 IDLE_STATE_ACTIVE, |
142 IDLE_STATE_ACTIVE, | 176 IDLE_STATE_ACTIVE, |
143 IDLE_STATE_ACTIVE | 177 IDLE_STATE_ACTIVE |
144 }; | 178 }; |
179 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | |
180 | |
145 // Test a single active sample. | 181 // Test a single active sample. |
146 status_collector_.Simulate(test_states, 1); | 182 status_collector_.Simulate(test_states, 1); |
147 status_collector_.GetStatus(&status_); | 183 status_collector_.GetStatus(&status_); |
148 EXPECT_EQ(1, status_.active_time_size()); | 184 EXPECT_EQ(1, status_.active_time_size()); |
149 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 185 EXPECT_EQ(1 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
150 status_.clear_active_time(); // Clear the result protobuf. | 186 status_.clear_active_time(); // Clear the result protobuf. |
151 | 187 |
152 // Test multiple consecutive active samples -- they should be coalesced | 188 // Test multiple consecutive active samples -- they should be coalesced |
153 // into a single active period. | 189 // into a single active period. |
154 status_collector_.Simulate(test_states, | 190 status_collector_.Simulate(test_states, |
155 sizeof(test_states) / sizeof(IdleState)); | 191 sizeof(test_states) / sizeof(IdleState)); |
156 status_collector_.GetStatus(&status_); | 192 status_collector_.GetStatus(&status_); |
157 EXPECT_EQ(1, status_.active_time_size()); | 193 EXPECT_EQ(1, status_.active_time_size()); |
158 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 194 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
159 } | 195 } |
160 | 196 |
161 TEST_F(DeviceStatusCollectorTest, MixedStates) { | 197 TEST_F(DeviceStatusCollectorTest, MixedStates) { |
162 IdleState test_states[] = { | 198 IdleState test_states[] = { |
163 IDLE_STATE_ACTIVE, | 199 IDLE_STATE_ACTIVE, |
164 IDLE_STATE_IDLE, | 200 IDLE_STATE_IDLE, |
165 IDLE_STATE_ACTIVE, | 201 IDLE_STATE_ACTIVE, |
166 IDLE_STATE_ACTIVE, | 202 IDLE_STATE_ACTIVE, |
167 IDLE_STATE_IDLE, | 203 IDLE_STATE_IDLE, |
168 IDLE_STATE_IDLE, | 204 IDLE_STATE_IDLE, |
169 IDLE_STATE_ACTIVE | 205 IDLE_STATE_ACTIVE |
170 }; | 206 }; |
207 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | |
171 status_collector_.Simulate(test_states, | 208 status_collector_.Simulate(test_states, |
172 sizeof(test_states) / sizeof(IdleState)); | 209 sizeof(test_states) / sizeof(IdleState)); |
173 status_collector_.GetStatus(&status_); | 210 status_collector_.GetStatus(&status_); |
174 EXPECT_EQ(3, status_.active_time_size()); | 211 EXPECT_EQ(3, status_.active_time_size()); |
175 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 212 EXPECT_EQ(4 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
176 } | 213 } |
177 | 214 |
178 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { | 215 TEST_F(DeviceStatusCollectorTest, StateKeptInPref) { |
179 IdleState test_states[] = { | 216 IdleState test_states[] = { |
180 IDLE_STATE_ACTIVE, | 217 IDLE_STATE_ACTIVE, |
181 IDLE_STATE_IDLE, | 218 IDLE_STATE_IDLE, |
182 IDLE_STATE_ACTIVE, | 219 IDLE_STATE_ACTIVE, |
183 IDLE_STATE_ACTIVE, | 220 IDLE_STATE_ACTIVE, |
184 IDLE_STATE_IDLE, | 221 IDLE_STATE_IDLE, |
185 IDLE_STATE_IDLE | 222 IDLE_STATE_IDLE |
186 }; | 223 }; |
224 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | |
187 status_collector_.Simulate(test_states, | 225 status_collector_.Simulate(test_states, |
188 sizeof(test_states) / sizeof(IdleState)); | 226 sizeof(test_states) / sizeof(IdleState)); |
189 | 227 |
190 // Process the list a second time with a different collector. | 228 // Process the list a second time with a different collector. |
191 // It should be able to count the active periods found by the first | 229 // It should be able to count the active periods found by the first |
192 // collector, because the results are stored in a pref. | 230 // collector, because the results are stored in a pref. |
193 TestingDeviceStatusCollector second_collector(&prefs_, | 231 TestingDeviceStatusCollector second_collector(&prefs_, |
194 &statistics_provider_); | 232 &statistics_provider_); |
195 second_collector.Simulate(test_states, | 233 second_collector.Simulate(test_states, |
196 sizeof(test_states) / sizeof(IdleState)); | 234 sizeof(test_states) / sizeof(IdleState)); |
197 | 235 |
198 second_collector.GetStatus(&status_); | 236 second_collector.GetStatus(&status_); |
199 EXPECT_EQ(4, status_.active_time_size()); | 237 EXPECT_EQ(4, status_.active_time_size()); |
200 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 238 EXPECT_EQ(6 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
201 } | 239 } |
202 | 240 |
203 TEST_F(DeviceStatusCollectorTest, Times) { | 241 TEST_F(DeviceStatusCollectorTest, Times) { |
204 IdleState test_states[] = { | 242 IdleState test_states[] = { |
205 IDLE_STATE_ACTIVE, | 243 IDLE_STATE_ACTIVE, |
206 IDLE_STATE_IDLE, | 244 IDLE_STATE_IDLE, |
207 IDLE_STATE_ACTIVE, | 245 IDLE_STATE_ACTIVE, |
208 IDLE_STATE_ACTIVE, | 246 IDLE_STATE_ACTIVE, |
209 IDLE_STATE_IDLE, | 247 IDLE_STATE_IDLE, |
210 IDLE_STATE_IDLE | 248 IDLE_STATE_IDLE |
211 }; | 249 }; |
250 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | |
212 status_collector_.Simulate(test_states, | 251 status_collector_.Simulate(test_states, |
213 sizeof(test_states) / sizeof(IdleState)); | 252 sizeof(test_states) / sizeof(IdleState)); |
214 status_collector_.GetStatus(&status_); | 253 status_collector_.GetStatus(&status_); |
215 EXPECT_EQ(2, status_.active_time_size()); | 254 EXPECT_EQ(2, status_.active_time_size()); |
216 | 255 |
217 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 256 EXPECT_EQ(3 * ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
218 } | 257 } |
219 | 258 |
220 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { | 259 TEST_F(DeviceStatusCollectorTest, MaxStoredPeriods) { |
221 IdleState test_states[] = { | 260 IdleState test_states[] = { |
222 IDLE_STATE_ACTIVE, | 261 IDLE_STATE_ACTIVE, |
223 IDLE_STATE_IDLE | 262 IDLE_STATE_IDLE |
224 }; | 263 }; |
225 unsigned int max_periods = 10; | 264 unsigned int max_periods = 10; |
226 | 265 |
266 cros_settings_->SetBoolean(chromeos::kReportDeviceActivityTimes, true); | |
227 status_collector_.set_max_stored_active_periods(max_periods); | 267 status_collector_.set_max_stored_active_periods(max_periods); |
228 | 268 |
229 // Simulate 12 active periods. | 269 // Simulate 12 active periods. |
230 for (int i = 0; i < 12; i++) { | 270 for (int i = 0; i < 12; i++) { |
231 status_collector_.Simulate(test_states, | 271 status_collector_.Simulate(test_states, |
232 sizeof(test_states) / sizeof(IdleState)); | 272 sizeof(test_states) / sizeof(IdleState)); |
233 } | 273 } |
234 | 274 |
235 // Check that we don't exceed the max number of periods. | 275 // Check that we don't exceed the max number of periods. |
236 status_collector_.GetStatus(&status_); | 276 status_collector_.GetStatus(&status_); |
237 EXPECT_EQ(static_cast<int>(max_periods), status_.active_time_size()); | 277 EXPECT_EQ(static_cast<int>(max_periods), status_.active_time_size()); |
238 } | 278 } |
239 | 279 |
280 TEST_F(DeviceStatusCollectorTest, ActivityTimesDisabledByDefault) { | |
281 // If the pref for collecting device activity times isn't explicitly turned | |
282 // on, no data on activity times should be reported. | |
283 | |
284 IdleState test_states[] = { | |
285 IDLE_STATE_ACTIVE, | |
286 IDLE_STATE_ACTIVE, | |
287 IDLE_STATE_ACTIVE | |
288 }; | |
289 status_collector_.Simulate(test_states, | |
290 sizeof(test_states) / sizeof(IdleState)); | |
291 status_collector_.GetStatus(&status_); | |
292 EXPECT_EQ(0, status_.active_time_size()); | |
293 EXPECT_EQ(0, GetActiveMilliseconds(status_)); | |
294 } | |
295 | |
240 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { | 296 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { |
297 // Test that boot mode data is not report if the pref is not turned on. | |
pastarmovj
2012/01/27 15:39:03
s/report/reported/ ?
Patrick Dubroy
2012/01/27 16:18:12
Done.
| |
241 status_collector_.GetStatus(&status_); | 298 status_collector_.GetStatus(&status_); |
242 EXPECT_EQ(false, status_.has_boot_mode()); | 299 EXPECT_EQ(false, status_.has_boot_mode()); |
243 | 300 |
244 EXPECT_CALL(statistics_provider_, | 301 EXPECT_CALL(statistics_provider_, |
245 GetMachineStatistic("devsw_boot", NotNull())) | 302 GetMachineStatistic("devsw_boot", NotNull())) |
303 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); | |
304 EXPECT_EQ(false, status_.has_boot_mode()); | |
305 | |
306 // Turn the pref on, and check that the status is reported iff the | |
307 // statistics provider returns valid data. | |
308 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true); | |
309 | |
310 EXPECT_CALL(statistics_provider_, | |
311 GetMachineStatistic("devsw_boot", NotNull())) | |
246 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); | 312 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); |
247 status_collector_.GetStatus(&status_); | 313 status_collector_.GetStatus(&status_); |
248 EXPECT_EQ(false, status_.has_boot_mode()); | 314 EXPECT_EQ(false, status_.has_boot_mode()); |
249 | 315 |
250 EXPECT_CALL(statistics_provider_, | 316 EXPECT_CALL(statistics_provider_, |
251 GetMachineStatistic("devsw_boot", NotNull())) | 317 GetMachineStatistic("devsw_boot", NotNull())) |
252 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); | 318 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); |
253 status_collector_.GetStatus(&status_); | 319 status_collector_.GetStatus(&status_); |
254 EXPECT_EQ(false, status_.has_boot_mode()); | 320 EXPECT_EQ(false, status_.has_boot_mode()); |
255 | 321 |
256 EXPECT_CALL(statistics_provider_, | 322 EXPECT_CALL(statistics_provider_, |
257 GetMachineStatistic("devsw_boot", NotNull())) | 323 GetMachineStatistic("devsw_boot", NotNull())) |
258 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); | 324 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); |
259 status_collector_.GetStatus(&status_); | 325 status_collector_.GetStatus(&status_); |
260 EXPECT_EQ("Verified", status_.boot_mode()); | 326 EXPECT_EQ("Verified", status_.boot_mode()); |
261 | 327 |
262 EXPECT_CALL(statistics_provider_, | 328 EXPECT_CALL(statistics_provider_, |
263 GetMachineStatistic("devsw_boot", NotNull())) | 329 GetMachineStatistic("devsw_boot", NotNull())) |
264 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); | 330 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); |
265 status_collector_.GetStatus(&status_); | 331 status_collector_.GetStatus(&status_); |
266 EXPECT_EQ("Dev", status_.boot_mode()); | 332 EXPECT_EQ("Dev", status_.boot_mode()); |
267 } | 333 } |
268 | 334 |
335 TEST_F(DeviceStatusCollectorTest, VersionInfo) { | |
336 // When the pref to collect this data is not enabled, expect that none of | |
337 // the fields are present in the protobuf. | |
338 status_collector_.GetStatus(&status_); | |
339 EXPECT_EQ(false, status_.has_browser_version()); | |
340 EXPECT_EQ(false, status_.has_os_version()); | |
341 EXPECT_EQ(false, status_.has_firmware_version()); | |
342 | |
343 cros_settings_->SetBoolean(chromeos::kReportDeviceVersionInfo, true); | |
344 status_collector_.GetStatus(&status_); | |
345 EXPECT_EQ(true, status_.has_browser_version()); | |
346 EXPECT_EQ(true, status_.has_os_version()); | |
347 EXPECT_EQ(true, status_.has_firmware_version()); | |
348 | |
349 // Check that the browser version is not empty. OS version & firmware | |
350 // don't have any reasonable values inside the unit test, so those | |
351 // aren't checked. | |
352 EXPECT_NE("", status_.browser_version()); | |
353 } | |
354 | |
269 } // namespace policy | 355 } // namespace policy |
OLD | NEW |