xref: /aosp_15_r20/external/webrtc/test/mock_audio_encoder_factory.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "api/make_ref_counted.h"
19 #include "api/scoped_refptr.h"
20 #include "test/gmock.h"
21 
22 namespace webrtc {
23 
24 class MockAudioEncoderFactory
25     : public ::testing::NiceMock<AudioEncoderFactory> {
26  public:
27   MOCK_METHOD(std::vector<AudioCodecSpec>,
28               GetSupportedEncoders,
29               (),
30               (override));
31   MOCK_METHOD(absl::optional<AudioCodecInfo>,
32               QueryAudioEncoder,
33               (const SdpAudioFormat& format),
34               (override));
35 
MakeAudioEncoder(int payload_type,const SdpAudioFormat & format,absl::optional<AudioCodecPairId> codec_pair_id)36   std::unique_ptr<AudioEncoder> MakeAudioEncoder(
37       int payload_type,
38       const SdpAudioFormat& format,
39       absl::optional<AudioCodecPairId> codec_pair_id) override {
40     std::unique_ptr<AudioEncoder> return_value;
41     MakeAudioEncoderMock(payload_type, format, codec_pair_id, &return_value);
42     return return_value;
43   }
44   MOCK_METHOD(void,
45               MakeAudioEncoderMock,
46               (int payload_type,
47                const SdpAudioFormat& format,
48                absl::optional<AudioCodecPairId> codec_pair_id,
49                std::unique_ptr<AudioEncoder>*));
50 
51   // Creates a MockAudioEncoderFactory with no formats and that may not be
52   // invoked to create a codec - useful for initializing a voice engine, for
53   // example.
54   static rtc::scoped_refptr<webrtc::MockAudioEncoderFactory>
CreateUnusedFactory()55   CreateUnusedFactory() {
56     using ::testing::_;
57     using ::testing::AnyNumber;
58     using ::testing::Return;
59 
60     auto factory = rtc::make_ref_counted<webrtc::MockAudioEncoderFactory>();
61     ON_CALL(*factory.get(), GetSupportedEncoders())
62         .WillByDefault(Return(std::vector<webrtc::AudioCodecSpec>()));
63     ON_CALL(*factory.get(), QueryAudioEncoder(_))
64         .WillByDefault(Return(absl::nullopt));
65 
66     EXPECT_CALL(*factory.get(), GetSupportedEncoders()).Times(AnyNumber());
67     EXPECT_CALL(*factory.get(), QueryAudioEncoder(_)).Times(AnyNumber());
68     EXPECT_CALL(*factory.get(), MakeAudioEncoderMock(_, _, _, _)).Times(0);
69     return factory;
70   }
71 
72   // Creates a MockAudioEncoderFactory with no formats that may be invoked to
73   // create a codec any number of times. It will, though, return nullptr on each
74   // call, since it supports no codecs.
75   static rtc::scoped_refptr<webrtc::MockAudioEncoderFactory>
CreateEmptyFactory()76   CreateEmptyFactory() {
77     using ::testing::_;
78     using ::testing::AnyNumber;
79     using ::testing::Return;
80     using ::testing::SetArgPointee;
81 
82     auto factory = rtc::make_ref_counted<webrtc::MockAudioEncoderFactory>();
83     ON_CALL(*factory.get(), GetSupportedEncoders())
84         .WillByDefault(Return(std::vector<webrtc::AudioCodecSpec>()));
85     ON_CALL(*factory.get(), QueryAudioEncoder(_))
86         .WillByDefault(Return(absl::nullopt));
87     ON_CALL(*factory.get(), MakeAudioEncoderMock(_, _, _, _))
88         .WillByDefault(SetArgPointee<3>(nullptr));
89 
90     EXPECT_CALL(*factory.get(), GetSupportedEncoders()).Times(AnyNumber());
91     EXPECT_CALL(*factory.get(), QueryAudioEncoder(_)).Times(AnyNumber());
92     EXPECT_CALL(*factory.get(), MakeAudioEncoderMock(_, _, _, _))
93         .Times(AnyNumber());
94     return factory;
95   }
96 };
97 
98 }  // namespace webrtc
99 
100 #endif  // TEST_MOCK_AUDIO_ENCODER_FACTORY_H_
101