Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(826)

Side by Side Diff: chrome/browser/chromeos/policy/device_status_collector_browsertest.cc

Issue 2383763002: Report ARC status asynchronously via DeviceStatusCollector (Closed)
Patch Set: Restructure AndroidStatusFetcher Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/bind.h" 15 #include "base/bind.h"
16 #include "base/environment.h" 16 #include "base/environment.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/macros.h" 18 #include "base/macros.h"
19 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
20 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
21 #include "base/run_loop.h" 21 #include "base/run_loop.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/sys_info.h" 23 #include "base/sys_info.h"
24 #include "base/test/scoped_path_override.h" 24 #include "base/test/scoped_path_override.h"
25 #include "base/threading/sequenced_worker_pool.h" 25 #include "base/threading/sequenced_worker_pool.h"
26 #include "base/threading/thread_task_runner_handle.h"
26 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h" 27 #include "chrome/browser/chromeos/app_mode/kiosk_app_data.h"
27 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 28 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
28 #include "chrome/browser/chromeos/login/users/mock_user_manager.h" 29 #include "chrome/browser/chromeos/login/users/mock_user_manager.h"
29 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 30 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
30 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h" 31 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h"
31 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 32 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
32 #include "chrome/browser/chromeos/policy/device_local_account.h" 33 #include "chrome/browser/chromeos/policy/device_local_account.h"
33 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h" 34 #include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
34 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h" 35 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
35 #include "chrome/common/chrome_paths.h" 36 #include "chrome/common/chrome_paths.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 const char kPublicAccountId[] = "public_user@localhost"; 80 const char kPublicAccountId[] = "public_user@localhost";
80 81
81 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector { 82 class TestingDeviceStatusCollector : public policy::DeviceStatusCollector {
82 public: 83 public:
83 TestingDeviceStatusCollector( 84 TestingDeviceStatusCollector(
84 PrefService* local_state, 85 PrefService* local_state,
85 chromeos::system::StatisticsProvider* provider, 86 chromeos::system::StatisticsProvider* provider,
86 const policy::DeviceStatusCollector::VolumeInfoFetcher& 87 const policy::DeviceStatusCollector::VolumeInfoFetcher&
87 volume_info_fetcher, 88 volume_info_fetcher,
88 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_fetcher, 89 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_fetcher,
89 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher) 90 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher,
91 const policy::DeviceStatusCollector::AndroidStatusFetcher&
92 android_status_fetcher)
90 : policy::DeviceStatusCollector(local_state, 93 : policy::DeviceStatusCollector(local_state,
91 provider, 94 provider,
92 volume_info_fetcher, 95 volume_info_fetcher,
93 cpu_fetcher, 96 cpu_fetcher,
94 cpu_temp_fetcher) { 97 cpu_temp_fetcher,
98 android_status_fetcher) {
95 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness 99 // Set the baseline time to a fixed value (1 AM) to prevent test flakiness
96 // due to a single activity period spanning two days. 100 // due to a single activity period spanning two days.
97 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1)); 101 SetBaselineTime(Time::Now().LocalMidnight() + TimeDelta::FromHours(1));
98 } 102 }
99 103
100 void Simulate(ui::IdleState* states, int len) { 104 void Simulate(ui::IdleState* states, int len) {
101 for (int i = 0; i < len; i++) 105 for (int i = 0; i < len; i++)
102 IdleStateCallback(states[i]); 106 IdleStateCallback(states[i]);
103 } 107 }
104 108
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 EXPECT_TRUE(found) << "Could not find matching mount point for " 209 EXPECT_TRUE(found) << "Could not find matching mount point for "
206 << info.volume_id(); 210 << info.volume_id();
207 } 211 }
208 return volume_info; 212 return volume_info;
209 } 213 }
210 214
211 std::vector<em::CPUTempInfo> GetEmptyCPUTempInfo() { 215 std::vector<em::CPUTempInfo> GetEmptyCPUTempInfo() {
212 return std::vector<em::CPUTempInfo>(); 216 return std::vector<em::CPUTempInfo>();
213 } 217 }
214 218
219 void CallAndroidStatusReceiver(
220 policy::DeviceStatusCollector::AndroidStatusReceiver receiver,
ljusten (tachyonic) 2016/10/05 16:10:42 Can you use const AndroidStatusReceiver&?
phweiss 2016/10/05 16:33:39 Done.
221 mojo::String status,
222 mojo::String droid_guard_info) {
223 receiver.Run(status, droid_guard_info);
224 }
225
226 bool GetEmptyAndroidStatus(
227 policy::DeviceStatusCollector::AndroidStatusReceiver receiver) {
228 // Post it to the thread because this call is expected to be asynchronous.
229 base::ThreadTaskRunnerHandle::Get()->PostTask(
230 FROM_HERE, Bind(&CallAndroidStatusReceiver, receiver, "", ""));
231 return true;
232 }
233
215 std::vector<em::CPUTempInfo> GetFakeCPUTempInfo( 234 std::vector<em::CPUTempInfo> GetFakeCPUTempInfo(
216 const std::vector<em::CPUTempInfo>& cpu_temp_info) { 235 const std::vector<em::CPUTempInfo>& cpu_temp_info) {
217 return cpu_temp_info; 236 return cpu_temp_info;
218 } 237 }
219 238
220 } // namespace 239 } // namespace
221 240
222 namespace policy { 241 namespace policy {
223 242
224 // Though it is a unit test, this test is linked with browser_tests so that it 243 // Though it is a unit test, this test is linked with browser_tests so that it
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 EXPECT_FALSE(external_mount_points.empty()); 291 EXPECT_FALSE(external_mount_points.empty());
273 292
274 // DiskMountManager takes ownership of the MockDiskMountManager. 293 // DiskMountManager takes ownership of the MockDiskMountManager.
275 DiskMountManager::InitializeForTesting(mock_disk_mount_manager.release()); 294 DiskMountManager::InitializeForTesting(mock_disk_mount_manager.release());
276 TestingDeviceStatusCollector::RegisterPrefs(prefs_.registry()); 295 TestingDeviceStatusCollector::RegisterPrefs(prefs_.registry());
277 296
278 settings_helper_.ReplaceProvider(chromeos::kReportDeviceActivityTimes); 297 settings_helper_.ReplaceProvider(chromeos::kReportDeviceActivityTimes);
279 owner_settings_service_ = 298 owner_settings_service_ =
280 settings_helper_.CreateOwnerSettingsService(nullptr); 299 settings_helper_.CreateOwnerSettingsService(nullptr);
281 300
282 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), 301 RestartStatusCollector(
283 base::Bind(&GetEmptyCPUStatistics), 302 base::Bind(&GetEmptyVolumeInfo), base::Bind(&GetEmptyCPUStatistics),
284 base::Bind(&GetEmptyCPUTempInfo)); 303 base::Bind(&GetEmptyCPUTempInfo), base::Bind(&GetEmptyAndroidStatus));
285 304
286 // Set up a fake local state for KioskAppManager. 305 // Set up a fake local state for KioskAppManager.
287 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); 306 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
288 chromeos::KioskAppManager::RegisterPrefs(local_state_.registry()); 307 chromeos::KioskAppManager::RegisterPrefs(local_state_.registry());
289 308
290 // Use FakeUpdateEngineClient. 309 // Use FakeUpdateEngineClient.
291 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = 310 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
292 chromeos::DBusThreadManager::GetSetterForTesting(); 311 chromeos::DBusThreadManager::GetSetterForTesting();
293 dbus_setter->SetUpdateEngineClient( 312 dbus_setter->SetUpdateEngineClient(
294 base::WrapUnique<chromeos::UpdateEngineClient>(update_engine_client_)); 313 base::WrapUnique<chromeos::UpdateEngineClient>(update_engine_client_));
(...skipping 24 matching lines...) Expand all
319 false); 338 false);
320 } 339 }
321 340
322 void TearDown() override { 341 void TearDown() override {
323 settings_helper_.RestoreProvider(); 342 settings_helper_.RestoreProvider();
324 } 343 }
325 344
326 void RestartStatusCollector( 345 void RestartStatusCollector(
327 const policy::DeviceStatusCollector::VolumeInfoFetcher& volume_info, 346 const policy::DeviceStatusCollector::VolumeInfoFetcher& volume_info,
328 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_stats, 347 const policy::DeviceStatusCollector::CPUStatisticsFetcher& cpu_stats,
329 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher) { 348 const policy::DeviceStatusCollector::CPUTempFetcher& cpu_temp_fetcher,
349 const policy::DeviceStatusCollector::AndroidStatusFetcher&
350 android_status_fetcher) {
330 std::vector<em::VolumeInfo> expected_volume_info; 351 std::vector<em::VolumeInfo> expected_volume_info;
331 status_collector_.reset(new TestingDeviceStatusCollector( 352 status_collector_.reset(new TestingDeviceStatusCollector(
332 &prefs_, &fake_statistics_provider_, volume_info, cpu_stats, 353 &prefs_, &fake_statistics_provider_, volume_info, cpu_stats,
333 cpu_temp_fetcher)); 354 cpu_temp_fetcher, android_status_fetcher));
334 } 355 }
335 356
336 void GetStatus() { 357 void GetStatus() {
337 device_status_.Clear(); 358 device_status_.Clear();
338 session_status_.Clear(); 359 session_status_.Clear();
339 got_session_status_ = false; 360 got_session_status_ = false;
340 run_loop_.reset(new base::RunLoop()); 361 run_loop_.reset(new base::RunLoop());
341 status_collector_->GetDeviceAndSessionStatusAsync(base::Bind( 362 status_collector_->GetDeviceAndSessionStatusAsync(base::Bind(
342 &DeviceStatusCollectorTest::OnStatusReceived, base::Unretained(this))); 363 &DeviceStatusCollectorTest::OnStatusReceived, base::Unretained(this)));
343 run_loop_->Run(); 364 run_loop_->Run();
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 ui::IDLE_STATE_IDLE, 533 ui::IDLE_STATE_IDLE,
513 ui::IDLE_STATE_IDLE 534 ui::IDLE_STATE_IDLE
514 }; 535 };
515 settings_helper_.SetBoolean(chromeos::kReportDeviceActivityTimes, true); 536 settings_helper_.SetBoolean(chromeos::kReportDeviceActivityTimes, true);
516 status_collector_->Simulate(test_states, 537 status_collector_->Simulate(test_states,
517 sizeof(test_states) / sizeof(ui::IdleState)); 538 sizeof(test_states) / sizeof(ui::IdleState));
518 539
519 // Process the list a second time after restarting the collector. It should be 540 // 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 541 // able to count the active periods found by the original collector, because
521 // the results are stored in a pref. 542 // the results are stored in a pref.
522 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), 543 RestartStatusCollector(
523 base::Bind(&GetEmptyCPUStatistics), 544 base::Bind(&GetEmptyVolumeInfo), base::Bind(&GetEmptyCPUStatistics),
524 base::Bind(&GetEmptyCPUTempInfo)); 545 base::Bind(&GetEmptyCPUTempInfo), base::Bind(&GetEmptyAndroidStatus));
525 status_collector_->Simulate(test_states, 546 status_collector_->Simulate(test_states,
526 sizeof(test_states) / sizeof(ui::IdleState)); 547 sizeof(test_states) / sizeof(ui::IdleState));
527 548
528 GetStatus(); 549 GetStatus();
529 EXPECT_EQ(6 * ActivePeriodMilliseconds(), 550 EXPECT_EQ(6 * ActivePeriodMilliseconds(),
530 GetActiveMilliseconds(device_status_)); 551 GetActiveMilliseconds(device_status_));
531 } 552 }
532 553
533 TEST_F(DeviceStatusCollectorTest, Times) { 554 TEST_F(DeviceStatusCollectorTest, Times) {
534 ui::IdleState test_states[] = { 555 ui::IdleState test_states[] = {
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
814 info.set_volume_id(mount_point); 835 info.set_volume_id(mount_point);
815 // Just put unique numbers in for storage_total/free. 836 // Just put unique numbers in for storage_total/free.
816 info.set_storage_total(size++); 837 info.set_storage_total(size++);
817 info.set_storage_free(size++); 838 info.set_storage_free(size++);
818 expected_volume_info.push_back(info); 839 expected_volume_info.push_back(info);
819 } 840 }
820 EXPECT_FALSE(expected_volume_info.empty()); 841 EXPECT_FALSE(expected_volume_info.empty());
821 842
822 RestartStatusCollector(base::Bind(&GetFakeVolumeInfo, expected_volume_info), 843 RestartStatusCollector(base::Bind(&GetFakeVolumeInfo, expected_volume_info),
823 base::Bind(&GetEmptyCPUStatistics), 844 base::Bind(&GetEmptyCPUStatistics),
824 base::Bind(&GetEmptyCPUTempInfo)); 845 base::Bind(&GetEmptyCPUTempInfo),
846 base::Bind(&GetEmptyAndroidStatus));
825 // Force finishing tasks posted by ctor of DeviceStatusCollector. 847 // Force finishing tasks posted by ctor of DeviceStatusCollector.
826 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 848 content::BrowserThread::GetBlockingPool()->FlushForTesting();
827 base::RunLoop().RunUntilIdle(); 849 base::RunLoop().RunUntilIdle();
828 850
829 GetStatus(); 851 GetStatus();
830 EXPECT_EQ(expected_mount_points.size(), 852 EXPECT_EQ(expected_mount_points.size(),
831 static_cast<size_t>(device_status_.volume_info_size())); 853 static_cast<size_t>(device_status_.volume_info_size()));
832 854
833 // Walk the returned VolumeInfo to make sure it matches. 855 // Walk the returned VolumeInfo to make sure it matches.
834 for (const em::VolumeInfo& expected_info : expected_volume_info) { 856 for (const em::VolumeInfo& expected_info : expected_volume_info) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 // No good way to inject specific test values for available system RAM, so 889 // No good way to inject specific test values for available system RAM, so
868 // just make sure it's > 0. 890 // just make sure it's > 0.
869 EXPECT_GT(device_status_.system_ram_total(), 0); 891 EXPECT_GT(device_status_.system_ram_total(), 0);
870 } 892 }
871 893
872 TEST_F(DeviceStatusCollectorTest, TestCPUSamples) { 894 TEST_F(DeviceStatusCollectorTest, TestCPUSamples) {
873 // Mock 100% CPU usage. 895 // Mock 100% CPU usage.
874 std::string full_cpu_usage("cpu 500 0 500 0 0 0 0"); 896 std::string full_cpu_usage("cpu 500 0 500 0 0 0 0");
875 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), 897 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo),
876 base::Bind(&GetFakeCPUStatistics, full_cpu_usage), 898 base::Bind(&GetFakeCPUStatistics, full_cpu_usage),
877 base::Bind(&GetEmptyCPUTempInfo)); 899 base::Bind(&GetEmptyCPUTempInfo),
900 base::Bind(&GetEmptyAndroidStatus));
878 // Force finishing tasks posted by ctor of DeviceStatusCollector. 901 // Force finishing tasks posted by ctor of DeviceStatusCollector.
879 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 902 content::BrowserThread::GetBlockingPool()->FlushForTesting();
880 base::RunLoop().RunUntilIdle(); 903 base::RunLoop().RunUntilIdle();
881 GetStatus(); 904 GetStatus();
882 ASSERT_EQ(1, device_status_.cpu_utilization_pct().size()); 905 ASSERT_EQ(1, device_status_.cpu_utilization_pct().size());
883 EXPECT_EQ(100, device_status_.cpu_utilization_pct(0)); 906 EXPECT_EQ(100, device_status_.cpu_utilization_pct(0));
884 907
885 // Now sample CPU usage again (active usage counters will not increase 908 // Now sample CPU usage again (active usage counters will not increase
886 // so should show 0% cpu usage). 909 // so should show 0% cpu usage).
887 status_collector_->RefreshSampleResourceUsage(); 910 status_collector_->RefreshSampleResourceUsage();
(...skipping 30 matching lines...) Expand all
918 int cpu_cnt = 12; 941 int cpu_cnt = 12;
919 for (int i = 0; i < cpu_cnt; ++i) { 942 for (int i = 0; i < cpu_cnt; ++i) {
920 em::CPUTempInfo info; 943 em::CPUTempInfo info;
921 info.set_cpu_temp(i * 10 + 100); 944 info.set_cpu_temp(i * 10 + 100);
922 info.set_cpu_label(base::StringPrintf("Core %d", i)); 945 info.set_cpu_label(base::StringPrintf("Core %d", i));
923 expected_temp_info.push_back(info); 946 expected_temp_info.push_back(info);
924 } 947 }
925 948
926 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo), 949 RestartStatusCollector(base::Bind(&GetEmptyVolumeInfo),
927 base::Bind(&GetEmptyCPUStatistics), 950 base::Bind(&GetEmptyCPUStatistics),
928 base::Bind(&GetFakeCPUTempInfo, expected_temp_info)); 951 base::Bind(&GetFakeCPUTempInfo, expected_temp_info),
952 base::Bind(&GetEmptyAndroidStatus));
929 // Force finishing tasks posted by ctor of DeviceStatusCollector. 953 // Force finishing tasks posted by ctor of DeviceStatusCollector.
930 content::BrowserThread::GetBlockingPool()->FlushForTesting(); 954 content::BrowserThread::GetBlockingPool()->FlushForTesting();
931 base::RunLoop().RunUntilIdle(); 955 base::RunLoop().RunUntilIdle();
932 956
933 GetStatus(); 957 GetStatus();
934 EXPECT_EQ(expected_temp_info.size(), 958 EXPECT_EQ(expected_temp_info.size(),
935 static_cast<size_t>(device_status_.cpu_temp_info_size())); 959 static_cast<size_t>(device_status_.cpu_temp_info_size()));
936 960
937 // Walk the returned CPUTempInfo to make sure it matches. 961 // Walk the returned CPUTempInfo to make sure it matches.
938 for (const em::CPUTempInfo& expected_info : expected_temp_info) { 962 for (const em::CPUTempInfo& expected_info : expected_temp_info) {
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 AccountId::FromUserEmail(kPublicAccountId)); 1431 AccountId::FromUserEmail(kPublicAccountId));
1408 EXPECT_CALL(*user_manager_, IsLoggedInAsPublicAccount()) 1432 EXPECT_CALL(*user_manager_, IsLoggedInAsPublicAccount())
1409 .WillRepeatedly(Return(true)); 1433 .WillRepeatedly(Return(true));
1410 1434
1411 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true); 1435 settings_helper_.SetBoolean(chromeos::kReportDeviceNetworkInterfaces, true);
1412 GetStatus(); 1436 GetStatus();
1413 VerifyNetworkReporting(); 1437 VerifyNetworkReporting();
1414 } 1438 }
1415 1439
1416 } // namespace policy 1440 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698