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 #include <climits>
18 #include <gtest/gtest.h>
19
20 using namespace testing::ext;
21 namespace OHOS {
22 class UIViewGroupTest : public testing::Test {
23 public:
UIViewGroupTest()24 UIViewGroupTest() : viewGroup_(nullptr) {}
~UIViewGroupTest()25 ~UIViewGroupTest() {}
SetUpTestCase(void)26 static void SetUpTestCase(void) {}
TearDownTestCase(void)27 static void TearDownTestCase(void) {}
28 void SetUp(void);
29 void TearDown(void);
30 UIViewGroup* viewGroup_;
31 };
32
SetUp(void)33 void UIViewGroupTest::SetUp(void)
34 {
35 if (viewGroup_ == nullptr) {
36 viewGroup_ = new UIViewGroup();
37 }
38 }
39
TearDown(void)40 void UIViewGroupTest::TearDown(void)
41 {
42 if (viewGroup_ != nullptr) {
43 delete viewGroup_;
44 viewGroup_ = nullptr;
45 }
46 }
47
48 /**
49 * @tc.name: UIViewGroupAdd_001
50 * @tc.desc: Verify Add function.
51 * @tc.type: FUNC
52 * @tc.require: AR000EEMQF
53 */
54 HWTEST_F(UIViewGroupTest, UIViewGroupAdd_001, TestSize.Level0)
55 {
56 if (viewGroup_ == nullptr) {
57 EXPECT_EQ(1, 0);
58 return;
59 }
60 UIView* view = new UIView();
61 if (view == nullptr) {
62 EXPECT_EQ(1, 0);
63 return;
64 }
65 viewGroup_->Add(view);
66 EXPECT_EQ(view, viewGroup_->GetChildrenHead());
67 EXPECT_EQ(view->GetParent(), viewGroup_);
68
69 delete view;
70 }
71
72 /**
73 * @tc.name: UIViewGroupInsert_001
74 * @tc.desc: Verify Insert function.
75 * @tc.type: FUNC
76 * @tc.require: AR000EEMQF
77 */
78 HWTEST_F(UIViewGroupTest, UIViewGroupInsert_001, TestSize.Level0)
79 {
80 if (viewGroup_ == nullptr) {
81 EXPECT_EQ(1, 0);
82 return;
83 }
84 UIView* preView = new UIView();
85 if (preView == nullptr) {
86 EXPECT_EQ(1, 0);
87 return;
88 }
89 UIView* view = new UIView();
90
91 viewGroup_->Add(preView);
92 viewGroup_->Insert(preView, view);
93 EXPECT_EQ(view, preView->GetNextSibling());
94
95 delete preView;
96 delete view;
97 }
98
99 /**
100 * @tc.name: UIViewGroupRemove_001
101 * @tc.desc: Verify Remove function.
102 * @tc.type: FUNC
103 * @tc.require: AR000EEMQF
104 */
105 HWTEST_F(UIViewGroupTest, UIViewGroupRemove_001, TestSize.Level0)
106 {
107 if (viewGroup_ == nullptr) {
108 EXPECT_EQ(1, 0);
109 return;
110 }
111 UIView* view = new UIView();
112 if (view == nullptr) {
113 EXPECT_EQ(1, 0);
114 return;
115 }
116 viewGroup_->Add(view);
117 viewGroup_->Remove(view);
118 EXPECT_EQ(nullptr, viewGroup_->GetChildrenHead());
119 EXPECT_EQ(nullptr, view->GetParent());
120
121 delete view;
122 }
123
124 /**
125 * @tc.name: UIViewGroupRemoveAll_001
126 * @tc.desc: Verify RemoveAll function.
127 * @tc.type: FUNC
128 * @tc.require: AR000EEMQF
129 */
130 HWTEST_F(UIViewGroupTest, UIViewGroupRemoveAll_001, TestSize.Level0)
131 {
132 if (viewGroup_ == nullptr) {
133 EXPECT_EQ(1, 0);
134 return;
135 }
136 UIView* view = new UIView();
137 UIView* view2 = new UIView();
138
139 viewGroup_->Add(view);
140 viewGroup_->RemoveAll();
141 EXPECT_EQ(nullptr, viewGroup_->GetChildrenHead());
142
143 delete view;
144 delete view2;
145 }
146
147 /**
148 * @tc.name: UIViewGroupGetViewType_001
149 * @tc.desc: Verify GetViewType function.
150 * @tc.type: FUNC
151 * @tc.require: AR000EEMQF
152 */
153 HWTEST_F(UIViewGroupTest, UIViewGroupGetViewType_001, TestSize.Level1)
154 {
155 if (viewGroup_ == nullptr) {
156 EXPECT_EQ(1, 0);
157 return;
158 }
159 EXPECT_EQ(viewGroup_->GetViewType(), UI_VIEW_GROUP);
160 }
161
162 /**
163 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_001
164 * @tc.desc: Check the child render head
165 * @tc.type: FUNC
166 * @tc.require:issueI5AD8G
167 */
168 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_001, TestSize.Level0)
169 {
170 UIViewGroup* viewGroup = new UIViewGroup();
171 UIView* sibling = viewGroup->GetChildrenRenderHead();
172 if (sibling != nullptr) {
173 EXPECT_NE(0, 0);
174 return;
175 }
176 EXPECT_EQ(0, 0);
177 delete viewGroup;
178 }
179
180 /**
181 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_001
182 * @tc.desc: Check child render head
183 * @tc.type: FUNC
184 * @tc.require:issueI5AD8G
185 */
186 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_002, TestSize.Level0)
187 {
188 UIView* view1 = new UIView();
189 UIView* view2 = new UIView();
190 UIView* view3 = new UIView();
191 UIViewGroup* viewGroup = new UIViewGroup();
192 viewGroup->Add(view1);
193 // check child head
194 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
195
196 viewGroup->Add(view2);
197 // check child head with two child
198 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
199
200 viewGroup->Remove(view1);
201 // check child head when remove head
202 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
203
204 viewGroup->Remove(view2);
205 // check child head when remove all
206 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
207
208 viewGroup->Add(view1);
209 viewGroup->Add(view2);
210 viewGroup->RemoveAll();
211 // check child head when remove all
212 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
213
214 viewGroup->Add(view1);
215 viewGroup->Add(view2);
216 viewGroup->Insert(view1, view3);
217 viewGroup->Remove(view1);
218 // check child head when insert view
219 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
220
221 delete view1;
222 delete view2;
223 delete view3;
224 delete viewGroup;
225 }
226
227 /**
228 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_003
229 * @tc.desc: Check child render head when zIndex change
230 * @tc.type: FUNC
231 * @tc.require:issueI5AD8G
232 */
233 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_003, TestSize.Level0)
234 {
235 UIView* view1 = new UIView();
236 UIView* view2 = new UIView();
237 UIView* view3 = new UIView();
238 UIView* view4 = new UIView();
239 UIViewGroup* viewGroup = new UIViewGroup();
240 viewGroup->Add(view1);
241 viewGroup->Add(view2);
242 viewGroup->Add(view3);
243 viewGroup->Add(view4);
244 // check child with zIndex = 0
245 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
246
247 view1->SetZIndex(10); // 10: zindex
248 // check child when zIndex change
249 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
250
251 view3->SetZIndex(-10); // -10: zindex
252 // check child when zIndex change
253 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
254
255 delete view1;
256 delete view2;
257 delete view3;
258 delete view4;
259 delete viewGroup;
260 }
261
262 /**
263 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_004
264 * @tc.desc: Check child render head when zIndex change
265 * @tc.type: FUNC
266 * @tc.require:issueI5AD8G
267 */
268 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_004, TestSize.Level0)
269 {
270 UIView* view1 = new UIView();
271 UIView* view2 = new UIView();
272 UIView* view3 = new UIView();
273 UIView* view4 = new UIView();
274 UIViewGroup* viewGroup = new UIViewGroup();
275 viewGroup->Add(view1);
276 viewGroup->Add(view2);
277 viewGroup->Add(view3);
278 viewGroup->Add(view4);
279 // check child with zIndex = 0
280 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
281 EXPECT_EQ(view1->GetNextRenderSibling(), view2);
282 EXPECT_EQ(view2->GetNextRenderSibling(), view3);
283 EXPECT_EQ(view3->GetNextRenderSibling(), view4);
284
285 view1->SetZIndex(10); // 10: zindex
286 // check child when zIndex change
287 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
288 EXPECT_EQ(view2->GetNextRenderSibling(), view3);
289 EXPECT_EQ(view3->GetNextRenderSibling(), view4);
290 EXPECT_EQ(view4->GetNextRenderSibling(), view1);
291
292 view3->SetZIndex(-10); // -10: zindex
293 // check child when zIndex change
294 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
295 EXPECT_EQ(view3->GetNextRenderSibling(), view2);
296 EXPECT_EQ(view2->GetNextRenderSibling(), view4);
297 EXPECT_EQ(view4->GetNextRenderSibling(), view1);
298
299 viewGroup->Remove(view3);
300 // check child when zIndex change
301 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
302 EXPECT_EQ(view2->GetNextRenderSibling(), view4);
303 EXPECT_EQ(view4->GetNextRenderSibling(), view1);
304
305 delete view1;
306 delete view2;
307 delete view3;
308 delete view4;
309 delete viewGroup;
310 }
311
312 /**
313 * @tc.name: Graphic_UIView_Test_SetChildrenRenderHead_001
314 * @tc.desc: Check child render head
315 * @tc.type: FUNC
316 * @tc.require:issueI5AD8G
317 */
318 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_SetChildrenRenderHead_001, TestSize.Level0)
319 {
320 UIView* view1 = new UIView();
321 UIViewGroup* viewGroup = new UIViewGroup();
322
323 // check default child head
324 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
325
326 viewGroup->SetChildrenRenderHead(view1);
327 // can not set child head if it is not child
328 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
329
330 viewGroup->Add(view1);
331 // check child head
332 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
333
334 delete view1;
335 delete viewGroup;
336 }
337
338 /**
339 * @tc.name: Graphic_UIView_Test_UpdateRenderView_001
340 * @tc.desc: check update render view
341 * @tc.type: FUNC
342 * @tc.require:issueI5AD8G
343 */
344 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_UpdateRenderView_001, TestSize.Level0)
345 {
346 UIView* view1 = new UIView();
347 UIView* view2 = new UIView();
348 UIViewGroup* viewGroup = new UIViewGroup();
349 viewGroup->Add(view1);
350 viewGroup->Add(view2);
351 viewGroup->UpdateRenderView(view2);
352 // check child head when remove all
353 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
354 // check child head when insert view
355 EXPECT_EQ(view1->GetNextRenderSibling(), view2);
356
357 delete view1;
358 delete view2;
359 delete viewGroup;
360 }
361 } // namespace OHOS
362