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
36 namespace OHOS {
37 namespace MMI {
38 using namespace Security::AccessToken;
39 using Security::AccessToken::AccessTokenID;
40 namespace {
41 using namespace testing::ext;
42 PermissionDef infoManagerTestPermDef_ = {
43 .permissionName = "ohos.permission.INPUT_MONITORING",
44 .bundleName = "accesstoken_test",
45 .grantMode = 1,
46 .label = "label",
47 .labelId = 1,
48 .description = "test input agent",
49 .descriptionId = 1,
50 .availableLevel = APL_NORMAL
51 };
52
53 PermissionStateFull infoManagerTestState_ = {
54 .grantFlags = {1},
55 .grantStatus = {PermissionState::PERMISSION_GRANTED},
56 .isGeneral = true,
57 .permissionName = "ohos.permission.INPUT_MONITORING",
58 .resDeviceID = {"local"}
59 };
60
61 HapPolicyParams infoManagerTestPolicyPrams_ = {
62 .apl = APL_NORMAL,
63 .domain = "test.domain",
64 .permList = {infoManagerTestPermDef_},
65 .permStateList = {infoManagerTestState_}
66 };
67
68 HapInfoParams infoManagerTestInfoParms_ = {
69 .bundleName = "inputManager_test",
70 .userID = 1,
71 .instIndex = 0,
72 .appIDDesc = "InputManagerTest"
73 };
74 } // namespace
75 enum class TestScene : int32_t {
76 NORMAL_TEST = 0,
77 EXCEPTION_TEST,
78 };
79
80 enum class RECV_FLAG : uint32_t {
81 RECV_FOCUS = 0x00000000,
82 RECV_MONITOR,
83 RECV_INTERCEPT,
84 RECV_MARK_CONSUMED,
85 };
86
87 class EventUtilTest final {
88 DECLARE_DELAYED_SINGLETON(EventUtilTest);
89 public:
90 DISALLOW_COPY_AND_MOVE(EventUtilTest);
91 bool Init();
92 std::string GetEventDump();
93 void AddEventDump(std::string eventDump);
94 std::string DumpInputEvent(const std::shared_ptr<PointerEvent>& pointerEvent);
95 std::string DumpInputEvent(const std::shared_ptr<KeyEvent>& keyEvent);
96 bool CompareDump(const std::shared_ptr<PointerEvent>& pointerEvent);
97 bool CompareDump(const std::shared_ptr<KeyEvent>& keyEvent);
98 public:
GetRecvFlag()99 inline RECV_FLAG GetRecvFlag()
100 {
101 return recvFlag_;
102 }
SetRecvFlag(RECV_FLAG flag)103 inline void SetRecvFlag(RECV_FLAG flag)
104 {
105 recvFlag_ = flag;
106 }
107 private:
108 RECV_FLAG recvFlag_ { RECV_FLAG::RECV_FOCUS };
109 std::mutex mutex_;
110 std::list<std::string> strEventDump_;
111 std::condition_variable conditionVariable_;
112 };
113
114 #define TestUtil ::OHOS::DelayedSingleton<EventUtilTest>::GetInstance()
115
116 class InputEventConsumer : public IInputEventConsumer {
117 public:
118 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
119 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)120 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
121 };
122
123 class InputEventCallback : public IInputEventConsumer {
124 public:
125 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
126 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)127 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
128 int32_t GetLastEventId() const;
129
130 private:
131 mutable int32_t lastPointerEventId_ { -1 };
132 };
133
GetLastEventId()134 inline int32_t InputEventCallback::GetLastEventId() const
135 {
136 return lastPointerEventId_;
137 }
138
139 class WindowEventConsumer : public IInputEventConsumer {
140 public:
141 virtual void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override;
142 virtual void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent)143 virtual void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
144 uint64_t GetConsumerThreadId();
145 private:
146 mutable uint64_t threadId_ { 0 };
147 };
148
149 int64_t GetNanoTime();
150 template<typename sharedType>
GetPtr()151 std::shared_ptr<sharedType> GetPtr()
152 {
153 return std::make_shared<sharedType>();
154 }
155
156 template<typename EventType>
157 void TestSimulateInputEvent(EventType& event, const TestScene& testScene = TestScene::NORMAL_TEST)
158 {
159 EXPECT_TRUE((static_cast<int32_t>(testScene) ^ TestUtil->CompareDump(event)));
160 }
161 void DumpWindowData(const std::shared_ptr<PointerEvent>& pointerEvent);
162 class AccessMonitor {
163 public:
AccessMonitor()164 AccessMonitor()
165 {
166 currentId_ = GetSelfTokenID();
167 AccessTokenIDEx tokenIdEx = { 0 };
168 tokenIdEx = AccessTokenKit::AllocHapToken(infoManagerTestInfoParms_, infoManagerTestPolicyPrams_);
169 monitorId_ = tokenIdEx.tokenIdExStruct.tokenID;
170 SetSelfTokenID(monitorId_);
171 }
~AccessMonitor()172 ~AccessMonitor()
173 {
174 AccessTokenKit::DeleteToken(monitorId_);
175 SetSelfTokenID(currentId_);
176 }
177 private:
178 AccessTokenID currentId_ { 0 };
179 AccessTokenID monitorId_ { 0 };
180 };
181 } // namespace MMI
182 } // namespace OHOS
183
184 #endif // EVENT_TEST_UTIL_H