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 // http://crbug.com/114048
18 TEST(HiResTimerManagerTest, DISABLED_ToggleOnOff) {
19 base::MessageLoop loop;
20 scoped_ptr<base::PowerMonitorSource> power_monitor_source(
21 new base::PowerMonitorDeviceSource());
22 scoped_ptr<base::PowerMonitor> power_monitor(
23 new base::PowerMonitor(power_monitor_source.Pass()));
24 HighResolutionTimerManager manager;
26 // At this point, we don't know if the high resolution timers are on or off,
27 // it depends on what system the tests are running on (for example, if this
28 // test is running on a laptop/battery, then the PowerMonitor would have
29 // already set the PowerState to battery power; but if we're running on a
30 // desktop, then the PowerState will be non-battery power). Simulate a power
31 // level change to get to a deterministic state.
32 manager.OnPowerStateChange(/* on_battery */ false);
34 // Loop a few times to test power toggling.
35 for (int loop = 2; loop >= 0; --loop) {
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());
41 // Activate the high resolution timer.
42 base::Time::ActivateHighResolutionTimer(true);
43 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
45 // Simulate a on-battery power event.
46 manager.OnPowerStateChange(/* on_battery */ true);
47 EXPECT_FALSE(manager.hi_res_clock_available());
48 EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse());
50 // Simulate a off-battery power event.
51 manager.OnPowerStateChange(/* on_battery */ false);
52 EXPECT_TRUE(manager.hi_res_clock_available());
53 EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse());
55 // De-activate the high resolution timer.
56 base::Time::ActivateHighResolutionTimer(false);
59 #endif // defined(OS_WIN)