1 /*
2  * Copyright (c) 2021-2021 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 "plugin/plugins/ffmpeg_adapter/audio_encoder/audio_ffmpeg_encoder_plugin.h"
18 #include "plugin/plugins/ffmpeg_adapter/audio_encoder/ffmpeg_au_enc_config.h"
19 #include "plugin/common/plugin_caps_builder.h"
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Test {
24 using namespace Plugin;
25 using namespace Ffmpeg;
26 using namespace testing::ext;
AuFfmpegEncoderCreator(const std::string & name)27 std::shared_ptr<CodecPlugin> AuFfmpegEncoderCreator(const std::string& name)
28 {
29     return std::make_shared<AudioFfmpegEncoderPlugin>(name);
30 }
31 
32 HWTEST(AudioFfmpegEncoderPluginTest, test_Init, TestSize.Level1)
33 {
34     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
35     ASSERT_EQ(Status::ERROR_UNSUPPORTED_FORMAT, aEncoderPlugin->Init());
36 }
37 
38 HWTEST(AudioFfmpegEncoderPluginTest, test_Prepare, TestSize.Level1)
39 {
40     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
41     ASSERT_EQ(Status::ERROR_WRONG_STATE, aEncoderPlugin->Prepare());
42 }
43 
44 HWTEST(AudioFfmpegEncoderPluginTest, test_Reset, TestSize.Level1)
45 {
46     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
47     ASSERT_EQ(Status::OK, aEncoderPlugin->Reset());
48 }
49 
50 HWTEST(AudioFfmpegEncoderPluginTest, test_Start, TestSize.Level1)
51 {
52     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
53     ASSERT_EQ(Status::ERROR_WRONG_STATE, aEncoderPlugin->Start());
54 }
55 
56 HWTEST(AudioFfmpegEncoderPluginTest, test_Stop, TestSize.Level1)
57 {
58     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
59     ASSERT_EQ(Status::OK, aEncoderPlugin->Stop());
60 }
61 
62 HWTEST(AudioFfmpegEncoderPluginTest, test_SetParameter, TestSize.Level1)
63 {
64     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
65     ValueType value = 128;
66     ASSERT_EQ(Status::OK, aEncoderPlugin->SetParameter(Tag::AUDIO_SAMPLE_PER_FRAME, &value));
67 }
68 
69 HWTEST(AudioFfmpegEncoderPluginTest, test_GetParameter, TestSize.Level1)
70 {
71     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
72     ValueType value;
73     ASSERT_EQ(Status::OK, aEncoderPlugin->GetParameter(Tag::REQUIRED_OUT_BUFFER_CNT, value));
74 }
75 
76 HWTEST(AudioFfmpegEncoderPluginTest, test_Flush, TestSize.Level1)
77 {
78     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
79     ASSERT_EQ(Status::OK, aEncoderPlugin->Flush());
80 }
81 
82 HWTEST(AudioFfmpegEncoderPluginTest, test_QueInputBuffer, TestSize.Level1)
83 {
84     std::shared_ptr<CodecPlugin> aEncoderPlugin = AuFfmpegEncoderCreator("AudioFfmpegEncoderPluginTest");
85     std::shared_ptr<Buffer> inputBuffer = std::make_shared<Buffer>(BufferMetaType::AUDIO);
86     int32_t timeoutMs = 100;
87     ASSERT_EQ(Status::ERROR_INVALID_DATA, aEncoderPlugin->QueueInputBuffer(inputBuffer, timeoutMs));
88     uint32_t size = 16;
89     inputBuffer->AllocMemory(nullptr, size);
90     ASSERT_EQ(Status::ERROR_WRONG_STATE, aEncoderPlugin->QueueInputBuffer(inputBuffer, timeoutMs));
91     inputBuffer->flag = 1;
92     ASSERT_EQ(Status::ERROR_WRONG_STATE, aEncoderPlugin->QueueInputBuffer(inputBuffer, timeoutMs));
93 }
94 
95 } //namespace Test
96 } //namespace Media
97 } //namespace OHOS