1 /*
2 * Copyright (C) 2023 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 #include "hls_playlist_downloader_unit_test.h"
16 #include "http_server_demo.h"
17
18 using namespace OHOS;
19 using namespace OHOS::Media;
20 namespace OHOS::Media::Plugins::HttpPlugin {
21 using namespace testing::ext;
22 using namespace std;
23 const static std::string TEST_URI_PATH = "http://127.0.0.1:4666/";
24 const static std::string M3U8_PATH_1 = "test_cbr/test_cbr.m3u8";
25 const static std::map<std::string, std::string> httpHeader = {
26 {"User-Agent", "userAgent"},
27 {"Referer", "DEF"},
28 };
29 std::unique_ptr<MediaAVCodec::HttpServerDemo> g_server = nullptr;
SetUpTestCase(void)30 void HlsPlayListDownloaderUnitTest::SetUpTestCase(void) {}
31
TearDownTestCase(void)32 void HlsPlayListDownloaderUnitTest::TearDownTestCase(void) {}
33
SetUp(void)34 void HlsPlayListDownloaderUnitTest::SetUp(void)
35 {
36 g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
37 g_server->StartServer();
38 }
39
TearDown(void)40 void HlsPlayListDownloaderUnitTest::TearDown(void)
41 {
42 g_server->StopServer();
43 g_server = nullptr;
44 }
45
46 HWTEST_F(HlsPlayListDownloaderUnitTest, TEST_OPEN, TestSize.Level1)
47 {
48 HlsPlayListDownloader downloader;
49 std::map<std::string, std::string> tmpHttpHeader;
50 std::string testUrl = TEST_URI_PATH + M3U8_PATH_1;
51 tmpHttpHeader["Content-Type"] = "application/x-mpegURL";
52 downloader.Open(testUrl, tmpHttpHeader);
53 EXPECT_EQ(testUrl, downloader.GetUrl());
54 EXPECT_EQ(nullptr, downloader.GetMaster());
55 }
56
57 HWTEST_F(HlsPlayListDownloaderUnitTest, GET_DURATION, TestSize.Level1)
58 {
59 std::string testUrl = TEST_URI_PATH + M3U8_PATH_1;
60 printf("----%s------", testUrl.c_str());
61 HlsPlayListDownloader downloader;
62 EXPECT_EQ(0, downloader.GetDuration());
63 downloader.Open(testUrl, httpHeader);
64 EXPECT_GE(downloader.GetDuration(), 0);
65 }
66
67 HWTEST_F(HlsPlayListDownloaderUnitTest, PARSE_MANIFEST_EMPTY, TestSize.Level1)
68 {
69 HlsPlayListDownloader downloader;
70 downloader.ParseManifest("", false);
71 EXPECT_GE(downloader.GetUrl(), "");
72 }
73
74 HWTEST_F(HlsPlayListDownloaderUnitTest, PARSE_MANIFEST_001, TestSize.Level1)
75 {
76 HlsPlayListDownloader downloader;
77 downloader.ParseManifest("http://new.url", false);
78 EXPECT_GE(downloader.GetUrl(), "http://new.url");
79 }
80
81 HWTEST_F(HlsPlayListDownloaderUnitTest, PARSE_MANIFEST_002, TestSize.Level1)
82 {
83 HlsPlayListDownloader downloader;
84 std::string testUrl = TEST_URI_PATH + "test_hls/testHLSEncode.m3u8";
85 downloader.Open(testUrl, httpHeader);
86 downloader.ParseManifest(testUrl, false);
87 EXPECT_FALSE(downloader.GetMaster()->isSimple_);
88 }
89
90 HWTEST_F(HlsPlayListDownloaderUnitTest, PARSE_MANIFEST_003, TestSize.Level1)
91 {
92 HlsPlayListDownloader downloader;
93 std::string testUrl = TEST_URI_PATH + M3U8_PATH_1;
94 downloader.Open(testUrl, httpHeader);
95 downloader.ParseManifest(testUrl, false);
96 EXPECT_FALSE(downloader.GetMaster()->isSimple_);
97 }
98
99 HWTEST_F(HlsPlayListDownloaderUnitTest, PARSE_MANIFEST_004, TestSize.Level1)
100 {
101 HlsPlayListDownloader downloader;
102
103 std::string testUrl = TEST_URI_PATH + "test_cbr/test_cbr.m3u8";
104 downloader.Open(testUrl, httpHeader);
105 downloader.ParseManifest(testUrl, false);
106 EXPECT_FALSE(downloader.GetMaster()->isSimple_);
107 }
108
109 HWTEST_F(HlsPlayListDownloaderUnitTest, PARSE_MANIFEST_005, TestSize.Level1)
110 {
111 HlsPlayListDownloader downloader;
112 std::string testUrl = TEST_URI_PATH + "test_cbr/test_cbr.m3u8";
113 downloader.ParseManifest(testUrl, false);
114 EXPECT_NE(downloader.GetMaster()->uri_, "");
115 EXPECT_EQ(downloader.GetMaster()->uri_, testUrl);
116 }
117
118 HWTEST_F(HlsPlayListDownloaderUnitTest, GET_CUR_BITRATE_001, TestSize.Level1)
119 {
120 HlsPlayListDownloader downloader;
121 std::string testUrl = TEST_URI_PATH + M3U8_PATH_1;
122 std::string testUrl1 = TEST_URI_PATH + "test_cbr/test_cbr.m3u8";
123 downloader.Open(testUrl1, httpHeader);
124 downloader.ParseManifest(testUrl1, false);
125 EXPECT_EQ(0, downloader.GetCurBitrate());
126 EXPECT_EQ(0, downloader.GetCurrentBitRate());
127 }
128
129 HWTEST_F(HlsPlayListDownloaderUnitTest, GET_CUR_BITRATE_002, TestSize.Level1)
130 {
131 HlsPlayListDownloader downloader;
132 uint64_t bitRate = downloader.GetCurBitrate();
133 EXPECT_EQ(0, bitRate);
134 }
135
136 HWTEST_F(HlsPlayListDownloaderUnitTest, GET_VEDIO_WIDTH, TestSize.Level1)
137 {
138 HlsPlayListDownloader downloader;
139 int width = downloader.GetVedioWidth();
140 EXPECT_EQ(0, width);
141 }
142
143 HWTEST_F(HlsPlayListDownloaderUnitTest, GET_VEDIO_HEIGHT, TestSize.Level1)
144 {
145 HlsPlayListDownloader downloader;
146 int height = downloader.GetVedioHeight();
147 EXPECT_EQ(0, height);
148 }
149
150 HWTEST_F(HlsPlayListDownloaderUnitTest, IS_LIVE, TestSize.Level1)
151 {
152 HlsPlayListDownloader downloader;
153 bool isLive = downloader.IsLive();
154 EXPECT_FALSE(isLive);
155 }
156 }