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_draw_line.h"
17 #include "common/screen.h"
18 #include "components/ui_canvas.h"
19 #include "components/ui_label.h"
20 #include "components/ui_label_button.h"
21 
22 namespace OHOS {
23 namespace {
24 const uint16_t DELTA_X = 24;
25 }
26 
SetUp()27 void UITestDrawLine::SetUp()
28 {
29     if (container_ == nullptr) {
30         container_ = new UIScrollView();
31         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
32         container_->SetHorizontalScrollState(false);
33     }
34 }
35 
TearDown()36 void UITestDrawLine::TearDown()
37 {
38     DeleteChildren(container_);
39     container_ = nullptr;
40 }
41 
GetTestView()42 const UIView* UITestDrawLine::GetTestView()
43 {
44     UIKitDrawLineTestLine001();
45     UIKitDrawLineTestLine002();
46     UIKitDrawLineTestLine003();
47     UIKitDrawLineTestLine004();
48     UIKitDrawLineTestLine005();
49     UIKitDrawLineTestLine006();
50     UIKitDrawLineTestLine007();
51     UIKitDrawLineTestLine008();
52     UIKitDrawLineTestLine009();
53     UIKitDrawLineTestLine010();
54     UIKitDrawLineTestLine011();
55     UIKitDrawLineTestLine012();
56     return container_;
57 }
58 
CreateTestCaseGroup() const59 UIViewGroup* UITestDrawLine::CreateTestCaseGroup() const
60 {
61     UIViewGroup* group = new UIViewGroup();
62     // 288: group width; 250: group height
63     group->Resize(288, 250);
64     return group;
65 }
66 
CreateTitleLabel() const67 UILabel* UITestDrawLine::CreateTitleLabel() const
68 {
69     UILabel* label = new UILabel();
70     // 2: half of screen width;
71     label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, Screen::GetInstance().GetWidth() / 2,
72                        TITLE_LABEL_DEFAULT_HEIGHT);
73     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
74     return label;
75 }
76 
CreateCanvas() const77 UICanvas* UITestDrawLine::CreateCanvas() const
78 {
79     UICanvas* canvas = new UICanvas();
80     canvas->SetHeight(200); // 200 : height size
81     canvas->SetWidth(200);  // 200 : width size
82     canvas->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, VIEW_DISTANCE_TO_TOP_SIDE);
83     canvas->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
84     return canvas;
85 }
86 
UIKitDrawLineTestLine001()87 void UITestDrawLine::UIKitDrawLineTestLine001()
88 {
89     if (container_ == nullptr) {
90         return;
91     }
92 
93     UIViewGroup* group = CreateTestCaseGroup();
94     group->SetViewId("UIKit_Draw_Line_Test_Line_001");
95     group->SetPosition(0, 0);
96 
97     UILabel* label = CreateTitleLabel();
98     group->Add(label);
99     label->SetText("水平直线绘制 ");
100 
101     UICanvas* canvas = CreateCanvas();
102     Paint paint;
103     canvas->DrawLine({50, 50}, {150, 50}, paint); // 50 : start x 50 : start y 150 : end x 50 : end y
104     group->Add(canvas);
105 
106     container_->Add(group);
107 }
108 
UIKitDrawLineTestLine002()109 void UITestDrawLine::UIKitDrawLineTestLine002()
110 {
111     if (container_ == nullptr) {
112         return;
113     }
114 
115     UIViewGroup* group = CreateTestCaseGroup();
116     group->SetViewId("UIKit_Draw_Line_Test_Line_002");
117     group->SetPosition(0, 0);
118 
119     UILabel* label = CreateTitleLabel();
120     group->Add(label);
121     label->SetText("垂直直线绘制 ");
122 
123     UICanvas* canvas = CreateCanvas();
124     Paint paint;
125     canvas->DrawLine({100, 50}, {100, 150}, paint); // 100 : start x 50 : start y 100 : end x 150 : end y
126     group->Add(canvas);
127 
128     container_->Add(group);
129     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_001", DELTA_X);
130 }
131 
UIKitDrawLineTestLine003()132 void UITestDrawLine::UIKitDrawLineTestLine003()
133 {
134     if (container_ == nullptr) {
135         return;
136     }
137 
138     UIViewGroup* group = CreateTestCaseGroup();
139     group->SetViewId("UIKit_Draw_Line_Test_Line_003");
140     group->SetPosition(0, 0);
141 
142     UILabel* label = CreateTitleLabel();
143     group->Add(label);
144     label->SetText("左下-右上方向直线绘制 ");
145 
146     UICanvas* canvas = CreateCanvas();
147     Paint paint;
148     canvas->DrawLine({20, 180}, {160, 20}, paint); // 20 : start x 180 : start y 160 : end x 20 : end y
149     canvas->DrawLine({20, 180}, {180, 20}, paint); // 20 : start x 180 : start y 180 : end x 20 : end y
150     canvas->DrawLine({20, 180}, {180, 40}, paint); // 20 : start x 180 : start y 180 : end x 40 : end y
151     group->Add(canvas);
152 
153     container_->Add(group);
154     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_002", DELTA_X);
155 }
156 
UIKitDrawLineTestLine004()157 void UITestDrawLine::UIKitDrawLineTestLine004()
158 {
159     if (container_ == nullptr) {
160         return;
161     }
162 
163     UIViewGroup* group = CreateTestCaseGroup();
164     group->SetViewId("UIKit_Draw_Line_Test_Line_004");
165     group->SetPosition(0, 0);
166 
167     UILabel* label = CreateTitleLabel();
168     group->Add(label);
169     label->SetText("左上-右下方向直线绘制 ");
170 
171     UICanvas* canvas = CreateCanvas();
172     Paint paint;
173     canvas->DrawLine({20, 20}, {160, 180}, paint); // 20 : start x 20 : start y 160 : end x 180 : end y
174     canvas->DrawLine({20, 20}, {180, 180}, paint); // 20 : start x 20 : start y 180 : end x 180 : end y
175     canvas->DrawLine({20, 20}, {180, 160}, paint); // 20 : start x 20 : start y 180 : end x 160 : end y
176     group->Add(canvas);
177 
178     container_->Add(group);
179     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_001", 10); // 10 : offset size
180 }
181 
UIKitDrawLineTestLine005()182 void UITestDrawLine::UIKitDrawLineTestLine005()
183 {
184     if (container_ == nullptr) {
185         return;
186     }
187 
188     UIViewGroup* group = CreateTestCaseGroup();
189     group->SetViewId("UIKit_Draw_Line_Test_Line_005");
190     group->SetPosition(0, 0);
191 
192     UILabel* label = CreateTitleLabel();
193     group->Add(label);
194     label->SetText("右上-左下方向直线绘制 ");
195 
196     UICanvas* canvas = CreateCanvas();
197     Paint paint;
198     canvas->DrawLine({180, 20}, {20, 160}, paint); // 180 : start x 20 : start y 20 : end x 160 : end y
199     canvas->DrawLine({180, 20}, {20, 180}, paint); // 180 : start x 20 : start y 20 : end x 180 : end y
200     canvas->DrawLine({180, 20}, {40, 180}, paint); // 180 : start x 20 : start y 40 : end x 180 : end y
201     group->Add(canvas);
202 
203     container_->Add(group);
204     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_002", 10); // 10: offset
205     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_004", DELTA_X);
206 }
207 
UIKitDrawLineTestLine006()208 void UITestDrawLine::UIKitDrawLineTestLine006()
209 {
210     if (container_ == nullptr) {
211         return;
212     }
213 
214     UIViewGroup* group = CreateTestCaseGroup();
215     group->SetViewId("UIKit_Draw_Line_Test_Line_006");
216     group->SetPosition(0, 0);
217 
218     UILabel* label = CreateTitleLabel();
219     group->Add(label);
220     label->SetText("左上-右下方向直线绘制 ");
221 
222     UICanvas* canvas = CreateCanvas();
223     Paint paint;
224     canvas->DrawLine({180, 180}, {20, 40}, paint); // 180 : start x 180 : start y 20 : end x 40 : end y
225     canvas->DrawLine({180, 180}, {20, 20}, paint); // 180 : start x 180 : start y 20 : end x 20 : end y
226     canvas->DrawLine({180, 180}, {40, 20}, paint); // 180 : start x 180 : start y 40 : end x 20 : end y
227     group->Add(canvas);
228 
229     container_->Add(group);
230     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_003", 10); // 10: offset
231     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_005", DELTA_X);
232 }
233 
UIKitDrawLineTestLine007()234 void UITestDrawLine::UIKitDrawLineTestLine007()
235 {
236     if (container_ == nullptr) {
237         return;
238     }
239 
240     UIViewGroup* group = CreateTestCaseGroup();
241     group->SetViewId("UIKit_Draw_Line_Test_Line_007");
242     group->SetPosition(0, 0);
243 
244     UILabel* label = CreateTitleLabel();
245     group->Add(label);
246     label->SetText("宽度为零直线绘制 ");
247 
248     UICanvas* canvas = CreateCanvas();
249     Paint paint;
250     paint.SetStrokeWidth(0);                       // 0 : stroke width
251     canvas->DrawLine({10, 190}, {190, 10}, paint); // 10 : start x 190 : start y 190 : end x 10 : end y
252     group->Add(canvas);
253 
254     container_->Add(group);
255     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_004", 10); // 10 : offset size
256 }
257 
UIKitDrawLineTestLine008()258 void UITestDrawLine::UIKitDrawLineTestLine008()
259 {
260     if (container_ == nullptr) {
261         return;
262     }
263 
264     UIViewGroup* group = CreateTestCaseGroup();
265     group->SetViewId("UIKit_Draw_Line_Test_Line_008");
266     group->SetPosition(0, 0);
267 
268     UILabel* label = CreateTitleLabel();
269     group->Add(label);
270     label->SetText("不同宽度的直线绘制 ");
271 
272     UICanvas* canvas = CreateCanvas();
273     Paint paint;
274     paint.SetStrokeWidth(2);                       // 2 : stroke width
275     canvas->DrawLine({10, 190}, {190, 10}, paint); // 10 : start x 190 : start y 190 : end x 10 : end y
276     Paint paint2;
277     paint2.SetStrokeWidth(4);                       // 4 : stroke width
278     canvas->DrawLine({30, 190}, {190, 30}, paint2); // 30 : start x 190 : start y 190 : end x 30 : end y
279     Paint paint3;
280     paint3.SetStrokeWidth(5);                       // 5 : stroke width
281     canvas->DrawLine({50, 190}, {190, 50}, paint3); // 50 : start x 190 : start y 190 : end x 50 : end y
282     Paint paint4;
283     paint4.SetStrokeWidth(90);                    // 90 : stroke width
284     canvas->DrawLine({40, 90}, {90, 40}, paint4); // 40 : start x 90 : start y 90 : end x 40 : end y
285     group->Add(canvas);
286     container_->Add(group);
287     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_005", 10); // 10: offset
288     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_007", DELTA_X);
289 }
290 
UIKitDrawLineTestLine009()291 void UITestDrawLine::UIKitDrawLineTestLine009()
292 {
293     if (container_ == nullptr) {
294         return;
295     }
296 
297     UIViewGroup* group = CreateTestCaseGroup();
298     group->SetViewId("UIKit_Draw_Line_Test_Line_009");
299     group->SetPosition(0, 0);
300 
301     UILabel* label = CreateTitleLabel();
302     group->Add(label);
303     label->SetText("全透明的直线绘制 ");
304 
305     UICanvas* canvas = CreateCanvas();
306     Paint paint;
307     paint.SetOpacity(OPA_TRANSPARENT);
308     canvas->DrawLine({10, 190}, {190, 10}, paint); // 10 : start x 190 : start y 190 : end x 10 : end y
309     group->Add(canvas);
310     container_->Add(group);
311     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_006", 10); // 10: offset
312     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_008", DELTA_X);
313 }
314 
UIKitDrawLineTestLine010()315 void UITestDrawLine::UIKitDrawLineTestLine010()
316 {
317     if (container_ == nullptr) {
318         return;
319     }
320 
321     UIViewGroup* group = CreateTestCaseGroup();
322     group->SetViewId("UIKit_Draw_Line_Test_Line_010");
323     group->SetPosition(0, 0);
324 
325     UILabel* label = CreateTitleLabel();
326     group->Add(label);
327     label->SetText("不透明的直线绘制 ");
328 
329     UICanvas* canvas = CreateCanvas();
330     Paint paint;
331     paint.SetOpacity(OPA_OPAQUE);
332     canvas->DrawLine({10, 190}, {190, 10}, paint); // 10 : start x 190 : start y 190 : end x 10 : end y
333     group->Add(canvas);
334 
335     container_->Add(group);
336     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_007", 10); // 10 : offset size
337 }
338 
UIKitDrawLineTestLine011()339 void UITestDrawLine::UIKitDrawLineTestLine011()
340 {
341     if (container_ == nullptr) {
342         return;
343     }
344 
345     UIViewGroup* group = CreateTestCaseGroup();
346     group->SetViewId("UIKit_Draw_Line_Test_Line_011");
347     group->SetPosition(0, 0);
348 
349     UILabel* label = CreateTitleLabel();
350     group->Add(label);
351     label->SetText("不同透明度的直线绘制 ");
352 
353     UICanvas* canvas = CreateCanvas();
354     Paint paint;
355     paint.SetOpacity(51);                          // 51 : opacity size
356     canvas->DrawLine({10, 190}, {190, 10}, paint); // 10 : start x 190 : start y 190 : end x 10 : end y
357     Paint paint2;
358     paint2.SetOpacity(127);                         // 127 : opacity size
359     canvas->DrawLine({30, 190}, {190, 30}, paint2); // 30 : start x 190 : start y 190 : end x 30 : end y
360     Paint paint3;
361     paint3.SetOpacity(178);                         // 178 : opacity size
362     canvas->DrawLine({50, 190}, {190, 50}, paint3); // 50 : start x 190 : start y 190 : end x 50 : end y
363     group->Add(canvas);
364     container_->Add(group);
365     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_008", 10); // 10 : offset size
366     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_010", DELTA_X);
367 }
368 
UIKitDrawLineTestLine012()369 void UITestDrawLine::UIKitDrawLineTestLine012()
370 {
371     if (container_ == nullptr) {
372         return;
373     }
374 
375     UIViewGroup* group = CreateTestCaseGroup();
376     group->SetViewId("UIKit_Draw_Line_Test_Line_012");
377     group->SetPosition(0, 0);
378 
379     UILabel* label = CreateTitleLabel();
380     group->Add(label);
381     label->SetText("不同颜色的直线绘制 ");
382 
383     UICanvas* canvas = CreateCanvas();
384     Paint paint;
385     paint.SetStrokeColor(Color::Red());
386     canvas->DrawLine({10, 190}, {190, 10}, paint); // 10 : start x 190 : start y 190 : end x 10 : end y
387     Paint paint2;
388     paint2.SetStrokeColor(Color::Yellow());
389     canvas->DrawLine({30, 190}, {190, 30}, paint2); // 30 : start x 190 : start y 190 : end x 30 : end y
390     Paint paint3;
391     paint3.SetStrokeColor(Color::Green());
392     canvas->DrawLine({50, 190}, {190, 50}, paint3); // 50 : start x 190 : start y 190 : end x 50 : end y
393     group->Add(canvas);
394     container_->Add(group);
395     group->LayoutBottomToSibling("UIKit_Draw_Line_Test_Line_009", 10); // 10 : offset size
396     group->LayoutRightToSibling("UIKit_Draw_Line_Test_Line_011", DELTA_X);
397 }
398 } // namespace OHOS