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.
5 #include "base/timer/hi_res_timer_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/power_monitor/power_monitor.h"
10 #include "base/power_monitor/power_monitor_device_source.h"
11 #include "base/time/time.h"
12 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(HiResTimerManagerTest
, ToggleOnOff
) {
18 // The power monitor creates Window to receive power notifications from
19 // Windows, which makes this test flaky if you run while the machine
20 // goes in or out of AC power.
21 base::MessageLoop
loop(base::MessageLoop::TYPE_UI
);
22 scoped_ptr
<base::PowerMonitorSource
> power_monitor_source(
23 new base::PowerMonitorDeviceSource());
24 scoped_ptr
<base::PowerMonitor
> power_monitor(
25 new base::PowerMonitor(power_monitor_source
.Pass()));
27 HighResolutionTimerManager manager
;
28 // Simulate a on-AC power event to get to a known initial state.
29 manager
.OnPowerStateChange(false);
31 // Loop a few times to test power toggling.
32 for (int times
= 0; times
!= 3; ++times
) {
33 // The manager has the high resolution clock enabled now.
34 EXPECT_TRUE(manager
.hi_res_clock_available());
35 // But the Time class has it off, because it hasn't been activated.
36 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
38 // Activate the high resolution timer.
39 base::Time::ActivateHighResolutionTimer(true);
40 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
42 // Simulate a on-battery power event.
43 manager
.OnPowerStateChange(true);
44 EXPECT_FALSE(manager
.hi_res_clock_available());
45 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
47 // Back to on-AC power.
48 manager
.OnPowerStateChange(false);
49 EXPECT_TRUE(manager
.hi_res_clock_available());
50 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
52 // De-activate the high resolution timer.
53 base::Time::ActivateHighResolutionTimer(false);
56 #endif // defined(OS_WIN)