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 "sec_comp_info_helper_test.h"
17
18 #include "accesstoken_kit.h"
19 #include "display.h"
20 #include "display_info.h"
21 #include "display_manager.h"
22 #include "location_button.h"
23 #include "paste_button.h"
24 #include "save_button.h"
25 #include "sec_comp_info_helper.h"
26 #include "sec_comp_log.h"
27 #include "sec_comp_err.h"
28 #include "sec_comp_tool.h"
29 #include "service_test_common.h"
30 #include "window_manager.h"
31
32 using namespace testing::ext;
33 using namespace OHOS;
34 using namespace OHOS::Security::SecurityComponent;
35
36 namespace {
37 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
38 LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompInfoHelperTest"};
39 static double g_curScreenWidth = 0.0F;
40 static double g_curScreenHeight = 0.0F;
41 static double g_testWidth = 0.0F;
42 static double g_testHeight = 0.0F;
43
GetScreenSize()44 static bool GetScreenSize()
45 {
46 sptr<OHOS::Rosen::Display> display =
47 OHOS::Rosen::DisplayManager::GetInstance().GetDefaultDisplaySync();
48 if (display == nullptr) {
49 return false;
50 }
51
52 auto info = display->GetDisplayInfo();
53 if (info == nullptr) {
54 return false;
55 }
56
57 g_curScreenWidth = static_cast<double>(info->GetWidth());
58 g_curScreenHeight = static_cast<double>(info->GetHeight());
59 return true;
60 }
61 }
62
SetUpTestCase()63 void SecCompInfoHelperTest::SetUpTestCase()
64 {
65 ASSERT_TRUE(GetScreenSize());
66 g_testWidth = (ServiceTestCommon::ZERO_OFFSET + g_curScreenWidth) / ServiceTestCommon::QUARTER;
67 g_testHeight = (ServiceTestCommon::ZERO_OFFSET + g_curScreenHeight) / ServiceTestCommon::QUARTER;
68 }
69
TearDownTestCase()70 void SecCompInfoHelperTest::TearDownTestCase()
71 {}
72
SetUp()73 void SecCompInfoHelperTest::SetUp()
74 {
75 SC_LOG_INFO(LABEL, "setup");
76 Rosen::WindowManager::GetInstance().SetDefaultSecCompScene();
77 }
78
TearDown()79 void SecCompInfoHelperTest::TearDown()
80 {}
81
82 /**
83 * @tc.name: ParseComponent001
84 * @tc.desc: Test parse component info success
85 * @tc.type: FUNC
86 * @tc.require: AR000HO9J7
87 */
88 HWTEST_F(SecCompInfoHelperTest, ParseComponent001, TestSize.Level1)
89 {
90 nlohmann::json jsonComponent;
91 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
92 std::string message;
93 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
94 ASSERT_NE(nullptr, comp);
95 ASSERT_TRUE(comp->GetValid());
96 }
97
98 /**
99 * @tc.name: ParseComponent002
100 * @tc.desc: Test parse component info with empty json
101 * @tc.type: FUNC
102 * @tc.require: AR000HO9J7
103 */
104 HWTEST_F(SecCompInfoHelperTest, ParseComponent002, TestSize.Level1)
105 {
106 nlohmann::json jsonComponent;
107 std::string message;
108 ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(UNKNOWN_SC_TYPE, jsonComponent, message));
109
110 ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message));
111 ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(PASTE_COMPONENT, jsonComponent, message));
112 ASSERT_EQ(nullptr, SecCompInfoHelper::ParseComponent(SAVE_COMPONENT, jsonComponent, message));
113 }
114
115 /**
116 * @tc.name: ParseComponent003
117 * @tc.desc: Test parse component info with invalid type
118 * @tc.type: FUNC
119 * @tc.require: AR000HO9J7
120 */
121 HWTEST_F(SecCompInfoHelperTest, ParseComponent003, TestSize.Level1)
122 {
123 nlohmann::json jsonComponent;
124 std::string message;
125 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
126
127 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
128 ASSERT_TRUE(comp->GetValid());
129
130 jsonComponent[JsonTagConstants::JSON_SC_TYPE] = UNKNOWN_SC_TYPE;
131 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
132 ASSERT_EQ(nullptr, comp);
133 }
134
GetDefaultRect(void)135 static SecCompRect GetDefaultRect(void)
136 {
137 SecCompRect rect = {
138 .x_ = g_testWidth,
139 .y_ = g_testHeight,
140 .width_ = g_testWidth,
141 .height_ = g_testHeight
142 };
143 return rect;
144 }
145
146 /**
147 * @tc.name: ParseComponent004
148 * @tc.desc: Test parse component info with invalid rect
149 * @tc.type: FUNC
150 * @tc.require: AR000HO9J7
151 */
152 HWTEST_F(SecCompInfoHelperTest, ParseComponent004, TestSize.Level1)
153 {
154 SecCompRect rect = GetDefaultRect();
155 SecCompRect windowRect = GetDefaultRect();
156 std::string message;
157 SecCompInfoHelper::ScreenInfo screenInfo = {
158 .displayId = 0,
159 .crossAxisState = CrossAxisState::STATE_INVALID,
160 .isWearable = false
161 };
162 ASSERT_TRUE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
163
164 rect.x_ = ServiceTestCommon::TEST_INVALID_DIMENSION;
165 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
166 rect.x_ = g_testWidth;
167
168 rect.y_ = ServiceTestCommon::TEST_INVALID_DIMENSION;
169 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
170 rect.y_ = g_testHeight;
171
172 rect.x_ = g_curScreenWidth + 1;
173 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
174 rect.x_ = g_testWidth;
175
176 rect.y_ = g_curScreenHeight + 1;
177 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
178 rect.y_ = g_testHeight;
179
180 rect.width_ = g_curScreenWidth;
181 rect.height_ = g_curScreenHeight;
182 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
183 rect.width_ = g_testWidth;
184 rect.height_ = g_testHeight;
185
186 rect.x_ = g_curScreenWidth - g_testWidth;
187 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
188 rect.x_ = g_testWidth;
189 rect.y_ = g_curScreenHeight - g_testHeight;
190
191 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
192 rect.y_ = g_testHeight;
193 }
194
195 /**
196 * @tc.name: ParseComponent005
197 * @tc.desc: Test parse component info with windowRect invalid
198 * @tc.type: FUNC
199 * @tc.require: AR000HO9J7
200 */
201 HWTEST_F(SecCompInfoHelperTest, ParseComponent005, TestSize.Level1)
202 {
203 SecCompRect rect = GetDefaultRect();
204 SecCompRect windowRect = GetDefaultRect();
205 std::string message;
206 SecCompInfoHelper::ScreenInfo screenInfo = {
207 .displayId = 0,
208 .crossAxisState = CrossAxisState::STATE_INVALID,
209 .isWearable = false
210 };
211 ASSERT_TRUE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
212
213 windowRect.x_ = g_testWidth + 2.0;
214 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
215 windowRect.x_ = g_testWidth;
216
217 windowRect.y_ = g_testHeight + 2.0;
218 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
219 windowRect.y_ = g_testHeight;
220
221 windowRect.width_ = g_testWidth - 2.0;
222 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
223 windowRect.width_ = g_testWidth;
224
225 windowRect.height_ = g_testHeight - 2.0;
226 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
227 windowRect.height_ = g_testHeight;
228
229 windowRect.width_ = ServiceTestCommon::TEST_INVALID_DIMENSION;
230 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
231 windowRect.width_ = g_testWidth;
232
233 windowRect.height_ = ServiceTestCommon::TEST_INVALID_DIMENSION;
234 ASSERT_FALSE(SecCompInfoHelper::CheckRectValid(rect, windowRect, screenInfo, message));
235 windowRect.height_ = g_testHeight;
236 }
237
238 /**
239 * @tc.name: ParseComponent006
240 * @tc.desc: Test parse component info with parentEffect active
241 * @tc.type: FUNC
242 * @tc.require: AR000HO9J7
243 */
244 HWTEST_F(SecCompInfoHelperTest, ParseComponent006, TestSize.Level1)
245 {
246 nlohmann::json jsonComponent;
247 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
248 std::string message;
249
250 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
251 ASSERT_TRUE(comp->GetValid());
252
253 jsonComponent[JsonTagConstants::JSON_PARENT_TAG] = nlohmann::json {
254 { JsonTagConstants::JSON_PARENT_EFFECT_TAG, true },
255 { JsonTagConstants::JSON_IS_CLIPPED_TAG, false },
256 { JsonTagConstants::JSON_TOP_CLIP_TAG, 0.0 },
257 { JsonTagConstants::JSON_BOTTOM_CLIP_TAG, 0.0 },
258 { JsonTagConstants::JSON_LEFT_CLIP_TAG, 0.0 },
259 { JsonTagConstants::JSON_RIGHT_CLIP_TAG, 0.0 },
260 { JsonTagConstants::JSON_PARENT_TAG_TAG, "" },
261 };
262 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
263 ASSERT_FALSE(comp->GetValid());
264 }
265
266 /**
267 * @tc.name: ParseComponent007
268 * @tc.desc: Test parse component info with invalid size
269 * @tc.type: FUNC
270 * @tc.require: AR000HO9J7
271 */
272 HWTEST_F(SecCompInfoHelperTest, ParseComponent007, TestSize.Level1)
273 {
274 nlohmann::json jsonComponent;
275 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
276 std::string message;
277
278 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
279 ASSERT_TRUE(comp->GetValid());
280
281 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
282 sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
283 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
284 ASSERT_FALSE(comp->GetValid());
285
286 sizeJson[JsonTagConstants::JSON_FONT_SIZE_TAG] = ServiceTestCommon::TEST_DIMENSION;
287 sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
288 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
289 ASSERT_FALSE(comp->GetValid());
290
291 sizeJson[JsonTagConstants::JSON_ICON_SIZE_TAG] = ServiceTestCommon::TEST_DIMENSION;
292 sizeJson[JsonTagConstants::JSON_TEXT_ICON_PADDING_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
293 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
294 ASSERT_FALSE(comp->GetValid());
295 }
296
297 /**
298 * @tc.name: ParseComponent008
299 * @tc.desc: Test parse component info with invalid size
300 * @tc.type: FUNC
301 * @tc.require: AR000HO9J7
302 */
303 HWTEST_F(SecCompInfoHelperTest, ParseComponent008, TestSize.Level1)
304 {
305 nlohmann::json jsonComponent;
306 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
307 std::string message;
308 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
309 ASSERT_TRUE(comp->GetValid());
310
311 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
312 auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG];
313 paddingJson[JsonTagConstants::JSON_TOP_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
314 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
315 ASSERT_FALSE(comp->GetValid());
316
317 paddingJson[JsonTagConstants::JSON_TOP_TAG] = ServiceTestCommon::TEST_DIMENSION;
318 paddingJson[JsonTagConstants::JSON_RIGHT_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
319 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
320 ASSERT_FALSE(comp->GetValid());
321 }
322
323 /**
324 * @tc.name: ParseComponent009
325 * @tc.desc: Test parse component info with invalid size
326 * @tc.type: FUNC
327 * @tc.require: AR000HO9J7
328 */
329 HWTEST_F(SecCompInfoHelperTest, ParseComponent009, TestSize.Level1)
330 {
331 nlohmann::json jsonComponent;
332 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
333 std::string message;
334 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
335 ASSERT_TRUE(comp->GetValid());
336
337 auto& sizesJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
338 auto& paddingsJson = sizesJson[JsonTagConstants::JSON_PADDING_SIZE_TAG];
339 paddingsJson[JsonTagConstants::JSON_BOTTOM_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
340 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
341 ASSERT_FALSE(comp->GetValid());
342
343 paddingsJson[JsonTagConstants::JSON_BOTTOM_TAG] = ServiceTestCommon::TEST_DIMENSION;
344 paddingsJson[JsonTagConstants::JSON_LEFT_TAG] = ServiceTestCommon::TEST_INVALID_DIMENSION;
345 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
346 ASSERT_FALSE(comp->GetValid());
347 }
348
349 /**
350 * @tc.name: ParseComponent010
351 * @tc.desc: Test parse component info with invalid color
352 * @tc.type: FUNC
353 * @tc.require: AR000HO9J7
354 */
355 HWTEST_F(SecCompInfoHelperTest, ParseComponent010, TestSize.Level1)
356 {
357 nlohmann::json jsonComponent;
358 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
359 std::string message;
360 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
361 ASSERT_TRUE(comp->GetValid());
362
363 auto& colorJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG];
364 colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_INVALID;
365 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
366 ASSERT_FALSE(comp->GetValid());
367
368 colorJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_RED;
369 colorJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_INVALID;
370 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
371 ASSERT_FALSE(comp->GetValid());
372 }
373
374 /**
375 * @tc.name: ParseComponent011
376 * @tc.desc: Test parse component info with invalid style
377 * @tc.type: FUNC
378 * @tc.require: AR000HO9J7
379 */
380 HWTEST_F(SecCompInfoHelperTest, ParseComponent011, TestSize.Level1)
381 {
382 nlohmann::json jsonComponent;
383 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
384 std::string message;
385 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
386 ASSERT_TRUE(comp->GetValid());
387
388 jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
389 { JsonTagConstants::JSON_TEXT_TAG, NO_TEXT },
390 { JsonTagConstants::JSON_ICON_TAG, NO_ICON },
391 { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE },
392 };
393 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
394 ASSERT_FALSE(comp->GetValid());
395 }
396
397 /**
398 * @tc.name: ParseComponent012
399 * @tc.desc: Test parse component info with invalid style
400 * @tc.type: FUNC
401 * @tc.require: AR000HO9J7
402 */
403 HWTEST_F(SecCompInfoHelperTest, ParseComponent012, TestSize.Level1)
404 {
405 nlohmann::json jsonComponent;
406 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
407 std::string message;
408 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
409 ASSERT_TRUE(comp->GetValid());
410
411 auto& colorsJson = jsonComponent[JsonTagConstants::JSON_COLORS_TAG];
412 colorsJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_YELLOW;
413 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
414 ASSERT_FALSE(comp->GetValid());
415
416 colorsJson[JsonTagConstants::JSON_FONT_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_RED;
417 colorsJson[JsonTagConstants::JSON_ICON_COLOR_TAG] = ServiceTestCommon::TEST_COLOR_YELLOW;
418 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
419 ASSERT_FALSE(comp->GetValid());
420
421 auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG];
422 styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE;
423 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
424 ASSERT_FALSE(comp->GetValid());
425 }
426
427 /**
428 * @tc.name: ParseComponent013
429 * @tc.desc: Test parse component info with invalid style
430 * @tc.type: FUNC
431 * @tc.require: AR000HO9J7
432 */
433 HWTEST_F(SecCompInfoHelperTest, ParseComponent013, TestSize.Level1)
434 {
435 nlohmann::json jsonComponent;
436 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
437 std::string message;
438 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
439 ASSERT_TRUE(comp->GetValid());
440
441 auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG];
442 styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE;
443 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
444 auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG];
445 paddingJson[JsonTagConstants::JSON_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG;
446 paddingJson[JsonTagConstants::JSON_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG;
447 paddingJson[JsonTagConstants::JSON_LEFT_TAG] = MIN_PADDING_WITHOUT_BG;
448 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
449 ASSERT_FALSE(comp->GetValid());
450
451 paddingJson[JsonTagConstants::JSON_TOP_TAG] = MIN_PADDING_WITHOUT_BG;
452 paddingJson[JsonTagConstants::JSON_RIGHT_TAG] = ServiceTestCommon::TEST_DIMENSION;
453 paddingJson[JsonTagConstants::JSON_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG;
454 paddingJson[JsonTagConstants::JSON_LEFT_TAG] = MIN_PADDING_WITHOUT_BG;
455 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
456 ASSERT_FALSE(comp->GetValid());
457 }
458
459 /**
460 * @tc.name: ParseComponent014
461 * @tc.desc: Test parse component info with invalid style
462 * @tc.type: FUNC
463 * @tc.require: AR000HO9J7
464 */
465 HWTEST_F(SecCompInfoHelperTest, ParseComponent014, TestSize.Level1)
466 {
467 nlohmann::json jsonComponent;
468 std::string message;
469 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
470 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
471 ASSERT_TRUE(comp->GetValid());
472
473 auto& styleJson = jsonComponent[JsonTagConstants::JSON_STYLE_TAG];
474 styleJson[JsonTagConstants::JSON_BG_TAG] = SecCompBackground::NO_BG_TYPE;
475
476 auto& sizeJson = jsonComponent[JsonTagConstants::JSON_SIZE_TAG];
477 auto& paddingJson = sizeJson[JsonTagConstants::JSON_PADDING_SIZE_TAG];
478 paddingJson[JsonTagConstants::JSON_TOP_TAG] = MIN_PADDING_WITHOUT_BG;
479 paddingJson[JsonTagConstants::JSON_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG;
480 paddingJson[JsonTagConstants::JSON_LEFT_TAG] = MIN_PADDING_WITHOUT_BG;
481 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
482 ASSERT_FALSE(comp->GetValid());
483
484 paddingJson[JsonTagConstants::JSON_TOP_TAG] = MIN_PADDING_WITHOUT_BG;
485 paddingJson[JsonTagConstants::JSON_RIGHT_TAG] = MIN_PADDING_WITHOUT_BG;
486 paddingJson[JsonTagConstants::JSON_BOTTOM_TAG] = MIN_PADDING_WITHOUT_BG;
487 paddingJson[JsonTagConstants::JSON_LEFT_TAG] = ServiceTestCommon::TEST_DIMENSION;
488 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
489 ASSERT_FALSE(comp->GetValid());
490 }
491
492
493 /**
494 * @tc.name: ParseComponent015
495 * @tc.desc: Test parse component info with similar color
496 * @tc.type: FUNC
497 * @tc.require: AR000HO9J7
498 */
499 HWTEST_F(SecCompInfoHelperTest, ParseComponent015, TestSize.Level1)
500 {
501 nlohmann::json jsonComponent;
502 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
503 std::string message;
504 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
505 ASSERT_TRUE(comp->GetValid());
506
507 jsonComponent[JsonTagConstants::JSON_STYLE_TAG] = nlohmann::json {
508 { JsonTagConstants::JSON_TEXT_TAG, LocationDesc::SELECT_LOCATION },
509 { JsonTagConstants::JSON_ICON_TAG, LocationIcon::LINE_ICON },
510 { JsonTagConstants::JSON_BG_TAG, SecCompBackground::CIRCLE }
511 };
512 jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
513 { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW },
514 { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK },
515 { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_YELLOW }
516 };
517 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
518 ASSERT_FALSE(comp->GetValid());
519 }
520
521 /**
522 * @tc.name: CheckComponentValid001
523 * @tc.desc: Test CheckComponentValid with invalid color
524 * @tc.type: FUNC
525 * @tc.require: AR000HO9J7
526 */
527 HWTEST_F(SecCompInfoHelperTest, CheckComponentValid001, TestSize.Level1)
528 {
529 nlohmann::json jsonComponent;
530 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
531 std::string message;
532 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
533 ASSERT_TRUE(comp->GetValid());
534 ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message));
535 jsonComponent[JsonTagConstants::JSON_COLORS_TAG] = nlohmann::json {
536 { JsonTagConstants::JSON_FONT_COLOR_TAG, ServiceTestCommon::TEST_COLOR_RED },
537 { JsonTagConstants::JSON_ICON_COLOR_TAG, ServiceTestCommon::TEST_COLOR_BLACK },
538 { JsonTagConstants::JSON_BG_COLOR_TAG, ServiceTestCommon::TEST_COLOR_WHITE }
539 };
540 comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
541 ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message));
542 }
543
544 /**
545 * @tc.name: CheckComponentValid002
546 * @tc.desc: Test CheckComponentValid with invalid text or icon
547 * @tc.type: FUNC
548 * @tc.require: AR000HO9J7
549 */
550 HWTEST_F(SecCompInfoHelperTest, CheckComponentValid002, TestSize.Level1)
551 {
552 nlohmann::json jsonComponent;
553 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
554 std::string message;
555 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
556 comp->text_ = UNKNOWN_TEXT;
557 ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message));
558
559 comp->text_ = static_cast<int32_t>(LocationDesc::SELECT_LOCATION);
560 comp->icon_ = UNKNOWN_ICON;
561 ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message));
562
563 comp->text_ = UNKNOWN_TEXT;
564 ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp, message));
565 }
566
567 /**
568 * @tc.name: CheckComponentValid003
569 * @tc.desc: Test CheckComponentValid with invalid type
570 * @tc.type: FUNC
571 * @tc.require: AR000HO9J7
572 */
573 HWTEST_F(SecCompInfoHelperTest, CheckComponentValid003, TestSize.Level1)
574 {
575 nlohmann::json jsonComponent;
576 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
577 std::string message;
578 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
579 comp->type_ = SecCompType::UNKNOWN_SC_TYPE;
580 ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp, message));
581
582 comp->type_ = SecCompType::MAX_SC_TYPE;
583 ASSERT_FALSE(SecCompInfoHelper::CheckComponentValid(comp, message));
584 }
585
586 /**
587 * @tc.name: CheckComponentValid004
588 * @tc.desc: Test CheckComponentValid with window scale changed
589 * @tc.type: FUNC
590 * @tc.require:
591 */
592 HWTEST_F(SecCompInfoHelperTest, CheckComponentValid004, TestSize.Level1)
593 {
594 nlohmann::json jsonComponent;
595 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
596 std::string message;
597 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
598
599 Rosen::WindowManager::GetInstance().result_ = Rosen::WMError::WM_OK;
600 std::vector<sptr<Rosen::AccessibilityWindowInfo>> list;
601 sptr<Rosen::AccessibilityWindowInfo> compWin = new Rosen::AccessibilityWindowInfo();
602 compWin->wid_ = 0;
603 compWin->layer_ = 1;
604 compWin->scaleVal_ = 0.99; // change window scale to 0.99
605 list.emplace_back(compWin);
606
607 sptr<Rosen::AccessibilityWindowInfo> otherWin = new Rosen::AccessibilityWindowInfo();
608 otherWin->wid_ = 1;
609 otherWin->layer_ = 0;
610 otherWin->scaleVal_ = 0.0;
611 list.emplace_back(otherWin);
612 Rosen::WindowManager::GetInstance().list_ = list;
613 ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message));
614 }
615
616 /**
617 * @tc.name: CheckComponentValid005
618 * @tc.desc: Test CheckComponentValid with get GetWindowScale failed
619 * @tc.type: FUNC
620 * @tc.require:
621 */
622 HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level1)
623 {
624 nlohmann::json jsonComponent;
625 ServiceTestCommon::BuildLocationComponentJson(jsonComponent);
626 std::string message;
627 SecCompBase* comp = SecCompInfoHelper::ParseComponent(LOCATION_COMPONENT, jsonComponent, message);
628
629 Rosen::WindowManager::GetInstance().result_ = static_cast<OHOS::Rosen::WMError>(-1);
630 ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message));
631 }
632
633 /**
634 * @tc.name: IsColorSimilar001
635 * @tc.desc: Test IsColorSimilar
636 * @tc.type: FUNC
637 * @tc.require:
638 */
639 HWTEST_F(SecCompInfoHelperTest, IsColorSimilar001, TestSize.Level1)
640 {
641 SecCompColor color1 = {
642 .argb = {
643 .red = 0xFF,
644 .green = 0xFF,
645 .blue = 0xFF,
646 .alpha = 0xFF,
647 }
648 };
649
650 SecCompColor color2 = {
651 .argb = {
652 .red = 0xFF,
653 .green = 0xFF,
654 .blue = 0xFF,
655 .alpha = 0xF0, // different alpha
656 }
657 };
658 EXPECT_TRUE(IsColorSimilar(color1, color2));
659 }
660