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_focus_manager.h"
17
18 #if ENABLE_FOCUS_MANAGER
19 namespace OHOS {
20 namespace {
21 const uint16_t LABEL_BUTTON_DEFAULT_FONT_SIZE = 15;
22 const uint16_t LABEL_BUTTON_DEFAULT_WIDTH = 60;
23 const uint16_t LABEL_BUTTON_DEFAULT_HEIGHT = 40;
24 } // namespace
25
26 class TestOnFocusListener : public UIView::OnFocusListener {
27 public:
TestOnFocusListener()28 TestOnFocusListener() {}
29
~TestOnFocusListener()30 ~TestOnFocusListener() {}
31
OnFocus(UIView & view)32 bool OnFocus(UIView& view) override
33 {
34 if (view.IsViewGroup()) {
35 view.SetStyle(STYLE_BORDER_COLOR, Color::Red().full);
36 } else {
37 view.SetStyle(STYLE_BACKGROUND_COLOR, Color::Red().full);
38 }
39 view.Invalidate();
40 return true;
41 }
42
OnBlur(UIView & view)43 bool OnBlur(UIView& view) override
44 {
45 if (view.IsViewGroup()) {
46 view.SetStyle(STYLE_BORDER_COLOR, Color::White().full);
47 } else {
48 /* 0: red, 125: green, 255: blue */
49 view.SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0, 125, 255).full);
50 }
51 view.Invalidate();
52 return true;
53 }
54 };
55
56 class RequestFocusByDirectionOnClickListener : public UIView::OnClickListener {
57 public:
RequestFocusByDirectionOnClickListener(uint8_t direction)58 explicit RequestFocusByDirectionOnClickListener(uint8_t direction) : direction_(direction) {}
59
~RequestFocusByDirectionOnClickListener()60 ~RequestFocusByDirectionOnClickListener() {}
61
OnClick(UIView & view,const ClickEvent & event)62 bool OnClick(UIView& view, const ClickEvent& event)
63 {
64 FocusManager::GetInstance()->RequestFocusByDirection(direction_);
65 return true;
66 }
67 private:
68 uint8_t direction_ = 0;
69 };
70
71 class ResetFocusOnClickListener : public UIView::OnClickListener {
72 public:
ResetFocusOnClickListener(UIView * focus)73 explicit ResetFocusOnClickListener(UIView* focus) : focus_(focus) {}
74
~ResetFocusOnClickListener()75 ~ResetFocusOnClickListener() {}
76
OnClick(UIView & view,const ClickEvent & event)77 bool OnClick(UIView& view, const ClickEvent& event)
78 {
79 FocusManager::GetInstance()->RequestFocus(focus_);
80 return true;
81 }
82
83 private:
84 UIView* focus_ = nullptr;
85 };
86
87 class SetGroupInterceptOnClickListener : public UIView::OnClickListener {
88 public:
SetGroupInterceptOnClickListener(UIViewGroup * viewGroup,bool intercept)89 SetGroupInterceptOnClickListener(UIViewGroup* viewGroup, bool intercept)
90 : viewGroup_(viewGroup), intercept_(intercept) {}
91
~SetGroupInterceptOnClickListener()92 ~SetGroupInterceptOnClickListener() {}
93
OnClick(UIView & view,const ClickEvent & event)94 bool OnClick(UIView& view, const ClickEvent& event)
95 {
96 if (viewGroup_ != nullptr) {
97 viewGroup_->SetInterceptFocus(intercept_);
98 return true;
99 }
100 return false;
101 }
102 private:
103 UIViewGroup* viewGroup_ = nullptr;
104 bool intercept_;
105 };
106
107 class SetGroupInterceptFalseOnClickListener : public UIView::OnClickListener {
108 public:
SetGroupInterceptFalseOnClickListener(UIViewGroup * viewGroup)109 explicit SetGroupInterceptFalseOnClickListener(UIViewGroup* viewGroup) : viewGroup_(viewGroup) {}
110
~SetGroupInterceptFalseOnClickListener()111 ~SetGroupInterceptFalseOnClickListener() {}
112
OnClick(UIView & view,const ClickEvent & event)113 bool OnClick(UIView& view, const ClickEvent& event)
114 {
115 viewGroup_->SetInterceptFocus(false);
116 return true;
117 }
118 private:
119 UIViewGroup* viewGroup_ = nullptr;
120 };
121
122 class SetFocusableOnClickListener : public UIView::OnClickListener {
123 public:
SetFocusableOnClickListener(UIView * view,bool enable)124 SetFocusableOnClickListener(UIView* view, bool enable) : view_(view), enable_(enable) {}
125
~SetFocusableOnClickListener()126 ~SetFocusableOnClickListener() {}
127
OnClick(UIView & view,const ClickEvent & event)128 bool OnClick(UIView& view, const ClickEvent& event)
129 {
130 view_->SetFocusable(enable_);
131 return true;
132 }
133 private:
134 UIView* view_ = nullptr;
135 bool enable_ = false;
136 };
137
138 class RequestFocusOnClickListener : public UIView::OnClickListener {
139 public:
RequestFocusOnClickListener(UIView * view)140 explicit RequestFocusOnClickListener(UIView* view) : view_(view) {}
141
~RequestFocusOnClickListener()142 ~RequestFocusOnClickListener() {}
143
OnClick(UIView & view,const ClickEvent & event)144 bool OnClick(UIView& view, const ClickEvent& event)
145 {
146 FocusManager::GetInstance()->RequestFocus(view_);
147 return true;
148 }
149 private:
150 UIView* view_ = nullptr;
151 };
152
153 class ClearFocusOnClickListener : public UIView::OnClickListener {
154 public:
ClearFocusOnClickListener()155 ClearFocusOnClickListener() {}
156
~ClearFocusOnClickListener()157 ~ClearFocusOnClickListener() {}
158
OnClick(UIView & view,const ClickEvent & event)159 bool OnClick(UIView& view, const ClickEvent& event)
160 {
161 FocusManager::GetInstance()->ClearFocus();
162 return true;
163 }
164 };
165
SetUp()166 void UITestFocusManager::SetUp()
167 {
168 if (container_ == nullptr) {
169 container_ = new UIScrollView();
170 container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
171 container_->SetHorizontalScrollState(false);
172 }
173 }
174
TearDown()175 void UITestFocusManager::TearDown()
176 {
177 FocusManager::GetInstance()->ClearFocus();
178 if (testOnFocusListener_ != nullptr) {
179 delete testOnFocusListener_;
180 testOnFocusListener_ = nullptr;
181 }
182 if (requestFocusByDirectionLeftListener_ != nullptr) {
183 delete requestFocusByDirectionLeftListener_;
184 requestFocusByDirectionLeftListener_ = nullptr;
185 }
186 if (requestFocusByDirectionRightListener_ != nullptr) {
187 delete requestFocusByDirectionRightListener_;
188 requestFocusByDirectionRightListener_ = nullptr;
189 }
190 if (requestFocusByDirectionUpListener_ != nullptr) {
191 delete requestFocusByDirectionUpListener_;
192 requestFocusByDirectionUpListener_ = nullptr;
193 }
194 if (requestFocusByDirectionLeftListener_ != nullptr) {
195 delete requestFocusByDirectionLeftListener_;
196 requestFocusByDirectionLeftListener_ = nullptr;
197 }
198 if (requestFocusByDirectionDownListener_ != nullptr) {
199 delete requestFocusByDirectionDownListener_;
200 requestFocusByDirectionDownListener_ = nullptr;
201 }
202 if (setFocusableViewListener_ != nullptr) {
203 delete setFocusableViewListener_;
204 setFocusableViewListener_ = nullptr;
205 }
206 if (setFocusableViewListener1_ != nullptr) {
207 delete setFocusableViewListener1_;
208 setFocusableViewListener1_ = nullptr;
209 }
210 if (setGroupInterceptListener_ != nullptr) {
211 delete setGroupInterceptListener_;
212 setGroupInterceptListener_ = nullptr;
213 }
214 if (setGroupInterceptListener1_ != nullptr) {
215 delete setGroupInterceptListener1_;
216 setGroupInterceptListener1_ = nullptr;
217 }
218 if (resetFocusListener_ != nullptr) {
219 delete resetFocusListener_;
220 resetFocusListener_ = nullptr;
221 }
222 if (clearFocusListener_ != nullptr) {
223 delete clearFocusListener_;
224 clearFocusListener_ = nullptr;
225 }
226 DeleteChildren(container_);
227 container_ = nullptr;
228 }
229
GetTestView()230 const UIView* UITestFocusManager::GetTestView()
231 {
232 UIKitFocusManagerTest001();
233 return container_;
234 }
235
CreateTestUILabel(UIViewGroup * parent,int16_t x,int16_t y,const char * text,bool focusable)236 UIView* UITestFocusManager::CreateTestUILabel(UIViewGroup* parent, int16_t x, int16_t y,
237 const char* text, bool focusable)
238 {
239 UILabel* label = new UILabel();
240 parent->Add(label);
241 label->SetPosition(x, y, LABEL_BUTTON_DEFAULT_WIDTH, LABEL_BUTTON_DEFAULT_HEIGHT);
242 label->SetText(text);
243 label->SetViewId(text);
244 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, LABEL_BUTTON_DEFAULT_FONT_SIZE);
245 label->SetFocusable(focusable);
246 /* 0: red, 125: green, 255: blue */
247 label->SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0, 125, 255).full);
248 label->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
249 label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
250 if (testOnFocusListener_ == nullptr) {
251 testOnFocusListener_ = static_cast<UIView::OnFocusListener*>(new TestOnFocusListener());
252 }
253 label->SetOnFocusListener(testOnFocusListener_);
254 return label;
255 }
256
CreateTestUIViewGroup(UIViewGroup * parent,bool focusable,bool interceptFocus)257 UIViewGroup* UITestFocusManager::CreateTestUIViewGroup(UIViewGroup* parent, bool focusable, bool interceptFocus)
258 {
259 UIViewGroup* viewGroup = new UIViewGroup();
260 parent->Add(viewGroup);
261 viewGroup->SetFocusable(focusable);
262 viewGroup->SetInterceptFocus(interceptFocus);
263 viewGroup->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
264 viewGroup->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
265 viewGroup->SetStyle(STYLE_BORDER_WIDTH, 1);
266 viewGroup->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
267 if (testOnFocusListener_ == nullptr) {
268 testOnFocusListener_ = static_cast<UIView::OnFocusListener*>(new TestOnFocusListener());
269 }
270 viewGroup->SetOnFocusListener(testOnFocusListener_);
271 return viewGroup;
272 }
273
UIKitFocusManagerTest001()274 void UITestFocusManager::UIKitFocusManagerTest001()
275 {
276 UILabel* label = new UILabel();
277 container_->Add(label);
278 /* 288: width */
279 label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, TITLE_LABEL_DEFAULT_HEIGHT);
280 label->SetText("焦点管理效果 ");
281 label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
282 UIViewGroup* viewGroup = CreateTestUIViewGroup(container_, true, false);
283 /* 600: width, 350: height */
284 viewGroup->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 600, 350);
285
286 UIViewGroup* viewGroup1 = CreateTestUIViewGroup(viewGroup, true, false);
287 UIViewGroup* viewGroup2 = CreateTestUIViewGroup(viewGroup1, true, false);
288 UIViewGroup* viewGroup3 = CreateTestUIViewGroup(viewGroup, true, false);
289 UIViewGroup* viewGroup4 = CreateTestUIViewGroup(viewGroup3, true, false);
290 UIViewGroup* viewGroup5 = CreateTestUIViewGroup(viewGroup4, true, false);
291 UIViewGroup* viewGroup6 = CreateTestUIViewGroup(viewGroup, true, false);
292
293 viewGroup1->SetPosition(5, 5, 270, 60); /* 5: x, 5: y; 270: width, 60: height */
294 viewGroup2->SetPosition(100, 5, 80, 50); /* 100: x, 5: y; 80: width, 50: height */
295 viewGroup3->SetPosition(290, 5, 280, 115); /* 290: x, 5: y; 280: width, 115: height */
296 viewGroup4->SetPosition(5, 5, 260, 60); /* 5: x, 5: y; 260: width, 60: height */
297 viewGroup5->SetPosition(5, 5, 250, 50); /* 5: x, 5: y; 250: width, 50: height */
298 viewGroup6->SetPosition(100, 160, 400, 160); /* 100: x, 160: y; 400: width, 160: height */
299
300 CreateTestUILabel(viewGroup1, 0, 5, "1", true); /* 0: x, 5: y */
301 UIView* view2 = CreateTestUILabel(viewGroup1, 195, 5, "2", true); /* 195: x, 5: y */
302 CreateTestUILabel(viewGroup2, 5, 5, "3", true); /* 5: x, 5: y */
303 CreateTestUILabel(viewGroup5, 5, 5, "4", true); /* 5: x, 5: y */
304 CreateTestUILabel(viewGroup5, 180, 5, "5", true); /* 180: x, 5: y */
305 CreateTestUILabel(viewGroup3, 150, 70, "6", true); /* 150: x, 70: y */
306
307 CreateTestUILabel(viewGroup, 80, 100, "7", true); /* 80: x, 100: y */
308 CreateTestUILabel(viewGroup6, 5, 5, "8", true); /* 5: x, 5: y */
309 CreateTestUILabel(viewGroup6, 100, 80, "9", true); /* 100: x, 80: y */
310 CreateTestUILabel(viewGroup6, 300, 5, "10", true); /* 300: x, 5: y */
311
312 UIViewGroup* btnViewGroup = new UIViewGroup();
313 /* 650: x, 300: width, 400: height */
314 btnViewGroup->SetPosition(650, VIEW_DISTANCE_TO_TOP_SIDE, 300, 400);
315 container_->Add(btnViewGroup);
316
317 if (requestFocusByDirectionLeftListener_ == nullptr) {
318 requestFocusByDirectionLeftListener_ = static_cast<UIView::OnClickListener*>(
319 new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_LEFT));
320 }
321 if (requestFocusByDirectionRightListener_ == nullptr) {
322 requestFocusByDirectionRightListener_ = static_cast<UIView::OnClickListener*>(
323 new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_RIGHT));
324 }
325 if (requestFocusByDirectionUpListener_ == nullptr) {
326 requestFocusByDirectionUpListener_ = static_cast<UIView::OnClickListener*>(
327 new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_UP));
328 }
329 if (requestFocusByDirectionDownListener_ == nullptr) {
330 requestFocusByDirectionDownListener_ = static_cast<UIView::OnClickListener*>(
331 new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_DOWN));
332 }
333 if (setFocusableViewListener_ == nullptr) {
334 setFocusableViewListener_ = static_cast<UIView::OnClickListener*>(
335 new SetFocusableOnClickListener(view2, true));
336 }
337 if (setFocusableViewListener1_ == nullptr) {
338 setFocusableViewListener1_ = static_cast<UIView::OnClickListener*>(
339 new SetFocusableOnClickListener(view2, false));
340 }
341 if (setGroupInterceptListener_ == nullptr) {
342 setGroupInterceptListener_ = static_cast<UIView::OnClickListener*>(
343 new SetGroupInterceptOnClickListener(viewGroup5, true));
344 }
345 if (setGroupInterceptListener1_ == nullptr) {
346 setGroupInterceptListener1_ = static_cast<UIView::OnClickListener*>(
347 new SetGroupInterceptOnClickListener(viewGroup5, false));
348 }
349 if (resetFocusListener_ == nullptr) {
350 resetFocusListener_ = static_cast<UIView::OnClickListener*>(
351 new ResetFocusOnClickListener(viewGroup1->GetChildById("1")));
352 }
353 if (clearFocusListener_ == nullptr) {
354 clearFocusListener_ = static_cast<UIView::OnClickListener*>(new ClearFocusOnClickListener());
355 }
356 /* 10: x, 10: y */
357 SetUpButton("向左 ", 10, 10, btnViewGroup, requestFocusByDirectionLeftListener_, UI_TEST_TOWRADS_LEFT);
358 /* 150: x, 10: y */
359 SetUpButton("向右 ", 150, 10, btnViewGroup, requestFocusByDirectionRightListener_, UI_TEST_TOWRADS_RIGHT);
360 /* 10: x, 60: y */
361 SetUpButton("向上 ", 10, 60, btnViewGroup, requestFocusByDirectionUpListener_, UI_TEST_TOWRADS_UP);
362 /* 150: x, 60: y */
363 SetUpButton("向下 ", 150, 60, btnViewGroup, requestFocusByDirectionDownListener_, UI_TEST_TOWRADS_DOWN);
364 /* 10: x, 110: y */
365 SetUpButton("2可获焦 ", 10, 110, btnViewGroup, setFocusableViewListener_, UI_TEST_2_FOCUSABLE);
366 /* 150: x, 110: y */
367 SetUpButton("2不可获焦 ", 150, 110, btnViewGroup, setFocusableViewListener1_, UI_TEST_2_UNFOCUSABLE);
368 /* 10: x, 160: y */
369 SetUpButton("设置4容器拦截 ", 10, 160, btnViewGroup, setGroupInterceptListener_, UI_TEST_4_INTERCEPT);
370 /* 150: x, 160: y */
371 SetUpButton("取消4容器拦截 ", 150, 160, btnViewGroup, setGroupInterceptListener1_, UI_TEST_4_CANCEL_INTERCEPT);
372 /* 10: x, 210: y */
373 SetUpButton("重置焦点 ", 10, 210, btnViewGroup, resetFocusListener_, UI_TEST_RESET_FOCUS);
374 /* 150: x, 210: y */
375 SetUpButton("清除焦点 ", 150, 210, btnViewGroup, clearFocusListener_, UI_TEST_CLEAR_FOCUS);
376
377 FocusManager::GetInstance()->RequestFocus(viewGroup1->GetChildById("1"));
378 }
379
SetUpButton(const char * title,int16_t x,int16_t y,UIViewGroup * viewGroup,UIView::OnClickListener * listener,const char * id)380 UILabelButton* UITestFocusManager::SetUpButton(const char* title, int16_t x, int16_t y, UIViewGroup* viewGroup,
381 UIView::OnClickListener* listener, const char* id)
382 {
383 if (viewGroup == nullptr || title == nullptr || id == nullptr) {
384 return nullptr;
385 }
386
387 UILabelButton* btn = new UILabelButton();
388 btn->SetPosition(x, y, BUTTON_WIDHT2, BUTTON_HEIGHT1);
389 btn->SetText(title);
390 btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
391 btn->SetOnClickListener(listener);
392 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
393 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
394 btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
395 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
396 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
397 btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
398 btn->SetViewId(id);
399 viewGroup->Add(btn);
400 return btn;
401 }
402 } // namespace OHOS
403 #endif
404