diff options
author | mbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-25 18:46:11 +0000 |
---|---|---|
committer | mbelshe@google.com <mbelshe@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-09-25 18:46:11 +0000 |
commit | a20e82fa0c45fee5515a2ebfc46f38dc65446ef7 (patch) | |
tree | 3d14c292858f52cee26ee431410849499f7fcc52 /base/system_monitor_unittest.cc | |
parent | f9ef79b3c3f5e6a1b00909b30692d47ba92925e6 (diff) | |
download | chromium_src-a20e82fa0c45fee5515a2ebfc46f38dc65446ef7.zip chromium_src-a20e82fa0c45fee5515a2ebfc46f38dc65446ef7.tar.gz chromium_src-a20e82fa0c45fee5515a2ebfc46f38dc65446ef7.tar.bz2 |
Implement a SystemMonitor class for monitoring system status
changes. For now, this is just implementing power changes
(e.g. battery/AC power changes).
This CL just contains this class; the hooking into Chrome
will come in a separate CL, but I've tested the basics already.
Review URL: http://codereview.chromium.org/4051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@2593 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/system_monitor_unittest.cc')
-rw-r--r-- | base/system_monitor_unittest.cc | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/base/system_monitor_unittest.cc b/base/system_monitor_unittest.cc new file mode 100644 index 0000000..aced8fe --- /dev/null +++ b/base/system_monitor_unittest.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2008 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/system_monitor.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+class PowerTest : public base::SystemMonitor::PowerObserver {
+ public:
+ PowerTest()
+ : battery_(false),
+ power_state_changes_(0),
+ suspends_(0),
+ resumes_(0) {};
+
+ // PowerObserver callbacks.
+ void OnPowerStateChange(base::SystemMonitor*) { power_state_changes_++; };
+ void OnSuspend(base::SystemMonitor*) { suspends_++; };
+ void OnResume(base::SystemMonitor*) { resumes_++; };
+
+ // Test status counts.
+ bool battery() { return battery_; }
+ int power_state_changes() { return power_state_changes_; }
+ int suspends() { return suspends_; }
+ int resumes() { return resumes_; }
+
+ private:
+ bool battery_; // Do we currently think we're on battery power.
+ int power_state_changes_; // Count of OnPowerStateChange notifications.
+ int suspends_; // Count of OnSuspend notifications.
+ int resumes_; // Count of OnResume notifications.
+};
+
+TEST(SystemMonitor, PowerNotifications) {
+ base::SystemMonitor* monitor = base::SystemMonitor::Get();
+ PowerTest test;
+ monitor->AddObserver(&test);
+
+ // 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++) {
+ monitor->ProcessPowerMessage(base::SystemMonitor::PowerStateEvent);
+ EXPECT_EQ(test.power_state_changes(), 0);
+ }
+
+ // Sending resume when not suspended should have no effect.
+ monitor->ProcessPowerMessage(base::SystemMonitor::ResumeEvent);
+ EXPECT_EQ(test.resumes(), 0);
+
+ // Pretend we suspended.
+ monitor->ProcessPowerMessage(base::SystemMonitor::SuspendEvent);
+ EXPECT_EQ(test.suspends(), 1);
+
+ // Send a second suspend notification. This should be suppressed.
+ monitor->ProcessPowerMessage(base::SystemMonitor::SuspendEvent);
+ EXPECT_EQ(test.suspends(), 1);
+
+ // Pretend we were awakened.
+ monitor->ProcessPowerMessage(base::SystemMonitor::ResumeEvent);
+ EXPECT_EQ(test.resumes(), 1);
+
+ // Send a duplicate resume notification. This should be suppressed.
+ monitor->ProcessPowerMessage(base::SystemMonitor::ResumeEvent);
+ EXPECT_EQ(test.resumes(), 1);
+}
+
|