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 "http_source_plugin.h"
17 #include "gtest/gtest.h"
18
19 using namespace std;
20 using namespace testing::ext;
21
22 namespace OHOS {
23 namespace Media {
24 namespace Plugins {
25 namespace HttpPlugin {
26 namespace {
27 class MockMediaDownloader : public MediaDownloader {
28 public:
29 MockMediaDownloader() = default;
30 virtual ~MockMediaDownloader() = default;
Open(const std::string & url,const std::map<std::string,std::string> & httpHeader)31 bool Open(const std::string& url, const std::map<std::string, std::string>& httpHeader) override
32 {
33 (void)url;
34 (void)httpHeader;
35 return true;
36 }
Close(bool isAsync)37 void Close(bool isAsync) override
38 {
39 (void)isAsync;
40 return;
41 }
Pause()42 void Pause() override
43 {
44 return;
45 }
Resume()46 void Resume() override
47 {
48 return;
49 }
Read(unsigned char * buff,ReadDataInfo & readDataInfo)50 Status Read(unsigned char* buff, ReadDataInfo& readDataInfo) override
51 {
52 (void)buff;
53 (void)readDataInfo;
54 return Status::OK;
55 }
GetBufferSize() const56 size_t GetBufferSize() const override
57 {
58 return 0;
59 }
GetBufferingTimeOut()60 bool GetBufferingTimeOut() override
61 {
62 return true;
63 }
GetContentLength() const64 size_t GetContentLength() const override
65 {
66 return contentLength_;
67 }
GetDuration() const68 int64_t GetDuration() const override
69 {
70 return 0;
71 }
GetSeekable() const72 Seekable GetSeekable() const override
73 {
74 return Seekable::SEEKABLE;
75 }
SetCallback(Callback * cb)76 void SetCallback(Callback* cb) override
77 {
78 (void)cb;
79 return;
80 }
SetStatusCallback(StatusCallbackFunc cb)81 void SetStatusCallback(StatusCallbackFunc cb) override
82 {
83 (void)cb;
84 return;
85 }
GetStartedStatus()86 bool GetStartedStatus() override
87 {
88 return true;
89 }
SetInterruptState(bool isInterruptNeeded)90 void SetInterruptState(bool isInterruptNeeded) override
91 {
92 (void)isInterruptNeeded;
93 return;
94 }
SetAppUid(int32_t appUid)95 void SetAppUid(int32_t appUid) override
96 {
97 (void)appUid;
98 return;
99 }
SelectBitRate(uint32_t bitRate)100 bool SelectBitRate(uint32_t bitRate) override
101 {
102 return bitRate == 0 ? false : true;
103 }
SetIsTriggerAutoMode(bool isAuto)104 void SetIsTriggerAutoMode(bool isAuto) override
105 {
106 (void)isAuto;
107 return;
108 }
AutoSelectBitRate(uint32_t bitRate)109 bool AutoSelectBitRate(uint32_t bitRate) override
110 {
111 return bitRate == 0 ? false : true;
112 }
GetSegmentOffset()113 size_t GetSegmentOffset() override
114 {
115 return segmentOffset_;
116 }
GetHLSDiscontinuity()117 bool GetHLSDiscontinuity() override
118 {
119 return true;
120 }
121 private:
122 size_t contentLength_ {4};
123 size_t segmentOffset_ {1};
124 };
125
126 class MockCallback : public Plugins::Callback {
127 public:
OnEvent(const Plugins::PluginEvent & event)128 void OnEvent(const Plugins::PluginEvent &event) override
129 {
130 (void)event;
131 return;
132 }
133 };
134 }
135
136 class HttpSourcePluginUnitTest : public testing::Test {
137 public:
SetUpTestCase(void)138 static void SetUpTestCase(void) {}
TearDownTestCase(void)139 static void TearDownTestCase(void) {}
140 void SetUp(void);
141 void TearDown(void);
142 protected:
143 std::shared_ptr<HttpSourcePlugin> plugin_;
144 };
145
SetUp(void)146 void HttpSourcePluginUnitTest::SetUp(void)
147 {
148 plugin_ = std::make_shared<HttpSourcePlugin>("mock");
149 }
150
TearDown(void)151 void HttpSourcePluginUnitTest::TearDown(void)
152 {
153 plugin_.reset();
154 }
155
156 /**
157 * @tc.name : Test SetDownloaderBySource API
158 * @tc.number: SetDownloaderBySource_001
159 * @tc.desc : Test source->GetSourceLoader() == nullptr and mimeType_== AVMimeTypes::APPLICATION_M3U8
160 */
161 HWTEST_F(HttpSourcePluginUnitTest, SetDownloaderBySource_001, TestSize.Level1)
162 {
163 ASSERT_NE(nullptr, plugin_);
164 std::string uri {"mock"};
165 std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(uri);
166 ASSERT_EQ(nullptr, source->sourceLoader_);
167 source->mimeType_ = AVMimeTypes::APPLICATION_M3U8;
168 plugin_->SetDownloaderBySource(source);
169 ASSERT_EQ(nullptr, plugin_->loaderCombinations_);
170 ASSERT_NE(nullptr, plugin_->downloader_);
171 }
172
173 /**
174 * @tc.name : Test IsSeekToTimeSupported API
175 * @tc.number: IsSeekToTimeSupported_001
176 * @tc.desc : Test mimeType_== AVMimeTypes::APPLICATION_M3U8
177 */
178 HWTEST_F(HttpSourcePluginUnitTest, IsSeekToTimeSupported_001, TestSize.Level1)
179 {
180 ASSERT_NE(nullptr, plugin_);
181 plugin_->mimeType_ = AVMimeTypes::APPLICATION_M3U8;
182 ASSERT_EQ(true, plugin_->IsSeekToTimeSupported());
183 }
184
185 /**
186 * @tc.name : Test SeekTo API
187 * @tc.number: SeekTo_001
188 * @tc.desc : Test offset > downloader_->GetContentLength() && downloader_->GetContentLength() != 0
189 */
190 HWTEST_F(HttpSourcePluginUnitTest, SeekTo_001, TestSize.Level1)
191 {
192 ASSERT_NE(nullptr, plugin_);
193 plugin_->downloader_ = std::make_shared<MockMediaDownloader>();
194 plugin_->seekErrorCount_ = 5;
195 MockCallback mockCallback;
196 plugin_->callback_ = &mockCallback;
197 uint64_t mockOffset {5};
198 ASSERT_EQ(Status::ERROR_INVALID_PARAMETER, plugin_->SeekTo(mockOffset));
199 plugin_->seekErrorCount_ = 0;
200 ASSERT_EQ(Status::ERROR_INVALID_PARAMETER, plugin_->SeekTo(mockOffset));
201 }
202
203 /**
204 * @tc.name : Test SelectBitRate API
205 * @tc.number: SelectBitRate_001
206 * @tc.desc : Test downloader_->SelectBitRate(bitRate) == false
207 */
208 HWTEST_F(HttpSourcePluginUnitTest, SelectBitRate_001, TestSize.Level1)
209 {
210 ASSERT_NE(nullptr, plugin_);
211 plugin_->downloader_ = std::make_shared<MockMediaDownloader>();
212 ASSERT_EQ(Status::ERROR_UNKNOWN, plugin_->SelectBitRate(0));
213 }
214
215 /**
216 * @tc.name : Test AutoSelectBitRate API
217 * @tc.number: AutoSelectBitRate_001
218 * @tc.desc : Test downloader_->AutoSelectBitRate(bitRate) == false
219 */
220 HWTEST_F(HttpSourcePluginUnitTest, AutoSelectBitRate_001, TestSize.Level1)
221 {
222 ASSERT_NE(nullptr, plugin_);
223 plugin_->downloader_ = std::make_shared<MockMediaDownloader>();
224 ASSERT_EQ(Status::ERROR_UNKNOWN, plugin_->AutoSelectBitRate(0));
225 }
226
227 /**
228 * @tc.name : Test AutoSelectBitRate API
229 * @tc.number: AutoSelectBitRate_002
230 * @tc.desc : Test downloader_->AutoSelectBitRate(bitRate) == true
231 */
232 HWTEST_F(HttpSourcePluginUnitTest, AutoSelectBitRate_002, TestSize.Level1)
233 {
234 ASSERT_NE(nullptr, plugin_);
235 plugin_->downloader_ = std::make_shared<MockMediaDownloader>();
236 ASSERT_EQ(Status::OK, plugin_->AutoSelectBitRate(1));
237 }
238
239 /**
240 * @tc.name : Test GetSegmentOffset API
241 * @tc.number: GetSegmentOffset_001
242 * @tc.desc : Test GetSegmentOffset interface
243 */
244 HWTEST_F(HttpSourcePluginUnitTest, GetSegmentOffset_001, TestSize.Level1)
245 {
246 ASSERT_NE(nullptr, plugin_);
247 ASSERT_EQ(0, plugin_->GetSegmentOffset());
248 plugin_->downloader_ = std::make_shared<MockMediaDownloader>();
249 ASSERT_EQ(1, plugin_->GetSegmentOffset());
250 }
251
252 /**
253 * @tc.name : Test GetHLSDiscontinuity API
254 * @tc.number: GetHLSDiscontinuity_001
255 * @tc.desc : Test GetHLSDiscontinuity interface
256 */
257 HWTEST_F(HttpSourcePluginUnitTest, GetHLSDiscontinuity_001, TestSize.Level1)
258 {
259 ASSERT_NE(nullptr, plugin_);
260 ASSERT_EQ(false, plugin_->GetHLSDiscontinuity());
261 plugin_->downloader_ = std::make_shared<MockMediaDownloader>();
262 ASSERT_EQ(true, plugin_->GetHLSDiscontinuity());
263 }
264
265 /**
266 * @tc.name : Test CheckIsM3U8Uri API
267 * @tc.number: CheckIsM3U8Uri_001
268 * @tc.desc : Test uri_.empty() == true
269 */
270 HWTEST_F(HttpSourcePluginUnitTest, CheckIsM3U8Uri_001, TestSize.Level1)
271 {
272 ASSERT_NE(nullptr, plugin_);
273 std::string mockUri;
274 plugin_->uri_.swap(mockUri);
275 ASSERT_EQ(false, plugin_->CheckIsM3U8Uri());
276 }
277 }
278 }
279 }
280 }