1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gtest/gtest.h"
17 #include <gmock/gmock.h>
18 #define private public
19 #include "avcodec_video_decoder_impl.h"
20 #include "media_codec_decoder_adapter_impl.h"
21 #undef private
22 #include "avsharedmemory.h"
23 #include "avsharedmemorybase.h"
24 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/window.h"
25 #include "native_window.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 using testing::ext::TestSize;
31 using namespace OHOS::MediaAVCodec;
32 
33 namespace OHOS {
34 namespace MediaAVCodec {
35 class AVCodecVideoDecoderImplMock : public AVCodecVideoDecoderImpl {
36 public:
37     MOCK_METHOD0(Prepare, int32_t());
38 };
39 }
40 namespace NWeb {
41 class DecoderCallbackImplTest : public testing::Test {};
42 
43 class DecoderCallbackAdapterMock : public DecoderCallbackAdapter {
44 public:
45     DecoderCallbackAdapterMock() = default;
46 
47     ~DecoderCallbackAdapterMock() override = default;
48 
OnError(ErrorType errorType,int32_t errorCode)49     void OnError(ErrorType errorType, int32_t errorCode) override {}
50 
OnStreamChanged(int32_t width,int32_t height,double frameRate)51     void OnStreamChanged(int32_t width, int32_t height, double frameRate) override {}
52 
OnNeedInputData(uint32_t index,std::shared_ptr<OhosBufferAdapter> buffer)53     void OnNeedInputData(uint32_t index, std::shared_ptr<OhosBufferAdapter> buffer) override {}
54 
OnNeedOutputData(uint32_t index,std::shared_ptr<BufferInfoAdapter> info,BufferFlag flag)55     void OnNeedOutputData(uint32_t index, std::shared_ptr<BufferInfoAdapter> info, BufferFlag flag) override {}
56 };
57 
58 class DecoderFormatAdapterMock : public DecoderFormatAdapter {
59 public:
60     DecoderFormatAdapterMock() = default;
61 
GetWidth()62     int32_t GetWidth() override
63     {
64         return width;
65     }
66 
GetHeight()67     int32_t GetHeight() override
68     {
69         return height;
70     }
71 
GetFrameRate()72     double GetFrameRate() override
73     {
74         return frameRate;
75     }
76 
SetWidth(int32_t w)77     void SetWidth(int32_t w) override
78     {
79         width = w;
80     }
81 
SetHeight(int32_t h)82     void SetHeight(int32_t h) override
83     {
84         height = h;
85     }
86 
SetFrameRate(double fr)87     void SetFrameRate(double fr) override
88     {
89         frameRate = fr;
90     }
91 
92     int32_t width;
93     int32_t height;
94     double frameRate;
95 };
96 
97 /**
98  * @tc.name: DecoderCallbackImpl_NormalTest_001.
99  * @tc.desc: test of DecoderCallbackImpl::OnError() OnOutputFormatChanged() OnInputBufferAvailable()
100  * @tc.type: FUNC.
101  * @tc.require:
102  */
103 HWTEST_F(DecoderCallbackImplTest, DecoderCallbackImpl_NormalTest_001, TestSize.Level1)
104 {
105     std::shared_ptr<DecoderCallbackAdapter> cb_ = nullptr;
106     std::shared_ptr<DecoderCallbackImpl> decoderCallbackImpl_ = std::make_shared<DecoderCallbackImpl>(cb_);
107     const int32_t errorcode_ = 0;
108     const AVCodecErrorType errorType_ = AVCODEC_ERROR_EXTEND_START;
109     decoderCallbackImpl_->OnError(errorType_, errorcode_);
110     Media::Format fomat_;
111     decoderCallbackImpl_->OnOutputFormatChanged(fomat_);
112     uint32_t index_ = 1;
113     std::shared_ptr<Media::AVSharedMemory> buffer_ = nullptr;
114     decoderCallbackImpl_->OnInputBufferAvailable(index_, buffer_);
115     AVCodecBufferInfo info;
116     decoderCallbackImpl_->OnOutputBufferAvailable(1, info, AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS, nullptr);
117 }
118 
119 class MediaCodecDecoderAdapterImplTest : public testing::Test {
120 public:
121     static void SetUpTestCase(void);
122     static void TearDownTestCase(void);
123     void SetUp();
124     void TearDown();
125 
126 protected:
127     std::shared_ptr<DecoderFormatAdapterMock> format_ = nullptr;
128     std::shared_ptr<MediaCodecDecoderAdapterImpl> mediaCodecDecoderAdapterImpl_ = nullptr;
129 };
130 
SetUpTestCase(void)131 void MediaCodecDecoderAdapterImplTest::SetUpTestCase(void) {}
132 
TearDownTestCase(void)133 void MediaCodecDecoderAdapterImplTest::TearDownTestCase(void) {}
134 
SetUp()135 void MediaCodecDecoderAdapterImplTest::SetUp()
136 {
137     EXPECT_EQ(mediaCodecDecoderAdapterImpl_, nullptr);
138     mediaCodecDecoderAdapterImpl_ = std::make_unique<MediaCodecDecoderAdapterImpl>();
139     EXPECT_NE(mediaCodecDecoderAdapterImpl_, nullptr);
140     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->decoder_, nullptr);
141     EXPECT_EQ(format_, nullptr);
142     format_ = std::make_unique<DecoderFormatAdapterMock>();
143     EXPECT_NE(format_, nullptr);
144     format_->width = 200;
145     format_->height = 300;
146     format_->frameRate = 20;
147 }
148 
TearDown(void)149 void MediaCodecDecoderAdapterImplTest::TearDown(void) {}
150 
151 /**
152  * @tc.name: MediaCodecDecoderAdapterImpl_CreateVideoDecoderByName_001.
153  * @tc.desc: test of MediaCodecDecoderAdapterImpl::CreateVideoDecoderByName() CreateVideoDecoderByName()
154  * @tc.type: FUNC.
155  * @tc.require:
156  */
157 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_CreateVideoDecoderByName_001, TestSize.Level1)
158 {
159     std::string mimetype_ = "testmimeType";
160     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByMime(mimetype_), DecoderAdapterCode::DECODER_ERROR);
161     std::string name_ = "testname";
162     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByName(name_), DecoderAdapterCode::DECODER_ERROR);
163     mimetype_ = "video/avc";
164     DecoderAdapterCode ret = mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByMime(mimetype_);
165     EXPECT_EQ(ret, DecoderAdapterCode::DECODER_OK);
166 }
167 
168 /**
169  * @tc.name: MediaCodecDecoderAdapterImpl_InvalidValueTest_002.
170  * @tc.desc: test of InvalidValueScene in MediaCodecDecoderAdapterImpl
171  * @tc.type: FUNC.
172  * @tc.require:
173  */
174 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_InvalidValueTest_002, TestSize.Level1)
175 {
176     uint32_t index_ = 0;
177     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->QueueInputBufferDec(index_, 0, 0, 0, BufferFlag::CODEC_BUFFER_FLAG_NONE),
178         DecoderAdapterCode::DECODER_ERROR);
179     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetOutputFormatDec(format_), DecoderAdapterCode::DECODER_ERROR);
180     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index_, true), DecoderAdapterCode::DECODER_ERROR);
181     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ConfigureDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
182     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetParameterDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
183     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetOutputSurface(nullptr), DecoderAdapterCode::DECODER_ERROR);
184     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->PrepareDecoder(), DecoderAdapterCode::DECODER_ERROR);
185     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StartDecoder(), DecoderAdapterCode::DECODER_ERROR);
186     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StopDecoder(), DecoderAdapterCode::DECODER_ERROR);
187     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->FlushDecoder(), DecoderAdapterCode::DECODER_ERROR);
188     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ResetDecoder(), DecoderAdapterCode::DECODER_ERROR);
189     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseDecoder(), DecoderAdapterCode::DECODER_ERROR);
190     std::shared_ptr<DecoderCallbackAdapter> callback = std::make_shared<DecoderCallbackAdapterMock>();
191     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
192 }
193 
194 /**
195  * @tc.name: MediaCodecDecoderAdapterImpl_NormalTest_003.
196  * @tc.desc: test of NormalScene in MediaCodecDecoderAdapterImpl
197  * @tc.type: FUNC.
198  * @tc.require:
199  */
200 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_NormalTest_003, TestSize.Level1)
201 {
202     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->PrepareDecoder(), DecoderAdapterCode::DECODER_ERROR);
203     AVCodecVideoDecoderImplMock *mock = new AVCodecVideoDecoderImplMock();
204     mediaCodecDecoderAdapterImpl_->decoder_.reset(mock);
205     uint32_t index_ = 1;
206     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->QueueInputBufferDec(index_, 0, 0, 0, BufferFlag::CODEC_BUFFER_FLAG_NONE),
207         DecoderAdapterCode::DECODER_ERROR);
208     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetOutputFormatDec(format_), DecoderAdapterCode::DECODER_ERROR);
209     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index_, true), DecoderAdapterCode::DECODER_ERROR);
210     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ConfigureDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
211     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetParameterDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
212     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetOutputSurface(nullptr), DecoderAdapterCode::DECODER_ERROR);
213     std::shared_ptr<DecoderCallbackAdapter> callback = std::make_shared<DecoderCallbackAdapterMock>();
214     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
215     EXPECT_CALL(*mock, Prepare())
216         .Times(1)
217         .WillRepeatedly(::testing::Return(0));
218     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->PrepareDecoder(), DecoderAdapterCode::DECODER_OK);
219     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StartDecoder(), DecoderAdapterCode::DECODER_ERROR);
220     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StopDecoder(), DecoderAdapterCode::DECODER_ERROR);
221     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->FlushDecoder(), DecoderAdapterCode::DECODER_ERROR);
222     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ResetDecoder(), DecoderAdapterCode::DECODER_ERROR);
223     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseDecoder(), DecoderAdapterCode::DECODER_ERROR);
224     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index_, true), DecoderAdapterCode::DECODER_ERROR);
225 }
226 
227 /**
228  * @tc.name: MediaCodecDecoderAdapterImpl_SetCallbackDec_004.
229  * @tc.desc: test of MediaCodecDecoderAdapterImpl::SetCallbackDec
230  * @tc.type: FUNC.
231  * @tc.require:
232  */
233 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_SetCallbackDec_004, TestSize.Level1)
234 {
235     std::shared_ptr<DecoderCallbackAdapter> callback = nullptr;
236     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
237     callback = std::make_shared<DecoderCallbackAdapterMock>();
238     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
239 }
240 
241 /**
242  * @tc.name: MediaCodecDecoderAdapterImpl_GetTypeOrFlag_005.
243  * @tc.desc: test of MediaCodecDecoderAdapterImpl::GetErrorType() GetBufferFlag() GetAVBufferFlag()
244  * @tc.type: FUNC.
245  * @tc.require:
246  */
247 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_GetTypeOrFlag_005, TestSize.Level1)
248 {
249     EXPECT_EQ(
250         mediaCodecDecoderAdapterImpl_->GetErrorType(OHOS::MediaAVCodec::AVCodecErrorType::AVCODEC_ERROR_EXTEND_START),
251         ErrorType::CODEC_ERROR_EXTEND_START);
252     EXPECT_EQ(
253         mediaCodecDecoderAdapterImpl_->GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS),
254         BufferFlag::CODEC_BUFFER_FLAG_EOS);
255     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetAVBufferFlag(BufferFlag::CODEC_BUFFER_FLAG_EOS),
256         OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS);
257     AVCodecErrorType testAVCodecErrorType = static_cast<AVCodecErrorType>(100);
258     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetErrorType(testAVCodecErrorType), ErrorType::CODEC_ERROR_INTERNAL);
259     AVCodecBufferFlag testAVCodecBufferFlag = static_cast<AVCodecBufferFlag>(100);
260     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetBufferFlag(testAVCodecBufferFlag), BufferFlag::CODEC_BUFFER_FLAG_NONE);
261     BufferFlag testBufferFlag = static_cast<BufferFlag>(100);
262     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetAVBufferFlag(testBufferFlag),
263         AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE);
264     std::string codecName = "OH.Media.Codec.Decoder.Video.AVC";
265     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByName(codecName), DecoderAdapterCode::DECODER_OK);
266     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ConfigureDecoder(format_), DecoderAdapterCode::DECODER_OK);
267 }
268 
269 /**
270  * @tc.name: MediaCodecDecoderAdapterImpl_OnError_006.
271  * @tc.desc: test of MediaCodecDecoderAdapterImpl::GetErrorType() GetBufferFlag() GetAVBufferFlag()
272  * @tc.type: FUNC.
273  * @tc.require:
274  */
275 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_OnError_006, TestSize.Level1)
276 {
277     std::shared_ptr<DecoderCallbackAdapter> cb = nullptr;
278     std::shared_ptr<DecoderCallbackImpl> decoderCallbackImpl = std::make_shared<DecoderCallbackImpl>(cb);
279     EXPECT_NE(decoderCallbackImpl, nullptr);
280     std::shared_ptr<DecoderCallbackAdapter> callback = std::make_shared<DecoderCallbackAdapterMock>();
281     EXPECT_NE(callback, nullptr);
282     decoderCallbackImpl->cb_ = callback;
283     decoderCallbackImpl->OnError(OHOS::MediaAVCodec::AVCodecErrorType::AVCODEC_ERROR_EXTEND_START, 1);
284     MediaAVCodec::Format fomat;
285     decoderCallbackImpl->OnOutputFormatChanged(fomat);
286     decoderCallbackImpl->OnInputBufferAvailable(1, nullptr);
287     std::shared_ptr<Media::AVSharedMemory> memory = std::make_shared<Media::AVSharedMemoryBase>(1, 1.0, "test");
288     decoderCallbackImpl->OnInputBufferAvailable(1, memory);
289     AVCodecBufferInfo info;
290     decoderCallbackImpl->OnOutputBufferAvailable(1, info, AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS, nullptr);
291 }
292 }
293 } // namespace OHOS::NWeb
294