• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "accesstoken_kit.h"
19 #include "define_multimodal.h"
20 #include "error_multimodal.h"
21 #include "input_handler_manager.h"
22 #include "input_manager.h"
23 #include "multimodal_event_handler.h"
24 #include "nativetoken_kit.h"
25 #include "pointer_event.h"
26 #include "proto.h"
27 #include "token_setproc.h"
28 
29 namespace OHOS {
30 namespace MMI {
31 using namespace Security::AccessToken;
32 using Security::AccessToken::AccessTokenID;
33 namespace {
34 using namespace testing::ext;
35 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
36 constexpr int32_t TIME_WAIT_FOR_OP = 500;
37 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputManagerManualTest" };
38 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
39 
40 HapInfoParams infoManagerTestInfoParms = {
41     .bundleName = "accesstoken_test",
42     .userID = 1,
43     .instIndex = 0,
44     .appIDDesc = "test"
45 };
46 
47 PermissionDef infoManagerTestPermDef = {
48     .permissionName = "ohos.permission.test",
49     .bundleName = "accesstoken_test",
50     .grantMode = 1,
51     .label = "label",
52     .labelId = 1,
53     .description = "test input event filter",
54     .descriptionId = 1,
55     .availableLevel = APL_SYSTEM_CORE
56 };
57 
58 PermissionStateFull infoManagerTestState = {
59     .grantFlags = {1},
60     .grantStatus = {PermissionState::PERMISSION_GRANTED},
61     .isGeneral = true,
62     .permissionName = "ohos.permission.test",
63     .resDeviceID = {"local"}
64 };
65 
66 HapPolicyParams infoManagerTestPolicyPrams = {
67     .apl = APL_SYSTEM_CORE,
68     .domain = "test.domain",
69     .permList = {infoManagerTestPermDef},
70     .permStateList = {infoManagerTestState}
71 };
72 } // namespace
73 
74 class AccessToken {
75 public:
AccessToken()76     AccessToken()
77     {
78         currentID_ = GetSelfTokenID();
79         AccessTokenIDEx tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms, infoManagerTestPolicyPrams);
80         accessID_ = tokenIdEx.tokenIdExStruct.tokenID;
81         SetSelfTokenID(accessID_);
82     }
~AccessToken()83     ~AccessToken()
84     {
85         AccessTokenKit::DeleteToken(accessID_);
86         SetSelfTokenID(currentID_);
87     }
88 private:
89     AccessTokenID currentID_ = 0;
90     AccessTokenID accessID_ = 0;
91 };
92 
93 class InputManagerManualTest : public testing::Test {
94 public:
SetUpTestCase(void)95     static void SetUpTestCase(void) {}
TearDownTestCase(void)96     static void TearDownTestCase(void) {}
97 
98     void SetUp();
TearDown()99     void TearDown() {}
100 
101 protected:
102 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
103     void AddInputEventFilter();
104     void SimulateInputEventHelper(int32_t physicalX, int32_t physicalY, int32_t expectVal);
105 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
106 private:
107     int32_t callbackRet = 0;
108 };
109 
SetUp()110 void InputManagerManualTest::SetUp()
111 {
112     callbackRet = 0;
113 }
114 
115 #if defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)
AddInputEventFilter()116 void InputManagerManualTest::AddInputEventFilter()
117 {
118     CALL_DEBUG_ENTER;
119     auto callback = [this](std::shared_ptr<PointerEvent> pointer) -> bool {
120         MMI_HILOGD("Callback enter");
121         CHKPF(pointer);
122         const std::vector<int32_t> ids = pointer->GetPointerIds();
123         if (ids.empty()) {
124             MMI_HILOGE("The ids is empty");
125             return false;
126         }
127 
128         const int32_t firstPointerId = ids[0];
129         PointerEvent::PointerItem item;
130         if (!pointer->GetPointerItem(firstPointerId, item)) {
131             MMI_HILOGE("GetPointerItem:%{public}d fail", firstPointerId);
132             return false;
133         }
134 
135         const int32_t x = item.GetDisplayX();
136         const int32_t y = item.GetDisplayY();
137         if (x == 10 && y == 10) {
138             MMI_HILOGI("The values of X and y are both 10, which meets the expectation and callbackRet is set to 1");
139             callbackRet = 1;
140             return false;
141         }
142 
143         MMI_HILOGI("The values of X and y are not 10, which meets the expectation and callbackRet is set to 2");
144         callbackRet = 2;
145         return false;
146     };
147     AccessToken accessToken;
148     int32_t ret = InputManager::GetInstance()->AddInputEventFilter(callback);
149     ASSERT_EQ(ret, RET_OK);
150 }
151 
SimulateInputEventHelper(int32_t physicalX,int32_t physicalY,int32_t expectVal)152 void InputManagerManualTest::SimulateInputEventHelper(int32_t physicalX, int32_t physicalY, int32_t expectVal)
153 {
154     CALL_DEBUG_ENTER;
155     const int32_t pointerId = 0;
156     PointerEvent::PointerItem item;
157     item.SetPointerId(pointerId);
158     item.SetDisplayX(physicalX);
159     item.SetDisplayY(physicalY);
160 
161     auto pointerEvent = PointerEvent::Create();
162     ASSERT_NE(pointerEvent, nullptr);
163     pointerEvent->AddPointerItem(item);
164     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_DOWN);
165     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_MOUSE);
166     pointerEvent->SetPointerId(pointerId);
167 
168     MMI_HILOGI("Call InputManager::SimulateInputEvent");
169     InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
170     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP));
171     EXPECT_EQ(callbackRet, expectVal);
172 }
173 
174 /**
175  * @tc.name:HandlePointerEventFilter_001
176  * @tc.desc:Verify pointer event filter
177  * @tc.type: FUNC
178  * @tc.require:
179  */
180 HWTEST_F(InputManagerManualTest, HandlePointerEventFilter_001, TestSize.Level1)
181 {
182     CALL_DEBUG_ENTER;
183     AddInputEventFilter();
184     SimulateInputEventHelper(10, 10, 1); // set physical x and physical y are 10, will expect value is 1
185     SimulateInputEventHelper(0, 0, 2); // set physical x and physical y are not 10, will expect value is 2
186 }
187 #endif // OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH
188 } // namespace MMI
189 } // namespace OHOS