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