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(true); 42 43 EXPECT_FALSE(manager.hi_res_clock_available()); 44 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 45 46 // Back to on-AC power. 47 power_monitor_source.GeneratePowerStateEvent(false); 48 EXPECT_TRUE(manager.hi_res_clock_available()); 49 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse()); 50 51 // De-activate the high resolution timer. 52 base::Time::ActivateHighResolutionTimer(false); 53 } 54 } 55 TEST(HiResTimerManagerTest,DisableFromCommandLine)56TEST(HiResTimerManagerTest, DisableFromCommandLine) { 57 base::test::ScopedCommandLine command_line; 58 command_line.GetProcessCommandLine()->AppendSwitch( 59 switches::kDisableHighResTimer); 60 61 // Reset to known initial state. Test suite implementation 62 // enables the high resolution timer by default. 63 Time::EnableHighResolutionTimer(false); 64 65 HighResolutionTimerManager manager; 66 67 // The high resolution clock is disabled via the command line flag. 68 EXPECT_FALSE(manager.hi_res_clock_available()); 69 70 // Time class has it off as well, because it hasn't been activated. 71 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 72 73 // Try to activate the high resolution timer. 74 base::Time::ActivateHighResolutionTimer(true); 75 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); 76 77 // De-activate the high resolution timer. 78 base::Time::ActivateHighResolutionTimer(false); 79 80 // Re-enable the high-resolution timer for testing. 81 Time::EnableHighResolutionTimer(true); 82 } 83 #endif // BUILDFLAG(IS_WIN) 84 85 } // namespace base 86