| Index: media/audio/audio_output_device_unittest.cc
|
| diff --git a/media/audio/audio_output_device_unittest.cc b/media/audio/audio_output_device_unittest.cc
|
| index 328526e3892d58d7fed60ab233a68b188cc1ac86..0f05a4a27429a57eaf107c87836d74553138108c 100644
|
| --- a/media/audio/audio_output_device_unittest.cc
|
| +++ b/media/audio/audio_output_device_unittest.cc
|
| @@ -15,9 +15,11 @@
|
| #include "base/message_loop/message_loop.h"
|
| #include "base/process/process_handle.h"
|
| #include "base/sync_socket.h"
|
| +#include "base/synchronization/waitable_event.h"
|
| #include "base/task_runner.h"
|
| #include "base/test/test_timeouts.h"
|
| #include "base/thread_task_runner_handle.h"
|
| +#include "base/threading/thread.h"
|
| #include "media/audio/audio_output_device.h"
|
| #include "media/audio/sample_rates.h"
|
| #include "testing/gmock/include/gmock/gmock.h"
|
| @@ -93,13 +95,16 @@ class AudioOutputDeviceTest
|
| AudioOutputDeviceTest();
|
| ~AudioOutputDeviceTest();
|
|
|
| + void SetDevice(const std::string& device_id);
|
| void ReceiveAuthorization(OutputDeviceStatus device_status);
|
| + void InitializeAudioDevice();
|
| void StartAudioDevice();
|
| void CreateStream();
|
| void ExpectRenderCallback();
|
| void WaitUntilRenderCallback();
|
| void StopAudioDevice();
|
| - void SetDevice(const std::string& device_id);
|
| + void GetOutputDeviceInfo(base::WaitableEvent* event);
|
| + void SetDeviceInfoToGarbage();
|
|
|
| protected:
|
| // Used to clean up TLS pointers that the test(s) will initialize.
|
| @@ -111,6 +116,7 @@ class AudioOutputDeviceTest
|
| MockAudioOutputIPC* audio_output_ipc_; // owned by audio_device_
|
| scoped_refptr<AudioOutputDevice> audio_device_;
|
| OutputDeviceStatus device_status_;
|
| + OutputDeviceInfo device_info_;
|
|
|
| private:
|
| int CalculateMemorySize();
|
| @@ -132,7 +138,7 @@ AudioOutputDeviceTest::AudioOutputDeviceTest()
|
| : device_status_(OUTPUT_DEVICE_STATUS_ERROR_INTERNAL) {
|
| default_audio_parameters_.Reset(AudioParameters::AUDIO_PCM_LINEAR,
|
| CHANNEL_LAYOUT_STEREO, 48000, 16, 1024);
|
| - SetDevice(kDefaultDeviceId);
|
| + SetDeviceInfoToGarbage();
|
| }
|
|
|
| AudioOutputDeviceTest::~AudioOutputDeviceTest() {
|
| @@ -156,8 +162,7 @@ void AudioOutputDeviceTest::SetDevice(const std::string& device_id) {
|
| : OUTPUT_DEVICE_STATUS_OK;
|
| ReceiveAuthorization(device_status);
|
|
|
| - audio_device_->Initialize(default_audio_parameters_,
|
| - &callback_);
|
| + InitializeAudioDevice();
|
| }
|
|
|
| void AudioOutputDeviceTest::ReceiveAuthorization(OutputDeviceStatus status) {
|
| @@ -180,6 +185,10 @@ void AudioOutputDeviceTest::StartAudioDevice() {
|
| io_loop_.RunUntilIdle();
|
| }
|
|
|
| +void AudioOutputDeviceTest::InitializeAudioDevice() {
|
| + audio_device_->Initialize(default_audio_parameters_, &callback_);
|
| +}
|
| +
|
| void AudioOutputDeviceTest::CreateStream() {
|
| const int kMemorySize = CalculateMemorySize();
|
|
|
| @@ -243,43 +252,66 @@ void AudioOutputDeviceTest::StopAudioDevice() {
|
| io_loop_.RunUntilIdle();
|
| }
|
|
|
| +void AudioOutputDeviceTest::GetOutputDeviceInfo(base::WaitableEvent* event) {
|
| + device_info_ = audio_device_->GetOutputDeviceInfo();
|
| + event->Signal();
|
| +}
|
| +
|
| +void AudioOutputDeviceTest::SetDeviceInfoToGarbage() {
|
| + device_info_ =
|
| + OutputDeviceInfo("qwertyuiop", OUTPUT_DEVICE_STATUS_ERROR_INTERNAL,
|
| + AudioParameters(AudioParameters::AUDIO_FAKE,
|
| + CHANNEL_LAYOUT_NONE, 1, 2, 3));
|
| +}
|
| +
|
| TEST_P(AudioOutputDeviceTest, Initialize) {
|
| // Tests that the object can be constructed, initialized and destructed
|
| // without having ever been started.
|
| + SetDevice(kDefaultDeviceId);
|
| StopAudioDevice();
|
| }
|
|
|
| // Calls Start() followed by an immediate Stop() and check for the basic message
|
| // filter messages being sent in that case.
|
| TEST_P(AudioOutputDeviceTest, StartStop) {
|
| + SetDevice(kDefaultDeviceId);
|
| StartAudioDevice();
|
| StopAudioDevice();
|
| }
|
|
|
| -// AudioOutputDevice supports multiple start/stop sequences.
|
| +// AudioOutputDevice supports multiple intialize/start/stop sequences.
|
| TEST_P(AudioOutputDeviceTest, StartStopStartStop) {
|
| + SetDevice(kDefaultDeviceId);
|
| StartAudioDevice();
|
| StopAudioDevice();
|
| + InitializeAudioDevice();
|
| + EXPECT_CALL(*audio_output_ipc_,
|
| + RequestDeviceAuthorization(audio_device_.get(), 0, _, _));
|
| StartAudioDevice();
|
| + // Simulate reply from browser
|
| + ReceiveAuthorization(OUTPUT_DEVICE_STATUS_OK);
|
| StopAudioDevice();
|
| }
|
|
|
| // Simulate receiving OnStreamCreated() prior to processing ShutDownOnIOThread()
|
| // on the IO loop.
|
| TEST_P(AudioOutputDeviceTest, StopBeforeRender) {
|
| + SetDevice(kDefaultDeviceId);
|
| StartAudioDevice();
|
|
|
| // Call Stop() but don't run the IO loop yet.
|
| audio_device_->Stop();
|
|
|
| - // Expect us to shutdown IPC but not to render anything despite the stream
|
| - // getting created.
|
| + // Expect to play the stream and to shutdown IPC but not to render anything
|
| + // despite the stream getting created.
|
| + EXPECT_CALL(*audio_output_ipc_, PlayStream());
|
| EXPECT_CALL(*audio_output_ipc_, CloseStream());
|
| CreateStream();
|
| }
|
|
|
| // Full test with output only.
|
| TEST_P(AudioOutputDeviceTest, CreateStream) {
|
| + SetDevice(kDefaultDeviceId);
|
| StartAudioDevice();
|
| ExpectRenderCallback();
|
| CreateStream();
|
| @@ -302,13 +334,12 @@ TEST_P(AudioOutputDeviceTest, NonDefaultStartStopStartStop) {
|
| SetDevice(kNonDefaultDeviceId);
|
| StartAudioDevice();
|
| StopAudioDevice();
|
| -
|
| + InitializeAudioDevice();
|
| EXPECT_CALL(*audio_output_ipc_,
|
| RequestDeviceAuthorization(audio_device_.get(), 0, _, _));
|
| StartAudioDevice();
|
| // Simulate reply from browser
|
| ReceiveAuthorization(OUTPUT_DEVICE_STATUS_OK);
|
| -
|
| StopAudioDevice();
|
| }
|
|
|
| @@ -318,6 +349,106 @@ TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) {
|
| StopAudioDevice();
|
| }
|
|
|
| +TEST_P(AudioOutputDeviceTest, MultipleStartStopDifferentDevices) {
|
| + SetDevice(kDefaultDeviceId);
|
| + StartAudioDevice();
|
| + StopAudioDevice();
|
| +
|
| + SetDevice(kNonDefaultDeviceId);
|
| + StartAudioDevice();
|
| + StopAudioDevice();
|
| +
|
| + SetDevice(kUnauthorizedDeviceId);
|
| + StartAudioDevice();
|
| + StopAudioDevice();
|
| +
|
| + SetDevice(kDefaultDeviceId);
|
| + StartAudioDevice();
|
| + StopAudioDevice();
|
| +}
|
| +
|
| +// Test getting output device info.
|
| +// GetOutputDeviceInfo() must be called on a different thread than the IO task
|
| +// runner we hand to the AudioOutputDevice. Since we use the task runner for the
|
| +// thread the test runs on as the IO task runner, we must spin up a new thread
|
| +// and call GetOutputDeviceInfo() on that.
|
| +TEST_P(AudioOutputDeviceTest, GetOutputDeviceInfo) {
|
| + SetDevice(kDefaultDeviceId);
|
| +
|
| + base::WaitableEvent event(true, false);
|
| + base::Thread thread("get_output_device_info");
|
| + thread.Start();
|
| +
|
| + // Get device info on the other thread and wait until finished.
|
| + thread.task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputDeviceTest::GetOutputDeviceInfo,
|
| + base::Unretained(this), &event));
|
| + event.Wait();
|
| +
|
| + EXPECT_EQ(kDefaultDeviceId, device_info_.device_id());
|
| + EXPECT_EQ(OUTPUT_DEVICE_STATUS_OK, device_info_.device_status());
|
| + EXPECT_TRUE(device_info_.output_params().Equals(default_audio_parameters_));
|
| +
|
| + StopAudioDevice();
|
| +}
|
| +
|
| +TEST_P(AudioOutputDeviceTest, NonDefaultGetOutputDeviceInfo) {
|
| + SetDevice(kNonDefaultDeviceId);
|
| +
|
| + // AudioOutputDevice::GetOutputDeviceInfo must be called on a different thread
|
| + // than the task runner given to it.
|
| + base::WaitableEvent event(true, false);
|
| + base::Thread thread("get_output_device_info");
|
| + thread.Start();
|
| +
|
| + // Get device info on the other thread and wait until finished.
|
| + thread.task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputDeviceTest::GetOutputDeviceInfo,
|
| + base::Unretained(this), &event));
|
| + event.Wait();
|
| +
|
| + EXPECT_EQ(kNonDefaultDeviceId, device_info_.device_id());
|
| + EXPECT_EQ(OUTPUT_DEVICE_STATUS_OK, device_info_.device_status());
|
| + EXPECT_TRUE(device_info_.output_params().Equals(default_audio_parameters_));
|
| +
|
| + StopAudioDevice();
|
| +}
|
| +
|
| +TEST_P(AudioOutputDeviceTest, MultipleNonDefaultGetOutputDeviceInfo) {
|
| + SetDevice(kNonDefaultDeviceId);
|
| +
|
| + // AudioOutputDevice::GetOutputDeviceInfo must be called on a different thread
|
| + // than the task runner given to it.
|
| + base::WaitableEvent event(true, false);
|
| + base::Thread thread("get_output_device_info");
|
| + thread.Start();
|
| +
|
| + // Get device info on the other thread and wait until finished.
|
| + thread.task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputDeviceTest::GetOutputDeviceInfo,
|
| + base::Unretained(this), &event));
|
| + event.Wait();
|
| +
|
| + EXPECT_EQ(kNonDefaultDeviceId, device_info_.device_id());
|
| + EXPECT_EQ(OUTPUT_DEVICE_STATUS_OK, device_info_.device_status());
|
| + EXPECT_TRUE(device_info_.output_params().Equals(default_audio_parameters_));
|
| +
|
| + SetDeviceInfoToGarbage();
|
| + event.Reset();
|
| +
|
| + // Get device info on the other thread and wait until finished.
|
| + thread.task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputDeviceTest::GetOutputDeviceInfo,
|
| + base::Unretained(this), &event));
|
| + event.Wait();
|
| +
|
| + EXPECT_EQ(kNonDefaultDeviceId, device_info_.device_id());
|
| + EXPECT_EQ(OUTPUT_DEVICE_STATUS_OK, device_info_.device_status());
|
| + EXPECT_TRUE(device_info_.output_params().Equals(default_audio_parameters_));
|
| +
|
| + StopAudioDevice();
|
| +}
|
| +
|
| INSTANTIATE_TEST_CASE_P(Render, AudioOutputDeviceTest, Values(false));
|
|
|
| } // namespace media.
|
|
|