| 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 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 mount_point, mount_point, chromeos::MOUNT_TYPE_DEVICE, | 327 mount_point, mount_point, chromeos::MOUNT_TYPE_DEVICE, |
| 328 chromeos::disks::MOUNT_CONDITION_NONE))); | 328 chromeos::disks::MOUNT_CONDITION_NONE))); |
| 329 } | 329 } |
| 330 | 330 |
| 331 ~DeviceStatusCollectorTest() override { | 331 ~DeviceStatusCollectorTest() override { |
| 332 chromeos::KioskAppManager::Shutdown(); | 332 chromeos::KioskAppManager::Shutdown(); |
| 333 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); | 333 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); |
| 334 | 334 |
| 335 // Finish pending tasks. | 335 // Finish pending tasks. |
| 336 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 336 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 337 message_loop_.RunUntilIdle(); | 337 base::RunLoop().RunUntilIdle(); |
| 338 storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems(); | 338 storage::ExternalMountPoints::GetSystemInstance()->RevokeAllFileSystems(); |
| 339 DiskMountManager::Shutdown(); | 339 DiskMountManager::Shutdown(); |
| 340 } | 340 } |
| 341 | 341 |
| 342 void SetUp() override { | 342 void SetUp() override { |
| 343 // Disable network interface reporting since it requires additional setup. | 343 // Disable network interface reporting since it requires additional setup. |
| 344 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, | 344 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, |
| 345 false); | 345 false); |
| 346 } | 346 } |
| 347 | 347 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 manager->SetEnableAutoLaunch(true); | 467 manager->SetEnableAutoLaunch(true); |
| 468 | 468 |
| 469 std::vector<DeviceLocalAccount> accounts; | 469 std::vector<DeviceLocalAccount> accounts; |
| 470 accounts.push_back(auto_launch_app_account); | 470 accounts.push_back(auto_launch_app_account); |
| 471 SetDeviceLocalAccounts(owner_settings_service_.get(), accounts); | 471 SetDeviceLocalAccounts(owner_settings_service_.get(), accounts); |
| 472 | 472 |
| 473 owner_settings_service_->SetString( | 473 owner_settings_service_->SetString( |
| 474 chromeos::kAccountsPrefDeviceLocalAccountAutoLoginId, | 474 chromeos::kAccountsPrefDeviceLocalAccountAutoLoginId, |
| 475 auto_launch_app_account.account_id); | 475 auto_launch_app_account.account_id); |
| 476 | 476 |
| 477 message_loop_.RunUntilIdle(); | 477 base::RunLoop().RunUntilIdle(); |
| 478 | 478 |
| 479 ASSERT_EQ(required_platform_version, | 479 ASSERT_EQ(required_platform_version, |
| 480 manager->GetAutoLaunchAppRequiredPlatformVersion()); | 480 manager->GetAutoLaunchAppRequiredPlatformVersion()); |
| 481 } | 481 } |
| 482 | 482 |
| 483 protected: | 483 protected: |
| 484 // Convenience method. | 484 // Convenience method. |
| 485 int64_t ActivePeriodMilliseconds() { | 485 int64_t ActivePeriodMilliseconds() { |
| 486 return policy::DeviceStatusCollector::kIdlePollIntervalSeconds * 1000; | 486 return policy::DeviceStatusCollector::kIdlePollIntervalSeconds * 1000; |
| 487 } | 487 } |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 kMillisecondsPerDay); | 735 kMillisecondsPerDay); |
| 736 EXPECT_EQ(time_period1.end_timestamp() - time_period1.start_timestamp(), | 736 EXPECT_EQ(time_period1.end_timestamp() - time_period1.start_timestamp(), |
| 737 kMillisecondsPerDay); | 737 kMillisecondsPerDay); |
| 738 } | 738 } |
| 739 | 739 |
| 740 TEST_F(DeviceStatusCollectorTest, ActivityTimesKeptUntilSubmittedSuccessfully) { | 740 TEST_F(DeviceStatusCollectorTest, ActivityTimesKeptUntilSubmittedSuccessfully) { |
| 741 ui::IdleState test_states[] = { | 741 ui::IdleState test_states[] = { |
| 742 ui::IDLE_STATE_ACTIVE, | 742 ui::IDLE_STATE_ACTIVE, |
| 743 ui::IDLE_STATE_ACTIVE, | 743 ui::IDLE_STATE_ACTIVE, |
| 744 }; | 744 }; |
| 745 message_loop_.RunUntilIdle(); | 745 base::RunLoop().RunUntilIdle(); |
| 746 settings_helper_.SetBoolean(chromeos::kReportDeviceActivityTimes, true); | 746 settings_helper_.SetBoolean(chromeos::kReportDeviceActivityTimes, true); |
| 747 | 747 |
| 748 status_collector_->Simulate(test_states, 2); | 748 status_collector_->Simulate(test_states, 2); |
| 749 GetDeviceStatus(); | 749 GetDeviceStatus(); |
| 750 EXPECT_EQ(2 * ActivePeriodMilliseconds(), | 750 EXPECT_EQ(2 * ActivePeriodMilliseconds(), |
| 751 GetActiveMilliseconds(device_status_)); | 751 GetActiveMilliseconds(device_status_)); |
| 752 em::DeviceStatusReportRequest first_status(device_status_); | 752 em::DeviceStatusReportRequest first_status(device_status_); |
| 753 | 753 |
| 754 // The collector returns the same status again. | 754 // The collector returns the same status again. |
| 755 GetDeviceStatus(); | 755 GetDeviceStatus(); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 base::Bind(&GetEmptyCPUStatistics), | 862 base::Bind(&GetEmptyCPUStatistics), |
| 863 base::Bind(&GetEmptyCPUTempInfo)); | 863 base::Bind(&GetEmptyCPUTempInfo)); |
| 864 CheckThatAValidLocationIsReported(); | 864 CheckThatAValidLocationIsReported(); |
| 865 EXPECT_TRUE(mock_position_to_return_next.get()); | 865 EXPECT_TRUE(mock_position_to_return_next.get()); |
| 866 | 866 |
| 867 // Check that after disabling location reporting again, the last known | 867 // Check that after disabling location reporting again, the last known |
| 868 // location has been cleared from local state and is no longer reported. | 868 // location has been cleared from local state and is no longer reported. |
| 869 SetMockPositionToReturnNext(valid_fix); | 869 SetMockPositionToReturnNext(valid_fix); |
| 870 settings_helper_.SetBoolean(chromeos::kReportDeviceLocation, false); | 870 settings_helper_.SetBoolean(chromeos::kReportDeviceLocation, false); |
| 871 // Allow the new pref to propagate to the status collector. | 871 // Allow the new pref to propagate to the status collector. |
| 872 message_loop_.RunUntilIdle(); | 872 base::RunLoop().RunUntilIdle(); |
| 873 EXPECT_TRUE(prefs_.GetDictionary(prefs::kDeviceLocation)->empty()); | 873 EXPECT_TRUE(prefs_.GetDictionary(prefs::kDeviceLocation)->empty()); |
| 874 CheckThatNoLocationIsReported(); | 874 CheckThatNoLocationIsReported(); |
| 875 | 875 |
| 876 // Check that after enabling location reporting again, an error is reported | 876 // Check that after enabling location reporting again, an error is reported |
| 877 // if no valid fix is available. | 877 // if no valid fix is available. |
| 878 SetMockPositionToReturnNext(invalid_fix); | 878 SetMockPositionToReturnNext(invalid_fix); |
| 879 settings_helper_.SetBoolean(chromeos::kReportDeviceLocation, true); | 879 settings_helper_.SetBoolean(chromeos::kReportDeviceLocation, true); |
| 880 // Allow the new pref to propagate to the status collector. | 880 // Allow the new pref to propagate to the status collector. |
| 881 message_loop_.RunUntilIdle(); | 881 base::RunLoop().RunUntilIdle(); |
| 882 CheckThatALocationErrorIsReported(); | 882 CheckThatALocationErrorIsReported(); |
| 883 } | 883 } |
| 884 | 884 |
| 885 TEST_F(DeviceStatusCollectorTest, ReportUsers) { | 885 TEST_F(DeviceStatusCollectorTest, ReportUsers) { |
| 886 const AccountId public_account_id( | 886 const AccountId public_account_id( |
| 887 AccountId::FromUserEmail("public@localhost")); | 887 AccountId::FromUserEmail("public@localhost")); |
| 888 const AccountId account_id0(AccountId::FromUserEmail("user0@managed.com")); | 888 const AccountId account_id0(AccountId::FromUserEmail("user0@managed.com")); |
| 889 const AccountId account_id1(AccountId::FromUserEmail("user1@managed.com")); | 889 const AccountId account_id1(AccountId::FromUserEmail("user1@managed.com")); |
| 890 const AccountId account_id2(AccountId::FromUserEmail("user2@managed.com")); | 890 const AccountId account_id2(AccountId::FromUserEmail("user2@managed.com")); |
| 891 const AccountId account_id3(AccountId::FromUserEmail("user3@unmanaged.com")); | 891 const AccountId account_id3(AccountId::FromUserEmail("user3@unmanaged.com")); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 945 info.set_storage_free(size++); | 945 info.set_storage_free(size++); |
| 946 expected_volume_info.push_back(info); | 946 expected_volume_info.push_back(info); |
| 947 } | 947 } |
| 948 EXPECT_FALSE(expected_volume_info.empty()); | 948 EXPECT_FALSE(expected_volume_info.empty()); |
| 949 | 949 |
| 950 RestartStatusCollector(base::Bind(&GetFakeVolumeInfo, expected_volume_info), | 950 RestartStatusCollector(base::Bind(&GetFakeVolumeInfo, expected_volume_info), |
| 951 base::Bind(&GetEmptyCPUStatistics), | 951 base::Bind(&GetEmptyCPUStatistics), |
| 952 base::Bind(&GetEmptyCPUTempInfo)); | 952 base::Bind(&GetEmptyCPUTempInfo)); |
| 953 // Force finishing tasks posted by ctor of DeviceStatusCollector. | 953 // Force finishing tasks posted by ctor of DeviceStatusCollector. |
| 954 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 954 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 955 message_loop_.RunUntilIdle(); | 955 base::RunLoop().RunUntilIdle(); |
| 956 | 956 |
| 957 GetDeviceStatus(); | 957 GetDeviceStatus(); |
| 958 EXPECT_EQ(expected_mount_points.size(), | 958 EXPECT_EQ(expected_mount_points.size(), |
| 959 static_cast<size_t>(device_status_.volume_info_size())); | 959 static_cast<size_t>(device_status_.volume_info_size())); |
| 960 | 960 |
| 961 // Walk the returned VolumeInfo to make sure it matches. | 961 // Walk the returned VolumeInfo to make sure it matches. |
| 962 for (const em::VolumeInfo& expected_info : expected_volume_info) { | 962 for (const em::VolumeInfo& expected_info : expected_volume_info) { |
| 963 bool found = false; | 963 bool found = false; |
| 964 for (const em::VolumeInfo& info : device_status_.volume_info()) { | 964 for (const em::VolumeInfo& info : device_status_.volume_info()) { |
| 965 if (info.volume_id() == expected_info.volume_id()) { | 965 if (info.volume_id() == expected_info.volume_id()) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 979 EXPECT_EQ(0, device_status_.volume_info_size()); | 979 EXPECT_EQ(0, device_status_.volume_info_size()); |
| 980 } | 980 } |
| 981 | 981 |
| 982 TEST_F(DeviceStatusCollectorTest, TestAvailableMemory) { | 982 TEST_F(DeviceStatusCollectorTest, TestAvailableMemory) { |
| 983 // Refresh our samples. Sample more than kMaxHardwareSamples times to | 983 // Refresh our samples. Sample more than kMaxHardwareSamples times to |
| 984 // make sure that the code correctly caps the number of cached samples. | 984 // make sure that the code correctly caps the number of cached samples. |
| 985 for (int i = 0; i < static_cast<int>( | 985 for (int i = 0; i < static_cast<int>( |
| 986 DeviceStatusCollector::kMaxResourceUsageSamples + 1); | 986 DeviceStatusCollector::kMaxResourceUsageSamples + 1); |
| 987 ++i) { | 987 ++i) { |
| 988 status_collector_->RefreshSampleResourceUsage(); | 988 status_collector_->RefreshSampleResourceUsage(); |
| 989 message_loop_.RunUntilIdle(); | 989 base::RunLoop().RunUntilIdle(); |
| 990 } | 990 } |
| 991 GetDeviceStatus(); | 991 GetDeviceStatus(); |
| 992 EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples), | 992 EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples), |
| 993 device_status_.system_ram_free().size()); | 993 device_status_.system_ram_free().size()); |
| 994 EXPECT_TRUE(device_status_.has_system_ram_total()); | 994 EXPECT_TRUE(device_status_.has_system_ram_total()); |
| 995 // No good way to inject specific test values for available system RAM, so | 995 // No good way to inject specific test values for available system RAM, so |
| 996 // just make sure it's > 0. | 996 // just make sure it's > 0. |
| 997 EXPECT_GT(device_status_.system_ram_total(), 0); | 997 EXPECT_GT(device_status_.system_ram_total(), 0); |
| 998 } | 998 } |
| 999 | 999 |
| 1000 TEST_F(DeviceStatusCollectorTest, TestCPUSamples) { | 1000 TEST_F(DeviceStatusCollectorTest, TestCPUSamples) { |
| 1001 // Mock 100% CPU usage. | 1001 // Mock 100% CPU usage. |
| 1002 std::string full_cpu_usage("cpu 500 0 500 0 0 0 0"); | 1002 std::string full_cpu_usage("cpu 500 0 500 0 0 0 0"); |
| 1003 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), | 1003 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), |
| 1004 base::Bind(&GetFakeCPUStatistics, full_cpu_usage), | 1004 base::Bind(&GetFakeCPUStatistics, full_cpu_usage), |
| 1005 base::Bind(&GetEmptyCPUTempInfo)); | 1005 base::Bind(&GetEmptyCPUTempInfo)); |
| 1006 // Force finishing tasks posted by ctor of DeviceStatusCollector. | 1006 // Force finishing tasks posted by ctor of DeviceStatusCollector. |
| 1007 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 1007 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 1008 message_loop_.RunUntilIdle(); | 1008 base::RunLoop().RunUntilIdle(); |
| 1009 GetDeviceStatus(); | 1009 GetDeviceStatus(); |
| 1010 ASSERT_EQ(1, device_status_.cpu_utilization_pct().size()); | 1010 ASSERT_EQ(1, device_status_.cpu_utilization_pct().size()); |
| 1011 EXPECT_EQ(100, device_status_.cpu_utilization_pct(0)); | 1011 EXPECT_EQ(100, device_status_.cpu_utilization_pct(0)); |
| 1012 | 1012 |
| 1013 // Now sample CPU usage again (active usage counters will not increase | 1013 // Now sample CPU usage again (active usage counters will not increase |
| 1014 // so should show 0% cpu usage). | 1014 // so should show 0% cpu usage). |
| 1015 status_collector_->RefreshSampleResourceUsage(); | 1015 status_collector_->RefreshSampleResourceUsage(); |
| 1016 message_loop_.RunUntilIdle(); | 1016 base::RunLoop().RunUntilIdle(); |
| 1017 GetDeviceStatus(); | 1017 GetDeviceStatus(); |
| 1018 ASSERT_EQ(2, device_status_.cpu_utilization_pct().size()); | 1018 ASSERT_EQ(2, device_status_.cpu_utilization_pct().size()); |
| 1019 EXPECT_EQ(0, device_status_.cpu_utilization_pct(1)); | 1019 EXPECT_EQ(0, device_status_.cpu_utilization_pct(1)); |
| 1020 | 1020 |
| 1021 // Now store a bunch of 0% cpu usage and make sure we cap the max number of | 1021 // Now store a bunch of 0% cpu usage and make sure we cap the max number of |
| 1022 // samples. | 1022 // samples. |
| 1023 for (int i = 0; | 1023 for (int i = 0; |
| 1024 i < static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples); | 1024 i < static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples); |
| 1025 ++i) { | 1025 ++i) { |
| 1026 status_collector_->RefreshSampleResourceUsage(); | 1026 status_collector_->RefreshSampleResourceUsage(); |
| 1027 message_loop_.RunUntilIdle(); | 1027 base::RunLoop().RunUntilIdle(); |
| 1028 } | 1028 } |
| 1029 GetDeviceStatus(); | 1029 GetDeviceStatus(); |
| 1030 | 1030 |
| 1031 // Should not be more than kMaxResourceUsageSamples, and they should all show | 1031 // Should not be more than kMaxResourceUsageSamples, and they should all show |
| 1032 // the CPU is idle. | 1032 // the CPU is idle. |
| 1033 EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples), | 1033 EXPECT_EQ(static_cast<int>(DeviceStatusCollector::kMaxResourceUsageSamples), |
| 1034 device_status_.cpu_utilization_pct().size()); | 1034 device_status_.cpu_utilization_pct().size()); |
| 1035 for (const auto utilization : device_status_.cpu_utilization_pct()) | 1035 for (const auto utilization : device_status_.cpu_utilization_pct()) |
| 1036 EXPECT_EQ(0, utilization); | 1036 EXPECT_EQ(0, utilization); |
| 1037 | 1037 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1049 info.set_cpu_temp(i * 10 + 100); | 1049 info.set_cpu_temp(i * 10 + 100); |
| 1050 info.set_cpu_label(base::StringPrintf("Core %d", i)); | 1050 info.set_cpu_label(base::StringPrintf("Core %d", i)); |
| 1051 expected_temp_info.push_back(info); | 1051 expected_temp_info.push_back(info); |
| 1052 } | 1052 } |
| 1053 | 1053 |
| 1054 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), | 1054 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), |
| 1055 base::Bind(&GetEmptyCPUStatistics), | 1055 base::Bind(&GetEmptyCPUStatistics), |
| 1056 base::Bind(&GetFakeCPUTempInfo, expected_temp_info)); | 1056 base::Bind(&GetFakeCPUTempInfo, expected_temp_info)); |
| 1057 // Force finishing tasks posted by ctor of DeviceStatusCollector. | 1057 // Force finishing tasks posted by ctor of DeviceStatusCollector. |
| 1058 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 1058 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 1059 message_loop_.RunUntilIdle(); | 1059 base::RunLoop().RunUntilIdle(); |
| 1060 | 1060 |
| 1061 GetDeviceStatus(); | 1061 GetDeviceStatus(); |
| 1062 EXPECT_EQ(expected_temp_info.size(), | 1062 EXPECT_EQ(expected_temp_info.size(), |
| 1063 static_cast<size_t>(device_status_.cpu_temp_info_size())); | 1063 static_cast<size_t>(device_status_.cpu_temp_info_size())); |
| 1064 | 1064 |
| 1065 // Walk the returned CPUTempInfo to make sure it matches. | 1065 // Walk the returned CPUTempInfo to make sure it matches. |
| 1066 for (const em::CPUTempInfo& expected_info : expected_temp_info) { | 1066 for (const em::CPUTempInfo& expected_info : expected_temp_info) { |
| 1067 bool found = false; | 1067 bool found = false; |
| 1068 for (const em::CPUTempInfo& info : device_status_.cpu_temp_info()) { | 1068 for (const em::CPUTempInfo& info : device_status_.cpu_temp_info()) { |
| 1069 if (info.cpu_label() == expected_info.cpu_label()) { | 1069 if (info.cpu_label() == expected_info.cpu_label()) { |
| (...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1535 AccountId::FromUserEmail(kPublicAccountId)); | 1535 AccountId::FromUserEmail(kPublicAccountId)); |
| 1536 EXPECT_CALL(*user_manager_, IsLoggedInAsPublicAccount()) | 1536 EXPECT_CALL(*user_manager_, IsLoggedInAsPublicAccount()) |
| 1537 .WillRepeatedly(Return(true)); | 1537 .WillRepeatedly(Return(true)); |
| 1538 | 1538 |
| 1539 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); | 1539 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); |
| 1540 GetDeviceStatus(); | 1540 GetDeviceStatus(); |
| 1541 VerifyNetworkReporting(); | 1541 VerifyNetworkReporting(); |
| 1542 } | 1542 } |
| 1543 | 1543 |
| 1544 } // namespace policy | 1544 } // namespace policy |
| OLD | NEW |