OLD | NEW |
---|---|
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/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_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 { | |
16 | |
15 class PowerTest : public SystemMonitor::PowerObserver { | 17 class PowerTest : public SystemMonitor::PowerObserver { |
16 public: | 18 public: |
17 PowerTest() | 19 PowerTest() |
18 : battery_(false), | 20 : battery_(false), |
19 power_state_changes_(0), | 21 power_state_changes_(0), |
20 suspends_(0), | 22 suspends_(0), |
21 resumes_(0) { | 23 resumes_(0) { |
22 } | 24 } |
23 | 25 |
24 // PowerObserver callbacks. | 26 // PowerObserver callbacks. |
(...skipping 15 matching lines...) Expand all Loading... | |
40 int suspends() { return suspends_; } | 42 int suspends() { return suspends_; } |
41 int resumes() { return resumes_; } | 43 int resumes() { return resumes_; } |
42 | 44 |
43 private: | 45 private: |
44 bool battery_; // Do we currently think we're on battery power. | 46 bool battery_; // Do we currently think we're on battery power. |
45 int power_state_changes_; // Count of OnPowerStateChange notifications. | 47 int power_state_changes_; // Count of OnPowerStateChange notifications. |
46 int suspends_; // Count of OnSuspend notifications. | 48 int suspends_; // Count of OnSuspend notifications. |
47 int resumes_; // Count of OnResume notifications. | 49 int resumes_; // Count of OnResume notifications. |
48 }; | 50 }; |
49 | 51 |
50 TEST(SystemMonitor, PowerNotifications) { | 52 class SystemMonitorTest : public testing::Test { |
53 public: | |
willchan no longer on Chromium
2012/05/21 23:51:35
These don't have to be public. TEST_Fs will call t
Lei Zhang
2012/05/21 23:58:10
Done.
| |
54 SystemMonitorTest() {} | |
55 virtual ~SystemMonitorTest() {} | |
56 | |
57 protected: | |
58 virtual void SetUp() OVERRIDE { | |
59 #if defined(OS_MACOSX) | |
60 SystemMonitor::AllocateSystemIOPorts(); | |
willchan no longer on Chromium
2012/05/21 23:51:35
If you wanted, you could move this into the constr
Lei Zhang
2012/05/21 23:58:10
Done.
| |
61 #endif | |
62 } | |
63 | |
64 MessageLoop message_loop_; | |
65 SystemMonitor system_monitor_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(SystemMonitorTest); | |
68 }; | |
69 | |
70 TEST_F(SystemMonitorTest, PowerNotifications) { | |
51 const int kObservers = 5; | 71 const int kObservers = 5; |
52 | 72 |
53 // Initialize a message loop for this to run on. | |
54 MessageLoop loop; | |
55 | |
56 #if defined(OS_MACOSX) | |
57 SystemMonitor::AllocateSystemIOPorts(); | |
58 #endif | |
59 | |
60 SystemMonitor system_monitor; | |
61 PowerTest test[kObservers]; | 73 PowerTest test[kObservers]; |
62 for (int index = 0; index < kObservers; ++index) | 74 for (int index = 0; index < kObservers; ++index) |
63 system_monitor.AddPowerObserver(&test[index]); | 75 system_monitor_.AddPowerObserver(&test[index]); |
64 | 76 |
65 // Send a bunch of power changes. Since the battery power hasn't | 77 // Send a bunch of power changes. Since the battery power hasn't |
66 // actually changed, we shouldn't get notifications. | 78 // actually changed, we shouldn't get notifications. |
67 for (int index = 0; index < 5; index++) { | 79 for (int index = 0; index < 5; index++) { |
68 system_monitor.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 80 system_monitor_.ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
69 EXPECT_EQ(test[0].power_state_changes(), 0); | 81 EXPECT_EQ(test[0].power_state_changes(), 0); |
70 } | 82 } |
71 | 83 |
72 // Sending resume when not suspended should have no effect. | 84 // Sending resume when not suspended should have no effect. |
73 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 85 system_monitor_.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
74 loop.RunAllPending(); | 86 message_loop_.RunAllPending(); |
75 EXPECT_EQ(test[0].resumes(), 0); | 87 EXPECT_EQ(test[0].resumes(), 0); |
76 | 88 |
77 // Pretend we suspended. | 89 // Pretend we suspended. |
78 system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 90 system_monitor_.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); |
79 loop.RunAllPending(); | 91 message_loop_.RunAllPending(); |
80 EXPECT_EQ(test[0].suspends(), 1); | 92 EXPECT_EQ(test[0].suspends(), 1); |
81 | 93 |
82 // Send a second suspend notification. This should be suppressed. | 94 // Send a second suspend notification. This should be suppressed. |
83 system_monitor.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 95 system_monitor_.ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); |
84 loop.RunAllPending(); | 96 message_loop_.RunAllPending(); |
85 EXPECT_EQ(test[0].suspends(), 1); | 97 EXPECT_EQ(test[0].suspends(), 1); |
86 | 98 |
87 // Pretend we were awakened. | 99 // Pretend we were awakened. |
88 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 100 system_monitor_.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
89 loop.RunAllPending(); | 101 message_loop_.RunAllPending(); |
90 EXPECT_EQ(test[0].resumes(), 1); | 102 EXPECT_EQ(test[0].resumes(), 1); |
91 | 103 |
92 // Send a duplicate resume notification. This should be suppressed. | 104 // Send a duplicate resume notification. This should be suppressed. |
93 system_monitor.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 105 system_monitor_.ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
94 loop.RunAllPending(); | 106 message_loop_.RunAllPending(); |
95 EXPECT_EQ(test[0].resumes(), 1); | 107 EXPECT_EQ(test[0].resumes(), 1); |
96 } | 108 } |
97 | 109 |
98 TEST(SystemMonitor, DeviceChangeNotifications) { | 110 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { |
99 const int kObservers = 5; | 111 const int kObservers = 5; |
100 | 112 |
101 // Initialize a message loop for this to run on. | |
102 MessageLoop loop; | |
103 | |
104 #if defined(OS_MACOSX) | |
105 SystemMonitor::AllocateSystemIOPorts(); | |
106 #endif | |
107 | |
108 testing::Sequence mock_sequencer[kObservers]; | 113 testing::Sequence mock_sequencer[kObservers]; |
109 SystemMonitor system_monitor; | |
110 MockDevicesChangedObserver observers[kObservers]; | 114 MockDevicesChangedObserver observers[kObservers]; |
111 for (int index = 0; index < kObservers; ++index) { | 115 for (int index = 0; index < kObservers; ++index) { |
112 system_monitor.AddDevicesChangedObserver(&observers[index]); | 116 system_monitor_.AddDevicesChangedObserver(&observers[index]); |
113 | 117 |
114 EXPECT_CALL(observers[index], OnDevicesChanged()) | 118 EXPECT_CALL(observers[index], OnDevicesChanged()) |
115 .Times(3) | 119 .Times(3) |
116 .InSequence(mock_sequencer[index]); | 120 .InSequence(mock_sequencer[index]); |
117 EXPECT_CALL(observers[index], OnMediaDeviceAttached(1, "media device", | 121 EXPECT_CALL(observers[index], OnMediaDeviceAttached(1, "media device", |
118 testing::_)) | 122 testing::_)) |
119 .InSequence(mock_sequencer[index]); | 123 .InSequence(mock_sequencer[index]); |
120 EXPECT_CALL(observers[index], OnMediaDeviceDetached(1)) | 124 EXPECT_CALL(observers[index], OnMediaDeviceDetached(1)) |
121 .InSequence(mock_sequencer[index]); | 125 .InSequence(mock_sequencer[index]); |
122 EXPECT_CALL(observers[index], OnMediaDeviceDetached(2)) | 126 EXPECT_CALL(observers[index], OnMediaDeviceDetached(2)) |
123 .InSequence(mock_sequencer[index]); | 127 .InSequence(mock_sequencer[index]); |
124 } | 128 } |
125 | 129 |
126 system_monitor.ProcessDevicesChanged(); | 130 system_monitor_.ProcessDevicesChanged(); |
127 loop.RunAllPending(); | 131 message_loop_.RunAllPending(); |
128 | 132 |
129 system_monitor.ProcessDevicesChanged(); | 133 system_monitor_.ProcessDevicesChanged(); |
130 system_monitor.ProcessDevicesChanged(); | 134 system_monitor_.ProcessDevicesChanged(); |
131 loop.RunAllPending(); | 135 message_loop_.RunAllPending(); |
132 | 136 |
133 system_monitor.ProcessMediaDeviceAttached( | 137 system_monitor_.ProcessMediaDeviceAttached( |
134 1, "media device", FilePath(FILE_PATH_LITERAL("path"))); | 138 1, "media device", FilePath(FILE_PATH_LITERAL("path"))); |
135 loop.RunAllPending(); | 139 message_loop_.RunAllPending(); |
136 | 140 |
137 system_monitor.ProcessMediaDeviceDetached(1); | 141 system_monitor_.ProcessMediaDeviceDetached(1); |
138 system_monitor.ProcessMediaDeviceDetached(2); | 142 system_monitor_.ProcessMediaDeviceDetached(2); |
139 loop.RunAllPending(); | 143 message_loop_.RunAllPending(); |
140 } | 144 } |
141 | 145 |
142 TEST(SystemMonitor, GetAttachedMediaDevicesEmpty) { | 146 TEST_F(SystemMonitorTest, GetAttachedMediaDevicesEmpty) { |
143 // Initialize a message loop for this to run on. | |
144 MessageLoop loop; | |
145 | |
146 #if defined(OS_MACOSX) | |
147 SystemMonitor::AllocateSystemIOPorts(); | |
148 #endif | |
149 | |
150 SystemMonitor system_monitor; | |
151 | |
152 scoped_ptr<std::vector<SystemMonitor::MediaDeviceInfo> > devices; | 147 scoped_ptr<std::vector<SystemMonitor::MediaDeviceInfo> > devices; |
153 devices.reset(system_monitor.GetAttachedMediaDevices()); | 148 devices.reset(system_monitor_.GetAttachedMediaDevices()); |
154 EXPECT_EQ(0U, devices->size()); | 149 EXPECT_EQ(0U, devices->size()); |
155 } | 150 } |
156 | 151 |
157 TEST(SystemMonitor, GetAttachedMediaDevicesAttachDetach) { | 152 TEST_F(SystemMonitorTest, GetAttachedMediaDevicesAttachDetach) { |
158 // Initialize a message loop for this to run on. | |
159 MessageLoop loop; | |
160 | |
161 #if defined(OS_MACOSX) | |
162 SystemMonitor::AllocateSystemIOPorts(); | |
163 #endif | |
164 | |
165 SystemMonitor system_monitor; | |
166 | |
167 const SystemMonitor::DeviceIdType kDeviceId1 = 42; | 153 const SystemMonitor::DeviceIdType kDeviceId1 = 42; |
168 const char kDeviceName1[] = "test"; | 154 const char kDeviceName1[] = "test"; |
169 const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); | 155 const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
170 system_monitor.ProcessMediaDeviceAttached(kDeviceId1, | 156 system_monitor_.ProcessMediaDeviceAttached(kDeviceId1, |
171 kDeviceName1, | 157 kDeviceName1, |
172 kDevicePath1); | 158 kDevicePath1); |
173 loop.RunAllPending(); | 159 message_loop_.RunAllPending(); |
174 scoped_ptr<std::vector<SystemMonitor::MediaDeviceInfo> > devices; | 160 scoped_ptr<std::vector<SystemMonitor::MediaDeviceInfo> > devices; |
175 devices.reset(system_monitor.GetAttachedMediaDevices()); | 161 devices.reset(system_monitor_.GetAttachedMediaDevices()); |
176 ASSERT_EQ(1U, devices->size()); | 162 ASSERT_EQ(1U, devices->size()); |
177 EXPECT_EQ(kDeviceId1, (*devices)[0].a); | 163 EXPECT_EQ(kDeviceId1, (*devices)[0].a); |
178 EXPECT_EQ(kDeviceName1, (*devices)[0].b); | 164 EXPECT_EQ(kDeviceName1, (*devices)[0].b); |
179 EXPECT_EQ(kDevicePath1, (*devices)[0].c); | 165 EXPECT_EQ(kDevicePath1, (*devices)[0].c); |
180 | 166 |
181 const SystemMonitor::DeviceIdType kDeviceId2 = 44; | 167 const SystemMonitor::DeviceIdType kDeviceId2 = 44; |
182 const char kDeviceName2[] = "test2"; | 168 const char kDeviceName2[] = "test2"; |
183 const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); | 169 const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); |
184 system_monitor.ProcessMediaDeviceAttached(kDeviceId2, | 170 system_monitor_.ProcessMediaDeviceAttached(kDeviceId2, |
185 kDeviceName2, | 171 kDeviceName2, |
186 kDevicePath2); | 172 kDevicePath2); |
187 loop.RunAllPending(); | 173 message_loop_.RunAllPending(); |
188 devices.reset(system_monitor.GetAttachedMediaDevices()); | 174 devices.reset(system_monitor_.GetAttachedMediaDevices()); |
189 ASSERT_EQ(2U, devices->size()); | 175 ASSERT_EQ(2U, devices->size()); |
190 EXPECT_EQ(kDeviceId1, (*devices)[0].a); | 176 EXPECT_EQ(kDeviceId1, (*devices)[0].a); |
191 EXPECT_EQ(kDeviceName1, (*devices)[0].b); | 177 EXPECT_EQ(kDeviceName1, (*devices)[0].b); |
192 EXPECT_EQ(kDevicePath1, (*devices)[0].c); | 178 EXPECT_EQ(kDevicePath1, (*devices)[0].c); |
193 EXPECT_EQ(kDeviceId2, (*devices)[1].a); | 179 EXPECT_EQ(kDeviceId2, (*devices)[1].a); |
194 EXPECT_EQ(kDeviceName2, (*devices)[1].b); | 180 EXPECT_EQ(kDeviceName2, (*devices)[1].b); |
195 EXPECT_EQ(kDevicePath2, (*devices)[1].c); | 181 EXPECT_EQ(kDevicePath2, (*devices)[1].c); |
196 | 182 |
197 system_monitor.ProcessMediaDeviceDetached(kDeviceId1); | 183 system_monitor_.ProcessMediaDeviceDetached(kDeviceId1); |
198 loop.RunAllPending(); | 184 message_loop_.RunAllPending(); |
199 devices.reset(system_monitor.GetAttachedMediaDevices()); | 185 devices.reset(system_monitor_.GetAttachedMediaDevices()); |
200 ASSERT_EQ(1U, devices->size()); | 186 ASSERT_EQ(1U, devices->size()); |
201 EXPECT_EQ(kDeviceId2, (*devices)[0].a); | 187 EXPECT_EQ(kDeviceId2, (*devices)[0].a); |
202 EXPECT_EQ(kDeviceName2, (*devices)[0].b); | 188 EXPECT_EQ(kDeviceName2, (*devices)[0].b); |
203 EXPECT_EQ(kDevicePath2, (*devices)[0].c); | 189 EXPECT_EQ(kDevicePath2, (*devices)[0].c); |
204 | 190 |
205 system_monitor.ProcessMediaDeviceDetached(kDeviceId2); | 191 system_monitor_.ProcessMediaDeviceDetached(kDeviceId2); |
206 loop.RunAllPending(); | 192 message_loop_.RunAllPending(); |
207 devices.reset(system_monitor.GetAttachedMediaDevices()); | 193 devices.reset(system_monitor_.GetAttachedMediaDevices()); |
208 EXPECT_EQ(0U, devices->size()); | 194 EXPECT_EQ(0U, devices->size()); |
209 } | 195 } |
210 | 196 |
211 TEST(SystemMonitor, PowerRequirements) { | 197 TEST_F(SystemMonitorTest, PowerRequirements) { |
212 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
213 MessageLoop loop; | 199 ASSERT_EQ(0, system_monitor_.GetPowerRequirementsCountForTest()); |
214 SystemMonitor system_monitor; | |
215 ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); | |
216 | 200 |
217 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); | 201 system_monitor_.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); |
218 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); | 202 ASSERT_EQ(1, system_monitor_.GetPowerRequirementsCountForTest()); |
219 | 203 |
220 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 204 system_monitor_.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
221 ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); | 205 ASSERT_EQ(2, system_monitor_.GetPowerRequirementsCountForTest()); |
222 | 206 |
223 // A second identical request should not increase the request count. | 207 // A second identical request should not increase the request count. |
224 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 208 system_monitor_.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
225 ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); | 209 ASSERT_EQ(2, system_monitor_.GetPowerRequirementsCountForTest()); |
226 | 210 |
227 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); | 211 system_monitor_.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); |
228 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); | 212 ASSERT_EQ(1, system_monitor_.GetPowerRequirementsCountForTest()); |
229 | 213 |
230 // The request count should not decrease until all identical requests end. | 214 // The request count should not decrease until all identical requests end. |
231 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 215 system_monitor_.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
232 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); | 216 ASSERT_EQ(1, system_monitor_.GetPowerRequirementsCountForTest()); |
233 | 217 |
234 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 218 system_monitor_.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
235 ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); | 219 ASSERT_EQ(0, system_monitor_.GetPowerRequirementsCountForTest()); |
236 #endif // defined(OS_WIN) | 220 #endif // defined(OS_WIN) |
237 } | 221 } |
238 | 222 |
223 } // namespace | |
224 | |
239 } // namespace base | 225 } // namespace base |
OLD | NEW |