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