Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2026)

Unified Diff: base/power_monitor/power_monitor_unittest.cc

Issue 10959020: SystemMonitor refactoring: move power state monitor into a separate class called PowerMonitor (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: make HWNDMessageHandler as a friend class of PowerMonitor Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/power_monitor/power_monitor_unittest.cc
diff --git a/base/power_monitor/power_monitor_unittest.cc b/base/power_monitor/power_monitor_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aa0679bd78ab80251a93f80e66e072e717d62b82
--- /dev/null
+++ b/base/power_monitor/power_monitor_unittest.cc
@@ -0,0 +1,107 @@
+// Copyright (c) 2012 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 "base/power_monitor/power_monitor.h"
+
+#include "base/message_loop.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+namespace {
+
+class PowerTest : public PowerMonitor::PowerObserver {
+ public:
+ PowerTest()
+ : power_state_changes_(0),
+ suspends_(0),
+ resumes_(0) {
+ }
+
+ // PowerObserver callbacks.
+ virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
+ power_state_changes_++;
+ }
+
+ virtual void OnSuspend() OVERRIDE {
+ suspends_++;
+ }
+
+ virtual void OnResume() OVERRIDE {
+ resumes_++;
+ }
+
+ // Test status counts.
+ int power_state_changes() { return power_state_changes_; }
+ int suspends() { return suspends_; }
+ int resumes() { return resumes_; }
+
+ private:
+ int power_state_changes_; // Count of OnPowerStateChange notifications.
+ int suspends_; // Count of OnSuspend notifications.
+ int resumes_; // Count of OnResume notifications.
+};
+
+} // namespace
+
+class PowerMonitorTest : public testing::Test {
+ protected:
+ PowerMonitorTest() {
+#if defined(OS_MACOSX)
+ // This needs to happen before PowerMonitor's ctor.
+ PowerMonitor::AllocateSystemIOPorts();
+#endif
+ power_monitor_ = new PowerMonitor;
+// power_monitor_.reset(new PowerMonitor);
+ }
+ virtual ~PowerMonitorTest() {}
+
+ MessageLoop message_loop_;
+ PowerMonitor* power_monitor_;
+// scoped_ptr<PowerMonitor> power_monitor_;
vandebo (ex-Chrome) 2012/10/11 00:58:43 Go back to the scoped_ptr ?
Hongbo Min 2012/10/11 07:57:53 Instead create the PowerMonitor on the stack and m
+
+ DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
+};
+
+TEST_F(PowerMonitorTest, PowerNotifications) {
+ const int kObservers = 5;
+
+ PowerTest test[kObservers];
+ for (int index = 0; index < kObservers; ++index)
+ power_monitor_->AddPowerObserver(&test[index]);
+
+ // Send a bunch of power changes. Since the battery power hasn't
+ // actually changed, we shouldn't get notifications.
+ for (int index = 0; index < 5; index++) {
+ power_monitor_->HandlePowerEvent(PowerMonitor::POWER_STATE_EVENT);
+ EXPECT_EQ(test[0].power_state_changes(), 0);
+ }
+
+ // Sending resume when not suspended should have no effect.
+ power_monitor_->HandlePowerEvent(PowerMonitor::RESUME_EVENT);
+ message_loop_.RunAllPending();
+ EXPECT_EQ(test[0].resumes(), 0);
+
+ // Pretend we suspended.
+ power_monitor_->HandlePowerEvent(PowerMonitor::SUSPEND_EVENT);
+ message_loop_.RunAllPending();
+ EXPECT_EQ(test[0].suspends(), 1);
+
+ // Send a second suspend notification. This should be suppressed.
+ power_monitor_->HandlePowerEvent(PowerMonitor::SUSPEND_EVENT);
+ message_loop_.RunAllPending();
+ EXPECT_EQ(test[0].suspends(), 1);
+
+ // Pretend we were awakened.
+ power_monitor_->HandlePowerEvent(PowerMonitor::RESUME_EVENT);
+ message_loop_.RunAllPending();
+ EXPECT_EQ(test[0].resumes(), 1);
+
+ // Send a duplicate resume notification. This should be suppressed.
+ power_monitor_->HandlePowerEvent(PowerMonitor::RESUME_EVENT);
+ message_loop_.RunAllPending();
+ EXPECT_EQ(test[0].resumes(), 1);
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698