• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
117 private:
118     UIViewGroup* viewGroup_ = nullptr;
119 };
120 
121 class SetFocusableOnClickListener : public UIView::OnClickListener {
122 public:
SetFocusableOnClickListener(UIView * view,bool enable)123     SetFocusableOnClickListener(UIView* view, bool enable) : view_(view), enable_(enable) {}
124 
~SetFocusableOnClickListener()125     ~SetFocusableOnClickListener() {}
126 
OnClick(UIView & view,const ClickEvent & event)127     bool OnClick(UIView& view, const ClickEvent& event)
128     {
129         view_->SetFocusable(enable_);
130         return true;
131     }
132 private:
133     UIView* view_ = nullptr;
134     bool enable_ = false;
135 };
136 
137 class RequestFocusOnClickListener : public UIView::OnClickListener {
138 public:
RequestFocusOnClickListener(UIView * view)139     explicit RequestFocusOnClickListener(UIView* view) : view_(view) {}
140 
~RequestFocusOnClickListener()141     ~RequestFocusOnClickListener() {}
142 
OnClick(UIView & view,const ClickEvent & event)143     bool OnClick(UIView& view, const ClickEvent& event)
144     {
145         FocusManager::GetInstance()->RequestFocus(view_);
146         return true;
147     }
148 private:
149     UIView* view_ = nullptr;
150 };
151 
152 class ClearFocusOnClickListener : public UIView::OnClickListener {
153 public:
ClearFocusOnClickListener()154     ClearFocusOnClickListener() {}
155 
~ClearFocusOnClickListener()156     ~ClearFocusOnClickListener() {}
157 
OnClick(UIView & view,const ClickEvent & event)158     bool OnClick(UIView& view, const ClickEvent& event)
159     {
160         FocusManager::GetInstance()->ClearFocus();
161         return true;
162     }
163 };
164 
SetUp()165 void UITestFocusManager::SetUp()
166 {
167     if (container_ == nullptr) {
168         container_ = new UIScrollView();
169         container_->Resize(Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight() - BACK_BUTTON_HEIGHT);
170         container_->SetHorizontalScrollState(false);
171     }
172 }
173 
TearDown()174 void UITestFocusManager::TearDown()
175 {
176     FocusManager::GetInstance()->ClearFocus();
177     if (testOnFocusListener_ != nullptr) {
178         delete testOnFocusListener_;
179         testOnFocusListener_ = nullptr;
180     }
181     if (requestFocusByDirectionLeftListener_ != nullptr) {
182         delete requestFocusByDirectionLeftListener_;
183         requestFocusByDirectionLeftListener_ = nullptr;
184     }
185     if (requestFocusByDirectionRightListener_ != nullptr) {
186         delete requestFocusByDirectionRightListener_;
187         requestFocusByDirectionRightListener_ = nullptr;
188     }
189     if (requestFocusByDirectionUpListener_ != nullptr) {
190         delete requestFocusByDirectionUpListener_;
191         requestFocusByDirectionUpListener_ = nullptr;
192     }
193     if (requestFocusByDirectionLeftListener_ != nullptr) {
194         delete requestFocusByDirectionLeftListener_;
195         requestFocusByDirectionLeftListener_ = nullptr;
196     }
197     if (requestFocusByDirectionDownListener_ != nullptr) {
198         delete requestFocusByDirectionDownListener_;
199         requestFocusByDirectionDownListener_ = nullptr;
200     }
201     if (setFocusableViewListener_ != nullptr) {
202         delete setFocusableViewListener_;
203         setFocusableViewListener_ = nullptr;
204     }
205     if (setFocusableViewListener1_ != nullptr) {
206         delete setFocusableViewListener1_;
207         setFocusableViewListener1_ = nullptr;
208     }
209     if (setGroupInterceptListener_ != nullptr) {
210         delete setGroupInterceptListener_;
211         setGroupInterceptListener_ = nullptr;
212     }
213     if (setGroupInterceptListener1_ != nullptr) {
214         delete setGroupInterceptListener1_;
215         setGroupInterceptListener1_ = nullptr;
216     }
217     if (resetFocusListener_ != nullptr) {
218         delete resetFocusListener_;
219         resetFocusListener_ = nullptr;
220     }
221     if (clearFocusListener_ != nullptr) {
222         delete clearFocusListener_;
223         clearFocusListener_ = nullptr;
224     }
225     DeleteChildren(container_);
226     container_ = nullptr;
227 }
228 
GetTestView()229 const UIView* UITestFocusManager::GetTestView()
230 {
231     UIKit_Focus_Manager_Test_001();
232     return container_;
233 }
234 
CreateTestUILabel(UIViewGroup * parent,int16_t x,int16_t y,const char * text,bool focusable)235 UIView* UITestFocusManager::CreateTestUILabel(UIViewGroup* parent, int16_t x, int16_t y,
236     const char* text, bool focusable)
237 {
238     UILabel* label = new UILabel();
239     parent->Add(label);
240     label->SetPosition(x, y, LABEL_BUTTON_DEFAULT_WIDTH, LABEL_BUTTON_DEFAULT_HEIGHT);
241     label->SetText(text);
242     label->SetViewId(text);
243     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, LABEL_BUTTON_DEFAULT_FONT_SIZE);
244     label->SetFocusable(focusable);
245     /* 0: red, 125: green, 255: blue */
246     label->SetStyle(STYLE_BACKGROUND_COLOR, Color::GetColorFromRGB(0, 125, 255).full);
247     label->SetStyle(STYLE_BACKGROUND_OPA, OPA_OPAQUE);
248     label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
249     if (testOnFocusListener_ == nullptr) {
250         testOnFocusListener_ = static_cast<UIView::OnFocusListener*>(new TestOnFocusListener());
251     }
252     label->SetOnFocusListener(testOnFocusListener_);
253     return label;
254 }
255 
CreateTestUIViewGroup(UIViewGroup * parent,bool focusable,bool interceptFocus)256 UIViewGroup* UITestFocusManager::CreateTestUIViewGroup(UIViewGroup* parent, bool focusable, bool interceptFocus)
257 {
258     UIViewGroup* viewGroup = new UIViewGroup();
259     parent->Add(viewGroup);
260     viewGroup->SetFocusable(focusable);
261     viewGroup->SetInterceptFocus(interceptFocus);
262     viewGroup->SetStyle(STYLE_BORDER_COLOR, Color::White().full);
263     viewGroup->SetStyle(STYLE_BORDER_OPA, HALF_OPA_OPAQUE);
264     viewGroup->SetStyle(STYLE_BORDER_WIDTH, 1);
265     viewGroup->SetStyle(STYLE_BORDER_RADIUS, VIEW_STYLE_BORDER_RADIUS);
266     if (testOnFocusListener_ == nullptr) {
267         testOnFocusListener_ = static_cast<UIView::OnFocusListener*>(new TestOnFocusListener());
268     }
269     viewGroup->SetOnFocusListener(testOnFocusListener_);
270     return viewGroup;
271 }
272 
UIKit_Focus_Manager_Test_001()273 void UITestFocusManager::UIKit_Focus_Manager_Test_001()
274 {
275     UILabel* label = new UILabel();
276     container_->Add(label);
277     /* 288: width */
278     label->SetPosition(TEXT_DISTANCE_TO_LEFT_SIDE, TEXT_DISTANCE_TO_TOP_SIDE, 288, TITLE_LABEL_DEFAULT_HEIGHT);
279     label->SetText("焦点管理效果 ");
280     label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, FONT_DEFAULT_SIZE);
281     UIViewGroup* viewGroup = CreateTestUIViewGroup(container_, true, false);
282     /* 600: width, 350: height */
283     viewGroup->SetPosition(VIEW_DISTANCE_TO_LEFT_SIDE2, VIEW_DISTANCE_TO_TOP_SIDE, 600, 350);
284 
285     UIViewGroup* viewGroup1 = CreateTestUIViewGroup(viewGroup, true, false);
286     UIViewGroup* viewGroup2 = CreateTestUIViewGroup(viewGroup1, true, false);
287     UIViewGroup* viewGroup3 = CreateTestUIViewGroup(viewGroup, true, false);
288     UIViewGroup* viewGroup4 = CreateTestUIViewGroup(viewGroup3, true, false);
289     UIViewGroup* viewGroup5 = CreateTestUIViewGroup(viewGroup4, true, false);
290     UIViewGroup* viewGroup6 = CreateTestUIViewGroup(viewGroup, true, false);
291 
292     viewGroup1->SetPosition(5, 5, 270, 60);      /* 5: x,   5: y;   270: width, 60: height */
293     viewGroup2->SetPosition(100, 5, 80, 50);     /* 100: x, 5: y;   80: width,  50: height */
294     viewGroup3->SetPosition(290, 5, 280, 115);   /* 290: x, 5: y;   280: width, 115: height */
295     viewGroup4->SetPosition(5, 5, 260, 60);      /* 5: x,   5: y;   260: width, 60: height */
296     viewGroup5->SetPosition(5, 5, 250, 50);      /* 5: x,   5: y;   250: width, 50: height */
297     viewGroup6->SetPosition(100, 160, 400, 160); /* 100: x, 160: y; 400: width, 160: height */
298 
299     CreateTestUILabel(viewGroup1, 0, 5, "1", true);                   /* 0: x,   5: y */
300     UIView* view2 = CreateTestUILabel(viewGroup1, 195, 5, "2", true); /* 195: x, 5: y */
301     CreateTestUILabel(viewGroup2, 5, 5, "3", true);                   /* 5: x,   5: y */
302     CreateTestUILabel(viewGroup5, 5, 5, "4", true);                   /* 5: x,   5: y */
303     CreateTestUILabel(viewGroup5, 180, 5, "5", true);                 /* 180: x, 5: y */
304     CreateTestUILabel(viewGroup3, 150, 70, "6", true);                /* 150: x, 70: y */
305 
306     CreateTestUILabel(viewGroup, 80, 100, "7", true);                 /* 80: x,  100: y */
307     CreateTestUILabel(viewGroup6, 5, 5, "8", true);                   /* 5: x,   5: y */
308     CreateTestUILabel(viewGroup6, 100, 80, "9", true);                /* 100: x, 80: y */
309     CreateTestUILabel(viewGroup6, 300, 5, "10", true);                /* 300: x, 5: y */
310 
311     UIViewGroup* btnViewGroup = new UIViewGroup();
312     /* 650: x, 300: width, 400: height */
313     btnViewGroup->SetPosition(650, VIEW_DISTANCE_TO_TOP_SIDE, 300, 400);
314     container_->Add(btnViewGroup);
315 
316     if (requestFocusByDirectionLeftListener_ == nullptr) {
317         requestFocusByDirectionLeftListener_ = static_cast<UIView::OnClickListener*>(
318             new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_LEFT));
319     }
320     if (requestFocusByDirectionRightListener_ == nullptr) {
321         requestFocusByDirectionRightListener_ = static_cast<UIView::OnClickListener*>(
322             new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_RIGHT));
323     }
324     if (requestFocusByDirectionUpListener_ == nullptr) {
325         requestFocusByDirectionUpListener_ = static_cast<UIView::OnClickListener*>(
326             new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_UP));
327     }
328     if (requestFocusByDirectionDownListener_ == nullptr) {
329         requestFocusByDirectionDownListener_ = static_cast<UIView::OnClickListener*>(
330             new RequestFocusByDirectionOnClickListener(FOCUS_DIRECTION_DOWN));
331     }
332     if (setFocusableViewListener_ == nullptr) {
333         setFocusableViewListener_ = static_cast<UIView::OnClickListener*>(
334             new SetFocusableOnClickListener(view2, true));
335     }
336     if (setFocusableViewListener1_ == nullptr) {
337         setFocusableViewListener1_ = static_cast<UIView::OnClickListener*>(
338             new SetFocusableOnClickListener(view2, false));
339     }
340     if (setGroupInterceptListener_ == nullptr) {
341         setGroupInterceptListener_ = static_cast<UIView::OnClickListener*>(
342             new SetGroupInterceptOnClickListener(viewGroup5, true));
343     }
344     if (setGroupInterceptListener1_ == nullptr) {
345         setGroupInterceptListener1_ = static_cast<UIView::OnClickListener*>(
346             new SetGroupInterceptOnClickListener(viewGroup5, false));
347     }
348     if (resetFocusListener_ == nullptr) {
349         resetFocusListener_ = static_cast<UIView::OnClickListener*>(
350             new ResetFocusOnClickListener(viewGroup1->GetChildById("1")));
351     }
352     if (clearFocusListener_ == nullptr) {
353         clearFocusListener_ = static_cast<UIView::OnClickListener*>(new ClearFocusOnClickListener());
354     }
355     /* 10: x, 10: y */
356     SetUpButton("向左 ", 10, 10, btnViewGroup, requestFocusByDirectionLeftListener_);
357     /* 150: x, 10: y */
358     SetUpButton("向右 ", 150, 10, btnViewGroup, requestFocusByDirectionRightListener_);
359     /* 10: x, 60: y */
360     SetUpButton("向上 ", 10, 60, btnViewGroup, requestFocusByDirectionUpListener_);
361     /* 150: x, 60: y */
362     SetUpButton("向下 ", 150, 60, btnViewGroup, requestFocusByDirectionDownListener_);
363     /* 10: x, 110: y */
364     SetUpButton("2可获焦 ", 10, 110, btnViewGroup, setFocusableViewListener_);
365     /* 150: x, 110: y */
366     SetUpButton("2不可获焦 ", 150, 110, btnViewGroup, setFocusableViewListener1_);
367     /* 10: x, 160: y */
368     SetUpButton("设置4容器拦截 ", 10, 160, btnViewGroup, setGroupInterceptListener_);
369     /* 150: x, 160: y */
370     SetUpButton("取消4容器拦截 ", 150, 160, btnViewGroup, setGroupInterceptListener1_);
371     /* 10: x, 210: y */
372     SetUpButton("重置焦点 ", 10, 210, btnViewGroup, resetFocusListener_);
373     /* 150: x, 210: y */
374     SetUpButton("清除焦点 ", 150, 210, btnViewGroup, clearFocusListener_);
375 
376     FocusManager::GetInstance()->RequestFocus(viewGroup1->GetChildById("1"));
377 }
378 
SetUpButton(const char * title,int16_t x,int16_t y,UIViewGroup * viewGroup,UIView::OnClickListener * listener)379 UILabelButton* UITestFocusManager::SetUpButton(const char* title, int16_t x, int16_t y, UIViewGroup* viewGroup,
380     UIView::OnClickListener* listener)
381 {
382     UILabelButton* btn = new UILabelButton();
383     btn->SetPosition(x, y, BUTTON_WIDHT2, BUTTON_HEIGHT1);
384     btn->SetText(title);
385     btn->SetFont(DEFAULT_VECTOR_FONT_FILENAME, BUTTON_LABEL_SIZE);
386     btn->SetOnClickListener(listener);
387     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::RELEASED);
388     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::PRESSED);
389     btn->SetStyleForState(STYLE_BORDER_RADIUS, BUTTON_STYLE_BORDER_RADIUS_VALUE, UIButton::INACTIVE);
390     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::RELEASED);
391     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::PRESSED);
392     btn->SetStyleForState(STYLE_BACKGROUND_COLOR, BUTTON_STYLE_BACKGROUND_COLOR_VALUE, UIButton::INACTIVE);
393     viewGroup->Add(btn);
394     return btn;
395 }
396 } // namespace OHOS
397 #endif