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