• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "dash_segment_downloader_unittest.h"
17 #include "http_server_demo.h"
18 
19 namespace OHOS {
20 namespace Media {
21 namespace Plugins {
22 namespace HttpPlugin {
23 namespace {
24 constexpr int32_t SERVERPORT = 47777;
25 static const std::string AUDIO_SEGMENT_URL = "http://127.0.0.1:47777/test_dash/segment_base/media-audio-und-mp4a.mp4";
26 
27 constexpr uint32_t DEFAULT_RING_BUFFER_SIZE = 5 * 1024 * 1024;
28 constexpr uint32_t AUD_RING_BUFFER_SIZE = 2 * 1024 * 1024;
29 constexpr uint32_t RECORD_TIME_INTERVAL = 1000;
30 constexpr uint32_t SPEED_MULTI_FACT = 1000;
31 constexpr uint32_t BYTE_TO_BIT = 8;
32 constexpr uint32_t TIMES_ONE = 1;
33 constexpr uint32_t ID_STREAM = 1;
34 constexpr uint32_t NUM_TEST = 10;
35 constexpr size_t MAX_BUFFERING_TIME_OUT = 30 * 1000;
36 }
37 using namespace testing::ext;
38 
39 std::unique_ptr<MediaAVCodec::HttpServerDemo> g_server = nullptr;
SetUpTestCase(void)40 void DashSegmentDownloaderUnitTest::SetUpTestCase(void)
41 {
42     g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
43     g_server->StartServer(SERVERPORT);
44     std::cout << "start" << std::endl;
45 }
46 
TearDownTestCase(void)47 void DashSegmentDownloaderUnitTest::TearDownTestCase(void)
48 {
49     g_server->StopServer();
50     g_server = nullptr;
51 }
52 
SetUp(void)53 void DashSegmentDownloaderUnitTest::SetUp(void)
54 {
55     if (g_server == nullptr) {
56         g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
57         g_server->StartServer(SERVERPORT);
58         std::cout << "start server" << std::endl;
59     }
60     segmentDownloader_ = std::make_shared<DashSegmentDownloader>(nullptr, ID_STREAM,
61             MediaAVCodec::MediaType::MEDIA_TYPE_AUD, NUM_TEST, nullptr);
62 }
63 
TearDown(void)64 void DashSegmentDownloaderUnitTest::TearDown(void)
65 {
66     segmentDownloader_ = nullptr;
67 }
68 
69 /**
70  * @tc.name  : Test Open
71  * @tc.number: Open_001
72  * @tc.desc  : Test Open mediaSegment_->byteRange_.length() > 0
73  *             Test Open mediaSegment_->startRangeValue_ >= 0
74  *             && mediaSegment_->endRangeValue_ < mediaSegment_->startRangeValue_
75  */
76 HWTEST_F(DashSegmentDownloaderUnitTest, Open_001, TestSize.Level1)
77 {
78     std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
79     segmentSp->url_ = AUDIO_SEGMENT_URL;
80     segmentSp->streamId_ = 1;
81     segmentSp->duration_ = 5;
82     segmentSp->bandwidth_ = 1024;
83     segmentSp->startNumberSeq_ = 1;
84     segmentSp->numberSeq_ = 1;
85     segmentSp->byteRange_ = "1024-0";
86     segmentDownloader_->Open(segmentSp);
87     segmentDownloader_->Close(true, true);
88     EXPECT_EQ(segmentDownloader_->mediaSegment_->startRangeValue_, 1024);
89     EXPECT_EQ(segmentDownloader_->mediaSegment_->endRangeValue_, 0);
90 }
91 
92 /**
93  * @tc.name  : Test Open
94  * @tc.number: Open_002
95  * @tc.desc  : Test Open initSegment->isDownloadFinish_ == true
96  *             Test ~DashSegmentDownloader buffer_ == nullptr
97  *             Test ~DashSegmentDownloader downloader_ == nullptr
98  */
99 HWTEST_F(DashSegmentDownloaderUnitTest, Open_002, TestSize.Level1)
100 {
101     // Test Open initSegment->isDownloadFinish_ == true
102     std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
103     segmentSp->url_ = AUDIO_SEGMENT_URL;
104     segmentSp->streamId_ = ID_STREAM;
105     segmentSp->duration_ = 5;
106     segmentSp->bandwidth_ = 1024;
107     segmentSp->startNumberSeq_ = 1;
108     segmentSp->numberSeq_ = 1;
109     std::shared_ptr<DashInitSegment> initSegment = std::make_shared<DashInitSegment>();
110     initSegment->streamId_ = ID_STREAM;
111     segmentDownloader_->SetInitSegment(initSegment);
112     initSegment->writeState_ = INIT_SEGMENT_STATE_UNUSE;
113     initSegment->isDownloadFinish_ = true;
114     segmentDownloader_->Open(segmentSp);
115     segmentDownloader_->Close(true, true);
116 
117     // Test ~DashSegmentDownloader buffer_ == nullptr
118     segmentDownloader_->buffer_->SetActive(false, true);
119     segmentDownloader_->buffer_.reset();
120 
121     // Test ~DashSegmentDownloader downloader_ == nullptr
122     segmentDownloader_->downloader_->Stop(false);
123     segmentDownloader_->downloader_.reset();
124 
125     EXPECT_EQ(initSegment->writeState_, INIT_SEGMENT_STATE_USED);
126 }
127 
128 /**
129  * @tc.name  : Test Read
130  * @tc.number: Read_001
131  * @tc.desc  : Test Read currentSegment == nullptr
132  */
133 HWTEST_F(DashSegmentDownloaderUnitTest, Read_001, TestSize.Level1)
134 {
135     ReadDataInfo readDataInfo;
136     readDataInfo.streamId_ = 1;
137     readDataInfo.nextStreamId_ = 1;
138     readDataInfo.wantReadLength_ = 1024;
139     readDataInfo.realReadLength_ = 0;
140     uint8_t buffer[1024];
141     std::atomic<bool> isInterruptNeeded = false;
142 
143     std::shared_ptr<DashInitSegment> initSegment = std::make_shared<DashInitSegment>();
144     initSegment->streamId_ = 1;
145     initSegment->isDownloadFinish_ = true;
146     initSegment->readState_ = INIT_SEGMENT_STATE_UNUSE;
147     segmentDownloader_->initSegments_.push_back(initSegment);
148 
149     DashReadRet result = segmentDownloader_->Read(buffer, readDataInfo, isInterruptNeeded);
150     segmentDownloader_->Close(true, true);
151     EXPECT_EQ(result, DASH_READ_OK);
152 }
153 
154 /**
155  * @tc.name  : Test GetMaxReadLength
156  * @tc.number: GetMaxReadLength_001
157  * @tc.desc  : Test GetMaxReadLength currentSegment == nullptr
158  */
159 HWTEST_F(DashSegmentDownloaderUnitTest, GetMaxReadLength_001, TestSize.Level1)
160 {
161     uint32_t wantReadLength = 1024;
162     int32_t currentStreamId = 1;
163     uint32_t len = segmentDownloader_->GetMaxReadLength(wantReadLength, nullptr, currentStreamId);
164     EXPECT_EQ(len, wantReadLength);
165 }
166 
167 /**
168  * @tc.name  : Test GetMaxReadLength
169  * @tc.number: GetMaxReadLength_002
170  * @tc.desc  : Test GetMaxReadLength availableSize <= 0
171  */
172 HWTEST_F(DashSegmentDownloaderUnitTest, GetMaxReadLength_002, TestSize.Level1)
173 {
174     uint32_t wantReadLength = 1024;
175     int32_t currentStreamId = 1;
176     std::shared_ptr<DashBufferSegment> currentSegment = std::make_shared<DashBufferSegment>();
177     currentSegment->bufferPosTail_ = 0;
178     uint32_t len = segmentDownloader_->GetMaxReadLength(wantReadLength, currentSegment, currentStreamId);
179     EXPECT_EQ(len, wantReadLength);
180 }
181 
182 /**
183  * @tc.name  : Test IsSegmentFinished
184  * @tc.number: IsSegmentFinished_001
185  * @tc.desc  : Test IsSegmentFinished buffer_->GetSize() != 0
186  */
187 HWTEST_F(DashSegmentDownloaderUnitTest, IsSegmentFinished_001, TestSize.Level1)
188 {
189     segmentDownloader_->isAllSegmentFinished_ = true;
190     segmentDownloader_->buffer_->head_ = 0;
191     segmentDownloader_->buffer_->tail_ = 1024;
192     uint32_t realReadLength = 0;
193     DashReadRet readRet = DASH_READ_OK;
194     bool ret = segmentDownloader_->IsSegmentFinished(realReadLength, readRet);
195     EXPECT_TRUE(!ret);
196 }
197 
198 /**
199  * @tc.name  : Test IsSegmentFinished
200  * @tc.number: IsSegmentFinished_002
201  * @tc.desc  : Test IsSegmentFinished mediaSegment_ == nullptr
202  */
203 HWTEST_F(DashSegmentDownloaderUnitTest, IsSegmentFinished_002, TestSize.Level1)
204 {
205     segmentDownloader_->isAllSegmentFinished_ = true;
206     segmentDownloader_->buffer_->head_ = 0;
207     segmentDownloader_->buffer_->tail_ = 0;
208     segmentDownloader_->mediaSegment_ = nullptr;
209     uint32_t realReadLength = 0;
210     DashReadRet readRet = DASH_READ_OK;
211     segmentDownloader_->IsSegmentFinished(realReadLength, readRet);
212     EXPECT_EQ(readRet, DASH_READ_END);
213 }
214 
215 /**
216  * @tc.name  : Test CheckReadInterrupt
217  * @tc.number: CheckReadInterrupt_001
218  * @tc.desc  : Test CheckReadInterrupt HandleCache() == false
219  */
220 HWTEST_F(DashSegmentDownloaderUnitTest, CheckReadInterrupt_001, TestSize.Level1)
221 {
222     segmentDownloader_->isAllSegmentFinished_ = false;
223     segmentDownloader_->isBuffering_ = false;
224     segmentDownloader_->isFirstFrameArrived_ = false;
225 
226     uint32_t realReadLength = 0;
227     uint32_t wantReadLength = 0;
228     DashReadRet readRet = DASH_READ_OK;
229     std::atomic<bool> isInterruptNeeded = true;
230     segmentDownloader_->CheckReadInterrupt(realReadLength, wantReadLength, readRet, isInterruptNeeded);
231     EXPECT_EQ(readRet, DASH_READ_INTERRUPT);
232 }
233 
234 /**
235  * @tc.name  : Test HandleBuffering
236  * @tc.number: HandleBuffering_001
237  * @tc.desc  : Test HandleBuffering isInterruptNeeded.load() == true
238  */
239 HWTEST_F(DashSegmentDownloaderUnitTest, HandleBuffering_001, TestSize.Level1)
240 {
241     segmentDownloader_->isBuffering_ = true;
242     std::atomic<bool> isInterruptNeeded = true;
243     bool ret = segmentDownloader_->HandleBuffering(isInterruptNeeded);
244     EXPECT_TRUE(ret);
245 }
246 
247 /**
248  * @tc.name  : Test HandleBuffering
249  * @tc.number: HandleBuffering_002
250  * @tc.desc  : Test HandleBuffering isAllSegmentFinished_.load() == true
251  *             Test SaveDataHandleBuffering isInterruptNeeded.load() == true
252  */
253 HWTEST_F(DashSegmentDownloaderUnitTest, HandleBuffering_002, TestSize.Level1)
254 {
255     segmentDownloader_->isBuffering_ = true;
256     segmentDownloader_->isAllSegmentFinished_.store(true);
257 
258     // Test SaveDataHandleBuffering isInterruptNeeded.load() == true
259     segmentDownloader_->bufferDurationForPlaying_ = 0;
260     segmentDownloader_->SaveDataHandleBuffering();
261 
262     // Test HandleBuffering isAllSegmentFinished_.load() == true
263     std::atomic<bool> isInterruptNeeded = false;
264     bool ret = segmentDownloader_->HandleBuffering(isInterruptNeeded);
265     EXPECT_TRUE(!ret);
266 }
267 
268 /**
269  * @tc.name  : Test HandleCache
270  * @tc.number: HandleCache_001
271  * @tc.desc  : Test HandleCache isBuffering_.exchange(true) = true
272  *             Test DoBufferingEndEvent isBuffering_.exchange(false) = false
273  */
274 HWTEST_F(DashSegmentDownloaderUnitTest, SaveDataHandleBuffering_001, TestSize.Level1)
275 {
276     // Test DoBufferingEndEvent isBuffering_.exchange(false) = false
277     segmentDownloader_->isBuffering_ = false;
278     segmentDownloader_->DoBufferingEndEvent();
279 
280     // Test HandleCache isBuffering_.exchange(true) = true
281     segmentDownloader_->isBuffering_ = true;
282     bool ret = segmentDownloader_->HandleCache();
283     EXPECT_TRUE(!ret);
284 }
285 
286 /**
287  * @tc.name  : Test GetWaterLineAbove
288  * @tc.number: GetWaterLineAbove_001
289  * @tc.desc  : Test GetWaterLineAbove downloadBiteRate_ != 0
290  *             Test GetWaterLineAbove realTimeBitBate_ > static_cast<int64_t>(downloadBiteRate_)
291  *             Test GetWaterLineAbove realTimeBitBate_ <= static_cast<int64_t>(downloadBiteRate_)
292  */
293 HWTEST_F(DashSegmentDownloaderUnitTest, GetWaterLineAbove_001, TestSize.Level1)
294 {
295     // Test GetWaterLineAbove realTimeBitBate_ > static_cast<int64_t>(downloadBiteRate_)
296     segmentDownloader_->downloadRequest_ = std::make_shared<DownloadRequest>("test_request", nullptr, nullptr);
297     segmentDownloader_->realTimeBitBate_ = 1024 * 1024;
298     segmentDownloader_->downloadBiteRate_ = 1024;
299 
300     int32_t waterLineAbove = segmentDownloader_->GetWaterLineAbove();
301     EXPECT_EQ(waterLineAbove, 1024 * 1024);
302 
303     // Test GetWaterLineAbove realTimeBitBate_ <= static_cast<int64_t>(downloadBiteRate_)
304     segmentDownloader_->downloadBiteRate_ = 1024 * 1024 * 2;
305     waterLineAbove = segmentDownloader_->GetWaterLineAbove();
306     EXPECT_EQ(waterLineAbove, 39321);
307 }
308 
309 /**
310  * @tc.name  : Test GetCachedPercent
311  * @tc.number: GetCachedPercent_001
312  * @tc.desc  : Test GetCachedPercent waterLineAbove_ == 0
313  *             Test GetCachedPercent waterLineAbove_ != 0
314  *             Test CalculateBitRate fragmentSize == 0
315  *             Test CalculateBitRate duration == 0
316  */
317 HWTEST_F(DashSegmentDownloaderUnitTest, GetCachedPercent_001, TestSize.Level1)
318 {
319     // Test CalculateBitRate fragmentSize == 0
320     size_t fragmentSize = 0;
321     double duration = 1.0;
322     segmentDownloader_->CalculateBitRate(fragmentSize, duration);
323 
324     // Test CalculateBitRate duration == 0
325     fragmentSize = 1024;
326     duration = 0.0;
327     segmentDownloader_->CalculateBitRate(fragmentSize, duration);
328 
329     // Test GetCachedPercent waterLineAbove_ == 0
330     segmentDownloader_->waterLineAbove_ = 0;
331     uint32_t cachedPercent = segmentDownloader_->GetCachedPercent();
332     EXPECT_EQ(cachedPercent, 0);
333 
334     // Test GetCachedPercent waterLineAbove_ != 0
335     segmentDownloader_->waterLineAbove_ = 100;
336     segmentDownloader_->buffer_->head_ = 0;
337     segmentDownloader_->buffer_->tail_ = 100;
338     cachedPercent = segmentDownloader_->GetCachedPercent();
339     EXPECT_EQ(cachedPercent, 100);
340 }
341 
342 /**
343  * @tc.name  : Test UpdateCachedPercent
344  * @tc.number: UpdateCachedPercent_001
345  * @tc.desc  : Test UpdateCachedPercent (waterLineAbove_ == 0 || bufferingCbFunc_ == nullptr) = false
346  *             Test UpdateCachedPercent infoType == BufferingInfoType::BUFFERING_START
347  *             Test UpdateCachedPercent infoType == BufferingInfoType::BUFFERING_END
348  *             Test UpdateCachedPercent infoType == BufferingInfoType::BUFFERING_PERCENT
349  *             Test UpdateCachedPercent infoType == BufferingInfoType::CACHED_DURATION
350  */
351 HWTEST_F(DashSegmentDownloaderUnitTest, UpdateCachedPercent_001, TestSize.Level1)
352 {
353     // Test (waterLineAbove_ == 0 || bufferingCbFunc_ == nullptr) = false
354     segmentDownloader_->waterLineAbove_ = 1;
__anona6cd4aaa0202(uint32_t percent, BufferingInfoType infoType) 355     segmentDownloader_->bufferingCbFunc_ = [] (uint32_t percent, BufferingInfoType infoType) {};
356 
357     // Test infoType == BufferingInfoType::BUFFERING_START
358     segmentDownloader_->lastCachedSize_ = 1024;
359     segmentDownloader_->UpdateCachedPercent(BufferingInfoType::BUFFERING_START);
360     EXPECT_EQ(segmentDownloader_->lastCachedSize_, 0);
361 
362     // Test infoType == BufferingInfoType::BUFFERING_END
363     segmentDownloader_->lastCachedSize_ = 1024;
364     segmentDownloader_->UpdateCachedPercent(BufferingInfoType::BUFFERING_END);
365     EXPECT_EQ(segmentDownloader_->lastCachedSize_, 0);
366 
367     // Test infoType == BufferingInfoType::BUFFERING_PERCENT
368     segmentDownloader_->lastCachedSize_ = 1024;
369     segmentDownloader_->UpdateCachedPercent(BufferingInfoType::BUFFERING_PERCENT);
370     EXPECT_EQ(segmentDownloader_->lastCachedSize_, 1024);
371 
372     // Test infoType == BufferingInfoType::CACHED_DURATION
373     segmentDownloader_->UpdateCachedPercent(BufferingInfoType::CACHED_DURATION);
374     EXPECT_EQ(segmentDownloader_->lastCachedSize_, 1024);
375 }
376 
377 /**
378  * @tc.name  : Test UpdateCachedPercent
379  * @tc.number: UpdateCachedPercent_002
380  * @tc.desc  : Test UpdateCachedPercent (deltaSize * BUFFERING_PERCENT_FULL / waterLineAbove_) >= UPDATE_CACHE_STEP
381  *             Test UpdateCachedPercent (deltaSize * BUFFERING_PERCENT_FULL / waterLineAbove_) < UPDATE_CACHE_STEP
382  */
383 HWTEST_F(DashSegmentDownloaderUnitTest, UpdateCachedPercent_002, TestSize.Level1)
384 {
385     segmentDownloader_->waterLineAbove_ = 1;
__anona6cd4aaa0302(uint32_t percent, BufferingInfoType infoType) 386     segmentDownloader_->bufferingCbFunc_ = [] (uint32_t percent, BufferingInfoType infoType) {};
387 
388     // Test (deltaSize * BUFFERING_PERCENT_FULL / waterLineAbove_) >= UPDATE_CACHE_STEP
389     segmentDownloader_->lastCachedSize_ = 1024;
390     segmentDownloader_->buffer_->head_ = 0;
391     segmentDownloader_->buffer_->tail_ = 1024 * 2;
392     segmentDownloader_->UpdateCachedPercent(BufferingInfoType::BUFFERING_PERCENT);
393     EXPECT_EQ(segmentDownloader_->lastCachedSize_, 1024 * 2);
394 
395     // Test (deltaSize * BUFFERING_PERCENT_FULL / waterLineAbove_) < UPDATE_CACHE_STEP
396     segmentDownloader_->UpdateCachedPercent(BufferingInfoType::BUFFERING_PERCENT);
397     EXPECT_EQ(segmentDownloader_->lastCachedSize_, 1024 * 2);
398 }
399 
400 /**
401  * @tc.name  : Test GetCurrentSegment
402  * @tc.number: GetCurrentSegment_001
403  * @tc.desc  : Test GetCurrentSegment it == segmentList_.end()
404  */
405 HWTEST_F(DashSegmentDownloaderUnitTest, GetCurrentSegment_001, TestSize.Level1)
406 {
407     segmentDownloader_->segmentList_.clear();
408     std::shared_ptr<DashBufferSegment> item = std::make_shared<DashBufferSegment>();
409     item->bufferPosHead_ = 1;
410     item->bufferPosTail_ = 1024;
411     segmentDownloader_->segmentList_.emplace_back(item);
412     segmentDownloader_->buffer_->head_ = 0;
413     EXPECT_EQ(segmentDownloader_->GetCurrentSegment(), nullptr);
414 }
415 
416 /**
417  * @tc.name  : Test UpdateInitSegmentState
418  * @tc.number: UpdateInitSegmentState_001
419  * @tc.desc  : Test UpdateInitSegmentState (*it)->streamId_ != currentStreamId
420  */
421 HWTEST_F(DashSegmentDownloaderUnitTest, UpdateInitSegmentState_001, TestSize.Level1)
422 {
423     std::shared_ptr<DashInitSegment> item = std::make_shared<DashInitSegment>();
424     segmentDownloader_->initSegments_.emplace_back(item);
425     int32_t currentStreamId = 1;
426     segmentDownloader_->UpdateInitSegmentState(currentStreamId);
427     EXPECT_EQ(item->readState_, INIT_SEGMENT_STATE_UNUSE);
428 }
429 
430 /**
431  * @tc.name  : Test ReadInitSegment
432  * @tc.number: ReadInitSegment_001
433  * @tc.desc  : Test ReadInitSegment initSegment->readIndex_ == contentLen && initSegment->isDownloadFinish_
434  */
435 HWTEST_F(DashSegmentDownloaderUnitTest, ReadInitSegment_001, TestSize.Level1)
436 {
437     std::shared_ptr<DashInitSegment> item = std::make_shared<DashInitSegment>();
438     item->streamId_ = 1;
439     item->readState_ = INIT_SEGMENT_STATE_UNUSE;
440     item->isDownloadFinish_ = true;
441     segmentDownloader_->initSegments_.emplace_back(item);
442 
443     uint8_t buff[100] = {0};
444     uint32_t wantReadLength = 100;
445     uint32_t realReadLength = 1;
446     int32_t currentStreamId = 1;
447     segmentDownloader_->ReadInitSegment(buff, wantReadLength, realReadLength, currentStreamId);
448     EXPECT_EQ(item->readState_, INIT_SEGMENT_STATE_USED);
449 }
450 
451 /**
452  * @tc.name  : Test ReadInitSegment
453  * @tc.number: ReadInitSegment_002
454  * @tc.desc  : Test ReadInitSegment initSegment->readIndex_ == contentLen && initSegment->isDownloadFinish_
455  *             when unReadSize > 0
456  */
457 HWTEST_F(DashSegmentDownloaderUnitTest, ReadInitSegment_002, TestSize.Level1)
458 {
459     std::shared_ptr<DashInitSegment> item = std::make_shared<DashInitSegment>();
460     item->streamId_ = 1;
461     item->readState_ = INIT_SEGMENT_STATE_UNUSE;
462     item->isDownloadFinish_ = true;
463     item->content_ = "test_content";
464     segmentDownloader_->initSegments_.emplace_back(item);
465 
466     uint8_t buff[100] = {0};
467     uint32_t wantReadLength = 100;
468     uint32_t realReadLength = 1;
469     int32_t currentStreamId = 1;
470     segmentDownloader_->ReadInitSegment(buff, wantReadLength, realReadLength, currentStreamId);
471     EXPECT_EQ(item->readState_, INIT_SEGMENT_STATE_USED);
472 
473     item->isDownloadFinish_ = false;
474     item->readState_ = INIT_SEGMENT_STATE_UNUSE;
475     segmentDownloader_->ReadInitSegment(buff, wantReadLength, realReadLength, currentStreamId);
476     EXPECT_EQ(item->readState_, INIT_SEGMENT_STATE_UNUSE);
477 
478     item->readIndex_ = 12;
479     segmentDownloader_->ReadInitSegment(buff, wantReadLength, realReadLength, currentStreamId);
480     EXPECT_EQ(item->readState_, INIT_SEGMENT_STATE_UNUSE);
481 }
482 
483 /**
484  * @tc.name  : Test GetRingBufferInitSize
485  * @tc.number: GetRingBufferInitSize_001
486  * @tc.desc  : Test GetRingBufferInitSize ringBufferSizeItem == BUFFER_SIZE_MAP.end()
487  */
488 HWTEST_F(DashSegmentDownloaderUnitTest, GetRingBufferInitSize_001, TestSize.Level1)
489 {
490     MediaAVCodec::MediaType streamType = MediaAVCodec::MediaType::MEDIA_TYPE_TIMED_METADATA;
491     size_t bufferSize = segmentDownloader_->GetRingBufferInitSize(streamType);
492     EXPECT_EQ(bufferSize, DEFAULT_RING_BUFFER_SIZE);
493 }
494 
495 /**
496  * @tc.name  : Test GetRingBufferInitSize
497  * @tc.number: GetRingBufferInitSize_002
498  * @tc.desc  : Test GetRingBufferInitSize ringBufferSize < DEFAULT_RING_BUFFER_SIZE
499  *             Test SetInitSegment initSegment == nullptr
500  */
501 HWTEST_F(DashSegmentDownloaderUnitTest, GetRingBufferInitSize_002, TestSize.Level1)
502 {
503     // Test SetInitSegment initSegment == nullptr
504     std::shared_ptr<DashInitSegment> initSegment = nullptr;
505     bool needUpdateState = true;
506     segmentDownloader_->SetInitSegment(initSegment, needUpdateState);
507 
508     // Test GetRingBufferInitSize ringBufferSize < DEFAULT_RING_BUFFER_SIZE
509     MediaAVCodec::MediaType streamType = MediaAVCodec::MediaType::MEDIA_TYPE_VID;
510     segmentDownloader_->userDefinedBufferDuration_ = true;
511     segmentDownloader_->expectDuration_ = 1;
512     segmentDownloader_->currentBitrate_ = 1 * 1024 * 1024;
513     size_t bufferSize = segmentDownloader_->GetRingBufferInitSize(streamType);
514     EXPECT_EQ(bufferSize, DEFAULT_RING_BUFFER_SIZE);
515 }
516 
517 /**
518  * @tc.name  : Test GetContentLength
519  * @tc.number: GetContentLength_001
520  * @tc.desc  : Test GetContentLength (downloadRequest_ == nullptr || downloadRequest_->IsClosed()) == false
521  */
522 HWTEST_F(DashSegmentDownloaderUnitTest, SetInitSegment_001, TestSize.Level1)
523 {
524     segmentDownloader_->downloadRequest_ = std::make_shared<DownloadRequest>("test_request", nullptr, nullptr);
525     segmentDownloader_->downloadRequest_->headerInfo_.isClosed = false;
526     segmentDownloader_->downloadRequest_->headerInfo_.isChunked = true;
527     segmentDownloader_->downloadRequest_->isHeaderUpdated_ = true;
528     size_t fileContentLen = segmentDownloader_->GetContentLength();
529     EXPECT_EQ(fileContentLen, 0);
530 }
531 
532 /**
533  * @tc.name  : Test IsSegmentFinish
534  * @tc.number: IsSegmentFinish_001
535  * @tc.desc  : Test IsSegmentFinish (mediaSegment_ != nullptr && mediaSegment_->isEos_) == true
536  */
537 HWTEST_F(DashSegmentDownloaderUnitTest, IsSegmentFinish_001, TestSize.Level1)
538 {
539     std::shared_ptr<DashBufferSegment> mediaSegment = std::make_shared<DashBufferSegment>();
540     mediaSegment->isEos_ = true;
541     segmentDownloader_->mediaSegment_ = mediaSegment;
542     EXPECT_TRUE(segmentDownloader_->IsSegmentFinish());
543 }
544 
545 /**
546  * @tc.name  : Test CleanAllSegmentBuffer
547  * @tc.number: CleanAllSegmentBuffer_001
548  * @tc.desc  : Test CleanAllSegmentBuffer buffer_->GetHead() > it->bufferPosTail_
549  */
550 HWTEST_F(DashSegmentDownloaderUnitTest, CleanAllSegmentBuffer_001, TestSize.Level1)
551 {
552     bool isCleanAll = true;
553     int64_t remainLastNumberSeq = 1;
554     segmentDownloader_->buffer_->head_ = 1;
555     std::shared_ptr<DashBufferSegment> segment = std::make_shared<DashBufferSegment>();
556     segment->bufferPosTail_ = 0;
557     segmentDownloader_->segmentList_.emplace_back(segment);
558     segmentDownloader_->CleanAllSegmentBuffer(isCleanAll, remainLastNumberSeq);
559     EXPECT_EQ(remainLastNumberSeq, 1);
560 }
561 
562 /**
563  * @tc.name  : Test CleanSegmentBuffer
564  * @tc.number: CleanSegmentBuffer_001
565  * @tc.desc  : Test CleanSegmentBuffer buffer_->GetHead() > it->bufferPosTail_ && it->bufferPosTail_ > 0
566  */
567 HWTEST_F(DashSegmentDownloaderUnitTest, CleanSegmentBuffer_001, TestSize.Level1)
568 {
569     bool isCleanAll = false;
570     int64_t remainLastNumberSeq = 0;
571     segmentDownloader_->buffer_->head_ = 10;
572     std::shared_ptr<DashBufferSegment> segment = std::make_shared<DashBufferSegment>();
573     segment->bufferPosTail_ = 1;
574     segment->numberSeq_ = 1;
575     segmentDownloader_->segmentList_.emplace_back(segment);
576     segmentDownloader_->CleanSegmentBuffer(isCleanAll, remainLastNumberSeq);
577     EXPECT_EQ(remainLastNumberSeq, -1);
578 }
579 
580 /**
581  * @tc.name  : Test CleanByTimeInternal
582  * @tc.number: CleanByTimeInternal_001
583  * @tc.desc  : Test CleanByTimeInternal buffer_->GetHead() > it->bufferPosTail_
584  *             Test CleanByTimeInternal it->bufferPosHead_ >= it->bufferPosTail_
585  */
586 HWTEST_F(DashSegmentDownloaderUnitTest, CleanByTimeInternal_001, TestSize.Level1)
587 {
588     // Test CleanByTimeInternal buffer_->GetHead() > it->bufferPosTail_
589     segmentDownloader_->buffer_->head_ = 1;
590     auto segment1 = std::make_shared<DashBufferSegment>();
591     segment1->numberSeq_ = 1;
592     segmentDownloader_->segmentList_.emplace_back(segment1);
593 
594     // Test CleanByTimeInternal it->bufferPosHead_ >= it->bufferPosTail_
595     std::shared_ptr<DashBufferSegment> segment2 = std::make_shared<DashBufferSegment>();
596     segment2->bufferPosHead_ = 1;
597     segment2->numberSeq_ = 1;
598     segmentDownloader_->segmentList_.emplace_back(segment2);
599 
600     int64_t remainLastNumberSeq = 0;
601     size_t clearTail = 0;
602     bool isEnd = false;
603     segmentDownloader_->CleanByTimeInternal(remainLastNumberSeq, clearTail, isEnd);
604     EXPECT_EQ(remainLastNumberSeq, 0);
605 }
606 
607 /**
608  * @tc.name  : Test CleanByTimeInternal
609  * @tc.number: CleanByTimeInternal_002
610  * @tc.desc  : Test CleanByTimeInternal it->contentLength_ == 0
611  *             Test CleanByTimeInternal it->duration_ == 0
612  */
613 HWTEST_F(DashSegmentDownloaderUnitTest, CleanByTimeInternal_002, TestSize.Level1)
614 {
615     int64_t remainLastNumberSeq = 0;
616     size_t clearTail = 0;
617     bool isEnd = false;
618 
619     // Test CleanByTimeInternal it->contentLength_ == 0
620     segmentDownloader_->buffer_->head_ = 0;
621     std::shared_ptr<DashBufferSegment> segment1 = std::make_shared<DashBufferSegment>();
622     segment1->bufferPosTail_ = 1;
623     segment1->numberSeq_ = 1;
624     segment1->duration_ = 1;
625     segmentDownloader_->segmentList_.emplace_back(segment1);
626     segmentDownloader_->CleanByTimeInternal(remainLastNumberSeq, clearTail, isEnd);
627     EXPECT_EQ(clearTail, segment1->bufferPosTail_);
628 
629     // Test CleanByTimeInternal it->duration_ == 0
630     std::shared_ptr<DashBufferSegment> segment2 = std::make_shared<DashBufferSegment>();
631     segment2->bufferPosTail_ = 2;
632     segment2->numberSeq_ = 1;
633     segment2->contentLength_ = 1;
634     segmentDownloader_->segmentList_.clear();
635     segmentDownloader_->segmentList_.emplace_back(segment2);
636     segmentDownloader_->CleanByTimeInternal(remainLastNumberSeq, clearTail, isEnd);
637     EXPECT_EQ(clearTail, segment2->bufferPosTail_);
638 }
639 
640 /**
641  * @tc.name  : Test CleanByTimeInternal
642  * @tc.number: CleanByTimeInternal_003
643  * @tc.desc  : Test remainDuration < segmentKeepDuration + segmentKeepDelta &&
644                remainDuration + segmentKeepDelta >= segmentKeepDuration
645  */
646 HWTEST_F(DashSegmentDownloaderUnitTest, CleanByTimeInternal_003, TestSize.Level1)
647 {
648     int64_t remainLastNumberSeq = 0;
649     size_t clearTail = 0;
650     bool isEnd = false;
651     segmentDownloader_->buffer_->head_ = 0;
652     std::shared_ptr<DashBufferSegment> segment = std::make_shared<DashBufferSegment>();
653     segment->bufferPosTail_ = 1000;
654     segment->numberSeq_ = 1;
655     segment->contentLength_ = 1;
656     segment->duration_ = 1;
657     segmentDownloader_->segmentList_.emplace_back(segment);
658     segmentDownloader_->CleanByTimeInternal(remainLastNumberSeq, clearTail, isEnd);
659     EXPECT_EQ(clearTail, segment->bufferPosTail_);
660 }
661 
662 /**
663  * @tc.name  : Test CleanByTimeInternal
664  * @tc.number: CleanByTimeInternal_004
665  * @tc.desc  : Test CleanByTimeInternal remainDuration < segmentKeepDuration
666  */
667 HWTEST_F(DashSegmentDownloaderUnitTest, CleanByTimeInternal_004, TestSize.Level1)
668 {
669     int64_t remainLastNumberSeq = 0;
670     size_t clearTail = 0;
671     bool isEnd = false;
672     segmentDownloader_->buffer_->head_ = 0;
673     std::shared_ptr<DashBufferSegment> segment = std::make_shared<DashBufferSegment>();
674     segment->bufferPosTail_ = 800;
675     segment->numberSeq_ = 1;
676     segment->contentLength_ = 1;
677     segment->duration_ = 1;
678     segmentDownloader_->segmentList_.emplace_back(segment);
679     segmentDownloader_->CleanByTimeInternal(remainLastNumberSeq, clearTail, isEnd);
680     EXPECT_EQ(clearTail, segment->bufferPosTail_);
681 }
682 
683 /**
684  * @tc.name  : Test CleanByTimeInternal
685  * @tc.number: CleanByTimeInternal_005
686  * @tc.desc  : Test CleanByTimeInternal clearTail > 0
687  */
688 HWTEST_F(DashSegmentDownloaderUnitTest, CleanByTimeInternal_005, TestSize.Level1)
689 {
690     int64_t remainLastNumberSeq = 0;
691     size_t clearTail = 1;
692     bool isEnd = false;
693     segmentDownloader_->buffer_->head_ = 0;
694     std::shared_ptr<DashBufferSegment> segment = std::make_shared<DashBufferSegment>();
695     segment->bufferPosTail_ = 1100;
696     segment->numberSeq_ = 1;
697     segment->contentLength_ = 1;
698     segment->duration_ = 1;
699     segmentDownloader_->segmentList_.emplace_back(segment);
700     segmentDownloader_->CleanByTimeInternal(remainLastNumberSeq, clearTail, isEnd);
701     EXPECT_EQ(clearTail, 1001);
702 }
703 
704 /**
705  * @tc.name  : Test SaveData
706  * @tc.number: SaveData_001
707  * @tc.desc  : Test SaveData freeSize == 0
708  *             Test SaveData freeSize != 0
709  */
710 HWTEST_F(DashSegmentDownloaderUnitTest, SaveData_001, TestSize.Level1)
711 {
712     // Test SaveData freeSize == 0
713     segmentDownloader_->buffer_->tail_ = AUD_RING_BUFFER_SIZE;
714     segmentDownloader_->buffer_->head_ = 0;
715     uint8_t data[2 * 1024 * 1024] = {0};
716     uint32_t len = AUD_RING_BUFFER_SIZE;
717     bool notBlock = true;
718     uint32_t dataSize = segmentDownloader_->SaveData(data, len, notBlock);
719     EXPECT_EQ(dataSize, 0);
720 
721     // Test SaveData freeSize != 0
722     segmentDownloader_->buffer_->tail_ = 0;
723     dataSize = segmentDownloader_->SaveData(data, len, notBlock);
724     EXPECT_EQ(dataSize, AUD_RING_BUFFER_SIZE);
725 }
726 
727 /**
728  * @tc.name  : Test SaveData
729  * @tc.number: SaveData_002
730  * @tc.desc  : Test SaveData (notBlock && len < freeSize) == true
731  *             Test SaveData (notBlock && len < freeSize) == false
732  */
733 HWTEST_F(DashSegmentDownloaderUnitTest, SaveData_002, TestSize.Level1)
734 {
735     // Test SaveData (notBlock && len < freeSize) == true
736     segmentDownloader_->buffer_->tail_ = 0;
737     segmentDownloader_->buffer_->head_ = 0;
738     uint8_t data[2 * 1024 * 1024] = {0};
739     uint32_t len = 0;
740     bool notBlock = true;
741     segmentDownloader_->SaveData(data, len, notBlock);
742     EXPECT_TRUE(segmentDownloader_->canWrite_);
743 
744     // Test SaveData (notBlock && len < freeSize) == false
745     notBlock = false;
746     segmentDownloader_->canWrite_ = false;
747     segmentDownloader_->SaveData(data, len, notBlock);
748     EXPECT_TRUE(!segmentDownloader_->canWrite_);
749 
750     len = AUD_RING_BUFFER_SIZE;
751     segmentDownloader_->SaveData(data, len, notBlock);
752     EXPECT_TRUE(!segmentDownloader_->canWrite_);
753 
754     notBlock = true;
755     segmentDownloader_->SaveData(data, len, notBlock);
756     EXPECT_TRUE(!segmentDownloader_->canWrite_);
757 }
758 
759 /**
760  * @tc.name  : Test UpdateBufferSegment
761  * @tc.number: UpdateBufferSegment_001
762  * @tc.desc  : Test UpdateBufferSegment mediaSegment->contentLength_ == 0
763  */
764 HWTEST_F(DashSegmentDownloaderUnitTest, UpdateBufferSegment_001, TestSize.Level1)
765 {
766     std::shared_ptr<DashBufferSegment> mediaSegment = std::make_shared<DashBufferSegment>();
767     mediaSegment->bufferPosTail_ = 1024;
768     uint32_t len = 0;
769     segmentDownloader_->UpdateBufferSegment(mediaSegment, len);
770     EXPECT_EQ(mediaSegment->contentLength_, 1024);
771 }
772 
773 /**
774  * @tc.name  : Test OnWriteRingBuffer
775  * @tc.number: OnWriteRingBuffer_001
776  * @tc.desc  : Test OnWriteRingBuffer now > lastCheckTime_ && now - lastCheckTime_ > RECORD_TIME_INTERVAL
777  *             Test OnWriteRingBuffer curDownloadBits < RECORD_DOWNLOAD_MIN_BIT
778  */
779 HWTEST_F(DashSegmentDownloaderUnitTest, OnWriteRingBuffer_001, TestSize.Level1)
780 {
781     segmentDownloader_->steadyClock_.Reset();
782     std::this_thread::sleep_for(std::chrono::milliseconds(RECORD_TIME_INTERVAL + 100));
783     uint32_t len = 1;
784     segmentDownloader_->OnWriteRingBuffer(len);
785     EXPECT_EQ(segmentDownloader_->lastBits_, BYTE_TO_BIT);
786 }
787 
788 /**
789  * @tc.name  : Test OnWriteRingBuffer
790  * @tc.number: OnWriteRingBuffer_002
791  * @tc.desc  : Test OnWriteRingBuffer curDownloadBits >= RECORD_DOWNLOAD_MIN_BIT
792  *             Test OnWriteRingBuffer recordData_ == nullptr
793  */
794 HWTEST_F(DashSegmentDownloaderUnitTest, OnWriteRingBuffer_002, TestSize.Level1)
795 {
796     segmentDownloader_->steadyClock_.Reset();
797     std::this_thread::sleep_for(std::chrono::milliseconds(RECORD_TIME_INTERVAL + 100));
798     segmentDownloader_->buffer_.reset();
799     uint32_t len = 1000;
800     segmentDownloader_->OnWriteRingBuffer(len);
801     EXPECT_EQ(segmentDownloader_->downloadBits_, len * BYTE_TO_BIT);
802 }
803 
804 /**
805  * @tc.name  : Test OnWriteRingBuffer
806  * @tc.number: OnWriteRingBuffer_003
807  * @tc.desc  : Test OnWriteRingBuffer recordData_ != nullptr
808  */
809 HWTEST_F(DashSegmentDownloaderUnitTest, OnWriteRingBuffer_003, TestSize.Level1)
810 {
811     segmentDownloader_->steadyClock_.Reset();
812     std::this_thread::sleep_for(std::chrono::milliseconds(RECORD_TIME_INTERVAL + 100));
813     segmentDownloader_->realTimeBitBate_ = 8;
814     segmentDownloader_->recordData_ = std::make_shared<DashSegmentDownloader::RecordData>();
815     segmentDownloader_->buffer_->tail_ = 1;
816     uint32_t len = 1000;
817     segmentDownloader_->OnWriteRingBuffer(len);
818     EXPECT_EQ(segmentDownloader_->recordData_->bufferDuring, 1);
819 }
820 
821 /**
822  * @tc.name  : Test GetIp
823  * @tc.number: GetIp_001
824  * @tc.desc  : Test GetIp downloader_ = nullptr
825  */
826 HWTEST_F(DashSegmentDownloaderUnitTest, GetIp_001, TestSize.Level1)
827 {
828     segmentDownloader_->downloader_.reset();
829     std::string ip = "";
830     segmentDownloader_->GetIp(ip);
831     EXPECT_EQ(ip, "");
832 }
833 
834 /**
835  * @tc.name  : Test GetDownloadFinishState
836  * @tc.number: GetDownloadFinishState_001
837  * @tc.desc  : Test GetDownloadFinishState finishState == false
838  */
839 HWTEST_F(DashSegmentDownloaderUnitTest, GetDownloadFinishState_001, TestSize.Level1)
840 {
841     segmentDownloader_->initSegments_.clear();
842     bool retResult = segmentDownloader_->GetDownloadFinishState();
843     EXPECT_TRUE(retResult);
844 }
845 
846 /**
847  * @tc.name  : Test GetDownloadRecordData
848  * @tc.number: GetDownloadRecordData_001
849  * @tc.desc  : Test GetDownloadRecordData recordData_ == nullptr
850  *             Test GetDownloadRecordData recordData_ != nullptr
851  */
852 HWTEST_F(DashSegmentDownloaderUnitTest, GetDownloadRecordData_001, TestSize.Level1)
853 {
854     // Test GetDownloadRecordData recordData_ == nullptr
855     segmentDownloader_->recordData_ = nullptr;
856     std::pair<int64_t, int64_t> pair = segmentDownloader_->GetDownloadRecordData();
857     EXPECT_EQ(pair.first, 0);
858 
859     // Test GetDownloadRecordData recordData_ != nullptr
860     auto recordData = std::make_shared<DashSegmentDownloader::RecordData>();
861     recordData->downloadRate = 8;
862     segmentDownloader_->recordData_ = recordData;
863     pair = segmentDownloader_->GetDownloadRecordData();
864     EXPECT_EQ(pair.first, 8);
865 }
866 
867 /**
868  * @tc.name  : Test GetBufferSize
869  * @tc.number: GetBufferSize_001
870  * @tc.desc  : Test GetBufferSize buffer_ == nullptr
871  */
872 HWTEST_F(DashSegmentDownloaderUnitTest, GetBufferSize_001, TestSize.Level1)
873 {
874     segmentDownloader_->buffer_.reset();
875     uint32_t size = segmentDownloader_->GetBufferSize();
876     EXPECT_EQ(size, 0);
877 }
878 
879 /**
880  * @tc.name  : Test PutRequestIntoDownloader
881  * @tc.number: PutRequestIntoDownloader_001
882  * @tc.desc  : Test PutRequestIntoDownloader downloader_ == nullptr
883  */
884 HWTEST_F(DashSegmentDownloaderUnitTest, PutRequestIntoDownloader_001, TestSize.Level1)
885 {
886     unsigned int duration = 10;
887     int64_t startPos = 0;
888     int64_t endPos = 0;
889     std::string url = "";
890     segmentDownloader_->downloader_.reset();
891     segmentDownloader_->PutRequestIntoDownloader(duration, startPos, endPos, url);
892     EXPECT_TRUE(!segmentDownloader_->isCleaningBuffer_);
893 }
894 
895 /**
896  * @tc.name  : Test UpdateDownloadFinished
897  * @tc.number: UpdateDownloadFinished_001
898  * @tc.desc  : Test UpdateDownloadFinished totalDownloadDuringTime_ > 0
899  *             Test UpdateDownloadFinished mediaSegment_ == nullptr
900  */
901 HWTEST_F(DashSegmentDownloaderUnitTest, UpdateDownloadFinished_001, TestSize.Level1)
902 {
903     // Test totalDownloadDuringTime_ > 0
904     segmentDownloader_->totalDownloadDuringTime_ = 1;
905     segmentDownloader_->downloadBits_ = 1;
906 
907     // Test mediaSegment_ == nullptr
908     segmentDownloader_->mediaSegment_.reset();
909 
910     segmentDownloader_->UpdateDownloadFinished("", "");
911     EXPECT_EQ(segmentDownloader_->downloadSpeed_, SPEED_MULTI_FACT);
912 }
913 
914 /**
915  * @tc.name  : Test UpdateDownloadFinished
916  * @tc.number: UpdateDownloadFinished_002
917  * @tc.desc  : Test UpdateDownloadFinished mediaSegment_->contentLength_ == 0 && downloadRequest_ != nullptr
918  */
919 HWTEST_F(DashSegmentDownloaderUnitTest, UpdateDownloadFinished_002, TestSize.Level1)
920 {
921     segmentDownloader_->mediaSegment_ = std::make_shared<DashBufferSegment>();
922     segmentDownloader_->downloadRequest_ = std::make_shared<DownloadRequest>("test_request", nullptr, nullptr);
923     segmentDownloader_->downloadRequest_->headerInfo_.isChunked = true;
924     segmentDownloader_->downloadRequest_->headerInfo_.fileContentLen = 1;
925     segmentDownloader_->downloadRequest_->isHeaderUpdated_ = true;
926 
927     segmentDownloader_->UpdateDownloadFinished("", "");
928     EXPECT_EQ(segmentDownloader_->mediaSegment_->contentLength_, 1);
929 }
930 
931 // 0627
932 /**
933  * @tc.name  : Test GetSegmentRemainDuration
934  * @tc.number: GetSegmentRemainDuration_001
935  * @tc.desc  : Test GetSegmentRemainDuration
936  */
937 HWTEST_F(DashSegmentDownloaderUnitTest, GetSegmentRemainDuration_001, TestSize.Level1)
938 {
939     // Test SetAppUid downloader_ != nullptr
940     int32_t appUid = 1;
941     segmentDownloader_->SetAppUid(appUid);
942 
943     // Test SetAppUid downloader_ == nullptr
944     segmentDownloader_->downloader_.reset();
945     segmentDownloader_->SetAppUid(appUid);
946 
947     // Test SetInterruptState isInterruptNeeded == false
948     segmentDownloader_->SetInterruptState(false);
949 
950     // Test GetSegmentRemainDuration buffer_->GetHead() > currentSegment->bufferPosHead_
951     segmentDownloader_->buffer_->head_ = 1;
952     std::shared_ptr<DashBufferSegment> currentSegment = std::make_shared<DashBufferSegment>();
953     currentSegment->bufferPosTail_ = 2;
954     currentSegment->duration_ = 2;
955     currentSegment->bufferPosHead_ = 0;
956     uint32_t duration = segmentDownloader_->GetSegmentRemainDuration(currentSegment);
957     EXPECT_EQ(duration, 1);
958 }
959 
960 /**
961  * @tc.name  : Test IsNeedBufferForPlaying
962  * @tc.number: IsNeedBufferForPlaying_001
963  * @tc.desc  : Test IsNeedBufferForPlaying GetBufferingTimeOut() && callback_
964  */
965 HWTEST_F(DashSegmentDownloaderUnitTest, IsNeedBufferForPlaying_001, TestSize.Level1)
966 {
967     segmentDownloader_->bufferDurationForPlaying_ = 1;
968     segmentDownloader_->isDemuxerInitSuccess_.store(true);
969     segmentDownloader_->isBuffering_.store(true);
970     segmentDownloader_->IsNeedBufferForPlaying();
971 
972     MockCallback *mockCallback = new MockCallback();
973     EXPECT_CALL(*mockCallback, OnEvent(testing::_)).Times(TIMES_ONE);
974     segmentDownloader_->callback_ = mockCallback;
975     segmentDownloader_->isDemuxerInitSuccess_.store(true);
976     segmentDownloader_->isBuffering_.store(true);
977     segmentDownloader_->IsNeedBufferForPlaying();
978 
979     segmentDownloader_->bufferingTime_ = 1;
980     segmentDownloader_->steadyClock_.Reset();
981     segmentDownloader_->steadyClock_.begin_ -= std::chrono::milliseconds(MAX_BUFFERING_TIME_OUT);
982     std::this_thread::sleep_for(std::chrono::milliseconds(RECORD_TIME_INTERVAL));
983     segmentDownloader_->isDemuxerInitSuccess_.store(true);
984     segmentDownloader_->isBuffering_.store(true);
985     segmentDownloader_->IsNeedBufferForPlaying();
986 
987     delete mockCallback;
988     segmentDownloader_->callback_ = nullptr;
989     segmentDownloader_->isDemuxerInitSuccess_.store(true);
990     segmentDownloader_->isBuffering_.store(true);
991     segmentDownloader_->IsNeedBufferForPlaying();
992 }
993 
994 /**
995  * @tc.name  : Test IsNeedBufferForPlaying
996  * @tc.number: IsNeedBufferForPlaying_002
997  * @tc.desc  : Test IsNeedBufferForPlaying GetBufferSize() >= waterlineForPlaying_ || isAllSegmentFinished_.load()
998  */
999 HWTEST_F(DashSegmentDownloaderUnitTest, IsNeedBufferForPlaying_002, TestSize.Level1)
1000 {
1001     segmentDownloader_->bufferDurationForPlaying_ = 1;
1002     segmentDownloader_->isDemuxerInitSuccess_.store(true);
1003     segmentDownloader_->isBuffering_.store(true);
1004     bool retResult = segmentDownloader_->IsNeedBufferForPlaying();
1005     EXPECT_EQ(retResult, false);
1006 
1007     segmentDownloader_->isDemuxerInitSuccess_.store(true);
1008     segmentDownloader_->isBuffering_.store(true);
1009     segmentDownloader_->isAllSegmentFinished_.store(true);
1010     retResult = segmentDownloader_->IsNeedBufferForPlaying();
1011     EXPECT_EQ(retResult, false);
1012 
1013     segmentDownloader_->isDemuxerInitSuccess_.store(true);
1014     segmentDownloader_->isBuffering_.store(true);
1015     segmentDownloader_->waterlineForPlaying_ = 1;
1016     retResult = segmentDownloader_->IsNeedBufferForPlaying();
1017     EXPECT_EQ(retResult, false);
1018 
1019     segmentDownloader_->isDemuxerInitSuccess_.store(true);
1020     segmentDownloader_->isBuffering_.store(true);
1021     segmentDownloader_->waterlineForPlaying_ = 1;
1022     segmentDownloader_->isAllSegmentFinished_.store(false);
1023     retResult = segmentDownloader_->IsNeedBufferForPlaying();
1024     EXPECT_EQ(retResult, true);
1025 }
1026 
1027 /**
1028  * @tc.name  : Test GetBufferingTimeOut
1029  * @tc.number: GetBufferingTimeOut_001
1030  * @tc.desc  : Test GetBufferingTimeOut bufferingTime_ == 0
1031  *             Test NotifyInitSuccess streamType_ != MediaAVCodec::MediaType::MEDIA_TYPE_VID
1032  */
1033 HWTEST_F(DashSegmentDownloaderUnitTest, GetBufferingTimeOut_001, TestSize.Level1)
1034 {
1035     // Test NotifyInitSuccess streamType_ != MediaAVCodec::MediaType::MEDIA_TYPE_VID
1036     segmentDownloader_->streamType_ = MediaAVCodec::MediaType::MEDIA_TYPE_AUD;
1037     segmentDownloader_->NotifyInitSuccess();
1038 
1039     // Test GetBufferingTimeOut bufferingTime_ == 0
1040     segmentDownloader_->bufferingTime_ = 0;
1041     bool retResult = segmentDownloader_->GetBufferingTimeOut();
1042     EXPECT_EQ(retResult, false);
1043 }
1044 } // namespace HttpPlugin
1045 } // namespace Plugins
1046 } // namespace Media
1047 } // namespace OHOS