1 /*
2 * Copyright (c) 2021-2022 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 <gtest/gtest.h>
17 #include "define_multimodal.h"
18 #include "error_multimodal.h"
19 #include "input_event_monitor_manager.h"
20 #include "input_handler_manager.h"
21 #include "input_manager.h"
22 #include "interceptor_manager.h"
23 #include "multimodal_event_handler.h"
24 #include "pointer_event.h"
25 #include "proto.h"
26 #include "run_shell_util.h"
27
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 using namespace testing::ext;
32 using namespace OHOS;
33 using namespace MMI;
34 namespace {
35 constexpr int32_t TIME_WAIT_FOR_OP = 500;
36 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputManagerManualTest" };
37 } // namespace
38
39 class InputManagerManualTest : public testing::Test {
40 public:
SetUpTestCase(void)41 static void SetUpTestCase(void) {}
TearDownTestCase(void)42 static void TearDownTestCase(void) {}
43
44 void SetUp();
TearDown()45 void TearDown() {}
46
47 protected:
48 void AddInputEventFilter();
49 void SimulateInputEventHelper(int32_t globalX, int32_t globalY, int32_t expectVal);
50 private:
51 int32_t callbackRet = 0;
52 };
53
SetUp()54 void InputManagerManualTest::SetUp()
55 {
56 callbackRet = 0;
57 }
58
AddInputEventFilter()59 void InputManagerManualTest::AddInputEventFilter()
60 {
61 MMI_LOGD("enter");
62 auto callback = [this](std::shared_ptr<PointerEvent> pointer) -> bool {
63 MMI_LOGD("callback enter");
64 CHKPF(pointer);
65 const std::vector<int32_t> ids = pointer->GetPointersIdList();
66 if (ids.empty()) {
67 MMI_LOGE("ids is empty");
68 return false;
69 }
70
71 const int32_t firstPointerId = ids[0];
72 PointerEvent::PointerItem item;
73 if (!pointer->GetPointerItem(firstPointerId, item)) {
74 MMI_LOGE("GetPointerItem:%{public}d fail", firstPointerId);
75 return false;
76 }
77
78 const int32_t x = item.GetGlobalX();
79 const int32_t y = item.GetGlobalY();
80 if (x == 10 && y == 10) {
81 MMI_LOGI("The values of X and y are both 10, which meets the expectation and callbackRet is set to 1");
82 callbackRet = 1;
83 return true;
84 }
85
86 MMI_LOGI("The values of X and y are not 10, which meets the expectation and callbackRet is set to 2");
87 callbackRet = 2;
88 return false;
89 };
90
91 int32_t ret = InputManager::GetInstance()->AddInputEventFilter(callback);
92 ASSERT_EQ(ret, RET_OK);
93 MMI_LOGD("leave");
94 }
95
SimulateInputEventHelper(int32_t globalX,int32_t globalY,int32_t expectVal)96 void InputManagerManualTest::SimulateInputEventHelper(int32_t globalX, int32_t globalY, int32_t expectVal)
97 {
98 MMI_LOGD("enter");
99 const int32_t pointerId = 0;
100 PointerEvent::PointerItem item;
101 item.SetPointerId(pointerId);
102 item.SetGlobalX(globalX);
103 item.SetGlobalY(globalY);
104
105 auto pointerEvent = PointerEvent::Create();
106 ASSERT_NE(pointerEvent, nullptr);
107 pointerEvent->AddPointerItem(item);
108 pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
109 pointerEvent->SetSourceType(-1);
110 pointerEvent->SetPointerId(pointerId);
111
112 MMI_LOGI("Call InputManager::SimulateInputEvent");
113 InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
114 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
115 EXPECT_EQ(callbackRet, expectVal);
116 MMI_LOGD("leave");
117 }
118
119 HWTEST_F(InputManagerManualTest, HandlePointerEventFilter_001, TestSize.Level1)
120 {
121 MMI_LOGD("enter");
122 AddInputEventFilter();
123 SimulateInputEventHelper(10, 10, 1); // set global x and global y are 10, will expect value is 1
124 SimulateInputEventHelper(0, 0, 2); // set global x and global y are not 10, will expect value is 2
125 }
126 } // namespace MMI
127 } // namespace OHOS