Created a mocked AudioTransport.

There are currently two nearly identical classes called
MockAudioTransport defined in two unit tests:
android/audio_transport_unittest.cc and
/ios/audio_transport_unittest_ios.cc

This change defines a common mocked AudioTransport. The two current
mocks are rewritten to use the common one. A GN target is created for
this mock and MockAudioDevice.

This change will allow to provide a mocked AudioTransport to
AudioState in a dependent CL https://codereview.webrtc.org/2454373002/

BUG=webrtc:6346
NOPRESUBMIT=True

Review-Url: https://codereview.webrtc.org/2493483002
Cr-Commit-Position: refs/heads/master@{#15010}
This commit is contained in:
aleloi
2016-11-10 01:05:34 -08:00
committed by Commit bot
parent 37e4ad598d
commit 5de52fd38e
5 changed files with 119 additions and 104 deletions

View File

@ -335,6 +335,7 @@ if (rtc_include_tests) {
":rtc_unittest_main",
"../audio",
"../base:rtc_base_tests_utils",
"../modules/audio_device:mock_audio_device",
"../system_wrappers:metrics_default",
]
}

View File

@ -241,6 +241,28 @@ rtc_static_library("audio_device") {
}
}
if (rtc_include_tests) {
rtc_source_set("mock_audio_device") {
testonly = true
sources = [
"include/mock_audio_device.h",
"include/mock_audio_transport.h",
]
deps = [
":audio_device",
"../../test:test_support",
]
if (is_win) {
cflags = [
# TODO(phoglund): get rid of 4373 supression when
# http://code.google.com/p/webrtc/issues/detail?id=261 is solved.
# legacy warning for ignoring const / volatile in signatures.
"/wd4373",
]
}
}
}
# These tests do not work on ios, see
# https://bugs.chromium.org/p/webrtc/issues/detail?id=4755
if (rtc_include_tests && !is_ios) {

View File

@ -26,6 +26,7 @@
#include "webrtc/modules/audio_device/android/ensure_initialized.h"
#include "webrtc/modules/audio_device/audio_device_impl.h"
#include "webrtc/modules/audio_device/include/audio_device.h"
#include "webrtc/modules/audio_device/include/mock_audio_transport.h"
#include "webrtc/system_wrappers/include/clock.h"
#include "webrtc/system_wrappers/include/event_wrapper.h"
#include "webrtc/system_wrappers/include/sleep.h"
@ -367,55 +368,16 @@ class LatencyMeasuringAudioStream : public AudioStreamInterface {
// Mocks the AudioTransport object and proxies actions for the two callbacks
// (RecordedDataIsAvailable and NeedMorePlayData) to different implementations
// of AudioStreamInterface.
class MockAudioTransport : public AudioTransport {
class MockAudioTransportAndroid : public test::MockAudioTransport {
public:
explicit MockAudioTransport(int type)
explicit MockAudioTransportAndroid(int type)
: num_callbacks_(0),
type_(type),
play_count_(0),
rec_count_(0),
audio_stream_(nullptr) {}
virtual ~MockAudioTransport() {}
MOCK_METHOD10(RecordedDataIsAvailable,
int32_t(const void* audioSamples,
const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
const uint32_t totalDelayMS,
const int32_t clockDrift,
const uint32_t currentMicLevel,
const bool keyPressed,
uint32_t& newMicLevel));
MOCK_METHOD8(NeedMorePlayData,
int32_t(const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
void* audioSamples,
size_t& nSamplesOut,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms));
MOCK_METHOD6(PushCaptureData,
void(int voe_channel,
const void* audio_data,
int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames));
MOCK_METHOD7(PullRenderData,
void(int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames,
void* audio_data,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms));
virtual ~MockAudioTransportAndroid() {}
// Set default actions of the mock object. We are delegating to fake
// implementations (of AudioStreamInterface) here.
@ -428,12 +390,12 @@ class MockAudioTransport : public AudioTransport {
if (play_mode()) {
ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _))
.WillByDefault(
Invoke(this, &MockAudioTransport::RealNeedMorePlayData));
Invoke(this, &MockAudioTransportAndroid::RealNeedMorePlayData));
}
if (rec_mode()) {
ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _))
.WillByDefault(
Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable));
.WillByDefault(Invoke(
this, &MockAudioTransportAndroid::RealRecordedDataIsAvailable));
}
}
@ -899,7 +861,7 @@ TEST_F(AudioDeviceTest, StopRecordingRequiresInitToRestart) {
// Start playout and verify that the native audio layer starts asking for real
// audio samples to play out using the NeedMorePlayData callback.
TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
MockAudioTransport mock(kPlayout);
MockAudioTransportAndroid mock(kPlayout);
mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
kBytesPerSample,
@ -917,7 +879,7 @@ TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
// Start recording and verify that the native audio layer starts feeding real
// audio samples via the RecordedDataIsAvailable callback.
TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
MockAudioTransport mock(kRecording);
MockAudioTransportAndroid mock(kRecording);
mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(mock, RecordedDataIsAvailable(NotNull(),
record_frames_per_10ms_buffer(),
@ -941,7 +903,7 @@ TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
// Start playout and recording (full-duplex audio) and verify that audio is
// active in both directions.
TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
MockAudioTransport mock(kPlayout | kRecording);
MockAudioTransportAndroid mock(kPlayout | kRecording);
mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
kBytesPerSample,
@ -975,7 +937,7 @@ TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
// TODO(henrika): extend test when mono output is supported.
EXPECT_EQ(1u, playout_channels());
NiceMock<MockAudioTransport> mock(kPlayout);
NiceMock<MockAudioTransportAndroid> mock(kPlayout);
const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond;
std::string file_name = GetFileName(playout_sample_rate());
std::unique_ptr<FileAudioStream> file_audio_stream(
@ -1006,7 +968,7 @@ TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) {
EXPECT_EQ(record_channels(), playout_channels());
EXPECT_EQ(record_sample_rate(), playout_sample_rate());
NiceMock<MockAudioTransport> mock(kPlayout | kRecording);
NiceMock<MockAudioTransportAndroid> mock(kPlayout | kRecording);
std::unique_ptr<FifoAudioStream> fifo_audio_stream(
new FifoAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(test_is_done_.get(),
@ -1040,7 +1002,7 @@ TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) {
TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) {
EXPECT_EQ(record_channels(), playout_channels());
EXPECT_EQ(record_sample_rate(), playout_sample_rate());
NiceMock<MockAudioTransport> mock(kPlayout | kRecording);
NiceMock<MockAudioTransportAndroid> mock(kPlayout | kRecording);
std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream(
new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(test_is_done_.get(),

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2016 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.
*/
#ifndef WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_MOCK_AUDIO_TRANSPORT_H_
#define WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_MOCK_AUDIO_TRANSPORT_H_
#include "webrtc/modules/audio_device/include/audio_device_defines.h"
#include "webrtc/test/gmock.h"
namespace webrtc {
namespace test {
class MockAudioTransport : public AudioTransport {
public:
MockAudioTransport() {}
~MockAudioTransport() {}
MOCK_METHOD10(RecordedDataIsAvailable,
int32_t(const void* audioSamples,
const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
const uint32_t totalDelayMS,
const int32_t clockDrift,
const uint32_t currentMicLevel,
const bool keyPressed,
uint32_t& newMicLevel));
MOCK_METHOD8(NeedMorePlayData,
int32_t(const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
void* audioSamples,
size_t& nSamplesOut,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms));
MOCK_METHOD6(PushCaptureData,
void(int voe_channel,
const void* audio_data,
int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames));
MOCK_METHOD7(PullRenderData,
void(int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames,
void* audio_data,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms));
};
} // namespace test
} // namespace webrtc
#endif // WEBRTC_MODULES_AUDIO_DEVICE_INCLUDE_MOCK_AUDIO_TRANSPORT_H_

View File

@ -23,6 +23,7 @@
#include "webrtc/base/scoped_ref_ptr.h"
#include "webrtc/modules/audio_device/audio_device_impl.h"
#include "webrtc/modules/audio_device/include/audio_device.h"
#include "webrtc/modules/audio_device/include/mock_audio_transport.h"
#include "webrtc/modules/audio_device/ios/audio_device_ios.h"
#include "webrtc/system_wrappers/include/clock.h"
#include "webrtc/system_wrappers/include/event_wrapper.h"
@ -358,55 +359,16 @@ class LatencyMeasuringAudioStream : public AudioStreamInterface {
// Mocks the AudioTransport object and proxies actions for the two callbacks
// (RecordedDataIsAvailable and NeedMorePlayData) to different implementations
// of AudioStreamInterface.
class MockAudioTransport : public AudioTransport {
class MockAudioTransportIOS : public test::MockAudioTransport {
public:
explicit MockAudioTransport(int type)
explicit MockAudioTransportIOS(int type)
: num_callbacks_(0),
type_(type),
play_count_(0),
rec_count_(0),
audio_stream_(nullptr) {}
virtual ~MockAudioTransport() {}
MOCK_METHOD10(RecordedDataIsAvailable,
int32_t(const void* audioSamples,
const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
const uint32_t totalDelayMS,
const int32_t clockDrift,
const uint32_t currentMicLevel,
const bool keyPressed,
uint32_t& newMicLevel));
MOCK_METHOD8(NeedMorePlayData,
int32_t(const size_t nSamples,
const size_t nBytesPerSample,
const size_t nChannels,
const uint32_t samplesPerSec,
void* audioSamples,
size_t& nSamplesOut,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms));
MOCK_METHOD6(PushCaptureData,
void(int voe_channel,
const void* audio_data,
int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames));
MOCK_METHOD7(PullRenderData,
void(int bits_per_sample,
int sample_rate,
size_t number_of_channels,
size_t number_of_frames,
void* audio_data,
int64_t* elapsed_time_ms,
int64_t* ntp_time_ms));
virtual ~MockAudioTransportIOS() {}
// Set default actions of the mock object. We are delegating to fake
// implementations (of AudioStreamInterface) here.
@ -419,12 +381,12 @@ class MockAudioTransport : public AudioTransport {
if (play_mode()) {
ON_CALL(*this, NeedMorePlayData(_, _, _, _, _, _, _, _))
.WillByDefault(
Invoke(this, &MockAudioTransport::RealNeedMorePlayData));
Invoke(this, &MockAudioTransportIOS::RealNeedMorePlayData));
}
if (rec_mode()) {
ON_CALL(*this, RecordedDataIsAvailable(_, _, _, _, _, _, _, _, _, _))
.WillByDefault(
Invoke(this, &MockAudioTransport::RealRecordedDataIsAvailable));
.WillByDefault(Invoke(
this, &MockAudioTransportIOS::RealRecordedDataIsAvailable));
}
}
@ -671,7 +633,7 @@ TEST_F(AudioDeviceTest, StartPlayoutOnTwoInstances) {
// Start playout for the default ADM but don't wait here. Instead use the
// upcoming second stream for that. We set the same expectation on number
// of callbacks as for the second stream.
NiceMock<MockAudioTransport> mock(kPlayout);
NiceMock<MockAudioTransportIOS> mock(kPlayout);
mock.HandleCallbacks(nullptr, nullptr, 0);
EXPECT_CALL(
mock, NeedMorePlayData(playout_frames_per_10ms_buffer(), kBytesPerSample,
@ -693,7 +655,7 @@ TEST_F(AudioDeviceTest, StartPlayoutOnTwoInstances) {
// Passing this test ensures that initialization of the second audio unit
// has been done successfully and that there is no conflict with the already
// playing first ADM.
MockAudioTransport mock2(kPlayout);
MockAudioTransportIOS mock2(kPlayout);
mock2.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(
mock2, NeedMorePlayData(playout_frames_per_10ms_buffer(), kBytesPerSample,
@ -714,7 +676,7 @@ TEST_F(AudioDeviceTest, StartPlayoutOnTwoInstances) {
// Start playout and verify that the native audio layer starts asking for real
// audio samples to play out using the NeedMorePlayData callback.
TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
MockAudioTransport mock(kPlayout);
MockAudioTransportIOS mock(kPlayout);
mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
kBytesPerSample, playout_channels(),
@ -729,7 +691,7 @@ TEST_F(AudioDeviceTest, StartPlayoutVerifyCallbacks) {
// Start recording and verify that the native audio layer starts feeding real
// audio samples via the RecordedDataIsAvailable callback.
TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
MockAudioTransport mock(kRecording);
MockAudioTransportIOS mock(kRecording);
mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(mock,
RecordedDataIsAvailable(
@ -747,7 +709,7 @@ TEST_F(AudioDeviceTest, StartRecordingVerifyCallbacks) {
// Start playout and recording (full-duplex audio) and verify that audio is
// active in both directions.
TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
MockAudioTransport mock(kPlayout | kRecording);
MockAudioTransportIOS mock(kPlayout | kRecording);
mock.HandleCallbacks(test_is_done_.get(), nullptr, kNumCallbacks);
EXPECT_CALL(mock, NeedMorePlayData(playout_frames_per_10ms_buffer(),
kBytesPerSample, playout_channels(),
@ -773,7 +735,7 @@ TEST_F(AudioDeviceTest, StartPlayoutAndRecordingVerifyCallbacks) {
TEST_F(AudioDeviceTest, RunPlayoutWithFileAsSource) {
// TODO(henrika): extend test when mono output is supported.
EXPECT_EQ(1, playout_channels());
NiceMock<MockAudioTransport> mock(kPlayout);
NiceMock<MockAudioTransportIOS> mock(kPlayout);
const int num_callbacks = kFilePlayTimeInSec * kNumCallbacksPerSecond;
std::string file_name = GetFileName(playout_sample_rate());
std::unique_ptr<FileAudioStream> file_audio_stream(
@ -809,7 +771,7 @@ TEST_F(AudioDeviceTest, Devices) {
TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) {
EXPECT_EQ(record_channels(), playout_channels());
EXPECT_EQ(record_sample_rate(), playout_sample_rate());
NiceMock<MockAudioTransport> mock(kPlayout | kRecording);
NiceMock<MockAudioTransportIOS> mock(kPlayout | kRecording);
std::unique_ptr<FifoAudioStream> fifo_audio_stream(
new FifoAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(test_is_done_.get(), fifo_audio_stream.get(),
@ -838,7 +800,7 @@ TEST_F(AudioDeviceTest, RunPlayoutAndRecordingInFullDuplex) {
TEST_F(AudioDeviceTest, DISABLED_MeasureLoopbackLatency) {
EXPECT_EQ(record_channels(), playout_channels());
EXPECT_EQ(record_sample_rate(), playout_sample_rate());
NiceMock<MockAudioTransport> mock(kPlayout | kRecording);
NiceMock<MockAudioTransportIOS> mock(kPlayout | kRecording);
std::unique_ptr<LatencyMeasuringAudioStream> latency_audio_stream(
new LatencyMeasuringAudioStream(playout_frames_per_10ms_buffer()));
mock.HandleCallbacks(test_is_done_.get(), latency_audio_stream.get(),