• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "m3u8_unit_test.h"
16 
17 #define LOCAL true
18 
19 using namespace OHOS;
20 using namespace OHOS::Media;
21 namespace OHOS::Media::Plugins::HttpPlugin {
22 using namespace testing::ext;
23 using namespace std;
24 
SetUpTestCase(void)25 void M3u8UnitTest::SetUpTestCase(void) {}
26 
TearDownTestCase(void)27 void M3u8UnitTest::TearDownTestCase(void) {}
28 
SetUp(void)29 void M3u8UnitTest::SetUp(void) {}
30 
TearDown(void)31 void M3u8UnitTest::TearDown(void) {}
32 
33 HWTEST_F(M3u8UnitTest, Init_Tag_Updaters_Map_001, TestSize.Level1)
34 {
35     double duration = testM3u8->GetDuration();
36     bool isLive = testM3u8->IsLive();
37     EXPECT_GE(duration, 0.0);
38     EXPECT_EQ(isLive, false);
39 }
40 
41 HWTEST_F(M3u8UnitTest, is_live_001, TestSize.Level1)
42 {
43     EXPECT_NE(testM3u8->GetDuration(), 0.0);
44 }
45 
46 HWTEST_F(M3u8UnitTest, parse_key_001, TestSize.Level1)
47 {
48     testM3u8->ParseKey(std::make_shared<AttributesTag>(HlsTag::EXTXKEY, tagAttribute));
49     testM3u8->DownloadKey();
50 }
51 
52 HWTEST_F(M3u8UnitTest, base_64_decode_001, TestSize.Level1)
53 {
54     EXPECT_EQ(testM3u8->Base64Decode((uint8_t *)0x20000550, (uint32_t)16, (uint8_t *)0x20000550, (uint32_t *)16), true);
55     EXPECT_EQ(testM3u8->Base64Decode((uint8_t *)0x20000550, (uint32_t)10, (uint8_t *)0x20000550, (uint32_t *)10), true);
56     EXPECT_EQ(testM3u8->Base64Decode(nullptr, (uint32_t)10, (uint8_t *)0x20000550, (uint32_t *)10), true);
57 }
58 
59 HWTEST_F(M3u8UnitTest, ConstructorTest, TestSize.Level1)
60 {
61     M3U8 m3u8(testUri, testName);
62     // 检查 URI 和名称是否正确设置
63     ASSERT_EQ(m3u8.uri_, testUri);
64     ASSERT_EQ(m3u8.name_, testName);
65     // 这里可以添加更多的断言来检查初始化的状态
66 }
67 
68 // 测试 Update 方法
69 HWTEST_F(M3u8UnitTest, UpdateTest, TestSize.Level1)
70 {
71     M3U8 m3u8("http://example.com/test.m3u8", "TestPlaylist");
72     std::string testPlaylist = "#EXTM3U\n#EXT-X-TARGETDURATION:10\n...";
73 
74     // 测试有效的播放列表更新
75     EXPECT_TRUE(m3u8.Update(testPlaylist));
76 
77     // 测试当播放列表不变时的更新
78     EXPECT_TRUE(m3u8.Update(testPlaylist));
79 
80     // 测试无效的播放列表更新
81     EXPECT_FALSE(m3u8.Update("Invalid Playlist"));
82 }
83 
84 // 测试 IsLive 方法
85 HWTEST_F(M3u8UnitTest, IsLiveTest, TestSize.Level1)
86 {
87     M3U8 m3u8(testUri, "LivePlaylist");
88     EXPECT_FALSE(m3u8.IsLive());
89 }
90 
91 // 测试 M3U8Fragment 构造函数
92 HWTEST_F(M3u8UnitTest, M3U8FragmentConstructorTest, TestSize.Level1)
93 {
94     std::string testTitle = "FragmentTitle";
95     double testDuration = 10.0;
96     int testSequence = 1;
97     bool testDiscont = false;
98     M3U8Fragment fragment(testUri, testTitle, testDuration, testSequence, testDiscont);
99     ASSERT_EQ(fragment.uri_, testUri);
100     ASSERT_EQ(fragment.title_, testTitle);
101     ASSERT_DOUBLE_EQ(fragment.duration_, testDuration);
102     ASSERT_EQ(fragment.sequence_, testSequence);
103     ASSERT_EQ(fragment.discont_, testDiscont);
104 }
105 }