diff options
author | mbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-15 19:20:49 +0000 |
---|---|---|
committer | mbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-15 19:20:49 +0000 |
commit | 14255a99d1709414d9b5419329e19bf9bd8406b7 (patch) | |
tree | 18616ca60c09839db53690e8c127f5972ca76899 /content | |
parent | 3f20a2191bdac8e08bc1572913d5aa4a17df930c (diff) | |
download | chromium_src-14255a99d1709414d9b5419329e19bf9bd8406b7.zip chromium_src-14255a99d1709414d9b5419329e19bf9bd8406b7.tar.gz chromium_src-14255a99d1709414d9b5419329e19bf9bd8406b7.tar.bz2 |
Reland old fix that was reverted incorrectly.
Fix regression where high resolution timers could be activated even under
battery power. Add unit test to protect chromium from developers like me
in the future.
The fix is a one-liner in hi_res_timer_manager_win.cc. The rest of the code
change is the mechanics to enable the unit test.
BUG=59528
TEST=HiResTimerManagerTest.ToggleOnOff
Review URL: http://codereview.chromium.org/6904117
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85413 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content')
-rw-r--r-- | content/common/hi_res_timer_manager.h | 5 | ||||
-rw-r--r-- | content/common/hi_res_timer_manager_posix.cc | 2 | ||||
-rw-r--r-- | content/common/hi_res_timer_manager_unittest.cc | 51 | ||||
-rw-r--r-- | content/common/hi_res_timer_manager_win.cc | 5 |
4 files changed, 59 insertions, 4 deletions
diff --git a/content/common/hi_res_timer_manager.h b/content/common/hi_res_timer_manager.h index 0fa64f9..af522cd 100644 --- a/content/common/hi_res_timer_manager.h +++ b/content/common/hi_res_timer_manager.h @@ -18,11 +18,14 @@ class HighResolutionTimerManager : public ui::SystemMonitor::PowerObserver { // ui::SystemMonitor::PowerObserver: virtual void OnPowerStateChange(bool on_battery_power); + // Returns true if the hi resolution clock could be used right now. + bool hi_res_clock_available() const { return hi_res_clock_available_; } + private: // Enable or disable the faster multimedia timer. void UseHiResClock(bool use); - bool hi_res_clock_used_; + bool hi_res_clock_available_; DISALLOW_COPY_AND_ASSIGN(HighResolutionTimerManager); }; diff --git a/content/common/hi_res_timer_manager_posix.cc b/content/common/hi_res_timer_manager_posix.cc index 63e7300..430902a 100644 --- a/content/common/hi_res_timer_manager_posix.cc +++ b/content/common/hi_res_timer_manager_posix.cc @@ -7,7 +7,7 @@ // On POSIX we don't need to do anything special with the system timer. HighResolutionTimerManager::HighResolutionTimerManager() - : hi_res_clock_used_(false) { + : hi_res_clock_available_(false) { } HighResolutionTimerManager::~HighResolutionTimerManager() { diff --git a/content/common/hi_res_timer_manager_unittest.cc b/content/common/hi_res_timer_manager_unittest.cc new file mode 100644 index 0000000..c1d3ed0 --- /dev/null +++ b/content/common/hi_res_timer_manager_unittest.cc @@ -0,0 +1,51 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" + +#include "base/memory/scoped_ptr.h" +#include "base/time.h" +#include "content/common/hi_res_timer_manager.h" +#include "ui/base/system_monitor/system_monitor.h" + +#if defined(OS_WIN) +TEST(HiResTimerManagerTest, ToggleOnOff) { + MessageLoop loop; + scoped_ptr<ui::SystemMonitor> system_monitor(new ui::SystemMonitor()); + HighResolutionTimerManager manager; + + // At this point, we don't know if the high resolution timers are on or off, + // it depends on what system the tests are running on (for example, if this + // test is running on a laptop/battery, then the SystemMonitor would have + // already set the PowerState to battery power; but if we're running on a + // desktop, then the PowerState will be non-battery power). Simulate a power + // level change to get to a deterministic state. + manager.OnPowerStateChange(/* on_battery */ false); + + // Loop a few times to test power toggling. + for (int loop = 2; loop >= 0; --loop) { + // The manager has the high resolution clock enabled now. + EXPECT_TRUE(manager.hi_res_clock_available()); + // But the Time class has it off, because it hasn't been activated. + EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); + + // Activate the high resolution timer. + base::Time::ActivateHighResolutionTimer(true); + EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse()); + + // Simulate a on-battery power event. + manager.OnPowerStateChange(/* on_battery */ true); + EXPECT_FALSE(manager.hi_res_clock_available()); + EXPECT_FALSE(base::Time::IsHighResolutionTimerInUse()); + + // Simulate a off-battery power event. + manager.OnPowerStateChange(/* on_battery */ false); + EXPECT_TRUE(manager.hi_res_clock_available()); + EXPECT_TRUE(base::Time::IsHighResolutionTimerInUse()); + + // De-activate the high resolution timer. + base::Time::ActivateHighResolutionTimer(false); + } +} +#endif // defined(OS_WIN) diff --git a/content/common/hi_res_timer_manager_win.cc b/content/common/hi_res_timer_manager_win.cc index 9c86fa0..d41c4bd 100644 --- a/content/common/hi_res_timer_manager_win.cc +++ b/content/common/hi_res_timer_manager_win.cc @@ -7,7 +7,7 @@ #include "base/time.h" HighResolutionTimerManager::HighResolutionTimerManager() - : hi_res_clock_used_(false) { + : hi_res_clock_available_(false) { ui::SystemMonitor* system_monitor = ui::SystemMonitor::Get(); system_monitor->AddObserver(this); UseHiResClock(!system_monitor->BatteryPower()); @@ -23,7 +23,8 @@ void HighResolutionTimerManager::OnPowerStateChange(bool on_battery_power) { } void HighResolutionTimerManager::UseHiResClock(bool use) { - if (use == hi_res_clock_used_) + if (use == hi_res_clock_available_) return; + hi_res_clock_available_ = use; base::Time::EnableHighResolutionTimer(use); } |