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 #include "location_button_test.h"
16
17 #include <string>
18 #include "sec_comp_log.h"
19 #include "sec_comp_err.h"
20 #include "test_common.h"
21
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::Security::SecurityComponent;
25
26 namespace {
27 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
28 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "LocationButtonTest"};
29 static const std::string WRONG_TYPE = "wrongType";
30 }
31
SetUpTestCase()32 void LocationButtonTest::SetUpTestCase()
33 {}
34
TearDownTestCase()35 void LocationButtonTest::TearDownTestCase()
36 {}
37
SetUp()38 void LocationButtonTest::SetUp()
39 {
40 SC_LOG_INFO(LABEL, "setup");
41 }
42
TearDown()43 void LocationButtonTest::TearDown()
44 {}
45
46 /**
47 * @tc.name: FromJson001
48 * @tc.desc: Test LocationButton from json success
49 * @tc.type: FUNC
50 * @tc.require: AR000HO9J7
51 */
52 HWTEST_F(LocationButtonTest, FromJson001, TestSize.Level1)
53 {
54 nlohmann::json jsonComponent;
55 TestCommon::BuildLocationComponentInfo(jsonComponent);
56 LocationButton comp;
57 ASSERT_TRUE(comp.FromJson(jsonComponent));
58 }
59
60 /**
61 * @tc.name: FromJson002
62 * @tc.desc: Test empty LocationButton from json
63 * @tc.type: FUNC
64 * @tc.require: AR000HO9J7
65 */
66 HWTEST_F(LocationButtonTest, FromJson002, TestSize.Level1)
67 {
68 nlohmann::json jsonComponent;
69 LocationButton comp;
70 ASSERT_FALSE(comp.FromJson(jsonComponent));
71 }
72
73 /**
74 * @tc.name: FromJson003
75 * @tc.desc: Test location button from wrong type json
76 * @tc.type: FUNC
77 * @tc.require: AR000HO9J7
78 */
79 HWTEST_F(LocationButtonTest, FromJson003, TestSize.Level1)
80 {
81 nlohmann::json jsonComponent;
82 TestCommon::BuildLocationComponentInfo(jsonComponent);
83 LocationButton comp;
84 ASSERT_TRUE(comp.FromJson(jsonComponent));
85
86 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = WRONG_TYPE;
87 ASSERT_FALSE(comp.FromJson(jsonComponent));
88
89 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = 0;
90 ASSERT_FALSE(comp.FromJson(jsonComponent));
91 }
92
93 /**
94 * @tc.name: FromJson004
95 * @tc.desc: Test location button from wrong rect json
96 * @tc.type: FUNC
97 * @tc.require: AR000HO9J7
98 */
99 HWTEST_F(LocationButtonTest, FromJson004, TestSize.Level1)
100 {
101 nlohmann::json jsonComponent;
102 LocationButton comp;
103 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT;
104 nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false);
105 jsonComponent[JsonTagConstants::JSON_RECT] = wrongJson;
106 ASSERT_FALSE(comp.FromJson(jsonComponent));
107
108 TestCommon::BuildLocationComponentInfo(jsonComponent);
109 ASSERT_TRUE(comp.FromJson(jsonComponent));
110
111 auto& rectJson = jsonComponent[JsonTagConstants::JSON_RECT];
112 rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE;
113 ASSERT_FALSE(comp.FromJson(jsonComponent));
114
115 rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE;
116 rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE;
117 ASSERT_FALSE(comp.FromJson(jsonComponent));
118
119 rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE;
120 rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE;
121 ASSERT_FALSE(comp.FromJson(jsonComponent));
122
123 rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE;
124 rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE;
125 ASSERT_FALSE(comp.FromJson(jsonComponent));
126 }
127
128 /**
129 * @tc.name: FromJson005
130 * @tc.desc: Test location button from wrong rect json
131 * @tc.type: FUNC
132 * @tc.require: AR000HO9J7
133 */
134 HWTEST_F(LocationButtonTest, FromJson005, TestSize.Level1)
135 {
136 nlohmann::json jsonComponent;
137 LocationButton comp;
138 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = LOCATION_COMPONENT;
139 nlohmann::json wrongJson = nlohmann::json::parse("{", nullptr, false);
140 jsonComponent[JsonTagConstants::JSON_WINDOW_RECT] = wrongJson;
141 ASSERT_FALSE(comp.FromJson(jsonComponent));
142
143 TestCommon::BuildLocationComponentInfo(jsonComponent);
144 ASSERT_TRUE(comp.FromJson(jsonComponent));
145
146 auto& rectJson = jsonComponent[JsonTagConstants::JSON_WINDOW_RECT];
147 rectJson[JsonTagConstants::JSON_RECT_X] = WRONG_TYPE;
148 ASSERT_FALSE(comp.FromJson(jsonComponent));
149
150 rectJson[JsonTagConstants::JSON_RECT_X] = TestCommon::TEST_COORDINATE;
151 rectJson[JsonTagConstants::JSON_RECT_Y] = WRONG_TYPE;
152 ASSERT_FALSE(comp.FromJson(jsonComponent));
153
154 rectJson[JsonTagConstants::JSON_RECT_Y] = TestCommon::TEST_COORDINATE;
155 rectJson[JsonTagConstants::JSON_RECT_WIDTH] = WRONG_TYPE;
156 ASSERT_FALSE(comp.FromJson(jsonComponent));
157
158 rectJson[JsonTagConstants::JSON_RECT_WIDTH] = TestCommon::TEST_COORDINATE;
159 rectJson[JsonTagConstants::JSON_RECT_HEIGHT] = WRONG_TYPE;
160 ASSERT_FALSE(comp.FromJson(jsonComponent));
161 }
162
163 /**
164 * @tc.name: FromJson006
165 * @tc.desc: Test location button from wrong size json
166 * @tc.type: FUNC
167 * @tc.require: AR000HO9J7
168 */
169 HWTEST_F(LocationButtonTest, FromJson006, TestSize.Level1)
170 {
171 nlohmann::json jsonComponent;
172 LocationButton comp;
173 TestCommon::BuildLocationComponentInfo(jsonComponent);
174 ASSERT_TRUE(comp.FromJson(jsonComponent));
175
176 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
177 sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = WRONG_TYPE;
178 ASSERT_FALSE(comp.FromJson(jsonComponent));
179
180 sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = TestCommon::TEST_SIZE;
181 sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = WRONG_TYPE;
182 ASSERT_FALSE(comp.FromJson(jsonComponent));
183
184 sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = TestCommon::TEST_SIZE;
185 sizeJson[JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG] = WRONG_TYPE;
186 ASSERT_FALSE(comp.FromJson(jsonComponent));
187 }
188
189 /**
190 * @tc.name: FromJson007
191 * @tc.desc: Test location button from wrong size json
192 * @tc.type: FUNC
193 * @tc.require: AR000HO9J7
194 */
195 HWTEST_F(LocationButtonTest, FromJson007, TestSize.Level1)
196 {
197 nlohmann::json jsonComponent;
198 LocationButton comp;
199 TestCommon::BuildLocationComponentInfo(jsonComponent);
200 ASSERT_TRUE(comp.FromJson(jsonComponent));
201
202 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
203 auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG];
204 paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = WRONG_TYPE;
205 ASSERT_FALSE(comp.FromJson(jsonComponent));
206
207 paddingJson[JsonTagConstants::JSON_PADDING_TOP_TAG] = TestCommon::TEST_DIMENSION;
208 paddingJson[JsonTagConstants::JSON_PADDING_RIGHT_TAG] = WRONG_TYPE;
209 ASSERT_FALSE(comp.FromJson(jsonComponent));
210 }
211
212 /**
213 * @tc.name: FromJson008
214 * @tc.desc: Test location button from wrong size json
215 * @tc.type: FUNC
216 * @tc.require: AR000HO9J7
217 */
218 HWTEST_F(LocationButtonTest, FromJson008, TestSize.Level1)
219 {
220 nlohmann::json jsonComponent;
221 LocationButton comp;
222 TestCommon::BuildLocationComponentInfo(jsonComponent);
223 ASSERT_TRUE(comp.FromJson(jsonComponent));
224
225 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
226 auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG];
227 paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = WRONG_TYPE;
228 ASSERT_FALSE(comp.FromJson(jsonComponent));
229
230 paddingJson[JsonTagConstants::JSON_PADDING_BOTTOM_TAG] = TestCommon::TEST_DIMENSION;
231 paddingJson[JsonTagConstants::JSON_PADDING_LEFT_TAG] = WRONG_TYPE;
232 ASSERT_FALSE(comp.FromJson(jsonComponent));
233 }
234
235 /**
236 * @tc.name: FromJson009
237 * @tc.desc: Test location button from wrong border and parent json
238 * @tc.type: FUNC
239 * @tc.require: AR000HO9J7
240 */
241 HWTEST_F(LocationButtonTest, FromJson009, TestSize.Level1)
242 {
243 nlohmann::json jsonComponent;
244 LocationButton comp;
245 TestCommon::BuildLocationComponentInfo(jsonComponent);
246 ASSERT_TRUE(comp.FromJson(jsonComponent));
247
248 jsonComponent[JsonTagConstants::JSON_BORDER_TAG] = nlohmann::json {
249 { JsonTagConstants::JSON_BORDER_WIDTH_TAG, WRONG_TYPE },
250 };
251 ASSERT_FALSE(comp.FromJson(jsonComponent));
252
253 TestCommon::BuildLocationComponentInfo(jsonComponent);
254 jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
255 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, WRONG_TYPE },
256 };
257 ASSERT_FALSE(comp.FromJson(jsonComponent));
258 }
259
260 /**
261 * @tc.name: FromJson010
262 * @tc.desc: Test location button from wrong type params json
263 * @tc.type: FUNC
264 * @tc.require: AR000HO9J7
265 */
266 HWTEST_F(LocationButtonTest, FromJson010, TestSize.Level1)
267 {
268 nlohmann::json jsonComponent;
269 TestCommon::BuildLocationComponentInfo(jsonComponent);
270 LocationButton button;
271 ASSERT_TRUE(button.FromJson(jsonComponent));
272
273 auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG];
274 styleJson[JsonTagConstants::JSON_TEXT_TAG] = WRONG_TYPE;
275 ASSERT_FALSE(button.FromJson(jsonComponent));
276
277 styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION;
278 styleJson[JsonTagConstants::JSON_ICON_TAG] = WRONG_TYPE;
279 ASSERT_FALSE(button.FromJson(jsonComponent));
280
281 styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON;
282 styleJson[JsonTagConstants::JSON_BG_TAG] = WRONG_TYPE;
283 ASSERT_FALSE(button.FromJson(jsonComponent));
284 }
285
286 /**
287 * @tc.name: FromJson011
288 * @tc.desc: Test location button from wrong type params json
289 * @tc.type: FUNC
290 * @tc.require: AR000HO9J7
291 */
292 HWTEST_F(LocationButtonTest, FromJson011, TestSize.Level1)
293 {
294 nlohmann::json jsonComponent;
295 TestCommon::BuildLocationComponentInfo(jsonComponent);
296 LocationButton button;
297 ASSERT_TRUE(button.FromJson(jsonComponent));
298
299 auto& colorJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG];
300 colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = WRONG_TYPE;
301 ASSERT_FALSE(button.FromJson(jsonComponent));
302
303 colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = TestCommon::TEST_COLOR_RED;
304 colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = WRONG_TYPE;
305 ASSERT_FALSE(button.FromJson(jsonComponent));
306
307 colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = TestCommon::TEST_COLOR_BLUE;
308 colorJson[JsonTagConstants::JSON_BG_COLOR_TAG] = WRONG_TYPE;
309 ASSERT_FALSE(button.FromJson(jsonComponent));
310 }
311
312 /**
313 * @tc.name: FromJson012
314 * @tc.desc: Test location button from wrong value params json
315 * @tc.type: FUNC
316 * @tc.require: AR000HO9J7
317 */
318 HWTEST_F(LocationButtonTest, FromJson012, TestSize.Level1)
319 {
320 nlohmann::json jsonComponent;
321 TestCommon::BuildLocationComponentInfo(jsonComponent);
322 LocationButton button;
323
324 ASSERT_TRUE(button.FromJson(jsonComponent));
325
326 auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG];
327 styleJson[JsonTagConstants::JSON_TEXT_TAG] = UNKNOWN_TEXT;
328 ASSERT_FALSE(button.FromJson(jsonComponent));
329
330 styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION;
331 styleJson[JsonTagConstants::JSON_ICON_TAG] = UNKNOWN_ICON;
332 ASSERT_FALSE(button.FromJson(jsonComponent));
333
334 styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON;
335 styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::UNKNOWN_BG;
336 ASSERT_FALSE(button.FromJson(jsonComponent));
337
338 styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::CIRCLE;
339 styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::MAX_LABEL_TYPE;
340 ASSERT_FALSE(button.FromJson(jsonComponent));
341
342 styleJson[JsonTagConstants::JSON_TEXT_TAG] = LocationDesc::SELECT_LOCATION;
343 styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::MAX_ICON_TYPE;
344 ASSERT_FALSE(button.FromJson(jsonComponent));
345
346 styleJson[JsonTagConstants::JSON_ICON_TAG] = LocationIcon::LINE_ICON;
347 styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::MAX_BG_TYPE;
348 ASSERT_FALSE(button.FromJson(jsonComponent));
349 }
350
351 /**
352 * @tc.name: ToJsonStr001
353 * @tc.desc: Test ToJsonStr normal branch
354 * @tc.type: FUNC
355 * @tc.require: AR000HO9J7
356 */
357 HWTEST_F(LocationButtonTest, ToJsonStr001, TestSize.Level1)
358 {
359 nlohmann::json jsonComponent;
360 TestCommon::BuildLocationComponentInfo(jsonComponent);
361 LocationButton button;
362
363 ASSERT_TRUE(button.FromJson(jsonComponent));
364 ASSERT_EQ(jsonComponent.dump(), button.ToJsonStr());
365 }
366
367 /**
368 * @tc.name: CompareComponentBasicInfo001
369 * @tc.desc: Test CompareComponentBasicInfo other is null
370 * @tc.type: FUNC
371 * @tc.require: AR000HO9J7
372 */
373 HWTEST_F(LocationButtonTest, CompareComponentBasicInfo001, TestSize.Level1)
374 {
375 nlohmann::json jsonComponent;
376 TestCommon::BuildLocationComponentInfo(jsonComponent);
377 LocationButton button;
378
379 ASSERT_FALSE(button.CompareComponentBasicInfo(nullptr, true));
380 }
381
382 /**
383 * @tc.name: CompareLocationButton001
384 * @tc.desc: Test LocationButton compare
385 * @tc.type: FUNC
386 * @tc.require: AR000HO9J7
387 */
388 HWTEST_F(LocationButtonTest, CompareLocationButton001, TestSize.Level1)
389 {
390 LocationButton button1;
391 LocationButton button2;
392
393 nlohmann::json jsonComponent;
394 TestCommon::BuildLocationComponentInfo(jsonComponent);
395
396 ASSERT_TRUE(button1.FromJson(jsonComponent));
397 ASSERT_TRUE(button2.FromJson(jsonComponent));
398 ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true));
399
400 button1.text_ = UNKNOWN_TEXT;
401 ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true));
402 button1.text_ = static_cast<int32_t>(LocationDesc::SELECT_LOCATION);
403
404 button1.icon_ = UNKNOWN_ICON;
405 ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true));
406 button1.icon_ = static_cast<int32_t>(LocationIcon::LINE_ICON);
407
408 button1.bg_ = SecCompBackground::UNKNOWN_BG;
409 ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, true));
410 button1.bg_ = SecCompBackground::CIRCLE;
411
412 ASSERT_TRUE(button1.CompareComponentBasicInfo(&button2, true));
413 }
414
415 /**
416 * @tc.name: CompareLocationButton002
417 * @tc.desc: Test LocationButton compare
418 * @tc.type: FUNC
419 * @tc.require: AR000HO9J7
420 */
421 HWTEST_F(LocationButtonTest, CompareLocationButton002, TestSize.Level1)
422 {
423 nlohmann::json jsonComponent;
424 LocationButton comp1;
425 TestCommon::BuildLocationComponentInfo(jsonComponent);
426 ASSERT_TRUE(comp1.FromJson(jsonComponent));
427 LocationButton comp2 = comp1;
428
429 comp1.type_ = SAVE_COMPONENT;
430 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
431 comp1.type_ = LOCATION_COMPONENT;
432
433 comp1.fontSize_ = DEFAULT_DIMENSION;
434 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
435 comp1.fontSize_ = TestCommon::TEST_SIZE;
436
437 comp1.iconSize_ = DEFAULT_DIMENSION;
438 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
439 comp1.iconSize_ = TestCommon::TEST_SIZE;
440
441 comp1.padding_.top = DEFAULT_DIMENSION;
442 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
443 comp1.padding_.top = TestCommon::TEST_DIMENSION;
444
445 comp1.padding_.right = DEFAULT_DIMENSION;
446 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
447 comp1.padding_.right = TestCommon::TEST_DIMENSION;
448
449 comp1.padding_.bottom = DEFAULT_DIMENSION;
450 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
451 comp1.padding_.bottom = TestCommon::TEST_DIMENSION;
452
453 comp1.padding_.left = DEFAULT_DIMENSION;
454 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
455 comp1.padding_.left = TestCommon::TEST_DIMENSION;
456
457 comp1.textIconSpace_ = DEFAULT_DIMENSION;
458 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
459 comp1.textIconSpace_ = TestCommon::TEST_SIZE;
460
461 comp1.borderWidth_ = DEFAULT_DIMENSION;
462 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
463 comp1.borderWidth_ = TestCommon::TEST_SIZE;
464
465 comp1.fontColor_.value = TestCommon::TEST_DIFF_COLOR;
466 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
467 comp1.fontColor_.value = TestCommon::TEST_COLOR_RED;
468
469 comp1.bgColor_.value = TestCommon::TEST_DIFF_COLOR;
470 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
471 comp1.bgColor_.value = TestCommon::TEST_COLOR_YELLOW;
472
473 comp1.iconColor_.value = TestCommon::TEST_DIFF_COLOR;
474 ASSERT_FALSE(comp1.CompareComponentBasicInfo(&comp2, true));
475 comp1.iconColor_.value = TestCommon::TEST_COLOR_BLUE;
476
477 ASSERT_TRUE(comp1.CompareComponentBasicInfo(&comp2, true));
478 }
479
480 /**
481 * @tc.name: CompareLocationButton003
482 * @tc.desc: Test LocationButton compare
483 * @tc.type: FUNC
484 * @tc.require: AR000HO9J7
485 */
486 HWTEST_F(LocationButtonTest, CompareLocationButton003, TestSize.Level1)
487 {
488 LocationButton button1;
489 SaveButton button2;
490 nlohmann::json jsonComponent;
491 TestCommon::BuildLocationComponentInfo(jsonComponent);
492
493 ASSERT_TRUE(button1.FromJson(jsonComponent));
494 ASSERT_FALSE(button1.CompareComponentBasicInfo(&button2, false));
495 }
496