event_injector_unit_test.cpp 14.7 KB
Newer Older
M
mamingshuai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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.
 */

#include "dfx/event_injector.h"

#if ENABLE_DEBUG
P
pssea 已提交
19 20 21 22 23
#include <climits>
#include <gtest/gtest.h>
#include <pthread.h>
#include <unistd.h>

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

using namespace testing::ext;

P
pssea 已提交
43
enum TestEventInjectorFlag { FLAG0, FLAG1 };
M
mamingshuai 已提交
44 45 46 47 48 49 50 51 52 53 54

namespace {
uint8_t REGISTER_POINT_FLAG = FLAG0;
uint8_t REGISTER_KEY_FLAG = FLAG0;
uint8_t UNREGISTER_POINT_FLAG = FLAG0;
uint8_t UNREGISTER_KEY_FLAG = FLAG0;
uint8_t CLICK_FLAG = FLAG0;
uint8_t LONG_PRESS_FLAG = FLAG0;
uint8_t DRAG_FLAG = FLAG0;
uint8_t KEY_FLAG = FLAG0;
uint8_t MAX_LOOP = 200;
P
pssea 已提交
55
} // namespace
M
mamingshuai 已提交
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 89 90 91 92 93 94 95

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

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

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

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

class EventInjectorTest : public testing::Test {
public:
    static void SetUpTestCase(void);
    static void TearDownTestCase(void);
    static void TestApp();
    static void SetUpTestview(TestEventInjectorView* testView, bool touchable, bool draggable);
    static void* MainTask(void* args);
    static void DeleteChildren(UIView* view);

    static pthread_t mainTaskThread_;
    static bool isRepeat_;
P
pssea 已提交
96
    static RootView* rootView_;
M
mamingshuai 已提交
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 204 205 206 207 208 209 210 211 212
    static GridLayout* layout_;
    static TestEventInjectorView* clickView_;
    static TestEventInjectorView* dragView_;
    static TestEventInjectorView* longPressView_;
    static TestEventInjectorView* keyView_;
    static Window* window_;
};

pthread_t EventInjectorTest::mainTaskThread_ = -1;
bool EventInjectorTest::isRepeat_ = true;
RootView* EventInjectorTest::rootView_ = nullptr;
GridLayout* EventInjectorTest::layout_ = nullptr;
TestEventInjectorView* EventInjectorTest::clickView_ = nullptr;
TestEventInjectorView* EventInjectorTest::dragView_ = nullptr;
TestEventInjectorView* EventInjectorTest::longPressView_ = nullptr;
TestEventInjectorView* EventInjectorTest::keyView_ = nullptr;
Window* EventInjectorTest::window_ = nullptr;

void EventInjectorTest::SetUpTestCase(void)
{
    GraphicStartUp::Init();
    TestApp();
    if (pthread_create(&mainTaskThread_, nullptr, MainTask, nullptr) != 0) {
        return;
    }
}

void* EventInjectorTest::MainTask(void* args)
{
    while (isRepeat_) {
        /* Periodically call TaskHandler(). It could be done in a timer interrupt or an OS task too. */
        OHOS::TaskManager::GetInstance()->TaskHandler();
        usleep(1000 * 10); /* 1000 * 10:10ms Just to let the system breathe */
    }
    return nullptr;
}

void EventInjectorTest::TestApp()
{
    WindowConfig config = {};
    config.rect.SetRect(0, 0, Screen::GetInstance().GetWidth() - 1, Screen::GetInstance().GetHeight() - 1);
    window_ = Window::CreateWindow(config);
    if (window_ == nullptr) {
        GRAPHIC_LOGE("Create window false!");
        return;
    }
    window_->BindRootView(RootView::GetInstance());

    if (EventInjector::GetInstance()->RegisterEventInjector(EventDataType::POINT_TYPE)) {
        REGISTER_POINT_FLAG = FLAG1;
    }
    if (EventInjector::GetInstance()->RegisterEventInjector(EventDataType::KEY_TYPE)) {
        REGISTER_KEY_FLAG = FLAG1;
    }
    EventInjector::GetInstance()->SetWindowId(window_->GetWindowId());
    rootView_ = RootView::GetInstance();
    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(4); /* 4:rows */
    layout_->SetCols(1);
    clickView_ = new TestEventInjectorView();
    SetUpTestview(clickView_, true, false);

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

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

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

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

void EventInjectorTest::SetUpTestview(TestEventInjectorView* testView, bool touchable, bool draggable)
{
    layout_->Add(testView);
    testView->Resize(HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION / 5); /* 5:ratio */
    testView->SetTouchable(touchable);
    testView->SetDraggable(draggable);
}

void EventInjectorTest::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 EventInjectorTest::TearDownTestCase(void)
{
    isRepeat_ = false;
    pthread_join(mainTaskThread_, nullptr);
    Window::DestoryWindow(window_);
    DeleteChildren(layout_);
    layout_ = nullptr;

    EventInjector::GetInstance()->UnregisterEventInjector(EventDataType::POINT_TYPE);
    EventInjector::GetInstance()->UnregisterEventInjector(EventDataType::KEY_TYPE);
P
pssea 已提交
213
    RootView::GetInstance()->ClearOnKeyActListener();
M
mamingshuai 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_RegisterEventInjector_001
 * @tc.desc: Verify RegisterEventInjector function, equal.
 * @tc.type: FUNC
 * @tc.require: SR000F74SS
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_RegisterEventInjector_001, TestSize.Level0)
{
    EXPECT_EQ(REGISTER_POINT_FLAG, FLAG1);
    EXPECT_EQ(REGISTER_KEY_FLAG, FLAG1);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetClickEvent_001
 * @tc.desc: Verify SetClickEvent function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetClickEvent_001, TestSize.Level2)
{
    if (clickView_ == nullptr) {
        ADD_FAILURE();
        return;
    }
    CLICK_FLAG = FLAG0;
    /* 2:ratio, 2:ratio */
    Point clickPoint = {static_cast<int16_t>(clickView_->GetRect().GetX() + clickView_->GetWidth() / 2),
                        static_cast<int16_t>(clickView_->GetRect().GetY() + clickView_->GetHeight() / 2)};
    bool ret = EventInjector::GetInstance()->SetClickEvent(clickPoint);
    EXPECT_EQ(ret, true);
    uint8_t loop = 0;
    while ((loop < MAX_LOOP) && !CLICK_FLAG) {
        loop++;
P
pssea 已提交
249
        usleep(10000); /* 10000:10ms */
M
mamingshuai 已提交
250 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
    }
    EXPECT_EQ(CLICK_FLAG, FLAG1);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetDragEvent_001
 * @tc.desc: Verify SetDragEvent function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetDragEvent_001, TestSize.Level2)
{
    if (dragView_ == nullptr) {
        ADD_FAILURE();
        return;
    }
    DRAG_FLAG = FLAG0;
    /* 4:ratio, 2:ratio */
    Point startPoint = {static_cast<int16_t>(dragView_->GetRect().GetX() + dragView_->GetWidth() / 4),
                        static_cast<int16_t>(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    /* 2:ratio, 2:ratio */
    Point endPoint = {static_cast<int16_t>(dragView_->GetRect().GetX() + dragView_->GetWidth() / 2),
                      static_cast<int16_t>(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    bool ret = EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, 100); /* 100:drag time (ms) */
    EXPECT_EQ(ret, true);
    uint8_t loop = 0;
    while ((loop < MAX_LOOP) && !DRAG_FLAG) {
        loop++;
P
pssea 已提交
278
        usleep(10000); /* 10000:10ms */
M
mamingshuai 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 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
    }
    EXPECT_EQ(DRAG_FLAG, FLAG1);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetDragEvent_002
 * @tc.desc: Verify SetDragEvent function, abnormal branch, SetDragEvent failure(dragTime is too short), equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetDragEvent_002, TestSize.Level0)
{
    if (dragView_ == nullptr) {
        ADD_FAILURE();
        return;
    }
    /* 4:ratio, 2:ratio */
    Point startPoint = {static_cast<int16_t>(dragView_->GetRect().GetX() + dragView_->GetWidth() / 4),
                        static_cast<int16_t>(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    /* 2:ratio, 2:ratio */
    Point endPoint = {static_cast<int16_t>(dragView_->GetRect().GetX() + dragView_->GetWidth() / 2),
                      static_cast<int16_t>(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    bool ret = EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, INDEV_READ_PERIOD);
    EXPECT_EQ(ret, false);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetDragEvent_003
 * @tc.desc: Verify SetDragEvent function, abnormal branch, SetDragEvent failure(dragTime is too long), equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetDragEvent_003, TestSize.Level0)
{
    if (dragView_ == nullptr) {
        ADD_FAILURE();
        return;
    }
    /* 4:ratio, 2:ratio */
    Point startPoint = {static_cast<int16_t>(dragView_->GetRect().GetX() + dragView_->GetWidth() / 4),
                        static_cast<int16_t>(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    /* 2:ratio, 2:ratio */
    Point endPoint = {static_cast<int16_t>(dragView_->GetRect().GetX() + dragView_->GetWidth() / 2),
                      static_cast<int16_t>(dragView_->GetRect().GetY() + dragView_->GetHeight() / 2)};
    bool ret = EventInjector::GetInstance()->SetDragEvent(startPoint, endPoint, 6000); /* 6000:drag time (ms) */
    EXPECT_EQ(ret, false);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetLongPressEvent_001
 * @tc.desc: Verify SetLongPressEvent function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetLongPressEvent_001, TestSize.Level2)
{
    if (longPressView_ == nullptr) {
        ADD_FAILURE();
        return;
    }
P
pssea 已提交
339
    sleep(2); /* 2:2s */
M
mamingshuai 已提交
340 341 342
    LONG_PRESS_FLAG = FLAG0;
    /* 2:ratio, 2:ratio */
    Point longPressPoint = {static_cast<int16_t>(longPressView_->GetRect().GetX() + longPressView_->GetWidth() / 2),
P
pssea 已提交
343
                            static_cast<int16_t>(longPressView_->GetRect().GetY() + longPressView_->GetHeight() / 2)};
M
mamingshuai 已提交
344 345 346 347 348
    bool ret = EventInjector::GetInstance()->SetLongPressEvent(longPressPoint);
    EXPECT_EQ(ret, true);
    uint8_t loop = 0;
    while ((loop < MAX_LOOP) && !LONG_PRESS_FLAG) {
        loop++;
P
pssea 已提交
349
        usleep(10000); /* 10000:10ms */
M
mamingshuai 已提交
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    }
    EXPECT_EQ(LONG_PRESS_FLAG, FLAG1);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetLongPressEvent_002
 * @tc.desc: Verify SetLongPressEvent function, abnormal branch, SetLongPressEvent failure, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetLongPressEvent_002, TestSize.Level0)
{
    if (longPressView_ == nullptr) {
        ADD_FAILURE();
        return;
    }
    /* 2:ratio, 2:ratio */
    Point longPressPoint = {static_cast<int16_t>(longPressView_->GetRect().GetX() + longPressView_->GetWidth() / 2),
P
pssea 已提交
368
                            static_cast<int16_t>(longPressView_->GetRect().GetY() + longPressView_->GetHeight() / 2)};
M
mamingshuai 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    /* 20:loop */
    for (uint8_t i = 0; i < 20; i++) {
        EventInjector::GetInstance()->SetLongPressEvent(longPressPoint);
    }
    bool ret = EventInjector::GetInstance()->SetLongPressEvent(longPressPoint);
    EXPECT_EQ(ret, false);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetKeyEvent_001
 * @tc.desc: Verify SetKeyEvent function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetKeyEvent_001, TestSize.Level2)
{
    /* 26:keyId */
    uint16_t keyId = 26;
    EventInjector::GetInstance()->SetKeyEvent(keyId, InputDevice::STATE_PRESS);
    uint8_t loop = 0;
    while ((loop < MAX_LOOP) && !KEY_FLAG) {
        loop++;
P
pssea 已提交
391
        usleep(10000); /* 10000:10ms */
M
mamingshuai 已提交
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 426 427 428 429 430 431 432
    }
    EXPECT_EQ(KEY_FLAG, FLAG1);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_SetKeyEvent_002
 * @tc.desc: Verify SetKeyEvent function, abnormal branch, SetKeyEvent failure, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_SetKeyEvent_002, TestSize.Level0)
{
    /* 26:keyId */
    uint16_t keyId = 26;
    /* 200:loop */
    for (uint8_t i = 0; i < 200; i++) {
        EventInjector::GetInstance()->SetKeyEvent(keyId, InputDevice::STATE_PRESS);
    }
    bool ret = EventInjector::GetInstance()->SetKeyEvent(keyId, InputDevice::STATE_PRESS);
    EXPECT_EQ(ret, false);
}

/**
 * @tc.name: Graphic_EventInjectorTest_Test_UnregisterEventInjector_001
 * @tc.desc: Verify UnregisterEventInjector function, equal.
 * @tc.type: FUNC
 * @tc.require: AR000F74ST
 */
HWTEST_F(EventInjectorTest, Graphic_EventInjectorTest_Test_UnregisterEventInjector_001, TestSize.Level0)
{
    EventInjector::GetInstance()->UnregisterEventInjector(EventDataType::POINT_TYPE);
    if (!EventInjector::GetInstance()->IsEventInjectorRegistered(EventDataType::POINT_TYPE)) {
        UNREGISTER_POINT_FLAG = FLAG1;
    }
    EventInjector::GetInstance()->UnregisterEventInjector(EventDataType::KEY_TYPE);
    if (!EventInjector::GetInstance()->IsEventInjectorRegistered(EventDataType::KEY_TYPE)) {
        UNREGISTER_KEY_FLAG = FLAG1;
    }
    EXPECT_EQ(UNREGISTER_POINT_FLAG, FLAG1);
    EXPECT_EQ(UNREGISTER_KEY_FLAG, FLAG1);
}
P
pssea 已提交
433
} // namespace OHOS
M
mamingshuai 已提交
434
#endif // ENABLE_DEBUG