| Index: media/audio/audio_input_controller_unittest.cc
|
| diff --git a/media/audio/audio_input_controller_unittest.cc b/media/audio/audio_input_controller_unittest.cc
|
| index 6d44002e817d1fba3b08b5d5051a28017f1f995e..fdf61208fae4594d8c2d37a6f9eb62078e440d07 100644
|
| --- a/media/audio/audio_input_controller_unittest.cc
|
| +++ b/media/audio/audio_input_controller_unittest.cc
|
| @@ -8,16 +8,19 @@
|
| #include "base/run_loop.h"
|
| #include "base/synchronization/waitable_event.h"
|
| #include "base/test/test_timeouts.h"
|
| +#include "base/threading/thread.h"
|
| #include "media/audio/audio_device_description.h"
|
| #include "media/audio/audio_input_controller.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| using ::testing::_;
|
| +using ::testing::AnyNumber;
|
| using ::testing::AtLeast;
|
| using ::testing::Exactly;
|
| using ::testing::InvokeWithoutArgs;
|
| using ::testing::NotNull;
|
| +using base::WaitableEvent;
|
|
|
| namespace media {
|
|
|
| @@ -53,8 +56,6 @@ class MockAudioInputControllerEventHandler
|
| MOCK_METHOD1(OnCreated, void(AudioInputController* controller));
|
| MOCK_METHOD2(OnError, void(AudioInputController* controller,
|
| AudioInputController::ErrorCode error_code));
|
| - MOCK_METHOD2(OnData,
|
| - void(AudioInputController* controller, const AudioBus* data));
|
| MOCK_METHOD2(OnLog,
|
| void(AudioInputController* controller,
|
| const std::string& message));
|
| @@ -63,23 +64,57 @@ class MockAudioInputControllerEventHandler
|
| DISALLOW_COPY_AND_ASSIGN(MockAudioInputControllerEventHandler);
|
| };
|
|
|
| +class MockSyncWriter : public AudioInputController::SyncWriter {
|
| + public:
|
| + MockSyncWriter() {}
|
| +
|
| + MOCK_METHOD4(Write,
|
| + void(const AudioBus* data,
|
| + double volume,
|
| + bool key_pressed,
|
| + uint32_t hardware_delay_bytes));
|
| + MOCK_METHOD0(Close, void());
|
| +};
|
| +
|
| // Test fixture.
|
| class AudioInputControllerTest : public testing::Test {
|
| public:
|
| AudioInputControllerTest()
|
| - : audio_manager_(
|
| - AudioManager::CreateForTesting(message_loop_.task_runner())) {
|
| - // Flush the message loop to ensure that AudioManager is fully initialized.
|
| - base::RunLoop().RunUntilIdle();
|
| + : audio_thread_("AudioThread"),
|
| + suspend_event_(WaitableEvent::ResetPolicy::AUTOMATIC,
|
| + WaitableEvent::InitialState::NOT_SIGNALED) {
|
| + audio_thread_.StartAndWaitForTesting();
|
| + audio_manager_ =
|
| + AudioManager::CreateForTesting(audio_thread_.task_runner());
|
| }
|
| ~AudioInputControllerTest() override {
|
| - audio_manager_.reset();
|
| - base::RunLoop().RunUntilIdle();
|
| + audio_task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioInputControllerTest::DeleteAudioManager,
|
| + base::Unretained(this)));
|
| + audio_thread_.Stop();
|
| + }
|
| +
|
| + scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner() const {
|
| + return audio_thread_.task_runner();
|
| }
|
|
|
| + void SuspendAudioThread() {
|
| + audio_task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioInputControllerTest::WaitForResume,
|
| + base::Unretained(this)));
|
| + }
|
| +
|
| + void ResumeAudioThread() { suspend_event_.Signal(); }
|
| +
|
| + private:
|
| + void DeleteAudioManager() { audio_manager_.reset(); }
|
| + void WaitForResume() { suspend_event_.Wait(); }
|
| +
|
| protected:
|
| + base::Thread audio_thread_;
|
| base::MessageLoop message_loop_;
|
| ScopedAudioManagerPtr audio_manager_;
|
| + WaitableEvent suspend_event_;
|
|
|
| private:
|
| DISALLOW_COPY_AND_ASSIGN(AudioInputControllerTest);
|
| @@ -90,56 +125,59 @@ TEST_F(AudioInputControllerTest, CreateAndClose) {
|
| base::RunLoop run_loop;
|
|
|
| MockAudioInputControllerEventHandler event_handler;
|
| -
|
| - // OnCreated() will be posted once.
|
| - EXPECT_CALL(event_handler, OnCreated(NotNull()))
|
| - .WillOnce(QuitRunLoop(&run_loop));
|
| + MockSyncWriter sync_writer;
|
| + scoped_refptr<AudioInputController> controller;
|
|
|
| AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
|
| kSampleRate, kBitsPerSample, kSamplesPerPacket);
|
|
|
| - scoped_refptr<AudioInputController> controller = AudioInputController::Create(
|
| - audio_manager_.get(), &event_handler, params,
|
| + SuspendAudioThread();
|
| + controller = AudioInputController::Create(
|
| + audio_manager_.get(), &event_handler, &sync_writer, params,
|
| AudioDeviceDescription::kDefaultDeviceId, NULL);
|
| ASSERT_TRUE(controller.get());
|
| + EXPECT_CALL(event_handler, OnCreated(controller.get())).Times(Exactly(1));
|
| + EXPECT_CALL(event_handler, OnLog(controller.get(), _));
|
| + EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
|
| + ResumeAudioThread();
|
|
|
| - // Wait for OnCreated() to fire.
|
| - run_loop.Run();
|
| -
|
| - // Close the AudioInputController synchronously.
|
| CloseAudioController(controller.get());
|
| +
|
| + audio_thread_.FlushForTesting();
|
| }
|
|
|
| // Test a normal call sequence of create, record and close.
|
| TEST_F(AudioInputControllerTest, RecordAndClose) {
|
| MockAudioInputControllerEventHandler event_handler;
|
| + MockSyncWriter sync_writer;
|
| int count = 0;
|
|
|
| // OnCreated() will be called once.
|
| EXPECT_CALL(event_handler, OnCreated(NotNull()))
|
| .Times(Exactly(1));
|
|
|
| - // OnData() shall be called ten times.
|
| - EXPECT_CALL(event_handler, OnData(NotNull(), NotNull()))
|
| + // Write() should be called ten times.
|
| + EXPECT_CALL(sync_writer, Write(NotNull(), _, _, _))
|
| .Times(AtLeast(10))
|
| - .WillRepeatedly(CheckCountAndPostQuitTask(
|
| - &count, 10, message_loop_.task_runner()));
|
| + .WillRepeatedly(
|
| + CheckCountAndPostQuitTask(&count, 10, message_loop_.task_runner()));
|
| +
|
| + EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber());
|
| + EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
|
|
|
| AudioParameters params(AudioParameters::AUDIO_FAKE, kChannelLayout,
|
| kSampleRate, kBitsPerSample, kSamplesPerPacket);
|
|
|
| // Creating the AudioInputController should render an OnCreated() call.
|
| scoped_refptr<AudioInputController> controller = AudioInputController::Create(
|
| - audio_manager_.get(), &event_handler, params,
|
| + audio_manager_.get(), &event_handler, &sync_writer, params,
|
| AudioDeviceDescription::kDefaultDeviceId, NULL);
|
| ASSERT_TRUE(controller.get());
|
|
|
| controller->Record();
|
|
|
| - // Record and wait until ten OnData() callbacks are received.
|
| + // Record and wait until ten Write() callbacks are received.
|
| base::RunLoop().Run();
|
| -
|
| - // Close the AudioInputController synchronously.
|
| CloseAudioController(controller.get());
|
| }
|
|
|
| @@ -147,10 +185,10 @@ TEST_F(AudioInputControllerTest, RecordAndClose) {
|
| TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
|
| // Create an audio device with a very large packet size.
|
| MockAudioInputControllerEventHandler event_handler;
|
| + MockSyncWriter sync_writer;
|
|
|
| // OnCreated() shall not be called in this test.
|
| - EXPECT_CALL(event_handler, OnCreated(NotNull()))
|
| - .Times(Exactly(0));
|
| + EXPECT_CALL(event_handler, OnCreated(NotNull())).Times(Exactly(0));
|
|
|
| AudioParameters params(AudioParameters::AUDIO_FAKE,
|
| kChannelLayout,
|
| @@ -158,7 +196,7 @@ TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
|
| kBitsPerSample,
|
| kSamplesPerPacket * 1000);
|
| scoped_refptr<AudioInputController> controller = AudioInputController::Create(
|
| - audio_manager_.get(), &event_handler, params,
|
| + audio_manager_.get(), &event_handler, &sync_writer, params,
|
| AudioDeviceDescription::kDefaultDeviceId, NULL);
|
| ASSERT_FALSE(controller.get());
|
| }
|
| @@ -166,9 +204,13 @@ TEST_F(AudioInputControllerTest, SamplesPerPacketTooLarge) {
|
| // Test calling AudioInputController::Close multiple times.
|
| TEST_F(AudioInputControllerTest, CloseTwice) {
|
| MockAudioInputControllerEventHandler event_handler;
|
| + MockSyncWriter sync_writer;
|
|
|
| // OnCreated() will be called only once.
|
| - EXPECT_CALL(event_handler, OnCreated(NotNull()));
|
| + EXPECT_CALL(event_handler, OnCreated(NotNull())).Times(Exactly(1));
|
| + EXPECT_CALL(event_handler, OnLog(_, _)).Times(AnyNumber());
|
| + // This callback should still only be called once.
|
| + EXPECT_CALL(sync_writer, Close()).Times(Exactly(1));
|
|
|
| AudioParameters params(AudioParameters::AUDIO_FAKE,
|
| kChannelLayout,
|
| @@ -176,7 +218,7 @@ TEST_F(AudioInputControllerTest, CloseTwice) {
|
| kBitsPerSample,
|
| kSamplesPerPacket);
|
| scoped_refptr<AudioInputController> controller = AudioInputController::Create(
|
| - audio_manager_.get(), &event_handler, params,
|
| + audio_manager_.get(), &event_handler, &sync_writer, params,
|
| AudioDeviceDescription::kDefaultDeviceId, NULL);
|
| ASSERT_TRUE(controller.get());
|
|
|
|
|