Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef AUDIO_MOCK_VOICE_ENGINE_H_ |
| 12 | #define AUDIO_MOCK_VOICE_ENGINE_H_ |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 13 | |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #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 Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace 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(). |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 26 | class MockVoiceEngine : public VoiceEngineImpl { |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 27 | public: |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 28 | // 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. |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 32 | MockVoiceEngine( |
| 33 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory = nullptr) |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 34 | : decoder_factory_(decoder_factory) { |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 35 | // Increase ref count so this object isn't automatically deleted whenever |
| 36 | // interfaces are Release():d. |
| 37 | ++_ref_count; |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 38 | // We add this default behavior to make the mock easier to use in tests. It |
| 39 | // will create a NiceMock of a voe::ChannelProxy. |
solenberg | 7602aab | 2016-11-14 11:30:07 -0800 | [diff] [blame] | 40 | // TODO(ossu): As long as AudioReceiveStream is implemented as a wrapper |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 41 | // around Channel, we need to make sure ChannelProxy returns the same |
| 42 | // decoder factory as the one passed in when creating an AudioReceiveStream. |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 43 | ON_CALL(*this, ChannelProxyFactory(testing::_)) |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 44 | .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_)); |
kwiberg | 1c07c70 | 2017-03-27 07:15:49 -0700 | [diff] [blame] | 49 | EXPECT_CALL(*proxy, SetReceiveCodecs(testing::_)) |
| 50 | .WillRepeatedly(testing::Invoke( |
| 51 | [](const std::map<int, SdpAudioFormat>& codecs) { |
| 52 | EXPECT_THAT(codecs, testing::IsEmpty()); |
| 53 | })); |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 54 | EXPECT_CALL(*proxy, GetRtpRtcp(testing::_, testing::_)) |
| 55 | .WillRepeatedly( |
| 56 | testing::SetArgPointee<0>(GetMockRtpRtcp(channel_id))); |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 57 | return proxy; |
| 58 | })); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 59 | } |
solenberg | 7602aab | 2016-11-14 11:30:07 -0800 | [diff] [blame] | 60 | virtual ~MockVoiceEngine() /* override */ { |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 61 | // Decrease ref count before base class d-tor is called; otherwise it will |
| 62 | // trigger an assertion. |
| 63 | --_ref_count; |
| 64 | } |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 65 | |
| 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 | |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 74 | // Allows injecting a ChannelProxy factory. |
| 75 | MOCK_METHOD1(ChannelProxyFactory, voe::ChannelProxy*(int channel_id)); |
| 76 | |
| 77 | // VoiceEngineImpl |
solenberg | 7602aab | 2016-11-14 11:30:07 -0800 | [diff] [blame] | 78 | virtual std::unique_ptr<voe::ChannelProxy> GetChannelProxy( |
nisse | ef8b61e | 2016-04-29 06:09:15 -0700 | [diff] [blame] | 79 | int channel_id) /* override */ { |
kwiberg | b7f89d6 | 2016-02-17 10:04:18 -0800 | [diff] [blame] | 80 | return std::unique_ptr<voe::ChannelProxy>(ChannelProxyFactory(channel_id)); |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 81 | } |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 82 | |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 83 | // VoEBase |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 84 | MOCK_METHOD3( |
| 85 | Init, |
| 86 | int(AudioDeviceModule* external_adm, |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 87 | AudioProcessing* external_apm, |
ossu | 5f7cfa5 | 2016-05-30 08:11:28 -0700 | [diff] [blame] | 88 | const rtc::scoped_refptr<AudioDecoderFactory>& decoder_factory)); |
Fredrik Solenberg | 55900fd | 2017-11-23 20:22:55 +0100 | [diff] [blame] | 89 | MOCK_METHOD0(Terminate, void()); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 90 | MOCK_METHOD0(CreateChannel, int()); |
solenberg | 88499ec | 2016-09-07 07:34:41 -0700 | [diff] [blame] | 91 | MOCK_METHOD1(CreateChannel, int(const ChannelConfig& config)); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 92 | MOCK_METHOD1(DeleteChannel, int(int channel)); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 93 | |
ossu | 29b1a8d | 2016-06-13 07:34:51 -0700 | [diff] [blame] | 94 | 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_; |
aleloi | dd31071 | 2016-11-17 06:28:59 -0800 | [diff] [blame] | 102 | |
ossu | c3d4b48 | 2017-05-23 06:07:11 -0700 | [diff] [blame] | 103 | std::map<int, std::unique_ptr<MockRtpRtcp>> mock_rtp_rtcps_; |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 104 | }; |
| 105 | } // namespace test |
| 106 | } // namespace webrtc |
| 107 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 108 | #endif // AUDIO_MOCK_VOICE_ENGINE_H_ |