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 <utility>
      8 
      9 #include "base/memory/scoped_ptr.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "base/power_monitor/power_monitor.h"
     12 #include "base/power_monitor/power_monitor_device_source.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   base::MessageLoop loop(base::MessageLoop::TYPE_UI);
     25   scoped_ptr<base::PowerMonitorSource> power_monitor_source(
     26       new base::PowerMonitorDeviceSource());
     27   scoped_ptr<base::PowerMonitor> power_monitor(
     28       new base::PowerMonitor(std::move(power_monitor_source)));
     29 
     30   HighResolutionTimerManager manager;
     31   // Simulate a on-AC power event to get to a known initial state.
     32   manager.OnPowerStateChange(false);
     33 
     34   // Loop a few times to test power toggling.
     35   for (int times = 0; times != 3; ++times) {
     36     // The manager has the high resolution clock enabled now.
     37     EXPECT_TRUE(manager.hi_res_clock_available());
     38     // But the Time class has it off, because it hasn't been activated.
     39     EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
     40 
     41     // Activate the high resolution timer.
     42     base::Time::ActivateHighResolutionTimer(true);
     43     EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
     44 
     45     // Simulate a on-battery power event.
     46     manager.OnPowerStateChange(true);
     47     EXPECT_FALSE(manager.hi_res_clock_available());
     48     EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
     49 
     50     // Back to on-AC power.
     51     manager.OnPowerStateChange(false);
     52     EXPECT_TRUE(manager.hi_res_clock_available());
     53     EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
     54 
     55     // De-activate the high resolution timer.
     56     base::Time::ActivateHighResolutionTimer(false);
     57   }
     58 }
     59 #endif  // defined(OS_WIN)
     60 
     61 }  // namespace base
     62