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_analog_clock.h"
17 
18 #include "common/screen.h"
19 #include "components/ui_analog_clock.h"
20 #include "components/ui_image_view.h"
21 #include "components/ui_label.h"
22 #include "test_resource_config.h"
23 
24 namespace OHOS {
25 namespace {
26 const uint8_t BUTTON_TEXT_SIZE = 16;
27 const uint8_t BUTTON_WIDTH = 120;
28 const uint8_t BUTTON_HEIGHT = 40;
29 } // namespace
30 
SetUp()31 void UITestAnalogClock::SetUp()
32 {
33     if (container_ == nullptr) {
34         container_ = new UIScrollView();
35         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
36     }
37 }
38 
TearDown()39 void UITestAnalogClock::TearDown()
40 {
41     TearDown001();
42     if (changeModeListener_ != nullptr) {
43         delete changeModeListener_;
44         changeModeListener_ = nullptr;
45     }
46     if (clickMoveLeftListener_ != nullptr) {
47         delete clickMoveLeftListener_;
48         clickMoveLeftListener_ = nullptr;
49     }
50     if (clickMoveRightListener_ != nullptr) {
51         delete clickMoveRightListener_;
52         clickMoveRightListener_ = nullptr;
53     }
54     if (clickMoveTopListener_ != nullptr) {
55         delete clickMoveTopListener_;
56         clickMoveTopListener_ = nullptr;
57     }
58     if (clickMoveBottomListener_ != nullptr) {
59         delete clickMoveBottomListener_;
60         clickMoveBottomListener_ = nullptr;
61     }
62     if (changeModeListener1_ != nullptr) {
63         delete changeModeListener1_;
64         changeModeListener1_ = nullptr;
65     }
66     if (clickMoveLeftListener1_ != nullptr) {
67         delete clickMoveLeftListener1_;
68         clickMoveLeftListener1_ = nullptr;
69     }
70     if (clickMoveRightListener1_ != nullptr) {
71         delete clickMoveRightListener1_;
72         clickMoveRightListener1_ = nullptr;
73     }
74     if (clickMoveTopListener1_ != nullptr) {
75         delete clickMoveTopListener1_;
76         clickMoveTopListener1_ = nullptr;
77     }
78     if (clickMoveBottomListener1_ != nullptr) {
79         delete clickMoveBottomListener1_;
80         clickMoveBottomListener1_ = nullptr;
81     }
82     DeleteChildren(container_);
83     container_ = nullptr;
84 }
85 
TearDown001()86 void UITestAnalogClock::TearDown001()
87 {
88     if (animator_ != nullptr) {
89         delete animator_;
90         delete callback_;
91         animator_ = nullptr;
92         callback_ = nullptr;
93     }
94 
95     if (animator2_ != nullptr) {
96         delete animator2_;
97         delete callback2_;
98         animator2_ = nullptr;
99         callback2_ = nullptr;
100     }
101 }
102 
GetTestView()103 const UIView* UITestAnalogClock::GetTestView()
104 {
105     UIKitTestLineHandAnalogClock001();
106     UIKitTestImageHandAnalogClock002();
107     return container_;
108 }
109 
110 class TestBtnOnClickChangeModeListener : public UIView::OnClickListener {
111 public:
TestBtnOnClickChangeModeListener(UIView * uiView,UIAnalogClock * clock)112     TestBtnOnClickChangeModeListener(UIView* uiView, UIAnalogClock* clock) : uiView_(uiView), clock_(clock) {}
~TestBtnOnClickChangeModeListener()113     ~TestBtnOnClickChangeModeListener() {}
OnClick(UIView & view,const ClickEvent & event)114     bool OnClick(UIView& view, const ClickEvent& event) override
115     {
116         if (clock_->GetWorkMode() == UIAbstractClock::ALWAYS_ON) {
117             clock_->SetWorkMode(UIAbstractClock::NORMAL);
118         } else {
119             clock_->SetWorkMode(UIAbstractClock::ALWAYS_ON);
120         }
121         return true;
122     }
123 
124 private:
125     UIView* uiView_;
126     UIAnalogClock* clock_;
127 };
128 
129 class TestBtnOnClickMovePositionListener : public UIView::OnClickListener {
130 public:
131     enum class MoveType {
132         MOVE_LEFT,
133         MOVE_RIGHT,
134         MOVE_TOP,
135         MOVE_BOTTOM,
136     };
TestBtnOnClickMovePositionListener(UIView * uiView,UIAnalogClock * clock,MoveType type,uint16_t grid)137     TestBtnOnClickMovePositionListener(UIView* uiView, UIAnalogClock* clock, MoveType type, uint16_t grid)
138         : uiView_(uiView), clock_(clock), type_(type), grid_(grid)
139     {
140     }
~TestBtnOnClickMovePositionListener()141     ~TestBtnOnClickMovePositionListener() {}
OnClick(UIView & view,const ClickEvent & event)142     bool OnClick(UIView& view, const ClickEvent& event) override
143     {
144         int16_t xPos = clock_->GetX();
145         int16_t yPos = clock_->GetY();
146         clock_->Invalidate();
147         if (type_ == MoveType::MOVE_LEFT) {
148             xPos -= grid_;
149         } else if (type_ == MoveType::MOVE_RIGHT) {
150             xPos += grid_;
151         } else if (type_ == MoveType::MOVE_TOP) {
152             yPos -= grid_;
153         } else if (type_ == MoveType::MOVE_BOTTOM) {
154             yPos += grid_;
155         }
156         clock_->SetPosition(xPos, yPos);
157         return true;
158     }
159 
160 private:
161     UIView* uiView_;
162     UIAnalogClock* clock_;
163     MoveType type_;
164     uint16_t grid_;
165 };
166 
UIKitTestLineHandAnalogClock001()167 void UITestAnalogClock::UIKitTestLineHandAnalogClock001()
168 {
169     if (container_ != nullptr) {
170         UIViewGroup* group1 = new UIViewGroup();
171         group1->SetPosition(0, 0);
172         group1->SetWidth(Screen::GetInstance().GetWidth());
173         group1->SetHeight(Screen::GetInstance().GetHeight() + 100); // 100: height bigger value
174         container_->Add(group1);
175 
176         UILabel* label = new UILabel();
177         group1->Add(label);
178         // 30: height
179         label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, Screen::GetInstance().GetWidth(), 30);
180         label->SetText("直线指针效果");
181         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
182 
183         UIAnalogClock* clock1 = new UIAnalogClock();
184         group1->Add(clock1);
185         clock1->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE,
186                             Screen::GetInstance().GetWidth() / 2, // 2: half of screen width
187                             Screen::GetInstance().GetHeight());
188 
189         UIImageView* curFace = new UIImageView();
190         curFace->SetSrc(B024_002_IMAGE_PATH);
191         clock1->Add(curFace);
192         curFace->SetPosition(0, 0);
193         // 214: position x 3:position y 14:center x 223:center y 1: width 223:height 255: opacity
194         clock1->SetHandLine(UIAnalogClock::HandType::SECOND_HAND, {214, 3}, {14, 223}, Color::White(), 1, 223, 255);
195         // 202: position x 25:position y 25:center x 201:center y 3: width 201:height 255: opacity
196         clock1->SetHandLine(UIAnalogClock::HandType::MINUTE_HAND, {202, 25}, {25, 201}, Color::Green(), 3, 201, 255);
197         // 202: position x 95:position y 25:center x 132:center y 5: width 132: height 255: opacity
198         clock1->SetHandLine(UIAnalogClock::HandType::HOUR_HAND, {202, 95}, {25, 132}, Color::Red(), 5, 132, 255);
199         clock1->SetInitTime24Hour(0, 0, 0);
200         clock1->SetWorkMode(UIAnalogClock::WorkMode::NORMAL);
201         clock1->SetTime24Hour(10, 3, 20); // 10: hour; 3: minute: 20: second
202         CreateButtons001(group1, curFace, clock1);
203         callback_ = new ClockAnimatorCallback(clock1);
204         animator_ = new Animator(callback_, clock1, 0, true);
205         animator_->Start();
206     }
207 }
208 
CreateButtons001(UIViewGroup * group,UIImageView * curFace,UIAnalogClock * clock)209 void UITestAnalogClock::CreateButtons001(UIViewGroup* group, UIImageView* curFace, UIAnalogClock* clock)
210 {
211     if ((group == nullptr) || (curFace == nullptr) || (clock == nullptr)) {
212         return;
213     }
214     UILabelButton* button1 = SetUpButton("切换模式", UI_TEST_SWITCH_MODE_1);
215     group->Add(button1);
216     // 58: increase x-position; 48: y-position
217     button1->SetPosition(curFace->GetWidth() + 58, 48, BUTTON_WIDTH, BUTTON_HEIGHT);
218     if (changeModeListener_ == nullptr) {
219         changeModeListener_ = static_cast<UIView::OnClickListener*>(
220             new TestBtnOnClickChangeModeListener(static_cast<UIView*>(button1), clock));
221     }
222     button1->SetOnClickListener(changeModeListener_);
223 
224     UILabelButton* button2 = SetUpButton("左移", UI_TEST_SHIF_LEFT_1);
225     group->Add(button2);
226     // 58: increase x-position; 58: increase y-position
227     button2->SetPosition(curFace->GetWidth() + 58, BUTTON_HEIGHT + 58, BUTTON_WIDTH, BUTTON_HEIGHT);
228     if (clickMoveLeftListener_ == nullptr) {
229         clickMoveLeftListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickMovePositionListener(
230             static_cast<UIView*>(button2), clock,
231             TestBtnOnClickMovePositionListener::MoveType::MOVE_LEFT, 10)); // 10: grid
232     }
233     button2->SetOnClickListener(clickMoveLeftListener_);
234 
235     UILabelButton* button3 = SetUpButton("右移", UI_TEST_SHIF_RIGHT_1);
236     group->Add(button3);
237     // 58: increase x-position; 148: y-position
238     button3->SetPosition(curFace->GetWidth() + 58, 148, BUTTON_WIDTH, BUTTON_HEIGHT);
239     if (clickMoveRightListener_ == nullptr) {
240         clickMoveRightListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickMovePositionListener(
241             static_cast<UIView*>(button3), clock,
242             TestBtnOnClickMovePositionListener::MoveType::MOVE_RIGHT, 10)); // 10: grid
243     }
244     button3->SetOnClickListener(clickMoveRightListener_);
245 
246     UILabelButton* button4 = SetUpButton("上移", UI_TEST_SHIF_UP_1);
247     group->Add(button4);
248     // 58: increase x-position; 198: y-position
249     button4->SetPosition(curFace->GetWidth() + 58, 198, BUTTON_WIDTH, BUTTON_HEIGHT);
250     if (clickMoveTopListener_ == nullptr) {
251         clickMoveTopListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickMovePositionListener(
252             static_cast<UIView*>(button4), clock,
253             TestBtnOnClickMovePositionListener::MoveType::MOVE_TOP, 10)); // 10: move value
254     }
255     button4->SetOnClickListener(clickMoveTopListener_);
256 
257     UILabelButton* button5 = SetUpButton("下移", UI_TEST_SHIF_DOWN_1);
258     group->Add(button5);
259     // 58: increase x-position; 248: y-position
260     button5->SetPosition(curFace->GetWidth() + 58, 248, BUTTON_WIDTH, BUTTON_HEIGHT);
261     if (clickMoveBottomListener_ == nullptr) {
262         clickMoveBottomListener_ = static_cast<UIView::OnClickListener*>(new TestBtnOnClickMovePositionListener(
263             static_cast<UIView*>(button5), clock,
264             TestBtnOnClickMovePositionListener::MoveType::MOVE_BOTTOM, 10)); // 10: move value
265     }
266     button5->SetOnClickListener(clickMoveBottomListener_);
267 }
268 
UIKitTestImageHandAnalogClock002()269 void UITestAnalogClock::UIKitTestImageHandAnalogClock002()
270 {
271     if (container_ != nullptr) {
272         UIViewGroup* group2 = new UIViewGroup();
273         group2->SetPosition(0, Screen::GetInstance().GetHeight() + 50); // 50: increase y-coordinate
274         group2->SetWidth(Screen::GetInstance().GetWidth());
275         group2->SetHeight(Screen::GetInstance().GetHeight() + 100); // 100: increase y-coordinate
276         container_->Add(group2);
277 
278         UILabel* label = new UILabel();
279         group2->Add(label);
280         label->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE, 0, Screen::GetInstance().GetWidth(), 30); // 30: height
281         label->SetText("图片指针效果");
282         label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
283 
284         UIImageView* image1 = new UIImageView();
285         image1->SetSrc(B024_010_IMAGE_PATH);
286         UIImageView* image2 = new UIImageView();
287         image2->SetSrc(B024_011_IMAGE_PATH);
288         UIImageView* image3 = new UIImageView();
289         image3->SetSrc(B024_009_IMAGE_PATH);
290 
291         UIAnalogClock* clock2 = new UIAnalogClock();
292         group2->Add(clock2);
293         clock2->SetDragParentInstead(true);
294         clock2->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE,
295                             Screen::GetInstance().GetWidth() / 2, // 2: half of screen width
296                             Screen::GetInstance().GetHeight());
297 
298         UIImageView* curFace = new UIImageView();
299         curFace->SetSrc(B024_002_IMAGE_PATH);
300         clock2->Add(curFace);
301         curFace->SetPosition(0, 0);
302         // {214, 3}: position x, y {14, 223}: center x, y
303         clock2->SetHandImage(UIAnalogClock::HandType::SECOND_HAND, *image1, {214, 3}, {14, 223});
304         // {202, 15}: position x, y {25, 211}: center x, y
305         clock2->SetHandImage(UIAnalogClock::HandType::MINUTE_HAND, *image2, {202, 15}, {25, 211});
306         // {202, 15}: position x, y {25, 211}: center x, y
307         clock2->SetHandImage(UIAnalogClock::HandType::HOUR_HAND, *image3, {202, 15}, {25, 211});
308 
309         clock2->SetInitTime24Hour(0, 0, 0);
310         clock2->SetWorkMode(UIAnalogClock::WorkMode::NORMAL);
311         clock2->SetTime24Hour(3, 3, 15); // 3: hour; 3: minute: 15: second
312         CreateButtons002(group2, curFace, clock2);
313         callback2_ = new ClockAnimatorCallback(clock2);
314         animator2_ = new Animator(callback2_, clock2, 0, true);
315         animator2_->Start();
316     }
317 }
318 
CreateButtons002(UIViewGroup * group,UIImageView * curFace,UIAnalogClock * clock)319 void UITestAnalogClock::CreateButtons002(UIViewGroup* group, UIImageView* curFace, UIAnalogClock* clock)
320 {
321     if ((group == nullptr) || (curFace == nullptr) || (clock == nullptr)) {
322         return;
323     }
324     UILabelButton* button1 = SetUpButton("切换模式", UI_TEST_SWITCH_MODE_2);
325     group->Add(button1);
326     // 58: increase x-coordinate; 48: y-coordinate
327     button1->SetPosition(curFace->GetWidth() + 58, 48, BUTTON_WIDTH, BUTTON_HEIGHT);
328     if (changeModeListener1_ == nullptr) {
329         changeModeListener1_ = static_cast<UIView::OnClickListener*>(
330             new TestBtnOnClickChangeModeListener(static_cast<UIView*>(button1), clock));
331     }
332     button1->SetOnClickListener(changeModeListener1_);
333 
334     UILabelButton* button2 = SetUpButton("左移", UI_TEST_SHIF_LEFT_2);
335     group->Add(button2);
336     // 58: increase x-coordinate; 98: y-coordinate
337     button2->SetPosition(curFace->GetWidth() + 58, 98, BUTTON_WIDTH, BUTTON_HEIGHT);
338     if (clickMoveLeftListener1_ == nullptr) {
339         clickMoveLeftListener1_ = static_cast<UIView::OnClickListener*>(
340             new TestBtnOnClickMovePositionListener(
341             static_cast<UIView*>(button2), clock,
342             TestBtnOnClickMovePositionListener::MoveType::MOVE_LEFT, 10)); // 10: grid
343     }
344     button2->SetOnClickListener(clickMoveLeftListener1_);
345 
346     UILabelButton* button3 = SetUpButton("右移", UI_TEST_SHIF_RIGHT_2);
347     group->Add(button3);
348     // 58: increase x-coordinate; 148: y-coordinate
349     button3->SetPosition(curFace->GetWidth() + 58, 148, BUTTON_WIDTH, BUTTON_HEIGHT);
350     if (clickMoveRightListener1_ == nullptr) {
351         clickMoveRightListener1_ = static_cast<UIView::OnClickListener*>(
352             new TestBtnOnClickMovePositionListener(
353             static_cast<UIView*>(button3), clock,
354             TestBtnOnClickMovePositionListener::MoveType::MOVE_RIGHT, 10)); // 10: move value
355     }
356     button3->SetOnClickListener(clickMoveRightListener1_);
357 
358     UILabelButton* button4 = SetUpButton("上移", UI_TEST_SHIF_UP_2);
359     group->Add(button4);
360     // 58: increase x-coordinate; 198: y-coordinate
361     button4->SetPosition(curFace->GetWidth() + 58, 198, BUTTON_WIDTH, BUTTON_HEIGHT);
362     if (clickMoveTopListener1_ == nullptr) {
363         clickMoveTopListener1_ = static_cast<UIView::OnClickListener*>(
364             new TestBtnOnClickMovePositionListener(
365             static_cast<UIView*>(button4), clock,
366             TestBtnOnClickMovePositionListener::MoveType::MOVE_TOP, 10)); // 10: move value
367     }
368     button4->SetOnClickListener(clickMoveTopListener1_);
369     UILabelButton* button5 = SetUpButton("下移", UI_TEST_SHIF_DOWN_2);
370     group->Add(button5);
371     // 58: increase x-coordinate; 248: y-coordinate
372     button5->SetPosition(curFace->GetWidth() + 58, 248, BUTTON_WIDTH, BUTTON_HEIGHT);
373     /* Move bottom */
374     if (clickMoveBottomListener1_ == nullptr) {
375         clickMoveBottomListener1_ = static_cast<UIView::OnClickListener*>(
376             new TestBtnOnClickMovePositionListener(
377             static_cast<UIView*>(button5), clock,
378             TestBtnOnClickMovePositionListener::MoveType::MOVE_BOTTOM, 10)); // 10: move value
379     }
380     button5->SetOnClickListener(clickMoveBottomListener1_);
381 }
382 } // namespace OHOS
383