• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "gtest/gtest.h"
18 #include "component/box_progress_adapter.h"
19 #include "component/label_btn_adapter.h"
20 #include "component/text_label_adapter.h"
21 #include "common/task_manager.h"
22 #include "dock/focus_manager.h"
23 #include "ui_test_graphic_engine.h"
24 
25 using namespace testing::ext;
26 using namespace Updater;
27 
28 namespace {
29 class UpdaterUiComponentUnitTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void)
32     {
33         TestGraphicEngine::GetInstance();
34     }
TearDownTestCase(void)35     static void TearDownTestCase(void) {}
SetUp()36     void SetUp() override {}
TearDown()37     void TearDown() override {}
38 };
39 
40 constexpr static int32_t MAX_PROGRESS_VALUE = 100;
41 
CheckCommInfo(OHOS::UIView & view,const UxViewCommonInfo & common)42 void CheckCommInfo(OHOS::UIView &view, const UxViewCommonInfo &common)
43 {
44     EXPECT_EQ(view.GetX(), common.x);
45     EXPECT_EQ(view.GetY(), common.x);
46     EXPECT_EQ(view.GetWidth(), common.w);
47     EXPECT_EQ(view.GetHeight(), common.h);
48     EXPECT_STREQ(view.GetViewId(), common.id.c_str());
49     EXPECT_EQ(view.IsVisible(), common.visible);
50 }
51 
52 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_constructor, TestSize.Level0)
53 {
54     UxBoxProgressInfo specInfo {50, "#ffffffff", "#000000ff", "", false};
55     UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
56     BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
57     CheckCommInfo(boxProgress, commonInfo);
58 
59     auto fgColor = StrToColor(specInfo.fgColor);
60     auto bgColor = StrToColor(specInfo.bgColor);
61     EXPECT_EQ(boxProgress.GetBackgroundStyle(OHOS::STYLE_BACKGROUND_COLOR), bgColor.full);
62     EXPECT_EQ(boxProgress.GetBackgroundStyle(OHOS::STYLE_BACKGROUND_OPA), bgColor.alpha);
63     EXPECT_EQ(boxProgress.GetForegroundStyle(OHOS::STYLE_BACKGROUND_COLOR), fgColor.full);
64     EXPECT_EQ(boxProgress.GetForegroundStyle(OHOS::STYLE_BACKGROUND_OPA), fgColor.alpha);
65     EXPECT_EQ(boxProgress.GetValue(), static_cast<int>(static_cast<float>(specInfo.defaultValue) /
66         MAX_PROGRESS_VALUE * (commonInfo.w - 1)));
67     EXPECT_EQ(boxProgress.GetRangeMin(), 0);
68     EXPECT_EQ(boxProgress.GetRangeMax(), commonInfo.w - 1);
69 }
70 
71 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_is_valid, TestSize.Level0)
72 {
73     EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {101, "", "", "", false}));
74     EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#", "", "", false}));
75     EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#000000ff", "", "", false}));
76     EXPECT_FALSE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#000000ff", "#000000ff", "", true}));
77     EXPECT_TRUE(BoxProgressAdapter::IsValid(UxBoxProgressInfo {50, "#000000ff", "#000000ff", "", false}));
78 }
79 
80 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_set_value_without_ep, TestSize.Level0)
81 {
82     UxBoxProgressInfo specInfo {50, "#ffffffff", "#000000ff", "", false};
83     UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
84     constexpr float validValue = 40;
85     {
86         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
87 
88         boxProgress.SetValue(-1);
89         EXPECT_EQ(boxProgress.GetValue(), 0);
90         boxProgress.SetValue(validValue);
91         EXPECT_EQ(boxProgress.GetValue(), static_cast<int>(validValue / MAX_PROGRESS_VALUE * (commonInfo.w - 1)));
92         boxProgress.SetValue(MAX_PROGRESS_VALUE + 1);
93         EXPECT_EQ(boxProgress.GetValue(), commonInfo.w - 1);
94     }
95     {
96         specInfo.hasEp = true;
97         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
98         boxProgress.SetValue(validValue);
99         EXPECT_EQ(boxProgress.GetValue(), static_cast<int>(validValue / MAX_PROGRESS_VALUE * (commonInfo.w - 1)));
100     }
101 }
102 
103 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_set_visible_without_ep, TestSize.Level0)
104 {
105     {
106         UxViewInfo info {{10, 10, 1000, 1000, "id", "UIBoxProgress", false},
107         UxBoxProgressInfo {50, "#ffffffff", "#000000ff", "", false}};
108         BoxProgressAdapter boxProgress {info};
109         boxProgress.SetVisible(true);
110         EXPECT_TRUE(boxProgress.IsVisible());
111     }
112     {
113         UxViewInfo info {{10, 10, 1000, 1000, "id", "UIBoxProgress", false},
114         UxBoxProgressInfo {50, "#ffffffff", "#000000ff", "", true}};
115         BoxProgressAdapter boxProgress {info};
116         boxProgress.SetVisible(true);
117         EXPECT_TRUE(boxProgress.IsVisible());
118     }
119 }
120 
121 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_init_endpoint, TestSize.Level0)
122 {
123     UxBoxProgressInfo specInfo {50, "#ffffffff", "#000000ff", "", false};
124     UxViewCommonInfo commonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false};
125     {
126         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
127         EXPECT_TRUE(boxProgress.InitEp());
128     }
129     {
130         specInfo.hasEp = true;
131         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
132         EXPECT_FALSE(boxProgress.InitEp());
133     }
134     {
135         OHOS::UIViewGroup parent {};
136         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
137         parent.Add(&boxProgress);
138         EXPECT_FALSE(boxProgress.InitEp());
139     }
140     {
141         constexpr auto epId = "endpoint";
142         specInfo.endPoint = epId;
143         OHOS::UIViewGroup parent {};
144         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
145         LabelBtnAdapter labelBtn {};
146         labelBtn.SetViewId(epId);
147         parent.Add(&boxProgress);
148         parent.Add(&labelBtn);
149         EXPECT_FALSE(boxProgress.InitEp());
150     }
151     {
152         constexpr auto epId = "endpoint";
153         specInfo.endPoint = epId;
154         OHOS::UIViewGroup parent {};
155         BoxProgressAdapter boxProgress {UxViewInfo {commonInfo, specInfo}};
156         ImgViewAdapter imgView {};
157         imgView.SetViewId(epId);
158         parent.Add(&boxProgress);
159         parent.Add(&imgView);
160         EXPECT_TRUE(boxProgress.InitEp());
161     }
162 }
163 
164 HWTEST_F(UpdaterUiComponentUnitTest, test_box_progress_with_ep, TestSize.Level0)
165 {
166     constexpr auto epId = "endpoint";
167     OHOS::UIViewGroup parent {};
168     ImgViewAdapter epView {};
169     BoxProgressAdapter boxProgress {UxViewInfo {UxViewCommonInfo {10, 10, 1000, 1000, "id", "UIBoxProgress", false},
170         UxBoxProgressInfo {50, "#ffffffff", "#000000ff", epId, true}}};
171     epView.SetViewId(epId);
172     parent.Add(&boxProgress);
173     parent.Add(&epView);
174     EXPECT_TRUE(boxProgress.InitEp());
175 
176     boxProgress.SetVisible(true);
177     EXPECT_TRUE(boxProgress.IsVisible());
178     EXPECT_TRUE(epView.IsVisible());
179 
180     boxProgress.SetVisible(false);
181     EXPECT_FALSE(boxProgress.IsVisible());
182     EXPECT_FALSE(epView.IsVisible());
183 
184     constexpr float testValue = 50;
185     constexpr float halfDivisor = 2.0;
186     boxProgress.SetValue(testValue);
187     float rate = static_cast<float>(boxProgress.GetValue()) / boxProgress.GetRangeMax();
188     EXPECT_EQ(epView.GetX(), static_cast<int16_t>(boxProgress.GetX() -
189         epView.GetWidth() / halfDivisor + boxProgress.GetWidth() * rate));
190     EXPECT_EQ(epView.GetY(), static_cast<int16_t>(boxProgress.GetY() -
191         epView.GetHeight() / halfDivisor + boxProgress.GetHeight() / halfDivisor));
192 }
193 
194 HWTEST_F(UpdaterUiComponentUnitTest, test_img_view_adapter_constructor, TestSize.Level0)
195 {
196     uint32_t interval = 0;
197     {
198         constexpr auto id = "img";
199         interval = 0;
200         UxImageInfo specInfo {"", "", 100, interval};
201         UxViewCommonInfo commonInfo {10, 10, 1000, 1000, id, "UIImageView", false};
202         ImgViewAdapter imgView {UxViewInfo {commonInfo, specInfo}};
203         CheckCommInfo(imgView, commonInfo);
204 
205         EXPECT_EQ(imgView.GetX(), commonInfo.x);
206         EXPECT_EQ(imgView.GetY(), commonInfo.y);
207         EXPECT_EQ(imgView.GetWidth(), commonInfo.w);
208         EXPECT_EQ(imgView.GetHeight(), commonInfo.h);
209         EXPECT_EQ(imgView.IsVisible(), false);
210         EXPECT_STREQ(imgView.GetViewId(), id);
211     }
212     {
213         interval = 10; // assign a non-zero interval
214         constexpr auto id = "img";
215         UxImageInfo specInfo {"", "", 100, interval};
216         UxViewCommonInfo commonInfo {10, 10, 1000, 1000, id, "UIImageView", false};
217         ImgViewAdapter imgView {UxViewInfo {commonInfo, specInfo}};
218         CheckCommInfo(imgView, commonInfo);
219 
220         ASSERT_NE(imgView.GetAnimatorCallback(), nullptr);
221         ASSERT_NE(imgView.GetAnimator(), nullptr);
222     }
223 }
224 
225 HWTEST_F(UpdaterUiComponentUnitTest, test_img_view_adapter_is_valid, TestSize.Level0)
226 {
227     constexpr auto maxImgCnt = 300U;
228     constexpr auto maxInterval = 5000U;
229 
230     // for static img
231     EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"", "", 100, 0}));
232     EXPECT_TRUE(ImgViewAdapter::IsValid(UxImageInfo {"resPath", "", 100, 0}));
233 
234     // for animator
235     EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"", "", 100, 10}));
236     EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "", 100, 10}));
237     EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", maxImgCnt + 1, 10}));
238     EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", 0, 10}));
239     EXPECT_FALSE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", 100, maxInterval + 1}));
240     EXPECT_TRUE(ImgViewAdapter::IsValid(UxImageInfo {"dir/", "filePrefix", 100, 10}));
241 }
242 
243 HWTEST_F(UpdaterUiComponentUnitTest, test_img_view_adapter_start_stop, TestSize.Level0)
244 {
245     {
246         ImgViewAdapter imgView {};
247         EXPECT_FALSE(imgView.Start());
248         EXPECT_FALSE(imgView.Stop());
249     }
250     {
251         // non animator
252         ImgViewAdapter imgView {UxViewInfo {UxViewCommonInfo {10, 10, 1000, 1000, "id", "UIImageView", false},
253             UxImageInfo {"respath", "", 100, 0}}};
254         EXPECT_FALSE(imgView.Start());
255         EXPECT_FALSE(imgView.Stop());
256     }
257     {
258         using namespace std::literals::chrono_literals;
259         ImgViewAdapter imgView {UxViewInfo {UxViewCommonInfo {10, 10, 1000, 1000, "id", "UIImageView", false},
260             UxImageInfo {"", "fileprefix", 100, 10}}};
261 
262         uint32_t currId = 0;
263         EXPECT_FALSE(imgView.Stop()); // stop would fail when hasn't been started
264         EXPECT_TRUE(imgView.Start()); // start success
265         EXPECT_FALSE(imgView.Start()); // shouldn't start when it has been started
266         EXPECT_TRUE(imgView.IsVisible());
267 
268         OHOS::TaskManager::GetInstance()->SetTaskRun(true); // set task can run
269         OHOS::AnimatorManager::GetInstance()->Init(); // add animator task
270         OHOS::TaskManager::GetInstance()->TaskHandler(); // run one loop
271         EXPECT_EQ(imgView.GetCurrId(), ++currId);
272         std::this_thread::sleep_for(0.1s);
273         OHOS::TaskManager::GetInstance()->TaskHandler();
274         EXPECT_EQ(imgView.GetCurrId(), ++currId);
275 
276         EXPECT_TRUE(imgView.Stop()); // stop success
277         EXPECT_FALSE(imgView.Stop()); // stop success
278         EXPECT_FALSE(imgView.IsVisible());
279 
280         OHOS::TaskManager::GetInstance()->TaskHandler();
281         EXPECT_EQ(imgView.GetCurrId(), currId);
282         OHOS::TaskManager::GetInstance()->TaskHandler();
283         EXPECT_EQ(imgView.GetCurrId(), currId);
284         OHOS::TaskManager::GetInstance()->SetTaskRun(false);
285         OHOS::TaskManager::GetInstance()->Remove(OHOS::AnimatorManager::GetInstance());
286     }
287 }
288 
289 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_constructor, TestSize.Level0)
290 {
291     constexpr auto labelText = "hello";
292     UxLabelBtnInfo specInfo {100, "hello", "#ffffffff", "#000000ff", "#000000ff", "#ffffffff", true};
293     UxViewCommonInfo commonInfo {0, 0, 0, 0, "id", "UILabelButton", false};
294     UxViewInfo info {commonInfo, specInfo};
295     LabelBtnAdapter labelBtn {info};
296     CheckCommInfo(labelBtn, commonInfo);
297 
298     auto fontColor = StrToColor(specInfo.txtColor);
299     auto bgColor = StrToColor(specInfo.bgColor);
300     EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_TEXT_COLOR), fontColor.full);
301     EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_TEXT_OPA), fontColor.alpha);
302     EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), bgColor.full);
303     EXPECT_EQ(labelBtn.GetStyle(OHOS::STYLE_BACKGROUND_OPA), bgColor.alpha);
304     EXPECT_EQ(labelBtn.IsFocusable(), specInfo.focusable);
305     EXPECT_STREQ(labelBtn.GetText(), labelText);
306 }
307 
308 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_on_press, TestSize.Level0)
309 {
310     LabelBtnAdapter labelBtn1 {UxViewInfo {{0, 0, 50, 50, "id", "UILabel", true},
311         UxLabelBtnInfo {100, "", "#000000ff", "#ffffffff", "#ffffffff", "#000000ff", true}}};
312     LabelBtnAdapter labelBtn2 {UxViewInfo {{100, 100, 50, 50, "id", "UILabel", true},
313         UxLabelBtnInfo {100, "", "#000000ff", "#ffffffff", "#ffffffff", "#000000ff", true}}};
314     OHOS::FocusManager::GetInstance()->RequestFocus(&labelBtn2);
315     labelBtn1.OnPressEvent(OHOS::PressEvent {OHOS::Point {}});
316     EXPECT_EQ(OHOS::FocusManager::GetInstance()->GetFocusedView(), &labelBtn1);
317     EXPECT_EQ(labelBtn1.GetLabelStyle(OHOS::STYLE_TEXT_COLOR), (OHOS::ColorType {{0xff, 0xff, 0xff, 0xff}}.full));
318     EXPECT_EQ(labelBtn2.GetLabelStyle(OHOS::STYLE_TEXT_COLOR), (OHOS::ColorType {{0, 0, 0, 0xff}}.full));
319     EXPECT_EQ(labelBtn1.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), (OHOS::ColorType {{0, 0, 0, 0xff}}.full));
320     EXPECT_EQ(labelBtn2.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), (OHOS::ColorType {{0xff, 0xff, 0xff, 0xff}}.full));
321     OHOS::FocusManager::GetInstance()->ClearFocus();
322 }
323 
324 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_is_valid, TestSize.Level0)
325 {
326     EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {201, "", "", "", "", "", false}));
327     EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff", "", "", "", false}));
328     EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff", "#000000ff", "", "", false}));
329     EXPECT_FALSE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff",
330         "#000000ff", "#000000ff", "", false}));
331     EXPECT_TRUE(LabelBtnAdapter::IsValid(UxLabelBtnInfo {100, "", "#000000ff",
332         "#000000ff", "#000000ff", "#000000ff", false}));
333 }
334 
335 HWTEST_F(UpdaterUiComponentUnitTest, test_label_btn_adapter_set_text, TestSize.Level0)
336 {
337     LabelBtnAdapter labelBtn {UxViewInfo {{0, 0, 0, 0, "id", "UILabel", false},
338         UxLabelBtnInfo {100, "", "#000000ff", "#000000ff", "#000000ff", "#000000ff", false}}};
339     constexpr auto testString = "test text";
340     labelBtn.SetText(testString);
341     EXPECT_STREQ(labelBtn.GetText(), testString);
342 
343     labelBtn.SetText("*");
344     EXPECT_STREQ(labelBtn.GetText(), testString);
345 
346     labelBtn.SetText("");
347     EXPECT_STREQ(labelBtn.GetText(), "");
348 }
349 
350 HWTEST_F(UpdaterUiComponentUnitTest, test_text_label_adapter_is_info_valid, TestSize.Level0)
351 {
352     EXPECT_FALSE(TextLabelAdapter::IsValid(UxLabelInfo {255, "", "", "", ""}));
353     EXPECT_FALSE(TextLabelAdapter::IsValid(UxLabelInfo {255, "", "", "#000000ff", ""}));
354     EXPECT_TRUE(TextLabelAdapter::IsValid(UxLabelInfo {100, "", "", "#000000ff", "#000000ff"}));
355 }
356 
357 HWTEST_F(UpdaterUiComponentUnitTest, test_text_label_adapter_constructor, TestSize.Level0)
358 {
359     constexpr auto labelText = "hello";
360     UxLabelInfo specInfo {100, "hello", "center", "#000000ff", "#000000ff"};
361     UxViewCommonInfo commonInfo {0, 0, 0, 0, "id", "UILabel", false};
362     UxViewInfo info {commonInfo, specInfo};
363     TextLabelAdapter textLabel {info};
364     CheckCommInfo(textLabel, commonInfo);
365 
366     auto fontColor = StrToColor(specInfo.fontColor);
367     auto bgColor = StrToColor(specInfo.bgColor);
368     EXPECT_EQ(textLabel.GetHorAlign(), GetAlign(specInfo.align));
369     EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_TEXT_COLOR), fontColor.full);
370     EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_TEXT_OPA), fontColor.alpha);
371     EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_BACKGROUND_COLOR), bgColor.full);
372     EXPECT_EQ(textLabel.GetStyle(OHOS::STYLE_BACKGROUND_OPA), bgColor.alpha);
373     EXPECT_STREQ(textLabel.GetText(), labelText);
374 }
375 
376 HWTEST_F(UpdaterUiComponentUnitTest, test_text_label_adapter_set_text, TestSize.Level0)
377 {
378     TextLabelAdapter textLabel {UxViewInfo {{0, 0, 0, 0, "id", "UILabel", false},
379         UxLabelInfo {255, "", "", "#000000ff", "#000000ff"}}};
380     constexpr auto testString = "test text";
381     textLabel.SetText(testString);
382     EXPECT_STREQ(textLabel.GetText(), testString);
383 
384     textLabel.SetText("*");
385     EXPECT_STREQ(textLabel.GetText(), testString);
386 
387     textLabel.SetText("");
388     EXPECT_STREQ(textLabel.GetText(), "");
389 }
390 }