From d7197080c086f1961a382cce3773e481334f4b6b Mon Sep 17 00:00:00 2001 From: Marina Ciocea Date: Mon, 4 May 2020 17:50:38 +0200 Subject: Add unit tests for audio channel send frame transformer delegate. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: webrtc:11380 Change-Id: I58a3983d3f16be8ed6a95ea2b9ce759bc3b3a7b7 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/174003 Commit-Queue: Marina Ciocea Reviewed-by: Per Ã…hgren Cr-Commit-Position: refs/heads/master@{#31159} --- audio/BUILD.gn | 1 + ...nel_send_frame_transformer_delegate_unittest.cc | 128 +++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 audio/channel_send_frame_transformer_delegate_unittest.cc (limited to 'audio') diff --git a/audio/BUILD.gn b/audio/BUILD.gn index 4f5e55b452..725128bb1a 100644 --- a/audio/BUILD.gn +++ b/audio/BUILD.gn @@ -128,6 +128,7 @@ if (rtc_include_tests) { "audio_send_stream_unittest.cc", "audio_state_unittest.cc", "channel_receive_frame_transformer_delegate_unittest.cc", + "channel_send_frame_transformer_delegate_unittest.cc", "mock_voe_channel_proxy.h", "remix_resample_unittest.cc", "test/audio_stats_test.cc", diff --git a/audio/channel_send_frame_transformer_delegate_unittest.cc b/audio/channel_send_frame_transformer_delegate_unittest.cc new file mode 100644 index 0000000000..e2f3647c0a --- /dev/null +++ b/audio/channel_send_frame_transformer_delegate_unittest.cc @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "audio/channel_send_frame_transformer_delegate.h" + +#include +#include + +#include "rtc_base/ref_counted_object.h" +#include "rtc_base/task_queue_for_test.h" +#include "test/gmock.h" +#include "test/gtest.h" +#include "test/mock_frame_transformer.h" +#include "test/mock_transformable_frame.h" + +namespace webrtc { +namespace { + +using ::testing::NiceMock; +using ::testing::SaveArg; + +class MockChannelSend { + public: + MockChannelSend() = default; + ~MockChannelSend() = default; + + MOCK_METHOD(int32_t, + SendFrame, + (AudioFrameType frameType, + uint8_t payloadType, + uint32_t rtp_timestamp, + rtc::ArrayView payload, + int64_t absolute_capture_timestamp_ms)); + + ChannelSendFrameTransformerDelegate::SendFrameCallback callback() { + return [this](AudioFrameType frameType, uint8_t payloadType, + uint32_t rtp_timestamp, rtc::ArrayView payload, + int64_t absolute_capture_timestamp_ms) { + return SendFrame(frameType, payloadType, rtp_timestamp, payload, + absolute_capture_timestamp_ms); + }; + } +}; + +// Test that the delegate registers itself with the frame transformer on Init(). +TEST(ChannelSendFrameTransformerDelegateTest, + RegisterTransformedFrameCallbackOnInit) { + rtc::scoped_refptr mock_frame_transformer = + new rtc::RefCountedObject(); + rtc::scoped_refptr delegate = + new rtc::RefCountedObject( + ChannelSendFrameTransformerDelegate::SendFrameCallback(), + mock_frame_transformer, nullptr); + EXPECT_CALL(*mock_frame_transformer, RegisterTransformedFrameCallback); + delegate->Init(); +} + +// Test that the delegate unregisters itself from the frame transformer on +// Reset(). +TEST(ChannelSendFrameTransformerDelegateTest, + UnregisterTransformedFrameCallbackOnReset) { + rtc::scoped_refptr mock_frame_transformer = + new rtc::RefCountedObject(); + rtc::scoped_refptr delegate = + new rtc::RefCountedObject( + ChannelSendFrameTransformerDelegate::SendFrameCallback(), + mock_frame_transformer, nullptr); + EXPECT_CALL(*mock_frame_transformer, UnregisterTransformedFrameCallback); + delegate->Reset(); +} + +// Test that when the delegate receives a transformed frame from the frame +// transformer, it passes it to the channel using the SendFrameCallback. +TEST(ChannelSendFrameTransformerDelegateTest, + TransformRunsChannelSendCallback) { + TaskQueueForTest channel_queue("channel_queue"); + rtc::scoped_refptr mock_frame_transformer = + new rtc::RefCountedObject>(); + MockChannelSend mock_channel; + rtc::scoped_refptr delegate = + new rtc::RefCountedObject( + mock_channel.callback(), mock_frame_transformer, &channel_queue); + rtc::scoped_refptr callback; + EXPECT_CALL(*mock_frame_transformer, RegisterTransformedFrameCallback) + .WillOnce(SaveArg<0>(&callback)); + delegate->Init(); + ASSERT_TRUE(callback); + + const uint8_t data[] = {1, 2, 3, 4}; + EXPECT_CALL(mock_channel, SendFrame); + ON_CALL(*mock_frame_transformer, Transform) + .WillByDefault( + [&callback](std::unique_ptr frame) { + callback->OnTransformedFrame(std::move(frame)); + }); + delegate->Transform(AudioFrameType::kEmptyFrame, 0, 0, 0, data, sizeof(data), + 0, 0); + channel_queue.WaitForPreviouslyPostedTasks(); +} + +// Test that if the delegate receives a transformed frame after it has been +// reset, it does not run the SendFrameCallback, as the channel is destroyed +// after resetting the delegate. +TEST(ChannelSendFrameTransformerDelegateTest, + OnTransformedDoesNotRunChannelSendCallbackAfterReset) { + TaskQueueForTest channel_queue("channel_queue"); + rtc::scoped_refptr mock_frame_transformer = + new rtc::RefCountedObject>(); + MockChannelSend mock_channel; + rtc::scoped_refptr delegate = + new rtc::RefCountedObject( + mock_channel.callback(), mock_frame_transformer, &channel_queue); + + delegate->Reset(); + EXPECT_CALL(mock_channel, SendFrame).Times(0); + delegate->OnTransformedFrame(std::make_unique()); + channel_queue.WaitForPreviouslyPostedTasks(); +} + +} // namespace +} // namespace webrtc -- cgit v1.2.3