• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <test_header.h>
18 
19 #include "hgm_test_base.h"
20 
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 class HgmXmlParserTest : public testing::Test {
28 public:
29     static void SetUpTestCase();
30     static void TearDownTestCase();
31     void SetUp();
32     void TearDown();
33     void LoadXmlContent1();
34     static constexpr char config[] = "/sys_prod/etc/graphic/hgm_policy_config.xml";
35     static constexpr char invalidConfig[] = "/sys_prod/etc/graphic/invalid_config.xml";
36 };
37 
SetUpTestCase()38 void HgmXmlParserTest::SetUpTestCase()
39 {
40     HgmTestBase::SetUpTestCase();
41 }
TearDownTestCase()42 void HgmXmlParserTest::TearDownTestCase() {}
SetUp()43 void HgmXmlParserTest::SetUp() {}
TearDown()44 void HgmXmlParserTest::TearDown() {}
45 
LoadXmlContent1()46 void HgmXmlParserTest::LoadXmlContent1()
47 {
48     // test xml file TEST_XML_CONTENT_1;
49     auto& hgmCore = HgmCore::Instance();
50     std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
51     parser->mParsedData_ = std::make_unique<PolicyConfigData>();
52     ASSERT_NE(parser->mParsedData_, nullptr);
53     parser->xmlDocument_ = StringToXmlDoc(TEST_XML_CONTENT_1);
54     ASSERT_NE(parser->xmlDocument_, nullptr);
55     parser->Parse();
56     hgmCore.mPolicyConfigData_ = parser->GetParsedData();
57     hgmCore.mParser_ = std::move(parser);
58 }
59 
60 /**
61  * @tc.name: LoadConfiguration
62  * @tc.desc: Verify the result of LoadConfiguration function
63  * @tc.type: FUNC
64  * @tc.require: I7DMS1
65  */
66 HWTEST_F(HgmXmlParserTest, LoadConfiguration, Function | SmallTest | Level0)
67 {
68     std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
69 
70     PART("CaseDescription") {
71         STEP("1. get an xml parser") {
72             STEP_ASSERT_NE(parser, nullptr);
73         }
74         STEP("2. check the result of configuration") {
75             int32_t load = parser->LoadConfiguration(invalidConfig);
76             STEP_ASSERT_EQ(load, static_cast<int32_t>(HgmErrCode::XML_FILE_LOAD_FAIL));
77             STEP_ASSERT_EQ(parser->xmlDocument_, nullptr);
78             STEP_ASSERT_EQ(parser->Parse(), static_cast<int32_t>(HgmErrCode::HGM_ERROR));
79             STEP_ASSERT_EQ(parser->mParsedData_, nullptr);
80             load = parser->LoadConfiguration(config);
81             STEP_ASSERT_GE(load, 0);
82             if (parser->mParsedData_ == nullptr) {
83                 return;
84             }
85             STEP_ASSERT_NE(parser->mParsedData_, nullptr);
86             load = parser->LoadConfiguration(config);
87             STEP_ASSERT_GE(load, 0);
88         }
89     }
90 }
91 
92 /**
93  * @tc.name: Parse
94  * @tc.desc: Verify the result of parsing functions
95  * @tc.type: FUNC
96  * @tc.require: I7DMS1
97  */
98 HWTEST_F(HgmXmlParserTest, Parse, Function | SmallTest | Level0)
99 {
100     std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
101     int32_t load = parser->LoadConfiguration(config);
102     EXPECT_GE(load, EXEC_SUCCESS);
103     parser->xmlDocument_ = xmlReadFile(config, nullptr, 0);
104     if (parser->xmlDocument_) {
105         auto parserData = std::move(parser->mParsedData_);
106         EXPECT_EQ(parser->mParsedData_, nullptr);
107         EXPECT_EQ(parser->Parse(), XML_PARSE_INTERNAL_FAIL);
108         parser->mParsedData_ = std::move(parserData);
109         EXPECT_NE(parser->mParsedData_, nullptr);
110         EXPECT_EQ(parser->Parse(), EXEC_SUCCESS);
111     } else {
112         parser->Parse();
113     }
114     parser->GetParsedData();
115 }
116 
117 /**
118  * @tc.name: IsNumber
119  * @tc.desc: Verify the result of IsNumber function
120  * @tc.type: FUNC
121  * @tc.require: IBCFDD
122  */
123 HWTEST_F(HgmXmlParserTest, IsNumber, Function | SmallTest | Level0)
124 {
125     std::vector<std::pair<std::string, bool>> cases = {
126         { "", false },
127         { "123456789", false },
128         { "a023", false },
129         { "02a3", false },
130         { "023a", false },
131         { "123", true },
132         { "-123", true },
133         { "023", true },
134         { "12345678", true }
135     };
136     for (const auto& [str, res] : cases) {
137         EXPECT_EQ(XMLParser::IsNumber(str), res);
138     }
139 }
140 
141 /**
142  * @tc.name: StringToVector001
143  * @tc.desc: Verify the result of StringToVector001 functions
144  * @tc.type: FUNC
145  * @tc.require:
146  */
147 HWTEST_F(HgmXmlParserTest, StringToVector001, Function | SmallTest | Level0)
148 {
149     std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
150     std::string emptyInput = "";
151     std::vector<uint32_t> result = parser->StringToVector(emptyInput);
152     EXPECT_TRUE(result.empty());
153 }
154 
155 /**
156  * @tc.name: StringToVector002
157  * @tc.desc: Verify the result of StringToVector002 functions
158  * @tc.type: FUNC
159  * @tc.require:
160  */
161 HWTEST_F(HgmXmlParserTest, StringToVector002, Function | SmallTest | Level0)
162 {
163     std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
164     std::string spacesBetweenNumbersInput = "1 2   3  45 ";
165     std::vector<uint32_t> expected = { 1, 2, 3, 45 };
166     std::vector<uint32_t> result = parser->StringToVector(spacesBetweenNumbersInput);
167     EXPECT_EQ(expected, result);
168 }
169 
170 /**
171  * @tc.name: StringToVector003
172  * @tc.desc: Verify the result of StringToVector003 functions
173  * @tc.type: FUNC
174  * @tc.require:
175  */
176 HWTEST_F(HgmXmlParserTest, StringToVector003, Function | SmallTest | Level0)
177 {
178     std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
179     std::string invalidInput = "abc";
180     std::vector<uint32_t> result = parser->StringToVector(invalidInput);
181     EXPECT_TRUE(result.empty());
182 }
183 
184 /**
185  * @tc.name: ParseParams001
186  * @tc.desc: Verify the result of ParseParams function
187  * @tc.type: FUNC
188  * @tc.require: IBCFDD
189  */
190 HWTEST_F(HgmXmlParserTest, ParseParams001, Function | SmallTest | Level1)
191 {
192     LoadXmlContent1();
193     auto& hgmCore = HgmCore::Instance();
194     const auto& settingMap = hgmCore.mPolicyConfigData_->refreshRateForSettingsMap_;
195     // test config for pcmode parse OK
196     auto it = settingMap.find("pcmode");
197     EXPECT_TRUE(it != settingMap.end());
198     auto pcmodeSettings = it->second;
199     EXPECT_GT(pcmodeSettings.size(), 0);
200     // test member refreshRateForSettings_ has been aligned
201     const auto& settings = hgmCore.mPolicyConfigData_->refreshRateForSettings_;
202     ASSERT_TRUE(settings.size() > 0);
203     int32_t name = settings.at(2).first;
204     EXPECT_EQ(name, 120);
205     int32_t id = settings.at(2).second;
206     EXPECT_EQ(id, 2);
207 }
208 
209 /**
210  * @tc.name: ParseRefreshRate4Settings001
211  * @tc.desc: Verify the result of ParseRefreshRate4Settings function
212  * @tc.type: FUNC
213  * @tc.require: IBCFDD
214  */
215 HWTEST_F(HgmXmlParserTest, ParseRefreshRate4Settings001, Function | SmallTest | Level1)
216 {
217     // test xml file TEST_XML_CONTENT_1;
218     LoadXmlContent1();
219     auto& hgmCore = HgmCore::Instance();
220     const auto& settingMap = hgmCore.mPolicyConfigData_->refreshRateForSettingsMap_;
221     // test config parse OK
222     auto it = settingMap.find("pcmode");
223     EXPECT_TRUE(it != settingMap.end());
224     // test member refreshRateForSettings_ has been aligned
225     const auto& settings = hgmCore.mPolicyConfigData_->refreshRateForSettings_;
226     EXPECT_GT(settings.size(), 0);
227 }
228 } // namespace Rosen
229 } // namespace OHOS