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_label.h"
17 #include "common/screen.h"
18 #include "components/ui_checkbox.h"
19 #include "components/ui_label.h"
20 #include "font/ui_font.h"
21
22 namespace OHOS {
SetUp()23 void UITestLabel::SetUp()
24 {
25 if (container_ == nullptr) {
26 container_ = new UIScrollView();
27 container_->SetThrowDrag(true);
28 container_->SetHorizontalScrollState(false);
29 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
30 positionX_ = 0;
31 positionY_ = 0;
32 }
33 }
34
TearDown()35 void UITestLabel::TearDown()
36 {
37 DeleteChildren(container_);
38 container_ = nullptr;
39 uiLabel = nullptr;
40 labelFontSizeBtn1_ = nullptr;
41 labelFontSizeBtn2_ = nullptr;
42 labelFontSizeBtn3_ = nullptr;
43 labelHorAlignBtn1_ = nullptr;
44 labelHorAlignBtn2_ = nullptr;
45 labelHorAlignBtn3_ = nullptr;
46 labelColorBtn1_ = nullptr;
47 labelColorBtn2_ = nullptr;
48 labelColorBtn3_ = nullptr;
49 labelBeyondBtn1_ = nullptr;
50 labelBeyondBtn2_ = nullptr;
51 labelBeyondBtn3_ = nullptr;
52 labelLineBtn1_ = nullptr;
53 labelLineBtn2_ = nullptr;
54 labelVerAlignlBtn1_ = nullptr;
55 labelVerAlignlBtn2_ = nullptr;
56 labelVerAlignlBtn3_ = nullptr;
57 labelDirectionBtn1_ = nullptr;
58 labelDirectionBtn2_ = nullptr;
59 labelSizeBtn1_ = nullptr;
60 labelSizeBtn2_ = nullptr;
61 labelLineHeightBtn1_ = nullptr;
62 labelLineHeightBtn2_ = nullptr;
63 labelLineHeightBtn3_ = nullptr;
64 }
65
GetTestView()66 const UIView* UITestLabel::GetTestView()
67 {
68 UIKitUILabelTestDisplay001();
69 UIKitUILabeTestDisplay002();
70 UIKitUILabeTestDisplay003();
71 return container_;
72 }
73
UIKitUILabelTestDisplay001()74 void UITestLabel::UIKitUILabelTestDisplay001()
75 {
76 if (container_ != nullptr) {
77 UIViewGroup* uiViewGroup = new UIViewGroup();
78 // 320: width; 390: height
79 uiViewGroup->SetPosition(positionX_, positionY_, 320, 390);
80 container_->Add(uiViewGroup);
81 UILabel* label = new UILabel();
82 uiViewGroup->Add(label);
83 // 288: width; 48: height
84 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
85 label->SetText("UILabel效果");
86 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
87 UIViewGroup* uiViewGroupFrame = new UIViewGroup();
88 uiViewGroup->Add(uiViewGroupFrame);
89 // 288: width; 336: height
90 uiViewGroupFrame->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 288, 336);
91 uiViewGroupFrame->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
92 uiViewGroupFrame->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
93 uiViewGroupFrame->SetStyle(STYLE_BORDER_WIDTH, VIEW_STYLE_BORDER_WIDTH);
94 uiViewGroupFrame->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
95 uiViewGroupFrame->SetStyle(STYLE_BACKGROUND_OPA, 0);
96 uiLabel = new UILabel();
97 uiLabel->SetText("图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统图形子系统");
98 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
99 uiLabel->SetPosition(0, 141, 280, 336); // 141: y-coordinate; 280: width; 336: height
100 uiViewGroupFrame->Add(uiLabel);
101 positionX_ += 336; // 336: x-coordinate
102
103 UIViewGroup* uiViewGroup2 = new UIViewGroup();
104 uiViewGroup2->SetPosition(positionX_, 0, 300, 384); // 300: width; 384: height
105 container_->Add(uiViewGroup2);
106 CreateLabels(uiViewGroup2);
107 }
108 }
109
CreateLabels(UIViewGroup * uiViewGroup)110 void UITestLabel::CreateLabels(UIViewGroup* uiViewGroup)
111 {
112 if (uiViewGroup == nullptr) {
113 return;
114 }
115 UILabel* label1 = new UILabel();
116 uiViewGroup->Add(label1);
117 // 288: width; 48: height
118 label1->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
119 label1->SetText("字号");
120 label1->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
121 labelFontSizeBtn1_ = SetUpButton("18 ", 24, 48, uiViewGroup); // 24: x-coordinate; 48: y-coordinate
122 labelFontSizeBtn2_ = SetUpButton("26", 116, 48, uiViewGroup); // 116: x-coordinate; 48: y-coordinate
123 labelFontSizeBtn3_ = SetUpButton("30", 208, 48, uiViewGroup); // 208: x-coordinate; 48: y-coordinate
124
125 UILabel* label2 = new UILabel();
126 uiViewGroup->Add(label2);
127 label2->SetPosition(24, 107, 288, 48); // 24: x-coordinate, 107: y-coordinate, 288: width; 48: height
128 label2->SetText("水平对齐方式");
129 label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
130 labelHorAlignBtn1_ = SetUpButton("左对齐 ", 24, 144, uiViewGroup); // 24: x-coordinate; 144: y-coordinate
131 labelHorAlignBtn2_ = SetUpButton("中对齐 ", 116, 144, uiViewGroup); // 116: x-coordinate; 144: y-coordinate
132 labelHorAlignBtn3_ = SetUpButton("右对齐 ", 208, 144, uiViewGroup); // 208: x-coordinate; 144: y-coordinate
133
134 UILabel* label3 = new UILabel();
135 uiViewGroup->Add(label3);
136 label3->SetPosition(24, 203, 288, 48); // 24: x-coordinate, 203: y-coordinate, 288: width; 48: height
137 label3->SetText("颜色");
138 label3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
139 labelColorBtn1_ = SetUpButton("白色 ", 24, 240, uiViewGroup); // 24: x-coordinate; 240: y-coordinate
140 labelColorBtn2_ = SetUpButton("变色 ", 116, 240, uiViewGroup); // 116: x-coordinate; 240: y-coordinate
141 labelColorBtn3_ = SetUpButton("半透明 ", 208, 240, uiViewGroup); // 208: x-coordinate; 240: y-coordinate
142
143 UILabel* label4 = new UILabel();
144 uiViewGroup->Add(label4);
145 label4->SetPosition(24, 299, 288, 48); // 24: x-coordinate, 299: y-coordinate, 288: width; 48: height
146 label4->SetText("超出处理");
147 label4->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
148 labelBeyondBtn1_ = SetUpButton("截断 ", 24, 336, uiViewGroup); // 24: x-coordinate; 336: y-coordinate
149 labelBeyondBtn2_ = SetUpButton("省略 ", 116, 336, uiViewGroup); // 116: x-coordinate; 336: y-coordinate
150 labelBeyondBtn3_ = SetUpButton("滚动 ", 208, 336, uiViewGroup); // 208: x-coordinate; 336: y-coordinate
151 positionX_ += 312; // 312: increase y-coordinate
152 }
153
UIKitUILabeTestDisplay002()154 void UITestLabel::UIKitUILabeTestDisplay002()
155 {
156 if (container_ != nullptr) {
157 UIViewGroup* uiViewGroup = new UIViewGroup();
158 uiViewGroup->SetPosition(positionX_, positionY_, 320, 390); // 320: width; 390: height
159 container_->Add(uiViewGroup);
160 UILabel* label = new UILabel();
161 uiViewGroup->Add(label);
162 // 288: width; 48: height
163 label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 288, 48);
164 label->SetText("行数");
165 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
166 labelLineBtn1_ = SetUpButton("单行 ", 24, 48, uiViewGroup); // 24: x-coordinate; 48: y-coordinate
167 labelLineBtn2_ = SetUpButton("多行 ", 116, 48, uiViewGroup); // 116: x-coordinate; 48: y-coordinate
168 UILabel* label2 = new UILabel();
169 uiViewGroup->Add(label2);
170 label2->SetPosition(24, 107, 288, 48); // 24: x-coordinate, 107: y-coordinate, 288: width; 48: height
171 label2->SetText("竖直对齐方式");
172 label2->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
173 labelVerAlignlBtn1_ = SetUpButton("上对齐 ", 24, 144, uiViewGroup); // 24: x-coordinate; 144: y-coordinate
174 labelVerAlignlBtn2_ = SetUpButton("中对齐 ", 116, 144, uiViewGroup); // 116: x-coordinate; 144: y-coordinate
175 labelVerAlignlBtn3_ = SetUpButton("下对齐 ", 208, 144, uiViewGroup); // 208: x-coordinate; 144: y-coordinate
176 UILabel* label3 = new UILabel();
177 uiViewGroup->Add(label3);
178 label3->SetPosition(24, 203, 288, 48); // 24: x-coordinate, 203: y-coordinate, 288: width; 48: height
179 label3->SetText("方向");
180 label3->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
181 labelDirectionBtn1_ = SetUpButton("左往右 ", 24, 240, uiViewGroup); // 24: x-coordinate; 240: y-coordinate
182 labelDirectionBtn2_ = SetUpButton("右往左 ", 116, 240, uiViewGroup); // 116: x-coordinate; 240: y-coordinate
183 UILabel* label4 = new UILabel();
184 uiViewGroup->Add(label4);
185 label4->SetPosition(24, 299, 288, 48); // 24: x-coordinate, 299: y-coordinate, 288: width; 48: height
186 label4->SetText("行高");
187 label4->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
188 labelLineHeightBtn1_ = SetUpButton("行高20 ", 24, 336, uiViewGroup); // 24: x-coordinate; 336: y-coordinate
189 labelLineHeightBtn2_ = SetUpButton("行高35 ", 116, 336, uiViewGroup); // 116: x-coordinate; 336: y-coordinate
190 labelLineHeightBtn3_ = SetUpButton("行高50 ", 208, 336, uiViewGroup); // 208: x-coordinate; 336: y-coordinate
191 positionY_ += 384; // 384: increase x-coordinate
192 positionX_ -= 312; // 312: increase y-coordinate
193 }
194 }
195
UIKitUILabeTestDisplay003()196 void UITestLabel::UIKitUILabeTestDisplay003()
197 {
198 if (container_ != nullptr) {
199 UIViewGroup* uiViewGroup = new UIViewGroup();
200 uiViewGroup->SetPosition(positionX_, positionY_, 450, 110); // 450: width; 110: height
201 container_->Add(uiViewGroup);
202 UILabel* label = new UILabel();
203 uiViewGroup->Add(label);
204 label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, TEXT_DISTANCE_TO_TOP_SIDE, 420, 48); // 420: width; 48: height
205 label->SetText("Label大小");
206 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
207 // 24: x-coordinate; 48: y-coordinate; 120: width
208 labelSizeBtn1_ = SetUpButton("动态宽度 ", 24, 48, uiViewGroup, 120);
209 // 156: x-coordinate; 48: y-coordinate; 120: width
210 labelSizeBtn2_ = SetUpButton("动态高度 ", 156, 48, uiViewGroup, 120);
211 }
212 }
213
OnClick(UIView & view,const ClickEvent & event)214 bool UITestLabel::OnClick(UIView& view, const ClickEvent& event)
215 {
216 uiLabel->Resize(288, 100); // 288: width; 100: height
217 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
218 if (&view == labelFontSizeBtn1_) {
219 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 18); // 18: font size
220 } else if (&view == labelFontSizeBtn2_) {
221 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26); // 26: font size
222 } else if (&view == labelFontSizeBtn3_) {
223 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
224 } else if (&view == labelHorAlignBtn1_) {
225 uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
226 } else if (&view == labelHorAlignBtn2_) {
227 uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
228 } else if (&view == labelHorAlignBtn3_) {
229 uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
230 } else if (&view == labelColorBtn1_) {
231 uiLabel->SetStyle(STYLE_TEXT_COLOR, Color::White().full);
232 uiLabel->SetStyle(STYLE_TEXT_OPA, OPA_OPAQUE);
233 } else if (&view == labelColorBtn2_) {
234 uiLabel->SetStyle(STYLE_TEXT_COLOR, Color::Red().full);
235 uiLabel->SetStyle(STYLE_TEXT_OPA, OPA_OPAQUE);
236 } else if (&view == labelColorBtn3_) {
237 uiLabel->SetStyle(STYLE_TEXT_OPA, 127); // 127: opaque
238 } else if (&view == labelBeyondBtn1_) {
239 uiLabel->Resize(288, 35); // 288: width; 35: height
240 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
241 uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
242 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_CLIP);
243 } else if (&view == labelBeyondBtn2_) {
244 uiLabel->Resize(288, 35); // 288: width; 35: height
245 uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
246 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
247 } else if (&view == labelBeyondBtn3_) {
248 uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
249 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_MARQUEE);
250 } else {
251 ExpandClick(view, event);
252 }
253 return true;
254 }
255
ExpandClick(UIView & view,const ClickEvent & event) const256 void UITestLabel::ExpandClick(UIView& view, const ClickEvent& event) const
257 {
258 if (&view == labelLineBtn1_) {
259 uiLabel->Resize(288, 50); // 288: width; 50: height
260 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30:font size
261 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
262 } else if (&view == labelLineBtn2_) {
263 uiLabel->Resize(288, 100); // 288: width; 100: height
264 uiLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30:font size
265 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS);
266 } else if (&view == labelVerAlignlBtn1_) {
267 uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_TOP);
268 } else if (&view == labelVerAlignlBtn2_) {
269 uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
270 } else if (&view == labelVerAlignlBtn3_) {
271 uiLabel->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT, UITextLanguageAlignment::TEXT_ALIGNMENT_BOTTOM);
272 } else if (&view == labelDirectionBtn1_) {
273 uiLabel->SetDirect(TEXT_DIRECT_LTR);
274 } else if (&view == labelDirectionBtn2_) {
275 uiLabel->SetDirect(TEXT_DIRECT_RTL);
276 } else if (&view == labelSizeBtn1_) {
277 uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
278 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
279 } else if (&view == labelSizeBtn2_) {
280 uiLabel->SetText("Test of 图形子系统,Test of 图形子系统 ");
281 uiLabel->SetLineBreakMode(UILabel::LINE_BREAK_WRAP);
282 } else if (&view == labelLineHeightBtn1_) {
283 uiLabel->SetStyle(STYLE_LINE_HEIGHT, 20); // 20: lineHeight
284 } else if (&view == labelLineHeightBtn2_) {
285 uiLabel->SetStyle(STYLE_LINE_HEIGHT, 35); // 35: lineHeight
286 } else if (&view == labelLineHeightBtn3_) {
287 uiLabel->SetStyle(STYLE_LINE_HEIGHT, 50); // 50: lineHeight
288 }
289 }
290 } // namespace OHOS
291