event_bubble_unit_test.cpp 13.5 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if ENABLE_DEBUG
P
pssea 已提交
17 18 19 20
#include <climits>
#include <gtest/gtest.h>
#include <unistd.h>

M
mamingshuai 已提交
21
#include "common/graphic_startup.h"
P
pssea 已提交
22
#include "common/input_device_manager.h"
M
mamingshuai 已提交
23 24
#include "common/screen.h"
#include "common/task_manager.h"
P
pssea 已提交
25
#include "components/root_view.h"
M
mamingshuai 已提交
26 27
#include "components/ui_label_button.h"
#include "components/ui_scroll_view.h"
P
pssea 已提交
28 29
#include "core/render_manager.h"
#include "dock/screen_device_proxy.h"
M
mamingshuai 已提交
30
#include "font/ui_font.h"
P
pssea 已提交
31
#include "gfx_utils/file.h"
Z
zhangguyuan 已提交
32
#include "gfx_utils/graphic_log.h"
P
pssea 已提交
33
#include "graphic_config.h"
M
mamingshuai 已提交
34 35 36 37 38
#include "imgdecode/cache_manager.h"
#include "layout/grid_layout.h"

using namespace testing::ext;

P
pssea 已提交
39
enum TestEventFlag { FLAG0, FLAG1 };
M
mamingshuai 已提交
40 41 42 43 44 45 46 47 48

namespace {
uint8_t REGISTER_POINT_FLAG = FLAG0;
uint8_t REGISTER_KEY_FLAG = FLAG0;
uint8_t CLICK_FLAG = FLAG0;
uint8_t LONG_PRESS_FLAG = FLAG0;
uint8_t PRESS_FLAG = FLAG0;
uint8_t DRAG_FLAG = FLAG0;
uint8_t KEY_FLAG = FLAG0;
P
pssea 已提交
49
} // namespace
M
mamingshuai 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

namespace OHOS {
class TestEventBubbleView : public UIView, public RootView::OnKeyActListener {
public:
    bool OnLongPressEvent(const LongPressEvent& event) override
    {
        longPressEventFlag_ = FLAG1;
        return true;
    }

    bool OnClickEvent(const ClickEvent& event) override
    {
        clickEventFlag_ = FLAG1;
        return true;
    }

    bool OnDragEvent(const DragEvent& event) override
    {
        dragEventFlag_ = FLAG1;
        return true;
    }

    bool OnKeyAct(UIView& view, const KeyEvent& event) override
    {
        KEY_FLAG = FLAG1;
        return true;
    }

private:
    bool longPressEventFlag_;
    bool clickEventFlag_;
    bool dragEventFlag_;
};

class EventBubbleTest : public testing::Test {
public:
    static void SetUpTestCase(void);
    static void TearDownTestCase(void);
    static void TestApp();
P
pssea 已提交
89
    static void SetUpTestview(TestEventBubbleView* testView, bool touchable, bool draggable);
M
mamingshuai 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    static void DeleteChildren(UIView* view);

    static RootView* rootView_;
    static GridLayout* layout_;
    static TestEventBubbleView* clickView_;
    static TestEventBubbleView* dragView_;
    static TestEventBubbleView* longPressView_;
    static TestEventBubbleView* keyView_;
    static TestEventBubbleView* unTouchView_;
};

class TestOnClickListener : public UIView::OnClickListener {
public:
    explicit TestOnClickListener(bool isConsume) : isConsume_(isConsume) {}
    virtual ~TestOnClickListener() {}
    virtual bool OnClick(UIView& view, const ClickEvent& event)
    {
        CLICK_FLAG = FLAG1;
        return isConsume_;
    }

private:
    bool isConsume_;
};

class TestOnLongPressListener : public UIView::OnLongPressListener {
public:
    explicit TestOnLongPressListener(bool isConsume) : isConsume_(isConsume) {}
    virtual ~TestOnLongPressListener() {}
    virtual bool OnLongPress(UIView& view, const LongPressEvent& event)
    {
        LONG_PRESS_FLAG = FLAG1;
        return isConsume_;
    }

private:
    bool isConsume_;
};

class TestOnTouchListener : public UIView::OnTouchListener {
public:
P
pssea 已提交
131
    explicit TestOnTouchListener(bool isConsume) : isConsume_(isConsume) {}
M
mamingshuai 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
    virtual ~TestOnTouchListener() {}
    virtual bool OnPress(UIView& view, const PressEvent& event)
    {
        PRESS_FLAG = FLAG1;
        return isConsume_;
    }

private:
    bool isConsume_;
};

class TestOnDragListener : public UIView::OnDragListener {
public:
P
pssea 已提交
145
    explicit TestOnDragListener(bool isConsume) : isConsume_(isConsume) {}
M
mamingshuai 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    virtual ~TestOnDragListener() {}
    virtual bool OnDrag(DragEvent& event)
    {
        DRAG_FLAG = FLAG1;
        return isConsume_;
    }

private:
    bool isConsume_;
};

RootView* EventBubbleTest::rootView_ = nullptr;
GridLayout* EventBubbleTest::layout_ = nullptr;
TestEventBubbleView* EventBubbleTest::clickView_ = nullptr;
TestEventBubbleView* EventBubbleTest::dragView_ = nullptr;
TestEventBubbleView* EventBubbleTest::longPressView_ = nullptr;
TestEventBubbleView* EventBubbleTest::keyView_ = nullptr;
TestEventBubbleView* EventBubbleTest::unTouchView_ = nullptr;


void EventBubbleTest::SetUpTestCase(void)
{
    GraphicStartUp::Init();
    TestApp();
}

void EventBubbleTest::TestApp()
{
    rootView_ = RootView::GetInstance();
    rootView_->SetTouchable(true);
    rootView_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
    layout_ = new GridLayout();
    layout_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
    rootView_->Add(layout_);
    layout_->SetLayoutDirection(LAYOUT_VER);
    layout_->SetRows(6); /* 6:rows */
    layout_->SetCols(1);

    clickView_ = new TestEventBubbleView();
    SetUpTestview(clickView_, true, false);

    longPressView_ = new TestEventBubbleView();
    SetUpTestview(longPressView_, true, false);

    dragView_ = new TestEventBubbleView();
    SetUpTestview(dragView_, true, true);

    unTouchView_ = new TestEventBubbleView();
    SetUpTestview(unTouchView_, false, false);

    keyView_ = new TestEventBubbleView();
    RootView::GetInstance()->SetOnKeyActListener(keyView_);
    SetUpTestview(keyView_, true, false);

    layout_->LayoutChildren();
    rootView_->Invalidate();
}

P
pssea 已提交
204
void EventBubbleTest::SetUpTestview(TestEventBubbleView* testView, bool touchable, bool draggable)
M
mamingshuai 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
{
    layout_->Add(testView);
    testView->Resize(HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION / 7); /* 7:ratio */
    testView->SetTouchable(touchable);
    testView->SetDraggable(draggable);
}

void EventBubbleTest::DeleteChildren(UIView* view)
{
    if (view == nullptr) {
        return;
    }
    while (view != nullptr) {
        UIView* tempView = view;
        view = view->GetNextSibling();
        if (tempView->IsViewGroup()) {
            DeleteChildren(static_cast<UIViewGroup*>(tempView)->GetChildrenHead());
        }
        if (tempView->GetParent()) {
            static_cast<UIViewGroup*>(tempView->GetParent())->Remove(tempView);
        }
        delete tempView;
    }
}

void EventBubbleTest::TearDownTestCase(void)
{
    DeleteChildren(layout_);
    layout_ = nullptr;
P
pssea 已提交
234
    RootView::GetInstance()->ClearOnKeyActListener();
M
mamingshuai 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
}

/**
 * @tc.name: Graphic_EventBubbleTest_Test_GetExtraMsg_SetExtraMsg_007
 * @tc.desc: Verify GetExtraMsg, SetExtraMsg function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F4E5C
 */
HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_GetExtraMsg_SetExtraMsg_007, TestSize.Level0)
{
    /* test for GetExtraMsg */
    TestEventBubbleView* view = new TestEventBubbleView();
    EXPECT_EQ(view->GetExtraMsg(), nullptr);

    /* test for SetExtraMsg */
P
pssea 已提交
250
    UIView::ViewExtraMsg* extraMsg = new UIView::ViewExtraMsg();
M
mamingshuai 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    extraMsg->elementPtr = malloc(sizeof(char));
    view->SetExtraMsg(extraMsg);
    EXPECT_EQ(view->GetExtraMsg()->elementPtr, extraMsg->elementPtr);
    free(extraMsg->elementPtr);
    delete extraMsg;
    delete view;
}

/**
 * @tc.name: Graphic_EventBubbleTest_Test_UIView_GetTargetView_008
 * @tc.desc: Verify UIView::GetTargetView equal.
 * @tc.type: FUNC
 * @tc.require: AR000F4E5C
 */
HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIViewGroup_GetTargetView_008, TestSize.Level0)
{
    if ((clickView_ == nullptr) || (rootView_ == nullptr)) {
        return;
    }
    /* test for touchable view */
    /* 2:ratio, 2:ratio */
    Point clickPoint = {(int16_t)(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
                        (int16_t)(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
    UIView* curView = nullptr;
    UIView* targetView = nullptr;
    rootView_->GetTargetView(clickPoint, &curView, &targetView);
    EXPECT_EQ(curView, clickView_);
    EXPECT_EQ(targetView, clickView_);

    /* test for unTouchable view */
    if (unTouchView_ == nullptr) {
        return;
    }
    /* 2:ratio, 2:ratio */
    Point clickPoint2 = {(int16_t)(unTouchView_->GetRect().GetX() + unTouchView_->GetWidth() / 2),
                        (int16_t)(unTouchView_->GetRect().GetY() + unTouchView_->GetHeight() / 2)};
    curView = nullptr;
    targetView = nullptr;
    rootView_->GetTargetView(clickPoint2, &curView, &targetView);

    EXPECT_EQ(curView, rootView_);
    EXPECT_EQ(targetView, unTouchView_);
}

/**
 * @tc.name: Graphic_EventBubbleTest_Test_UIView_GetTargetView_009
 * @tc.desc: Verify UIView::GetTargetView equal.
 * @tc.type: FUNC
 * @tc.require: AR000F4E5C
 */
邓志豪 已提交
301
HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_GetTargetView_009, TestSize.Level1)
M
mamingshuai 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
{
    if ((clickView_ == nullptr) || (rootView_ == nullptr)) {
        return;
    }
    /* test for touchable view */
    /* 2:ratio, 2:ratio */
    Point clickPoint = {(int16_t)(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
                        (int16_t)(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
    UIView* curView = nullptr;
    UIView* targetView = nullptr;
    clickView_->GetTargetView(clickPoint, &curView, &targetView);
    EXPECT_EQ(curView, clickView_);
    EXPECT_EQ(targetView, clickView_);

    /* test for unTouchable view */
    if (unTouchView_ == nullptr) {
        return;
    }
    /* 2:ratio, 2:ratio */
    Point clickPoint2 = {(int16_t)(unTouchView_->GetRect().GetX() + unTouchView_->GetWidth() / 2),
                        (int16_t)(unTouchView_->GetRect().GetY() + unTouchView_->GetHeight() / 2)};
    curView = nullptr;
    targetView = nullptr;
    unTouchView_->GetTargetView(clickPoint2, &curView, &targetView);

    EXPECT_EQ(curView, nullptr);
    EXPECT_EQ(targetView, unTouchView_);
}

/**
 * @tc.name: Graphic_EventBubbleTest_Test_UIView_OnClick_010
 * @tc.desc: Verify UIView::OnClick equal.
 * @tc.type: FUNC
 * @tc.require: SR000F3PEA
 */
HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_OnClick_010, TestSize.Level0)
{
    /* 2:ratio, 2:ratio */
    Point clickPoint = {(int16_t)(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
                        (int16_t)(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
    ClickEvent clickEvent(clickPoint);

    /* test for not comsumed view */
    TestOnClickListener* clickListener = new TestOnClickListener(false);
    bool ret = clickListener->OnClick(*clickView_, clickEvent);
    EXPECT_EQ(ret, false);
    EXPECT_EQ(CLICK_FLAG, true);
    delete clickListener;

    /* test for comsumed view */
    CLICK_FLAG = false;
    TestOnClickListener* clickListener2 = new TestOnClickListener(true);
    ret = clickListener->OnClick(*clickView_, clickEvent);
    EXPECT_EQ(ret, true);
    EXPECT_EQ(CLICK_FLAG, true);
    delete clickListener2;
}

/**
 * @tc.name: Graphic_EventBubbleTest_Test_UIView_OnLongPress_011
 * @tc.desc: Verify UIView::OnLongPress equal.
 * @tc.type: FUNC
 * @tc.require: AR000F4E5C
 */
HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_OnLongPress_011, TestSize.Level0)
{
    /* 2:ratio, 2:ratio */
    Point longPressPoint = {(int16_t)(longPressView_->GetRect().GetX() + longPressView_->GetWidth() / 2),
                        (int16_t)(longPressView_->GetRect().GetY() + longPressView_->GetHeight() / 2)};
    uint32_t pressTimeStamp = 2; /* 2:second */
    LongPressEvent longPressEvent(longPressPoint, pressTimeStamp);

    /* test for not comsumed view */
    TestOnLongPressListener* longPressListener = new TestOnLongPressListener(false);
    bool ret = longPressListener->OnLongPress(*clickView_, longPressEvent);
    EXPECT_EQ(ret, false);
    EXPECT_EQ(LONG_PRESS_FLAG, true);
    delete longPressListener;

    /* test for comsumed view */
    LONG_PRESS_FLAG = false;
    TestOnLongPressListener* longPressListener2 = new TestOnLongPressListener(true);
    ret = longPressListener2->OnLongPress(*clickView_, longPressEvent);
    EXPECT_EQ(ret, true);
    EXPECT_EQ(LONG_PRESS_FLAG, true);
    delete longPressListener2;
}

/**
 * @tc.name: Graphic_EventBubbleTest_Test_UIView_OnDrag_012
 * @tc.desc: Verify UIView::OnDrag equal.
 * @tc.type: FUNC
 * @tc.require: AR000F4E5C
 */
HWTEST_F(EventBubbleTest, Graphic_EventBubbleTest_Test_UIView_OnDrag_012, TestSize.Level0)
{
    /* 2:ratio, 2:ratio */
    Point dragCurPoint = {(int16_t)(dragView_->GetRect().GetX() + dragView_->GetWidth() / 2),
                        (int16_t)(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    /* 3:ratio, 3:ratio */
    Point dragLastPoint = {(int16_t)(dragView_->GetRect().GetX() + dragView_->GetWidth() / 3),
                        (int16_t)(dragView_->GetRect().GetY() + dragView_->GetHeight() / 3)};
    Point dragLen;
    dragLen.x = dragCurPoint.x - dragLastPoint.x;
    dragLen.y = dragCurPoint.y - dragLastPoint.y;
    DragEvent dragEvent(dragCurPoint, dragLastPoint, dragLen);

    /* test for not comsumed view */
    TestOnDragListener* dragListener = new TestOnDragListener(false);
    bool ret = dragListener->OnDrag(dragEvent);
    EXPECT_EQ(ret, false);
    EXPECT_EQ(DRAG_FLAG, true);
    delete dragListener;

    /* test for comsumed view */
    LONG_PRESS_FLAG = false;
    TestOnDragListener* dragListener2 = new TestOnDragListener(true);
    ret = dragListener2->OnDrag(dragEvent);
    EXPECT_EQ(ret, true);
    EXPECT_EQ(DRAG_FLAG, true);
    delete dragListener2;
}
} // namespace OHOS
#endif // ENABLE_DEBUG