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