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 <fstream>
17 #include <vector>
18 #include "file_ex.h"
19 #include "gtest/gtest.h"
20 #include "layout/layout_parser.h"
21 #include "view_api.h"
22
23 using namespace Updater;
24 using namespace std;
25 using namespace testing::ext;
26 namespace Fs = std::filesystem;
27 namespace Updater {
operator ==(const UxViewCommonInfo & lhs,const UxViewCommonInfo & rhs)28 bool operator == (const UxViewCommonInfo &lhs, const UxViewCommonInfo &rhs)
29 {
30 return std::tie(lhs.x, lhs.y, lhs.w, lhs.h, lhs.id, lhs.type, lhs.visible) ==
31 std::tie(rhs.x, rhs.y, rhs.w, rhs.h, rhs.id, rhs.type, rhs.visible);
32 }
33
operator ==(const UxBoxProgressInfo & lhs,const UxBoxProgressInfo & rhs)34 bool operator == (const UxBoxProgressInfo &lhs, const UxBoxProgressInfo &rhs)
35 {
36 return std::tie(lhs.defaultValue, lhs.fgColor, lhs.bgColor, lhs.hasEp, lhs.endPoint) ==
37 std::tie(rhs.defaultValue, rhs.fgColor, rhs.bgColor, rhs.hasEp, rhs.endPoint);
38 }
39
operator ==(const UxLabelInfo & lhs,const UxLabelInfo & rhs)40 bool operator == (const UxLabelInfo &lhs, const UxLabelInfo &rhs)
41 {
42 return std::tie(lhs.text, lhs.bgColor, lhs.align, lhs.fontColor, lhs.fontSize) ==
43 std::tie(rhs.text, rhs.bgColor, rhs.align, rhs.fontColor, rhs.fontSize);
44 }
45
operator ==(const UxImageInfo & lhs,const UxImageInfo & rhs)46 bool operator == (const UxImageInfo &lhs, const UxImageInfo &rhs)
47 {
48 return std::tie(lhs.imgCnt, lhs.updInterval, lhs.resPath, lhs.filePrefix) ==
49 std::tie(rhs.imgCnt, rhs.updInterval, rhs.resPath, rhs.filePrefix);
50 }
51
operator ==(const UxLabelBtnInfo & lhs,const UxLabelBtnInfo & rhs)52 bool operator == (const UxLabelBtnInfo &lhs, const UxLabelBtnInfo &rhs)
53 {
54 return std::tie(lhs.fontSize, lhs.text, lhs.txtColor, lhs.bgColor, lhs.focusedBgColor, lhs.focusedTxtColor) ==
55 std::tie(rhs.fontSize, rhs.text, rhs.txtColor, rhs.bgColor, rhs.focusedBgColor, rhs.focusedTxtColor);
56 }
57
operator ==(const UxViewInfo & lhs,const UxViewInfo & rhs)58 bool operator == (const UxViewInfo &lhs, const UxViewInfo &rhs)
59 {
60 return lhs.commonInfo == rhs.commonInfo && lhs.specificInfo == rhs.specificInfo;
61 }
62 }
63 namespace {
64 class UpdaterUiLayoutParserUnitTest : public testing::Test {
65 public:
66 static void SetUpTestCase(void);
67 static void TearDownTestCase(void);
68 void SetUp() override;
69 void TearDown() override;
70 };
71
72
73 // do something at the each function begining
SetUp(void)74 void UpdaterUiLayoutParserUnitTest::SetUp(void)
75 {
76 cout << "Updater Unit UpdaterUiLayoutParserUnitTest Begin!" << endl;
77 }
78
79 // do something at the each function end
TearDown(void)80 void UpdaterUiLayoutParserUnitTest::TearDown(void)
81 {
82 cout << "Updater Unit UpdaterUiLayoutParserUnitTest End!" << endl;
83 }
84
85 // init
SetUpTestCase(void)86 void UpdaterUiLayoutParserUnitTest::SetUpTestCase(void)
87 {
88 cout << "SetUpTestCase" << endl;
89 }
90
91 // end
TearDownTestCase(void)92 void UpdaterUiLayoutParserUnitTest::TearDownTestCase(void)
93 {
94 cout << "TearDownTestCase" << endl;
95 }
96
97 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_label_parser, TestSize.Level0)
98 {
99 UxPageInfo pageInfo;
100 LayoutParser::GetInstance().LoadLayout("/data/updater/ui/label.json", pageInfo);
101 ASSERT_EQ(pageInfo.viewInfos.size(), 1UL);
102 EXPECT_EQ(pageInfo.id, "menu"s);
103
104 UxViewInfo expectedLabel { UxViewCommonInfo { 0, 0, 100, 100, "label_id_0", "UILabel", true },
105 UxLabelInfo {
106 10,
107 "Reboot to normal system",
108 "center",
109 "#ffffffff",
110 "#ffffffff"
111 } };
112 EXPECT_EQ(pageInfo.viewInfos[0], expectedLabel);
113 }
114
115 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_progress_parser, TestSize.Level0)
116 {
117 UxPageInfo pageInfo;
118 LayoutParser::GetInstance().LoadLayout("/data/updater/ui/boxprogress.json", pageInfo);
119 ASSERT_EQ(pageInfo.viewInfos.size(), 1UL);
120 EXPECT_EQ(pageInfo.id, "menu"s);
121
122 UxViewInfo expectedProgress { UxViewCommonInfo { 0, 0, 100, 100, "box_progress_0", "UIBoxProgress", true },
123 UxBoxProgressInfo { 10, "#ffffffff", "#ffffffff" } };
124 EXPECT_EQ(pageInfo.viewInfos[0], expectedProgress);
125 }
126
127 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_image_view_parser, TestSize.Level0)
128 {
129 UxPageInfo pageInfo;
130 LayoutParser::GetInstance().LoadLayout("/data/updater/ui/imageview.json", pageInfo);
131 ASSERT_EQ(pageInfo.viewInfos.size(), 1UL);
132 EXPECT_EQ(pageInfo.id, "menu"s);
133
134 UxViewInfo expectedImage { UxViewCommonInfo { 0, 0, 100, 100, "image_view", "UIImageView", true },
135 UxImageInfo { "/res/images", "", 100, 1 } };
136 EXPECT_EQ(pageInfo.viewInfos[0], expectedImage);
137 }
138
139 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_all, TestSize.Level1)
140 {
141 UxPageInfo pageInfo;
142 LayoutParser::GetInstance().LoadLayout("/data/updater/ui/all.json", pageInfo);
143 ASSERT_EQ(pageInfo.viewInfos.size(), 3UL);
144 EXPECT_EQ(pageInfo.id, "menu"s);
145
146 UxViewInfo expectedLabel { UxViewCommonInfo { 0, 0, 100, 100, "label_id_0", "UILabel", true },
147 UxLabelInfo {
148 10,
149 "Reboot to normal system",
150 "center",
151 "#ffffffff",
152 "#ffffffff"
153 } };
154 EXPECT_EQ(pageInfo.viewInfos[0], expectedLabel);
155
156 UxViewInfo expectedImage { UxViewCommonInfo { 0, 0, 100, 100, "image_view", "UIImageView", true },
157 UxImageInfo { "/res/images", "", 100, 1 } };
158 EXPECT_EQ(pageInfo.viewInfos[1], expectedImage);
159
160 UxViewInfo expectedProgress { UxViewCommonInfo { 0, 0, 100, 100, "box_progress_0", "UIBoxProgress",
161 true },
162 UxBoxProgressInfo { 10, "#ffffffff", "#ffffffff", "", false } };
163 EXPECT_EQ(pageInfo.viewInfos[2], expectedProgress);
164 }
165
166 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_all_default, TestSize.Level0)
167 {
168 UxPageInfo pageInfo;
169 LayoutParser::GetInstance().LoadLayout("/data/updater/ui/menu.json", pageInfo);
170
171 ASSERT_EQ(pageInfo.viewInfos.size(), 8UL);
172 EXPECT_EQ(pageInfo.id, "menu"s);
173
174 UxViewInfo expected { UxViewCommonInfo { 1280, 0, 800, 200, "Label_RebootToNormalSystem", "UILabel", true },
175 UxLabelInfo {
176 60,
177 "Reboot to normal system",
178 "center",
179 "#ffffffff",
180 "#000000ff"
181 } };
182 expected.commonInfo.visible = false;
183 EXPECT_EQ(expected.commonInfo, pageInfo.viewInfos[0].commonInfo);
184 EXPECT_EQ(expected.specificInfo, pageInfo.viewInfos[0].specificInfo);
185 EXPECT_EQ(expected, pageInfo.viewInfos[0]);
186
187 expected.commonInfo = { 1280, 200, 800, 200, "Label_UserdataReset", "UILabel", true };
188 std::get<UxLabelInfo>(expected.specificInfo).text = "Userdata reset";
189 EXPECT_EQ(expected, pageInfo.viewInfos[1]);
190
191 expected.commonInfo = { 1280, 400, 800, 200, "Label_UpdateFromSDCard", "UILabel", true };
192 std::get<UxLabelInfo>(expected.specificInfo).text = "Update from SD Card";
193 EXPECT_EQ(expected, pageInfo.viewInfos[2]);
194
195 expected.commonInfo = { 1280, 600, 800, 200, "Label_MenuDialogTitle", "UILabel", true };
196 std::get<UxLabelInfo>(expected.specificInfo).text = "Tip";
197 std::get<UxLabelInfo>(expected.specificInfo).fontSize = 40;
198 EXPECT_EQ(expected, pageInfo.viewInfos[3]);
199
200 expected.commonInfo = { 1280, 800, 800, 200, "Label_MenuDialogNote", "UILabel", true };
201 std::get<UxLabelInfo>(expected.specificInfo).text = "Delete user date now...";
202 EXPECT_EQ(expected, pageInfo.viewInfos[4]);
203
204 expected.commonInfo = { 1280, 1000, 800, 200, "Label_MenuDialogNext", "UILabel", true };
205 std::get<UxLabelInfo>(expected.specificInfo).text = "Do you want to continue?";
206 EXPECT_EQ(expected, pageInfo.viewInfos[5]);
207
208 expected.commonInfo = { 1280, 1200, 800, 200, "Label_MenuDialogOK", "UILabel", true };
209 std::get<UxLabelInfo>(expected.specificInfo).text = "Continue";
210 EXPECT_EQ(expected, pageInfo.viewInfos[6]);
211
212 expected.commonInfo = { 1680, 1200, 800, 200, "Label_MenuDialogCancel", "UILabel", true };
213 std::get<UxLabelInfo>(expected.specificInfo).text = "Cancel";
214 EXPECT_EQ(expected, pageInfo.viewInfos[7]);
215 }
216
217 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_load_multiple_page_info, TestSize.Level0)
218 {
219 std::vector<std::string> layoutFiles { "/data/updater/ui/imageview.json", "/data/updater/ui/boxprogress.json" };
220 std::vector<UxPageInfo> pageInfos {};
221 LayoutParser::GetInstance().LoadLayout(layoutFiles, pageInfos);
222
223 ASSERT_EQ(pageInfos.size(), 2ul);
224 UxViewInfo expectedImage { UxViewCommonInfo { 0, 0, 100, 100, "image_view", "UIImageView", true },
225 UxImageInfo { "/res/images", "", 100, 1 } };
226 EXPECT_EQ(pageInfos[0].id, "menu");
227 ASSERT_EQ(pageInfos[0].viewInfos.size(), 1ul);
228 EXPECT_EQ(pageInfos[0].viewInfos[0], expectedImage);
229
230 UxViewInfo expectedProgress { UxViewCommonInfo { 0, 0, 100, 100, "box_progress_0", "UIBoxProgress", true },
231 UxBoxProgressInfo { 10, "#ffffffff", "#ffffffff" } };
232 EXPECT_EQ(pageInfos[1].id, "menu");
233 ASSERT_EQ(pageInfos[1].viewInfos.size(), 1ul);
234 EXPECT_EQ(pageInfos[1].viewInfos[0].commonInfo, expectedProgress.commonInfo);
235 }
236
237 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_load_sub_page_info, TestSize.Level0)
238 {
239 std::vector<std::string> layoutFiles { "/data/updater/ui/subpage.json" };
240 std::vector<UxPageInfo> pageInfos {};
241 LayoutParser::GetInstance().LoadLayout(layoutFiles, pageInfos);
242
243 ASSERT_EQ(pageInfos.size(), 1ul);
244 auto &subPages = pageInfos[0].subpages;
245 ASSERT_EQ(subPages.size(), 1UL);
246 EXPECT_EQ(subPages[0].id, "subpage1");
247 EXPECT_EQ(subPages[0].coms.size(), 3UL);
248 EXPECT_EQ(subPages[0].coms[0], "a");
249 EXPECT_EQ(subPages[0].coms[1], "b");
250 EXPECT_EQ(subPages[0].coms[2], "c");
251 }
252
253 HWTEST_F(UpdaterUiLayoutParserUnitTest, test_invalid_cases, TestSize.Level0)
254 {
255 constexpr std::array files { "/data/updater/ui/noPageInfo.json", "/data/updater/ui/noComs.json",
256 "/data/updater/ui/comsNoType.json", "/data/updater/ui/commonInvalid.json",
257 "/data/updater/ui/typeInvalid.json", "/data/updater/ui/incompleteComInfo.json"};
258 for (auto file : files) {
259 std::vector<std::string> layoutFiles { file };
260 std::vector<UxPageInfo> pageInfos {};
261 EXPECT_EQ(true, OHOS::FileExists(file)) << file;
262 EXPECT_EQ(false, LayoutParser::GetInstance().LoadLayout(layoutFiles, pageInfos));
263 }
264 }
265 } // namespace
266