Commit 3ead5290 authored by Antonio Gomes's avatar Antonio Gomes Committed by Commit Bot

Prepare webrtc_audio_renderer_unittest.cc for Onion souping

This CL is a preparation step to being able to move this file to Blink,
and then removing the whole directory.

Basically:

- it removes WebRtcAudioRendererTest inheritance to AudioDeviceFactory,
since it is used solely to override AudioDeviceFactory::CreateAudioRendererSink().

Instead of the explicit override, which is called by explicitly by
AudioDeviceFactoryTestingPlatformSupport::NewAudioRendererSink(), the CL just
creates the needed media::AudioRendererSink instance directly in
AudioDeviceFactoryTestingPlatformSupport::NewAudioRendererSink() and return it.

- the rest of the CL adapts expectation checks related to
WebRtcAudioRendererTest::MockCreateAudioRendererSink() with using
AudioDeviceFactoryTestingPlatformSupport::MockNewAudioRendererSink() and some
gtest tricks (eg testing::SaveArgs<>, and testing::InvokeWithoutArgs).
This way all original checks are kept!

R=guidou@chromium.org

Change-Id: If517cb2ea413359bcd80ca3e25e3d91b1e3a12ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1888555
Commit-Queue: Antonio Gomes <tonikitoo@igalia.com>
Reviewed-by: default avatarGuido Urdaneta <guidou@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712294}
parent 6db1185f
...@@ -31,8 +31,10 @@ ...@@ -31,8 +31,10 @@
#include "third_party/blink/renderer/platform/testing/testing_platform_support.h" #include "third_party/blink/renderer/platform/testing/testing_platform_support.h"
#include "third_party/webrtc/api/media_stream_interface.h" #include "third_party/webrtc/api/media_stream_interface.h"
using testing::Return;
using testing::_; using testing::_;
using testing::InvokeWithoutArgs;
using testing::Return;
using testing::SaveArg;
namespace content { namespace content {
...@@ -75,22 +77,40 @@ class AudioDeviceFactoryTestingPlatformSupport : public blink::Platform { ...@@ -75,22 +77,40 @@ class AudioDeviceFactoryTestingPlatformSupport : public blink::Platform {
blink::WebLocalFrame* web_frame, blink::WebLocalFrame* web_frame,
const media::AudioSinkParameters& params) override { const media::AudioSinkParameters& params) override {
MockNewAudioRendererSink(source_type, web_frame, params); MockNewAudioRendererSink(source_type, web_frame, params);
// The actual |web_frame| is irrelevant in this mock implementation,
// hence it is fine to use MSG_ROUTING_NONE mock_sink_ = new media::MockAudioRendererSink(
return AudioDeviceFactory::NewAudioRendererSink(source_type, params.device_id,
MSG_ROUTING_NONE, params); params.device_id == kInvalidOutputDeviceId
? media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL
: media::OUTPUT_DEVICE_STATUS_OK,
media::AudioParameters(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::CHANNEL_LAYOUT_STEREO,
kHardwareSampleRate, kHardwareBufferSize));
if (params.device_id != kInvalidOutputDeviceId) {
EXPECT_CALL(*mock_sink_.get(), Start());
EXPECT_CALL(*mock_sink_.get(), Play());
} else {
EXPECT_CALL(*mock_sink_.get(), Stop());
}
return mock_sink_;
} }
MOCK_METHOD3(MockNewAudioRendererSink, MOCK_METHOD3(MockNewAudioRendererSink,
void(blink::WebAudioDeviceSourceType, void(blink::WebAudioDeviceSourceType,
blink::WebLocalFrame*, blink::WebLocalFrame*,
const media::AudioSinkParameters&)); const media::AudioSinkParameters&));
media::MockAudioRendererSink* mock_sink() { return mock_sink_.get(); }
private:
scoped_refptr<media::MockAudioRendererSink> mock_sink_;
}; };
} // namespace } // namespace
class WebRtcAudioRendererTest : public testing::Test, class WebRtcAudioRendererTest : public testing::Test {
public AudioDeviceFactory {
public: public:
MOCK_METHOD1(MockSwitchDeviceCallback, void(media::OutputDeviceStatus)); MOCK_METHOD1(MockSwitchDeviceCallback, void(media::OutputDeviceStatus));
void SwitchDeviceCallback(base::RunLoop* loop, void SwitchDeviceCallback(base::RunLoop* loop,
...@@ -113,13 +133,16 @@ class WebRtcAudioRendererTest : public testing::Test, ...@@ -113,13 +133,16 @@ class WebRtcAudioRendererTest : public testing::Test,
blink::scheduler::GetSingleThreadTaskRunnerForTesting(), stream_, blink::scheduler::GetSingleThreadTaskRunnerForTesting(), stream_,
nullptr, base::UnguessableToken::Create(), device_id); nullptr, base::UnguessableToken::Create(), device_id);
media::AudioSinkParameters params;
EXPECT_CALL( EXPECT_CALL(
*audio_device_factory_platform_, *audio_device_factory_platform_,
MockNewAudioRendererSink(_, nullptr /*blink::WebLocalFrame*/, _)) MockNewAudioRendererSink(blink::WebAudioDeviceSourceType::kWebRtc,
.Times(testing::AtLeast(1)); nullptr /*blink::WebLocalFrame*/, _))
EXPECT_CALL(*this, MockCreateAudioRendererSink( .Times(testing::AtLeast(1))
blink::WebAudioDeviceSourceType::kWebRtc, _, _, .WillRepeatedly(DoAll(SaveArg<2>(&params), InvokeWithoutArgs([&]() {
device_id, _)); EXPECT_EQ(params.device_id, device_id);
})));
EXPECT_CALL(*source_.get(), SetOutputDeviceForAec(device_id)); EXPECT_CALL(*source_.get(), SetOutputDeviceForAec(device_id));
EXPECT_TRUE(renderer_->Initialize(source_.get())); EXPECT_TRUE(renderer_->Initialize(source_.get()));
...@@ -146,29 +169,8 @@ class WebRtcAudioRendererTest : public testing::Test, ...@@ -146,29 +169,8 @@ class WebRtcAudioRendererTest : public testing::Test,
const std::string&, const std::string&,
const base::Optional<base::UnguessableToken>&)); const base::Optional<base::UnguessableToken>&));
scoped_refptr<media::AudioRendererSink> CreateAudioRendererSink( media::MockAudioRendererSink* mock_sink() {
blink::WebAudioDeviceSourceType source_type, return audio_device_factory_platform_->mock_sink();
int render_frame_id,
const media::AudioSinkParameters& params) override {
mock_sink_ = new media::MockAudioRendererSink(
params.device_id,
params.device_id == kInvalidOutputDeviceId
? media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL
: media::OUTPUT_DEVICE_STATUS_OK,
media::AudioParameters(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::CHANNEL_LAYOUT_STEREO,
kHardwareSampleRate, kHardwareBufferSize));
if (params.device_id != kInvalidOutputDeviceId) {
EXPECT_CALL(*mock_sink_.get(), Start());
EXPECT_CALL(*mock_sink_.get(), Play());
} else {
EXPECT_CALL(*mock_sink_.get(), Stop());
}
MockCreateAudioRendererSink(source_type, render_frame_id, params.session_id,
params.device_id, params.processing_id);
return mock_sink_;
} }
void TearDown() override { void TearDown() override {
...@@ -176,7 +178,6 @@ class WebRtcAudioRendererTest : public testing::Test, ...@@ -176,7 +178,6 @@ class WebRtcAudioRendererTest : public testing::Test,
renderer_ = nullptr; renderer_ = nullptr;
stream_.Reset(); stream_.Reset();
source_.reset(); source_.reset();
mock_sink_ = nullptr;
blink::WebHeap::CollectAllGarbageForTesting(); blink::WebHeap::CollectAllGarbageForTesting();
} }
...@@ -186,7 +187,6 @@ class WebRtcAudioRendererTest : public testing::Test, ...@@ -186,7 +187,6 @@ class WebRtcAudioRendererTest : public testing::Test,
base::UnguessableToken::Create(); base::UnguessableToken::Create();
base::test::SingleThreadTaskEnvironment task_environment_{ base::test::SingleThreadTaskEnvironment task_environment_{
base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
scoped_refptr<media::MockAudioRendererSink> mock_sink_;
std::unique_ptr<MockAudioRendererSource> source_; std::unique_ptr<MockAudioRendererSource> source_;
blink::WebMediaStream stream_; blink::WebMediaStream stream_;
scoped_refptr<blink::WebRtcAudioRenderer> renderer_; scoped_refptr<blink::WebRtcAudioRenderer> renderer_;
...@@ -200,7 +200,7 @@ TEST_F(WebRtcAudioRendererTest, StopRenderer) { ...@@ -200,7 +200,7 @@ TEST_F(WebRtcAudioRendererTest, StopRenderer) {
// |renderer_| has only one proxy, stopping the proxy should stop the sink of // |renderer_| has only one proxy, stopping the proxy should stop the sink of
// |renderer_|. // |renderer_|.
EXPECT_CALL(*mock_sink_.get(), Stop()); EXPECT_CALL(*mock_sink(), Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
renderer_proxy_->Stop(); renderer_proxy_->Stop();
} }
...@@ -224,16 +224,16 @@ TEST_F(WebRtcAudioRendererTest, MultipleRenderers) { ...@@ -224,16 +224,16 @@ TEST_F(WebRtcAudioRendererTest, MultipleRenderers) {
// Stop the |renderer_proxy_| should not stop the sink since it is used by // Stop the |renderer_proxy_| should not stop the sink since it is used by
// other proxies. // other proxies.
EXPECT_CALL(*mock_sink_.get(), Stop()).Times(0); EXPECT_CALL(*mock_sink(), Stop()).Times(0);
renderer_proxy_->Stop(); renderer_proxy_->Stop();
for (int i = 0; i < kNumberOfRendererProxy; ++i) { for (int i = 0; i < kNumberOfRendererProxy; ++i) {
if (i != kNumberOfRendererProxy -1) { if (i != kNumberOfRendererProxy -1) {
EXPECT_CALL(*mock_sink_.get(), Stop()).Times(0); EXPECT_CALL(*mock_sink(), Stop()).Times(0);
} else { } else {
// When the last proxy is stopped, the sink will stop. // When the last proxy is stopped, the sink will stop.
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
EXPECT_CALL(*mock_sink_.get(), Stop()); EXPECT_CALL(*mock_sink(), Stop());
} }
renderer_proxies_[i]->Stop(); renderer_proxies_[i]->Stop();
} }
...@@ -257,7 +257,7 @@ TEST_F(WebRtcAudioRendererTest, VerifySinkParameters) { ...@@ -257,7 +257,7 @@ TEST_F(WebRtcAudioRendererTest, VerifySinkParameters) {
EXPECT_EQ(kHardwareSampleRate, renderer_->sample_rate()); EXPECT_EQ(kHardwareSampleRate, renderer_->sample_rate());
EXPECT_EQ(2, renderer_->channels()); EXPECT_EQ(2, renderer_->channels());
EXPECT_CALL(*mock_sink_.get(), Stop()); EXPECT_CALL(*mock_sink(), Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
renderer_proxy_->Stop(); renderer_proxy_->Stop();
} }
...@@ -265,19 +265,19 @@ TEST_F(WebRtcAudioRendererTest, VerifySinkParameters) { ...@@ -265,19 +265,19 @@ TEST_F(WebRtcAudioRendererTest, VerifySinkParameters) {
TEST_F(WebRtcAudioRendererTest, NonDefaultDevice) { TEST_F(WebRtcAudioRendererTest, NonDefaultDevice) {
SetupRenderer(kDefaultOutputDeviceId); SetupRenderer(kDefaultOutputDeviceId);
EXPECT_EQ(kDefaultOutputDeviceId, EXPECT_EQ(kDefaultOutputDeviceId,
mock_sink_->GetOutputDeviceInfo().device_id()); mock_sink()->GetOutputDeviceInfo().device_id());
renderer_proxy_->Start(); renderer_proxy_->Start();
EXPECT_CALL(*mock_sink_.get(), Stop()); EXPECT_CALL(*mock_sink(), Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
renderer_proxy_->Stop(); renderer_proxy_->Stop();
SetupRenderer(kOtherOutputDeviceId); SetupRenderer(kOtherOutputDeviceId);
EXPECT_EQ(kOtherOutputDeviceId, EXPECT_EQ(kOtherOutputDeviceId,
mock_sink_->GetOutputDeviceInfo().device_id()); mock_sink()->GetOutputDeviceInfo().device_id());
renderer_proxy_->Start(); renderer_proxy_->Start();
EXPECT_CALL(*mock_sink_.get(), Stop()); EXPECT_CALL(*mock_sink(), Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
renderer_proxy_->Stop(); renderer_proxy_->Stop();
} }
...@@ -285,13 +285,16 @@ TEST_F(WebRtcAudioRendererTest, NonDefaultDevice) { ...@@ -285,13 +285,16 @@ TEST_F(WebRtcAudioRendererTest, NonDefaultDevice) {
TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) { TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) {
SetupRenderer(kDefaultOutputDeviceId); SetupRenderer(kDefaultOutputDeviceId);
EXPECT_EQ(kDefaultOutputDeviceId, EXPECT_EQ(kDefaultOutputDeviceId,
mock_sink_->GetOutputDeviceInfo().device_id()); mock_sink()->GetOutputDeviceInfo().device_id());
renderer_proxy_->Start(); renderer_proxy_->Start();
EXPECT_CALL(*mock_sink_.get(), Stop()); EXPECT_CALL(*mock_sink(), Stop());
EXPECT_CALL(*this, MockCreateAudioRendererSink(
blink::WebAudioDeviceSourceType::kWebRtc, _, _, media::AudioSinkParameters params;
kOtherOutputDeviceId, kAudioProcessingId)); EXPECT_CALL(
*audio_device_factory_platform_,
MockNewAudioRendererSink(blink::WebAudioDeviceSourceType::kWebRtc, _, _))
.WillOnce(SaveArg<2>(&params));
EXPECT_CALL(*source_.get(), AudioRendererThreadStopped()); EXPECT_CALL(*source_.get(), AudioRendererThreadStopped());
EXPECT_CALL(*source_.get(), SetOutputDeviceForAec(kOtherOutputDeviceId)); EXPECT_CALL(*source_.get(), SetOutputDeviceForAec(kOtherOutputDeviceId));
EXPECT_CALL(*this, MockSwitchDeviceCallback(media::OUTPUT_DEVICE_STATUS_OK)); EXPECT_CALL(*this, MockSwitchDeviceCallback(media::OUTPUT_DEVICE_STATUS_OK));
...@@ -302,9 +305,13 @@ TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) { ...@@ -302,9 +305,13 @@ TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) {
base::Unretained(this), &loop)); base::Unretained(this), &loop));
loop.Run(); loop.Run();
EXPECT_EQ(kOtherOutputDeviceId, EXPECT_EQ(kOtherOutputDeviceId,
mock_sink_->GetOutputDeviceInfo().device_id()); mock_sink()->GetOutputDeviceInfo().device_id());
EXPECT_CALL(*mock_sink_.get(), Stop()); // blink::Platform::NewAudioRendererSink should have been called by now.
EXPECT_EQ(params.device_id, kOtherOutputDeviceId);
EXPECT_EQ(params.processing_id, kAudioProcessingId);
EXPECT_CALL(*mock_sink(), Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
renderer_proxy_->Stop(); renderer_proxy_->Stop();
} }
...@@ -312,13 +319,15 @@ TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) { ...@@ -312,13 +319,15 @@ TEST_F(WebRtcAudioRendererTest, SwitchOutputDevice) {
TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceInvalidDevice) { TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceInvalidDevice) {
SetupRenderer(kDefaultOutputDeviceId); SetupRenderer(kDefaultOutputDeviceId);
EXPECT_EQ(kDefaultOutputDeviceId, EXPECT_EQ(kDefaultOutputDeviceId,
mock_sink_->GetOutputDeviceInfo().device_id()); mock_sink()->GetOutputDeviceInfo().device_id());
auto original_sink = mock_sink_; auto* original_sink = mock_sink();
renderer_proxy_->Start(); renderer_proxy_->Start();
EXPECT_CALL(*this, MockCreateAudioRendererSink( media::AudioSinkParameters params;
blink::WebAudioDeviceSourceType::kWebRtc, _, _, EXPECT_CALL(
kInvalidOutputDeviceId, kAudioProcessingId)); *audio_device_factory_platform_,
MockNewAudioRendererSink(blink::WebAudioDeviceSourceType::kWebRtc, _, _))
.WillOnce(SaveArg<2>(&params));
EXPECT_CALL(*this, MockSwitchDeviceCallback( EXPECT_CALL(*this, MockSwitchDeviceCallback(
media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL)); media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL));
base::RunLoop loop; base::RunLoop loop;
...@@ -330,7 +339,11 @@ TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceInvalidDevice) { ...@@ -330,7 +339,11 @@ TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceInvalidDevice) {
EXPECT_EQ(kDefaultOutputDeviceId, EXPECT_EQ(kDefaultOutputDeviceId,
original_sink->GetOutputDeviceInfo().device_id()); original_sink->GetOutputDeviceInfo().device_id());
EXPECT_CALL(*original_sink.get(), Stop()); // blink::Platform::NewAudioRendererSink should have been called by now.
EXPECT_EQ(params.device_id, kInvalidOutputDeviceId);
EXPECT_EQ(params.processing_id, kAudioProcessingId);
EXPECT_CALL(*original_sink, Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
renderer_proxy_->Stop(); renderer_proxy_->Stop();
} }
...@@ -341,24 +354,30 @@ TEST_F(WebRtcAudioRendererTest, InitializeWithInvalidDevice) { ...@@ -341,24 +354,30 @@ TEST_F(WebRtcAudioRendererTest, InitializeWithInvalidDevice) {
nullptr /*blink::WebLocalFrame*/, base::UnguessableToken::Create(), nullptr /*blink::WebLocalFrame*/, base::UnguessableToken::Create(),
kInvalidOutputDeviceId); kInvalidOutputDeviceId);
EXPECT_CALL(*this, MockCreateAudioRendererSink( media::AudioSinkParameters params;
blink::WebAudioDeviceSourceType::kWebRtc, _, _, EXPECT_CALL(
kInvalidOutputDeviceId, kAudioProcessingId)); *audio_device_factory_platform_,
MockNewAudioRendererSink(blink::WebAudioDeviceSourceType::kWebRtc, _, _))
.WillOnce(SaveArg<2>(&params));
EXPECT_FALSE(renderer_->Initialize(source_.get())); EXPECT_FALSE(renderer_->Initialize(source_.get()));
// blink::Platform::NewAudioRendererSink should have been called by now.
EXPECT_EQ(params.device_id, kInvalidOutputDeviceId);
EXPECT_EQ(params.processing_id, kAudioProcessingId);
renderer_proxy_ = renderer_->CreateSharedAudioRendererProxy(stream_); renderer_proxy_ = renderer_->CreateSharedAudioRendererProxy(stream_);
EXPECT_EQ(kInvalidOutputDeviceId, EXPECT_EQ(kInvalidOutputDeviceId,
mock_sink_->GetOutputDeviceInfo().device_id()); mock_sink()->GetOutputDeviceInfo().device_id());
} }
TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceStoppedSource) { TEST_F(WebRtcAudioRendererTest, SwitchOutputDeviceStoppedSource) {
SetupRenderer(kDefaultOutputDeviceId); SetupRenderer(kDefaultOutputDeviceId);
auto original_sink = mock_sink_; auto* original_sink = mock_sink();
renderer_proxy_->Start(); renderer_proxy_->Start();
EXPECT_CALL(*original_sink.get(), Stop()); EXPECT_CALL(*original_sink, Stop());
EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get())); EXPECT_CALL(*source_.get(), RemoveAudioRenderer(renderer_.get()));
EXPECT_CALL(*this, MockSwitchDeviceCallback( EXPECT_CALL(*this, MockSwitchDeviceCallback(
media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL)); media::OUTPUT_DEVICE_STATUS_ERROR_INTERNAL));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment