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 "xml_parser.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace Rosen {
26 class HgmXmlParserTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32
33 static constexpr char config[] = "/sys_prod/etc/graphic/hgm_policy_config.xml";
34 static constexpr char invalidConfig[] = "/sys_prod/etc/graphic/invalid_config.xml";
35 };
36
SetUpTestCase()37 void HgmXmlParserTest::SetUpTestCase() {}
TearDownTestCase()38 void HgmXmlParserTest::TearDownTestCase() {}
SetUp()39 void HgmXmlParserTest::SetUp() {}
TearDown()40 void HgmXmlParserTest::TearDown() {}
41
42 /**
43 * @tc.name: LoadConfiguration
44 * @tc.desc: Verify the result of LoadConfiguration function
45 * @tc.type: FUNC
46 * @tc.require: I7DMS1
47 */
48 HWTEST_F(HgmXmlParserTest, LoadConfiguration, Function | SmallTest | Level1)
49 {
50 std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
51
52 PART("CaseDescription") {
53 STEP("1. get an xml parser") {
54 STEP_ASSERT_NE(parser, nullptr);
55 }
56 STEP("2. check the result of configuration") {
57 int32_t load = parser->LoadConfiguration(invalidConfig);
58 STEP_ASSERT_EQ(load, static_cast<int32_t>(HgmErrCode::XML_FILE_LOAD_FAIL));
59 STEP_ASSERT_EQ(parser->xmlDocument_, nullptr);
60 STEP_ASSERT_EQ(parser->Parse(), static_cast<int32_t>(HgmErrCode::HGM_ERROR));
61 STEP_ASSERT_EQ(parser->mParsedData_, nullptr);
62 load = parser->LoadConfiguration(config);
63 STEP_ASSERT_GE(load, 0);
64 if (parser->mParsedData_ == nullptr) {
65 return;
66 }
67 STEP_ASSERT_NE(parser->mParsedData_, nullptr);
68 load = parser->LoadConfiguration(config);
69 STEP_ASSERT_GE(load, 0);
70 }
71 }
72 }
73
74 /**
75 * @tc.name: Parse
76 * @tc.desc: Verify the result of parsing functions
77 * @tc.type: FUNC
78 * @tc.require: I7DMS1
79 */
80 HWTEST_F(HgmXmlParserTest, Parse, Function | SmallTest | Level1)
81 {
82 std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
83 int32_t load = parser->LoadConfiguration(config);
84 EXPECT_GE(load, EXEC_SUCCESS);
85 parser->Parse();
86 parser->GetParsedData();
87 }
88
89 /**
90 * @tc.name: StringToVector001
91 * @tc.desc: Verify the result of StringToVector001 functions
92 * @tc.type: FUNC
93 * @tc.require:
94 */
95 HWTEST_F(HgmXmlParserTest, StringToVector001, Function | SmallTest | Level1)
96 {
97 std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
98 std::string emptyInput = "";
99 std::vector<uint32_t> result = parser->StringToVector(emptyInput);
100 EXPECT_TRUE(result.empty());
101 }
102
103 /**
104 * @tc.name: StringToVector002
105 * @tc.desc: Verify the result of StringToVector002 functions
106 * @tc.type: FUNC
107 * @tc.require:
108 */
109 HWTEST_F(HgmXmlParserTest, StringToVector002, Function | SmallTest | Level1)
110 {
111 std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
112 std::string spacesBetweenNumbersInput = "1 2 3 45 ";
113 std::vector<uint32_t> expected = {1, 2, 3, 45};
114 std::vector<uint32_t> result = parser->StringToVector(spacesBetweenNumbersInput);
115 EXPECT_EQ(expected, result);
116 }
117
118 /**
119 * @tc.name: StringToVector003
120 * @tc.desc: Verify the result of StringToVector003 functions
121 * @tc.type: FUNC
122 * @tc.require:
123 */
124 HWTEST_F(HgmXmlParserTest, StringToVector003, Function | SmallTest | Level1)
125 {
126 std::unique_ptr<XMLParser> parser = std::make_unique<XMLParser>();
127 std::string invalidInput = "abc";
128 std::vector<uint32_t> result = parser->StringToVector(invalidInput);
129 EXPECT_TRUE(result.empty());
130 }
131
132 /**
133 * @tc.name: IsNumber
134 * @tc.desc: Verify the result of IsNumber function
135 * @tc.type: FUNC
136 * @tc.require: IBCFDD
137 */
138 HWTEST_F(HgmXmlParserTest, IsNumber, Function | SmallTest | Level1)
139 {
140 std::vector<std::pair<std::string, bool>> cases = {
141 {"", false},
142 {"123456789", false},
143 {"a023", false},
144 {"02a3", false},
145 {"023a", false},
146 {"123", true},
147 {"-123", true},
148 {"023", true},
149 {"12345678", true}
150 };
151 for (const auto& [str, res] : cases) {
152 EXPECT_EQ(XMLParser::IsNumber(str), res);
153 }
154 }
155 } // namespace Rosen
156 } // namespace OHOS