Home | History | Annotate | Download | only in timer
      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
      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/power_monitor/power_monitor.h"
     11 #include "base/power_monitor/power_monitor_device_source.h"
     12 #include "base/test/scoped_task_environment.h"
     13 #include "base/time/time.h"
     14 #include "build/build_config.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace base {
     18 
     19 #if defined(OS_WIN)
     20 TEST(HiResTimerManagerTest, ToggleOnOff) {
     21   // The power monitor creates Window to receive power notifications from
     22   // Windows, which makes this test flaky if you run while the machine
     23   // goes in or out of AC power.
     24   test::ScopedTaskEnvironment scoped_task_environment(
     25       test::ScopedTaskEnvironment::MainThreadType::UI);
     26   std::unique_ptr<base::PowerMonitorSource> power_monitor_source(
     27       new base::PowerMonitorDeviceSource());
     28   std::unique_ptr<base::PowerMonitor> power_monitor(
     29       new base::PowerMonitor(std::move(power_monitor_source)));
     30 
     31   HighResolutionTimerManager manager;
     32   // Simulate a on-AC power event to get to a known initial state.
     33   manager.OnPowerStateChange(false);
     34 
     35   // Loop a few times to test power toggling.
     36   for (int times = 0; times != 3; ++times) {
     37     // The manager has the high resolution clock enabled now.
     38     EXPECT_TRUE(manager.hi_res_clock_available());
     39     // But the Time class has it off, because it hasn't been activated.
     40     EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
     41 
     42     // Activate the high resolution timer.
     43     base::Time::ActivateHighResolutionTimer(true);
     44     EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
     45 
     46     // Simulate a on-battery power event.
     47     manager.OnPowerStateChange(true);
     48     EXPECT_FALSE(manager.hi_res_clock_available());
     49     EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
     50 
     51     // Back to on-AC power.
     52     manager.OnPowerStateChange(false);
     53     EXPECT_TRUE(manager.hi_res_clock_available());
     54     EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
     55 
     56     // De-activate the high resolution timer.
     57     base::Time::ActivateHighResolutionTimer(false);
     58   }
     59 }
     60 #endif  // defined(OS_WIN)
     61 
     62 }  // namespace base
     63