1 /*
2 * Copyright (c) 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
18 #include <libxml/globals.h>
19 #include <libxml/xmlstring.h>
20
21 #include "display_manager_config.h"
22 #include "display_manager_config.cpp"
23 #include "scene_board_judgement.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Rosen {
30 class DisplayManagerConfigTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 virtual void SetUp() override;
35 virtual void TearDown() override;
36 };
37
SetUpTestCase()38 void DisplayManagerConfigTest::SetUpTestCase()
39 {
40 }
41
TearDownTestCase()42 void DisplayManagerConfigTest::TearDownTestCase()
43 {
44 }
45
SetUp()46 void DisplayManagerConfigTest::SetUp()
47 {
48 }
49
TearDown()50 void DisplayManagerConfigTest::TearDown()
51 {
52 }
53
54 namespace {
55 /**
56 * @tc.name: Split
57 * @tc.desc: test function : Split
58 * @tc.type: FUNC
59 */
60 HWTEST_F(DisplayManagerConfigTest, Split, Function | SmallTest | Level1)
61 {
62 std::string str = "stringPatStr";
63 std::string pattern = "pattern";
64 std::vector<std::string> result(DisplayManagerConfig::Split(str, pattern));
65 EXPECT_EQ(result[0], "stringPatStr");
66 result.clear();
67 std::string str02 = "stringPatternStr";
68 std::vector<std::string> result02(DisplayManagerConfig::Split(str02, pattern));
69 EXPECT_EQ(result02[0], "stringPatternStr");
70 }
71
72 /**
73 * @tc.name: LoadConfigXml
74 * @tc.desc: test function : LoadConfigXml
75 * @tc.type: FUNC
76 */
77 HWTEST_F(DisplayManagerConfigTest, LoadConfigXml, Function | SmallTest | Level1)
78 {
79 bool result = DisplayManagerConfig::LoadConfigXml();
80 EXPECT_TRUE(result);
81 }
82
83 /**
84 * @tc.name: ReadIntNumbersConfigInfo
85 * @tc.desc: test function : ReadIntNumbersConfigInfo
86 * @tc.type: FUNC
87 */
88 HWTEST_F(DisplayManagerConfigTest, ReadIntNumbersConfigInfo, Function | SmallTest | Level1)
89 {
90 DisplayManagerConfig::enableConfig_.clear();
91
92 const char* xmlContent = "1 2 3 4";
93 xmlNodePtr validNode = xmlNewNode(NULL, BAD_CAST"TestNode");
94 xmlNodeSetContent(validNode, BAD_CAST xmlContent);
95 DisplayManagerConfig::ReadIntNumbersConfigInfo(validNode);
96
97 const char* invalidContent = "1 2 abc 4";
98 xmlNodePtr invalidNode = xmlNewNode(NULL, BAD_CAST"TestNode");
99 xmlNodeSetContent(invalidNode, BAD_CAST invalidContent);
100 DisplayManagerConfig::ReadIntNumbersConfigInfo(invalidNode);
101
102 EXPECT_LE(DisplayManagerConfig::enableConfig_.size(), 0);
103 xmlFreeNode(validNode);
104 xmlFreeNode(invalidNode);
105 }
106
107 /**
108 * @tc.name: IsNumber
109 * @tc.desc: test function : IsNumber
110 * @tc.type: FUNC
111 */
112 HWTEST_F(DisplayManagerConfigTest, IsNumber, Function | SmallTest | Level1)
113 {
114 bool result = DisplayManagerConfig::IsNumber("123");
115 ASSERT_EQ(true, result);
116 result = DisplayManagerConfig::IsNumber("a123");
117 ASSERT_EQ(false, result);
118 }
119
120 /**
121 * @tc.name: GetConfigPath
122 * @tc.desc: test function : GetConfigPath
123 * @tc.type: FUNC
124 */
125 HWTEST_F(DisplayManagerConfigTest, GetConfigPath, Function | SmallTest | Level1)
126 {
127 auto result = DisplayManagerConfig::GetConfigPath("");
128 ASSERT_STRNE("/system/", result.c_str());
129
130 result = DisplayManagerConfig::GetConfigPath("a.xml");
131 ASSERT_STREQ("/system/a.xml", result.c_str());
132 }
133
134 /**
135 * @tc.name: ReadEnableConfigInfo
136 * @tc.desc: test function : ReadEnableConfigInfo
137 * @tc.type: FUNC
138 */
139 HWTEST_F(DisplayManagerConfigTest, ReadEnableConfigInfo, Function | SmallTest | Level1)
140 {
141 DisplayManagerConfig::enableConfig_.clear();
142
143 auto configFilePath = DisplayManagerConfig::GetConfigPath("etc/window/resources/display_manager_config.xml");
144 xmlDocPtr docPtr = xmlReadFile(configFilePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
145 if (docPtr == nullptr) {
146 return;
147 }
148
149 xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
150 if (rootPtr == nullptr || rootPtr->name == nullptr ||
151 xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar*>("Configs"))) {
152 xmlFreeDoc(docPtr);
153 return;
154 }
155 uint32_t readCount = 0;
156 for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
157 if (!DisplayManagerConfig::IsValidNode(*curNodePtr)) {
158 continue;
159 }
160
161 auto nodeName = curNodePtr->name;
162 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("isWaterfallDisplay"))) {
163 DisplayManagerConfig::ReadEnableConfigInfo(curNodePtr);
164 readCount++;
165 continue;
166 }
167
168 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("dpi"))) {
169 DisplayManagerConfig::ReadEnableConfigInfo(curNodePtr);
170 readCount++;
171 continue;
172 }
173 }
174
175 ASSERT_LE(DisplayManagerConfig::enableConfig_.size(), readCount);
176
177 DisplayManagerConfig::DumpConfig();
178 xmlFreeDoc(docPtr);
179 }
180
181 /**
182 * @tc.name: ReadStringConfigInfo
183 * @tc.desc: test function : ReadStringConfigInfo
184 * @tc.type: FUNC
185 */
186 HWTEST_F(DisplayManagerConfigTest, ReadStringConfigInfo, Function | SmallTest | Level1)
187 {
188 DisplayManagerConfig::enableConfig_.clear();
189
190 auto configFilePath = DisplayManagerConfig::GetConfigPath("etc/window/resources/display_manager_config.xml");
191 xmlDocPtr docPtr = xmlReadFile(configFilePath.c_str(), nullptr, XML_PARSE_NOBLANKS);
192 if (docPtr == nullptr) {
193 return;
194 }
195
196 xmlNodePtr rootPtr = xmlDocGetRootElement(docPtr);
197 if (rootPtr == nullptr || rootPtr->name == nullptr ||
198 xmlStrcmp(rootPtr->name, reinterpret_cast<const xmlChar*>("Configs"))) {
199 xmlFreeDoc(docPtr);
200 return;
201 }
202 uint32_t readCount = 0;
203 for (xmlNodePtr curNodePtr = rootPtr->xmlChildrenNode; curNodePtr != nullptr; curNodePtr = curNodePtr->next) {
204 if (!DisplayManagerConfig::IsValidNode(*curNodePtr)) {
205 continue;
206 }
207
208 auto nodeName = curNodePtr->name;
209 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("defaultDisplayCutoutPath"))) {
210 DisplayManagerConfig::ReadStringConfigInfo(curNodePtr);
211 readCount++;
212 continue;
213 }
214
215 if (!xmlStrcmp(nodeName, reinterpret_cast<const xmlChar*>("dpi"))) {
216 DisplayManagerConfig::ReadStringConfigInfo(curNodePtr);
217 readCount++;
218 continue;
219 }
220 }
221
222 ASSERT_LE(DisplayManagerConfig::stringConfig_.size(), readCount);
223 DisplayManagerConfig::DumpConfig();
224 xmlFreeDoc(docPtr);
225 }
226
227 }
228 } // namespace Rosen
229 } // namespace OHOS