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 #if ENABLE_DEBUG
17 #include <climits>
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20 
21 #include "common/graphic_startup.h"
22 #include "common/input_device_manager.h"
23 #include "common/screen.h"
24 #include "common/task_manager.h"
25 #include "components/root_view.h"
26 #include "components/ui_label_button.h"
27 #include "components/ui_scroll_view.h"
28 #include "core/render_manager.h"
29 #include "dock/screen_device_proxy.h"
30 #include "font/ui_font.h"
31 #include "gfx_utils/file.h"
32 #include "gfx_utils/graphic_log.h"
33 #include "graphic_config.h"
34 #include "imgdecode/cache_manager.h"
35 #include "layout/grid_layout.h"
36 
37 using namespace testing::ext;
38 
39 enum TestEventFlag { FLAG0, FLAG1 };
40 
41 namespace {
42 uint8_t REGISTER_POINT_FLAG = FLAG0;
43 uint8_t REGISTER_KEY_FLAG = FLAG0;
44 uint8_t CLICK_FLAG = FLAG0;
45 uint8_t LONG_PRESS_FLAG = FLAG0;
46 uint8_t PRESS_FLAG = FLAG0;
47 uint8_t DRAG_FLAG = FLAG0;
48 uint8_t KEY_FLAG = FLAG0;
49 } // namespace
50 
51 namespace OHOS {
52 class TestEventBubbleView : public UIView, public RootView::OnKeyActListener {
53 public:
OnLongPressEvent(const LongPressEvent & event)54     bool OnLongPressEvent(const LongPressEvent& event) override
55     {
56         longPressEventFlag_ = FLAG1;
57         return true;
58     }
59 
OnClickEvent(const ClickEvent & event)60     bool OnClickEvent(const ClickEvent& event) override
61     {
62         clickEventFlag_ = FLAG1;
63         return true;
64     }
65 
OnDragEvent(const DragEvent & event)66     bool OnDragEvent(const DragEvent& event) override
67     {
68         dragEventFlag_ = FLAG1;
69         return true;
70     }
71 
OnKeyAct(UIView & view,const KeyEvent & event)72     bool OnKeyAct(UIView& view, const KeyEvent& event) override
73     {
74         KEY_FLAG = FLAG1;
75         return true;
76     }
77 
78 private:
79     bool longPressEventFlag_;
80     bool clickEventFlag_;
81     bool dragEventFlag_;
82 };
83 
84 class EventBubbleTest : public testing::Test {
85 public:
86     static void SetUpTestCase(void);
87     static void TearDownTestCase(void);
88     static void TestApp();
89     static void SetUpTestview(TestEventBubbleView* testView, bool touchable, bool draggable);
90     static void DeleteChildren(UIView* view);
91 
92     static RootView* rootView_;
93     static GridLayout* layout_;
94     static TestEventBubbleView* clickView_;
95     static TestEventBubbleView* dragView_;
96     static TestEventBubbleView* longPressView_;
97     static TestEventBubbleView* keyView_;
98     static TestEventBubbleView* unTouchView_;
99 };
100 
101 class TestOnClickListener : public UIView::OnClickListener {
102 public:
TestOnClickListener(bool isConsume)103     explicit TestOnClickListener(bool isConsume) : isConsume_(isConsume) {}
~TestOnClickListener()104     virtual ~TestOnClickListener() {}
OnClick(UIView & view,const ClickEvent & event)105     virtual bool OnClick(UIView& view, const ClickEvent& event)
106     {
107         CLICK_FLAG = FLAG1;
108         return isConsume_;
109     }
110 
111 private:
112     bool isConsume_;
113 };
114 
115 class TestOnLongPressListener : public UIView::OnLongPressListener {
116 public:
TestOnLongPressListener(bool isConsume)117     explicit TestOnLongPressListener(bool isConsume) : isConsume_(isConsume) {}
~TestOnLongPressListener()118     virtual ~TestOnLongPressListener() {}
OnLongPress(UIView & view,const LongPressEvent & event)119     virtual bool OnLongPress(UIView& view, const LongPressEvent& event)
120     {
121         LONG_PRESS_FLAG = FLAG1;
122         return isConsume_;
123     }
124 
125 private:
126     bool isConsume_;
127 };
128 
129 class TestOnTouchListener : public UIView::OnTouchListener {
130 public:
TestOnTouchListener(bool isConsume)131     explicit TestOnTouchListener(bool isConsume) : isConsume_(isConsume) {}
~TestOnTouchListener()132     virtual ~TestOnTouchListener() {}
OnPress(UIView & view,const PressEvent & event)133     virtual bool OnPress(UIView& view, const PressEvent& event)
134     {
135         PRESS_FLAG = FLAG1;
136         return isConsume_;
137     }
138 
139 private:
140     bool isConsume_;
141 };
142 
143 class TestOnDragListener : public UIView::OnDragListener {
144 public:
TestOnDragListener(bool isConsume)145     explicit TestOnDragListener(bool isConsume) : isConsume_(isConsume) {}
~TestOnDragListener()146     virtual ~TestOnDragListener() {}
OnDrag(DragEvent & event)147     virtual bool OnDrag(DragEvent& event)
148     {
149         DRAG_FLAG = FLAG1;
150         return isConsume_;
151     }
152 
153 private:
154     bool isConsume_;
155 };
156 
157 RootView* EventBubbleTest::rootView_ = nullptr;
158 GridLayout* EventBubbleTest::layout_ = nullptr;
159 TestEventBubbleView* EventBubbleTest::clickView_ = nullptr;
160 TestEventBubbleView* EventBubbleTest::dragView_ = nullptr;
161 TestEventBubbleView* EventBubbleTest::longPressView_ = nullptr;
162 TestEventBubbleView* EventBubbleTest::keyView_ = nullptr;
163 TestEventBubbleView* EventBubbleTest::unTouchView_ = nullptr;
164 
165 
SetUpTestCase(void)166 void EventBubbleTest::SetUpTestCase(void)
167 {
168     GraphicStartUp::Init();
169     TestApp();
170 }
171 
TestApp()172 void EventBubbleTest::TestApp()
173 {
174     rootView_ = RootView::GetInstance();
175     rootView_->SetTouchable(true);
176     rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
177     layout_ = new GridLayout();
178     layout_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
179     rootView_->Add(layout_);
180     layout_->SetLayoutDirection(LAYOUT_VER);
181     layout_->SetRows(6); /* 6:rows */
182     layout_->SetCols(1);
183 
184     clickView_ = new TestEventBubbleView();
185     SetUpTestview(clickView_, true, false);
186 
187     longPressView_ = new TestEventBubbleView();
188     SetUpTestview(longPressView_, true, false);
189 
190     dragView_ = new TestEventBubbleView();
191     SetUpTestview(dragView_, true, true);
192 
193     unTouchView_ = new TestEventBubbleView();
194     SetUpTestview(unTouchView_, false, false);
195 
196     keyView_ = new TestEventBubbleView();
197     RootView::GetInstance()->SetOnKeyActListener(keyView_);
198     SetUpTestview(keyView_, true, false);
199 
200     layout_->LayoutChildren();
201     rootView_->Invalidate();
202 }
203 
SetUpTestview(TestEventBubbleView * testView,bool touchable,bool draggable)204 void EventBubbleTest::SetUpTestview(TestEventBubbleView* testView, bool touchable, bool draggable)
205 {
206     layout_->Add(testView);
207     testView->Resize(HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION / 7); /* 7:ratio */
208     testView->SetTouchable(touchable);
209     testView->SetDraggable(draggable);
210 }
211 
DeleteChildren(UIView * view)212 void EventBubbleTest::DeleteChildren(UIView* view)
213 {
214     if (view == nullptr) {
215         return;
216     }
217     while (view != nullptr) {
218         UIView* tempView = view;
219         view = view->GetNextSibling();
220         if (tempView->IsViewGroup()) {
221             DeleteChildren(static_cast<UIViewGroup*>(tempView)->GetChildrenHead());
222         }
223         if (tempView->GetParent()) {
224             static_cast<UIViewGroup*>(tempView->GetParent())->Remove(tempView);
225         }
226         delete tempView;
227     }
228 }
229 
TearDownTestCase(void)230 void EventBubbleTest::TearDownTestCase(void)
231 {
232     DeleteChildren(layout_);
233     layout_ = nullptr;
234     RootView::GetInstance()->ClearOnKeyActListener();
235 }
236 
237 /**
238  * @tc.name: Graphic_EventBubbleTest_Test_GetExtraMsg_SetExtraMsg_007
239  * @tc.desc: Verify GetExtraMsg, SetExtraMsg function, equal.
240  * @tc.type: FUNC
241  * @tc.require: AR000F4E5C
242  */
243 HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_GetExtraMsg_SetExtraMsg_007, TestSize.Level0)
244 {
245     /* test for GetExtraMsg */
246     TestEventBubbleView* view = new TestEventBubbleView();
247     EXPECT_EQ(view->GetExtraMsg(), nullptr);
248 
249     /* test for SetExtraMsg */
250     UIView::ViewExtraMsg* extraMsg = new UIView::ViewExtraMsg();
251     extraMsg->elementPtr = malloc(sizeof(char));
252     view->SetExtraMsg(extraMsg);
253     EXPECT_EQ(view->GetExtraMsg()->elementPtr, extraMsg->elementPtr);
254     free(extraMsg->elementPtr);
255     delete extraMsg;
256     delete view;
257 }
258 
259 /**
260  * @tc.name: Graphic_EventBubbleTest_Test_UIView_GetTargetView_008
261  * @tc.desc: Verify UIView::GetTargetView equal.
262  * @tc.type: FUNC
263  * @tc.require: AR000F4E5C
264  */
265 HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIViewGroup_GetTargetView_008, TestSize.Level0)
266 {
267     if ((clickView_ == nullptr) || (rootView_ == nullptr)) {
268         return;
269     }
270     /* test for touchable view */
271     /* 2:ratio, 2:ratio */
272     Point clickPoint = {(int16_t)(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
273                         (int16_t)(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
274     UIView* curView = nullptr;
275     UIView* targetView = nullptr;
276     rootView_->GetTargetView(clickPoint, &curView, &targetView);
277     EXPECT_EQ(curView, clickView_);
278     EXPECT_EQ(targetView, clickView_);
279 
280     /* test for unTouchable view */
281     if (unTouchView_ == nullptr) {
282         return;
283     }
284     /* 2:ratio, 2:ratio */
285     Point clickPoint2 = {(int16_t)(unTouchView_->GetRect().GetX() + unTouchView_->GetWidth() / 2),
286                         (int16_t)(unTouchView_->GetRect().GetY() + unTouchView_->GetHeight() / 2)};
287     curView = nullptr;
288     targetView = nullptr;
289     rootView_->GetTargetView(clickPoint2, &curView, &targetView);
290 
291     EXPECT_EQ(curView, rootView_);
292     EXPECT_EQ(targetView, unTouchView_);
293 }
294 
295 /**
296  * @tc.name: Graphic_EventBubbleTest_Test_UIView_GetTargetView_009
297  * @tc.desc: Verify UIView::GetTargetView equal.
298  * @tc.type: FUNC
299  * @tc.require: AR000F4E5C
300  */
301 HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_GetTargetView_009, TestSize.Level1)
302 {
303     if ((clickView_ == nullptr) || (rootView_ == nullptr)) {
304         return;
305     }
306     /* test for touchable view */
307     /* 2:ratio, 2:ratio */
308     Point clickPoint = {(int16_t)(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
309                         (int16_t)(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
310     UIView* curView = nullptr;
311     UIView* targetView = nullptr;
312     clickView_->GetTargetView(clickPoint, &curView, &targetView);
313     EXPECT_EQ(curView, clickView_);
314     EXPECT_EQ(targetView, clickView_);
315 
316     /* test for unTouchable view */
317     if (unTouchView_ == nullptr) {
318         return;
319     }
320     /* 2:ratio, 2:ratio */
321     Point clickPoint2 = {(int16_t)(unTouchView_->GetRect().GetX() + unTouchView_->GetWidth() / 2),
322                         (int16_t)(unTouchView_->GetRect().GetY() + unTouchView_->GetHeight() / 2)};
323     curView = nullptr;
324     targetView = nullptr;
325     unTouchView_->GetTargetView(clickPoint2, &curView, &targetView);
326 
327     EXPECT_EQ(curView, nullptr);
328     EXPECT_EQ(targetView, unTouchView_);
329 }
330 
331 /**
332  * @tc.name: Graphic_EventBubbleTest_Test_UIView_OnClick_010
333  * @tc.desc: Verify UIView::OnClick equal.
334  * @tc.type: FUNC
335  * @tc.require: SR000F3PEA
336  */
337 HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_OnClick_010, TestSize.Level0)
338 {
339     /* 2:ratio, 2:ratio */
340     Point clickPoint = {(int16_t)(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
341                         (int16_t)(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
342     ClickEvent clickEvent(clickPoint);
343 
344     /* test for not comsumed view */
345     TestOnClickListener* clickListener = new TestOnClickListener(false);
346     bool ret = clickListener->OnClick(*clickView_, clickEvent);
347     EXPECT_EQ(ret, false);
348     EXPECT_EQ(CLICK_FLAG, true);
349     delete clickListener;
350 
351     /* test for comsumed view */
352     CLICK_FLAG = false;
353     TestOnClickListener* clickListener2 = new TestOnClickListener(true);
354     ret = clickListener->OnClick(*clickView_, clickEvent);
355     EXPECT_EQ(ret, true);
356     EXPECT_EQ(CLICK_FLAG, true);
357     delete clickListener2;
358 }
359 
360 /**
361  * @tc.name: Graphic_EventBubbleTest_Test_UIView_OnLongPress_011
362  * @tc.desc: Verify UIView::OnLongPress equal.
363  * @tc.type: FUNC
364  * @tc.require: AR000F4E5C
365  */
366 HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_OnLongPress_011, TestSize.Level0)
367 {
368     /* 2:ratio, 2:ratio */
369     Point longPressPoint = {(int16_t)(longPressView_->GetRect().GetX() + longPressView_->GetWidth() / 2),
370                         (int16_t)(longPressView_->GetRect().GetY() + longPressView_->GetHeight() / 2)};
371     uint32_t pressTimeStamp = 2; /* 2:second */
372     LongPressEvent longPressEvent(longPressPoint, pressTimeStamp);
373 
374     /* test for not comsumed view */
375     TestOnLongPressListener* longPressListener = new TestOnLongPressListener(false);
376     bool ret = longPressListener->OnLongPress(*clickView_, longPressEvent);
377     EXPECT_EQ(ret, false);
378     EXPECT_EQ(LONG_PRESS_FLAG, true);
379     delete longPressListener;
380 
381     /* test for comsumed view */
382     LONG_PRESS_FLAG = false;
383     TestOnLongPressListener* longPressListener2 = new TestOnLongPressListener(true);
384     ret = longPressListener2->OnLongPress(*clickView_, longPressEvent);
385     EXPECT_EQ(ret, true);
386     EXPECT_EQ(LONG_PRESS_FLAG, true);
387     delete longPressListener2;
388 }
389 
390 /**
391  * @tc.name: Graphic_EventBubbleTest_Test_UIView_OnDrag_012
392  * @tc.desc: Verify UIView::OnDrag equal.
393  * @tc.type: FUNC
394  * @tc.require: AR000F4E5C
395  */
396 HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_OnDrag_012, TestSize.Level0)
397 {
398     /* 2:ratio, 2:ratio */
399     Point dragCurPoint = {(int16_t)(dragView_->GetRect().GetX() + dragView_->GetWidth() / 2),
400                         (int16_t)(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
401     /* 3:ratio, 3:ratio */
402     Point dragLastPoint = {(int16_t)(dragView_->GetRect().GetX() + dragView_->GetWidth() / 3),
403                         (int16_t)(dragView_->GetRect().GetY() + dragView_->GetHeight() / 3)};
404     Point dragLen;
405     dragLen.x = dragCurPoint.x - dragLastPoint.x;
406     dragLen.y = dragCurPoint.y - dragLastPoint.y;
407     DragEvent dragEvent(dragCurPoint, dragLastPoint, dragLen);
408 
409     /* test for not comsumed view */
410     TestOnDragListener* dragListener = new TestOnDragListener(false);
411     bool ret = dragListener->OnDrag(dragEvent);
412     EXPECT_EQ(ret, false);
413     EXPECT_EQ(DRAG_FLAG, true);
414     delete dragListener;
415 
416     /* test for comsumed view */
417     LONG_PRESS_FLAG = false;
418     TestOnDragListener* dragListener2 = new TestOnDragListener(true);
419     ret = dragListener2->OnDrag(dragEvent);
420     EXPECT_EQ(ret, true);
421     EXPECT_EQ(DRAG_FLAG, true);
422     delete dragListener2;
423 }
424 } // namespace OHOS
425 #endif // ENABLE_DEBUG
426