Chromium Code Reviews| 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..fe8dc53991fe5862f7c454248127c5236027d184 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,15 @@ 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); |
| protected: |
| // Used to clean up TLS pointers that the test(s) will initialize. |
| @@ -111,6 +115,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(); |
| @@ -156,8 +161,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 +184,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,6 +251,11 @@ void AudioOutputDeviceTest::StopAudioDevice() { |
| io_loop_.RunUntilIdle(); |
| } |
| +void AudioOutputDeviceTest::GetOutputDeviceInfo(base::WaitableEvent* event) { |
| + device_info_ = audio_device_->GetOutputDeviceInfo(); |
| + event->Signal(); |
| +} |
| + |
| TEST_P(AudioOutputDeviceTest, Initialize) { |
| // Tests that the object can be constructed, initialized and destructed |
| // without having ever been started. |
| @@ -260,6 +273,7 @@ TEST_P(AudioOutputDeviceTest, StartStop) { |
| TEST_P(AudioOutputDeviceTest, StartStopStartStop) { |
| StartAudioDevice(); |
| StopAudioDevice(); |
| + InitializeAudioDevice(); |
| StartAudioDevice(); |
| StopAudioDevice(); |
| } |
| @@ -272,8 +286,9 @@ TEST_P(AudioOutputDeviceTest, StopBeforeRender) { |
| // 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(); |
| } |
| @@ -289,6 +304,7 @@ TEST_P(AudioOutputDeviceTest, CreateStream) { |
| // Full test with output only with nondefault device. |
| TEST_P(AudioOutputDeviceTest, NonDefaultCreateStream) { |
| + StopAudioDevice(); |
|
o1ka
2016/05/03 15:47:32
Here and below: this does not look like a natural
Henrik Grunell
2016/05/04 09:05:12
Agree. A general note on this file is that I wasn'
|
| SetDevice(kNonDefaultDeviceId); |
| StartAudioDevice(); |
| ExpectRenderCallback(); |
| @@ -299,9 +315,11 @@ TEST_P(AudioOutputDeviceTest, NonDefaultCreateStream) { |
| // Multiple start/stop with nondefault device |
| TEST_P(AudioOutputDeviceTest, NonDefaultStartStopStartStop) { |
| + StopAudioDevice(); |
| SetDevice(kNonDefaultDeviceId); |
| StartAudioDevice(); |
| StopAudioDevice(); |
| + InitializeAudioDevice(); |
| EXPECT_CALL(*audio_output_ipc_, |
| RequestDeviceAuthorization(audio_device_.get(), 0, _, _)); |
| @@ -313,11 +331,91 @@ TEST_P(AudioOutputDeviceTest, NonDefaultStartStopStartStop) { |
| } |
| TEST_P(AudioOutputDeviceTest, UnauthorizedDevice) { |
| + StopAudioDevice(); |
| SetDevice(kUnauthorizedDeviceId); |
| StartAudioDevice(); |
| StopAudioDevice(); |
| } |
| +TEST_P(AudioOutputDeviceTest, GetOutputDeviceInfo) { |
| + // AudioOutputDevice::GetOutputDeviceInfo must be called on a different thread |
| + // than the task runner given to it. |
|
o1ka
2016/05/03 15:47:32
Comment is a bit unclear. GetOutputDeviceInfo must
Henrik Grunell
2016/05/04 09:05:12
Done.
|
| + base::WaitableEvent event(true, false); |
| + base::Thread thread("get_output_device_info"); |
| + thread.Start(); |
| + |
|
o1ka
2016/05/03 15:47:32
Probably you should initialize device_info_ with s
Henrik Grunell
2016/05/04 09:05:12
Good point. Done.
|
| + // 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) { |
| + StopAudioDevice(); |
| + 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) { |
| + StopAudioDevice(); |
| + 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_)); |
| + |
| + device_info_ = OutputDeviceInfo(); |
| + 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. |