Index: content/browser/renderer_host/media/audio_input_device_manager_unittest.cc |
diff --git a/content/browser/renderer_host/media/audio_input_device_manager_unittest.cc b/content/browser/renderer_host/media/audio_input_device_manager_unittest.cc |
index bf3b80a5528b2ecc579ddfedbbb3281c6f043d9b..2cb8741912f2ed1224f1f6c91e5374c02f02d886 100644 |
--- a/content/browser/renderer_host/media/audio_input_device_manager_unittest.cc |
+++ b/content/browser/renderer_host/media/audio_input_device_manager_unittest.cc |
@@ -21,7 +21,9 @@ |
#include "content/public/common/media_stream_request.h" |
#include "content/public/test/test_browser_thread_bundle.h" |
#include "media/audio/audio_manager_base.h" |
+#include "media/audio/audio_system_impl.h" |
#include "media/base/media_switches.h" |
+#include "media/base/test_helpers.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -58,18 +60,23 @@ class MockAudioInputDeviceManagerListener |
class MAYBE_AudioInputDeviceManagerTest : public testing::Test { |
public: |
- MAYBE_AudioInputDeviceManagerTest() {} |
+ MAYBE_AudioInputDeviceManagerTest() : audio_thread_("AudioSystemThread") { |
+ audio_thread_.StartAndWaitForTesting(); |
+ } |
protected: |
void SetUp() override { |
base::CommandLine::ForCurrentProcess()->AppendSwitch( |
switches::kUseFakeDeviceForMediaStream); |
- audio_manager_ = media::AudioManager::CreateForTesting( |
- base::ThreadTaskRunnerHandle::Get()); |
+ // AudioInputDeviceManager accesses AudioSystem from IO thread, so it never |
+ // runs on the same thread with it, even on Mac. |
+ audio_manager_ = |
+ media::AudioManager::CreateForTesting(audio_thread_.task_runner()); |
// Flush the message loop to ensure proper initialization of AudioManager. |
base::RunLoop().RunUntilIdle(); |
- manager_ = new AudioInputDeviceManager(audio_manager_.get()); |
+ audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
+ manager_ = new AudioInputDeviceManager(audio_system_.get()); |
audio_input_listener_.reset(new MockAudioInputDeviceManagerListener()); |
manager_->RegisterListener(audio_input_listener_.get()); |
@@ -85,12 +92,27 @@ class MAYBE_AudioInputDeviceManagerTest : public testing::Test { |
void TearDown() override { |
manager_->UnregisterListener(audio_input_listener_.get()); |
+ audio_system_.reset(); |
+ audio_manager_.reset(); |
+ audio_thread_.Stop(); |
+ } |
+ |
+ void WaitForOpenCompletion() { |
+ media::WaitableMessageLoopEvent event; |
+ audio_thread_.task_runner()->PostTaskAndReply( |
+ FROM_HERE, base::Bind(&base::DoNothing), event.GetClosure()); |
+ // Runs the loop and waits for the |audio_thread_| to call event's |
+ // closure. |
+ event.RunAndWait(); |
+ base::RunLoop().RunUntilIdle(); |
} |
TestBrowserThreadBundle thread_bundle_; |
+ base::Thread audio_thread_; |
+ media::ScopedAudioManagerPtr audio_manager_; |
+ std::unique_ptr<media::AudioSystem> audio_system_; |
scoped_refptr<AudioInputDeviceManager> manager_; |
std::unique_ptr<MockAudioInputDeviceManagerListener> audio_input_listener_; |
- media::ScopedAudioManagerPtr audio_manager_; |
StreamDeviceInfoArray devices_; |
private: |
@@ -113,7 +135,7 @@ TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenAndCloseDevice) { |
Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
.Times(1); |
// Waits for the callback. |
- base::RunLoop().RunUntilIdle(); |
+ WaitForOpenCompletion(); |
manager_->Close(session_id); |
EXPECT_CALL(*audio_input_listener_, |
@@ -146,7 +168,7 @@ TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenMultipleDevices) { |
.Times(1); |
// Waits for the callback. |
- base::RunLoop().RunUntilIdle(); |
+ WaitForOpenCompletion(); |
} |
// Checks if the session_ids are unique. |
@@ -183,7 +205,7 @@ TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenNotExistingDevice) { |
.Times(1); |
// Waits for the callback. |
- base::RunLoop().RunUntilIdle(); |
+ WaitForOpenCompletion(); |
} |
// Opens default device twice. |
@@ -205,7 +227,7 @@ TEST_F(MAYBE_AudioInputDeviceManagerTest, OpenDeviceTwice) { |
Opened(MEDIA_DEVICE_AUDIO_CAPTURE, second_session_id)) |
.Times(1); |
// Waits for the callback. |
- base::RunLoop().RunUntilIdle(); |
+ WaitForOpenCompletion(); |
manager_->Close(first_session_id); |
manager_->Close(second_session_id); |
@@ -238,7 +260,7 @@ TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessAndCloseSession) { |
EXPECT_CALL(*audio_input_listener_, |
Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id[index])) |
.Times(1); |
- base::RunLoop().RunUntilIdle(); |
+ WaitForOpenCompletion(); |
const StreamDeviceInfo* info = manager_->GetOpenedDeviceInfoById( |
session_id[index]); |
@@ -262,7 +284,7 @@ TEST_F(MAYBE_AudioInputDeviceManagerTest, AccessInvalidSession) { |
EXPECT_CALL(*audio_input_listener_, |
Opened(MEDIA_DEVICE_AUDIO_CAPTURE, session_id)) |
.Times(1); |
- base::RunLoop().RunUntilIdle(); |
+ WaitForOpenCompletion(); |
// Access a non-opened device. |
// This should fail and return an empty StreamDeviceInfo. |