OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "media/midi/midi_manager.h" | 5 #include "media/midi/midi_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/logging.h" | 14 #include "base/logging.h" |
14 #include "base/macros.h" | 15 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
19 #include "base/system_monitor/system_monitor.h" | 19 #include "base/system_monitor/system_monitor.h" |
20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace media { | 23 namespace media { |
24 namespace midi { | 24 namespace midi { |
25 | 25 |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 void CompleteInitialization(Result result) { | 154 void CompleteInitialization(Result result) { |
155 manager_->CallCompleteInitialization(result); | 155 manager_->CallCompleteInitialization(result); |
156 } | 156 } |
157 | 157 |
158 void RunLoopUntilIdle() { | 158 void RunLoopUntilIdle() { |
159 base::RunLoop run_loop; | 159 base::RunLoop run_loop; |
160 run_loop.RunUntilIdle(); | 160 run_loop.RunUntilIdle(); |
161 } | 161 } |
162 | 162 |
163 protected: | 163 protected: |
164 scoped_ptr<FakeMidiManager> manager_; | 164 std::unique_ptr<FakeMidiManager> manager_; |
165 | 165 |
166 private: | 166 private: |
167 scoped_ptr<base::MessageLoop> message_loop_; | 167 std::unique_ptr<base::MessageLoop> message_loop_; |
168 | 168 |
169 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); | 169 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest); |
170 }; | 170 }; |
171 | 171 |
172 TEST_F(MidiManagerTest, StartAndEndSession) { | 172 TEST_F(MidiManagerTest, StartAndEndSession) { |
173 scoped_ptr<FakeMidiManagerClient> client; | 173 std::unique_ptr<FakeMidiManagerClient> client; |
174 client.reset(new FakeMidiManagerClient); | 174 client.reset(new FakeMidiManagerClient); |
175 | 175 |
176 StartTheFirstSession(client.get()); | 176 StartTheFirstSession(client.get()); |
177 CompleteInitialization(Result::OK); | 177 CompleteInitialization(Result::OK); |
178 EXPECT_EQ(Result::OK, client->WaitForResult()); | 178 EXPECT_EQ(Result::OK, client->WaitForResult()); |
179 EndSession(client.get(), 1U, 0U); | 179 EndSession(client.get(), 1U, 0U); |
180 } | 180 } |
181 | 181 |
182 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { | 182 TEST_F(MidiManagerTest, StartAndEndSessionWithError) { |
183 scoped_ptr<FakeMidiManagerClient> client; | 183 std::unique_ptr<FakeMidiManagerClient> client; |
184 client.reset(new FakeMidiManagerClient); | 184 client.reset(new FakeMidiManagerClient); |
185 | 185 |
186 StartTheFirstSession(client.get()); | 186 StartTheFirstSession(client.get()); |
187 CompleteInitialization(Result::INITIALIZATION_ERROR); | 187 CompleteInitialization(Result::INITIALIZATION_ERROR); |
188 EXPECT_EQ(Result::INITIALIZATION_ERROR, client->WaitForResult()); | 188 EXPECT_EQ(Result::INITIALIZATION_ERROR, client->WaitForResult()); |
189 EndSession(client.get(), 0U, 0U); | 189 EndSession(client.get(), 0U, 0U); |
190 } | 190 } |
191 | 191 |
192 TEST_F(MidiManagerTest, StartMultipleSessions) { | 192 TEST_F(MidiManagerTest, StartMultipleSessions) { |
193 scoped_ptr<FakeMidiManagerClient> client1; | 193 std::unique_ptr<FakeMidiManagerClient> client1; |
194 scoped_ptr<FakeMidiManagerClient> client2; | 194 std::unique_ptr<FakeMidiManagerClient> client2; |
195 scoped_ptr<FakeMidiManagerClient> client3; | 195 std::unique_ptr<FakeMidiManagerClient> client3; |
196 client1.reset(new FakeMidiManagerClient); | 196 client1.reset(new FakeMidiManagerClient); |
197 client2.reset(new FakeMidiManagerClient); | 197 client2.reset(new FakeMidiManagerClient); |
198 client3.reset(new FakeMidiManagerClient); | 198 client3.reset(new FakeMidiManagerClient); |
199 | 199 |
200 StartTheFirstSession(client1.get()); | 200 StartTheFirstSession(client1.get()); |
201 StartTheNthSession(client2.get(), 2); | 201 StartTheNthSession(client2.get(), 2); |
202 StartTheNthSession(client3.get(), 3); | 202 StartTheNthSession(client3.get(), 3); |
203 CompleteInitialization(Result::OK); | 203 CompleteInitialization(Result::OK); |
204 EXPECT_EQ(Result::OK, client1->WaitForResult()); | 204 EXPECT_EQ(Result::OK, client1->WaitForResult()); |
205 EXPECT_EQ(Result::OK, client2->WaitForResult()); | 205 EXPECT_EQ(Result::OK, client2->WaitForResult()); |
(...skipping 10 matching lines...) Expand all Loading... |
216 // Push as many client requests for starting session as possible. | 216 // Push as many client requests for starting session as possible. |
217 ScopedVector<FakeMidiManagerClient> many_existing_clients; | 217 ScopedVector<FakeMidiManagerClient> many_existing_clients; |
218 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); | 218 many_existing_clients.resize(MidiManager::kMaxPendingClientCount); |
219 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { | 219 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) { |
220 many_existing_clients[i] = new FakeMidiManagerClient; | 220 many_existing_clients[i] = new FakeMidiManagerClient; |
221 StartTheNthSession(many_existing_clients[i], i + 1); | 221 StartTheNthSession(many_existing_clients[i], i + 1); |
222 } | 222 } |
223 EXPECT_TRUE(manager_->start_initialization_is_called_); | 223 EXPECT_TRUE(manager_->start_initialization_is_called_); |
224 | 224 |
225 // Push the last client that should be rejected for too many pending requests. | 225 // Push the last client that should be rejected for too many pending requests. |
226 scoped_ptr<FakeMidiManagerClient> additional_client( | 226 std::unique_ptr<FakeMidiManagerClient> additional_client( |
227 new FakeMidiManagerClient); | 227 new FakeMidiManagerClient); |
228 manager_->start_initialization_is_called_ = false; | 228 manager_->start_initialization_is_called_ = false; |
229 manager_->StartSession(additional_client.get()); | 229 manager_->StartSession(additional_client.get()); |
230 EXPECT_FALSE(manager_->start_initialization_is_called_); | 230 EXPECT_FALSE(manager_->start_initialization_is_called_); |
231 manager_->start_initialization_is_called_ = true; | 231 manager_->start_initialization_is_called_ = true; |
232 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); | 232 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result()); |
233 | 233 |
234 // Other clients still should not receive a result. | 234 // Other clients still should not receive a result. |
235 RunLoopUntilIdle(); | 235 RunLoopUntilIdle(); |
236 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 236 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
237 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); | 237 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result()); |
238 | 238 |
239 // The Result::OK should be distributed to other clients. | 239 // The Result::OK should be distributed to other clients. |
240 CompleteInitialization(Result::OK); | 240 CompleteInitialization(Result::OK); |
241 for (size_t i = 0; i < many_existing_clients.size(); ++i) | 241 for (size_t i = 0; i < many_existing_clients.size(); ++i) |
242 EXPECT_EQ(Result::OK, many_existing_clients[i]->WaitForResult()); | 242 EXPECT_EQ(Result::OK, many_existing_clients[i]->WaitForResult()); |
243 | 243 |
244 // Close all successful sessions in FIFO order. | 244 // Close all successful sessions in FIFO order. |
245 size_t sessions = many_existing_clients.size(); | 245 size_t sessions = many_existing_clients.size(); |
246 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) | 246 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions) |
247 EndSession(many_existing_clients[i], sessions, sessions - 1); | 247 EndSession(many_existing_clients[i], sessions, sessions - 1); |
248 } | 248 } |
249 | 249 |
250 TEST_F(MidiManagerTest, AbortSession) { | 250 TEST_F(MidiManagerTest, AbortSession) { |
251 // A client starting a session can be destructed while an asynchronous | 251 // A client starting a session can be destructed while an asynchronous |
252 // initialization is performed. | 252 // initialization is performed. |
253 scoped_ptr<FakeMidiManagerClient> client; | 253 std::unique_ptr<FakeMidiManagerClient> client; |
254 client.reset(new FakeMidiManagerClient); | 254 client.reset(new FakeMidiManagerClient); |
255 | 255 |
256 StartTheFirstSession(client.get()); | 256 StartTheFirstSession(client.get()); |
257 EndSession(client.get(), 0, 0); | 257 EndSession(client.get(), 0, 0); |
258 client.reset(); | 258 client.reset(); |
259 | 259 |
260 // Following function should not call the destructed |client| function. | 260 // Following function should not call the destructed |client| function. |
261 CompleteInitialization(Result::OK); | 261 CompleteInitialization(Result::OK); |
262 base::RunLoop run_loop; | 262 base::RunLoop run_loop; |
263 run_loop.RunUntilIdle(); | 263 run_loop.RunUntilIdle(); |
264 } | 264 } |
265 | 265 |
266 TEST_F(MidiManagerTest, CreateMidiManager) { | 266 TEST_F(MidiManagerTest, CreateMidiManager) { |
267 // SystemMonitor is needed on Windows. | 267 // SystemMonitor is needed on Windows. |
268 base::SystemMonitor system_monitor; | 268 base::SystemMonitor system_monitor; |
269 | 269 |
270 scoped_ptr<FakeMidiManagerClient> client; | 270 std::unique_ptr<FakeMidiManagerClient> client; |
271 client.reset(new FakeMidiManagerClient); | 271 client.reset(new FakeMidiManagerClient); |
272 | 272 |
273 scoped_ptr<MidiManager> manager(MidiManager::Create()); | 273 std::unique_ptr<MidiManager> manager(MidiManager::Create()); |
274 manager->StartSession(client.get()); | 274 manager->StartSession(client.get()); |
275 | 275 |
276 Result result = client->WaitForResult(); | 276 Result result = client->WaitForResult(); |
277 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc. | 277 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc. |
278 // Do not change the condition for disabling this test. | 278 // Do not change the condition for disabling this test. |
279 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ | 279 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \ |
280 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) | 280 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID) |
281 EXPECT_EQ(Result::NOT_SUPPORTED, result); | 281 EXPECT_EQ(Result::NOT_SUPPORTED, result); |
282 #elif defined(USE_ALSA) | 282 #elif defined(USE_ALSA) |
283 // Temporary until http://crbug.com/371230 is resolved. | 283 // Temporary until http://crbug.com/371230 is resolved. |
284 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); | 284 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR); |
285 #else | 285 #else |
286 EXPECT_EQ(Result::OK, result); | 286 EXPECT_EQ(Result::OK, result); |
287 #endif | 287 #endif |
288 | 288 |
289 manager->Shutdown(); | 289 manager->Shutdown(); |
290 base::RunLoop run_loop; | 290 base::RunLoop run_loop; |
291 run_loop.RunUntilIdle(); | 291 run_loop.RunUntilIdle(); |
292 } | 292 } |
293 | 293 |
294 // TODO(toyoshim): Add multi-threaded unit tests to check races around | 294 // TODO(toyoshim): Add multi-threaded unit tests to check races around |
295 // StartInitialization(), CompleteInitialization(), and Finalize(). | 295 // StartInitialization(), CompleteInitialization(), and Finalize(). |
296 | 296 |
297 } // namespace | 297 } // namespace |
298 | 298 |
299 } // namespace midi | 299 } // namespace midi |
300 } // namespace media | 300 } // namespace media |
OLD | NEW |