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