Chromium Code Reviews| Index: content/browser/renderer_host/media/audio_input_device_manager_unittest.cc |
| =================================================================== |
| --- content/browser/renderer_host/media/audio_input_device_manager_unittest.cc (revision 111331) |
| +++ content/browser/renderer_host/media/audio_input_device_manager_unittest.cc (working copy) |
| @@ -53,13 +53,30 @@ |
| class MockAudioInputDeviceManagerEventHandler |
| : public AudioInputDeviceManagerEventHandler { |
| public: |
| - MockAudioInputDeviceManagerEventHandler() {} |
| + MockAudioInputDeviceManagerEventHandler(MessageLoop* message_loop) |
| + : message_loop_(message_loop) {} |
| virtual ~MockAudioInputDeviceManagerEventHandler() {} |
| - MOCK_METHOD2(OnDeviceStarted, void(int, const std::string&)); |
| - MOCK_METHOD1(OnDeviceStopped, void(int)); |
| + MOCK_METHOD2(DeviceStarted, void(int, const std::string&)); |
| + MOCK_METHOD1(DeviceStopped, void(int)); |
| + virtual void OnDeviceStarted(int session_id, |
| + const std::string& device_id) { |
| + message_loop_->PostTask( |
| + FROM_HERE, base::Bind( |
| + &MockAudioInputDeviceManagerEventHandler::DeviceStarted, |
| + base::Unretained(this), session_id, device_id)); |
| + } |
| + |
| + virtual void OnDeviceStopped(int session_id) { |
| + message_loop_->PostTask( |
| + FROM_HERE, base::Bind( |
| + &MockAudioInputDeviceManagerEventHandler::DeviceStopped, |
| + base::Unretained(this), session_id)); |
| + } |
| + |
| private: |
| + MessageLoop* message_loop_; |
| DISALLOW_COPY_AND_ASSIGN(MockAudioInputDeviceManagerEventHandler); |
| }; |
| @@ -98,8 +115,8 @@ |
| manager_->EnumerateDevices(); |
| EXPECT_CALL(*audio_input_listener_, DevicesEnumerated(_)) |
| .Times(1); |
| - // Sync up the threads to make sure we get the list. |
| - SyncWithAudioInputDeviceManagerThread(); |
| + // Wait for the callback. |
|
henrika (OOO until Aug 14)
2011/11/24 15:22:26
As I've understand it all comments but the first i
no longer working on chromium
2011/11/24 16:00:42
Done.
|
| + message_loop_->RunAllPending(); |
| } |
| virtual void TearDown() { |
| @@ -107,29 +124,6 @@ |
| io_thread_.reset(); |
| } |
| - // Called on the AudioInputDeviceManager thread. |
| - static void PostQuitMessageLoop(MessageLoop* message_loop) { |
| - message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| - } |
| - |
| - // Called on the main thread. |
| - static void PostQuitOnAudioInputDeviceManagerThread( |
| - MessageLoop* message_loop, AudioInputDeviceManager* manager) { |
| - manager->message_loop()->PostTask( |
| - FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop)); |
| - } |
| - |
| - // SyncWithAudioInputDeviceManagerThread() waits until all pending tasks on |
| - // the audio_input_device_manager thread are executed while also processing |
| - // pending task in message_loop_ on the current thread. |
| - void SyncWithAudioInputDeviceManagerThread() { |
| - message_loop_->PostTask( |
| - FROM_HERE, |
| - base::Bind(&PostQuitOnAudioInputDeviceManagerThread, |
| - message_loop_.get(), |
| - manager_.get())); |
| - message_loop_->Run(); |
| - } |
| scoped_ptr<MessageLoop> message_loop_; |
| scoped_ptr<BrowserThreadImpl> io_thread_; |
| scoped_ptr<AudioInputDeviceManager> manager_; |
| @@ -157,7 +151,8 @@ |
| .Times(1); |
| EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + // Wait for the callback. |
| + message_loop_->RunAllPending(); |
| } |
| } |
| @@ -182,7 +177,8 @@ |
| EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, |
| session_id[index])) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + // Wait for the callback. |
| + message_loop_->RunAllPending(); |
| } |
| // Check if the session_ids are unique |
| @@ -197,7 +193,8 @@ |
| manager_->Close(session_id[i]); |
| EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id[i])) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + // Wait for the callback. |
| + message_loop_->RunAllPending(); |
| } |
| } |
| @@ -212,35 +209,14 @@ |
| std::string device_id("id_doesnt_exist"); |
| StreamDeviceInfo dummy_device(stream_type, device_name, device_id, false); |
| - // This should fail and trigger error code 'kDeviceNotAvailable'. |
| + // This should still pass. |
|
henrika (OOO until Aug 14)
2011/11/24 15:22:26
Comment does not add much value to the test descri
no longer working on chromium
2011/11/24 16:00:42
Done.
|
| int session_id = manager_->Open(dummy_device); |
| - |
| - EXPECT_CALL(*audio_input_listener_, Error(_, session_id, kDeviceNotAvailable)) |
| + EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + // Wait for the callback. |
| + message_loop_->RunAllPending(); |
| } |
| -// Try open an invalid device. |
| -TEST_F(AudioInputDeviceManagerTest, OpenInvalidDevice) { |
| - if (!CanRunAudioInputDeviceTests()) |
| - return; |
| - InSequence s; |
| - |
| - MediaStreamType stream_type = kAudioCapture; |
| - std::string device_name; |
| - std::string device_id; |
| - device_name = audio_input_listener_->devices_.front().name; |
| - device_id = "wrong_id"; |
| - StreamDeviceInfo invalid_device(stream_type, device_name, device_id, false); |
| - |
| - // This should fail and trigger error code 'kDeviceNotAvailable'. |
| - int session_id = manager_->Open(invalid_device); |
| - |
| - EXPECT_CALL(*audio_input_listener_, Error(_, session_id, kDeviceNotAvailable)) |
| - .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| -} |
| - |
| // Opening default device twice should work. |
| TEST_F(AudioInputDeviceManagerTest, OpenDeviceTwice) { |
| if (!CanRunAudioInputDeviceTests()) |
| @@ -265,11 +241,12 @@ |
| .Times(1); |
| EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + // Wait for the callback. |
| + message_loop_->RunAllPending(); |
| } |
| // Test the Start and Close function after opening the devices. |
| -TEST_F(AudioInputDeviceManagerTest, StartAndStopDevice) { |
| +TEST_F(AudioInputDeviceManagerTest, StartAndStopSession) { |
| if (!CanRunAudioInputDeviceTests()) |
| return; |
| InSequence s; |
| @@ -280,7 +257,8 @@ |
| // Create the EventHandler for the sessions. |
| scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| - audio_input_event_handler(new MockAudioInputDeviceManagerEventHandler()); |
| + audio_input_event_handler( |
| + new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| // Loop through the devices, and Open/start/stop/close each device. |
| for (StreamDeviceInfoArray::const_iterator iter = |
| @@ -290,26 +268,28 @@ |
| // stopped the device before calling close. |
| // Open/start/stop/close the device. |
| session_id[index] = manager_->Open(*iter); |
| - manager_->Start(session_id[index], audio_input_event_handler.get()); |
| - manager_->Stop(session_id[index]); |
| - manager_->Close(session_id[index]); |
| - |
| - // Expected mock calls with expected return values. |
| EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, |
| session_id[index])) |
| .Times(1); |
| + message_loop_->RunAllPending(); |
| + |
| + manager_->Start(session_id[index], audio_input_event_handler.get()); |
| EXPECT_CALL(*audio_input_event_handler, |
| - OnDeviceStarted(session_id[index], iter->device_id)) |
| + DeviceStarted(session_id[index], iter->device_id)) |
| .Times(1); |
| + message_loop_->RunAllPending(); |
| + |
| + manager_->Stop(session_id[index]); |
| + manager_->Close(session_id[index]); |
| EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, |
| session_id[index])) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + message_loop_->RunAllPending(); |
| } |
| } |
| // Test the behavior of calling Close without calling Stop. |
| -TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopDevice) { |
| +TEST_F(AudioInputDeviceManagerTest, CloseWithoutStopSession) { |
| if (!CanRunAudioInputDeviceTests()) |
| return; |
| InSequence s; |
| @@ -320,7 +300,8 @@ |
| // Create the EventHandlers for the sessions. |
| scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| - audio_input_event_handler(new MockAudioInputDeviceManagerEventHandler()); |
| + audio_input_event_handler( |
| + new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| // Loop through the devices, and open/start/close the devices. |
| // Note that we do not call stop. |
| @@ -329,29 +310,31 @@ |
| iter != audio_input_listener_->devices_.end(); ++iter, ++index) { |
| // Open/start/close the device. |
| session_id[index] = manager_->Open(*iter); |
| - manager_->Start(session_id[index], audio_input_event_handler.get()); |
| - manager_->Close(session_id[index]); |
| - |
| - // Expected mock calls with expected return values. |
| EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, |
| session_id[index])) |
| .Times(1); |
| + message_loop_->RunAllPending(); |
| + |
| + manager_->Start(session_id[index], audio_input_event_handler.get()); |
| EXPECT_CALL(*audio_input_event_handler, |
| - OnDeviceStarted(session_id[index], iter->device_id)) |
| + DeviceStarted(session_id[index], iter->device_id)) |
| .Times(1); |
| + message_loop_->RunAllPending(); |
| + |
| // Event Handler should get a stop device notification as no stop is called |
| // before closing the device. |
| + manager_->Close(session_id[index]); |
| EXPECT_CALL(*audio_input_event_handler, |
| - OnDeviceStopped(session_id[index])) |
| + DeviceStopped(session_id[index])) |
| .Times(1); |
| EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, |
| session_id[index])) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + message_loop_->RunAllPending(); |
| } |
| } |
| -// Should be able to start the default device twice. |
| +// Should be able to start the same device twice. |
| TEST_F(AudioInputDeviceManagerTest, StartDeviceTwice) { |
| if (!CanRunAudioInputDeviceTests()) |
| return; |
| @@ -359,45 +342,122 @@ |
| // Create one EventHandler for each session. |
| scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| - first_audio_input_event_handler( |
| - new MockAudioInputDeviceManagerEventHandler()); |
| + first_event_handler( |
| + new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| - second_audio_input_event_handler( |
| - new MockAudioInputDeviceManagerEventHandler()); |
| + second_event_handler( |
| + new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| // Open the default device twice. |
| StreamDeviceInfoArray::const_iterator iter = |
| audio_input_listener_->devices_.begin(); |
| int first_session_id = manager_->Open(*iter); |
| int second_session_id = manager_->Open(*iter); |
| + EXPECT_NE(first_session_id, second_session_id); |
| + EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) |
| + .Times(1); |
| + EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) |
| + .Times(1); |
| + message_loop_->RunAllPending(); |
| // Start/stop/close the default device twice. |
| - manager_->Start(first_session_id, first_audio_input_event_handler.get()); |
| - manager_->Start(second_session_id, second_audio_input_event_handler.get()); |
| + manager_->Start(first_session_id, first_event_handler.get()); |
| + manager_->Start(second_session_id, second_event_handler.get()); |
| + EXPECT_CALL(*first_event_handler, |
| + DeviceStarted(first_session_id, |
| + AudioManagerBase::kDefaultDeviceId)) |
| + .Times(1); |
| + EXPECT_CALL(*second_event_handler, |
| + DeviceStarted(second_session_id, |
| + AudioManagerBase::kDefaultDeviceId)) |
| + .Times(1); |
| + message_loop_->RunAllPending(); |
| + |
| manager_->Stop(first_session_id); |
| manager_->Stop(second_session_id); |
| manager_->Close(first_session_id); |
| manager_->Close(second_session_id); |
| + EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) |
| + .Times(1); |
| + EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) |
| + .Times(1); |
| + message_loop_->RunAllPending(); |
| +} |
| - // Expected mock calls with expected return values. |
| - EXPECT_NE(first_session_id, second_session_id); |
| - EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, first_session_id)) |
| +// Try start a invalid session device. |
|
henrika (OOO until Aug 14)
2011/11/24 15:22:26
an
no longer working on chromium
2011/11/24 16:00:42
Done.
|
| +TEST_F(AudioInputDeviceManagerTest, StartInvalidSession) { |
| + if (!CanRunAudioInputDeviceTests()) |
| + return; |
| + InSequence s; |
| + |
| + // Create the EventHandlers for the sessions. |
| + scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| + audio_input_event_handler( |
| + new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| + |
| + // Open the first device. |
| + StreamDeviceInfoArray::const_iterator iter = |
| + audio_input_listener_->devices_.begin(); |
| + int session_id = manager_->Open(*iter); |
| + EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) |
| .Times(1); |
| - EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, second_session_id)) |
| + message_loop_->RunAllPending(); |
| + |
| + // Start a non-opened device. |
| + // This should fail and trigger error code 'kDeviceNotAvailable'. |
| + int invalid_session_id = session_id + 1; |
| + manager_->Start(invalid_session_id, audio_input_event_handler.get()); |
| + EXPECT_CALL(*audio_input_event_handler, |
| + DeviceStarted(invalid_session_id, |
| + AudioInputDeviceManager::kInvalidDeviceId)) |
| .Times(1); |
| - EXPECT_CALL(*first_audio_input_event_handler, |
| - OnDeviceStarted(first_session_id, |
| - AudioManagerBase::kDefaultDeviceId)) |
| + message_loop_->RunAllPending(); |
| + |
| + manager_->Close(session_id); |
| + EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) |
| .Times(1); |
| - EXPECT_CALL(*second_audio_input_event_handler, |
| - OnDeviceStarted(second_session_id, |
| - AudioManagerBase::kDefaultDeviceId)) |
| + message_loop_->RunAllPending(); |
| +} |
| + |
| +// Try start a session twice. |
|
henrika (OOO until Aug 14)
2011/11/24 15:22:26
Try to start..
And please add some more about the
no longer working on chromium
2011/11/24 16:00:42
Done.
|
| +TEST_F(AudioInputDeviceManagerTest, StartSessionTwice) { |
| + if (!CanRunAudioInputDeviceTests()) |
| + return; |
| + InSequence s; |
| + |
| + // Create the EventHandlers for the sessions. |
| + scoped_ptr<MockAudioInputDeviceManagerEventHandler> |
| + audio_input_event_handler( |
| + new MockAudioInputDeviceManagerEventHandler(message_loop_.get())); |
| + |
| + // Open the first device. |
| + StreamDeviceInfoArray::const_iterator iter = |
| + audio_input_listener_->devices_.begin(); |
| + int session_id = manager_->Open(*iter); |
| + EXPECT_CALL(*audio_input_listener_, Opened(kAudioCapture, session_id)) |
| .Times(1); |
| - EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, first_session_id)) |
| + message_loop_->RunAllPending(); |
| + |
| + // Start the session, it should succeed. |
| + manager_->Start(session_id, audio_input_event_handler.get()); |
| + EXPECT_CALL(*audio_input_event_handler, |
| + DeviceStarted(session_id, |
| + AudioManagerBase::kDefaultDeviceId)) |
| .Times(1); |
| - EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, second_session_id)) |
| + message_loop_->RunAllPending(); |
| + |
| + // Start the session for the second time, it should fail. |
| + manager_->Start(session_id, audio_input_event_handler.get()); |
| + EXPECT_CALL(*audio_input_event_handler, |
| + DeviceStarted(session_id, |
| + AudioInputDeviceManager::kInvalidDeviceId)) |
| .Times(1); |
| - SyncWithAudioInputDeviceManagerThread(); |
| + |
| + manager_->Stop(session_id); |
| + manager_->Close(session_id); |
| + EXPECT_CALL(*audio_input_listener_, Closed(kAudioCapture, session_id)) |
| + .Times(1); |
| + message_loop_->RunAllPending(); |
| } |
| } // namespace media_stream |