1 /*
2 * Copyright (c) 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 #ifndef EVENT_TEST_UTIL_H
17 #define EVENT_TEST_UTIL_H
18
19 #include <chrono>
20 #include <condition_variable>
21 #include <list>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25
26 #include <gtest/gtest.h>
27
28 #include "accesstoken_kit.h"
29 #include "nativetoken_kit.h"
30 #include "token_setproc.h"
31
32 #include "input_manager.h"
33 #include "singleton.h"
34 #include "window_utils_test.h"
35 #include "scene_board_judgement.h"
36
37 namespace OHOS {
38 namespace MMI {
39 using namespace Security::AccessToken;
40 using Security::AccessToken::AccessTokenID;
41 namespace {
42 using namespace testing::ext;
43 PermissionDef inputDispatchControlPermDef_ = {
44 .permissionName = "ohos.permission.INPUT_CONTROL_DISPATCHING",
45 .bundleName = "accesstoken_test",
46 .grantMode = 1,
47 .label = "label",
48 .labelId = 1,
49 .description = "test input agent",
50 .availableLevel = APL_SYSTEM_CORE,
51 .descriptionId = 1
52 };
53
54 PermissionDef infoManagerTestPermDef_ = {
55 .permissionName = "ohos.permission.INPUT_MONITORING",
56 .bundleName = "accesstoken_test",
57 .grantMode = 1,
58 .label = "label",
59 .labelId = 1,
60 .description = "test input agent",
61 .availableLevel = APL_SYSTEM_CORE,
62 .descriptionId = 1,
63 };
64
65 PermissionStateFull infoManagerTestState_ = {
66 .permissionName = "ohos.permission.INPUT_MONITORING",
67 .isGeneral = true,
68 .grantStatus = { PermissionState::PERMISSION_GRANTED },
69 .grantFlags = { 1 },
70 .resDeviceID = { "local" }
71 };
72
73 PermissionStateFull inputDispatchControlPermState_ = {
74 .permissionName = "ohos.permission.INPUT_CONTROL_DISPATCHING",
75 .isGeneral = true,
76 .grantStatus = { PermissionState::PERMISSION_GRANTED },
77 .grantFlags = { 1 },
78 .resDeviceID = { "local" }
79 };
80
81 HapPolicyParams infoManagerTestPolicyPrams_ = {
82 .apl = APL_SYSTEM_CORE,
83 .domain = "test.domain",
84 .permList = { infoManagerTestPermDef_, infoManagerTestPermDef_},
85 .permStateList = { infoManagerTestState_, inputDispatchControlPermState_}
86 };
87
88 HapInfoParams infoManagerTestInfoParms_ = {
89 .userID = 1,
90 .bundleName = "inputManager_test",
91 .instIndex = 0,
92 .appIDDesc = "InputManagerTest",
93 .isSystemApp = true
94 };
95 } // namespace
96 enum class TestScene : int32_t {
97 NORMAL_TEST = 0,
98 EXCEPTION_TEST,
99 };
100
101 enum class RECV_FLAG : uint32_t {
102 RECV_FOCUS = 0x00000000,
103 RECV_MONITOR,
104 RECV_INTERCEPT,
105 RECV_MARK_CONSUMED,
106 };
107
108 class EventUtilTest final {
109 DECLARE_DELAYED_SINGLETON(EventUtilTest);
110 public:
111 DISALLOW_COPY_AND_MOVE(EventUtilTest);
112 bool Init();
113 std::string GetEventDump();
114 void AddEventDump(std::string eventDump);
115 std::string DumpInputEvent(const std::shared_ptr<PointerEvent>& pointerEvent);
116 std::string DumpInputEvent(const std::shared_ptr<KeyEvent>& keyEvent);
117 bool CompareDump(const std::shared_ptr<PointerEvent>& pointerEvent);
118 bool CompareDump(const std::shared_ptr<KeyEvent>& keyEvent);
119 public:
GetRecvFlag()120 inline RECV_FLAG GetRecvFlag()
121 {
122 return recvFlag_;
123 }
SetRecvFlag(RECV_FLAG flag)124 inline void SetRecvFlag(RECV_FLAG flag)
125 {
126 recvFlag_ = flag;
127 }
128 private:
129 RECV_FLAG recvFlag_ { RECV_FLAG::RECV_FOCUS };
130 std::mutex mutex_;
131 std::list<std::string> strEventDump_;
132 std::condition_variable conditionVariable_;
133 };
134
135 #define TestUtil ::OHOS::DelayedSingleton<EventUtilTest>::GetInstance()
136
137 class InputEventConsumer : public IInputEventConsumer {
138 public:
139 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
140 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)141 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
142 };
143
144 class PriorityMiddleCallback : public IInputEventConsumer {
145 public:
146 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
147 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)148 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
149 };
150
151 class PriorityHighCallback : public IInputEventConsumer {
152 public:
153 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
154 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)155 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
156 };
157
158 class InputEventCallback : public IInputEventConsumer {
159 public:
160 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
161 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)162 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
163 int32_t GetLastEventId() const;
164
165 private:
166 mutable int32_t lastPointerEventId_ { -1 };
167 };
168
GetLastEventId()169 inline int32_t InputEventCallback::GetLastEventId() const
170 {
171 return lastPointerEventId_;
172 }
173
174 class WindowEventConsumer : public IInputEventConsumer {
175 public:
176 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
177 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)178 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
179 uint64_t GetConsumerThreadId();
180 private:
181 mutable uint64_t threadId_ { 0 };
182 };
183
184 int64_t GetNanoTime();
185 template<typename sharedType>
GetPtr()186 std::shared_ptr<sharedType> GetPtr()
187 {
188 return std::make_shared<sharedType>();
189 }
190
191 template<typename EventType>
192 void TestSimulateInputEvent(EventType& event, const TestScene& testScene = TestScene::NORMAL_TEST)
193 {
194 EXPECT_TRUE((static_cast<int32_t>(testScene) ^ TestUtil->CompareDump(event)));
195 }
196 template<typename EventType>
SimulateInputEventUtilTest(EventType & event)197 void SimulateInputEventUtilTest(EventType& event)
198 {
199 TestSimulateInputEvent(event);
200 }
201 void DumpWindowData(const std::shared_ptr<PointerEvent>& pointerEvent);
202 class AccessMonitor {
203 public:
AccessMonitor()204 AccessMonitor()
205 {
206 currentId_ = GetSelfTokenID();
207 AccessTokenIDEx tokenIdEx = { 0 };
208 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_);
209 monitorId_ = tokenIdEx.tokenIDEx;
210 SetSelfTokenID(monitorId_);
211 }
~AccessMonitor()212 ~AccessMonitor()
213 {
214 AccessTokenKit::DeleteToken(monitorId_);
215 SetSelfTokenID(currentId_);
216 }
217 private:
218 uint64_t currentId_ { 0 };
219 uint64_t monitorId_ { 0 };
220 };
221 } // namespace MMI
222 } // namespace OHOS
223 #endif // EVENT_TEST_UTIL_H