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 "ui_test_view_zindex.h"
17 
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20 
21 namespace OHOS {
22 namespace {
23 constexpr int16_t BUTTON_OFFSET = 5;
24 constexpr int16_t BUTTON_GROUP_WIDTH = 150;
25 constexpr int16_t BUTTON_GROUP_HEIGHT = 600;
26 constexpr uint8_t ZINDEX_VIEW_NUM = 6;
27 constexpr uint8_t ZINDEX_BTN_NUM = 8;
28 constexpr uint8_t ZINDEX_MODIFY_NUM = 7;
29 constexpr uint8_t CHANGE_ZINDEX_VIEW_Y = 10;
30 constexpr uint8_t CHANGE_ZINDEX_VIEW_X = 100;
31 struct ZIndexView {
32     const char* text;
33     int16_t zIndex;
34     ColorType color;
35 };
36 struct ZIndexBtn {
37     const char* text;
38     int16_t zIndex;
39 };
40 const ZIndexBtn ZINDEX_BTN_GROUP[ZINDEX_BTN_NUM] = {{"zIndex->-1", -1}, {"zIndex->0", 0}, {"zIndex->1", 1},
41                                                     {"zIndex->2", 2},   {"zIndex->3", 3}, {"zIndex->4", 4},
42                                                     {"zIndex->5", 5},   {"zIndex->7", 7}};
43 const ZIndexView ZINDEX_VIEW_GROUP[ZINDEX_VIEW_NUM] = {
44     {"zIndex=6", 6, Color::Blue()},      {"zIndex=4", 4, Color::Blue()},      {"zIndex=2", 2, Color::Blue()},
45     {"A.zIndex=0", 0, Color::Magenta()}, {"B.zIndex=0", 0, Color::Magenta()}, {"C.zIndex=0", 0, Color::Magenta()}};
46 const char* ZINDEX_BTN_VIEWID_GROUP[ZINDEX_BTN_NUM] = {"zIndex=-1", "zIndex=0", "zIndex=1", "zIndex=2",
47                                                        "zIndex=3", "zIndex=4", "zIndex=5", "zIndex=7"};
48 const char* MODIFY_INSERT_HEAD = "InsertHead";
49 const char* MODIFY_INSERT_TAIL = "InsertTail";
50 const char* MODIFY_AFTER_ZINDEX_4 = "AfterZIndex=4";
51 const char* MODIFY_AFTER_B_ZINDEX_0 = "AfterB.ZIndex=0";
52 const char* MODIFY_REMOVE = "Remove";
53 const char* MODIFY_RESET = "Reset";
54 const char* MODIFY_ANIMATE_IN = "AnimateIn";
55 const char* ZINDEX_MODIFY_GROUP[ZINDEX_MODIFY_NUM] = {
56     MODIFY_INSERT_HEAD, MODIFY_INSERT_TAIL, MODIFY_AFTER_ZINDEX_4, MODIFY_AFTER_B_ZINDEX_0,
57     MODIFY_REMOVE,      MODIFY_RESET,       MODIFY_ANIMATE_IN};
58 const char* ID_CHANGE_MY_ZINDEX = "Change zIndex";
59 const char* ID_CLICK_HINT = "clickHint";
60 const char* ID_ZINDEX_VIEW_VG = "zIndex_view_group1";
61 const char* ID_ZINDEX_BUTTON_VG = "zIndex_button_group";
62 const char* ID_CHILD_CHANGE_VG = "child_change_btn_group";
63 } // namespace
~UITestViewZIndex()64 UITestViewZIndex::~UITestViewZIndex()
65 {
66     // todo
67 }
SetUp()68 void UITestViewZIndex::SetUp()
69 {
70     if (container_ == nullptr) {
71         container_ = new UIScrollView();
72         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
73         container_->SetHorizontalScrollState(false);
74         container_->SetThrowDrag(true);
75     }
76 }
77 
TearDown()78 void UITestViewZIndex::TearDown()
79 {
80     DeleteChildren(container_);
81     container_ = nullptr;
82     if (animator_ != nullptr) {
83         delete animator_;
84         animator_ = nullptr;
85     }
86 }
87 
GetTestView()88 const UIView* UITestViewZIndex::GetTestView()
89 {
90     UIKitViewZIndex001();
91     return container_;
92 }
93 
CreateTitleLabel() const94 UILabel* UITestViewZIndex::CreateTitleLabel() const
95 {
96     UILabel* label = new UILabel();
97     label->SetHeight(TITLE_LABEL_DEFAULT_HEIGHT);
98     label->SetLineBreakMode(UILabel::LINE_BREAK_STRETCH);
99     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
100     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
101     return label;
102 }
103 
CreateZIndexLabel(const char * text,int16_t zIndex,ColorType color)104 UILabel* UITestViewZIndex::CreateZIndexLabel(const char* text, int16_t zIndex, ColorType color)
105 {
106     UILabel* label = new UILabel();
107     label->Resize(300, 70); // 300: width 70:height
108     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
109     label->SetZIndex(zIndex);
110     label->SetText(text);
111     label->SetViewId(text);
112     label->SetOnClickListener(this);
113     label->SetTouchable(true);
114     label->SetStyle(STYLE_BACKGROUND_COLOR, color.full);
115     label->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
116     label->SetStyle(STYLE_BORDER_COLOR, Color::Red().full);
117     label->SetStyle(STYLE_BORDER_OPA, OPA_OPAQUE);
118     label->SetStyle(STYLE_BORDER_WIDTH, 2); // 2: border width
119     label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
120     return label;
121 }
122 
CreateZIndexBtn(const char * text,const char * viewId)123 UILabelButton* UITestViewZIndex::CreateZIndexBtn(const char* text, const char* viewId)
124 {
125     UILabelButton* btn = new UILabelButton();
126     btn->Resize(120, 40);                           // 120: width 40:height
127     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 15); // 15: font size
128     btn->SetText(text);
129     btn->SetViewId(viewId);
130     btn->SetOnClickListener(this);
131     return btn;
132 }
133 
OnClick(UIView & view,const ClickEvent & event)134 bool UITestViewZIndex::OnClick(UIView& view, const ClickEvent& event)
135 {
136     if (container_ == nullptr) {
137         return true;
138     }
139 
140     ClickZIndexBtn(view);
141     ClickModifyBtn(view);
142     UpdateClickHint(view);
143 
144     container_->Invalidate();
145     return true;
146 }
147 
ClickZIndexBtn(UIView & view)148 void UITestViewZIndex::ClickZIndexBtn(UIView& view)
149 {
150     UIView* zIndexView = container_->GetChildById(ID_CHANGE_MY_ZINDEX);
151     if (zIndexView == nullptr) {
152         return;
153     }
154     const char* id = view.GetViewId();
155     for (uint8_t i = 0; i < ZINDEX_BTN_NUM; i++) {
156         if (strcmp(id, ZINDEX_BTN_VIEWID_GROUP[i]) == 0) {
157             zIndexView->SetZIndex(ZINDEX_BTN_GROUP[i].zIndex);
158         }
159     }
160 }
161 
ClickModifyBtn(UIView & view)162 void UITestViewZIndex::ClickModifyBtn(UIView& view)
163 {
164     UIView* zIndexView = container_->GetChildById(ID_CHANGE_MY_ZINDEX);
165     if (zIndexView == nullptr) {
166         SetupChangeView();
167         zIndexView = container_->GetChildById(ID_CHANGE_MY_ZINDEX);
168     }
169     UIView* zIndexViewGroupTmp = container_->GetChildById(ID_ZINDEX_VIEW_VG);
170     if (zIndexViewGroupTmp == nullptr) {
171         return;
172     }
173 
174     UIViewGroup* zIndexViewGroup = reinterpret_cast<UIViewGroup*>(zIndexViewGroupTmp);
175     const char* id = view.GetViewId();
176     if (strcmp(id, MODIFY_INSERT_HEAD) == 0) {
177         zIndexViewGroup->Remove(zIndexView);
178         zIndexViewGroup->Insert(zIndexViewGroup->GetChildrenHead(), zIndexView);
179     } else if (strcmp(id, MODIFY_INSERT_TAIL) == 0) {
180         zIndexViewGroup->Remove(zIndexView);
181         zIndexViewGroup->Add(zIndexView);
182     } else if (strcmp(id, MODIFY_AFTER_ZINDEX_4) == 0) {
183         zIndexViewGroup->Remove(zIndexView);
184         zIndexViewGroup->Insert(zIndexViewGroup->GetChildrenHead()->GetNextSibling(), zIndexView);
185     } else if (strcmp(id, MODIFY_AFTER_B_ZINDEX_0) == 0) {
186         zIndexViewGroup->Remove(zIndexView);
187         UIView* view5 = container_->GetChildById(ZINDEX_VIEW_GROUP[4].text); // 4 : the fifth view
188         if (view5 == nullptr) {
189             return;
190         }
191         zIndexViewGroup->Insert(view5, zIndexView);
192     } else if (strcmp(id, MODIFY_REMOVE) == 0) {
193         zIndexViewGroup->Remove(zIndexView);
194     } else if (strcmp(id, MODIFY_RESET) == 0) {
195         zIndexViewGroup->Remove(zIndexView);
196         SetupChangeView();
197     } else if (strcmp(id, MODIFY_ANIMATE_IN) == 0) {
198         if (animator_ != nullptr) {
199             animator_->Stop();
200             delete animator_;
201         }
202 
203         zIndexView->SetY(container_->GetHeight());
204         animator_ = new Animator(this, zIndexView, 15, true); // 15: time millsec
205         animator_->Start();
206     }
207 }
208 
Callback(UIView * view)209 void UITestViewZIndex::Callback(UIView* view)
210 {
211     if ((view == nullptr) || (container_ == nullptr)) {
212         return;
213     }
214     int16_t y = view->GetY();
215     int16_t step = 2;
216     if (y <= CHANGE_ZINDEX_VIEW_Y) {
217         animator_->Stop();
218         return;
219     }
220     if (y - step < CHANGE_ZINDEX_VIEW_Y) {
221         y = CHANGE_ZINDEX_VIEW_Y;
222     } else {
223         y -= step;
224     }
225     view->Invalidate();
226     view->SetY(y);
227     view->Invalidate();
228 }
229 
UpdateClickHint(UIView & view)230 void UITestViewZIndex::UpdateClickHint(UIView& view)
231 {
232     UIView* tmpView = container_->GetChildById(ID_CLICK_HINT);
233     if (tmpView == nullptr) {
234         return;
235     }
236     UILabel* clickHint = reinterpret_cast<UILabel*>(tmpView);
237     clickHint->SetText(view.GetViewId());
238 }
239 
UIKitViewZIndex001()240 void UITestViewZIndex::UIKitViewZIndex001()
241 {
242     if (container_ == nullptr) {
243         return;
244     }
245     SetupZIndexView();
246     SetupZIndexBtn();
247     SetupChildModifyBtn();
248 }
249 
SetupZIndexView()250 void UITestViewZIndex::SetupZIndexView()
251 {
252     UIViewGroup* group = new UIViewGroup();
253     container_->Add(group);
254     group->SetOnClickListener(this);
255     group->SetTouchable(true);
256     group->SetViewId(ID_ZINDEX_VIEW_VG);
257     group->SetPosition(48, 5, 500, 500); // 48:position x 5: offset 500: width/height
258 
259     int16_t alignBottom = -55;
260     int16_t alignLeft = 30;
261     for (uint8_t i = 0; i < ZINDEX_VIEW_NUM; i++) {
262         UILabel* zIndexView =
263             CreateZIndexLabel(ZINDEX_VIEW_GROUP[i].text, ZINDEX_VIEW_GROUP[i].zIndex, ZINDEX_VIEW_GROUP[i].color);
264         group->Add(zIndexView);
265         if (i == 0) {
266             zIndexView->SetPosition(0, 0);
267         } else {
268             if (i == 3) { // 3: for zindex 0 group
269                 zIndexView->AlignBottomToSibling(ZINDEX_VIEW_GROUP[i - 1].text, alignBottom - 80); // 80: offset
270                 zIndexView->AlignLeftToSibling(ZINDEX_VIEW_GROUP[0].text, alignLeft);
271             } else {
272                 zIndexView->AlignBottomToSibling(ZINDEX_VIEW_GROUP[i - 1].text, alignBottom);
273                 zIndexView->AlignLeftToSibling(ZINDEX_VIEW_GROUP[i - 1].text, alignLeft);
274             }
275         }
276     }
277 
278     SetupChangeView();
279 }
280 
SetupChangeView()281 void UITestViewZIndex::SetupChangeView()
282 {
283     UIView* zIndexViewGroupTmp = container_->GetChildById(ID_ZINDEX_VIEW_VG);
284     if (zIndexViewGroupTmp == nullptr) {
285         return;
286     }
287     UIViewGroup* group = reinterpret_cast<UIViewGroup*>(zIndexViewGroupTmp);
288     UILabel* target = CreateZIndexLabel(ID_CHANGE_MY_ZINDEX, 0, Color::Cyan());
289     target->SetTextColor(Color::Black());
290     UIView* tmpView = group->GetChildById(ZINDEX_VIEW_GROUP[2].text);
291     if (tmpView == nullptr) {
292         delete target;
293         return;
294     }
295     group->Insert(tmpView, target);
296     target->SetPosition(CHANGE_ZINDEX_VIEW_X, CHANGE_ZINDEX_VIEW_Y);
297     target->Resize(200, 400); // 200:width 400:height
298 }
299 
SetupZIndexBtn()300 void UITestViewZIndex::SetupZIndexBtn()
301 {
302     UIViewGroup* group = new UIViewGroup();
303     container_->Add(group);
304     group->SetOnClickListener(this);
305     group->SetViewId(ID_ZINDEX_BUTTON_VG);
306     group->Resize(BUTTON_GROUP_WIDTH, BUTTON_GROUP_HEIGHT);
307     group->LayoutRightToSibling(ID_ZINDEX_VIEW_VG);
308 
309     UILabel* clickHint = new UILabel();
310     clickHint->Resize(150, 30);    // 150: width 30: height
311     clickHint->SetPosition(0, 10); // 10: offset y
312     clickHint->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
313     clickHint->SetViewId(ID_CLICK_HINT);
314     clickHint->SetAlign(TEXT_ALIGNMENT_LEFT, TEXT_ALIGNMENT_CENTER);
315     group->Add(clickHint);
316 
317     for (uint8_t i = 0; i < ZINDEX_BTN_NUM; i++) {
318         UILabelButton* zIndexBtn = CreateZIndexBtn(ZINDEX_BTN_GROUP[i].text, ZINDEX_BTN_VIEWID_GROUP[i]);
319         group->Add(zIndexBtn);
320         if (i == 0) {
321             zIndexBtn->SetPosition(0, 50); // 50: offset y
322         } else {
323             zIndexBtn->LayoutBottomToSibling(ZINDEX_BTN_VIEWID_GROUP[i - 1], BUTTON_OFFSET);
324             zIndexBtn->AlignLeftToSibling(ZINDEX_BTN_VIEWID_GROUP[i - 1]);
325         }
326     }
327 }
328 
SetupChildModifyBtn()329 void UITestViewZIndex::SetupChildModifyBtn()
330 {
331     UIViewGroup* group = new UIViewGroup();
332     container_->Add(group);
333     group->SetOnClickListener(this);
334     group->SetViewId(ID_CHILD_CHANGE_VG);
335     group->Resize(BUTTON_GROUP_WIDTH, BUTTON_GROUP_HEIGHT);
336     group->LayoutRightToSibling(ID_ZINDEX_BUTTON_VG);
337 
338     for (uint8_t i = 0; i < ZINDEX_MODIFY_NUM; i++) {
339         UILabelButton* zIndexModifyBtn = CreateZIndexBtn(ZINDEX_MODIFY_GROUP[i], ZINDEX_MODIFY_GROUP[i]);
340         zIndexModifyBtn->SetWidth(150); // 150: width
341         group->Add(zIndexModifyBtn);
342         if (i == 0) {
343             zIndexModifyBtn->SetPosition(0, 50); // 50: position y
344         } else {
345             zIndexModifyBtn->LayoutBottomToSibling(ZINDEX_MODIFY_GROUP[i - 1], BUTTON_OFFSET);
346             zIndexModifyBtn->AlignLeftToSibling(ZINDEX_MODIFY_GROUP[i - 1]);
347         }
348     }
349 }
350 } // namespace OHOS
351