1 /*
2 * Copyright (C) 2025 Huawei Device Co., Ltd.
3 * Licensed undr 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 "source_test.h"
17 #include "plugin/plugin_base.h"
18 #include "plugin/source_plugin.h"
19 #include "common/media_source.h"
20 #include "plugin/plugin_event.h"
21 #include "meta/media_types.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace OHOS::MediaAVCodec;
26
27 const static int32_t TEST_TIMES_ONE = 1;
28 const static std::string TEST_NAME = "test_source_plugin";
29 const std::string MEDIA_ROOT = "file:///data/test/media/";
30 const std::string VIDEO_FILE1 = MEDIA_ROOT + "camera_info_parser.mp4";
31 const static int32_t NUM_VALUE = 3;
32
33 namespace OHOS {
34 namespace Media {
35
SetUp(void)36 void SourceTest::SetUp(void)
37 {
38 source_ = std::make_shared<Source>();
39 ASSERT_TRUE(source_ != nullptr);
40
41 mockSourcePlugin_ = std::make_shared<MockSourcePlugin>(TEST_NAME);
42 ASSERT_TRUE(mockSourcePlugin_ != nullptr);
43
44 mockCallback_ = std::make_shared<MockCallback>();
45 ASSERT_TRUE(mockCallback_ != nullptr);
46
47 source_->plugin_ = mockSourcePlugin_;
48 source_->mediaDemuxerCallback_ = mockCallback_;
49
50 mockMediaSource_ = std::make_shared<MockMediaSource>(VIDEO_FILE1);
51 ASSERT_TRUE(mockMediaSource_ != nullptr);
52 }
53
TearDown(void)54 void SourceTest::TearDown(void)
55 {
56 source_ = nullptr;
57 mockCallback_ = nullptr;
58 mockSourcePlugin_ = nullptr;
59 }
60
61 /**
62 * @tc.name Test SetStartPts API
63 * @tc.number SetStartPts_001
64 * @tc.desc Test plugin_ != nullptr
65 */
66 HWTEST_F(SourceTest, SetStartPts_001, TestSize.Level1)
67 {
68 EXPECT_CALL(*(mockSourcePlugin_), SetStartPts(testing::_)).WillRepeatedly(Return(Status::OK));
69 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
70 Status ret = source_->SetStartPts(0);
71 EXPECT_EQ(Status::OK, ret);
72 }
73
74 /**
75 * @tc.name Test SetStartPts API
76 * @tc.number SetStartPts_002
77 * @tc.desc Test plugin_ == nullptr
78 */
79 HWTEST_F(SourceTest, SetStartPts_002, TestSize.Level1)
80 {
81 source_->plugin_ = nullptr;
82 Status ret = source_->SetStartPts(0);
83 EXPECT_EQ(Status::ERROR_INVALID_OPERATION, ret);
84 }
85
86 /**
87 * @tc.name Test SetExtraCache API
88 * @tc.number SetExtraCache_001
89 * @tc.desc Test plugin_ != nullptr
90 */
91 HWTEST_F(SourceTest, SetExtraCache_001, TestSize.Level1)
92 {
93 EXPECT_CALL(*(mockSourcePlugin_), SetExtraCache(testing::_)).WillRepeatedly(Return(Status::OK));
94 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
95 Status ret = source_->SetExtraCache(100);
96 EXPECT_EQ(Status::OK, ret);
97 }
98
99 /**
100 * @tc.name Test SetExtraCache API
101 * @tc.number SetExtraCache_002
102 * @tc.desc Test plugin_ == nullptr
103 */
104 HWTEST_F(SourceTest, SetExtraCache_002, TestSize.Level1)
105 {
106 source_->plugin_ = nullptr;
107 Status ret = source_->SetExtraCache(100);
108 EXPECT_EQ(Status::ERROR_INVALID_OPERATION, ret);
109 }
110
111 /**
112 * @tc.name Test Prepare API
113 * @tc.number Prepare_001
114 * @tc.desc Test plugin_ == nullptr
115 */
116 HWTEST_F(SourceTest, Prepare_001, TestSize.Level1)
117 {
118 source_->plugin_ = nullptr;
119 Status ret = source_->Prepare();
120 EXPECT_EQ(Status::OK, ret);
121 }
122
123 /**
124 * @tc.name Test Prepare API
125 * @tc.number Prepare_002
126 * @tc.desc Test ret == Status::OK
127 */
128 HWTEST_F(SourceTest, Prepare_002, TestSize.Level1)
129 {
130 EXPECT_CALL(*(mockSourcePlugin_), Prepare()).WillRepeatedly(Return(Status::OK));
131 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
132 Status ret = source_->Prepare();
133 EXPECT_EQ(Status::OK, ret);
134 }
135
136 /**
137 * @tc.name Test Prepare API
138 * @tc.number Prepare_003
139 * @tc.desc Test ret != Status::OK && ret != Status::ERROR_DELAY_READY
140 */
141 HWTEST_F(SourceTest, Prepare_003, TestSize.Level1)
142 {
143 EXPECT_CALL(*(mockSourcePlugin_), Prepare()).WillRepeatedly(Return(Status::ERROR_WRONG_STATE));
144 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
145 Status ret = source_->Prepare();
146 EXPECT_EQ(Status::ERROR_WRONG_STATE, ret);
147 }
148
149 /**
150 * @tc.name Test Prepare API
151 * @tc.number Prepare_004
152 * @tc.desc Test ret = Status::ERROR_DELAY_READY && isAboveWaterline_ = false
153 */
154 HWTEST_F(SourceTest, Prepare_004, TestSize.Level1)
155 {
156 EXPECT_CALL(*(mockSourcePlugin_), Prepare()).WillRepeatedly(Return(Status::ERROR_DELAY_READY));
157 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
158 source_->isAboveWaterline_ = false;
159 Status ret = source_->Prepare();
160 EXPECT_EQ(Status::ERROR_DELAY_READY, ret);
161 }
162
163 /**
164 * @tc.name Test Prepare API
165 * @tc.number Prepare_005
166 * @tc.desc Test ret = Status::ERROR_DELAY_READY && isAboveWaterline_ = true
167 */
168 HWTEST_F(SourceTest, Prepare_005, TestSize.Level1)
169 {
170 EXPECT_CALL(*(mockSourcePlugin_), Prepare()).WillRepeatedly(Return(Status::ERROR_DELAY_READY));
171 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
172 source_->isAboveWaterline_ = true;
173 Status ret = source_->Prepare();
174 EXPECT_EQ(source_->isPluginReady_, false);
175 EXPECT_EQ(source_->isAboveWaterline_, false);
176 EXPECT_EQ(Status::ERROR_DELAY_READY, ret);
177 }
178
179 /**
180 * @tc.name Test AutoSelectBitRate API
181 * @tc.number AutoSelectBitRate_001
182 * @tc.desc Test plugin_ != nullptr
183 */
184 HWTEST_F(SourceTest, AutoSelectBitRate_001, TestSize.Level1)
185 {
186 EXPECT_CALL(*(mockSourcePlugin_), AutoSelectBitRate(testing::_)).WillRepeatedly(Return(Status::OK));
187 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
188 Status ret = source_->AutoSelectBitRate(100);
189 EXPECT_EQ(Status::OK, ret);
190 }
191
192 /**
193 * @tc.name Test AutoSelectBitRate API
194 * @tc.number AutoSelectBitRate_002
195 * @tc.desc Test plugin_ != nullptr
196 */
197 HWTEST_F(SourceTest, AutoSelectBitRate_002, TestSize.Level1)
198 {
199 source_->plugin_ = nullptr;
200 Status ret = source_->AutoSelectBitRate(100);
201 EXPECT_EQ(Status::ERROR_INVALID_OPERATION, ret);
202 }
203
204 /**
205 * @tc.name Test SetCurrentBitRate API
206 * @tc.number SetCurrentBitRate_001
207 * @tc.desc Test plugin_ == nullptr
208 */
209 HWTEST_F(SourceTest, SetCurrentBitRate_001, TestSize.Level1)
210 {
211 source_->plugin_ = nullptr;
212 Status ret = source_->SetCurrentBitRate(100, 1);
213 EXPECT_EQ(Status::ERROR_INVALID_OPERATION, ret);
214 }
215
216 /**
217 * @tc.name Test OnEvent API
218 * @tc.number OnEvent_001
219 * @tc.desc Test protocol_ == "http" && isInterruptNeeded_
220 */
221 HWTEST_F(SourceTest, OnEvent_001, TestSize.Level1)
222 {
223 source_->protocol_ = "http";
224 source_->isInterruptNeeded_ = true;
225 Plugins::PluginEvent event;
226 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
227 source_->OnEvent(event);
228 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
229 }
230
231 /**
232 * @tc.name Test OnEvent API
233 * @tc.number OnEvent_002
234 * @tc.desc Test event.type == PluginEventType::SOURCE_DRM_INFO_UPDATE
235 */
236 HWTEST_F(SourceTest, OnEvent_002, TestSize.Level1)
237 {
238 source_->protocol_ = "http";
239 source_->isInterruptNeeded_ = false;
240 Plugins::PluginEvent event;
241 event.type = PluginEventType::SOURCE_DRM_INFO_UPDATE;
242 EXPECT_CALL(*(mockCallback_), OnEvent(testing::_)).Times(TEST_TIMES_ONE);
243 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
244 source_->OnEvent(event);
245 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
246 }
247
248 /**
249 * @tc.name Test OnEvent API
250 * @tc.number OnEvent_003
251 * @tc.desc Test event.type == PluginEventType::SOURCE_BITRATE_START
252 */
253 HWTEST_F(SourceTest, OnEvent_003, TestSize.Level1)
254 {
255 source_->protocol_ = "http";
256 source_->isInterruptNeeded_ = false;
257 Plugins::PluginEvent event;
258 event.type = PluginEventType::SOURCE_BITRATE_START;
259 EXPECT_CALL(*(mockCallback_), OnEvent(testing::_)).Times(TEST_TIMES_ONE);
260 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
261 source_->OnEvent(event);
262 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
263 }
264
265 /**
266 * @tc.name Test OnEvent API
267 * @tc.number OnEvent_004
268 * @tc.desc Test event.type == PluginEventType::DASH_SEEK_READY
269 */
270 HWTEST_F(SourceTest, OnEvent_004, TestSize.Level1)
271 {
272 source_->protocol_ = "http";
273 source_->isInterruptNeeded_ = false;
274 Plugins::PluginEvent event;
275 event.type = PluginEventType::DASH_SEEK_READY;
276 EXPECT_CALL(*(mockCallback_), OnEvent(testing::_)).Times(TEST_TIMES_ONE);
277 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
278 source_->OnEvent(event);
279 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
280 }
281
282 /**
283 * @tc.name Test OnEvent API
284 * @tc.number OnEvent_005
285 * @tc.desc Test event.type == PluginEventType::FLV_AUTO_SELECT_BITRATE
286 */
287 HWTEST_F(SourceTest, OnEvent_005, TestSize.Level1)
288 {
289 source_->protocol_ = "http";
290 source_->isInterruptNeeded_ = false;
291 Plugins::PluginEvent event;
292 event.type = PluginEventType::FLV_AUTO_SELECT_BITRATE;
293 EXPECT_CALL(*(mockCallback_), OnEvent(testing::_)).Times(TEST_TIMES_ONE);
294 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
295 source_->OnEvent(event);
296 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
297 }
298
299 /**
300 * @tc.name Test OnEvent API
301 * @tc.number OnEvent_006
302 * @tc.desc Test event.type == PluginEventType::HLS_SEEK_READY
303 */
304 HWTEST_F(SourceTest, OnEvent_006, TestSize.Level1)
305 {
306 source_->protocol_ = "http";
307 source_->isInterruptNeeded_ = false;
308 Plugins::PluginEvent event;
309 event.type = PluginEventType::HLS_SEEK_READY;
310 EXPECT_CALL(*(mockCallback_), OnEvent(testing::_)).Times(TEST_TIMES_ONE);
311 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
312 source_->OnEvent(event);
313 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
314 }
315
316 /**
317 * @tc.name Test SetSelectBitRateFlag API
318 * @tc.number SetSelectBitRateFlag_001
319 * @tc.desc Test mediaDemuxerCallback_ == nullptr
320 */
321 HWTEST_F(SourceTest, SetSelectBitRateFlag_001, TestSize.Level1)
322 {
323 source_->mediaDemuxerCallback_ = nullptr;
324 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
325 source_->SetSelectBitRateFlag(true, 100);
326 EXPECT_EQ(source_->mediaDemuxerCallback_, nullptr);
327 ASSERT_TRUE(source_->plugin_ != nullptr);
328 }
329
330 /**
331 * @tc.name Test SetSelectBitRateFlag API
332 * @tc.number SetSelectBitRateFlag_002
333 * @tc.desc Test mediaDemuxerCallback_ != nullptr
334 */
335 HWTEST_F(SourceTest, SetSelectBitRateFlag_002, TestSize.Level1)
336 {
337 EXPECT_CALL(*(mockCallback_), SetSelectBitRateFlag(testing::_, testing::_)).Times(TEST_TIMES_ONE);
338 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
339 source_->SetSelectBitRateFlag(true, 100);
340 ASSERT_TRUE(source_->mediaDemuxerCallback_ != nullptr);
341 }
342
343 /**
344 * @tc.name Test CanAutoSelectBitRate API
345 * @tc.number CanAutoSelectBitRate_001
346 * @tc.desc Test mediaDemuxerCallback_ == nullptr
347 */
348 HWTEST_F(SourceTest, CanAutoSelectBitRate_001, TestSize.Level1)
349 {
350 source_->mediaDemuxerCallback_ = nullptr;
351 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
352 bool ret = source_->CanAutoSelectBitRate();
353 EXPECT_EQ(false, ret);
354 }
355
356 /**
357 * @tc.name Test ReadWithPerfRecord API
358 * @tc.number ReadWithPerfRecord_001
359 * @tc.desc Test seekToTimeFlag_ = true
360 */
361 HWTEST_F(SourceTest, ReadWithPerfRecord_001, TestSize.Level1)
362 {
363 source_->seekToTimeFlag_ = true;
364 EXPECT_CALL(*(mockSourcePlugin_),
365 Read(testing::_, testing::_, testing::_, testing::_)).WillRepeatedly(Return(Status::OK));
366 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
367 std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>();
368 Status ret = source_->ReadWithPerfRecord(1, buffer, 0, 100);
369 EXPECT_EQ(Status::OK, ret);
370 }
371
372 /**
373 * @tc.name Test GetStreamInfo API
374 * @tc.number GetStreamInfo_001
375 * @tc.desc Test ret != Status::OK
376 */
377 HWTEST_F(SourceTest, GetStreamInfo_001, TestSize.Level1)
378 {
379 std::vector<Plugins::StreamInfo> streams_;
380 EXPECT_CALL(*(mockSourcePlugin_),
381 GetStreamInfo(testing::_)).WillRepeatedly(Return(Status::ERROR_INVALID_OPERATION));
382 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
383 Status ret = source_->GetStreamInfo(streams_);
384 EXPECT_EQ(Status::ERROR_INVALID_OPERATION, ret);
385 }
386
387 /**
388 * @tc.name Test GetStreamInfo API
389 * @tc.number GetStreamInfo_002
390 * @tc.desc Test ret == Status::OK && && streams.size() != 0
391 */
392 HWTEST_F(SourceTest, GetStreamInfo_002, TestSize.Level1)
393 {
394 std::vector<Plugins::StreamInfo> streams_;
395 Plugins::StreamInfo info;
396 info.streamId = 0;
397 info.bitRate = 0;
398 info.type = Plugins::MIXED;
399 streams_.push_back(info);
400 EXPECT_CALL(*(mockSourcePlugin_), GetStreamInfo(testing::_)).WillRepeatedly(Return(Status::OK));
401 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
402 Status ret = source_->GetStreamInfo(streams_);
403 EXPECT_EQ(Status::OK, ret);
404 }
405
406 /**
407 * @tc.name Test SeekToTime API
408 * @tc.number SeekToTime_001
409 * @tc.desc Test seekable_ != Seekable::SEEKABLE && Ms2HstTime return true
410 */
411 HWTEST_F(SourceTest, SeekToTime_001, TestSize.Level1)
412 {
413 source_->seekable_ = Plugins::Seekable::UNSEEKABLE;
414 source_->isInterruptNeeded_ = true;
415 int64_t seekTime = 0;
416 Plugins::SeekMode mode = Plugins::SeekMode::SEEK_NEXT_SYNC;
417 EXPECT_CALL(*(mockSourcePlugin_), SeekToTime(testing::_, testing::_)).WillRepeatedly(Return(Status::OK));
418 EXPECT_CALL(*(mockSourcePlugin_), GetSeekable()).WillRepeatedly(Return(Plugins::Seekable::INVALID));
419 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
420 Status ret = source_->SeekToTime(seekTime, mode);
421 EXPECT_EQ(Status::OK, ret);
422 }
423
424 /**
425 * @tc.name Test SeekToTime API
426 * @tc.number SeekToTime_002
427 * @tc.desc Test seekable_ == Seekable::SEEKABLE && Ms2HstTime return true
428 */
429 HWTEST_F(SourceTest, SeekToTime_002, TestSize.Level1)
430 {
431 source_->seekable_ = Plugins::Seekable::SEEKABLE;
432 source_->isInterruptNeeded_ = true;
433 int64_t seekTime = 0;
434 Plugins::SeekMode mode = Plugins::SeekMode::SEEK_NEXT_SYNC;
435 EXPECT_CALL(*(mockSourcePlugin_), SeekToTime(testing::_, testing::_)).WillRepeatedly(Return(Status::OK));
436 EXPECT_CALL(*(mockSourcePlugin_), GetSeekable()).WillRepeatedly(Return(Plugins::Seekable::INVALID));
437 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
438 Status ret = source_->SeekToTime(seekTime, mode);
439 EXPECT_EQ(Status::OK, ret);
440 }
441
442 /**
443 * @tc.name Test SeekToTime API
444 * @tc.number SeekToTime_003
445 * @tc.desc Test seekable_ != Seekable::SEEKABLE && Ms2HstTime return false
446 */
447 HWTEST_F(SourceTest, SeekToTime_003, TestSize.Level1)
448 {
449 source_->seekable_ = Plugins::Seekable::UNSEEKABLE;
450 source_->isInterruptNeeded_ = true;
451 int64_t seekTime = INT64_MAX / HST_MSECOND + 1;
452 Plugins::SeekMode mode = Plugins::SeekMode::SEEK_NEXT_SYNC;
453 EXPECT_CALL(*(mockSourcePlugin_), GetSeekable()).WillRepeatedly(Return(Plugins::Seekable::INVALID));
454 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
455 Status ret = source_->SeekToTime(seekTime, mode);
456 EXPECT_EQ(Status::ERROR_INVALID_PARAMETER, ret);
457 }
458
459 /**
460 * @tc.name Test SeekToTime API
461 * @tc.number SeekToTime_004
462 * @tc.desc Test seekable_ == Seekable::SEEKABLE && Ms2HstTime return false
463 */
464 HWTEST_F(SourceTest, SeekToTime_004, TestSize.Level1)
465 {
466 source_->seekable_ = Plugins::Seekable::SEEKABLE;
467 source_->isInterruptNeeded_ = true;
468 int64_t seekTime = INT64_MAX / HST_MSECOND + 1;
469 Plugins::SeekMode mode = Plugins::SeekMode::SEEK_NEXT_SYNC;
470 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
471
472 Status ret = source_->SeekToTime(seekTime, mode);
473 EXPECT_EQ(Status::ERROR_INVALID_PARAMETER, ret);
474 }
475
476 /**
477 * @tc.name Test ParseProtocol API
478 * @tc.number ParseProtocol_001
479 * @tc.desc Test srcType == SourceType::SOURCE_TYPE_URI && mimeType == AVMimeTypes::APPLICATION_M3U8
480 */
481 HWTEST_F(SourceTest, ParseProtocol_001, TestSize.Level1)
482 {
483 std::shared_ptr<Plugins::MediaSource> source_temp =
484 std::dynamic_pointer_cast<Plugins::MediaSource>(mockMediaSource_);
485 ASSERT_TRUE(source_temp != nullptr);
486 source_temp->type_ = SourceType::SOURCE_TYPE_URI;
487 source_temp->mimeType_ = AVMimeTypes::APPLICATION_M3U8;
488 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
489
490 bool ret = source_->ParseProtocol(source_temp);
491 EXPECT_EQ(true, ret);
492 }
493
494 /**
495 * @tc.name Test ParseProtocol API
496 * @tc.number ParseProtocol_002
497 * @tc.desc Test srcType == SourceType::SOURCE_TYPE_FD
498 */
499 HWTEST_F(SourceTest, ParseProtocol_002, TestSize.Level1)
500 {
501 std::shared_ptr<Plugins::MediaSource> source_temp =
502 std::dynamic_pointer_cast<Plugins::MediaSource>(mockMediaSource_);
503 ASSERT_TRUE(source_temp != nullptr);
504 source_temp->type_ = SourceType::SOURCE_TYPE_FD;
505 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
506
507 bool ret = source_->ParseProtocol(source_temp);
508 EXPECT_EQ(true, ret);
509 }
510
511 /**
512 * @tc.name Test ParseProtocol API
513 * @tc.number ParseProtocol_003
514 * @tc.desc Test srcType == SourceType::SOURCE_TYPE_STREAM
515 */
516 HWTEST_F(SourceTest, ParseProtocol_003, TestSize.Level1)
517 {
518 std::shared_ptr<Plugins::MediaSource> source_temp =
519 std::dynamic_pointer_cast<Plugins::MediaSource>(mockMediaSource_);
520 ASSERT_TRUE(source_temp != nullptr);
521 source_temp->type_ = SourceType::SOURCE_TYPE_STREAM;
522 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
523
524 bool ret = source_->ParseProtocol(source_temp);
525 EXPECT_EQ(true, ret);
526 }
527
528 /**
529 * @tc.name Test FindPlugin API
530 * @tc.number FindPlugin_001
531 * @tc.desc Test protocol_.empty()
532 */
533 HWTEST_F(SourceTest, FindPlugin_001, TestSize.Level1)
534 {
535 std::shared_ptr<Plugins::MediaSource> source_temp =
536 std::dynamic_pointer_cast<Plugins::MediaSource>(mockMediaSource_);
537 ASSERT_TRUE(source_temp != nullptr);
538 source_temp->type_ = static_cast<OHOS::Media::Plugins::SourceType>(NUM_VALUE);
539 source_->protocol_= "";
540 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
541
542 Status ret = source_->FindPlugin(source_temp);
543 EXPECT_EQ(ret, Status::ERROR_INVALID_PARAMETER);
544 }
545
546 /**
547 * @tc.name Test GetDuration API
548 * @tc.number GetDuration_001
549 * @tc.desc Test ret == Status::ERROR_INVALID_PARAMETER
550 */
551 HWTEST_F(SourceTest, GetDuration_001, TestSize.Level1)
552 {
553 source_->seekToTimeFlag_ = true;
554 EXPECT_CALL(*(mockSourcePlugin_), GetDuration(testing::_)).WillRepeatedly(Return(Status::ERROR_INVALID_PARAMETER));
555 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
556 int64_t ret = source_->GetDuration();
557 EXPECT_EQ(ret, Plugins::HST_TIME_NONE);
558 }
559
560 /**
561 * @tc.name Test GetDuration API
562 * @tc.number GetDuration_002
563 * @tc.desc Test ret == Status::OK
564 */
565 HWTEST_F(SourceTest, GetDuration_002, TestSize.Level1)
566 {
567 source_->seekToTimeFlag_ = true;
568 EXPECT_CALL(*(mockSourcePlugin_), GetDuration(testing::_)).WillRepeatedly(Return(Status::OK));
569 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
570 source_->GetDuration();
571 }
572
573 /**
574 * @tc.name Test SelectStream API
575 * @tc.number SelectStream_001
576 * @tc.desc Test SelectStream SUCCESSED
577 */
578 HWTEST_F(SourceTest, SelectStream_001, TestSize.Level1)
579 {
580 EXPECT_CALL(*(mockSourcePlugin_), SelectStream(testing::_)).WillRepeatedly(Return(Status::OK));
581 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
582 int32_t streamID = 0;
583 Status ret = source_->SelectStream(streamID);
584 EXPECT_EQ(ret, Status::OK);
585 }
586
587 /**
588 * @tc.name Test GetSegmentOffset API
589 * @tc.number GetSegmentOffset_001
590 * @tc.desc Test GetSegmentOffset SUCCESSED
591 */
592 HWTEST_F(SourceTest, GetSegmentOffset_001, TestSize.Level1)
593 {
594 EXPECT_CALL(*(mockSourcePlugin_), GetSegmentOffset()).WillRepeatedly(Return(0));
595 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
596 size_t ret = source_->GetSegmentOffset();
597 EXPECT_EQ(ret, 0);
598 }
599
600 /**
601 * @tc.name Test GetCachedDuration API
602 * @tc.number GetCachedDuration_001
603 * @tc.desc Test GetCachedDuration SUCCESSED
604 */
605 HWTEST_F(SourceTest, GetCachedDuration_001, TestSize.Level1)
606 {
607 EXPECT_CALL(*(mockSourcePlugin_), GetCachedDuration()).WillRepeatedly(Return(0));
608 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
609 uint64_t ret = source_->GetCachedDuration();
610 EXPECT_EQ(ret, 0);
611 }
612
613 /**
614 * @tc.name Test RestartAndClearBuffer API
615 * @tc.number RestartAndClearBuffer_001
616 * @tc.desc Test RestartAndClearBuffer SUCCESSED
617 */
618 HWTEST_F(SourceTest, RestartAndClearBuffer_001, TestSize.Level1)
619 {
620 EXPECT_CALL(*(mockSourcePlugin_), RestartAndClearBuffer()).Times(TEST_TIMES_ONE);
621 EXPECT_CALL(*(mockSourcePlugin_), Deinit()).WillOnce(Return(Status::OK));
622 source_->RestartAndClearBuffer();
623 }
624 } // namespace Media
625 } // namespace OHOS
626