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