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_bounds.h"
17 
18 #if ENABLE_DEBUG
19 #include "common/screen.h"
20 #include "components/root_view.h"
21 #include "components/ui_button.h"
22 #include "components/ui_checkbox.h"
23 #include "components/ui_digital_clock.h"
24 #include "components/ui_label.h"
25 #include "components/ui_label_button.h"
26 #include "components/ui_radio_button.h"
27 #include "components/ui_toggle_button.h"
28 #include "font/ui_font.h"
29 #include "test_resource_config.h"
30 
31 #include "dfx/ui_view_bounds.h"
32 
33 namespace OHOS {
~UITestViewBounds()34 UITestViewBounds::~UITestViewBounds()
35 {
36     if (viewBoundsChangeListener_ != nullptr) {
37         delete viewBoundsChangeListener_;
38         viewBoundsChangeListener_ = nullptr;
39     }
40 }
SetUp()41 void UITestViewBounds::SetUp()
42 {
43     if (container_ == nullptr) {
44         container_ = new UIScrollView();
45         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
46         container_->SetHorizontalScrollState(false);
47         container_->SetThrowDrag(true);
48     }
49 }
50 
TearDown()51 void UITestViewBounds::TearDown()
52 {
53     DeleteChildren(container_);
54     container_ = nullptr;
55 }
56 
GetTestView()57 const UIView* UITestViewBounds::GetTestView()
58 {
59     UIKitViewBoundsSetState001();
60     UIKitViewBoundsMargin001();
61     UIKitViewBoundsPadding001();
62     UIKitViewBoundsBorder001();
63     UIKitViewBounds001();
64     UIKitViewBoundsInvisible001();
65     UIKitViewBoundsSmallView001();
66     return container_;
67 }
68 
69 class ViewBoundsChangeListener : public UIToggleButton::OnChangeListener {
70 public:
ViewBoundsChangeListener()71     ViewBoundsChangeListener() {}
~ViewBoundsChangeListener()72     ~ViewBoundsChangeListener() {}
73 
OnChange(UICheckBox::UICheckBoxState state)74     bool OnChange(UICheckBox::UICheckBoxState state) override
75     {
76         switch (state) {
77             case UICheckBox::SELECTED:
78                 UIViewBounds::GetInstance()->SetShowState(true);
79                 break;
80 
81             case UICheckBox::UNSELECTED:
82                 UIViewBounds::GetInstance()->SetShowState(false);
83                 break;
84             default:
85                 break;
86         }
87         return true;
88     }
89 };
90 
CreateTitleLabel() const91 UILabel* UITestViewBounds::CreateTitleLabel() const
92 {
93     UILabel* label = new UILabel();
94     label->SetHeight(TITLE_LABEL_DEFAULT_HEIGHT);
95     label->SetLineBreakMode(UILabel::LINE_BREAK_STRETCH);
96     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE);
97     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
98     return label;
99 }
100 
UIKitViewBoundsSetState001()101 void UITestViewBounds::UIKitViewBoundsSetState001()
102 {
103     if (container_ == nullptr) {
104         return;
105     }
106     UIViewGroup* group = new UIViewGroup();
107     container_->Add(group);
108     group->SetViewId("UIKitViewBoundsSetState001");
109     group->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, 5, 400, 50); // 5: offset 400: width 50: height
110     UILabel* viewBoundsLabel = new UILabel();
111     group->Add(viewBoundsLabel);
112     viewBoundsLabel->SetHeight(30); // 30: height
113     viewBoundsLabel->LayoutCenterOfParent();
114     viewBoundsLabel->LayoutLeftOfParent(5); // 5: offset
115     viewBoundsLabel->SetLineBreakMode(UILabel::LINE_BREAK_STRETCH);
116     viewBoundsLabel->SetText("Show View Bounds: ");
117 
118     UIToggleButton* toggle = new UIToggleButton();
119     group->Add(toggle);
120     toggle->SetWidth(50);  // 50: width
121     toggle->SetHeight(50); // 50: height
122     toggle->LayoutCenterOfParent();
123     toggle->LayoutRightOfParent(5); // 5: offset
124     toggle->SetViewId(UI_TEST_SHOW_VIEW_BOUNDS);
125     if (viewBoundsChangeListener_ != nullptr) {
126         delete viewBoundsChangeListener_;
127     }
128     viewBoundsChangeListener_ = new ViewBoundsChangeListener();
129     toggle->SetOnChangeListener(viewBoundsChangeListener_);
130     if (UIViewBounds::GetInstance()->GetShowState()) {
131         toggle->SetState(true);
132     } else {
133         toggle->SetState(false);
134     }
135 }
136 
UIKitViewBoundsMargin001()137 void UITestViewBounds::UIKitViewBoundsMargin001()
138 {
139     if (container_ == nullptr) {
140         return;
141     }
142     UIViewGroup* group = new UIViewGroup();
143     container_->Add(group);
144     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
145     group->SetViewId("UIKitViewBoundsMargin001");
146     group->LayoutBottomToSibling("UIKitViewBoundsSetState001");
147 
148     UILabel* title = CreateTitleLabel();
149     group->Add(title);
150     title->SetText("Test View bounds with margin: ");
151     title->SetViewId("margin_title");
152 
153     UIView* view1 = new UIView();
154     group->Add(view1);
155     view1->Resize(50, 50);                            // 50: view size
156     view1->LayoutBottomToSibling("margin_title", 10); // 10: offset
157     view1->AlignLeftToSibling("margin_title");
158     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
159     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
160     view1->SetStyle(STYLE_MARGIN_LEFT, 50); // 50: margin size
161 }
162 
UIKitViewBoundsPadding001()163 void UITestViewBounds::UIKitViewBoundsPadding001()
164 {
165     if (container_ == nullptr) {
166         return;
167     }
168     UIViewGroup* group = new UIViewGroup();
169     container_->Add(group);
170     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
171     group->SetViewId("UIKitViewBoundsPadding001");
172     group->LayoutBottomToSibling("UIKitViewBoundsMargin001");
173 
174     UILabel* title = CreateTitleLabel();
175     group->Add(title);
176     title->SetText("Test View bounds with padding: ");
177     title->SetViewId("padding_title");
178 
179     UIView* view1 = new UIView();
180     group->Add(view1);
181     view1->Resize(50, 50);                             // 50: view width and height
182     view1->LayoutBottomToSibling("padding_title", 10); // 10: offset
183     view1->AlignLeftToSibling("padding_title");
184     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
185     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
186     view1->SetStyle(STYLE_PADDING_LEFT, 50); // 50: padding size
187 }
188 
UIKitViewBoundsBorder001()189 void UITestViewBounds::UIKitViewBoundsBorder001()
190 {
191     if (container_ == nullptr) {
192         return;
193     }
194     UIViewGroup* group = new UIViewGroup();
195     container_->Add(group);
196     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
197     group->SetViewId("UIKitViewBoundsBorder001");
198     group->LayoutBottomToSibling("UIKitViewBoundsPadding001");
199 
200     UILabel* title = CreateTitleLabel();
201     group->Add(title);
202     title->SetText("Test View bounds with border: ");
203     title->SetViewId("border_title");
204 
205     UIView* view1 = new UIView();
206     group->Add(view1);
207     view1->Resize(50, 50);                            // 50: view size
208     view1->LayoutBottomToSibling("border_title", 10); // 10: offset
209     view1->AlignLeftToSibling("border_title");
210     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
211     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
212     view1->SetStyle(STYLE_BORDER_COLOR, Color::Red().full);
213     view1->SetStyle(STYLE_BORDER_OPA, OPA_OPAQUE);
214     view1->SetStyle(STYLE_BORDER_WIDTH, 20); // 20: border size
215 }
216 
UIKitViewBounds001()217 void UITestViewBounds::UIKitViewBounds001()
218 {
219     if (container_ == nullptr) {
220         return;
221     }
222     UIViewGroup* group = new UIViewGroup();
223     container_->Add(group);
224     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
225     group->SetViewId("UIKitViewBounds001");
226     group->LayoutBottomToSibling("UIKitViewBoundsBorder001");
227 
228     UILabel* title = CreateTitleLabel();
229     group->Add(title);
230     title->SetText("Test View bounds with margin/padding/border: ");
231     title->SetViewId("margin_padding_border_title");
232 
233     UIView* view1 = new UIView();
234     group->Add(view1);
235     view1->Resize(50, 50);                                           // 50: view size
236     view1->LayoutBottomToSibling("margin_padding_border_title", 10); // 10: offset
237     view1->AlignLeftToSibling("margin_padding_border_title");
238     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
239     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
240     view1->SetStyle(STYLE_BORDER_COLOR, Color::Yellow().full);
241     view1->SetStyle(STYLE_BORDER_OPA, OPA_OPAQUE);
242     view1->SetStyle(STYLE_BORDER_WIDTH, 20); // 20: border size
243     view1->SetStyle(STYLE_MARGIN_LEFT, 20);  // 20: margin size
244     view1->SetStyle(STYLE_PADDING_LEFT, 50); // 50: padding size
245 }
246 
UIKitViewBoundsInvisible001()247 void UITestViewBounds::UIKitViewBoundsInvisible001()
248 {
249     if (container_ == nullptr) {
250         return;
251     }
252     UIViewGroup* group = new UIViewGroup();
253     container_->Add(group);
254     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
255     group->SetViewId("UIKitViewBoundsInvisible001");
256     group->LayoutBottomToSibling("UIKitViewBounds001");
257 
258     UILabel* title = CreateTitleLabel();
259     group->Add(title);
260     title->SetText("Test View bounds when view is invisible: ");
261     title->SetViewId("invisible_title");
262 
263     UIView* view1 = new UIView();
264     group->Add(view1);
265     view1->Resize(50, 50);                               // 50: view size
266     view1->LayoutBottomToSibling("invisible_title", 10); // 10: offset
267     view1->AlignLeftToSibling("invisible_title");
268     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
269     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
270     view1->SetStyle(STYLE_MARGIN_LEFT, 20); // 20: margin size
271     view1->SetVisible(false);
272 }
273 
UIKitViewBoundsSmallView001()274 void UITestViewBounds::UIKitViewBoundsSmallView001()
275 {
276     if (container_ == nullptr) {
277         return;
278     }
279     UIViewGroup* group = new UIViewGroup();
280     container_->Add(group);
281     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
282     group->SetViewId("UIKitViewBoundsSmallView001");
283     group->LayoutBottomToSibling("UIKitViewBoundsInvisible001");
284 
285     UILabel* title = CreateTitleLabel();
286     group->Add(title);
287     title->SetText("Test View bounds when view is small: ");
288     title->SetViewId("small_title");
289 
290     UIView* view1 = new UIView();
291     group->Add(view1);
292     view1->Resize(10, 10);                           // 10: view size
293     view1->LayoutBottomToSibling("small_title", 10); // 10: offset
294     view1->AlignLeftToSibling("small_title");
295     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
296     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
297 }
298 
UIKitViewBoundsPostDraw001()299 void UITestViewBounds::UIKitViewBoundsPostDraw001()
300 {
301     if (container_ == nullptr) {
302         return;
303     }
304 
305     UIViewGroup* group = new UIViewGroup();
306     container_->Add(group);
307     group->Resize(Screen::GetInstance().GetWidth(), 150); // 150: height
308     group->SetViewId("UIKitViewBoundsPostDraw001");
309     group->LayoutBottomToSibling("UIKitViewBoundsSmallView001");
310 
311     UILabel* title = CreateTitleLabel();
312     group->Add(title);
313     title->SetText("Test View bounds when child view override OnPostDraw: ");
314     title->SetViewId("postdraw_title");
315 
316     UIView* view1 = new UIView();
317     group->Add(view1);
318     view1->Resize(10, 10);                           // 10: view size
319     view1->LayoutBottomToSibling("small_title", 10); // 10: offset
320     view1->AlignLeftToSibling("small_title");
321     view1->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
322     view1->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
323 }
324 } // namespace OHOS
325 #endif
326