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