#include "base/timer/hi_res_timer_manager.h"
#include <memory>
#include <utility>
#include "base/base_switches.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/test/power_monitor_test.h"
#include "base/test/scoped_command_line.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
#if BUILDFLAG(IS_WIN)
TEST(HiResTimerManagerTest, ToggleOnOff) {
test::TaskEnvironment task_environment;
base::test::ScopedPowerMonitorTestSource power_monitor_source;
HighResolutionTimerManager manager;
for (int times = 0; times != 3; ++times) {
EXPECT_TRUE(manager.hi_res_clock_available());
EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
base::Time::ActivateHighResolutionTimer(true);
EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
power_monitor_source.GeneratePowerStateEvent(true);
EXPECT_FALSE(manager.hi_res_clock_available());
EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
power_monitor_source.GeneratePowerStateEvent(false);
EXPECT_TRUE(manager.hi_res_clock_available());
EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
base::Time::ActivateHighResolutionTimer(false);
}
}
TEST(HiResTimerManagerTest, DisableFromCommandLine) {
base::test::ScopedCommandLine command_line;
command_line.GetProcessCommandLine()->AppendSwitch(
switches::kDisableHighResTimer);
Time::EnableHighResolutionTimer(false);
HighResolutionTimerManager manager;
EXPECT_FALSE(manager.hi_res_clock_available());
EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
base::Time::ActivateHighResolutionTimer(true);
EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
base::Time::ActivateHighResolutionTimer(false);
Time::EnableHighResolutionTimer(true);
}
#endif
}