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/chromeos/policy/device_status_collector.h" | 5 #include "chrome/browser/chromeos/policy/device_status_collector.h" |
6 | 6 |
7 #include "base/environment.h" | 7 #include "base/environment.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/prefs/testing_pref_service.h" | 12 #include "base/prefs/testing_pref_service.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
15 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" | 15 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" |
16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | 17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
18 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" | 18 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" |
19 #include "chrome/browser/chromeos/settings/cros_settings.h" | 19 #include "chrome/browser/chromeos/settings/cros_settings.h" |
20 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 20 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
21 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 21 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
23 #include "chrome/test/base/testing_browser_process.h" | 23 #include "chrome/test/base/testing_browser_process.h" |
24 #include "chromeos/dbus/dbus_thread_manager.h" | 24 #include "chromeos/dbus/dbus_thread_manager.h" |
25 #include "chromeos/dbus/shill_device_client.h" | 25 #include "chromeos/dbus/shill_device_client.h" |
26 #include "chromeos/network/network_handler.h" | 26 #include "chromeos/network/network_handler.h" |
27 #include "chromeos/settings/cros_settings_names.h" | 27 #include "chromeos/settings/cros_settings_names.h" |
28 #include "chromeos/settings/cros_settings_provider.h" | 28 #include "chromeos/settings/cros_settings_provider.h" |
29 #include "chromeos/system/mock_statistics_provider.h" | 29 #include "chromeos/system/fake_statistics_provider.h" |
30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/geolocation_provider.h" | 31 #include "content/public/browser/geolocation_provider.h" |
32 #include "content/public/test/test_browser_thread.h" | 32 #include "content/public/test/test_browser_thread.h" |
33 #include "content/public/test/test_utils.h" | 33 #include "content/public/test/test_utils.h" |
34 #include "policy/proto/device_management_backend.pb.h" | 34 #include "policy/proto/device_management_backend.pb.h" |
35 #include "testing/gmock/include/gmock/gmock.h" | |
36 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
37 #include "third_party/cros_system_api/dbus/service_constants.h" | 36 #include "third_party/cros_system_api/dbus/service_constants.h" |
38 | 37 |
39 using ::testing::DoAll; | |
40 using ::testing::NotNull; | |
41 using ::testing::Return; | |
42 using ::testing::SetArgPointee; | |
43 using ::testing::_; | |
44 using base::Time; | 38 using base::Time; |
45 using base::TimeDelta; | 39 using base::TimeDelta; |
46 | 40 |
47 namespace em = enterprise_management; | 41 namespace em = enterprise_management; |
48 | 42 |
49 namespace { | 43 namespace { |
50 | 44 |
51 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; | 45 const int64 kMillisecondsPerDay = Time::kMicrosecondsPerDay / 1000; |
52 | 46 |
53 scoped_ptr<content::Geoposition> mock_position_to_return_next; | 47 scoped_ptr<content::Geoposition> mock_position_to_return_next; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 DEVICE_MODE_ENTERPRISE), | 152 DEVICE_MODE_ENTERPRISE), |
159 user_manager_(new chromeos::MockUserManager()), | 153 user_manager_(new chromeos::MockUserManager()), |
160 user_manager_enabler_(user_manager_) { | 154 user_manager_enabler_(user_manager_) { |
161 // Run this test with a well-known timezone so that Time::LocalMidnight() | 155 // Run this test with a well-known timezone so that Time::LocalMidnight() |
162 // returns the same values on all machines. | 156 // returns the same values on all machines. |
163 scoped_ptr<base::Environment> env(base::Environment::Create()); | 157 scoped_ptr<base::Environment> env(base::Environment::Create()); |
164 env->SetVar("TZ", "UTC"); | 158 env->SetVar("TZ", "UTC"); |
165 | 159 |
166 TestingDeviceStatusCollector::RegisterPrefs(prefs_.registry()); | 160 TestingDeviceStatusCollector::RegisterPrefs(prefs_.registry()); |
167 | 161 |
168 EXPECT_CALL(statistics_provider_, GetMachineStatistic(_, NotNull())) | |
169 .WillRepeatedly(Return(false)); | |
170 | |
171 // Remove the real DeviceSettingsProvider and replace it with a stub. | 162 // Remove the real DeviceSettingsProvider and replace it with a stub. |
172 cros_settings_ = chromeos::CrosSettings::Get(); | 163 cros_settings_ = chromeos::CrosSettings::Get(); |
173 device_settings_provider_ = | 164 device_settings_provider_ = |
174 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); | 165 cros_settings_->GetProvider(chromeos::kReportDeviceVersionInfo); |
175 EXPECT_TRUE(device_settings_provider_ != NULL); | 166 EXPECT_TRUE(device_settings_provider_ != NULL); |
176 EXPECT_TRUE( | 167 EXPECT_TRUE( |
177 cros_settings_->RemoveSettingsProvider(device_settings_provider_)); | 168 cros_settings_->RemoveSettingsProvider(device_settings_provider_)); |
178 cros_settings_->AddSettingsProvider(&stub_settings_provider_); | 169 cros_settings_->AddSettingsProvider(&stub_settings_provider_); |
179 | 170 |
180 RestartStatusCollector(); | 171 RestartStatusCollector(); |
(...skipping 13 matching lines...) Expand all Loading... |
194 virtual void SetUp() override { | 185 virtual void SetUp() override { |
195 // Disable network interface reporting since it requires additional setup. | 186 // Disable network interface reporting since it requires additional setup. |
196 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, false); | 187 cros_settings_->SetBoolean(chromeos::kReportDeviceNetworkInterfaces, false); |
197 } | 188 } |
198 | 189 |
199 void RestartStatusCollector() { | 190 void RestartStatusCollector() { |
200 policy::DeviceStatusCollector::LocationUpdateRequester callback = | 191 policy::DeviceStatusCollector::LocationUpdateRequester callback = |
201 base::Bind(&MockPositionUpdateRequester); | 192 base::Bind(&MockPositionUpdateRequester); |
202 status_collector_.reset( | 193 status_collector_.reset( |
203 new TestingDeviceStatusCollector(&prefs_, | 194 new TestingDeviceStatusCollector(&prefs_, |
204 &statistics_provider_, | 195 &fake_statistics_provider_, |
205 &callback)); | 196 &callback)); |
206 } | 197 } |
207 | 198 |
208 void GetStatus() { | 199 void GetStatus() { |
209 status_.Clear(); | 200 status_.Clear(); |
210 status_collector_->GetDeviceStatus(&status_); | 201 status_collector_->GetDeviceStatus(&status_); |
211 } | 202 } |
212 | 203 |
213 void CheckThatNoLocationIsReported() { | 204 void CheckThatNoLocationIsReported() { |
214 GetStatus(); | 205 GetStatus(); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 // Since this is a unit test running in browser_tests we must do additional | 249 // Since this is a unit test running in browser_tests we must do additional |
259 // unit test setup and make a TestingBrowserProcess. Must be first member. | 250 // unit test setup and make a TestingBrowserProcess. Must be first member. |
260 TestingBrowserProcessInitializer initializer_; | 251 TestingBrowserProcessInitializer initializer_; |
261 base::MessageLoopForUI message_loop_; | 252 base::MessageLoopForUI message_loop_; |
262 content::TestBrowserThread ui_thread_; | 253 content::TestBrowserThread ui_thread_; |
263 content::TestBrowserThread file_thread_; | 254 content::TestBrowserThread file_thread_; |
264 content::TestBrowserThread io_thread_; | 255 content::TestBrowserThread io_thread_; |
265 | 256 |
266 ScopedStubEnterpriseInstallAttributes install_attributes_; | 257 ScopedStubEnterpriseInstallAttributes install_attributes_; |
267 TestingPrefServiceSimple prefs_; | 258 TestingPrefServiceSimple prefs_; |
268 chromeos::system::MockStatisticsProvider statistics_provider_; | 259 chromeos::system::ScopedFakeStatisticsProvider fake_statistics_provider_; |
269 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 260 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
270 chromeos::ScopedTestCrosSettings test_cros_settings_; | 261 chromeos::ScopedTestCrosSettings test_cros_settings_; |
271 chromeos::CrosSettings* cros_settings_; | 262 chromeos::CrosSettings* cros_settings_; |
272 chromeos::CrosSettingsProvider* device_settings_provider_; | 263 chromeos::CrosSettingsProvider* device_settings_provider_; |
273 chromeos::StubCrosSettingsProvider stub_settings_provider_; | 264 chromeos::StubCrosSettingsProvider stub_settings_provider_; |
274 chromeos::MockUserManager* user_manager_; | 265 chromeos::MockUserManager* user_manager_; |
275 chromeos::ScopedUserManagerEnabler user_manager_enabler_; | 266 chromeos::ScopedUserManagerEnabler user_manager_enabler_; |
276 em::DeviceStatusReportRequest status_; | 267 em::DeviceStatusReportRequest status_; |
277 scoped_ptr<TestingDeviceStatusCollector> status_collector_; | 268 scoped_ptr<TestingDeviceStatusCollector> status_collector_; |
278 }; | 269 }; |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 // After indicating a successful submit, the submitted status gets cleared, | 501 // After indicating a successful submit, the submitted status gets cleared, |
511 // but what got collected meanwhile sticks around. | 502 // but what got collected meanwhile sticks around. |
512 status_collector_->Simulate(test_states, 1); | 503 status_collector_->Simulate(test_states, 1); |
513 status_collector_->OnSubmittedSuccessfully(); | 504 status_collector_->OnSubmittedSuccessfully(); |
514 GetStatus(); | 505 GetStatus(); |
515 EXPECT_EQ(ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); | 506 EXPECT_EQ(ActivePeriodMilliseconds(), GetActiveMilliseconds(status_)); |
516 } | 507 } |
517 | 508 |
518 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { | 509 TEST_F(DeviceStatusCollectorTest, DevSwitchBootMode) { |
519 // Test that boot mode data is reported by default. | 510 // Test that boot mode data is reported by default. |
520 EXPECT_CALL(statistics_provider_, | 511 fake_statistics_provider_.SetMachineStatistic("devsw_boot", "0"); |
521 GetMachineStatistic("devsw_boot", NotNull())) | |
522 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); | |
523 GetStatus(); | 512 GetStatus(); |
524 EXPECT_EQ("Verified", status_.boot_mode()); | 513 EXPECT_EQ("Verified", status_.boot_mode()); |
525 | 514 |
526 // Test that boot mode data is not reported if the pref turned off. | 515 // Test that boot mode data is not reported if the pref turned off. |
527 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, false); | 516 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, false); |
528 | 517 |
529 EXPECT_CALL(statistics_provider_, | |
530 GetMachineStatistic("devsw_boot", NotNull())) | |
531 .WillRepeatedly(DoAll(SetArgPointee<1>("0"), Return(true))); | |
532 GetStatus(); | 518 GetStatus(); |
533 EXPECT_FALSE(status_.has_boot_mode()); | 519 EXPECT_FALSE(status_.has_boot_mode()); |
534 | 520 |
535 // Turn the pref on, and check that the status is reported iff the | 521 // Turn the pref on, and check that the status is reported iff the |
536 // statistics provider returns valid data. | 522 // statistics provider returns valid data. |
537 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true); | 523 cros_settings_->SetBoolean(chromeos::kReportDeviceBootMode, true); |
538 | 524 |
539 EXPECT_CALL(statistics_provider_, | 525 fake_statistics_provider_.SetMachineStatistic("devsw_boot", "(error)"); |
540 GetMachineStatistic("devsw_boot", NotNull())) | |
541 .WillOnce(DoAll(SetArgPointee<1>("(error)"), Return(true))); | |
542 GetStatus(); | 526 GetStatus(); |
543 EXPECT_FALSE(status_.has_boot_mode()); | 527 EXPECT_FALSE(status_.has_boot_mode()); |
544 | 528 |
545 EXPECT_CALL(statistics_provider_, | 529 fake_statistics_provider_.SetMachineStatistic("devsw_boot", " "); |
546 GetMachineStatistic("devsw_boot", NotNull())) | |
547 .WillOnce(DoAll(SetArgPointee<1>(" "), Return(true))); | |
548 GetStatus(); | 530 GetStatus(); |
549 EXPECT_FALSE(status_.has_boot_mode()); | 531 EXPECT_FALSE(status_.has_boot_mode()); |
550 | 532 |
551 EXPECT_CALL(statistics_provider_, | 533 fake_statistics_provider_.SetMachineStatistic("devsw_boot", "0"); |
552 GetMachineStatistic("devsw_boot", NotNull())) | |
553 .WillOnce(DoAll(SetArgPointee<1>("0"), Return(true))); | |
554 GetStatus(); | 534 GetStatus(); |
555 EXPECT_EQ("Verified", status_.boot_mode()); | 535 EXPECT_EQ("Verified", status_.boot_mode()); |
556 | 536 |
557 EXPECT_CALL(statistics_provider_, | 537 fake_statistics_provider_.SetMachineStatistic("devsw_boot", "1"); |
558 GetMachineStatistic("devsw_boot", NotNull())) | |
559 .WillOnce(DoAll(SetArgPointee<1>("1"), Return(true))); | |
560 GetStatus(); | 538 GetStatus(); |
561 EXPECT_EQ("Dev", status_.boot_mode()); | 539 EXPECT_EQ("Dev", status_.boot_mode()); |
562 } | 540 } |
563 | 541 |
564 TEST_F(DeviceStatusCollectorTest, VersionInfo) { | 542 TEST_F(DeviceStatusCollectorTest, VersionInfo) { |
565 // Expect the version info to be reported by default. | 543 // Expect the version info to be reported by default. |
566 GetStatus(); | 544 GetStatus(); |
567 EXPECT_TRUE(status_.has_browser_version()); | 545 EXPECT_TRUE(status_.has_browser_version()); |
568 EXPECT_TRUE(status_.has_os_version()); | 546 EXPECT_TRUE(status_.has_os_version()); |
569 EXPECT_TRUE(status_.has_firmware_version()); | 547 EXPECT_TRUE(status_.has_firmware_version()); |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 } | 765 } |
788 | 766 |
789 EXPECT_TRUE(found_match) << "No matching interface for fake device " << i; | 767 EXPECT_TRUE(found_match) << "No matching interface for fake device " << i; |
790 count++; | 768 count++; |
791 } | 769 } |
792 | 770 |
793 EXPECT_EQ(count, status_.network_interface_size()); | 771 EXPECT_EQ(count, status_.network_interface_size()); |
794 } | 772 } |
795 | 773 |
796 } // namespace policy | 774 } // namespace policy |
OLD | NEW |