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 "ui_test_view_group.h"
17 
18 #include "common/screen.h"
19 
20 namespace OHOS {
SetUp()21 void UITestViewGroup::SetUp()
22 {
23     if (container_ == nullptr) {
24         container_ = new UIScrollView();
25         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
26     }
27 }
28 
TearDown()29 void UITestViewGroup::TearDown()
30 {
31     DeleteChildren(container_);
32     container_ = nullptr;
33 }
34 
GetTestView()35 const UIView* UITestViewGroup::GetTestView()
36 {
37     UIKitViewGroupTestAddRemove001();
38     UIKitViewGroupTestAddError001();
39     UIKitViewGroupTestInsertError001();
40     return container_;
41 }
42 
CreateTestCaseGroup() const43 UIViewGroup* UITestViewGroup::CreateTestCaseGroup() const
44 {
45     UIViewGroup* group = new UIViewGroup();
46     group->Resize(Screen::GetInstance().GetWidth(), 200); // 200: height
47     return group;
48 }
49 
CreateTitleLabel() const50 UILabel* UITestViewGroup::CreateTitleLabel() const
51 {
52     UILabel* label = new UILabel();
53     // 300: label width
54     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 300, TITLE_LABEL_DEFAULT_HEIGHT);
55     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
56 
57     return label;
58 }
59 
CreateButton(const char * text,int16_t width,int16_t height) const60 UILabelButton* UITestViewGroup::CreateButton(const char* text, int16_t width, int16_t height) const
61 {
62     UILabelButton* button = new UILabelButton();
63     button->Resize(width, height);
64     button->SetText(text);
65     button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
66     button->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
67     button->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
68     button->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
69     button->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_PRESS, UIButton::PRESSED);
70 
71     return button;
72 }
73 
UIKitViewGroupTestAddRemove001()74 void UITestViewGroup::UIKitViewGroupTestAddRemove001()
75 {
76     if (container_ == nullptr) {
77         return;
78     }
79 
80     UIViewGroup* group = CreateTestCaseGroup();
81     group->SetViewId("UIKit_ViewGroup_Test_AddRemove_001");
82     group->SetPosition(0, 0);
83 
84     UILabel* label = CreateTitleLabel();
85     group->Add(label);
86     label->SetText("UIViewGroup 添加/删除组件:");
87     label->SetViewId("label_text");
88 
89     addBtn_ = CreateButton("添加View", 150, 40); // 150: width 40:height
90     group->Add(addBtn_);
91     addBtn_->SetOnClickListener(this);
92     addBtn_->SetViewId("addBtn");
93     addBtn_->LayoutBottomOfParent();
94     addBtn_->LayoutLeftOfParent(10); // 10: offset
95 
96     removeBtn_ = CreateButton("删除View", 150, 40); // 150: width 40:height
97     group->Add(removeBtn_);
98     removeBtn_->SetOnClickListener(this);
99     removeBtn_->SetViewId("removeBtn");
100     removeBtn_->LayoutBottomOfParent();
101     removeBtn_->LayoutRightToSibling("addBtn", 10); // 10: offset
102 
103     removeAddBtn_ = CreateButton("删除再添加View", 160, 40); // 160: width 40:height
104     group->Add(removeAddBtn_);
105     removeAddBtn_->SetOnClickListener(this);
106     removeAddBtn_->SetViewId("removeAddBtn");
107     removeAddBtn_->LayoutBottomOfParent();
108     removeAddBtn_->LayoutRightToSibling("removeBtn", 10); // 10: offset
109 
110     container_->Add(group);
111 }
112 
UIKitViewGroupTestAddError001()113 void UITestViewGroup::UIKitViewGroupTestAddError001()
114 {
115     if (container_ == nullptr) {
116         return;
117     }
118 
119     UIViewGroup* group = CreateTestCaseGroup();
120     group->SetViewId("UIKit_ViewGroup_Test_Add_Error_001");
121     container_->Add(group);
122 
123     UILabel* label = CreateTitleLabel();
124     group->Add(label);
125     // 2: half of screen width
126     label->Resize(Screen::GetInstance().GetWidth() / 2, TITLE_LABEL_DEFAULT_HEIGHT);
127     label->SetText("测试重复添加子组件问题:");
128 
129     addTwiceBtn_ = CreateButton("重复add", 150, 40); // 150: width 40:height
130     group->Add(addTwiceBtn_);
131     addTwiceBtn_->SetViewId("addTwiceBtn");
132     addTwiceBtn_->SetOnClickListener(this);
133     addTwiceBtn_->LayoutCenterOfParent();
134     addTwiceBtn_->LayoutLeftOfParent(10); // 10: offset
135 
136     addMultiParentBtn_ = CreateButton("add到不同父节点", 200, 40); // 200: width 40:height
137     group->Add(addMultiParentBtn_);
138     addMultiParentBtn_->SetViewId("addMultiParentBtn");
139     addMultiParentBtn_->SetOnClickListener(this);
140     addMultiParentBtn_->LayoutCenterOfParent();
141     addMultiParentBtn_->LayoutRightToSibling("addTwiceBtn", 10); // 10: offset
142 
143     addSelfBtn_ = CreateButton("add到自己", 150, 40); // 150: width 40:height
144     group->Add(addSelfBtn_);
145     addSelfBtn_->SetViewId("addSelfBtn");
146     addSelfBtn_->SetOnClickListener(this);
147     addSelfBtn_->LayoutCenterOfParent();
148     addSelfBtn_->LayoutRightToSibling("addMultiParentBtn", 10); // 10: offset
149 
150     group->LayoutBottomToSibling("UIKit_ViewGroup_Test_AddRemove_001", 10); // 10: offset
151 }
152 
UIKitViewGroupTestInsertError001()153 void UITestViewGroup::UIKitViewGroupTestInsertError001()
154 {
155     if (container_ == nullptr) {
156         return;
157     }
158 
159     UIViewGroup* group = CreateTestCaseGroup();
160     group->SetViewId("UIKit_ViewGroup_Test_Insert_Error_001");
161     container_->Add(group);
162 
163     UILabel* label = CreateTitleLabel();
164     group->Add(label);
165     // 2: half of screen width
166     label->Resize(Screen::GetInstance().GetWidth() / 2, TITLE_LABEL_DEFAULT_HEIGHT);
167     label->SetText("测试Insert子组件问题:");
168 
169     insertTwiceBtn_ = CreateButton("重复insert", 150, 40); // 150: width 40:height
170     group->Add(insertTwiceBtn_);
171     insertTwiceBtn_->SetViewId("insertTwiceBtn");
172     insertTwiceBtn_->SetOnClickListener(this);
173     insertTwiceBtn_->LayoutCenterOfParent();
174     insertTwiceBtn_->LayoutLeftOfParent(10); // 10: offset
175 
176     insertMultiParentBtn_ = CreateButton("insert到不同父节点", 200, 40); // 200: width 40:height
177     group->Add(insertMultiParentBtn_);
178     insertMultiParentBtn_->SetViewId("insertMultiParentBtn");
179     insertMultiParentBtn_->SetOnClickListener(this);
180     insertMultiParentBtn_->LayoutCenterOfParent();
181     insertMultiParentBtn_->LayoutRightToSibling("insertTwiceBtn", 10); // 10: offset
182 
183     insertSelfBtn_ = CreateButton("insert到自己", 150, 40); // 150: width 40:height
184     group->Add(insertSelfBtn_);
185     insertSelfBtn_->SetViewId("addSelfBtn");
186     insertSelfBtn_->SetOnClickListener(this);
187     insertSelfBtn_->LayoutCenterOfParent();
188     insertSelfBtn_->LayoutRightToSibling("insertMultiParentBtn", 10); // 10: offset
189 
190     group->LayoutBottomToSibling("UIKit_ViewGroup_Test_Add_Error_001", 10); // 10: offset
191 }
192 
OnClick(UIView & view,const ClickEvent & event)193 bool UITestViewGroup::OnClick(UIView& view, const ClickEvent& event)
194 {
195     if (&view == addBtn_) {
196         AddView();
197     } else if (&view == removeBtn_) {
198         RemoveView();
199     } else if (&view == removeAddBtn_) {
200         RemoveAndAddView();
201     } else if (&view == addTwiceBtn_) {
202         UIView* viewToAdd = new UIView();
203         container_->Add(viewToAdd);
204         container_->Add(viewToAdd);
205     } else if (&view == addMultiParentBtn_) {
206         AddMultiParent();
207     } else if (&view == addSelfBtn_) {
208         container_->Add(container_);
209     } else if (&view == insertTwiceBtn_) {
210         UIView* viewToBeInsert = new UIView();
211         container_->Insert(nullptr, viewToBeInsert);
212         container_->Insert(nullptr, viewToBeInsert);
213     } else if (&view == insertMultiParentBtn_) {
214         InsertMultiParent();
215     } else if (&view == insertSelfBtn_) {
216         container_->Insert(nullptr, container_);
217     }
218     return true;
219 }
220 
AddView()221 void UITestViewGroup::AddView()
222 {
223     UIView* view = new UIView();
224     view->Resize(200, 50);     // 200: width 50: height
225     view->SetPosition(50, 50); // 50: position x 50: position y
226     view->SetStyle(STYLE_BACKGROUND_COLOR, Color::Yellow().full);
227     view->SetViewId("id_view1");
228     UIViewGroup* vg = static_cast<UIViewGroup*>(container_->GetChildById("UIKit_ViewGroup_Test_AddRemove_001"));
229     if (vg != nullptr) {
230         vg->Add(view);
231         vg->Invalidate();
232     }
233 }
234 
RemoveView()235 void UITestViewGroup::RemoveView()
236 {
237     UIViewGroup* vg = static_cast<UIViewGroup*>(container_->GetChildById("UIKit_ViewGroup_Test_AddRemove_001"));
238     UIView* view1 = container_->GetChildById("id_view1");
239     if ((vg != nullptr) && (view1 != nullptr)) {
240         vg->Remove(view1);
241         vg->Invalidate();
242     }
243 }
244 
RemoveAndAddView()245 void UITestViewGroup::RemoveAndAddView()
246 {
247     UIViewGroup* vg = static_cast<UIViewGroup*>(container_->GetChildById("UIKit_ViewGroup_Test_AddRemove_001"));
248     UIView* view1 = container_->GetChildById("id_view1");
249     if ((vg != nullptr) && (view1 != nullptr)) {
250         vg->Remove(view1);
251         vg->Add(view1);
252         vg->Invalidate();
253     }
254 }
255 
AddMultiParent()256 void UITestViewGroup::AddMultiParent()
257 {
258     UIView* view = new UIView();
259     UIViewGroup* vg = static_cast<UIViewGroup*>(container_->GetChildById("UIKit_ViewGroup_Test_Add_Error_001"));
260     if ((vg != nullptr) && (view != nullptr)) {
261         vg->Add(view);
262     }
263     container_->Add(view);
264 }
265 
InsertMultiParent()266 void UITestViewGroup::InsertMultiParent()
267 {
268     UIView* view = new UIView();
269     UIViewGroup* vg = static_cast<UIViewGroup*>(container_->GetChildById("UIKit_ViewGroup_Test_Insert_Error_001"));
270     if ((vg != nullptr) && (view != nullptr)) {
271         vg->Insert(nullptr, view);
272     }
273     container_->Insert(nullptr, view);
274 }
275 } // namespace OHOS
276