• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
43 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetDeviceTags_01, TestSize.Level1)
44 {
45     CALL_TEST_DEBUG;
__anon69f7578b0202(DTaskCallback cb) 46     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
47         return 0;
48     };
__anon69f7578b0302(DTaskCallback cb) 49     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
50         return 0;
51     };
52     DelegateInterface delegateInterface(delegate, asyncFun);
53     InputHandlerType type = InputHandlerType::MONITOR;
54     uint32_t ret = delegateInterface.GetDeviceTags(type);
55     EXPECT_EQ(ret, 0);
56 
57     type = InputHandlerType::NONE;
58     EXPECT_TRUE(delegateInterface.handlers_.empty());
59     uint32_t ret2 = delegateInterface.GetDeviceTags(type);
60     EXPECT_EQ(ret2, 0);
61 }
62 
63 /**
64  * @tc.name: DelegateInterfaceTest_GetDeviceTags_02
65  * @tc.desc: Test the function GetDeviceTags
66  * @tc.type: FUNC
67  * @tc.require:
68  */
69 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetDeviceTags_02, TestSize.Level1)
70 {
71     CALL_TEST_DEBUG;
__anon69f7578b0402(DTaskCallback cb) 72     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
73         return 0;
74     };
__anon69f7578b0502(DTaskCallback cb) 75     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
76         return 0;
77     };
78     DelegateInterface delegateInterface(delegate, asyncFun);
79     InputHandlerType type = InputHandlerType::INTERCEPTOR;
80     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
81     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
82     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
83     delegateInterface.handlers_.insert({MONITOR, handler2});
84     EXPECT_FALSE(delegateInterface.handlers_.empty());
85     uint32_t ret1 = delegateInterface.GetDeviceTags(type);
86     EXPECT_EQ(ret1, 2);
87 
88     type = InputHandlerType::NONE;
89     uint32_t ret2 = delegateInterface.GetDeviceTags(type);
90     EXPECT_EQ(ret2, 0);
91 }
92 
93 /**
94  * @tc.name: DelegateInterfaceTest_RemoveLocal_01
95  * @tc.desc: Test the function RemoveLocal
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_RemoveLocal_01, TestSize.Level1)
100 {
101     CALL_TEST_DEBUG;
__anon69f7578b0602(DTaskCallback cb) 102     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
103         return 0;
104     };
__anon69f7578b0702(DTaskCallback cb) 105     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
106         return 0;
107     };
108     DelegateInterface delegateInterface(delegate, asyncFun);
109     InputHandlerType type = InputHandlerType::NONE;
110     std::string name = "handler";
111     uint32_t deviceTags = 1;
112     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
113     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
114     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
115     delegateInterface.handlers_.insert({MONITOR, handler2});
116     ASSERT_NO_FATAL_FAILURE(delegateInterface.RemoveLocal(type, name, deviceTags));
117 
118     type = InputHandlerType::INTERCEPTOR;
119     name = "handler3";
120     ASSERT_NO_FATAL_FAILURE(delegateInterface.RemoveLocal(type, name, deviceTags));
121 
122     type = InputHandlerType::INTERCEPTOR;
123     name = "handler1";
124     ASSERT_NO_FATAL_FAILURE(delegateInterface.RemoveLocal(type, name, deviceTags));
125 }
126 
127 /**
128  * @tc.name: DelegateInterfaceTest_GetPriority_01
129  * @tc.desc: Test the function GetPriority
130  * @tc.type: FUNC
131  * @tc.require:
132  */
133 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetPriority_01, TestSize.Level1)
134 {
135     CALL_TEST_DEBUG;
__anon69f7578b0802(DTaskCallback cb) 136     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
137         return 0;
138     };
__anon69f7578b0902(DTaskCallback cb) 139     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
140         return 0;
141     };
142     DelegateInterface delegateInterface(delegate, asyncFun);
143     InputHandlerType type = InputHandlerType::INTERCEPTOR;
144     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
145     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
146     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
147     delegateInterface.handlers_.insert({MONITOR, handler2});
148 
149     int32_t ret = delegateInterface.GetPriority(type);
150     EXPECT_EQ(ret, 1);
151 
152     type = InputHandlerType::NONE;
153     int32_t ret2 = delegateInterface.GetPriority(type);
154     EXPECT_EQ(ret2, 500);
155 }
156 
157 /**
158  * @tc.name: DelegateInterfaceTest_GetEventType_01
159  * @tc.desc: Test the function GetEventType
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetEventType_01, TestSize.Level1)
164 {
165     CALL_TEST_DEBUG;
__anon69f7578b0a02(DTaskCallback cb) 166     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
167         return 0;
168     };
__anon69f7578b0b02(DTaskCallback cb) 169     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
170         return 0;
171     };
172     DelegateInterface delegateInterface(delegate, asyncFun);
173     InputHandlerType type = InputHandlerType::MONITOR;
174     EXPECT_TRUE(delegateInterface.handlers_.empty());
175     uint32_t ret = delegateInterface.GetEventType(type);
176     EXPECT_EQ(ret, 0);
177 }
178 
179 /**
180  * @tc.name: DelegateInterfaceTest_GetEventType_02
181  * @tc.desc: Test the function GetEventType
182  * @tc.type: FUNC
183  * @tc.require:
184  */
185 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_GetEventType_02, TestSize.Level1)
186 {
187     CALL_TEST_DEBUG;
__anon69f7578b0c02(DTaskCallback cb) 188     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
189         return 0;
190     };
__anon69f7578b0d02(DTaskCallback cb) 191     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
192         return 0;
193     };
194     DelegateInterface delegateInterface(delegate, asyncFun);
195     InputHandlerType type = InputHandlerType::MONITOR;
196     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
197     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
198     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
199     delegateInterface.handlers_.insert({MONITOR, handler2});
200     uint32_t ret = delegateInterface.GetEventType(type);
201     EXPECT_EQ(ret, 2);
202 }
203 
204 /**
205  * @tc.name: DelegateInterfaceTest_OnPostSyncTask_01
206  * @tc.desc: Test the function OnPostSyncTask
207  * @tc.type: FUNC
208  * @tc.require:
209  */
210 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_OnPostSyncTask_01, TestSize.Level1)
211 {
212     CALL_TEST_DEBUG;
__anon69f7578b0e02(DTaskCallback cb) 213     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
214         return 0;
215     };
__anon69f7578b0f02(DTaskCallback cb) 216     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
217         return 0;
218     };
219     DelegateInterface delegateInterface(delegate, asyncFun);
__anon69f7578b1002() 220     DTaskCallback myCallback = []() -> int32_t {
221         return RET_OK;
222     };
223     uint32_t ret = delegateInterface.OnPostSyncTask(myCallback);
224     EXPECT_EQ(ret, RET_OK);
225 }
226 
227 /**
228  * @tc.name: DelegateInterfaceTest_OnInputEventHandler_01
229  * @tc.desc: Test the function OnInputEventHandler
230  * @tc.type: FUNC
231  * @tc.require:
232  */
233 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_OnInputEventHandler_01, TestSize.Level1)
234 {
235     CALL_TEST_DEBUG;
__anon69f7578b1102(DTaskCallback cb) 236     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
237         return 0;
238     };
__anon69f7578b1202(DTaskCallback cb) 239     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
240         return 0;
241     };
242     DelegateInterface delegateInterface(delegate, asyncFun);
243     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
244     ASSERT_NE(pointerEvent, nullptr);
245 
246     InputHandlerType type = InputHandlerType::NONE;
247     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
248     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
249     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
250     delegateInterface.handlers_.insert({MONITOR, handler2});
251     ASSERT_NO_FATAL_FAILURE(delegateInterface.OnInputEventHandler(type, pointerEvent));
252 #ifdef OHOS_BUILD_ENABLE_MONITOR
253     type = InputHandlerType::MONITOR;
254     ASSERT_NO_FATAL_FAILURE(delegateInterface.OnInputEventHandler(type, pointerEvent));
255 #endif // OHOS_BUILD_ENABLE_MONITOR
256 
257 #ifdef OHOS_BUILD_ENABLE_INTERCEPTOR
258     type = InputHandlerType::INTERCEPTOR;
259     ASSERT_NO_FATAL_FAILURE(delegateInterface.OnInputEventHandler(type, pointerEvent));
260 #endif // OHOS_BUILD_ENABLE_INTERCEPTOR
261 }
262 
263 /**
264  * @tc.name: DelegateInterfaceTest_AddHandler_01
265  * @tc.desc: Test the function AddHandler
266  * @tc.type: FUNC
267  * @tc.require:
268  */
269 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_AddHandler_01, TestSize.Level1)
270 {
271     CALL_TEST_DEBUG;
__anon69f7578b1302(DTaskCallback cb) 272     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
273         return 0;
274     };
__anon69f7578b1402(DTaskCallback cb) 275     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
276         return 0;
277     };
278     DelegateInterface delegateInterface(delegate, asyncFun);
279     DelegateInterface::HandlerSummary summary;
280     summary.handlerName = "handler1";
281     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
282     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
283     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
284     delegateInterface.handlers_.insert({MONITOR, handler2});
285 
286     InputHandlerType type = InputHandlerType::MONITOR;
287     int32_t ret = delegateInterface.AddHandler(type, summary);
288     EXPECT_EQ(ret, ERROR_NULL_POINTER);
289 
290     summary.handlerName = "handler";
291     int32_t ret2 = delegateInterface.AddHandler(type, summary);
292     EXPECT_EQ(ret2, ERROR_NULL_POINTER);
293 }
294 
295 /**
296  * @tc.name: DelegateInterfaceTest_AddHandler_02
297  * @tc.desc: Test the function AddHandler
298  * @tc.type: FUNC
299  * @tc.require:
300  */
301 HWTEST_F(DelegateInterfaceTest, DelegateInterfaceTest_AddHandler_02, TestSize.Level1)
302 {
303     CALL_TEST_DEBUG;
__anon69f7578b1502(DTaskCallback cb) 304     std::function<int32_t(DTaskCallback)> delegate = [](DTaskCallback cb) -> int32_t {
305         return 0;
306     };
__anon69f7578b1602(DTaskCallback cb) 307     std::function<int32_t(DTaskCallback)> asyncFun = [this](DTaskCallback cb) -> int32_t {
308         return 0;
309     };
310     DelegateInterface delegateInterface(delegate, asyncFun);
311     DelegateInterface::HandlerSummary summary;
312     summary.handlerName = "handler";
313     DelegateInterface::HandlerSummary handler1 = {"handler1", 0x1, HandlerMode::SYNC, 1, 2};
314     DelegateInterface::HandlerSummary handler2 = {"handler2", 0x2, HandlerMode::ASYNC, 2, 3};
315     delegateInterface.handlers_.insert({INTERCEPTOR, handler1});
316     delegateInterface.handlers_.insert({MONITOR, handler2});
317 
318     InputHandlerType type = InputHandlerType::MONITOR;
319     HandleEventType currentType = delegateInterface.GetEventType(type);
320     type = InputHandlerType::INTERCEPTOR;
321     HandleEventType newType = delegateInterface.GetEventType(type);
322     EXPECT_TRUE(currentType != newType);
323 
324     uint32_t currentTags = delegateInterface.GetDeviceTags(type);
325     summary.deviceTags = 1;
326     EXPECT_TRUE((currentTags & summary.deviceTags) != summary.deviceTags);
327 
328     int32_t ret = delegateInterface.AddHandler(type, summary);
329     EXPECT_EQ(ret, ERROR_NULL_POINTER);
330 
331     type = InputHandlerType::MONITOR;
332     currentType = delegateInterface.GetEventType(type);
333     newType = delegateInterface.GetEventType(type);
334     EXPECT_FALSE(currentType != newType);
335     int32_t ret2 = delegateInterface.AddHandler(type, summary);
336     EXPECT_EQ(ret2, ERROR_NULL_POINTER);
337 }
338 } // namespace MMI
339 } // namespace OHOS