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/run_loop.h" |
9 #include "base/test/mock_devices_changed_observer.h" | 10 #include "base/test/mock_devices_changed_observer.h" |
10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
11 #include "testing/gmock/include/gmock/gmock.h" | 12 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 class PowerTest : public SystemMonitor::PowerObserver { | 19 class PowerTest : public SystemMonitor::PowerObserver { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 | 74 |
74 // Send a bunch of power changes. Since the battery power hasn't | 75 // Send a bunch of power changes. Since the battery power hasn't |
75 // actually changed, we shouldn't get notifications. | 76 // actually changed, we shouldn't get notifications. |
76 for (int index = 0; index < 5; index++) { | 77 for (int index = 0; index < 5; index++) { |
77 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); | 78 system_monitor_->ProcessPowerMessage(SystemMonitor::POWER_STATE_EVENT); |
78 EXPECT_EQ(test[0].power_state_changes(), 0); | 79 EXPECT_EQ(test[0].power_state_changes(), 0); |
79 } | 80 } |
80 | 81 |
81 // Sending resume when not suspended should have no effect. | 82 // Sending resume when not suspended should have no effect. |
82 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 83 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
83 message_loop_.RunUntilIdle(); | 84 RunLoop().RunUntilIdle(); |
84 EXPECT_EQ(test[0].resumes(), 0); | 85 EXPECT_EQ(test[0].resumes(), 0); |
85 | 86 |
86 // Pretend we suspended. | 87 // Pretend we suspended. |
87 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 88 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); |
88 message_loop_.RunUntilIdle(); | 89 RunLoop().RunUntilIdle(); |
89 EXPECT_EQ(test[0].suspends(), 1); | 90 EXPECT_EQ(test[0].suspends(), 1); |
90 | 91 |
91 // Send a second suspend notification. This should be suppressed. | 92 // Send a second suspend notification. This should be suppressed. |
92 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); | 93 system_monitor_->ProcessPowerMessage(SystemMonitor::SUSPEND_EVENT); |
93 message_loop_.RunUntilIdle(); | 94 RunLoop().RunUntilIdle(); |
94 EXPECT_EQ(test[0].suspends(), 1); | 95 EXPECT_EQ(test[0].suspends(), 1); |
95 | 96 |
96 // Pretend we were awakened. | 97 // Pretend we were awakened. |
97 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 98 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
98 message_loop_.RunUntilIdle(); | 99 RunLoop().RunUntilIdle(); |
99 EXPECT_EQ(test[0].resumes(), 1); | 100 EXPECT_EQ(test[0].resumes(), 1); |
100 | 101 |
101 // Send a duplicate resume notification. This should be suppressed. | 102 // Send a duplicate resume notification. This should be suppressed. |
102 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); | 103 system_monitor_->ProcessPowerMessage(SystemMonitor::RESUME_EVENT); |
103 message_loop_.RunUntilIdle(); | 104 RunLoop().RunUntilIdle(); |
104 EXPECT_EQ(test[0].resumes(), 1); | 105 EXPECT_EQ(test[0].resumes(), 1); |
105 } | 106 } |
106 | 107 |
107 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { | 108 TEST_F(SystemMonitorTest, DeviceChangeNotifications) { |
108 const int kObservers = 5; | 109 const int kObservers = 5; |
109 const string16 kDeviceName = ASCIIToUTF16("media device"); | 110 const string16 kDeviceName = ASCIIToUTF16("media device"); |
110 const std::string kDeviceId1 = "1"; | 111 const std::string kDeviceId1 = "1"; |
111 const std::string kDeviceId2 = "2"; | 112 const std::string kDeviceId2 = "2"; |
112 | 113 |
113 testing::Sequence mock_sequencer[kObservers]; | 114 testing::Sequence mock_sequencer[kObservers]; |
114 MockDevicesChangedObserver observers[kObservers]; | 115 MockDevicesChangedObserver observers[kObservers]; |
115 for (int index = 0; index < kObservers; ++index) { | 116 for (int index = 0; index < kObservers; ++index) { |
116 system_monitor_->AddDevicesChangedObserver(&observers[index]); | 117 system_monitor_->AddDevicesChangedObserver(&observers[index]); |
117 | 118 |
118 EXPECT_CALL(observers[index], | 119 EXPECT_CALL(observers[index], |
119 OnDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN)) | 120 OnDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN)) |
120 .Times(3) | 121 .Times(3) |
121 .InSequence(mock_sequencer[index]); | 122 .InSequence(mock_sequencer[index]); |
122 EXPECT_CALL(observers[index], OnRemovableStorageAttached(kDeviceId1, | 123 EXPECT_CALL(observers[index], OnRemovableStorageAttached(kDeviceId1, |
123 kDeviceName, | 124 kDeviceName, |
124 testing::_)) | 125 testing::_)) |
125 .InSequence(mock_sequencer[index]); | 126 .InSequence(mock_sequencer[index]); |
126 EXPECT_CALL(observers[index], OnRemovableStorageDetached(kDeviceId1)) | 127 EXPECT_CALL(observers[index], OnRemovableStorageDetached(kDeviceId1)) |
127 .InSequence(mock_sequencer[index]); | 128 .InSequence(mock_sequencer[index]); |
128 EXPECT_CALL(observers[index], OnRemovableStorageDetached(kDeviceId2)) | 129 EXPECT_CALL(observers[index], OnRemovableStorageDetached(kDeviceId2)) |
129 .Times(0).InSequence(mock_sequencer[index]); | 130 .Times(0).InSequence(mock_sequencer[index]); |
130 } | 131 } |
131 | 132 |
132 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | 133 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); |
133 message_loop_.RunUntilIdle(); | 134 RunLoop().RunUntilIdle(); |
134 | 135 |
135 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | 136 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); |
136 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); | 137 system_monitor_->ProcessDevicesChanged(SystemMonitor::DEVTYPE_UNKNOWN); |
137 message_loop_.RunUntilIdle(); | 138 RunLoop().RunUntilIdle(); |
138 | 139 |
139 system_monitor_->ProcessRemovableStorageAttached(kDeviceId1, | 140 system_monitor_->ProcessRemovableStorageAttached(kDeviceId1, |
140 kDeviceName, | 141 kDeviceName, |
141 FILE_PATH_LITERAL("path")); | 142 FILE_PATH_LITERAL("path")); |
142 message_loop_.RunUntilIdle(); | 143 RunLoop().RunUntilIdle(); |
143 | 144 |
144 system_monitor_->ProcessRemovableStorageDetached(kDeviceId1); | 145 system_monitor_->ProcessRemovableStorageDetached(kDeviceId1); |
145 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); | 146 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); |
146 message_loop_.RunUntilIdle(); | 147 RunLoop().RunUntilIdle(); |
147 } | 148 } |
148 | 149 |
149 TEST_F(SystemMonitorTest, GetAttachedRemovableStorageEmpty) { | 150 TEST_F(SystemMonitorTest, GetAttachedRemovableStorageEmpty) { |
150 std::vector<SystemMonitor::RemovableStorageInfo> devices = | 151 std::vector<SystemMonitor::RemovableStorageInfo> devices = |
151 system_monitor_->GetAttachedRemovableStorage(); | 152 system_monitor_->GetAttachedRemovableStorage(); |
152 EXPECT_EQ(0U, devices.size()); | 153 EXPECT_EQ(0U, devices.size()); |
153 } | 154 } |
154 | 155 |
155 TEST_F(SystemMonitorTest, GetAttachedRemovableStorageAttachDetach) { | 156 TEST_F(SystemMonitorTest, GetAttachedRemovableStorageAttachDetach) { |
156 const std::string kDeviceId1 = "42"; | 157 const std::string kDeviceId1 = "42"; |
157 const string16 kDeviceName1 = ASCIIToUTF16("test"); | 158 const string16 kDeviceName1 = ASCIIToUTF16("test"); |
158 const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); | 159 const FilePath kDevicePath1(FILE_PATH_LITERAL("/testfoo")); |
159 system_monitor_->ProcessRemovableStorageAttached(kDeviceId1, | 160 system_monitor_->ProcessRemovableStorageAttached(kDeviceId1, |
160 kDeviceName1, | 161 kDeviceName1, |
161 kDevicePath1.value()); | 162 kDevicePath1.value()); |
162 message_loop_.RunUntilIdle(); | 163 RunLoop().RunUntilIdle(); |
163 std::vector<SystemMonitor::RemovableStorageInfo> devices = | 164 std::vector<SystemMonitor::RemovableStorageInfo> devices = |
164 system_monitor_->GetAttachedRemovableStorage(); | 165 system_monitor_->GetAttachedRemovableStorage(); |
165 ASSERT_EQ(1U, devices.size()); | 166 ASSERT_EQ(1U, devices.size()); |
166 EXPECT_EQ(kDeviceId1, devices[0].device_id); | 167 EXPECT_EQ(kDeviceId1, devices[0].device_id); |
167 EXPECT_EQ(kDeviceName1, devices[0].name); | 168 EXPECT_EQ(kDeviceName1, devices[0].name); |
168 EXPECT_EQ(kDevicePath1.value(), devices[0].location); | 169 EXPECT_EQ(kDevicePath1.value(), devices[0].location); |
169 | 170 |
170 const std::string kDeviceId2 = "44"; | 171 const std::string kDeviceId2 = "44"; |
171 const string16 kDeviceName2 = ASCIIToUTF16("test2"); | 172 const string16 kDeviceName2 = ASCIIToUTF16("test2"); |
172 const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); | 173 const FilePath kDevicePath2(FILE_PATH_LITERAL("/testbar")); |
173 system_monitor_->ProcessRemovableStorageAttached(kDeviceId2, | 174 system_monitor_->ProcessRemovableStorageAttached(kDeviceId2, |
174 kDeviceName2, | 175 kDeviceName2, |
175 kDevicePath2.value()); | 176 kDevicePath2.value()); |
176 message_loop_.RunUntilIdle(); | 177 RunLoop().RunUntilIdle(); |
177 devices = system_monitor_->GetAttachedRemovableStorage(); | 178 devices = system_monitor_->GetAttachedRemovableStorage(); |
178 ASSERT_EQ(2U, devices.size()); | 179 ASSERT_EQ(2U, devices.size()); |
179 EXPECT_EQ(kDeviceId1, devices[0].device_id); | 180 EXPECT_EQ(kDeviceId1, devices[0].device_id); |
180 EXPECT_EQ(kDeviceName1, devices[0].name); | 181 EXPECT_EQ(kDeviceName1, devices[0].name); |
181 EXPECT_EQ(kDevicePath1.value(), devices[0].location); | 182 EXPECT_EQ(kDevicePath1.value(), devices[0].location); |
182 EXPECT_EQ(kDeviceId2, devices[1].device_id); | 183 EXPECT_EQ(kDeviceId2, devices[1].device_id); |
183 EXPECT_EQ(kDeviceName2, devices[1].name); | 184 EXPECT_EQ(kDeviceName2, devices[1].name); |
184 EXPECT_EQ(kDevicePath2.value(), devices[1].location); | 185 EXPECT_EQ(kDevicePath2.value(), devices[1].location); |
185 | 186 |
186 system_monitor_->ProcessRemovableStorageDetached(kDeviceId1); | 187 system_monitor_->ProcessRemovableStorageDetached(kDeviceId1); |
187 message_loop_.RunUntilIdle(); | 188 RunLoop().RunUntilIdle(); |
188 devices = system_monitor_->GetAttachedRemovableStorage(); | 189 devices = system_monitor_->GetAttachedRemovableStorage(); |
189 ASSERT_EQ(1U, devices.size()); | 190 ASSERT_EQ(1U, devices.size()); |
190 EXPECT_EQ(kDeviceId2, devices[0].device_id); | 191 EXPECT_EQ(kDeviceId2, devices[0].device_id); |
191 EXPECT_EQ(kDeviceName2, devices[0].name); | 192 EXPECT_EQ(kDeviceName2, devices[0].name); |
192 EXPECT_EQ(kDevicePath2.value(), devices[0].location); | 193 EXPECT_EQ(kDevicePath2.value(), devices[0].location); |
193 | 194 |
194 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); | 195 system_monitor_->ProcessRemovableStorageDetached(kDeviceId2); |
195 message_loop_.RunUntilIdle(); | 196 RunLoop().RunUntilIdle(); |
196 devices = system_monitor_->GetAttachedRemovableStorage(); | 197 devices = system_monitor_->GetAttachedRemovableStorage(); |
197 EXPECT_EQ(0U, devices.size()); | 198 EXPECT_EQ(0U, devices.size()); |
198 } | 199 } |
199 | 200 |
200 } // namespace | 201 } // namespace |
201 | 202 |
202 } // namespace base | 203 } // namespace base |
OLD | NEW |