blob: 67bc993c9bbab2776947206f6b0fbd67a630b6d1 [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_device.h"
17#include "modules/audio_device/include/mock_audio_transport.h"
18#include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
19#include "test/gmock.h"
20#include "test/mock_voe_channel_proxy.h"
21#include "voice_engine/voice_engine_impl.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010022
23namespace webrtc {
solenberg796b8f92017-03-01 17:02:23 -080024namespace voe {
25class TransmitMixer;
26} // namespace voe
27
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010028namespace test {
29
30// NOTE: This class inherits from VoiceEngineImpl so that its clients will be
31// able to get the various interfaces as usual, via T::GetInterface().
solenberg3a941542015-11-16 07:34:50 -080032class MockVoiceEngine : public VoiceEngineImpl {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010033 public:
nisseef8b61e2016-04-29 06:09:15 -070034 // TODO(nisse): Valid overrides commented out, because the gmock
35 // methods don't use any override declarations, and we want to avoid
36 // warnings from -Winconsistent-missing-override. See
37 // http://crbug.com/428099.
ossu29b1a8d2016-06-13 07:34:51 -070038 MockVoiceEngine(
39 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr)
solenberg88499ec2016-09-07 07:34:41 -070040 : decoder_factory_(decoder_factory) {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010041 // Increase ref count so this object isn't automatically deleted whenever
42 // interfaces are Release():d.
43 ++_ref_count;
solenberg13725082015-11-25 08:16:52 -080044 // We add this default behavior to make the mock easier to use in tests. It
45 // will create a NiceMock of a voe::ChannelProxy.
solenberg7602aab2016-11-14 11:30:07 -080046 // TODO(ossu): As long as AudioReceiveStream is implemented as a wrapper
ossu29b1a8d2016-06-13 07:34:51 -070047 // around Channel, we need to make sure ChannelProxy returns the same
48 // decoder factory as the one passed in when creating an AudioReceiveStream.
solenberg13725082015-11-25 08:16:52 -080049 ON_CALL(*this, ChannelProxyFactory(testing::_))
ossu29b1a8d2016-06-13 07:34:51 -070050 .WillByDefault(testing::Invoke([this](int channel_id) {
51 auto* proxy =
52 new testing::NiceMock<webrtc::test::MockVoEChannelProxy>();
53 EXPECT_CALL(*proxy, GetAudioDecoderFactory())
54 .WillRepeatedly(testing::ReturnRef(decoder_factory_));
kwiberg1c07c702017-03-27 07:15:49 -070055 EXPECT_CALL(*proxy, SetReceiveCodecs(testing::_))
56 .WillRepeatedly(testing::Invoke(
57 [](const std::map<int, SdpAudioFormat>& codecs) {
58 EXPECT_THAT(codecs, testing::IsEmpty());
59 }));
ossuc3d4b482017-05-23 06:07:11 -070060 EXPECT_CALL(*proxy, GetRtpRtcp(testing::_, testing::_))
61 .WillRepeatedly(
62 testing::SetArgPointee<0>(GetMockRtpRtcp(channel_id)));
ossu29b1a8d2016-06-13 07:34:51 -070063 return proxy;
64 }));
aleloidd310712016-11-17 06:28:59 -080065
66 ON_CALL(*this, audio_device_module())
67 .WillByDefault(testing::Return(&mock_audio_device_));
aleloidd310712016-11-17 06:28:59 -080068 ON_CALL(*this, audio_transport())
69 .WillByDefault(testing::Return(&mock_audio_transport_));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010070 }
solenberg7602aab2016-11-14 11:30:07 -080071 virtual ~MockVoiceEngine() /* override */ {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010072 // Decrease ref count before base class d-tor is called; otherwise it will
73 // trigger an assertion.
74 --_ref_count;
75 }
ossuc3d4b482017-05-23 06:07:11 -070076
77 // These need to be the same each call to channel_id and must not leak.
78 MockRtpRtcp* GetMockRtpRtcp(int channel_id) {
79 if (mock_rtp_rtcps_.find(channel_id) == mock_rtp_rtcps_.end()) {
80 mock_rtp_rtcps_[channel_id].reset(new ::testing::NiceMock<MockRtpRtcp>);
81 }
82 return mock_rtp_rtcps_[channel_id].get();
83 }
84
solenberg13725082015-11-25 08:16:52 -080085 // Allows injecting a ChannelProxy factory.
86 MOCK_METHOD1(ChannelProxyFactory, voe::ChannelProxy*(int channel_id));
87
88 // VoiceEngineImpl
solenberg7602aab2016-11-14 11:30:07 -080089 virtual std::unique_ptr<voe::ChannelProxy> GetChannelProxy(
nisseef8b61e2016-04-29 06:09:15 -070090 int channel_id) /* override */ {
kwibergb7f89d62016-02-17 10:04:18 -080091 return std::unique_ptr<voe::ChannelProxy>(ChannelProxyFactory(channel_id));
solenberg13725082015-11-25 08:16:52 -080092 }
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010093
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010094 // VoEBase
ossu5f7cfa52016-05-30 08:11:28 -070095 MOCK_METHOD3(
96 Init,
97 int(AudioDeviceModule* external_adm,
peaha9cc40b2017-06-29 08:32:09 -070098 AudioProcessing* external_apm,
ossu5f7cfa52016-05-30 08:11:28 -070099 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory));
aleloidd310712016-11-17 06:28:59 -0800100 MOCK_METHOD0(audio_device_module, AudioDeviceModule*());
solenberg796b8f92017-03-01 17:02:23 -0800101 MOCK_METHOD0(transmit_mixer, voe::TransmitMixer*());
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100102 MOCK_METHOD0(Terminate, int());
103 MOCK_METHOD0(CreateChannel, int());
solenberg88499ec2016-09-07 07:34:41 -0700104 MOCK_METHOD1(CreateChannel, int(const ChannelConfig& config));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100105 MOCK_METHOD1(DeleteChannel, int(int channel));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100106 MOCK_METHOD1(StartPlayout, int(int channel));
107 MOCK_METHOD1(StopPlayout, int(int channel));
108 MOCK_METHOD1(StartSend, int(int channel));
109 MOCK_METHOD1(StopSend, int(int channel));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100110 MOCK_METHOD0(audio_transport, AudioTransport*());
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100111
ossu29b1a8d2016-06-13 07:34:51 -0700112 private:
113 // TODO(ossu): I'm not particularly happy about keeping the decoder factory
114 // here, but due to how gmock is implemented, I cannot just keep it in the
115 // functor implementing the default version of ChannelProxyFactory, above.
116 // GMock creates an unfortunate copy of the functor, which would cause us to
117 // return a dangling reference. Fortunately, this should go away once
118 // voe::Channel does.
119 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
aleloidd310712016-11-17 06:28:59 -0800120
ossuc3d4b482017-05-23 06:07:11 -0700121 std::map<int, std::unique_ptr<MockRtpRtcp>> mock_rtp_rtcps_;
122
aleloidd310712016-11-17 06:28:59 -0800123 MockAudioDeviceModule mock_audio_device_;
aleloidd310712016-11-17 06:28:59 -0800124 MockAudioTransport mock_audio_transport_;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100125};
126} // namespace test
127} // namespace webrtc
128
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200129#endif // AUDIO_MOCK_VOICE_ENGINE_H_