1 /*
2  *  Copyright (c) 2016 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 
11 #ifndef TEST_MOCK_AUDIO_DECODER_FACTORY_H_
12 #define TEST_MOCK_AUDIO_DECODER_FACTORY_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "api/audio_codecs/audio_decoder_factory.h"
18 #include "api/audio_codecs/builtin_audio_decoder_factory.h"
19 #include "api/scoped_refptr.h"
20 #include "rtc_base/ref_counted_object.h"
21 #include "test/gmock.h"
22 
23 namespace webrtc {
24 
25 class MockAudioDecoderFactory : public AudioDecoderFactory {
26  public:
27   MOCK_METHOD(std::vector<AudioCodecSpec>,
28               GetSupportedDecoders,
29               (),
30               (override));
31   MOCK_METHOD(bool, IsSupportedDecoder, (const SdpAudioFormat&), (override));
MakeAudioDecoder(const SdpAudioFormat & format,absl::optional<AudioCodecPairId> codec_pair_id)32   std::unique_ptr<AudioDecoder> MakeAudioDecoder(
33       const SdpAudioFormat& format,
34       absl::optional<AudioCodecPairId> codec_pair_id) override {
35     std::unique_ptr<AudioDecoder> return_value;
36     MakeAudioDecoderMock(format, codec_pair_id, &return_value);
37     return return_value;
38   }
39   MOCK_METHOD(void,
40               MakeAudioDecoderMock,
41               (const SdpAudioFormat& format,
42                absl::optional<AudioCodecPairId> codec_pair_id,
43                std::unique_ptr<AudioDecoder>*));
44 
45   // Creates a MockAudioDecoderFactory with no formats and that may not be
46   // invoked to create a codec - useful for initializing a voice engine, for
47   // example.
48   static rtc::scoped_refptr<webrtc::MockAudioDecoderFactory>
CreateUnusedFactory()49   CreateUnusedFactory() {
50     using ::testing::_;
51     using ::testing::AnyNumber;
52     using ::testing::Return;
53 
54     rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> factory =
55         new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>;
56     ON_CALL(*factory.get(), GetSupportedDecoders())
57         .WillByDefault(Return(std::vector<webrtc::AudioCodecSpec>()));
58     EXPECT_CALL(*factory.get(), GetSupportedDecoders()).Times(AnyNumber());
59     ON_CALL(*factory, IsSupportedDecoder(_)).WillByDefault(Return(false));
60     EXPECT_CALL(*factory, IsSupportedDecoder(_)).Times(AnyNumber());
61     EXPECT_CALL(*factory.get(), MakeAudioDecoderMock(_, _, _)).Times(0);
62     return factory;
63   }
64 
65   // Creates a MockAudioDecoderFactory with no formats that may be invoked to
66   // create a codec any number of times. It will, though, return nullptr on each
67   // call, since it supports no codecs.
68   static rtc::scoped_refptr<webrtc::MockAudioDecoderFactory>
CreateEmptyFactory()69   CreateEmptyFactory() {
70     using ::testing::_;
71     using ::testing::AnyNumber;
72     using ::testing::Return;
73     using ::testing::SetArgPointee;
74 
75     rtc::scoped_refptr<webrtc::MockAudioDecoderFactory> factory =
76         new rtc::RefCountedObject<webrtc::MockAudioDecoderFactory>;
77     ON_CALL(*factory.get(), GetSupportedDecoders())
78         .WillByDefault(Return(std::vector<webrtc::AudioCodecSpec>()));
79     EXPECT_CALL(*factory.get(), GetSupportedDecoders()).Times(AnyNumber());
80     ON_CALL(*factory, IsSupportedDecoder(_)).WillByDefault(Return(false));
81     EXPECT_CALL(*factory, IsSupportedDecoder(_)).Times(AnyNumber());
82     ON_CALL(*factory.get(), MakeAudioDecoderMock(_, _, _))
83         .WillByDefault(SetArgPointee<2>(nullptr));
84     EXPECT_CALL(*factory.get(), MakeAudioDecoderMock(_, _, _))
85         .Times(AnyNumber());
86     return factory;
87   }
88 };
89 
90 }  // namespace webrtc
91 
92 #endif  // TEST_MOCK_AUDIO_DECODER_FACTORY_H_
93