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_ui_picker.h"
17 #include "common/screen.h"
18 #include "components/ui_label.h"
19 #include "components/ui_label_button.h"
20
21 #include <string>
22
23 namespace OHOS {
24 namespace {
25 static int16_t g_blank = 38;
26 static int16_t g_ListW = 200;
27 static int16_t g_ListH = 300;
28 static const char* g_pickerRange[] = {"A0", "B1", "C2", "D3", "E4", "F5", "G6", "H7", "I8", "J9", "K10", "L11"};
29 } // namespace
30
SetUp()31 void UITestUIPicker::SetUp()
32 {
33 if (container_ == nullptr) {
34 container_ = new UIScrollView();
35 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
36 container_->SetThrowDrag(true);
37 container_->SetHorizontalScrollState(false);
38 }
39 }
40
TearDown()41 void UITestUIPicker::TearDown()
42 {
43 container_->Remove(setLoopBtn_);
44 container_->Remove(setLoopOffBtn_);
45 container_->Remove(setSelectBtn_);
46 container_->Remove(setLeftToRightBtn_);
47 container_->Remove(setRightToLeftBtn_);
48 container_->Remove(setMarginBtn_);
49 container_->Remove(selectIndex_);
50 container_->Remove(selectTime_);
51 container_->Remove(picker1_);
52 container_->Remove(picker2_);
53 container_->Remove(picker3_);
54 DeleteChildren(container_);
55 setLoopBtn_ = nullptr;
56 setLoopOffBtn_ = nullptr;
57 setSelectBtn_ = nullptr;
58 setLeftToRightBtn_ = nullptr;
59 setRightToLeftBtn_ = nullptr;
60 setMarginBtn_ = nullptr;
61 selectIndex_ = nullptr;
62 selectTime_ = nullptr;
63 container_ = nullptr;
64 picker1_ = nullptr;
65 picker2_ = nullptr;
66 picker3_ = nullptr;
67
68 lastX_ = 0;
69 lastY_ = 0;
70 }
71
GetTestView()72 const UIView* UITestUIPicker::GetTestView()
73 {
74 UIKitPickerTestBase001();
75 UIKitPickerTestBase002();
76 UIKitPickerTestTimePicker001();
77 return container_;
78 }
79
OnPickerStoped(UIPicker & picker)80 void UITestUIPicker::OnPickerStoped(UIPicker& picker)
81 {
82 uint16_t index = picker.GetSelected();
83 selectIndex_->SetText(std::to_string(index).c_str());
84 selectIndex_->Invalidate();
85 }
86
OnTimePickerStoped(UITimePicker & picker)87 void UITestUIPicker::OnTimePickerStoped(UITimePicker& picker)
88 {
89 selectTime_->SetText(picker.GetSelectValue());
90 selectTime_->Invalidate();
91 }
92
UIKitPickerTestBase001()93 void UITestUIPicker::UIKitPickerTestBase001()
94 {
95 if (container_ == nullptr) {
96 return;
97 }
98 positionX_ = VIEW_DISTANCE_TO_LEFT_SIDE2;
99 UILabel* label = GetTitleLabel("UIPicker动态字体 ");
100 container_->Add(label);
101 label->SetPosition(positionX_, 0);
102 if (picker1_ == nullptr) {
103 picker1_ = new UIPicker();
104 }
105 picker1_->SetPosition(positionX_, label->GetY() + g_blank, g_ListW, g_ListH);
106 picker1_->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
107 picker1_->SetStyle(STYLE_BACKGROUND_COLOR, Color::Green().full);
108 picker1_->SetFontId(16, 18); // 16:back font id 18:high light font id
109 picker1_->SetItemHeight(50); // 50: height
110 picker1_->SetTextColor(Color::Gray(), Color::Red());
111 picker1_->SetDirect(UITextLanguageDirect::TEXT_DIRECT_RTL);
112 picker1_->SetValues(g_pickerRange, sizeof(g_pickerRange) / sizeof(g_pickerRange[0]));
113 picker1_->SetSelected(3); // 3 : default selected value
114 picker1_->RegisterSelectedListener(this);
115
116 positionX_ = picker1_->GetWidth();
117 positionY_ = picker1_->GetY();
118 CreatButtons();
119 if (selectIndex_ == nullptr) {
120 selectIndex_ = new UILabel();
121 }
122 selectIndex_ = GetTitleLabel("NULL");
123 selectIndex_->SetPosition(positionX_ + 40, label->GetY() + g_blank); // 40: increase x-coordinate
124 positionY_ += label->GetY() + g_blank;
125 container_->Add(selectIndex_);
126 SetUpButton(setLoopBtn_, "开启循环 ", UI_TEST_START_LOOP);
127 SetUpButton(setLoopOffBtn_, "关闭循环 ", UI_TEST_STOP_LOOP);
128 SetUpButton(setSelectBtn_, "定位到第5个 ", UI_TEST_GO_TO_THE_FIFTH);
129 SetUpButton(setLeftToRightBtn_, "从左往右 ", UI_TEST_FROM_LEFT_TO_RIGHT);
130 SetUpButton(setRightToLeftBtn_, "从右往左 ", UI_TEST_FROM_RIGHT_TO_LEFT);
131 container_->Add(picker1_);
132 SetLastPos(picker1_);
133 }
134
CreatButtons()135 void UITestUIPicker::CreatButtons()
136 {
137 if (setLoopBtn_ == nullptr) {
138 setLoopBtn_ = new UILabelButton();
139 }
140 if (setLoopOffBtn_ == nullptr) {
141 setLoopOffBtn_ = new UILabelButton();
142 }
143 if (setSelectBtn_ == nullptr) {
144 setSelectBtn_ = new UILabelButton();
145 }
146 if (setLeftToRightBtn_ == nullptr) {
147 setLeftToRightBtn_ = new UILabelButton();
148 }
149 if (setRightToLeftBtn_ == nullptr) {
150 setRightToLeftBtn_ = new UILabelButton();
151 }
152 if (setMarginBtn_ == nullptr) {
153 setMarginBtn_ = new UILabelButton();
154 }
155 }
156
UIKitPickerTestBase002()157 void UITestUIPicker::UIKitPickerTestBase002()
158 {
159 if (container_ == nullptr) {
160 return;
161 }
162 UILabel* label = GetTitleLabel("UIPicker区间数字 ");
163 container_->Add(label);
164 label->SetPosition(390, 0); // 390 x-coordinate
165 if (picker3_ == nullptr) {
166 picker3_ = new UIPicker();
167 }
168 picker3_->SetPosition(390, g_blank, g_ListW, g_ListH); // 390: x-coordinate
169 picker3_->SetFontId(16, 18); // 16:back font id 18:high light font id
170 picker3_->SetItemHeight(50); // 50: height
171 picker3_->SetTextColor(Color::Gray(), Color::Red());
172 picker3_->SetValues(-5, 20); // -5: start 20:end
173 picker3_->SetTextFormatter(new TextFormatter());
174
175 positionX_ = picker3_->GetWidth();
176 positionY_ = picker3_->GetY();
177
178 container_->Add(picker3_);
179 SetLastPos(picker3_);
180 }
181
UIKitPickerTestTimePicker001()182 void UITestUIPicker::UIKitPickerTestTimePicker001()
183 {
184 if (container_ == nullptr) {
185 return;
186 }
187 UILabel* label = GetTitleLabel("Time Picker");
188 container_->Add(label);
189 label->SetPosition(672, 0); // 672: x-coordinate
190 label->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
191
192 container_->Add(setMarginBtn_);
193 setMarginBtn_->SetPosition(672, 30, 120, 30); // 672: x-coordinate, 30 : y-coordinate, 120 : width, 30 : height
194 setMarginBtn_->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
195 setMarginBtn_->SetOnClickListener(this);
196 setMarginBtn_->SetText("增加margin");
197 setMarginBtn_->SetViewId(UI_TEST_PICKER_ADD_MARGIN);
198
199 if (picker2_ == nullptr) {
200 picker2_ = new UITimePicker();
201 }
202 picker2_->EnableSecond(true);
203 picker2_->SetPosition(652, setMarginBtn_->GetY() + 40, g_ListW, g_ListH); // 652: x-coordinate, 40: offset
204 picker2_->SetItemHeight(50); // 50 height
205 picker2_->RegisterSelectedListener(this);
206 picker2_->SetSelected("12:20:30");
207 positionX_ = picker2_->GetWidth();
208 positionY_ = picker2_->GetY();
209
210 if (selectTime_ == nullptr) {
211 selectTime_ = new UILabel();
212 }
213 selectTime_ = GetTitleLabel(picker2_->GetSelectValue());
214 picker2_->RegisterSelectedListener(this);
215 selectTime_->SetPosition(662 + picker2_->GetWidth(), label->GetY() + g_blank); // 662: increase x-coordinate
216 container_->Add(selectTime_);
217 container_->Add(picker2_);
218 SetLastPos(picker2_);
219 }
220
OnClick(UIView & view,const ClickEvent & event)221 bool UITestUIPicker::OnClick(UIView& view, const ClickEvent& event)
222 {
223 if (&view == setLoopBtn_) {
224 picker1_->SetLoopState(true);
225 } else if (&view == setLoopOffBtn_) {
226 picker1_->SetLoopState(false);
227 } else if (&view == setSelectBtn_) {
228 picker1_->SetSelected(5); // 5: index
229 } else if (&view == setLeftToRightBtn_) {
230 picker1_->SetDirect(UITextLanguageDirect::TEXT_DIRECT_LTR);
231 } else if (&view == setRightToLeftBtn_) {
232 picker1_->SetDirect(UITextLanguageDirect::TEXT_DIRECT_RTL);
233 } else if (&view == setMarginBtn_) {
234 picker2_->Invalidate();
235 picker2_->SetStyle(STYLE_MARGIN_LEFT, ++margin_);
236 picker2_->SetStyle(STYLE_MARGIN_RIGHT, margin_);
237 picker2_->SetStyle(STYLE_MARGIN_TOP, margin_);
238 picker2_->SetStyle(STYLE_MARGIN_BOTTOM, margin_);
239 picker2_->SetWidth(++g_ListW);
240 picker2_->Invalidate();
241 }
242 return true;
243 }
244
SetUpButton(UILabelButton * btn,const char * title,const char * id)245 void UITestUIPicker::SetUpButton(UILabelButton* btn, const char* title, const char* id)
246 {
247 if (btn == nullptr || title == nullptr || id == nullptr) {
248 return;
249 }
250 container_->Add(btn);
251 btn->SetPosition(positionX_ + 30, positionY_, BUTTON_WIDHT2, BUTTON_HEIGHT2); // 30: increase x-coordinate;
252 positionY_ += btn->GetHeight() + 12; // 12: increase y-coordinate
253 btn->SetText(title);
254 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
255 btn->SetOnClickListener(this);
256 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
257 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
258 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
259 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
260 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
261 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
262 btn->SetViewId(id);
263 container_->Invalidate();
264 }
265
SetLastPos(UIView * view)266 void UITestUIPicker::SetLastPos(UIView* view)
267 {
268 lastX_ = view->GetX();
269 lastY_ = view->GetY() + view->GetHeight();
270 }
271 } // namespace OHOS
272