blob: 0bbf5ee8f0b812bab500968058beaa29bac74139 [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/rtp_rtcp/mocks/mock_rtp_rtcp.h"
17#include "test/gmock.h"
18#include "test/mock_voe_channel_proxy.h"
19#include "voice_engine/voice_engine_impl.h"
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010020
21namespace webrtc {
22namespace test {
23
24// NOTE: This class inherits from VoiceEngineImpl so that its clients will be
25// able to get the various interfaces as usual, via T::GetInterface().
solenberg3a941542015-11-16 07:34:50 -080026class MockVoiceEngine : public VoiceEngineImpl {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010027 public:
nisseef8b61e2016-04-29 06:09:15 -070028 // TODO(nisse): Valid overrides commented out, because the gmock
29 // methods don't use any override declarations, and we want to avoid
30 // warnings from -Winconsistent-missing-override. See
31 // http://crbug.com/428099.
ossu29b1a8d2016-06-13 07:34:51 -070032 MockVoiceEngine(
33 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr)
solenberg88499ec2016-09-07 07:34:41 -070034 : decoder_factory_(decoder_factory) {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010035 // Increase ref count so this object isn't automatically deleted whenever
36 // interfaces are Release():d.
37 ++_ref_count;
solenberg13725082015-11-25 08:16:52 -080038 // We add this default behavior to make the mock easier to use in tests. It
39 // will create a NiceMock of a voe::ChannelProxy.
solenberg7602aab2016-11-14 11:30:07 -080040 // TODO(ossu): As long as AudioReceiveStream is implemented as a wrapper
ossu29b1a8d2016-06-13 07:34:51 -070041 // around Channel, we need to make sure ChannelProxy returns the same
42 // decoder factory as the one passed in when creating an AudioReceiveStream.
solenberg13725082015-11-25 08:16:52 -080043 ON_CALL(*this, ChannelProxyFactory(testing::_))
ossu29b1a8d2016-06-13 07:34:51 -070044 .WillByDefault(testing::Invoke([this](int channel_id) {
45 auto* proxy =
46 new testing::NiceMock<webrtc::test::MockVoEChannelProxy>();
47 EXPECT_CALL(*proxy, GetAudioDecoderFactory())
48 .WillRepeatedly(testing::ReturnRef(decoder_factory_));
kwiberg1c07c702017-03-27 07:15:49 -070049 EXPECT_CALL(*proxy, SetReceiveCodecs(testing::_))
50 .WillRepeatedly(testing::Invoke(
51 [](const std::map<int, SdpAudioFormat>& codecs) {
52 EXPECT_THAT(codecs, testing::IsEmpty());
53 }));
ossuc3d4b482017-05-23 06:07:11 -070054 EXPECT_CALL(*proxy, GetRtpRtcp(testing::_, testing::_))
55 .WillRepeatedly(
56 testing::SetArgPointee<0>(GetMockRtpRtcp(channel_id)));
ossu29b1a8d2016-06-13 07:34:51 -070057 return proxy;
58 }));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010059 }
solenberg7602aab2016-11-14 11:30:07 -080060 virtual ~MockVoiceEngine() /* override */ {
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010061 // Decrease ref count before base class d-tor is called; otherwise it will
62 // trigger an assertion.
63 --_ref_count;
64 }
ossuc3d4b482017-05-23 06:07:11 -070065
66 // These need to be the same each call to channel_id and must not leak.
67 MockRtpRtcp* GetMockRtpRtcp(int channel_id) {
68 if (mock_rtp_rtcps_.find(channel_id) == mock_rtp_rtcps_.end()) {
69 mock_rtp_rtcps_[channel_id].reset(new ::testing::NiceMock<MockRtpRtcp>);
70 }
71 return mock_rtp_rtcps_[channel_id].get();
72 }
73
solenberg13725082015-11-25 08:16:52 -080074 // Allows injecting a ChannelProxy factory.
75 MOCK_METHOD1(ChannelProxyFactory, voe::ChannelProxy*(int channel_id));
76
77 // VoiceEngineImpl
solenberg7602aab2016-11-14 11:30:07 -080078 virtual std::unique_ptr<voe::ChannelProxy> GetChannelProxy(
nisseef8b61e2016-04-29 06:09:15 -070079 int channel_id) /* override */ {
kwibergb7f89d62016-02-17 10:04:18 -080080 return std::unique_ptr<voe::ChannelProxy>(ChannelProxyFactory(channel_id));
solenberg13725082015-11-25 08:16:52 -080081 }
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010082
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010083 // VoEBase
ossu5f7cfa52016-05-30 08:11:28 -070084 MOCK_METHOD3(
85 Init,
86 int(AudioDeviceModule* external_adm,
peaha9cc40b2017-06-29 08:32:09 -070087 AudioProcessing* external_apm,
ossu5f7cfa52016-05-30 08:11:28 -070088 const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory));
Fredrik Solenberg55900fd2017-11-23 20:22:55 +010089 MOCK_METHOD0(Terminate, void());
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010090 MOCK_METHOD0(CreateChannel, int());
solenberg88499ec2016-09-07 07:34:41 -070091 MOCK_METHOD1(CreateChannel, int(const ChannelConfig& config));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010092 MOCK_METHOD1(DeleteChannel, int(int channel));
Fredrik Solenberg0ccae132015-11-03 10:15:49 +010093
ossu29b1a8d2016-06-13 07:34:51 -070094 private:
95 // TODO(ossu): I'm not particularly happy about keeping the decoder factory
96 // here, but due to how gmock is implemented, I cannot just keep it in the
97 // functor implementing the default version of ChannelProxyFactory, above.
98 // GMock creates an unfortunate copy of the functor, which would cause us to
99 // return a dangling reference. Fortunately, this should go away once
100 // voe::Channel does.
101 rtc::scoped_refptr<AudioDecoderFactory> decoder_factory_;
aleloidd310712016-11-17 06:28:59 -0800102
ossuc3d4b482017-05-23 06:07:11 -0700103 std::map<int, std::unique_ptr<MockRtpRtcp>> mock_rtp_rtcps_;
Fredrik Solenberg0ccae132015-11-03 10:15:49 +0100104};
105} // namespace test
106} // namespace webrtc
107
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200108#endif // AUDIO_MOCK_VOICE_ENGINE_H_