1 /*
2 * Copyright (c) 2025 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 <thread>
17 #include <vector>
18 #include "common/task_manager.h"
19 #include "component/box_progress_adapter.h"
20 #include "component/component_factory.h"
21 #include "component/component_register.h"
22 #include "component/label_btn_adapter.h"
23 #include "component/text_label_adapter.h"
24 #include "dock/focus_manager.h"
25 #include "gtest/gtest.h"
26 #include "ui_test_graphic_engine.h"
27 #include "view/page/view_proxy.h"
28 #include "view_api.h"
29
30 using namespace testing::ext;
31 using namespace Updater;
32
33 namespace {
34 class UpdaterUiComponentUnitTest : public testing::Test {
35 public:
SetUpTestCase(void)36 static void SetUpTestCase(void)
37 {
38 TestGraphicEngine::GetInstance();
39 }
TearDownTestCase(void)40 static void TearDownTestCase(void)
41 {
42 components_.clear();
43 }
SetUp()44 void SetUp() override
45 {
46 RegisterComponents();
47 }
48
TearDown()49 void TearDown() override {}
50
51 protected:
52 template <typename T>
53 T *CreateAdapterProxy(UxViewCommonInfo commonInfo, typename T::SpecificInfoType specInfo);
54
55 private:
56 inline static std::vector<std::unique_ptr<ViewProxy>> components_;
57 };
58
59 constexpr static int32_t MAX_PROGRESS_VALUE = 100;
60
CheckCommInfo(OHOS::UIView & view,const UxViewCommonInfo & common)61 void CheckCommInfo(OHOS::UIView &view, const UxViewCommonInfo &common)
62 {
63 EXPECT_EQ(view.GetX(), common.x);
64 EXPECT_EQ(view.GetY(), common.x);
65 EXPECT_EQ(view.GetWidth(), common.w);
66 EXPECT_EQ(view.GetHeight(), common.h);
67 EXPECT_STREQ(view.GetViewId(), common.id.c_str());
68 EXPECT_EQ(view.IsVisible(), common.visible);
69 }
70
71 template <typename T>
CreateAdapterProxy(UxViewCommonInfo commonInfo,typename T::SpecificInfoType specInfo)72 T *UpdaterUiComponentUnitTest::CreateAdapterProxy(UxViewCommonInfo commonInfo, typename T::SpecificInfoType specInfo)
73 {
74 UxViewInfo info {};
75 info.commonInfo = commonInfo;
76 info.specificInfo = ComponentFactory::CreateSpecificInfo(T::COMPONENT_TYPE);
77 if (info.specificInfo == nullptr) {
78 LOG(ERROR) << "create specific info failed, COMPONENT_TYPE: " << T::COMPONENT_TYPE;
79 return nullptr;
80 }
81
82 static_cast<SpecificInfoWrapper<T> *>(info.specificInfo.get())->data = specInfo;
83
84 std::unique_ptr<ComponentInterface> ptr = ComponentFactory::CreateView(T::COMPONENT_TYPE, info);
85 if (ptr == nullptr) {
86 LOG(ERROR) << "create component failed, COMPONENT_TYPE: " << T::COMPONENT_TYPE;
87 return nullptr;
88 }
89
90 CheckCommInfo(*static_cast<T *>(ptr.get()), info.commonInfo);
91
92 std::string message = std::string("component id:") + info.commonInfo.id + ", ";
93 std::unique_ptr<ViewProxy> proxy = std::make_unique<ViewProxy>(std::move(ptr), message);
94 T* raw = proxy->As<T>();
95 components_.emplace_back(std::move(proxy));
96 return raw;
97 }
98
99 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_constructor, TestSize.Level0)
100 {
101 UxBoxProgressInfo specInfo {50, "#ffffffff", "#000000ff", "", false};
102 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
103 BoxProgressAdapter *boxProgressPtr = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
104 ASSERT_NE(boxProgressPtr, nullptr);
105 BoxProgressAdapter &boxProgress = *boxProgressPtr;
106
107 auto fgColor = StrToColor(specInfo.fgColor);
108 auto bgColor = StrToColor(specInfo.bgColor);
109 EXPECT_EQ(boxProgress.GetBackgroundStyle(OHOS::STYLE_BACKGROUND_COLOR), bgColor.full);
110 EXPECT_EQ(boxProgress.GetBackgroundStyle(OHOS::STYLE_BACKGROUND_OPA), bgColor.alpha);
111 EXPECT_EQ(boxProgress.GetForegroundStyle(OHOS::STYLE_BACKGROUND_COLOR), fgColor.full);
112 EXPECT_EQ(boxProgress.GetForegroundStyle(OHOS::STYLE_BACKGROUND_OPA), fgColor.alpha);
113 EXPECT_EQ(boxProgress.GetValue(),
114 static_cast<int>(static_cast<float>(specInfo.defaultValue) / MAX_PROGRESS_VALUE * (commonInfo.w - 1)));
115 EXPECT_EQ(boxProgress.GetRangeMin(), 0);
116 EXPECT_EQ(boxProgress.GetRangeMax(), commonInfo.w - 1);
117 }
118
119 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_is_valid, TestSize.Level0)
120 {
121 EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {101, "", "", "", false}));
122 EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#", "", "", false}));
123 EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#000000ff", "", "", false}));
124 EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#000000ff", "#000000ff", "", true}));
125 EXPECT_TRUE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#000000ff", "#000000ff", "", false}));
126 }
127
128 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_set_value_without_ep, TestSize.Level0)
129 {
130 UxBoxProgressInfo specInfo {50, "#ffffffff", "#000000ff", "", false};
131 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
132 constexpr float validValue = 40;
133 {
134 BoxProgressAdapter *boxProgressPtr = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
135 ASSERT_NE(boxProgressPtr, nullptr);
136 BoxProgressAdapter &boxProgress = *boxProgressPtr;
137
138 boxProgress.SetValue(-1);
139 EXPECT_EQ(boxProgress.GetValue(), 0);
140 boxProgress.SetValue(validValue);
141 EXPECT_EQ(boxProgress.GetValue(), static_cast<int>(validValue / MAX_PROGRESS_VALUE * (commonInfo.w - 1)));
142 boxProgress.SetValue(MAX_PROGRESS_VALUE + 1);
143 EXPECT_EQ(boxProgress.GetValue(), commonInfo.w - 1);
144 }
145 {
146 specInfo.hasEp = true;
147 BoxProgressAdapter *boxProgressPtr = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
148 ASSERT_NE(boxProgressPtr, nullptr);
149 BoxProgressAdapter &boxProgress = *boxProgressPtr;
150
151 boxProgress.SetValue(validValue);
152 EXPECT_EQ(boxProgress.GetValue(), static_cast<int>(validValue / MAX_PROGRESS_VALUE * (commonInfo.w - 1)));
153 }
154 }
155
156 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_set_visible_without_ep, TestSize.Level0)
157 {
158 {
159 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
160 UxBoxProgressInfo specificInfo {50, "#ffffffff", "#000000ff", "", false};
161 BoxProgressAdapter *boxProgressPtr = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specificInfo);
162 ASSERT_NE(boxProgressPtr, nullptr);
163 BoxProgressAdapter &boxProgress = *boxProgressPtr;
164
165 boxProgress.SetVisible(true);
166 EXPECT_TRUE(boxProgress.IsVisible());
167 }
168 {
169 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
170 UxBoxProgressInfo specificInfo {50, "#ffffffff", "#000000ff", "", true};
171 BoxProgressAdapter *boxProgressPtr = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specificInfo);
172 ASSERT_NE(boxProgressPtr, nullptr);
173 BoxProgressAdapter &boxProgress = *boxProgressPtr;
174
175 boxProgress.SetVisible(true);
176 EXPECT_TRUE(boxProgress.IsVisible());
177 }
178 }
179
180 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_init_endpoint, TestSize.Level0)
181 {
182 UxBoxProgressInfo specInfo {50, "#ffffffff", "#000000ff", "", false};
183 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
184 {
185 BoxProgressAdapter *boxProgress = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
186 ASSERT_NE(boxProgress, nullptr);
187 EXPECT_TRUE(boxProgress->InitEp());
188 }
189 {
190 specInfo.hasEp = true;
191 BoxProgressAdapter *boxProgress = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
192 ASSERT_NE(boxProgress, nullptr);
193 EXPECT_FALSE(boxProgress->InitEp());
194 }
195 {
196 BoxProgressAdapter *boxProgress = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
197 ASSERT_NE(boxProgress, nullptr);
198 OHOS::UIViewGroup parent {};
199 parent.Add(boxProgress);
200 EXPECT_FALSE(boxProgress->InitEp());
201 }
202 {
203 constexpr auto epId = "endpoint";
204 specInfo.endPoint = epId;
205 BoxProgressAdapter *boxProgress = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
206 ASSERT_NE(boxProgress, nullptr);
207 LabelBtnAdapter labelBtn {};
208 labelBtn.SetViewId(epId);
209
210 OHOS::UIViewGroup parent {};
211 parent.Add(boxProgress);
212 parent.Add(&labelBtn);
213 EXPECT_FALSE(boxProgress->InitEp());
214 }
215 {
216 constexpr auto epId = "endpoint";
217 specInfo.endPoint = epId;
218 BoxProgressAdapter *boxProgress = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specInfo);
219 ASSERT_NE(boxProgress, nullptr);
220 ImgViewAdapter imgView {};
221 imgView.SetViewId(epId);
222
223 OHOS::UIViewGroup parent {};
224 parent.Add(boxProgress);
225 parent.Add(&imgView);
226 EXPECT_TRUE(boxProgress->InitEp());
227 }
228 }
229
230 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_with_ep, TestSize.Level0)
231 {
232 constexpr auto epId = "endpoint";
233 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
234 UxBoxProgressInfo specificInfo {50, "#ffffffff", "#000000ff", epId, true};
235 BoxProgressAdapter *boxProgressPtr = CreateAdapterProxy<BoxProgressAdapter>(commonInfo, specificInfo);
236 ASSERT_NE(boxProgressPtr, nullptr);
237 BoxProgressAdapter &boxProgress = *boxProgressPtr;
238
239 ImgViewAdapter epView {};
240 epView.SetViewId(epId);
241
242 OHOS::UIViewGroup parent {};
243 parent.Add(&boxProgress);
244 parent.Add(&epView);
245 EXPECT_TRUE(boxProgress.InitEp());
246
247 boxProgress.SetVisible(true);
248 EXPECT_TRUE(boxProgress.IsVisible());
249 EXPECT_TRUE(epView.IsVisible());
250
251 boxProgress.SetVisible(false);
252 EXPECT_FALSE(boxProgress.IsVisible());
253 EXPECT_FALSE(epView.IsVisible());
254
255 boxProgress.SetVisible(true);
256 float testValue = 50;
257 float halfDivisor = 2.0;
258 boxProgress.SetValue(testValue);
259 float rate = static_cast<float>(boxProgress.GetValue()) / boxProgress.GetRangeMax();
260 EXPECT_EQ(epView.GetX(), static_cast<int16_t>(boxProgress.GetX() -
261 epView.GetWidth() / halfDivisor + boxProgress.GetWidth() * rate));
262 EXPECT_EQ(epView.GetY(), static_cast<int16_t>(boxProgress.GetY() -
263 epView.GetHeight() / halfDivisor + boxProgress.GetHeight() / halfDivisor));
264 }
265
266 HWTEST_F(UpdaterUiComponentUnitTest, test_img_view_adapter_constructor, TestSize.Level0)
267 {
268 uint32_t interval = 0;
269 {
270 constexpr auto id = "img";
271 interval = 0;
272 UxImageInfo specInfo {"", "", 100, interval};
273 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, id, "UIImageView", false};
274 ImgViewAdapter *imgViewPtr = CreateAdapterProxy<ImgViewAdapter>(commonInfo, specInfo);
275 ASSERT_NE(imgViewPtr, nullptr);
276 ImgViewAdapter &imgView = *imgViewPtr;
277
278 EXPECT_EQ(imgView.GetX(), commonInfo.x);
279 EXPECT_EQ(imgView.GetY(), commonInfo.y);
280 EXPECT_EQ(imgView.GetWidth(), commonInfo.w);
281 EXPECT_EQ(imgView.GetHeight(), commonInfo.h);
282 EXPECT_EQ(imgView.IsVisible(), false);
283 EXPECT_STREQ(imgView.GetViewId(), id);
284 }
285 {
286 interval = 10; // assign a non-zero interval
287 constexpr auto id = "img";
288 UxImageInfo specInfo {"", "", 100, interval};
289 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, id, "UIImageView", false};
290 ImgViewAdapter *imgViewPtr = CreateAdapterProxy<ImgViewAdapter>(commonInfo, specInfo);
291 ASSERT_NE(imgViewPtr, nullptr);
292 ImgViewAdapter &imgView = *imgViewPtr;
293
294 ASSERT_NE(imgView.GetAnimatorCallback(), nullptr);
295 ASSERT_NE(imgView.GetAnimator(), nullptr);
296 }
297 }
298
299 HWTEST_F(UpdaterUiComponentUnitTest, test_img_view_adapter_is_valid, TestSize.Level0)
300 {
301 constexpr auto maxImgCnt = 300U;
302 constexpr auto maxInterval = 5000U;
303
304 // for static img
305 EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"", "", 100, 0}));
306 EXPECT_TRUE(ImgViewAdapter::IsValid(UxImageInfo {"resPath", "", 100, 0}));
307
308 // for animator
309 EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"", "", 100, 10}));
310 EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "", 100, 10}));
311 EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", maxImgCnt + 1, 10}));
312 EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", 0, 10}));
313 EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", 100, maxInterval + 1}));
314 EXPECT_TRUE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", 100, 10}));
315 }
316
317 HWTEST_F(UpdaterUiComponentUnitTest, test_img_view_adapter_start_stop, TestSize.Level0)
318 {
319 {
320 ImgViewAdapter imgView {};
321 EXPECT_FALSE(imgView.Start());
322 EXPECT_FALSE(imgView.Stop());
323 }
324 {
325 // non animator
326 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIImageView", false};
327 UxImageInfo specInfo {"respath", "", 100, 0};
328 ImgViewAdapter *imgViewPtr = CreateAdapterProxy<ImgViewAdapter>(commonInfo, specInfo);
329 ASSERT_NE(imgViewPtr, nullptr);
330 ImgViewAdapter &imgView = *imgViewPtr;
331
332 EXPECT_FALSE(imgView.Start());
333 EXPECT_FALSE(imgView.Stop());
334 }
335 {
336 using namespace std::literals::chrono_literals;
337 UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIImageView", false};
338 UxImageInfo specInfo {"", "fileprefix", 100, 10};
339 ImgViewAdapter *imgViewPtr = CreateAdapterProxy<ImgViewAdapter>(commonInfo, specInfo);
340 ASSERT_NE(imgViewPtr, nullptr);
341 ImgViewAdapter &imgView = *imgViewPtr;
342
343 uint32_t currId = 0;
344 EXPECT_FALSE(imgView.Stop()); // stop would fail when hasn't been started
345 EXPECT_TRUE(imgView.Start()); // start success
346 EXPECT_FALSE(imgView.Start()); // shouldn't start when it has been started
347 EXPECT_TRUE(imgView.IsVisible());
348
349 OHOS::TaskManager::GetInstance()->SetTaskRun(true); // set task can run
350 OHOS::AnimatorManager::GetInstance()->Init(); // add animator task
351 OHOS::TaskManager::GetInstance()->TaskHandler(); // run one loop
352 EXPECT_EQ(imgView.GetCurrId(), ++currId);
353 std::this_thread::sleep_for(0.1s);
354 OHOS::TaskManager::GetInstance()->TaskHandler();
355 EXPECT_EQ(imgView.GetCurrId(), ++currId);
356
357 EXPECT_TRUE(imgView.Stop()); // stop success
358 EXPECT_FALSE(imgView.Stop()); // consecutive stop would fail
359 EXPECT_FALSE(imgView.IsVisible());
360
361 OHOS::TaskManager::GetInstance()->TaskHandler();
362 EXPECT_EQ(imgView.GetCurrId(), currId);
363 OHOS::TaskManager::GetInstance()->TaskHandler();
364 EXPECT_EQ(imgView.GetCurrId(), currId);
365 OHOS::TaskManager::GetInstance()->SetTaskRun(false);
366 OHOS::TaskManager::GetInstance()->Remove(OHOS::AnimatorManager::GetInstance());
367 }
368 }
369
370 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_constructor, TestSize.Level0)
371 {
372 constexpr auto labelText = "hello";
373 UxLabelBtnInfo specInfo {100, "hello", "#ffffffff", "#000000ff", "#000000ff", "#ffffffff", true};
374 UxViewCommonInfo commonInfo {0, 0, 0, 0, "id", "UILabelButton", false};
375 LabelBtnAdapter *labelBtnPtr = CreateAdapterProxy<LabelBtnAdapter>(commonInfo, specInfo);
376 ASSERT_NE(labelBtnPtr, nullptr);
377 LabelBtnAdapter &labelBtn = *labelBtnPtr;
378
379 auto fontColor = StrToColor(specInfo.txtColor);
380 auto bgColor = StrToColor(specInfo.bgColor);
381 EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_TEXT_COLOR), fontColor.full);
382 EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_TEXT_OPA), fontColor.alpha);
383 EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), bgColor.full);
384 EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_BACKGROUND_OPA), bgColor.alpha);
385 EXPECT_EQ(labelBtn.IsFocusable(), specInfo.focusable);
386 EXPECT_STREQ(labelBtn.GetText(), labelText);
387 }
388
389 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_on_press, TestSize.Level0)
390 {
391 UxViewCommonInfo commonInfo1{0, 0, 50, 50, "id", "UILabelButton", true};
392 UxLabelBtnInfo specInfo1{100, "", "#000000ff", "#ffffffff", "#ffffffff", "#000000ff", true};
393 LabelBtnAdapter *labelBtn1Ptr = CreateAdapterProxy<LabelBtnAdapter>(commonInfo1, specInfo1);
394 ASSERT_NE(labelBtn1Ptr, nullptr);
395 LabelBtnAdapter &labelBtn1 = *labelBtn1Ptr;
396
397 UxViewCommonInfo commonInfo2{100, 100, 50, 50, "id", "UILabelButton", true};
398 UxLabelBtnInfo specInfo2{100, "", "#000000ff", "#ffffffff", "#ffffffff", "#000000ff", true};
399 LabelBtnAdapter *labelBtn2Ptr = CreateAdapterProxy<LabelBtnAdapter>(commonInfo2, specInfo2);
400 ASSERT_NE(labelBtn2Ptr, nullptr);
401 LabelBtnAdapter &labelBtn2 = *labelBtn2Ptr;
402
403 OHOS::FocusManager::GetInstance()->RequestFocus(&labelBtn2);
404 labelBtn1.OnPressEvent(OHOS::PressEvent {OHOS::Point {}});
405 EXPECT_EQ(OHOS::FocusManager::GetInstance()->GetFocusedView(), &labelBtn1);
406 EXPECT_EQ(labelBtn1.GetLabelStyle(OHOS::STYLE_TEXT_COLOR), (OHOS::ColorType {{0xff, 0xff, 0xff, 0xff}}.full));
407 EXPECT_EQ(labelBtn2.GetLabelStyle(OHOS::STYLE_TEXT_COLOR), (OHOS::ColorType {{0, 0, 0, 0xff}}.full));
408 EXPECT_EQ(labelBtn1.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), (OHOS::ColorType {{0, 0, 0, 0xff}}.full));
409 EXPECT_EQ(labelBtn2.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), (OHOS::ColorType {{0xff, 0xff, 0xff, 0xff}}.full));
410 OHOS::FocusManager::GetInstance()->ClearFocus();
411 }
412
413 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_is_valid, TestSize.Level0)
414 {
415 EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {201, "", "", "", "", "", false}));
416 EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff", "", "", "", false}));
417 EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff", "#000000ff", "", "", false}));
418 EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff",
419 "#000000ff", "#000000ff", "", false}));
420 EXPECT_TRUE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff",
421 "#000000ff", "#000000ff", "#000000ff", false}));
422 }
423
424 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_set_text, TestSize.Level0)
425 {
426 UxViewCommonInfo commonInfo {0, 0, 0, 0, "id", "UILabelButton", false};
427 UxLabelBtnInfo specInfo {100, "", "#000000ff", "#000000ff", "#000000ff", "#000000ff", false};
428 LabelBtnAdapter *labelBtnPtr = CreateAdapterProxy<LabelBtnAdapter>(commonInfo, specInfo);
429 ASSERT_NE(labelBtnPtr, nullptr);
430 LabelBtnAdapter &labelBtn = *labelBtnPtr;
431
432 constexpr auto testString = "test text";
433 labelBtn.SetText(testString);
434 EXPECT_STREQ(labelBtn.GetText(), testString);
435
436 labelBtn.SetText("*");
437 EXPECT_STREQ(labelBtn.GetText(), testString);
438
439 labelBtn.SetText("");
440 EXPECT_STREQ(labelBtn.GetText(), "");
441 }
442
443 HWTEST_F(UpdaterUiComponentUnitTest, test_text_label_adapter_is_info_valid, TestSize.Level0)
444 {
445 EXPECT_FALSE(TextLabelAdapter::IsValid(UxLabelInfo {255, "", "", "", ""}));
446 EXPECT_FALSE(TextLabelAdapter::IsValid(UxLabelInfo {255, "", "", "#000000ff", ""}));
447 EXPECT_TRUE(TextLabelAdapter::IsValid(UxLabelInfo {100, "", "", "#000000ff", "#000000ff"}));
448 }
449
450 HWTEST_F(UpdaterUiComponentUnitTest, test_text_label_adapter_constructor, TestSize.Level0)
451 {
452 constexpr auto labelText = "hello";
453 UxLabelInfo specInfo {100, "hello", "center", "#000000ff", "#000000ff", "normal",
454 {"#000000ff", "#000000ff", false}, false, "ellipsis"};
455 UxViewCommonInfo commonInfo {0, 0, 0, 0, "id", "UILabel", false};
456 TextLabelAdapter *textLabelPtr = CreateAdapterProxy<TextLabelAdapter>(commonInfo, specInfo);
457 ASSERT_NE(textLabelPtr, nullptr);
458 TextLabelAdapter &textLabel = *textLabelPtr;
459
460 auto fontColor = StrToColor(specInfo.fontColor);
461 auto bgColor = StrToColor(specInfo.bgColor);
462 EXPECT_EQ(textLabel.GetHorAlign(), GetAlign(specInfo.align));
463 EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_TEXT_COLOR), fontColor.full);
464 EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_TEXT_OPA), fontColor.alpha);
465 EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), bgColor.full);
466 EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_BACKGROUND_OPA), bgColor.alpha);
467 EXPECT_STREQ(textLabel.GetText(), labelText);
468 }
469
470 HWTEST_F(UpdaterUiComponentUnitTest, test_text_label_adapter_set_text, TestSize.Level0)
471 {
472 UxViewCommonInfo commonInfo {0, 0, 0, 0, "id", "UILabel", false};
473 UxLabelInfo specInfo {255, "", "", "#000000ff", "#000000ff", "normal",
474 {"#000000ff", "#000000ff", false}, false, "ellipsis"};
475
476 TextLabelAdapter *textLabelPtr = CreateAdapterProxy<TextLabelAdapter>(commonInfo, specInfo);
477 ASSERT_NE(textLabelPtr, nullptr);
478 TextLabelAdapter &textLabel = *textLabelPtr;
479
480 constexpr auto testString = "test text";
481 textLabel.SetText(testString);
482 EXPECT_STREQ(textLabel.GetText(), testString);
483
484 textLabel.SetText("*");
485 EXPECT_STREQ(textLabel.GetText(), testString);
486
487 textLabel.SetText("");
488 EXPECT_STREQ(textLabel.GetText(), "");
489 }
490 } // namespace
491