• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <gtest/gtest.h>
17 #include "utils/utils.h"
18 
19 using namespace OHOS::Global::Resource;
20 using namespace testing::ext;
21 namespace {
22 class UtilsTest : public testing::Test {};
23 
24 /*
25  * @tc.name: TestConvertColorToUInt32Func001
26  * @tc.desc: Test ConvertColorToUInt32 function
27  * @tc.type: FUNC
28  */
29 HWTEST_F(UtilsTest, TestConvertColorToUInt32Func001, TestSize.Level1)
30 {
31     uint32_t outValue;
32     EXPECT_EQ(Utils::ConvertColorToUInt32(nullptr, outValue), RState::INVALID_FORMAT);
33     EXPECT_EQ(Utils::ConvertColorToUInt32("", outValue), RState::INVALID_FORMAT);
34     EXPECT_EQ(Utils::ConvertColorToUInt32("123", outValue), RState::INVALID_FORMAT);
35     EXPECT_EQ(Utils::ConvertColorToUInt32("#12", outValue), RState::INVALID_FORMAT);
36     EXPECT_EQ(Utils::ConvertColorToUInt32("##12", outValue), RState::INVALID_FORMAT);
37     EXPECT_EQ(Utils::ConvertColorToUInt32("#123", outValue), RState::SUCCESS);
38     EXPECT_EQ(outValue, 4279312947);
39     EXPECT_EQ(Utils::ConvertColorToUInt32("#1233", outValue), RState::SUCCESS);
40     EXPECT_EQ(outValue, 287454003);
41     EXPECT_EQ(Utils::ConvertColorToUInt32("#12334", outValue), RState::INVALID_FORMAT);
42     EXPECT_EQ(Utils::ConvertColorToUInt32("#123344", outValue), RState::SUCCESS);
43     EXPECT_EQ(outValue, 4279382852);
44     EXPECT_EQ(Utils::ConvertColorToUInt32("#12334455", outValue), RState::SUCCESS);
45     EXPECT_EQ(outValue, 305349717);
46     EXPECT_EQ(Utils::ConvertColorToUInt32("#1a3344ff", outValue), RState::SUCCESS);
47     EXPECT_EQ(outValue, 439567615);
48 }
49 
50 /*
51  * @tc.name: TestGetMediaBase64Data001
52  * @tc.desc: Test GetMediaBase64Data function
53  * @tc.type: FUNC
54  */
55 HWTEST_F(UtilsTest, TestGetMediaBase64Data001, TestSize.Level1)
56 {
57     std::string iconPath = "/data/test/all/assets/entry/resources/base/media/icon.png";
58     std::string base64Data;
59     RState state = Utils::GetMediaBase64Data(iconPath, base64Data);
60     EXPECT_EQ(SUCCESS, state);
61 }
62 
63 /*
64  * @tc.name: TestGetMediaBase64Data002
65  * @tc.desc: Test GetMediaBase64Data function
66  * @tc.type: FUNC
67  */
68 HWTEST_F(UtilsTest, TestGetMediaBase64Data002, TestSize.Level1)
69 {
70     std::string iconPath = "/data/test/all/assets/entry/resources/base/media/icon_no_exist.png";
71     std::string base64Data;
72     RState state = Utils::GetMediaBase64Data(iconPath, base64Data);
73     EXPECT_EQ(NOT_FOUND, state);
74 }
75 
76 /*
77  * @tc.name: TestCanonicalizePath001
78  * @tc.desc: Test CanonicalizePath function
79  * @tc.type: FUNC
80  */
81 HWTEST_F(UtilsTest, TestCanonicalizePath001, TestSize.Level1)
82 {
83     char *outPath = nullptr;
84     size_t len = 0;
85     Utils::CanonicalizePath(nullptr, outPath, len);
86     EXPECT_EQ(outPath, nullptr);
87 }
88 
89 /*
90  * @tc.name: TestCanonicalizePath002
91  * @tc.desc: Test CanonicalizePath function
92  * @tc.type: FUNC
93  */
94 HWTEST_F(UtilsTest, TestCanonicalizePath002, TestSize.Level1)
95 {
96     std::string iconPath = "/data/test/all/assets/entry/resources/base/media/icon.png";
97     char *outPath = nullptr;
98     size_t len = 1;
99     Utils::CanonicalizePath(iconPath.c_str(), outPath, len);
100     EXPECT_EQ(outPath, nullptr);
101 }
102 
103 /*
104  * @tc.name: TestGetFiles001
105  * @tc.desc: Test GetFiles function
106  * @tc.type: FUNC
107  */
108 HWTEST_F(UtilsTest, TestGetFiles001, TestSize.Level1)
109 {
110     std::string currentDir = "/data/test/all/assets/entry/resources/";
111     std::vector<std::string> vFiles;
112     RState state = Utils::GetFiles(currentDir, vFiles);
113     EXPECT_EQ(SUCCESS, state);
114 }
115 
116 /*
117  * @tc.name: TestGetFiles002
118  * @tc.desc: Test GetFiles function
119  * @tc.type: FUNC
120  */
121 HWTEST_F(UtilsTest, TestGetFiles002, TestSize.Level1)
122 {
123     std::string currentDir = "/data/test/all/assets/entry/resources/no_exist_dir_123456";
124     std::vector<std::string> vFiles;
125     RState state = Utils::GetFiles(currentDir, vFiles);
126     EXPECT_EQ(ERROR_CODE_RES_PATH_INVALID, state);
127 }
128 
129 /*
130  * @tc.name: TestIsAlphaString001
131  * @tc.desc: Test IsAlphaString function
132  * @tc.type: FUNC
133  */
134 HWTEST_F(UtilsTest, TestIsAlphaString001, TestSize.Level1)
135 {
136     char* tmpString = nullptr;
137     bool result = Utils::IsAlphaString(tmpString, 0);
138     EXPECT_FALSE(result);
139 }
140 
141 /*
142  * @tc.name: TestIsNumericString001
143  * @tc.desc: Test IsNumericString function
144  * @tc.type: FUNC
145  */
146 HWTEST_F(UtilsTest, TestIsNumericString001, TestSize.Level1)
147 {
148     char* tmpString = nullptr;
149     bool result = Utils::IsNumericString(tmpString, 0);
150     EXPECT_FALSE(result);
151 }
152 
153 /*
154  * @tc.name: TestIsNumericString002
155  * @tc.desc: Test IsNumericString function
156  * @tc.type: FUNC
157  */
158 HWTEST_F(UtilsTest, TestIsNumericString002, TestSize.Level1)
159 {
160     std::string tmpString = "abcd";
161     bool result = Utils::IsNumericString(tmpString.c_str(), 3);
162     EXPECT_FALSE(result);
163 }
164 
165 /*
166  * @tc.name: TestDecodeScript001
167  * @tc.desc: Test DecodeScript function
168  * @tc.type: FUNC
169  */
170 HWTEST_F(UtilsTest, TestDecodeScript001, TestSize.Level1)
171 {
172     char* outValue = nullptr;
173     Utils::DecodeScript(123, outValue);
174     EXPECT_EQ(outValue, nullptr);
175 }
176 
177 /*
178  * @tc.name: TestEncodeLanguage001
179  * @tc.desc: Test EncodeLanguage function
180  * @tc.type: FUNC
181  */
182 HWTEST_F(UtilsTest, TestEncodeLanguage001, TestSize.Level1)
183 {
184     char* language = nullptr;
185     uint16_t result = Utils::EncodeLanguage(language);
186     EXPECT_EQ(Utils::NULL_LANGUAGE, result);
187 }
188 
189 /*
190  * @tc.name: TestEncodeRegionByResLocale001
191  * @tc.desc: Test EncodeRegionByResLocale function
192  * @tc.type: FUNC
193  */
194 HWTEST_F(UtilsTest, TestEncodeRegionByResLocale001, TestSize.Level1)
195 {
196     ResLocale *locale = nullptr;
197     uint16_t result = Utils::EncodeRegionByResLocale(locale);
198     EXPECT_EQ(Utils::NULL_REGION, result);
199 }
200 
201 /*
202  * @tc.name: TestconvertToInteger001
203  * @tc.desc: Test convertToInteger function
204  * @tc.type: FUNC
205  */
206 HWTEST_F(UtilsTest, TestconvertToInteger001, TestSize.Level1)
207 {
208     std::string str = "abc";
209     int outValue;
210     bool result = Utils::convertToInteger(str, outValue);
211     EXPECT_FALSE(result);
212 }
213 
214 /*
215  * @tc.name: TestconvertToDouble001
216  * @tc.desc: Test convertToDouble function
217  * @tc.type: FUNC
218  */
219 HWTEST_F(UtilsTest, TestconvertToDouble001, TestSize.Level1)
220 {
221     std::string str = "abc";
222     double outValue;
223     bool result = Utils::convertToDouble(str, outValue);
224     EXPECT_FALSE(result);
225 }
226 }  // namespace
227