1 /*
2  * Copyright (c) 2021-2022 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  */
HWTEST_F(UtilsTest, TestConvertColorToUInt32Func001, TestSize.Level1)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  */
HWTEST_F(UtilsTest, TestGetMediaBase64Data001, TestSize.Level1)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  */
HWTEST_F(UtilsTest, TestGetMediaBase64Data002, TestSize.Level1)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  */
HWTEST_F(UtilsTest, TestCanonicalizePath001, TestSize.Level1)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  */
HWTEST_F(UtilsTest, TestCanonicalizePath002, TestSize.Level1)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  */
HWTEST_F(UtilsTest, TestGetFiles001, TestSize.Level1)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  */
HWTEST_F(UtilsTest, TestGetFiles002, TestSize.Level1)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 }  // namespace
129