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" |
| 6 |
5 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/message_loop.h" |
6 #include "base/test/mock_devices_changed_observer.h" | 9 #include "base/test/mock_devices_changed_observer.h" |
7 #include "base/system_monitor/system_monitor.h" | |
8 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 | 14 |
13 class PowerTest : public SystemMonitor::PowerObserver { | 15 class PowerTest : public SystemMonitor::PowerObserver { |
14 public: | 16 public: |
15 PowerTest() | 17 PowerTest() |
16 : battery_(false), | 18 : battery_(false), |
17 power_state_changes_(0), | 19 power_state_changes_(0), |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 132 |
131 system_monitor.ProcessMediaDeviceAttached( | 133 system_monitor.ProcessMediaDeviceAttached( |
132 1, "media device", FilePath(FILE_PATH_LITERAL("path"))); | 134 1, "media device", FilePath(FILE_PATH_LITERAL("path"))); |
133 loop.RunAllPending(); | 135 loop.RunAllPending(); |
134 | 136 |
135 system_monitor.ProcessMediaDeviceDetached(1); | 137 system_monitor.ProcessMediaDeviceDetached(1); |
136 system_monitor.ProcessMediaDeviceDetached(2); | 138 system_monitor.ProcessMediaDeviceDetached(2); |
137 loop.RunAllPending(); | 139 loop.RunAllPending(); |
138 } | 140 } |
139 | 141 |
| 142 TEST(SystemMonitor, 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; |
| 153 devices.reset(system_monitor.GetAttachedMediaDevices()); |
| 154 EXPECT_EQ(0U, devices->size()); |
| 155 } |
| 156 |
| 157 TEST(SystemMonitor, 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; |
| 168 const char kDeviceName1[] = "test"; |
| 169 const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
| 170 system_monitor.ProcessMediaDeviceAttached(kDeviceId1, |
| 171 kDeviceName1, |
| 172 kDevicePath1); |
| 173 loop.RunAllPending(); |
| 174 scoped_ptr<std::vector<SystemMonitor::MediaDeviceInfo> > devices; |
| 175 devices.reset(system_monitor.GetAttachedMediaDevices()); |
| 176 ASSERT_EQ(1U, devices->size()); |
| 177 EXPECT_EQ(kDeviceId1, (*devices)[0].a); |
| 178 EXPECT_EQ(kDeviceName1, (*devices)[0].b); |
| 179 EXPECT_EQ(kDevicePath1, (*devices)[0].c); |
| 180 |
| 181 const SystemMonitor::DeviceIdType kDeviceId2 = 44; |
| 182 const char kDeviceName2[] = "test2"; |
| 183 const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); |
| 184 system_monitor.ProcessMediaDeviceAttached(kDeviceId2, |
| 185 kDeviceName2, |
| 186 kDevicePath2); |
| 187 loop.RunAllPending(); |
| 188 devices.reset(system_monitor.GetAttachedMediaDevices()); |
| 189 ASSERT_EQ(2U, devices->size()); |
| 190 EXPECT_EQ(kDeviceId1, (*devices)[0].a); |
| 191 EXPECT_EQ(kDeviceName1, (*devices)[0].b); |
| 192 EXPECT_EQ(kDevicePath1, (*devices)[0].c); |
| 193 EXPECT_EQ(kDeviceId2, (*devices)[1].a); |
| 194 EXPECT_EQ(kDeviceName2, (*devices)[1].b); |
| 195 EXPECT_EQ(kDevicePath2, (*devices)[1].c); |
| 196 |
| 197 system_monitor.ProcessMediaDeviceDetached(kDeviceId1); |
| 198 loop.RunAllPending(); |
| 199 devices.reset(system_monitor.GetAttachedMediaDevices()); |
| 200 ASSERT_EQ(1U, devices->size()); |
| 201 EXPECT_EQ(kDeviceId2, (*devices)[0].a); |
| 202 EXPECT_EQ(kDeviceName2, (*devices)[0].b); |
| 203 EXPECT_EQ(kDevicePath2, (*devices)[0].c); |
| 204 |
| 205 system_monitor.ProcessMediaDeviceDetached(kDeviceId2); |
| 206 loop.RunAllPending(); |
| 207 devices.reset(system_monitor.GetAttachedMediaDevices()); |
| 208 EXPECT_EQ(0U, devices->size()); |
| 209 } |
| 210 |
140 TEST(SystemMonitor, PowerRequirements) { | 211 TEST(SystemMonitor, PowerRequirements) { |
141 #if defined(OS_WIN) | 212 #if defined(OS_WIN) |
142 MessageLoop loop; | 213 MessageLoop loop; |
143 SystemMonitor system_monitor; | 214 SystemMonitor system_monitor; |
144 ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); | 215 ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); |
145 | 216 |
146 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); | 217 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); |
147 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); | 218 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); |
148 | 219 |
149 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 220 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
150 ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); | 221 ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); |
151 | 222 |
152 // A second identical request should not increase the request count. | 223 // A second identical request should not increase the request count. |
153 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 224 system_monitor.BeginPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
154 ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); | 225 ASSERT_EQ(2, system_monitor.GetPowerRequirementsCountForTest()); |
155 | 226 |
156 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); | 227 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "foo"); |
157 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); | 228 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); |
158 | 229 |
159 // The request count should not decrease until all identical requests end. | 230 // The request count should not decrease until all identical requests end. |
160 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 231 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
161 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); | 232 ASSERT_EQ(1, system_monitor.GetPowerRequirementsCountForTest()); |
162 | 233 |
163 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); | 234 system_monitor.EndPowerRequirement(SystemMonitor::TEST_REQUIRED, "bar"); |
164 ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); | 235 ASSERT_EQ(0, system_monitor.GetPowerRequirementsCountForTest()); |
165 #endif // defined(OS_WIN) | 236 #endif // defined(OS_WIN) |
166 } | 237 } |
167 | 238 |
168 } // namespace base | 239 } // namespace base |
OLD | NEW |