1 /*
2  * Copyright (C) 2024 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 "demuxer_plugin_manager_test.h"
17 
18 #include "gtest/gtest.h"
19 #include "stream_demuxer.h"
20 #include "demuxer_plugin_manager.h"
21 
22 #define LOCAL true
23 
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 using namespace testing::ext;
27 using namespace std;
28 
29 namespace OHOS {
30 namespace Media {
SetUpTestCase(void)31 void DemuxerPluginManagerUnitTest::SetUpTestCase(void) {}
32 
TearDownTestCase(void)33 void DemuxerPluginManagerUnitTest::TearDownTestCase(void) {}
34 
SetUp(void)35 void DemuxerPluginManagerUnitTest::SetUp(void)
36 {
37     demuxerPluginManager_ = std::make_shared<DemuxerPluginManager>();
38     std::shared_ptr<StreamDemuxer> streamDemuxer = std::make_shared<StreamDemuxer>();
39     int32_t streamId = 0;
40     dataSourceImpl_ = std::make_shared<DataSourceImpl>(streamDemuxer, streamId);
41 }
42 
TearDown(void)43 void DemuxerPluginManagerUnitTest::TearDown(void)
44 {
45     demuxerPluginManager_ = nullptr;
46     dataSourceImpl_ = nullptr;
47 }
48 
HWTEST_F(DemuxerPluginManagerUnitTest, SetStreamID_001, TestSize.Level1)49 HWTEST_F(DemuxerPluginManagerUnitTest, SetStreamID_001, TestSize.Level1)
50 {
51     // 1. Set up the test environment
52     ASSERT_NE(dataSourceImpl_, nullptr);
53 
54     // 2. Call the function to be tested
55     Status status = dataSourceImpl_->SetStreamID(1);
56 
57     // 3. Verify the result
58     EXPECT_EQ(status, Status::OK);
59     EXPECT_EQ(dataSourceImpl_->streamID_, 1);
60     EXPECT_EQ(dataSourceImpl_->GetStreamID(), 1);
61 }
62 
HWTEST_F(DemuxerPluginManagerUnitTest, GetStreamCount_001, TestSize.Level1)63 HWTEST_F(DemuxerPluginManagerUnitTest, GetStreamCount_001, TestSize.Level1)
64 {
65     size_t streamCount = demuxerPluginManager_->GetStreamCount();
66 
67     EXPECT_EQ(streamCount, demuxerPluginManager_->streamInfoMap_.size());
68 }
69 
HWTEST_F(DemuxerPluginManagerUnitTest, LoadCurrentSubtitlePlugin_001, TestSize.Level1)70 HWTEST_F(DemuxerPluginManagerUnitTest, LoadCurrentSubtitlePlugin_001, TestSize.Level1)
71 {
72     demuxerPluginManager_->curSubTitleStreamID_ = -1;
73     std::shared_ptr<StreamDemuxer> streamDemuxer = std::make_shared<StreamDemuxer>();
74     Plugins::MediaInfo mediaInfo;
75     Status status = demuxerPluginManager_->LoadCurrentSubtitlePlugin(streamDemuxer, mediaInfo);
76 
77     EXPECT_EQ(status, Status::ERROR_UNKNOWN);
78 }
79 
HWTEST_F(DemuxerPluginManagerUnitTest, GetInnerTrackIDByTrackID_001, TestSize.Level1)80 HWTEST_F(DemuxerPluginManagerUnitTest, GetInnerTrackIDByTrackID_001, TestSize.Level1)
81 {
82     int32_t trackId = 1;
83     demuxerPluginManager_->trackInfoMap_[trackId].innerTrackIndex = 2;
84 
85     // 2. Call the function to be tested
86     int32_t innerTrackId = demuxerPluginManager_->GetInnerTrackIDByTrackID(trackId);
87 
88     // 3. Verify the result
89     EXPECT_EQ(innerTrackId, 2);
90 }
91 
HWTEST_F(DemuxerPluginManagerUnitTest, GetInnerTrackIDByTrackID_002, TestSize.Level1)92 HWTEST_F(DemuxerPluginManagerUnitTest, GetInnerTrackIDByTrackID_002, TestSize.Level1)
93 {
94     int32_t trackId = 1;
95 
96     // 2. Call the function to be tested
97     int32_t innerTrackId = demuxerPluginManager_->GetInnerTrackIDByTrackID(trackId);
98 
99     // 3. Verify the result
100     EXPECT_EQ(innerTrackId, -1);
101 }
102 
HWTEST_F(DemuxerPluginManagerUnitTest, AddExternalSubtitle_001, TestSize.Level1)103 HWTEST_F(DemuxerPluginManagerUnitTest, AddExternalSubtitle_001, TestSize.Level1)
104 {
105     // 1. Set up the test environment
106     demuxerPluginManager_->curSubTitleStreamID_ = -1;
107     // 2. Call the function to be tested
108     int32_t result = demuxerPluginManager_->AddExternalSubtitle();
109     // 3. Verify the result
110     EXPECT_EQ(result, 0);
111     EXPECT_EQ(demuxerPluginManager_->curSubTitleStreamID_, 0);
112 
113     // 1. Set up the test environment
114     demuxerPluginManager_->curSubTitleStreamID_ = 0;
115     // 2. Call the function to be tested
116     result = demuxerPluginManager_->AddExternalSubtitle();
117 }
118 
HWTEST_F(DemuxerPluginManagerUnitTest, IsSubtitleMime_001, TestSize.Level1)119 HWTEST_F(DemuxerPluginManagerUnitTest, IsSubtitleMime_001, TestSize.Level1)
120 {
121     // 1. Set up the test environment
122     std::string mime = "application/x-subrip";
123     // 2. Call the function to be tested
124     bool result = demuxerPluginManager_->IsSubtitleMime(mime);
125     // 3. Verify the result
126     EXPECT_EQ(result, true);
127 
128     // 1. Set up the test environment
129     mime = "text/vtt";
130     // 2. Call the function to be tested
131     result = demuxerPluginManager_->IsSubtitleMime(mime);
132     // 3. Verify the result
133     EXPECT_EQ(result, true);
134 
135     // 1. Set up the test environment
136     mime = "video/mp4";
137     // 2. Call the function to be tested
138     result = demuxerPluginManager_->IsSubtitleMime(mime);
139     // 3. Verify the result
140     EXPECT_EQ(result, false);
141 }
142 
HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_001, TestSize.Level1)143 HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_001, TestSize.Level1)
144 {
145     // 1. Set up the test environment
146     int32_t trackId = 0;
147     std::vector<Meta> tracks;
148     Meta meta;
149     meta.SetData(Tag::MIME_TYPE, "audio/mpeg");
150     tracks.push_back(meta);
151     Plugins::MediaInfo mediaInfo;
152     mediaInfo.tracks = tracks;
153     demuxerPluginManager_->curMediaInfo_ = mediaInfo;
154 
155     // 2. Call the function to be tested
156     TrackType result = demuxerPluginManager_->GetTrackTypeByTrackID(trackId);
157 
158     // 3. Verify the result
159     EXPECT_EQ(result, TRACK_AUDIO);
160 }
161 
HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_002, TestSize.Level1)162 HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_002, TestSize.Level1)
163 {
164     // 1. Set up the test environment
165     int32_t trackId = 0;
166     std::vector<Meta> tracks;
167     Meta meta;
168     meta.SetData(Tag::MIME_TYPE, "video/mpeg");
169     tracks.push_back(meta);
170     Plugins::MediaInfo mediaInfo;
171     mediaInfo.tracks = tracks;
172     demuxerPluginManager_->curMediaInfo_ = mediaInfo;
173 
174     // 2. Call the function to be tested
175     TrackType result = demuxerPluginManager_->GetTrackTypeByTrackID(trackId);
176 
177     // 3. Verify the result
178     EXPECT_EQ(result, TRACK_VIDEO);
179 }
180 
HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_003, TestSize.Level1)181 HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_003, TestSize.Level1)
182 {
183     // 1. Set up the test environment
184     int32_t trackId = 0;
185     std::vector<Meta> tracks;
186     Meta meta;
187     meta.SetData(Tag::MIME_TYPE, "application/x-subrip");
188     tracks.push_back(meta);
189     Plugins::MediaInfo mediaInfo;
190     mediaInfo.tracks = tracks;
191     demuxerPluginManager_->curMediaInfo_ = mediaInfo;
192 
193     // 2. Call the function to be tested
194     TrackType result = demuxerPluginManager_->GetTrackTypeByTrackID(trackId);
195 
196     // 3. Verify the result
197     EXPECT_EQ(result, TRACK_SUBTITLE);
198 }
199 
HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_004, TestSize.Level1)200 HWTEST_F(DemuxerPluginManagerUnitTest, GetTrackTypeByTrackID_004, TestSize.Level1)
201 {
202     // 1. Set up the test environment
203     int32_t trackId = 0;
204     std::vector<Meta> tracks;
205     Meta meta;
206     meta.SetData(Tag::MIME_TYPE, "unknown/type");
207     tracks.push_back(meta);
208     Plugins::MediaInfo mediaInfo;
209     mediaInfo.tracks = tracks;
210     demuxerPluginManager_->curMediaInfo_ = mediaInfo;
211 
212     // 2. Call the function to be tested
213     TrackType result = demuxerPluginManager_->GetTrackTypeByTrackID(trackId);
214 
215     // 3. Verify the result
216     EXPECT_EQ(result, TRACK_INVALID);
217 }
218 
HWTEST_F(DemuxerPluginManagerUnitTest, GetStreamTypeByTrackID_001, TestSize.Level1)219 HWTEST_F(DemuxerPluginManagerUnitTest, GetStreamTypeByTrackID_001, TestSize.Level1)
220 {
221     int32_t trackId = 1;
222     std::map<int32_t, MediaTrackMap> mediaTrackMap;
223     mediaTrackMap[1].streamID = 1;
224     demuxerPluginManager_->trackInfoMap_ = mediaTrackMap;
225     demuxerPluginManager_->streamInfoMap_[1].type = StreamType::VIDEO;
226     // 2. Call the function to be tested
227     StreamType result = demuxerPluginManager_->GetStreamTypeByTrackID(trackId);
228 
229     // 3. Verify the result
230     EXPECT_EQ(result, StreamType::VIDEO);
231 }
232 
HWTEST_F(DemuxerPluginManagerUnitTest, UpdateDefaultStreamID_001, TestSize.Level1)233 HWTEST_F(DemuxerPluginManagerUnitTest, UpdateDefaultStreamID_001, TestSize.Level1)
234 {
235     // 1. Set up the test environment
236     Plugins::MediaInfo mediaInfo;
237     int32_t newStreamID = 1; // Assuming streamID 1 is valid
238 
239     // 2. Call the function to be tested
240     Status result = demuxerPluginManager_->UpdateDefaultStreamID(mediaInfo, StreamType::AUDIO, newStreamID);
241 
242     // 3. Verify the result
243     EXPECT_EQ(demuxerPluginManager_->curAudioStreamID_, newStreamID);
244     EXPECT_EQ(result, Status::OK);
245 }
246 
HWTEST_F(DemuxerPluginManagerUnitTest, UpdateDefaultStreamID_002, TestSize.Level1)247 HWTEST_F(DemuxerPluginManagerUnitTest, UpdateDefaultStreamID_002, TestSize.Level1)
248 {
249     // 1. Set up the test environment
250     Plugins::MediaInfo mediaInfo;
251     int32_t newStreamID = 1; // Assuming streamID 1 is valid
252 
253     // 2. Call the function to be tested
254     Status result = demuxerPluginManager_->UpdateDefaultStreamID(mediaInfo, StreamType::SUBTITLE, newStreamID);
255 
256     // 3. Verify the result
257     EXPECT_EQ(demuxerPluginManager_->curSubTitleStreamID_, newStreamID);
258     EXPECT_EQ(result, Status::OK);
259 }
260 
HWTEST_F(DemuxerPluginManagerUnitTest, UpdateDefaultStreamID_003, TestSize.Level1)261 HWTEST_F(DemuxerPluginManagerUnitTest, UpdateDefaultStreamID_003, TestSize.Level1)
262 {
263     // 1. Set up the test environment
264     Plugins::MediaInfo mediaInfo;
265     int32_t newStreamID = 1; // Assuming streamID 1 is valid
266 
267     // 2. Call the function to be tested
268     Status result = demuxerPluginManager_->UpdateDefaultStreamID(mediaInfo, StreamType::VIDEO, newStreamID);
269 
270     // 3. Verify the result
271     EXPECT_EQ(demuxerPluginManager_->curVideoStreamID_, newStreamID);
272     EXPECT_EQ(result, Status::OK);
273 }
274 
HWTEST_F(DemuxerPluginManagerUnitTest, SetResetEosStatus_001, TestSize.Level1)275 HWTEST_F(DemuxerPluginManagerUnitTest, SetResetEosStatus_001, TestSize.Level1)
276 {
277     // 1. Set up the test environment
278     bool flag = true;
279 
280     // 2. Call the function to be tested
281     demuxerPluginManager_->SetResetEosStatus(flag);
282 
283     // 3. Verify the result
284     EXPECT_EQ(demuxerPluginManager_->needResetEosStatus_, flag);
285 }
286 }
287 }