1 /*
2 * Copyright (c) 2024 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 "delegate_interface.h"
19 #include "error_multimodal.h"
20
21 #include "mmi_log.h"
22
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "DelegateInterfaceTest"
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 using namespace testing::ext;
29 } // namespace
30
31 class DelegateInterfaceTest : public testing::Test {
32 public:
SetUpTestCase(void)33 static void SetUpTestCase(void) {}
TearDownTestCase(void)34 static void TearDownTestCase(void) {}
35 };
36
37 /**
38 * @tc.name: DelegateInterfaceTest_GetDeviceTags_01
39 * @tc.desc: Test the function GetDeviceTags
40 * @tc.type: FUNC
41 * @tc.require:
42 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetDeviceTags_01, TestSize.Level1)43 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetDeviceTags_01, TestSize.Level1)
44 {
45 CALL_TEST_DEBUG;
46 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
47 return 0;
48 };
49 DelegateInterface delegateInterface(delegate);
50 InputHandlerType type = InputHandlerType::MONITOR;
51 uint32_t ret = delegateInterface.GetDeviceTags(type);
52 EXPECT_EQ(ret, 0);
53
54 type = InputHandlerType::NONE;
55 EXPECT_TRUE(delegateInterface.handlers.empty());
56 uint32_t ret2 = delegateInterface.GetDeviceTags(type);
57 EXPECT_EQ(ret2, 0);
58 }
59
60 /**
61 * @tc.name: DelegateInterfaceTest_GetDeviceTags_02
62 * @tc.desc: Test the function GetDeviceTags
63 * @tc.type: FUNC
64 * @tc.require:
65 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetDeviceTags_02, TestSize.Level1)66 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetDeviceTags_02, TestSize.Level1)
67 {
68 CALL_TEST_DEBUG;
69 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
70 return 0;
71 };
72 DelegateInterface delegateInterface(delegate);
73 InputHandlerType type = InputHandlerType::INTERCEPTOR;
74 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
75 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
76 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
77 delegateInterface.handlers.insert({MONITOR, handler2});
78 EXPECT_FALSE(delegateInterface.handlers.empty());
79 uint32_t ret1 = delegateInterface.GetDeviceTags(type);
80 EXPECT_EQ(ret1, 2);
81
82 type = InputHandlerType::NONE;
83 uint32_t ret2 = delegateInterface.GetDeviceTags(type);
84 EXPECT_EQ(ret2, 0);
85 }
86
87 /**
88 * @tc.name: DelegateInterfaceTest_RemoveLocal_01
89 * @tc.desc: Test the function RemoveLocal
90 * @tc.type: FUNC
91 * @tc.require:
92 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_RemoveLocal_01, TestSize.Level1)93 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_RemoveLocal_01, TestSize.Level1)
94 {
95 CALL_TEST_DEBUG;
96 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
97 return 0;
98 };
99 DelegateInterface delegateInterface(delegate);
100 InputHandlerType type = InputHandlerType::NONE;
101 std::string name = "handler";
102 uint32_t deviceTags = 1;
103 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
104 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
105 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
106 delegateInterface.handlers.insert({MONITOR, handler2});
107 ASSERT_NO_FATAL_FAILURE(delegateInterface.RemoveLocal(type, name, deviceTags));
108
109 type = InputHandlerType::INTERCEPTOR;
110 name = "handler3";
111 ASSERT_NO_FATAL_FAILURE(delegateInterface.RemoveLocal(type, name, deviceTags));
112
113 type = InputHandlerType::INTERCEPTOR;
114 name = "handler1";
115 ASSERT_NO_FATAL_FAILURE(delegateInterface.RemoveLocal(type, name, deviceTags));
116 }
117
118 /**
119 * @tc.name: DelegateInterfaceTest_GetPriority_01
120 * @tc.desc: Test the function GetPriority
121 * @tc.type: FUNC
122 * @tc.require:
123 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetPriority_01, TestSize.Level1)124 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetPriority_01, TestSize.Level1)
125 {
126 CALL_TEST_DEBUG;
127 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
128 return 0;
129 };
130 DelegateInterface delegateInterface(delegate);
131 InputHandlerType type = InputHandlerType::INTERCEPTOR;
132 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
133 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
134 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
135 delegateInterface.handlers.insert({MONITOR, handler2});
136
137 int32_t ret = delegateInterface.GetPriority(type);
138 EXPECT_EQ(ret, 1);
139
140 type = InputHandlerType::NONE;
141 int32_t ret2 = delegateInterface.GetPriority(type);
142 EXPECT_EQ(ret2, 500);
143 }
144
145 /**
146 * @tc.name: DelegateInterfaceTest_GetEventType_01
147 * @tc.desc: Test the function GetEventType
148 * @tc.type: FUNC
149 * @tc.require:
150 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetEventType_01, TestSize.Level1)151 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetEventType_01, TestSize.Level1)
152 {
153 CALL_TEST_DEBUG;
154 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
155 return 0;
156 };
157 DelegateInterface delegateInterface(delegate);
158 InputHandlerType type = InputHandlerType::MONITOR;
159 EXPECT_TRUE(delegateInterface.handlers.empty());
160 uint32_t ret = delegateInterface.GetEventType(type);
161 EXPECT_EQ(ret, 0);
162 }
163
164 /**
165 * @tc.name: DelegateInterfaceTest_GetEventType_02
166 * @tc.desc: Test the function GetEventType
167 * @tc.type: FUNC
168 * @tc.require:
169 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetEventType_02, TestSize.Level1)170 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetEventType_02, TestSize.Level1)
171 {
172 CALL_TEST_DEBUG;
173 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
174 return 0;
175 };
176 DelegateInterface delegateInterface(delegate);
177 InputHandlerType type = InputHandlerType::MONITOR;
178 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
179 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
180 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
181 delegateInterface.handlers.insert({MONITOR, handler2});
182 uint32_t ret = delegateInterface.GetEventType(type);
183 EXPECT_EQ(ret, 2);
184 }
185
186 /**
187 * @tc.name: DelegateInterfaceTest_OnPostSyncTask_01
188 * @tc.desc: Test the function OnPostSyncTask
189 * @tc.type: FUNC
190 * @tc.require:
191 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_OnPostSyncTask_01, TestSize.Level1)192 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_OnPostSyncTask_01, TestSize.Level1)
193 {
194 CALL_TEST_DEBUG;
195 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
196 return 0;
197 };
198 DelegateInterface delegateInterface(delegate);
199 DTaskCallback myCallback = []() -> int32_t {
200 return RET_OK;
201 };
202 uint32_t ret = delegateInterface.OnPostSyncTask(myCallback);
203 EXPECT_EQ(ret, RET_OK);
204 }
205
206 /**
207 * @tc.name: DelegateInterfaceTest_OnInputEventHandler_01
208 * @tc.desc: Test the function OnInputEventHandler
209 * @tc.type: FUNC
210 * @tc.require:
211 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_OnInputEventHandler_01, TestSize.Level1)212 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_OnInputEventHandler_01, TestSize.Level1)
213 {
214 CALL_TEST_DEBUG;
215 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
216 return 0;
217 };
218 DelegateInterface delegateInterface(delegate);
219 std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
220 ASSERT_NE(pointerEvent, nullptr);
221
222 InputHandlerType type = InputHandlerType::NONE;
223 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
224 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
225 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
226 delegateInterface.handlers.insert({MONITOR, handler2});
227 ASSERT_NO_FATAL_FAILURE(delegateInterface.OnInputEventHandler(type, pointerEvent));
228 #ifdef OHOS_BUILD_ENABLE_MONITOR
229 type = InputHandlerType::MONITOR;
230 ASSERT_NO_FATAL_FAILURE(delegateInterface.OnInputEventHandler(type, pointerEvent));
231 #endif // OHOS_BUILD_ENABLE_MONITOR
232
233 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
234 type = InputHandlerType::INTERCEPTOR;
235 ASSERT_NO_FATAL_FAILURE(delegateInterface.OnInputEventHandler(type, pointerEvent));
236 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
237 }
238
239 /**
240 * @tc.name: DelegateInterfaceTest_AddHandler_01
241 * @tc.desc: Test the function AddHandler
242 * @tc.type: FUNC
243 * @tc.require:
244 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_AddHandler_01, TestSize.Level1)245 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_AddHandler_01, TestSize.Level1)
246 {
247 CALL_TEST_DEBUG;
248 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
249 return 0;
250 };
251 DelegateInterface delegateInterface(delegate);
252 DelegateInterface::HandlerSummary summary;
253 summary.handlerName = "handler1";
254 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
255 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
256 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
257 delegateInterface.handlers.insert({MONITOR, handler2});
258
259 InputHandlerType type = InputHandlerType::MONITOR;
260 int32_t ret = delegateInterface.AddHandler(type, summary);
261 EXPECT_EQ(ret, ERROR_NULL_POINTER);
262
263 summary.handlerName = "handler";
264 int32_t ret2 = delegateInterface.AddHandler(type, summary);
265 EXPECT_EQ(ret2, ERROR_NULL_POINTER);
266 }
267
268 /**
269 * @tc.name: DelegateInterfaceTest_AddHandler_02
270 * @tc.desc: Test the function AddHandler
271 * @tc.type: FUNC
272 * @tc.require:
273 */
HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_AddHandler_02, TestSize.Level1)274 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_AddHandler_02, TestSize.Level1)
275 {
276 CALL_TEST_DEBUG;
277 std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
278 return 0;
279 };
280 DelegateInterface delegateInterface(delegate);
281 DelegateInterface::HandlerSummary summary;
282 summary.handlerName = "handler";
283 DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
284 DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
285 delegateInterface.handlers.insert({INTERCEPTOR, handler1});
286 delegateInterface.handlers.insert({MONITOR, handler2});
287
288 InputHandlerType type = InputHandlerType::MONITOR;
289 HandleEventType currentType = delegateInterface.GetEventType(type);
290 type = InputHandlerType::INTERCEPTOR;
291 HandleEventType newType = delegateInterface.GetEventType(type);
292 EXPECT_TRUE(currentType != newType);
293
294 uint32_t currentTags = delegateInterface.GetDeviceTags(type);
295 summary.deviceTags = 1;
296 EXPECT_TRUE((currentTags & summary.deviceTags) != summary.deviceTags);
297
298 int32_t ret = delegateInterface.AddHandler(type, summary);
299 EXPECT_EQ(ret, ERROR_NULL_POINTER);
300
301 type = InputHandlerType::MONITOR;
302 currentType = delegateInterface.GetEventType(type);
303 newType = delegateInterface.GetEventType(type);
304 EXPECT_FALSE(currentType != newType);
305 int32_t ret2 = delegateInterface.AddHandler(type, summary);
306 EXPECT_EQ(ret2, ERROR_NULL_POINTER);
307 }
308 } // namespace MMI
309 } // namespace OHOS