| 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 <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 const char kPublicAccountId[] = "public_user@localhost"; | 79 const char kPublicAccountId[] = "public_user@localhost"; |
| 80 | 80 |
| 81 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { | 81 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { |
| 82 public: | 82 public: |
| 83 TestingDeviceStatusCollector( | 83 TestingDeviceStatusCollector( |
| 84 PrefService* local_state, | 84 PrefService* local_state, |
| 85 chromeos::system::StatisticsProvider* provider, | 85 chromeos::system::StatisticsProvider* provider, |
| 86 const policy::DeviceStatusCollector::VolumeInfoFetcher& | 86 const policy::DeviceStatusCollector::VolumeInfoFetcher& |
| 87 volume_info_fetcher, | 87 volume_info_fetcher, |
| 88 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_fetcher, | 88 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_fetcher, |
| 89 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher) | 89 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher, |
| 90 const policy::DeviceStatusCollector::AndroidStatusFetcher& |
| 91 android_status_fetcher) |
| 90 : policy::DeviceStatusCollector(local_state, | 92 : policy::DeviceStatusCollector(local_state, |
| 91 provider, | 93 provider, |
| 92 volume_info_fetcher, | 94 volume_info_fetcher, |
| 93 cpu_fetcher, | 95 cpu_fetcher, |
| 94 cpu_temp_fetcher) { | 96 cpu_temp_fetcher, |
| 97 android_status_fetcher) { |
| 95 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness | 98 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness |
| 96 // due to a single activity period spanning two days. | 99 // due to a single activity period spanning two days. |
| 97 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1)); | 100 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1)); |
| 98 } | 101 } |
| 99 | 102 |
| 100 void Simulate(ui::IdleState* states, int len) { | 103 void Simulate(ui::IdleState* states, int len) { |
| 101 for (int i = 0; i < len; i++) | 104 for (int i = 0; i < len; i++) |
| 102 IdleStateCallback(states[i]); | 105 IdleStateCallback(states[i]); |
| 103 } | 106 } |
| 104 | 107 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 EXPECT_TRUE(found) << "Could not find matching mount point for " | 208 EXPECT_TRUE(found) << "Could not find matching mount point for " |
| 206 << info.volume_id(); | 209 << info.volume_id(); |
| 207 } | 210 } |
| 208 return volume_info; | 211 return volume_info; |
| 209 } | 212 } |
| 210 | 213 |
| 211 std::vector<em::CPUTempInfo> GetEmptyCPUTempInfo() { | 214 std::vector<em::CPUTempInfo> GetEmptyCPUTempInfo() { |
| 212 return std::vector<em::CPUTempInfo>(); | 215 return std::vector<em::CPUTempInfo>(); |
| 213 } | 216 } |
| 214 | 217 |
| 218 std::pair<mojo::String, mojo::String> GetEmptyAndroidStatus() { |
| 219 return std::pair<mojo::String, mojo::String>("", ""); |
| 220 } |
| 221 |
| 215 std::vector<em::CPUTempInfo> GetFakeCPUTempInfo( | 222 std::vector<em::CPUTempInfo> GetFakeCPUTempInfo( |
| 216 const std::vector<em::CPUTempInfo>& cpu_temp_info) { | 223 const std::vector<em::CPUTempInfo>& cpu_temp_info) { |
| 217 return cpu_temp_info; | 224 return cpu_temp_info; |
| 218 } | 225 } |
| 219 | 226 |
| 220 } // namespace | 227 } // namespace |
| 221 | 228 |
| 222 namespace policy { | 229 namespace policy { |
| 223 | 230 |
| 224 // Though it is a unit test, this test is linked with browser_tests so that it | 231 // Though it is a unit test, this test is linked with browser_tests so that it |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 // DiskMountManager takes ownership of the MockDiskMountManager. | 281 // DiskMountManager takes ownership of the MockDiskMountManager. |
| 275 DiskMountManager::InitializeForTesting(mock_disk_mount_manager.release()); | 282 DiskMountManager::InitializeForTesting(mock_disk_mount_manager.release()); |
| 276 TestingDeviceStatusCollector::RegisterPrefs(prefs_.registry()); | 283 TestingDeviceStatusCollector::RegisterPrefs(prefs_.registry()); |
| 277 | 284 |
| 278 settings_helper_.ReplaceProvider(chromeos::kReportDeviceActivityTimes); | 285 settings_helper_.ReplaceProvider(chromeos::kReportDeviceActivityTimes); |
| 279 owner_settings_service_ = | 286 owner_settings_service_ = |
| 280 settings_helper_.CreateOwnerSettingsService(nullptr); | 287 settings_helper_.CreateOwnerSettingsService(nullptr); |
| 281 | 288 |
| 282 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), | 289 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), |
| 283 base::Bind(&GetEmptyCPUStatistics), | 290 base::Bind(&GetEmptyCPUStatistics), |
| 284 base::Bind(&GetEmptyCPUTempInfo)); | 291 base::Bind(&GetEmptyCPUTempInfo), |
| 292 base::Bind(&GetEmptyAndroidStatus)); |
| 285 | 293 |
| 286 // Set up a fake local state for KioskAppManager. | 294 // Set up a fake local state for KioskAppManager. |
| 287 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); | 295 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); |
| 288 chromeos::KioskAppManager::RegisterPrefs(local_state_.registry()); | 296 chromeos::KioskAppManager::RegisterPrefs(local_state_.registry()); |
| 289 | 297 |
| 290 // Use FakeUpdateEngineClient. | 298 // Use FakeUpdateEngineClient. |
| 291 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = | 299 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = |
| 292 chromeos::DBusThreadManager::GetSetterForTesting(); | 300 chromeos::DBusThreadManager::GetSetterForTesting(); |
| 293 dbus_setter->SetUpdateEngineClient( | 301 dbus_setter->SetUpdateEngineClient( |
| 294 base::WrapUnique<chromeos::UpdateEngineClient>(update_engine_client_)); | 302 base::WrapUnique<chromeos::UpdateEngineClient>(update_engine_client_)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 319 false); | 327 false); |
| 320 } | 328 } |
| 321 | 329 |
| 322 void TearDown() override { | 330 void TearDown() override { |
| 323 settings_helper_.RestoreProvider(); | 331 settings_helper_.RestoreProvider(); |
| 324 } | 332 } |
| 325 | 333 |
| 326 void RestartStatusCollector( | 334 void RestartStatusCollector( |
| 327 const policy::DeviceStatusCollector::VolumeInfoFetcher& volume_info, | 335 const policy::DeviceStatusCollector::VolumeInfoFetcher& volume_info, |
| 328 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_stats, | 336 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_stats, |
| 329 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher) { | 337 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher, |
| 338 const policy::DeviceStatusCollector::AndroidStatusFetcher& |
| 339 android_status_fetcher) { |
| 330 std::vector<em::VolumeInfo> expected_volume_info; | 340 std::vector<em::VolumeInfo> expected_volume_info; |
| 331 status_collector_.reset(new TestingDeviceStatusCollector( | 341 status_collector_.reset(new TestingDeviceStatusCollector( |
| 332 &prefs_, &fake_statistics_provider_, volume_info, cpu_stats, | 342 &prefs_, &fake_statistics_provider_, volume_info, cpu_stats, |
| 333 cpu_temp_fetcher)); | 343 cpu_temp_fetcher, android_status_fetcher)); |
| 334 } | 344 } |
| 335 | 345 |
| 336 void GetStatus() { | 346 void GetStatus() { |
| 337 device_status_.Clear(); | 347 device_status_.Clear(); |
| 338 session_status_.Clear(); | 348 session_status_.Clear(); |
| 339 got_session_status_ = false; | 349 got_session_status_ = false; |
| 340 run_loop_.reset(new base::RunLoop()); | 350 run_loop_.reset(new base::RunLoop()); |
| 341 status_collector_->GetDeviceAndSessionStatusAsync(base::Bind( | 351 status_collector_->GetDeviceAndSessionStatusAsync(base::Bind( |
| 342 &DeviceStatusCollectorTest::OnStatusReceived, base::Unretained(this))); | 352 &DeviceStatusCollectorTest::OnStatusReceived, base::Unretained(this))); |
| 343 run_loop_->Run(); | 353 run_loop_->Run(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 }; | 524 }; |
| 515 settings_helper_.SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 525 settings_helper_.SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 516 status_collector_->Simulate(test_states, | 526 status_collector_->Simulate(test_states, |
| 517 sizeof(test_states) / sizeof(ui::IdleState)); | 527 sizeof(test_states) / sizeof(ui::IdleState)); |
| 518 | 528 |
| 519 // Process the list a second time after restarting the collector. It should be | 529 // Process the list a second time after restarting the collector. It should be |
| 520 // able to count the active periods found by the original collector, because | 530 // able to count the active periods found by the original collector, because |
| 521 // the results are stored in a pref. | 531 // the results are stored in a pref. |
| 522 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), | 532 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), |
| 523 base::Bind(&GetEmptyCPUStatistics), | 533 base::Bind(&GetEmptyCPUStatistics), |
| 524 base::Bind(&GetEmptyCPUTempInfo)); | 534 base::Bind(&GetEmptyCPUTempInfo), |
| 535 base::Bind(&GetEmptyAndroidStatus)); |
| 525 status_collector_->Simulate(test_states, | 536 status_collector_->Simulate(test_states, |
| 526 sizeof(test_states) / sizeof(ui::IdleState)); | 537 sizeof(test_states) / sizeof(ui::IdleState)); |
| 527 | 538 |
| 528 GetStatus(); | 539 GetStatus(); |
| 529 EXPECT_EQ(6 * ActivePeriodMilliseconds(), | 540 EXPECT_EQ(6 * ActivePeriodMilliseconds(), |
| 530 GetActiveMilliseconds(device_status_)); | 541 GetActiveMilliseconds(device_status_)); |
| 531 } | 542 } |
| 532 | 543 |
| 533 TEST_F(DeviceStatusCollectorTest, Times) { | 544 TEST_F(DeviceStatusCollectorTest, Times) { |
| 534 ui::IdleState test_states[] = { | 545 ui::IdleState test_states[] = { |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 info.set_volume_id(mount_point); | 825 info.set_volume_id(mount_point); |
| 815 // Just put unique numbers in for storage_total/free. | 826 // Just put unique numbers in for storage_total/free. |
| 816 info.set_storage_total(size++); | 827 info.set_storage_total(size++); |
| 817 info.set_storage_free(size++); | 828 info.set_storage_free(size++); |
| 818 expected_volume_info.push_back(info); | 829 expected_volume_info.push_back(info); |
| 819 } | 830 } |
| 820 EXPECT_FALSE(expected_volume_info.empty()); | 831 EXPECT_FALSE(expected_volume_info.empty()); |
| 821 | 832 |
| 822 RestartStatusCollector(base::Bind(&GetFakeVolumeInfo, expected_volume_info), | 833 RestartStatusCollector(base::Bind(&GetFakeVolumeInfo, expected_volume_info), |
| 823 base::Bind(&GetEmptyCPUStatistics), | 834 base::Bind(&GetEmptyCPUStatistics), |
| 824 base::Bind(&GetEmptyCPUTempInfo)); | 835 base::Bind(&GetEmptyCPUTempInfo), |
| 836 base::Bind(&GetEmptyAndroidStatus)); |
| 825 // Force finishing tasks posted by ctor of DeviceStatusCollector. | 837 // Force finishing tasks posted by ctor of DeviceStatusCollector. |
| 826 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 838 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 827 base::RunLoop().RunUntilIdle(); | 839 base::RunLoop().RunUntilIdle(); |
| 828 | 840 |
| 829 GetStatus(); | 841 GetStatus(); |
| 830 EXPECT_EQ(expected_mount_points.size(), | 842 EXPECT_EQ(expected_mount_points.size(), |
| 831 static_cast<size_t>(device_status_.volume_info_size())); | 843 static_cast<size_t>(device_status_.volume_info_size())); |
| 832 | 844 |
| 833 // Walk the returned VolumeInfo to make sure it matches. | 845 // Walk the returned VolumeInfo to make sure it matches. |
| 834 for (const em::VolumeInfo& expected_info : expected_volume_info) { | 846 for (const em::VolumeInfo& expected_info : expected_volume_info) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 867 // No good way to inject specific test values for available system RAM, so | 879 // No good way to inject specific test values for available system RAM, so |
| 868 // just make sure it's > 0. | 880 // just make sure it's > 0. |
| 869 EXPECT_GT(device_status_.system_ram_total(), 0); | 881 EXPECT_GT(device_status_.system_ram_total(), 0); |
| 870 } | 882 } |
| 871 | 883 |
| 872 TEST_F(DeviceStatusCollectorTest, TestCPUSamples) { | 884 TEST_F(DeviceStatusCollectorTest, TestCPUSamples) { |
| 873 // Mock 100% CPU usage. | 885 // Mock 100% CPU usage. |
| 874 std::string full_cpu_usage("cpu 500 0 500 0 0 0 0"); | 886 std::string full_cpu_usage("cpu 500 0 500 0 0 0 0"); |
| 875 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), | 887 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), |
| 876 base::Bind(&GetFakeCPUStatistics, full_cpu_usage), | 888 base::Bind(&GetFakeCPUStatistics, full_cpu_usage), |
| 877 base::Bind(&GetEmptyCPUTempInfo)); | 889 base::Bind(&GetEmptyCPUTempInfo), |
| 890 base::Bind(&GetEmptyAndroidStatus)); |
| 878 // Force finishing tasks posted by ctor of DeviceStatusCollector. | 891 // Force finishing tasks posted by ctor of DeviceStatusCollector. |
| 879 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 892 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 880 base::RunLoop().RunUntilIdle(); | 893 base::RunLoop().RunUntilIdle(); |
| 881 GetStatus(); | 894 GetStatus(); |
| 882 ASSERT_EQ(1, device_status_.cpu_utilization_pct().size()); | 895 ASSERT_EQ(1, device_status_.cpu_utilization_pct().size()); |
| 883 EXPECT_EQ(100, device_status_.cpu_utilization_pct(0)); | 896 EXPECT_EQ(100, device_status_.cpu_utilization_pct(0)); |
| 884 | 897 |
| 885 // Now sample CPU usage again (active usage counters will not increase | 898 // Now sample CPU usage again (active usage counters will not increase |
| 886 // so should show 0% cpu usage). | 899 // so should show 0% cpu usage). |
| 887 status_collector_->RefreshSampleResourceUsage(); | 900 status_collector_->RefreshSampleResourceUsage(); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 918 int cpu_cnt = 12; | 931 int cpu_cnt = 12; |
| 919 for (int i = 0; i < cpu_cnt; ++i) { | 932 for (int i = 0; i < cpu_cnt; ++i) { |
| 920 em::CPUTempInfo info; | 933 em::CPUTempInfo info; |
| 921 info.set_cpu_temp(i * 10 + 100); | 934 info.set_cpu_temp(i * 10 + 100); |
| 922 info.set_cpu_label(base::StringPrintf("Core %d", i)); | 935 info.set_cpu_label(base::StringPrintf("Core %d", i)); |
| 923 expected_temp_info.push_back(info); | 936 expected_temp_info.push_back(info); |
| 924 } | 937 } |
| 925 | 938 |
| 926 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), | 939 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), |
| 927 base::Bind(&GetEmptyCPUStatistics), | 940 base::Bind(&GetEmptyCPUStatistics), |
| 928 base::Bind(&GetFakeCPUTempInfo, expected_temp_info)); | 941 base::Bind(&GetFakeCPUTempInfo, expected_temp_info), |
| 942 base::Bind(&GetEmptyAndroidStatus)); |
| 929 // Force finishing tasks posted by ctor of DeviceStatusCollector. | 943 // Force finishing tasks posted by ctor of DeviceStatusCollector. |
| 930 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 944 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 931 base::RunLoop().RunUntilIdle(); | 945 base::RunLoop().RunUntilIdle(); |
| 932 | 946 |
| 933 GetStatus(); | 947 GetStatus(); |
| 934 EXPECT_EQ(expected_temp_info.size(), | 948 EXPECT_EQ(expected_temp_info.size(), |
| 935 static_cast<size_t>(device_status_.cpu_temp_info_size())); | 949 static_cast<size_t>(device_status_.cpu_temp_info_size())); |
| 936 | 950 |
| 937 // Walk the returned CPUTempInfo to make sure it matches. | 951 // Walk the returned CPUTempInfo to make sure it matches. |
| 938 for (const em::CPUTempInfo& expected_info : expected_temp_info) { | 952 for (const em::CPUTempInfo& expected_info : expected_temp_info) { |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1407 AccountId::FromUserEmail(kPublicAccountId)); | 1421 AccountId::FromUserEmail(kPublicAccountId)); |
| 1408 EXPECT_CALL(*user_manager_, IsLoggedInAsPublicAccount()) | 1422 EXPECT_CALL(*user_manager_, IsLoggedInAsPublicAccount()) |
| 1409 .WillRepeatedly(Return(true)); | 1423 .WillRepeatedly(Return(true)); |
| 1410 | 1424 |
| 1411 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); | 1425 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); |
| 1412 GetStatus(); | 1426 GetStatus(); |
| 1413 VerifyNetworkReporting(); | 1427 VerifyNetworkReporting(); |
| 1414 } | 1428 } |
| 1415 | 1429 |
| 1416 } // namespace policy | 1430 } // namespace policy |
| OLD | NEW |