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