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_arc_label.h"
17 #include "common/screen.h"
18 #include "font/ui_font.h"
19 
20 #define COMPATIBILITY_MODE
21 namespace OHOS {
22 namespace {
23 const int16_t GAP = 5;
24 const int16_t TITLE_HEIGHT = 29;
25 const uint16_t RADIUS = 150;
26 #ifdef COMPATIBILITY_MODE
27 const uint16_t CENTER_X = 185;
28 const uint16_t CENTER_Y = 155;
29 #else
30 const uint16_t CENTER_X = 200;
31 const uint16_t CENTER_Y = 200;
32 #endif
33 const uint16_t LABEL_WIDTH = 400;
34 const uint16_t LABEL_HEIGHT = 400;
35 const uint16_t LABEL_WIDTH_500 = 500;
36 const uint16_t LABEL_HEIGHT_500 = 500;
37 const uint16_t ANIMATOR_LOOPCOUNT = 2;
38 const uint16_t ANIMATOR_SPEED = 15;
39 const std::string DEFAULT_ARCLABEL_TEXT = "考虑线段在框内, 框外和部分在框内";
40 } // namespace
41 
SetUp()42 void UITestArcLabel::SetUp()
43 {
44     if (container_ == nullptr) {
45         container_ = new UIScrollView();
46         container_->SetThrowDrag(true);
47         container_->SetHorizontalScrollState(false);
48         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
49         positionX_ = 0;
50         positionY_ = 0;
51     }
52 }
53 
TearDown()54 void UITestArcLabel::TearDown()
55 {
56     DeleteChildren(container_);
57     container_ = nullptr;
58 }
59 
InnerTestTitle(const char * title,int16_t x,int16_t y) const60 void UITestArcLabel::InnerTestTitle(const char* title, int16_t x, int16_t y) const
61 {
62     UILabel* titleLabel = new UILabel();
63     titleLabel->SetPosition(x, y, Screen::GetInstance().GetWidth(), TITLE_HEIGHT);
64     titleLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
65     titleLabel->SetText(title);
66     container_->Add(titleLabel);
67 }
68 
GetTestView()69 const UIView* UITestArcLabel::GetTestView()
70 {
71 #ifdef COMPATIBILITY_MODE
72     UIKitUIArcLabelTestDisplay001();
73     UIKitUIArcLabelTestDisplay002();
74     UIKitUIArcLabelTestDisplay003();
75     UIKitUIArcLabelTestDisplay004();
76 
77     UIKitUIArcLabelTestAlign001();
78     UIKitUIArcLabelTestAlign002();
79     UIKitUIArcLabelTestAlign003();
80 #else
81     UIKitUIArcLabelTestIncompatible001();
82     UIKitUIArcLabelTestIncompatible002();
83     UIKitUIArcLabelTestIncompatible003();
84     UIKitUIArcLabelTestIncompatible004();
85 #endif
86     return container_;
87 }
88 
UIKitUIArcLabelTestDisplay001()89 void UITestArcLabel::UIKitUIArcLabelTestDisplay001()
90 {
91     const char* title = "显示内部角度从0到270的弧形文本 ";
92     // 0: start angle  270: end angle
93     TestArcLabelDisplay(title, 0, 270, TextOrientation::INSIDE, VIEW_DISTANCE_TO_LEFT_SIDE,
94                         VIEW_DISTANCE_TO_TOP_SIDE);
95 }
96 
UIKitUIArcLabelTestDisplay002()97 void UITestArcLabel::UIKitUIArcLabelTestDisplay002()
98 {
99     const char* title = "显示内部角度从0到-270的弧形文本 ";
100     // 0: start angle  -270: end angle  100: y-coordinate
101     TestArcLabelDisplay(title, 0, -270, TextOrientation::INSIDE, VIEW_DISTANCE_TO_LEFT_SIDE, 100);
102 }
103 
UIKitUIArcLabelTestDisplay003()104 void UITestArcLabel::UIKitUIArcLabelTestDisplay003()
105 {
106     const char* title = "显示外部角度从0到270的弧形文本 ";
107     // 0: start angle  270: end angle  200: y-coordinate
108     TestArcLabelDisplay(title, 0, 270, TextOrientation::OUTSIDE, VIEW_DISTANCE_TO_LEFT_SIDE, 200);
109 }
110 
UIKitUIArcLabelTestDisplay004()111 void UITestArcLabel::UIKitUIArcLabelTestDisplay004()
112 {
113     const char* title = "显示外部角度从0到-270的弧形文本 ";
114     // 0: start angle  -270: end angle  300: y-coordinate
115     TestArcLabelDisplay(title, 0, -270, TextOrientation::OUTSIDE, VIEW_DISTANCE_TO_LEFT_SIDE, 300);
116 }
117 
TestArcLabelDisplay(const char * title,const int16_t startAngle,const int16_t endAngle,const TextOrientation orientation,int16_t x,int16_t y)118 void UITestArcLabel::TestArcLabelDisplay(const char* title,
119                                          const int16_t startAngle,
120                                          const int16_t endAngle,
121                                          const TextOrientation orientation,
122                                          int16_t x,
123                                          int16_t y)
124 {
125     if (container_ != nullptr) {
126         InnerTestTitle(title, x, positionY_);
127         UIArcLabel* label = new UIArcLabel();
128         label->SetArcTextCenter(CENTER_X, CENTER_Y + positionY_ + GAP * 4); // 4: 4 times GAP
129         label->SetArcTextRadius(RADIUS);
130         label->SetArcTextAngle(startAngle, endAngle);
131         label->SetArcTextOrientation(orientation);
132         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
133         label->SetText("012345678ABCDEF0123456789ABCDE");
134         label->SetStyle(STYLE_LETTER_SPACE, 10); // 10: space
135         container_->Add(label);
136         positionY_ += (RADIUS * 2) + GAP + TITLE_HEIGHT; // 2: diameter
137     }
138 }
139 
140 class LabelButtonClickListener : public OHOS::UIView::OnClickListener {
141 public:
LabelButtonClickListener(UIView * view)142     explicit LabelButtonClickListener(UIView* view) : view_(view)
143     {
144     }
145 
OnClick(UIView & view,const ClickEvent & event)146     bool OnClick(UIView& view, const ClickEvent& event) override
147     {
148         UIArcLabel* label = static_cast<UIArcLabel*>(view_);
149         if (label == nullptr) {
150             return false;
151         }
152 
153         label->SetRollCount(ANIMATOR_LOOPCOUNT);
154         label->SetRollSpeed(ANIMATOR_SPEED);
155         label->Start();
156     }
157 private:
158     UIView* view_;
159 };
160 
161 class TestArcLabelScrollListener : public ArcLabelScrollListener {
162 public:
TestArcLabelScrollListener(UIView * view)163     explicit TestArcLabelScrollListener(UIView* view) : view_(view)
164     {
165     }
166 
Finish()167     void Finish() override
168     {
169         UILabelButton* button = static_cast<UILabelButton*>(view_);
170         if (button == nullptr) {
171             return;
172         }
173 
174         button->SetText("旋转动画结束!!!");
175     }
176 
177 private:
178     UIView* view_;
179 };
180 
GetTestUIButton(const char * buttonText,int16_t x,int16_t y,const char * id)181 UILabelButton* UITestArcLabel::GetTestUIButton(const char* buttonText, int16_t x, int16_t y, const char* id)
182 {
183     if (buttonText == nullptr) {
184         return nullptr;
185     }
186 
187     UILabelButton* labelButton = new UILabelButton();
188     labelButton->SetPosition(x, y);
189     labelButton->Resize(LABEL_WIDTH, BUTTON_HEIGHT1);
190     labelButton->SetText(buttonText);
191     labelButton->SetViewId(id);
192     labelButton->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
193     labelButton->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
194     labelButton->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
195     labelButton->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
196     labelButton->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
197     labelButton->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
198     return labelButton;
199 }
200 
UIKitUIArcLabelTestIncompatible001()201 void UITestArcLabel::UIKitUIArcLabelTestIncompatible001()
202 {
203     if (container_ == nullptr) {
204         return;
205     }
206 
207     positionX_ = 0;
208     positionY_ = 0;
209     UILabelButton* button = GetTestUIButton("0-90点击旋转", positionX_, positionY_, nullptr);
210     if (button == nullptr) {
211         return;
212     }
213     UIArcLabel* label = new UIArcLabel();
214     if (label == nullptr) {
215         return;
216     }
217     label->SetText(DEFAULT_ARCLABEL_TEXT.c_str());
218     label->SetArcTextCenter(CENTER_X, CENTER_Y + TITLE_HEIGHT);
219     label->SetArcTextRadius(RADIUS);
220     label->SetCompatibilityMode(false);
221     label->SetArcTextAngle(0, 90);                      // 0: start angle, 90: end angle.
222     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);   // 30: font size
223     label->SetPosition(0, BUTTON_HEIGHT1, LABEL_WIDTH, LABEL_HEIGHT);
224     label->RegisterScrollListener(new TestArcLabelScrollListener(button));
225     button->SetOnClickListener(new LabelButtonClickListener(label));
226 
227     container_->Add(button);
228     container_->Add(label);
229 }
230 
UIKitUIArcLabelTestIncompatible002()231 void UITestArcLabel::UIKitUIArcLabelTestIncompatible002()
232 {
233     if (container_ == nullptr) {
234         return;
235     }
236 
237     positionX_ = LABEL_WIDTH_500;
238     positionY_ = 0;
239     UILabelButton* button = GetTestUIButton("90-0点击旋转", positionX_, positionY_, nullptr);
240     if (button == nullptr) {
241         return;
242     }
243     UIArcLabel* label = new UIArcLabel();
244     if (label == nullptr) {
245         return;
246     }
247     label->SetText(DEFAULT_ARCLABEL_TEXT.c_str());
248     label->SetArcTextCenter(CENTER_X + LABEL_WIDTH_500, CENTER_Y + TITLE_HEIGHT);
249     label->SetArcTextRadius(RADIUS);
250     label->SetCompatibilityMode(false);
251     label->SetArcTextAngle(90, 0);                      // 90: start angle, 0: end angle.
252     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);   // 30: font size
253     label->SetPosition(LABEL_WIDTH_500, BUTTON_HEIGHT1, LABEL_WIDTH, LABEL_HEIGHT);
254     label->RegisterScrollListener(new TestArcLabelScrollListener(button));
255     button->SetOnClickListener(new LabelButtonClickListener(label));
256 
257     container_->Add(button);
258     container_->Add(label);
259 }
260 
UIKitUIArcLabelTestIncompatible003()261 void UITestArcLabel::UIKitUIArcLabelTestIncompatible003()
262 {
263     if (container_ == nullptr) {
264         return;
265     }
266 
267     positionX_ = 0;
268     positionY_ = LABEL_HEIGHT_500 - BUTTON_HEIGHT1;
269     UILabelButton* button = GetTestUIButton("30-260点击旋转", positionX_, positionY_, nullptr);
270     if (button == nullptr) {
271         return;
272     }
273     UIArcLabel* label = new UIArcLabel();
274     if (label == nullptr) {
275         return;
276     }
277     label->SetText(DEFAULT_ARCLABEL_TEXT.c_str());
278     label->SetArcTextCenter(CENTER_X, CENTER_Y + LABEL_HEIGHT_500);
279     label->SetArcTextRadius(RADIUS);
280     label->SetCompatibilityMode(false);
281     label->SetArcTextAngle(30, 260);                      // 30: start angle, 260: end angle.
282     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);   // 30: font size
283     label->SetPosition(0, LABEL_HEIGHT_500, LABEL_WIDTH, LABEL_HEIGHT);
284     label->RegisterScrollListener(new TestArcLabelScrollListener(button));
285     button->SetOnClickListener(new LabelButtonClickListener(label));
286 
287     container_->Add(button);
288     container_->Add(label);
289 }
290 
UIKitUIArcLabelTestIncompatible004()291 void UITestArcLabel::UIKitUIArcLabelTestIncompatible004()
292 {
293     if (container_ == nullptr) {
294         return;
295     }
296 
297     positionX_ = LABEL_WIDTH_500;
298     positionY_ = LABEL_HEIGHT_500 - BUTTON_HEIGHT1;
299     UILabelButton* button = GetTestUIButton("260-30点击旋转", positionX_, positionY_, nullptr);
300     if (button == nullptr) {
301         return;
302     }
303     UIArcLabel* label = new UIArcLabel();
304     if (label == nullptr) {
305         return;
306     }
307     label->SetText(DEFAULT_ARCLABEL_TEXT.c_str());
308     label->SetArcTextCenter(CENTER_X + LABEL_WIDTH_500, CENTER_Y + LABEL_HEIGHT_500);
309     label->SetArcTextRadius(RADIUS);
310     label->SetCompatibilityMode(false);
311     label->SetArcTextAngle(260, 30);                      // 260: start angle, 30: end angle.
312     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);   // 30: font size
313     label->SetPosition(LABEL_WIDTH_500, LABEL_HEIGHT_500, LABEL_WIDTH, LABEL_HEIGHT);
314     label->RegisterScrollListener(new TestArcLabelScrollListener(button));
315     button->SetOnClickListener(new LabelButtonClickListener(label));
316 
317     container_->Add(button);
318     container_->Add(label);
319 }
320 
UIKitUIArcLabelTestAlign001()321 void UITestArcLabel::UIKitUIArcLabelTestAlign001()
322 {
323     if (container_ != nullptr) {
324         // 2: half of screen width
325         positionX_ = Screen::GetInstance().GetWidth() / 2 + VIEW_DISTANCE_TO_LEFT_SIDE;
326         positionY_ = 0;
327         InnerTestTitle("弧形文本在0到90度之间左对齐 ", positionX_, positionY_);
328         positionY_ += TITLE_HEIGHT + GAP;
329         UIView* back = new UIView();
330         back->SetPosition(positionX_, positionY_ + GAP);
331         back->Resize(RADIUS, RADIUS);
332         back->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
333         back->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
334 
335         UIArcLabel* label = new UIArcLabel();
336         label->SetArcTextCenter(positionX_, CENTER_Y + positionY_);
337         label->SetArcTextRadius(RADIUS);
338         label->SetArcTextAngle(0, 90);                    // 0: start angle, 90: end angle
339         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
340         label->SetText("LEFT");
341         label->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_LEFT);
342         label->SetStyle(STYLE_LETTER_SPACE, 6); // 6: space
343 
344         container_->Add(back);
345         container_->Add(label);
346         positionY_ += RADIUS + GAP + TITLE_HEIGHT;
347     }
348 }
349 
UIKitUIArcLabelTestAlign002()350 void UITestArcLabel::UIKitUIArcLabelTestAlign002()
351 {
352     if (container_ != nullptr) {
353         InnerTestTitle("弧形文本在0到90度之间居中对齐 ", positionX_, positionY_);
354         positionY_ += TITLE_HEIGHT + GAP;
355         UIView* back = new UIView();
356         back->SetPosition(positionX_, positionY_ + GAP);
357         back->Resize(RADIUS, RADIUS);
358         back->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
359         back->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
360 
361         UIArcLabel* label = new UIArcLabel();
362         label->SetArcTextCenter(positionX_, CENTER_Y + positionY_);
363         label->SetArcTextRadius(RADIUS);
364         label->SetArcTextAngle(0, 90);                    // 0: start angle, 90: end angle
365         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
366         label->SetText("CENTER");
367         label->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_CENTER);
368         label->SetStyle(STYLE_LETTER_SPACE, 6); // 6: space
369 
370         container_->Add(back);
371         container_->Add(label);
372         positionY_ += RADIUS + GAP + TITLE_HEIGHT;
373     }
374 }
375 
UIKitUIArcLabelTestAlign003()376 void UITestArcLabel::UIKitUIArcLabelTestAlign003()
377 {
378     if (container_ != nullptr) {
379         InnerTestTitle("弧形文本在0到90度之间右对齐 ", positionX_, positionY_);
380         positionY_ += TITLE_HEIGHT + GAP;
381         UIView* back = new UIView();
382         back->SetPosition(positionX_, positionY_ + GAP);
383         back->Resize(RADIUS, RADIUS);
384         back->SetStyle(STYLE_BACKGROUND_COLOR, Color::Gray().full);
385         back->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
386 
387         UIArcLabel* label = new UIArcLabel();
388         label->SetArcTextCenter(positionX_, CENTER_Y + positionY_);
389         label->SetArcTextRadius(RADIUS);
390         label->SetArcTextAngle(0, 90);                    // 0: start angle, 90: end angle
391         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30); // 30: font size
392         label->SetText("RIGHT");
393         label->SetAlign(UITextLanguageAlignment::TEXT_ALIGNMENT_RIGHT);
394         label->SetStyle(STYLE_LETTER_SPACE, 6); // 6: space
395 
396         container_->Add(back);
397         container_->Add(label);
398         positionY_ += RADIUS + GAP + TITLE_HEIGHT;
399     }
400 }
401 } // namespace OHOS
402