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