1 /*
2  * Copyright (c) 2020-2021 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 "components/ui_view_group.h"
17 
18 #include <climits>
19 #include <gtest/gtest.h>
20 
21 #include "components/ui_label.h"
22 
23 using namespace testing::ext;
24 namespace OHOS {
25 class UIViewGroupTest : public testing::Test {
26 public:
UIViewGroupTest()27     UIViewGroupTest() : viewGroup_(nullptr) {}
~UIViewGroupTest()28     ~UIViewGroupTest() {}
SetUpTestCase(void)29     static void SetUpTestCase(void) {}
TearDownTestCase(void)30     static void TearDownTestCase(void) {}
31     void SetUp(void);
32     void TearDown(void);
33     UIViewGroup* viewGroup_;
34 };
35 
SetUp(void)36 void UIViewGroupTest::SetUp(void)
37 {
38     if (viewGroup_ == nullptr) {
39         viewGroup_ = new UIViewGroup();
40     }
41 }
42 
TearDown(void)43 void UIViewGroupTest::TearDown(void)
44 {
45     if (viewGroup_ != nullptr) {
46         delete viewGroup_;
47         viewGroup_ = nullptr;
48     }
49 }
50 
51 /**
52  * @tc.name: UIViewGroupAdd_001
53  * @tc.desc: Verify Add function.
54  * @tc.type: FUNC
55  * @tc.require: AR000EEMQF
56  */
57 HWTEST_F(UIViewGroupTest, UIViewGroupAdd_001, TestSize.Level0)
58 {
59     if (viewGroup_ == nullptr) {
60         EXPECT_EQ(1, 0);
61         return;
62     }
63     UIView* view = new UIView();
64     if (view == nullptr) {
65         EXPECT_EQ(1, 0);
66         return;
67     }
68     viewGroup_->Add(view);
69     EXPECT_EQ(view, viewGroup_->GetChildrenHead());
70     EXPECT_EQ(view->GetParent(), viewGroup_);
71 
72     delete view;
73 }
74 
75 /**
76  * @tc.name: UIViewGroupInsert_001
77  * @tc.desc: Verify Insert function.
78  * @tc.type: FUNC
79  * @tc.require: AR000EEMQF
80  */
81 HWTEST_F(UIViewGroupTest, UIViewGroupInsert_001, TestSize.Level0)
82 {
83     if (viewGroup_ == nullptr) {
84         EXPECT_EQ(1, 0);
85         return;
86     }
87     UIView* preView = new UIView();
88     if (preView == nullptr) {
89         EXPECT_EQ(1, 0);
90         return;
91     }
92     UIView* view = new UIView();
93 
94     viewGroup_->Add(preView);
95     viewGroup_->Insert(preView, view);
96     EXPECT_EQ(view, preView->GetNextSibling());
97 
98     delete preView;
99     delete view;
100 }
101 
102 /**
103  * @tc.name: UIViewGroupRemove_001
104  * @tc.desc: Verify Remove function.
105  * @tc.type: FUNC
106  * @tc.require: AR000EEMQF
107  */
108 HWTEST_F(UIViewGroupTest, UIViewGroupRemove_001, TestSize.Level0)
109 {
110     if (viewGroup_ == nullptr) {
111         EXPECT_EQ(1, 0);
112         return;
113     }
114     UIView* view = new UIView();
115     if (view == nullptr) {
116         EXPECT_EQ(1, 0);
117         return;
118     }
119     viewGroup_->Add(view);
120     viewGroup_->Remove(view);
121     EXPECT_EQ(nullptr, viewGroup_->GetChildrenHead());
122     EXPECT_EQ(nullptr, view->GetParent());
123 
124     delete view;
125 }
126 
127 /**
128  * @tc.name: UIViewGroupRemoveAll_001
129  * @tc.desc: Verify RemoveAll function.
130  * @tc.type: FUNC
131  * @tc.require: AR000EEMQF
132  */
133 HWTEST_F(UIViewGroupTest, UIViewGroupRemoveAll_001, TestSize.Level0)
134 {
135     if (viewGroup_ == nullptr) {
136         EXPECT_EQ(1, 0);
137         return;
138     }
139     UIView* view = new UIView();
140     UIView* view2 = new UIView();
141 
142     viewGroup_->Add(view);
143     viewGroup_->RemoveAll();
144     EXPECT_EQ(nullptr, viewGroup_->GetChildrenHead());
145 
146     delete view;
147     delete view2;
148 }
149 
150 /**
151  * @tc.name: UIViewGroupGetViewType_001
152  * @tc.desc: Verify GetViewType function.
153  * @tc.type: FUNC
154  * @tc.require: AR000EEMQF
155  */
156 HWTEST_F(UIViewGroupTest, UIViewGroupGetViewType_001, TestSize.Level1)
157 {
158     if (viewGroup_ == nullptr) {
159         EXPECT_EQ(1, 0);
160         return;
161     }
162     EXPECT_EQ(viewGroup_->GetViewType(), UI_VIEW_GROUP);
163 }
164 
165 /**
166  * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_001
167  * @tc.desc: Check the child render head
168  * @tc.type: FUNC
169  * @tc.require:issueI5AD8G
170  */
171 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_001, TestSize.Level0)
172 {
173     UIViewGroup* viewGroup = new UIViewGroup();
174     UIView* sibling = viewGroup->GetChildrenRenderHead();
175     EXPECT_EQ(sibling, nullptr);
176     delete viewGroup;
177 }
178 
179 /**
180  * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_001
181  * @tc.desc: Check child render head
182  * @tc.type: FUNC
183  * @tc.require:issueI5AD8G
184  */
185 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_002, TestSize.Level0)
186 {
187     UIView* view1 = new UIView();
188     UIView* view2 = new UIView();
189     UIView* view3 = new UIView();
190     UIViewGroup* viewGroup = new UIViewGroup();
191     viewGroup->Add(view1);
192     // check child head
193     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
194 
195     viewGroup->Add(view2);
196     // check child head with two child
197     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
198 
199     viewGroup->Remove(view1);
200     // check child head when remove head
201     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
202 
203     viewGroup->Remove(view2);
204     // check child head when remove all
205     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
206 
207     viewGroup->Add(view1);
208     viewGroup->Add(view2);
209     viewGroup->RemoveAll();
210     // check child head when remove all
211     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
212 
213     viewGroup->Add(view1);
214     viewGroup->Add(view2);
215     viewGroup->Insert(view1, view3);
216     viewGroup->Remove(view1);
217     // check child head when insert view
218     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
219 
220     delete view1;
221     delete view2;
222     delete view3;
223     delete viewGroup;
224 }
225 
226 /**
227  * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_003
228  * @tc.desc: Check child render head when zIndex change
229  * @tc.type: FUNC
230  * @tc.require:issueI5AD8G
231  */
232 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_003, TestSize.Level0)
233 {
234     UIView* view1 = new UIView();
235     UIView* view2 = new UIView();
236     UIView* view3 = new UIView();
237     UIView* view4 = new UIView();
238     UIViewGroup* viewGroup = new UIViewGroup();
239     viewGroup->Add(view1);
240     viewGroup->Add(view2);
241     viewGroup->Add(view3);
242     viewGroup->Add(view4);
243     // check child with zIndex = 0
244     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
245 
246     view1->SetZIndex(10); // 10: zindex
247     // check child when zIndex change
248     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
249 
250     view3->SetZIndex(-10); // -10: zindex
251     // check child when zIndex change
252     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
253 
254     delete view1;
255     delete view2;
256     delete view3;
257     delete view4;
258     delete viewGroup;
259 }
260 
261 /**
262  * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_004
263  * @tc.desc: Check child render head when zIndex change
264  * @tc.type: FUNC
265  * @tc.require:issueI5AD8G
266  */
267 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_004, TestSize.Level0)
268 {
269     UIView* view1 = new UIView();
270     UIView* view2 = new UIView();
271     UIView* view3 = new UIView();
272     UIView* view4 = new UIView();
273     UIViewGroup* viewGroup = new UIViewGroup();
274     viewGroup->Add(view1);
275     viewGroup->Add(view2);
276     viewGroup->Add(view3);
277     viewGroup->Add(view4);
278     // check child with zIndex = 0
279     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
280     EXPECT_EQ(view1->GetNextRenderSibling(), view2);
281     EXPECT_EQ(view2->GetNextRenderSibling(), view3);
282     EXPECT_EQ(view3->GetNextRenderSibling(), view4);
283 
284     view1->SetZIndex(10); // 10: zindex
285     // check child when zIndex change
286     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
287     EXPECT_EQ(view2->GetNextRenderSibling(), view3);
288     EXPECT_EQ(view3->GetNextRenderSibling(), view4);
289     EXPECT_EQ(view4->GetNextRenderSibling(), view1);
290 
291     view3->SetZIndex(-10); // -10: zindex
292     // check child when zIndex change
293     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
294     EXPECT_EQ(view3->GetNextRenderSibling(), view2);
295     EXPECT_EQ(view2->GetNextRenderSibling(), view4);
296     EXPECT_EQ(view4->GetNextRenderSibling(), view1);
297 
298     viewGroup->Remove(view3);
299     // check child when zIndex change
300     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
301     EXPECT_EQ(view2->GetNextRenderSibling(), view4);
302     EXPECT_EQ(view4->GetNextRenderSibling(), view1);
303 
304     delete view1;
305     delete view2;
306     delete view3;
307     delete view4;
308     delete viewGroup;
309 }
310 
311 /**
312  * @tc.name: Graphic_UIView_Test_SetChildrenRenderHead_001
313  * @tc.desc: Check child render head
314  * @tc.type: FUNC
315  * @tc.require:issueI5AD8G
316  */
317 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_SetChildrenRenderHead_001, TestSize.Level0)
318 {
319     UIView* view1 = new UIView();
320     UIViewGroup* viewGroup = new UIViewGroup();
321 
322     // check default child head
323     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
324 
325     viewGroup->SetChildrenRenderHead(view1);
326     // can not set child head if it is not child
327     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
328 
329     viewGroup->Add(view1);
330     // check child head
331     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
332 
333     delete view1;
334     delete viewGroup;
335 }
336 
337 /**
338  * @tc.name: Graphic_UIView_Test_UpdateRenderView_001
339  * @tc.desc: check update render view
340  * @tc.type: FUNC
341  * @tc.require:issueI5AD8G
342  */
343 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_UpdateRenderView_001, TestSize.Level0)
344 {
345     UIView* view1 = new UIView();
346     UIView* view2 = new UIView();
347     UIViewGroup* viewGroup = new UIViewGroup();
348     viewGroup->Add(view1);
349     viewGroup->Add(view2);
350     viewGroup->UpdateRenderView(view2);
351     // check child head when remove all
352     EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
353     // check child head when insert view
354     EXPECT_EQ(view1->GetNextRenderSibling(), view2);
355 
356     delete view1;
357     delete view2;
358     delete viewGroup;
359 }
360 
361 /**
362  * @tc.name: Graphic_UIView_Test_ZIndex_001
363  * @tc.desc: check zIndex
364  * @tc.type: FUNC
365  */
366 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_ZIndex_001, TestSize.Level0)
367 {
368     UIViewGroup* group = new UIViewGroup();
369     struct ZIndexView {
370         const char* text;
371         int16_t zIndex;
372     };
373     constexpr uint8_t VIEW_NUM = 12;
374     const ZIndexView VIEW_GROUP[VIEW_NUM] = {{"label1=0", 1}, {"label1=1", 1}, {"label1=2", 1}, {"label1=3", 1},
375                                              {"label2=0", 2}, {"label2=1", 2}, {"label2=2", 2}, {"label2=3", 2},
376                                              {"label3=0", 3}, {"label3=1", 3}, {"label3=2", 3}, {"label3=3", 3}};
377     for (int16_t i = 0; i < VIEW_NUM; i++) {
378         UILabel* label = new UILabel();
379         label->SetText(VIEW_GROUP[i].text);
380         label->SetViewId(VIEW_GROUP[i].text);
381         label->SetZIndex(VIEW_GROUP[i].zIndex);
382         group->Add(label);
383     }
384 
385     UIView* label = group->GetChildrenHead();
386     // check child head
387     UIView* targetLabel = group->GetChildById(VIEW_GROUP[0].text); // 0: label1=0
388     EXPECT_EQ(group->GetChildrenRenderHead(), targetLabel);
389 
390     // check head change zIndex
391     targetLabel->SetZIndex(3);                                     // 3: zIndex
392     UIView* beforeLabel = group->GetChildById(VIEW_GROUP[7].text); // 7: label2=3
393     EXPECT_EQ(beforeLabel->GetNextRenderSibling(), targetLabel);
394 
395     UIView* newRenderHead = group->GetChildById(VIEW_GROUP[1].text); // 7: label1=1
396     EXPECT_EQ(group->GetChildrenRenderHead(), newRenderHead);
397 
398     targetLabel->SetZIndex(0);
399     EXPECT_EQ(group->GetChildrenRenderHead(), targetLabel);
400 
401     targetLabel->SetZIndex(5);                              // 5: zIndex
402     beforeLabel = group->GetChildById(VIEW_GROUP[11].text); // 11: label3=3
403     EXPECT_EQ(beforeLabel->GetNextRenderSibling(), targetLabel);
404 
405     // free view
406     for (int16_t i = 0; i < VIEW_NUM; i++) {
407         UIView* label = group->GetChildrenHead();
408         group->Remove(label);
409         delete label;
410     }
411     delete group;
412 }
413 
414 /**
415  * @tc.name: Graphic_UIView_Test_ZIndex_002
416  * @tc.desc: check zIndex
417  * @tc.type: FUNC
418  */
419 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_ZIndex_002, TestSize.Level0)
420 {
421     struct ZIndexView {
422         const char* text;
423         int16_t zIndex;
424     };
425     constexpr uint8_t VIEW_NUM = 14;
426     const ZIndexView VIEW_GROUP[VIEW_NUM] = {{"label1=0", 1}, {"label1=1", 1}, {"label1=2", 1}, {"label1=3", 1},
427                                              {"label2=0", 2}, {"label2=1", 2}, {"label2=2", 2}, {"label2=3", 2},
428                                              {"label3=0", 3}, {"label3=1", 3}, {"label3=2", 3}, {"label3=3", 3},
429                                              {"label5=0", 5}, {"label6=0", 6}};
430     UIViewGroup* group = new UIViewGroup();
431     for (int16_t i = 0; i < VIEW_NUM; i++) {
432         UILabel* label = new UILabel();
433         label->SetText(VIEW_GROUP[i].text);
434         label->SetViewId(VIEW_GROUP[i].text);
435         label->SetZIndex(VIEW_GROUP[i].zIndex);
436         group->Add(label);
437     }
438 
439     UIView* label = group->GetChildrenHead();
440     // check child head
441     UIView* targetLabel = group->GetChildById(VIEW_GROUP[0].text);
442 
443     targetLabel->SetZIndex(4); // 4: zIndex
444     UIView* beforeLabel = group->GetChildById(VIEW_GROUP[11].text); // 11: label3=3
445     EXPECT_EQ(beforeLabel->GetNextRenderSibling(), targetLabel);
446 
447     targetLabel = group->GetChildById(VIEW_GROUP[2].text); // 2: label1=2
448     targetLabel->SetZIndex(2);                             // 2: zIndex
449     beforeLabel = group->GetChildById(VIEW_GROUP[3].text); // 3: label1=3
450     EXPECT_EQ(beforeLabel->GetNextRenderSibling(), targetLabel);
451 
452     targetLabel->SetZIndex(6);                              // 6: zIndex
453     beforeLabel = group->GetChildById(VIEW_GROUP[12].text); // 12: label5=0
454     EXPECT_EQ(beforeLabel->GetNextRenderSibling(), targetLabel);
455 
456     targetLabel = group->GetChildById(VIEW_GROUP[12].text); // 12: label5=0
457     targetLabel->SetZIndex(2);                              // 2: zIndex
458     beforeLabel = group->GetChildById(VIEW_GROUP[7].text);  // 7: label2=3
459     EXPECT_EQ(beforeLabel->GetNextRenderSibling(), targetLabel);
460 
461     // free view
462     for (int16_t i = 0; i < VIEW_NUM; i++) {
463         UIView* label = group->GetChildrenHead();
464         group->Remove(label);
465         delete label;
466     }
467     delete group;
468 }
469 } // namespace OHOS
470