1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/timer/hi_res_timer_manager.h" 6 7 #include <memory> 8 #include <utility> 9 10 #include "base/base_switches.h" 11 #include "base/power_monitor/power_monitor.h" 12 #include "base/power_monitor/power_monitor_device_source.h" 13 #include "base/test/power_monitor_test.h" 14 #include "base/test/scoped_command_line.h" 15 #include "base/test/task_environment.h" 16 #include "base/time/time.h" 17 #include "build/build_config.h" 18 #include "testing/gtest/include/gtest/gtest.h" 19 20 namespace base { 21 22 #if BUILDFLAG(IS_WIN) TEST(HiResTimerManagerTest,ToggleOnOff)23TEST(HiResTimerManagerTest, ToggleOnOff) { 24 test::TaskEnvironment task_environment; 25 base::test::ScopedPowerMonitorTestSource power_monitor_source; 26 27 HighResolutionTimerManager manager; 28 29 // Loop a few times to test power toggling. 30 for (int times = 0; times != 3; ++times) { 31 // The manager has the high resolution clock enabled now. 32 EXPECT_TRUE(manager.hi_res_clock_available()); 33 // But the Time class has it off, because it hasn't been activated. 34 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 35 36 // Activate the high resolution timer. 37 base::Time::ActivateHighResolutionTimer(true); 38 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse()); 39 40 // Simulate a on-battery power event. 41 power_monitor_source.GeneratePowerStateEvent( 42 PowerStateObserver::BatteryPowerStatus::kBatteryPower); 43 44 EXPECT_FALSE(manager.hi_res_clock_available()); 45 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 46 47 // Back to on-AC power. 48 power_monitor_source.GeneratePowerStateEvent( 49 PowerStateObserver::BatteryPowerStatus::kExternalPower); 50 EXPECT_TRUE(manager.hi_res_clock_available()); 51 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse()); 52 53 // De-activate the high resolution timer. 54 base::Time::ActivateHighResolutionTimer(false); 55 } 56 } 57 TEST(HiResTimerManagerTest,DisableFromCommandLine)58TEST(HiResTimerManagerTest, DisableFromCommandLine) { 59 base::test::ScopedCommandLine command_line; 60 command_line.GetProcessCommandLine()->AppendSwitch( 61 switches::kDisableHighResTimer); 62 63 // Reset to known initial state. Test suite implementation 64 // enables the high resolution timer by default. 65 Time::EnableHighResolutionTimer(false); 66 67 HighResolutionTimerManager manager; 68 69 // The high resolution clock is disabled via the command line flag. 70 EXPECT_FALSE(manager.hi_res_clock_available()); 71 72 // Time class has it off as well, because it hasn't been activated. 73 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 74 75 // Try to activate the high resolution timer. 76 base::Time::ActivateHighResolutionTimer(true); 77 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 78 79 // De-activate the high resolution timer. 80 base::Time::ActivateHighResolutionTimer(false); 81 82 // Re-enable the high-resolution timer for testing. 83 Time::EnableHighResolutionTimer(true); 84 } 85 #endif // BUILDFLAG(IS_WIN) 86 87 } // namespace base 88