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

Side by Side Diff: base/system_monitor/system_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: Use PowerMonitor instead of SystemMonitor in XXXMain function Created 7 years, 9 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/system_monitor/system_monitor.h" 5 #include "base/system_monitor/system_monitor.h"
6 6
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/mock_devices_changed_observer.h" 9 #include "base/test/mock_devices_changed_observer.h"
10 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 namespace { 15 namespace {
16 16
17 class PowerTest : public SystemMonitor::PowerObserver {
18 public:
19 PowerTest()
20 : power_state_changes_(0),
21 suspends_(0),
22 resumes_(0) {
23 }
24
25 // PowerObserver callbacks.
26 virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE {
27 power_state_changes_++;
28 }
29
30 virtual void OnSuspend() OVERRIDE {
31 suspends_++;
32 }
33
34 virtual void OnResume() OVERRIDE {
35 resumes_++;
36 }
37
38 // Test status counts.
39 int power_state_changes() { return power_state_changes_; }
40 int suspends() { return suspends_; }
41 int resumes() { return resumes_; }
42
43 private:
44 int power_state_changes_; // Count of OnPowerStateChange notifications.
45 int suspends_; // Count of OnSuspend notifications.
46 int resumes_; // Count of OnResume notifications.
47 };
48
49 class SystemMonitorTest : public testing::Test { 17 class SystemMonitorTest : public testing::Test {
50 protected: 18 protected:
51 SystemMonitorTest() { 19 SystemMonitorTest() {
52 #if defined(OS_MACOSX)
53 // This needs to happen before SystemMonitor's ctor.
54 SystemMonitor::AllocateSystemIOPorts();
55 #endif
56 system_monitor_.reset(new SystemMonitor); 20 system_monitor_.reset(new SystemMonitor);
57 } 21 }
58 virtual ~SystemMonitorTest() {} 22 virtual ~SystemMonitorTest() {}
59 23
60 MessageLoop message_loop_; 24 MessageLoop message_loop_;
61 scoped_ptr<SystemMonitor> system_monitor_; 25 scoped_ptr<SystemMonitor> system_monitor_;
62 26
63 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); 27 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest);
64 }; 28 };
65 29
66 TEST_F(SystemMonitorTest, PowerNotifications) {
67 const int kObservers = 5;
68
69 PowerTest test[kObservers];
70 for (int index = 0; index < kObservers; ++index)
71 system_monitor_->AddPowerObserver(&test[index]);
72
73 // Send a bunch of power changes. Since the battery power hasn't
74 // actually changed, we shouldn't get notifications.
75 for (int index = 0; index < 5; index++) {
76 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT);
77 EXPECT_EQ(test[0].power_state_changes(), 0);
78 }
79
80 // Sending resume when not suspended should have no effect.
81 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
82 RunLoop().RunUntilIdle();
83 EXPECT_EQ(test[0].resumes(), 0);
84
85 // Pretend we suspended.
86 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
87 RunLoop().RunUntilIdle();
88 EXPECT_EQ(test[0].suspends(), 1);
89
90 // Send a second suspend notification. This should be suppressed.
91 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT);
92 RunLoop().RunUntilIdle();
93 EXPECT_EQ(test[0].suspends(), 1);
94
95 // Pretend we were awakened.
96 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
97 RunLoop().RunUntilIdle();
98 EXPECT_EQ(test[0].resumes(), 1);
99
100 // Send a duplicate resume notification. This should be suppressed.
101 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT);
102 RunLoop().RunUntilIdle();
103 EXPECT_EQ(test[0].resumes(), 1);
104 }
105
106 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { 30 TEST_F(SystemMonitorTest, DeviceChangeNotifications) {
107 const int kObservers = 5; 31 const int kObservers = 5;
108 32
109 testing::Sequence mock_sequencer[kObservers]; 33 testing::Sequence mock_sequencer[kObservers];
110 MockDevicesChangedObserver observers[kObservers]; 34 MockDevicesChangedObserver observers[kObservers];
111 for (int index = 0; index < kObservers; ++index) { 35 for (int index = 0; index < kObservers; ++index) {
112 system_monitor_->AddDevicesChangedObserver(&observers[index]); 36 system_monitor_->AddDevicesChangedObserver(&observers[index]);
113 37
114 EXPECT_CALL(observers[index], 38 EXPECT_CALL(observers[index],
115 OnDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN)) 39 OnDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN))
116 .Times(3) 40 .Times(3)
117 .InSequence(mock_sequencer[index]); 41 .InSequence(mock_sequencer[index]);
118 } 42 }
119 43
120 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); 44 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN);
121 RunLoop().RunUntilIdle(); 45 RunLoop().RunUntilIdle();
122 46
123 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); 47 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN);
124 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); 48 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN);
125 RunLoop().RunUntilIdle(); 49 RunLoop().RunUntilIdle();
126 } 50 }
127 51
128 } // namespace 52 } // namespace
129 53
130 } // namespace base 54 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698