1 /*
2  * Copyright (c) 2024 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 <gtest/gtest.h>
17 
18 #include "util.h"
19 
20 using namespace testing;
21 using namespace testing::ext;
22 
23 namespace OHOS::Rosen {
24 class UtilTest : public testing::Test {
25 public:
26     static void SetUpTestCase();
27     static void TearDownTestCase();
28     void SetUp() override;
29     void TearDown() override;
30 };
31 
SetUpTestCase()32 void UtilTest::SetUpTestCase() {}
TearDownTestCase()33 void UtilTest::TearDownTestCase() {}
SetUp()34 void UtilTest::SetUp() {}
TearDown()35 void UtilTest::TearDown() {}
36 
37 /**
38  * @tc.name: UtilTest_001
39  * @tc.desc: Verify the ParseOldConfigFile
40  * @tc.type:FUNC
41  */
HWTEST_F(UtilTest, UtilTest_001, TestSize.Level1)42 HWTEST_F(UtilTest, UtilTest_001, TestSize.Level1)
43 {
44     std::vector<BootAnimationConfig> configs;
45 
46     std::string jsonStr1 = "{}";
47     cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
48     OHOS::ParseOldConfigFile(jsonData1, configs);
49 
50     std::string jsonStr2 = "{\"cust.bootanimation.pics\":1,\"cust.bootanimation.sounds\":1,\
51     \"cust.bootanimation.video\":1,\"cust.bootanimation.video.extra\":1,\"cust.bootanimation.rotate.screenid\":1,\
52     \"cust.bootanimation.rotate.degree\":1}";
53     cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
54     OHOS::ParseOldConfigFile(jsonData2, configs);
55     EXPECT_EQ(configs.front().rotateDegree, 0);
56 
57     std::string jsonStr3 = "{\"cust.bootanimation.pics\":\"abc\",\"cust.bootanimation.sounds\":\"abc\",\
58     \"cust.bootanimation.video\":\"abc\",\"cust.bootanimation.video.extra\":\"abc\",\
59     \"cust.bootanimation.rotate.screenid\":\"0\", \"cust.bootanimation.rotate.degree\":\"270\"}";
60     cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
61     OHOS::ParseOldConfigFile(jsonData3, configs);
62     EXPECT_EQ(configs.back().rotateDegree, 270);
63 }
64 
65 /**
66  * @tc.name: UtilTest_002
67  * @tc.desc: Verify the ParseNewConfigFile
68  * @tc.type:FUNC
69  */
HWTEST_F(UtilTest, UtilTest_002, TestSize.Level1)70 HWTEST_F(UtilTest, UtilTest_002, TestSize.Level1)
71 {
72     bool isMultiDisplay = false;
73     std::vector<BootAnimationConfig> configs;
74 
75     std::string jsonStr1 = "{}";
76     cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
77     OHOS::ParseNewConfigFile(jsonData1, isMultiDisplay, configs);
78 
79     std::string jsonStr2 = "{\"cust.bootanimation.multi_display\":1}";
80     cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
81     OHOS::ParseNewConfigFile(jsonData2, isMultiDisplay, configs);
82 
83     std::string jsonStr3 = "{\"cust.bootanimation.multi_display\":false}";
84     cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
85     OHOS::ParseNewConfigFile(jsonData3, isMultiDisplay, configs);
86     EXPECT_EQ(isMultiDisplay, false);
87 
88     std::string jsonStr4 = "{\"cust.bootanimation.multi_display\":true}";
89     cJSON* jsonData4 = cJSON_Parse(jsonStr4.c_str());
90     OHOS::ParseNewConfigFile(jsonData4, isMultiDisplay, configs);
91 
92     std::string jsonStr5 = "{\"screen_config\":[]}";
93     cJSON* jsonData5 = cJSON_Parse(jsonStr5.c_str());
94     OHOS::ParseNewConfigFile(jsonData5, isMultiDisplay, configs);
95 
96     std::string jsonStr6 = "{\"screen_config\":[{}]}";
97     cJSON* jsonData6 = cJSON_Parse(jsonStr6.c_str());
98     OHOS::ParseNewConfigFile(jsonData6, isMultiDisplay, configs);
99 
100     std::string jsonStr7 = "{\"screen_config\":[{\"cust.bootanimation.screen_id\":1,\
101     \"cust.bootanimation.pics\":1,\"cust.bootanimation.sounds\":1,\
102     \"cust.bootanimation.video_default\":1,\"cust.bootanimation.rotate_degree\":1,\
103     \"cust.bootanimation.video_extensions\":1}]}";
104     cJSON* jsonData7 = cJSON_Parse(jsonStr7.c_str());
105     OHOS::ParseNewConfigFile(jsonData7, isMultiDisplay, configs);
106 
107     std::string jsonStr8 = "{\"screen_config\":[{\"cust.bootanimation.screen_id\":\"0\",\
108     \"cust.bootanimation.pics\":\"abc\",\"cust.bootanimation.sounds\":\"abc\",\
109     \"cust.bootanimation.video_default\":\"abc\",\"cust.bootanimation.rotate_degree\":\"270\",\
110     \"cust.bootanimation.video_extensions\":[]}]}";
111     cJSON* jsonData8 = cJSON_Parse(jsonStr8.c_str());
112     OHOS::ParseNewConfigFile(jsonData8, isMultiDisplay, configs);
113     EXPECT_EQ(isMultiDisplay, true);
114 }
115 
116 /**
117  * @tc.name: UtilTest_003
118  * @tc.desc: Verify the ParseVideoExtraPath
119  * @tc.type:FUNC
120  */
HWTEST_F(UtilTest, UtilTest_003, TestSize.Level1)121 HWTEST_F(UtilTest, UtilTest_003, TestSize.Level1)
122 {
123     BootAnimationConfig config;
124     std::string jsonStr1 = "[nullptr]";
125     cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
126     OHOS::ParseVideoExtraPath(jsonData1, config);
127 
128     std::string jsonStr2 = "[1]";
129     cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
130     OHOS::ParseVideoExtraPath(jsonData2, config);
131 
132     std::string jsonStr3 = "[\"abc\"]";
133     cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
134     OHOS::ParseVideoExtraPath(jsonData3, config);
135     EXPECT_EQ(config.videoExtPath.front(), "abc");
136 }
137 
138 /**
139  * @tc.name: UtilTest_004
140  * @tc.desc: Verify the ParseBootDuration
141  * @tc.type:FUNC
142  */
HWTEST_F(UtilTest, UtilTest_004, TestSize.Level1)143 HWTEST_F(UtilTest, UtilTest_004, TestSize.Level1)
144 {
145     BootAnimationConfig config;
146     int32_t duration;
147     std::string jsonStr1 = "{}";
148     cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
149     OHOS::ParseBootDuration(jsonData1, duration);
150 
151     std::string jsonStr2 = "{\"cust.bootanimation.duration\":10}";
152     cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
153     OHOS::ParseBootDuration(jsonData2, duration);
154 
155     std::string jsonStr3 = "{\"cust.bootanimation.duration\":\"10\"}";
156     cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
157     OHOS::ParseBootDuration(jsonData3, duration);
158     EXPECT_EQ(duration, 10);
159 }
160 }
161