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

Side by Side 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: 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 unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "base/power_monitor/power_monitor.h"
6
7 #include "base/message_loop.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace base {
11
12 class PowerTest : public PowerObserver {
13 public:
14 PowerTest()
15 : power_state_changes_(0),
16 suspends_(0),
17 resumes_(0) {
18 }
19
20 // PowerObserver callbacks.
21 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
22 power_state_changes_++;
23 }
24
25 virtual void OnSuspend() OVERRIDE {
26 suspends_++;
27 }
28
29 virtual void OnResume() OVERRIDE {
30 resumes_++;
31 }
32
33 // Test status counts.
34 int power_state_changes() { return power_state_changes_; }
35 int suspends() { return suspends_; }
36 int resumes() { return resumes_; }
37
38 private:
39 int power_state_changes_; // Count of OnPowerStateChange notifications.
40 int suspends_; // Count of OnSuspend notifications.
41 int resumes_; // Count of OnResume notifications.
42 };
43
44 class PowerMonitorTest : public testing::Test {
45 protected:
46 PowerMonitorTest() {
47 #if defined(OS_MACOSX)
48 // This needs to happen before PowerMonitor's ctor.
49 PowerMonitor::AllocateSystemIOPorts();
50 #endif
51 }
52 virtual ~PowerMonitorTest() {}
53
54 void ProcessPowerEvent(PowerMonitor::PowerEvent event) {
55 power_monitor_.ProcessPowerEvent(event);
56 }
57
58 MessageLoop message_loop_;
59 PowerMonitor power_monitor_;
60
61 DISALLOW_COPY_AND_ASSIGN(PowerMonitorTest);
62 };
63
64 TEST_F(PowerMonitorTest, PowerNotifications) {
65 const int kObservers = 5;
66
67 PowerTest test[kObservers];
68 for (int index = 0; index < kObservers; ++index)
69 power_monitor_.AddObserver(&test[index]);
70
71 // Send a bunch of power changes. Since the battery power hasn't
72 // actually changed, we shouldn't get notifications.
73 for (int index = 0; index < 5; index++) {
74 ProcessPowerEvent(PowerMonitor::POWER_STATE_EVENT);
75 EXPECT_EQ(test[0].power_state_changes(), 0);
76 }
77
78 // Sending resume when not suspended should have no effect.
79 ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
80 message_loop_.RunAllPending();
81 EXPECT_EQ(test[0].resumes(), 0);
82
83 // Pretend we suspended.
84 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT);
85 message_loop_.RunAllPending();
86 EXPECT_EQ(test[0].suspends(), 1);
87
88 // Send a second suspend notification. This should be suppressed.
89 ProcessPowerEvent(PowerMonitor::SUSPEND_EVENT);
90 message_loop_.RunAllPending();
91 EXPECT_EQ(test[0].suspends(), 1);
92
93 // Pretend we were awakened.
94 ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
95 message_loop_.RunAllPending();
96 EXPECT_EQ(test[0].resumes(), 1);
97
98 // Send a duplicate resume notification. This should be suppressed.
99 ProcessPowerEvent(PowerMonitor::RESUME_EVENT);
100 message_loop_.RunAllPending();
101 EXPECT_EQ(test[0].resumes(), 1);
102 }
103
104 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698