Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: content/browser/renderer_host/media/audio_input_device_manager_unittest.cc

Issue 12440027: Do not pass the string device_id via IPC message to create an audio input stream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed Per's comments. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 5 #include <string>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h" 8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
11 #include "content/browser/browser_thread_impl.h" 11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/renderer_host/media/audio_input_device_manager.h" 12 #include "content/browser/renderer_host/media/audio_input_device_manager.h"
13 #include "content/browser/renderer_host/media/audio_input_device_manager_event_h andler.h"
14 #include "content/public/common/media_stream_request.h" 13 #include "content/public/common/media_stream_request.h"
15 #include "media/audio/audio_manager_base.h" 14 #include "media/audio/audio_manager_base.h"
16 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
18 17
19 using testing::_; 18 using testing::_;
20 using testing::InSequence; 19 using testing::InSequence;
21 using testing::SaveArg; 20 using testing::SaveArg;
22 using testing::Return; 21 using testing::Return;
23 22
(...skipping 10 matching lines...) Expand all
34 MOCK_METHOD2(DevicesEnumerated, void(MediaStreamType, 33 MOCK_METHOD2(DevicesEnumerated, void(MediaStreamType,
35 const StreamDeviceInfoArray&)); 34 const StreamDeviceInfoArray&));
36 MOCK_METHOD3(Error, void(MediaStreamType, int, MediaStreamProviderError)); 35 MOCK_METHOD3(Error, void(MediaStreamType, int, MediaStreamProviderError));
37 36
38 StreamDeviceInfoArray devices_; 37 StreamDeviceInfoArray devices_;
39 38
40 private: 39 private:
41 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerListener); 40 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerListener);
42 }; 41 };
43 42
44 class MockAudioInputDeviceManagerEventHandler
45 : public AudioInputDeviceManagerEventHandler {
46 public:
47 explicit MockAudioInputDeviceManagerEventHandler(MessageLoop* message_loop)
48 : message_loop_(message_loop) {}
49 virtual ~MockAudioInputDeviceManagerEventHandler() {}
50
51 MOCK_METHOD2(DeviceStarted, void(int, const std::string&));
52 MOCK_METHOD1(DeviceStopped, void(int));
53
54 virtual void OnDeviceStarted(int session_id,
55 const std::string& device_id) {
56 message_loop_->PostTask(
57 FROM_HERE, base::Bind(
58 &MockAudioInputDeviceManagerEventHandler::DeviceStarted,
59 base::Unretained(this), session_id, device_id));
60 }
61
62 virtual void OnDeviceStopped(int session_id) {
63 message_loop_->PostTask(
64 FROM_HERE, base::Bind(
65 &MockAudioInputDeviceManagerEventHandler::DeviceStopped,
66 base::Unretained(this), session_id));
67 }
68
69 private:
70 MessageLoop* message_loop_;
71 DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerEventHandler);
72 };
73
74 class AudioInputDeviceManagerTest : public testing::Test { 43 class AudioInputDeviceManagerTest : public testing::Test {
75 public: 44 public:
76 AudioInputDeviceManagerTest() {} 45 AudioInputDeviceManagerTest() {}
77 46
78 // Returns true iff machine has an audio input device. 47 // Returns true iff machine has an audio input device.
79 bool CanRunAudioInputDeviceTests() { 48 bool CanRunAudioInputDeviceTests() {
80 return audio_manager_->HasAudioInputDevices(); 49 return audio_manager_->HasAudioInputDevices();
81 } 50 }
82 51
83 protected: 52 protected:
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 return; 95 return;
127 96
128 ASSERT_FALSE(devices_.empty()); 97 ASSERT_FALSE(devices_.empty());
129 98
130 InSequence s; 99 InSequence s;
131 100
132 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); 101 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin();
133 iter != devices_.end(); ++iter) { 102 iter != devices_.end(); ++iter) {
134 // Opens/closes the devices. 103 // Opens/closes the devices.
135 int session_id = manager_->Open(*iter); 104 int session_id = manager_->Open(*iter);
136 manager_->Close(session_id);
137 105
138 // Expected mock call with expected return value. 106 // Expected mock call with expected return value.
139 EXPECT_CALL(*audio_input_listener_, 107 EXPECT_CALL(*audio_input_listener_,
140 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) 108 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id))
141 .Times(1); 109 .Times(1);
110 // Waits for the callback.
111 message_loop_->RunUntilIdle();
112
113 manager_->Close(session_id);
142 EXPECT_CALL(*audio_input_listener_, 114 EXPECT_CALL(*audio_input_listener_,
143 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) 115 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id))
144 .Times(1); 116 .Times(1);
145 117
146 // Waits for the callback. 118 // Waits for the callback.
147 message_loop_->RunUntilIdle(); 119 message_loop_->RunUntilIdle();
148 } 120 }
149 } 121 }
150 122
151 // Opens multiple devices at one time and closes them later. 123 // Opens multiple devices at one time and closes them later.
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 if (!CanRunAudioInputDeviceTests()) 194 if (!CanRunAudioInputDeviceTests())
223 return; 195 return;
224 196
225 ASSERT_FALSE(devices_.empty()); 197 ASSERT_FALSE(devices_.empty());
226 198
227 InSequence s; 199 InSequence s;
228 200
229 // Opens and closes the default device twice. 201 // Opens and closes the default device twice.
230 int first_session_id = manager_->Open(devices_.front()); 202 int first_session_id = manager_->Open(devices_.front());
231 int second_session_id = manager_->Open(devices_.front()); 203 int second_session_id = manager_->Open(devices_.front());
232 manager_->Close(first_session_id);
233 manager_->Close(second_session_id);
234 204
235 // Expected mock calls with expected returned values. 205 // Expected mock calls with expected returned values.
236 EXPECT_NE(first_session_id, second_session_id); 206 EXPECT_NE(first_session_id, second_session_id);
237 EXPECT_CALL(*audio_input_listener_, 207 EXPECT_CALL(*audio_input_listener_,
238 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) 208 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id))
239 .Times(1); 209 .Times(1);
240 EXPECT_CALL(*audio_input_listener_, 210 EXPECT_CALL(*audio_input_listener_,
241 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) 211 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id))
242 .Times(1); 212 .Times(1);
213 // Waits for the callback.
214 message_loop_->RunUntilIdle();
215
216 manager_->Close(first_session_id);
217 manager_->Close(second_session_id);
243 EXPECT_CALL(*audio_input_listener_, 218 EXPECT_CALL(*audio_input_listener_,
244 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id)) 219 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id))
245 .Times(1); 220 .Times(1);
246 EXPECT_CALL(*audio_input_listener_, 221 EXPECT_CALL(*audio_input_listener_,
247 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) 222 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id))
248 .Times(1); 223 .Times(1);
249
250 // Waits for the callback. 224 // Waits for the callback.
251 message_loop_->RunUntilIdle(); 225 message_loop_->RunUntilIdle();
252 } 226 }
253 227
254 // Starts and closes the sessions after opening the devices. 228 // Accesses then closes the sessions after opening the devices.
255 TEST_F(AudioInputDeviceManagerTest, StartAndStopSession) { 229 TEST_F(AudioInputDeviceManagerTest, AccessAndCloseSession) {
256 if (!CanRunAudioInputDeviceTests()) 230 if (!CanRunAudioInputDeviceTests())
257 return; 231 return;
258 232
259 ASSERT_FALSE(devices_.empty()); 233 ASSERT_FALSE(devices_.empty());
260 234
261 InSequence s; 235 InSequence s;
262 236
263 int index = 0; 237 int index = 0;
264 scoped_array<int> session_id(new int[devices_.size()]); 238 scoped_array<int> session_id(new int[devices_.size()]);
265 239
266 // Creates the EventHandler for the sessions. 240 // Loops through the devices and calls Open()/Close()/GetOpenedDeviceInfoById
267 scoped_ptr<MockAudioInputDeviceManagerEventHandler> 241 // for each device.
268 audio_input_event_handler(
269 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
270
271 // Loops through the devices and calls Open()/Start()/Stop()/Close() for
272 // each device.
273 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin(); 242 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin();
274 iter != devices_.end(); ++iter, ++index) { 243 iter != devices_.end(); ++iter, ++index) {
275 // Note that no DeviceStopped() notification for Event Handler as we have 244 // Note that no DeviceStopped() notification for Event Handler as we have
276 // stopped the device before calling close. 245 // stopped the device before calling close.
277 session_id[index] = manager_->Open(*iter); 246 session_id[index] = manager_->Open(*iter);
278 EXPECT_CALL(*audio_input_listener_, 247 EXPECT_CALL(*audio_input_listener_,
279 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) 248 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index]))
280 .Times(1); 249 .Times(1);
281 message_loop_->RunUntilIdle(); 250 message_loop_->RunUntilIdle();
282 251
283 manager_->Start(session_id[index], audio_input_event_handler.get()); 252 const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById(
284 EXPECT_CALL(*audio_input_event_handler, 253 session_id[index]);
285 DeviceStarted(session_id[index], iter->device.id)) 254 DCHECK(info);
286 .Times(1); 255 EXPECT_EQ(iter->device.id, info->device.id);
287 message_loop_->RunUntilIdle();
288
289 manager_->Stop(session_id[index]);
290 manager_->Close(session_id[index]); 256 manager_->Close(session_id[index]);
291 EXPECT_CALL(*audio_input_listener_, 257 EXPECT_CALL(*audio_input_listener_,
292 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) 258 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index]))
293 .Times(1); 259 .Times(1);
294 message_loop_->RunUntilIdle(); 260 message_loop_->RunUntilIdle();
295 } 261 }
296 } 262 }
297 263
298 // Tests the behavior of calling Close() without calling Stop(). 264 // Access an invalid session.
299 TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopSession) { 265 TEST_F(AudioInputDeviceManagerTest, AccessInvalidSession) {
300 if (!CanRunAudioInputDeviceTests())
301 return;
302
303 ASSERT_FALSE(devices_.empty());
304
305 InSequence s;
306
307 int index = 0;
308 scoped_array<int> session_id(new int[devices_.size()]);
309
310 // Creates the EventHandlers for the sessions.
311 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
312 audio_input_event_handler(
313 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
314
315 // Loop through the devices, and calls Open()/Start()/Close() for the devices.
316 // Note that we do not call stop.
317 for (StreamDeviceInfoArray::const_iterator iter = devices_.begin();
318 iter != devices_.end(); ++iter, ++index) {
319 // Calls Open()/Start()/Close() for each device.
320 session_id[index] = manager_->Open(*iter);
321 EXPECT_CALL(*audio_input_listener_,
322 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index]))
323 .Times(1);
324 message_loop_->RunUntilIdle();
325
326 manager_->Start(session_id[index], audio_input_event_handler.get());
327 EXPECT_CALL(*audio_input_event_handler,
328 DeviceStarted(session_id[index], iter->device.id))
329 .Times(1);
330 message_loop_->RunUntilIdle();
331
332 // Event Handler should get a stop device notification as no stop is called
333 // before closing the device.
334 manager_->Close(session_id[index]);
335 EXPECT_CALL(*audio_input_event_handler,
336 DeviceStopped(session_id[index]))
337 .Times(1);
338 EXPECT_CALL(*audio_input_listener_,
339 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index]))
340 .Times(1);
341 message_loop_->RunUntilIdle();
342 }
343 }
344
345 // Starts the same device twice.
346 TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) {
347 if (!CanRunAudioInputDeviceTests())
348 return;
349
350 ASSERT_FALSE(devices_.empty());
351
352 InSequence s;
353
354 // Create one EventHandler for each session.
355 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
356 first_event_handler(
357 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
358 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
359 second_event_handler(
360 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
361
362 // Open the default device twice.
363 StreamDeviceInfoArray::const_iterator iter = devices_.begin();
364 int first_session_id = manager_->Open(*iter);
365 int second_session_id = manager_->Open(*iter);
366 EXPECT_NE(first_session_id, second_session_id);
367 EXPECT_CALL(*audio_input_listener_,
368 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id))
369 .Times(1);
370 EXPECT_CALL(*audio_input_listener_,
371 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id))
372 .Times(1);
373 message_loop_->RunUntilIdle();
374
375 // Calls Start()/Stop()/Close() for the default device twice.
376 manager_->Start(first_session_id, first_event_handler.get());
377 manager_->Start(second_session_id, second_event_handler.get());
378 EXPECT_CALL(*first_event_handler,
379 DeviceStarted(first_session_id,
380 media::AudioManagerBase::kDefaultDeviceId))
381 .Times(1);
382 EXPECT_CALL(*second_event_handler,
383 DeviceStarted(second_session_id,
384 media::AudioManagerBase::kDefaultDeviceId))
385 .Times(1);
386 message_loop_->RunUntilIdle();
387
388 manager_->Stop(first_session_id);
389 manager_->Stop(second_session_id);
390 manager_->Close(first_session_id);
391 manager_->Close(second_session_id);
392 EXPECT_CALL(*audio_input_listener_,
393 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, first_session_id))
394 .Times(1);
395 EXPECT_CALL(*audio_input_listener_,
396 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id))
397 .Times(1);
398 message_loop_->RunUntilIdle();
399 }
400
401 // Starts an invalid session.
402 TEST_F(AudioInputDeviceManagerTest, StartInvalidSession) {
403 if (!CanRunAudioInputDeviceTests()) 266 if (!CanRunAudioInputDeviceTests())
404 return; 267 return;
405 InSequence s; 268 InSequence s;
406 269
407 // Creates the EventHandlers for the sessions.
408 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
409 audio_input_event_handler(
410 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
411
412 // Opens the first device. 270 // Opens the first device.
413 StreamDeviceInfoArray::const_iterator iter = devices_.begin(); 271 StreamDeviceInfoArray::const_iterator iter = devices_.begin();
414 int session_id = manager_->Open(*iter); 272 int session_id = manager_->Open(*iter);
415 EXPECT_CALL(*audio_input_listener_, 273 EXPECT_CALL(*audio_input_listener_,
416 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) 274 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id))
417 .Times(1); 275 .Times(1);
418 message_loop_->RunUntilIdle(); 276 message_loop_->RunUntilIdle();
419 277
420 // Starts a non-opened device. 278 // Access a non-opened device.
421 // This should fail and trigger error code 'kDeviceNotAvailable'. 279 // This should fail and return an empty StreamDeviceInfo.
422 int invalid_session_id = session_id + 1; 280 int invalid_session_id = session_id + 1;
423 manager_->Start(invalid_session_id, audio_input_event_handler.get()); 281 const StreamDeviceInfo* info =
424 EXPECT_CALL(*audio_input_event_handler, 282 manager_->GetOpenedDeviceInfoById(invalid_session_id);
425 DeviceStarted(invalid_session_id, std::string())) 283 DCHECK(!info);
426 .Times(1);
427 message_loop_->RunUntilIdle();
428 284
429 manager_->Close(session_id); 285 manager_->Close(session_id);
430 EXPECT_CALL(*audio_input_listener_, 286 EXPECT_CALL(*audio_input_listener_,
431 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) 287 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id))
432 .Times(1);
433 message_loop_->RunUntilIdle();
434 }
435
436 // Starts a session twice, the first time should succeed, while the second
437 // time should fail.
438 TEST_F(AudioInputDeviceManagerTest, StartSessionTwice) {
439 if (!CanRunAudioInputDeviceTests())
440 return;
441 InSequence s;
442
443 // Creates the EventHandlers for the sessions.
444 scoped_ptr<MockAudioInputDeviceManagerEventHandler>
445 audio_input_event_handler(
446 new MockAudioInputDeviceManagerEventHandler(message_loop_.get()));
447
448 // Opens the first device.
449 StreamDeviceInfoArray::const_iterator iter = devices_.begin();
450 int session_id = manager_->Open(*iter);
451 EXPECT_CALL(*audio_input_listener_,
452 Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id))
453 .Times(1);
454 message_loop_->RunUntilIdle();
455
456 // Starts the session, it should succeed.
457 manager_->Start(session_id, audio_input_event_handler.get());
458 EXPECT_CALL(*audio_input_event_handler,
459 DeviceStarted(session_id,
460 media::AudioManagerBase::kDefaultDeviceId))
461 .Times(1);
462 message_loop_->RunUntilIdle();
463
464 // Starts the session for the second time, it should fail.
465 manager_->Start(session_id, audio_input_event_handler.get());
466 EXPECT_CALL(*audio_input_event_handler,
467 DeviceStarted(session_id, std::string()))
468 .Times(1);
469
470 manager_->Stop(session_id);
471 manager_->Close(session_id);
472 EXPECT_CALL(*audio_input_listener_,
473 Closed(MEDIA_DEVICE_AUDIO_CAPTURE, session_id))
474 .Times(1); 288 .Times(1);
475 message_loop_->RunUntilIdle(); 289 message_loop_->RunUntilIdle();
476 } 290 }
477 291
478 } // namespace content 292 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698