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 EXPECT_EQ(sibling, nullptr);
173 delete viewGroup;
174 }
175
176 /**
177 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_001
178 * @tc.desc: Check child render head
179 * @tc.type: FUNC
180 * @tc.require:issueI5AD8G
181 */
182 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_002, TestSize.Level0)
183 {
184 UIView* view1 = new UIView();
185 UIView* view2 = new UIView();
186 UIView* view3 = new UIView();
187 UIViewGroup* viewGroup = new UIViewGroup();
188 viewGroup->Add(view1);
189 // check child head
190 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
191
192 viewGroup->Add(view2);
193 // check child head with two child
194 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
195
196 viewGroup->Remove(view1);
197 // check child head when remove head
198 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
199
200 viewGroup->Remove(view2);
201 // check child head when remove all
202 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
203
204 viewGroup->Add(view1);
205 viewGroup->Add(view2);
206 viewGroup->RemoveAll();
207 // check child head when remove all
208 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
209
210 viewGroup->Add(view1);
211 viewGroup->Add(view2);
212 viewGroup->Insert(view1, view3);
213 viewGroup->Remove(view1);
214 // check child head when insert view
215 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
216
217 delete view1;
218 delete view2;
219 delete view3;
220 delete viewGroup;
221 }
222
223 /**
224 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_003
225 * @tc.desc: Check child render head when zIndex change
226 * @tc.type: FUNC
227 * @tc.require:issueI5AD8G
228 */
229 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_003, TestSize.Level0)
230 {
231 UIView* view1 = new UIView();
232 UIView* view2 = new UIView();
233 UIView* view3 = new UIView();
234 UIView* view4 = new UIView();
235 UIViewGroup* viewGroup = new UIViewGroup();
236 viewGroup->Add(view1);
237 viewGroup->Add(view2);
238 viewGroup->Add(view3);
239 viewGroup->Add(view4);
240 // check child with zIndex = 0
241 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
242
243 view1->SetZIndex(10); // 10: zindex
244 // check child when zIndex change
245 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
246
247 view3->SetZIndex(-10); // -10: zindex
248 // check child when zIndex change
249 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
250
251 delete view1;
252 delete view2;
253 delete view3;
254 delete view4;
255 delete viewGroup;
256 }
257
258 /**
259 * @tc.name: Graphic_UIView_Test_GetChildrenRenderHead_004
260 * @tc.desc: Check child render head when zIndex change
261 * @tc.type: FUNC
262 * @tc.require:issueI5AD8G
263 */
264 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_GetChildrenRenderHead_004, TestSize.Level0)
265 {
266 UIView* view1 = new UIView();
267 UIView* view2 = new UIView();
268 UIView* view3 = new UIView();
269 UIView* view4 = new UIView();
270 UIViewGroup* viewGroup = new UIViewGroup();
271 viewGroup->Add(view1);
272 viewGroup->Add(view2);
273 viewGroup->Add(view3);
274 viewGroup->Add(view4);
275 // check child with zIndex = 0
276 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
277 EXPECT_EQ(view1->GetNextRenderSibling(), view2);
278 EXPECT_EQ(view2->GetNextRenderSibling(), view3);
279 EXPECT_EQ(view3->GetNextRenderSibling(), view4);
280
281 view1->SetZIndex(10); // 10: zindex
282 // check child when zIndex change
283 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
284 EXPECT_EQ(view2->GetNextRenderSibling(), view3);
285 EXPECT_EQ(view3->GetNextRenderSibling(), view4);
286 EXPECT_EQ(view4->GetNextRenderSibling(), view1);
287
288 view3->SetZIndex(-10); // -10: zindex
289 // check child when zIndex change
290 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view3);
291 EXPECT_EQ(view3->GetNextRenderSibling(), view2);
292 EXPECT_EQ(view2->GetNextRenderSibling(), view4);
293 EXPECT_EQ(view4->GetNextRenderSibling(), view1);
294
295 viewGroup->Remove(view3);
296 // check child when zIndex change
297 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view2);
298 EXPECT_EQ(view2->GetNextRenderSibling(), view4);
299 EXPECT_EQ(view4->GetNextRenderSibling(), view1);
300
301 delete view1;
302 delete view2;
303 delete view3;
304 delete view4;
305 delete viewGroup;
306 }
307
308 /**
309 * @tc.name: Graphic_UIView_Test_SetChildrenRenderHead_001
310 * @tc.desc: Check child render head
311 * @tc.type: FUNC
312 * @tc.require:issueI5AD8G
313 */
314 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_SetChildrenRenderHead_001, TestSize.Level0)
315 {
316 UIView* view1 = new UIView();
317 UIViewGroup* viewGroup = new UIViewGroup();
318
319 // check default child head
320 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
321
322 viewGroup->SetChildrenRenderHead(view1);
323 // can not set child head if it is not child
324 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), nullptr);
325
326 viewGroup->Add(view1);
327 // check child head
328 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
329
330 delete view1;
331 delete viewGroup;
332 }
333
334 /**
335 * @tc.name: Graphic_UIView_Test_UpdateRenderView_001
336 * @tc.desc: check update render view
337 * @tc.type: FUNC
338 * @tc.require:issueI5AD8G
339 */
340 HWTEST_F(UIViewGroupTest, Graphic_UIView_Test_UpdateRenderView_001, TestSize.Level0)
341 {
342 UIView* view1 = new UIView();
343 UIView* view2 = new UIView();
344 UIViewGroup* viewGroup = new UIViewGroup();
345 viewGroup->Add(view1);
346 viewGroup->Add(view2);
347 viewGroup->UpdateRenderView(view2);
348 // check child head when remove all
349 EXPECT_EQ(viewGroup->GetChildrenRenderHead(), view1);
350 // check child head when insert view
351 EXPECT_EQ(view1->GetNextRenderSibling(), view2);
352
353 delete view1;
354 delete view2;
355 delete viewGroup;
356 }
357 } // namespace OHOS
358