blob: 57c6b58c67521fb1a1b3a4a9e7e4955a1f4e89f9 [file] [log] [blame]
Fredrik Solenberg0ccae132015-11-03 10:15:49 +01001/*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef AUDIO_MOCK_VOICE_ENGINE_H_
12#define AUDIO_MOCK_VOICE_ENGINE_H_
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010013
kwibergb7f89d62016-02-17 10:04:18 -080014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_device/include/mock_audio_transport.h"
17#include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
18#include "test/gmock.h"
19#include "test/mock_voe_channel_proxy.h"
20#include "voice_engine/voice_engine_impl.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010021
22namespace webrtc {
solenberg796b8f92017-03-01 17:02:23 -080023namespace voe {
24class TransmitMixer;
25} // namespace voe
26
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010027namespace test {
28
29// NOTE: This class inherits from VoiceEngineImpl so that its clients will be
30// able to get the various interfaces as usual, via T::GetInterface().
solenberg3a941542015-11-16 07:34:50 -080031class MockVoiceEngine : public VoiceEngineImpl {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010032 public:
nisseef8b61e2016-04-29 06:09:15 -070033 // TODO(nisse): Valid overrides commented out, because the gmock
34 // methods don't use any override declarations, and we want to avoid
35 // warnings from -Winconsistent-missing-override. See
36 // http://crbug.com/428099.
ossu29b1a8d2016-06-13 07:34:51 -070037 MockVoiceEngine(
38 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr)
solenberg88499ec2016-09-07 07:34:41 -070039 : decoder_factory_(decoder_factory) {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010040 // Increase ref count so this object isn't automatically deleted whenever
41 // interfaces are Release():d.
42 ++_ref_count;
solenberg13725082015-11-25 08:16:52 -080043 // We add this default behavior to make the mock easier to use in tests. It
44 // will create a NiceMock of a voe::ChannelProxy.
solenberg7602aab2016-11-14 11:30:07 -080045 // TODO(ossu): As long as AudioReceiveStream is implemented as a wrapper
ossu29b1a8d2016-06-13 07:34:51 -070046 // around Channel, we need to make sure ChannelProxy returns the same
47 // decoder factory as the one passed in when creating an AudioReceiveStream.
solenberg13725082015-11-25 08:16:52 -080048 ON_CALL(*this, ChannelProxyFactory(testing::_))
ossu29b1a8d2016-06-13 07:34:51 -070049 .WillByDefault(testing::Invoke([this](int channel_id) {
50 auto* proxy =
51 new testing::NiceMock<webrtc::test::MockVoEChannelProxy>();
52 EXPECT_CALL(*proxy, GetAudioDecoderFactory())
53 .WillRepeatedly(testing::ReturnRef(decoder_factory_));
kwiberg1c07c702017-03-27 07:15:49 -070054 EXPECT_CALL(*proxy, SetReceiveCodecs(testing::_))
55 .WillRepeatedly(testing::Invoke(
56 [](const std::map<int, SdpAudioFormat>& codecs) {
57 EXPECT_THAT(codecs, testing::IsEmpty());
58 }));
ossuc3d4b482017-05-23 06:07:11 -070059 EXPECT_CALL(*proxy, GetRtpRtcp(testing::_, testing::_))
60 .WillRepeatedly(
61 testing::SetArgPointee<0>(GetMockRtpRtcp(channel_id)));
ossu29b1a8d2016-06-13 07:34:51 -070062 return proxy;
63 }));
aleloidd310712016-11-17 06:28:59 -080064
aleloidd310712016-11-17 06:28:59 -080065 ON_CALL(*this, audio_transport())
66 .WillByDefault(testing::Return(&mock_audio_transport_));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010067 }
solenberg7602aab2016-11-14 11:30:07 -080068 virtual ~MockVoiceEngine() /* override */ {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010069 // Decrease ref count before base class d-tor is called; otherwise it will
70 // trigger an assertion.
71 --_ref_count;
72 }
ossuc3d4b482017-05-23 06:07:11 -070073
74 // These need to be the same each call to channel_id and must not leak.
75 MockRtpRtcp* GetMockRtpRtcp(int channel_id) {
76 if (mock_rtp_rtcps_.find(channel_id) == mock_rtp_rtcps_.end()) {
77 mock_rtp_rtcps_[channel_id].reset(new ::testing::NiceMock<MockRtpRtcp>);
78 }
79 return mock_rtp_rtcps_[channel_id].get();
80 }
81
solenberg13725082015-11-25 08:16:52 -080082 // Allows injecting a ChannelProxy factory.
83 MOCK_METHOD1(ChannelProxyFactory, voe::ChannelProxy*(int channel_id));
84
85 // VoiceEngineImpl
solenberg7602aab2016-11-14 11:30:07 -080086 virtual std::unique_ptr<voe::ChannelProxy> GetChannelProxy(
nisseef8b61e2016-04-29 06:09:15 -070087 int channel_id) /* override */ {
kwibergb7f89d62016-02-17 10:04:18 -080088 return std::unique_ptr<voe::ChannelProxy>(ChannelProxyFactory(channel_id));
solenberg13725082015-11-25 08:16:52 -080089 }
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010090
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010091 // VoEBase
ossu5f7cfa52016-05-30 08:11:28 -070092 MOCK_METHOD3(
93 Init,
94 int(AudioDeviceModule* external_adm,
peaha9cc40b2017-06-29 08:32:09 -070095 AudioProcessing* external_apm,
ossu5f7cfa52016-05-30 08:11:28 -070096 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory));
solenberg796b8f92017-03-01 17:02:23 -080097 MOCK_METHOD0(transmit_mixer, voe::TransmitMixer*());
Fredrik Solenberg55900fd2017-11-23 20:22:55 +010098 MOCK_METHOD0(Terminate, void());
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010099 MOCK_METHOD0(CreateChannel, int());
solenberg88499ec2016-09-07 07:34:41 -0700100 MOCK_METHOD1(CreateChannel, int(const ChannelConfig& config));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100101 MOCK_METHOD1(DeleteChannel, int(int channel));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100102 MOCK_METHOD1(StartPlayout, int(int channel));
103 MOCK_METHOD1(StopPlayout, int(int channel));
104 MOCK_METHOD1(StartSend, int(int channel));
105 MOCK_METHOD1(StopSend, int(int channel));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100106 MOCK_METHOD0(audio_transport, AudioTransport*());
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100107
ossu29b1a8d2016-06-13 07:34:51 -0700108 private:
109 // TODO(ossu): I'm not particularly happy about keeping the decoder factory
110 // here, but due to how gmock is implemented, I cannot just keep it in the
111 // functor implementing the default version of ChannelProxyFactory, above.
112 // GMock creates an unfortunate copy of the functor, which would cause us to
113 // return a dangling reference. Fortunately, this should go away once
114 // voe::Channel does.
115 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
aleloidd310712016-11-17 06:28:59 -0800116
ossuc3d4b482017-05-23 06:07:11 -0700117 std::map<int, std::unique_ptr<MockRtpRtcp>> mock_rtp_rtcps_;
118
aleloidd310712016-11-17 06:28:59 -0800119 MockAudioTransport mock_audio_transport_;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100120};
121} // namespace test
122} // namespace webrtc
123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // AUDIO_MOCK_VOICE_ENGINE_H_