1 /*
2 * Copyright (c) 2021 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 "res_desc_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "res_desc.h"
21 #include "test_common.h"
22 #include "utils/string_utils.h"
23
24 using namespace OHOS::Global::Resource;
25 using namespace testing::ext;
26
27 class ResDescTest : public testing::Test {
28 public:
29 static void SetUpTestCase(void);
30
31 static void TearDownTestCase(void);
32
33 void SetUp();
34
35 void TearDown();
36 };
37
SetUpTestCase(void)38 void ResDescTest::SetUpTestCase(void)
39 {
40 // step 1: input testsuit setup step
41 g_logLevel = LOG_DEBUG;
42 }
43
TearDownTestCase(void)44 void ResDescTest::TearDownTestCase(void)
45 {
46 // step 2: input testsuit teardown step
47 }
48
SetUp()49 void ResDescTest::SetUp()
50 {
51 // step 3: input testcase setup step
52 HILOG_DEBUG("ResDescTest setup");
53 }
54
TearDown()55 void ResDescTest::TearDown()
56 {
57 // step 4: input testcase teardown step
58 HILOG_DEBUG("ResDescTest teardown");
59 }
60
61 /*
62 * @tc.name: ResDescFuncTest001
63 * @tc.desc: Test IsRef function, non file case.
64 * @tc.type: FUNC
65 */
66 HWTEST_F(ResDescTest, ResDescFuncTest001, TestSize.Level1)
67 {
68 std::string str;
69 int id;
70 ResType resType;
71 str.assign("abc");
72 EXPECT_TRUE(!IdItem::IsRef(str, resType, id));
73
74 str.assign("$abc");
75 EXPECT_TRUE(!IdItem::IsRef(str, resType, id));
76
77 str.assign("$abc:");
78 EXPECT_TRUE(!IdItem::IsRef(str, resType, id));
79
80 str.assign("$abc:abc");
81 EXPECT_TRUE(!IdItem::IsRef(str, resType, id));
82
83 str.assign("$abc:123456");
84 EXPECT_TRUE(!IdItem::IsRef(str, resType, id));
85
86 str.assign("$string:123456");
87 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
88 EXPECT_EQ(ResType::STRING, resType);
89 EXPECT_EQ(123456, id);
90 str.assign("$boolean:100001");
91 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
92 EXPECT_EQ(ResType::BOOLEAN, resType);
93 EXPECT_EQ(100001, id);
94 str.assign("$color:66666");
95 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
96 EXPECT_EQ(ResType::COLOR, resType);
97 EXPECT_EQ(66666, id);
98 str.assign("$float:100002");
99 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
100 EXPECT_EQ(ResType::FLOAT, resType);
101 EXPECT_EQ(100002, id);
102 str.assign("$integer:2008168");
103 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
104 EXPECT_EQ(ResType::INTEGER, resType);
105 EXPECT_EQ(2008168, id);
106 str.assign("$pattern:654321");
107 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
108 EXPECT_EQ(ResType::PATTERN, resType);
109 EXPECT_EQ(654321, id);
110 str.assign("$theme:99999");
111 EXPECT_TRUE(IdItem::IsRef(str, resType, id));
112 EXPECT_EQ(ResType::THEME, resType);
113 EXPECT_EQ(99999, id);
114 }
115
TestKeyParam(KeyType keyType,int value,std::string expectStr)116 void TestKeyParam(KeyType keyType, int value, std::string expectStr)
117 {
118 KeyParam keyParam;
119 keyParam.type_ = keyType;
120 keyParam.value_ = value;
121 keyParam.InitStr();
122 EXPECT_EQ(expectStr, keyParam.str_);
123 }
124
125 /*
126 * @tc.name: ResDescFuncTest002
127 * @tc.desc: Test IsRef function, non file case.
128 * @tc.type: FUNC
129 */
130 HWTEST_F(ResDescTest, ResDescFuncTest002, TestSize.Level1)
131 {
132 TestKeyParam(KeyType::LANGUAGES, 25966, "en");
133 TestKeyParam(KeyType::LANGUAGES, 31336, "zh");
134
135 TestKeyParam(KeyType::REGION, 21843, "US");
136 TestKeyParam(KeyType::REGION, 17230, "CN");
137
138 TestKeyParam(KeyType::DIRECTION, 0, VERTICAL);
139 TestKeyParam(KeyType::DIRECTION, 1, HORIZONTAL);
140
141 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_PHONE, PHONE_STR);
142 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_TABLET, TABLET_STR);
143 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_CAR, CAR_STR);
144 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_PC, PC_STR);
145 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_TV, TV_STR);
146 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_WEARABLE, WEARABLE_STR);
147 TestKeyParam(KeyType::DEVICETYPE, DeviceType::DEVICE_NOT_SET, "not_device_type");
148
149 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_SDPI, RE_120_STR);
150 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_MDPI, RE_160_STR);
151 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_LDPI, RE_240_STR);
152 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_XLDPI, RE_320_STR);
153 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_XXLDPI, RE_480_STR);
154 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_XXXLDPI, RE_640_STR);
155 TestKeyParam(KeyType::SCREEN_DENSITY, ScreenDensity::SCREEN_DENSITY_NOT_SET, "not_screen_density");
156 }