• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "input_scene_board_judgement.h"
19 #include "pre_monitor_manager.h"
20 #include "multimodal_event_handler.h"
21 #include "mmi_log.h"
22 
23 #undef MMI_LOG_TAG
24 #define MMI_LOG_TAG "PreMonitorManagerTest"
25 
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 using namespace testing::ext;
30 } // namespace
31 
32 class PreMonitorManagerTest : public testing::Test {
33 public:
SetUpTestCase(void)34     static void SetUpTestCase(void) {}
TearDownTestCase(void)35     static void TearDownTestCase(void) {}
36 };
37 
38 class InputEventConsumerTest : public IInputEventConsumer {
39     public:
OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const40         void OnInputEvent(std::shared_ptr<KeyEvent> keyEvent) const override {};
OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const41         void OnInputEvent(std::shared_ptr<PointerEvent> pointerEvent) const override
42         {
43             MMI_HILOGD("Report pointer event success");
44         };
OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const45         void OnInputEvent(std::shared_ptr<AxisEvent> axisEvent) const override {};
46     };
47 
48 /**
49  * @tc.name: PreMonitorManagerTest_FindHandler_001
50  * @tc.desc: Test FindHandler
51  * @tc.type: FUNC
52  * @tc.require:
53  */
54 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_FindHandler_001, TestSize.Level1)
55 {
56     CALL_TEST_DEBUG;
57     PreMonitorManager manager;
58     int32_t handlerId = 1;
59     ASSERT_NO_FATAL_FAILURE(manager.FindHandler(handlerId));
60     handlerId = -1;
61     ASSERT_NO_FATAL_FAILURE(manager.FindHandler(handlerId));
62 }
63 
64 /**
65  * @tc.name: PreMonitorManagerTest_FindHandler_002
66  * @tc.desc: Test FindHandler
67  * @tc.type: FUNC
68  * @tc.require:
69  */
70 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_FindHandler_002, TestSize.Level2)
71 {
72     CALL_TEST_DEBUG;
73     PreMonitorManager manager;
74     int32_t handlerId = 1;
75     PreMonitorManager::Handler handler;
76     std::shared_ptr<IInputEventConsumer> consumer = nullptr;
77     handler.callback_ = consumer;
78     manager.monitorHandlers_[handlerId] = handler;
79     std::shared_ptr<IInputEventConsumer> result = manager.FindHandler(handlerId);
80     ASSERT_EQ(result, consumer);
81 }
82 
83 /**
84  * @tc.name: PreMonitorManagerTest_AddHandler_001
85  * @tc.desc: Test AddHandler
86  * @tc.type: FUNC
87  * @tc.require:
88  */
89 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_AddHandler_001, TestSize.Level1)
90 {
91     CALL_TEST_DEBUG;
92     PreMonitorManager manager;
93     HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY;
94     std::vector<int32_t> keys = {1, 2, 3};
95     std::shared_ptr<IInputEventConsumer> consumer = nullptr;
96     int32_t ret = manager.AddHandler(consumer, eventType, keys);
97     EXPECT_EQ(ret, RET_ERR);
98 }
99 
100 /**
101  * @tc.name: PreMonitorManagerTest_AddHandler_002
102  * @tc.desc: Test AddHandler
103  * @tc.type: FUNC
104  * @tc.require:
105  */
106 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_AddHandler_002, TestSize.Level1)
107 {
108     CALL_TEST_DEBUG;
109     PreMonitorManager manager;
110     HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY;
111     std::vector<int32_t> keys = { KeyEvent::KEYCODE_POWER, KeyEvent::KEYCODE_VOLUME_DOWN};
112     auto consumer = std::make_shared<InputEventConsumerTest>();
113     ASSERT_NE(consumer, nullptr);
114 
115     auto id = manager.nextId_;
116     int32_t ret = manager.AddHandler(consumer, eventType, keys);
117     EXPECT_EQ(ret, id);
118 
119     manager.nextId_--;
120     ret = manager.AddHandler(consumer, eventType, keys);
121     EXPECT_EQ(ret, INVALID_HANDLER_ID);
122 }
123 
124 /**
125  * @tc.name: PreMonitorManagerTest_RemoveHandler_001
126  * @tc.desc: Test RemoveHandler
127  * @tc.type: FUNC
128  * @tc.require:
129  */
130 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_RemoveHandler_001, TestSize.Level2)
131 {
132     CALL_TEST_DEBUG;
133     PreMonitorManager manager;
134     int32_t handlerId = 1;
135     PreMonitorManager::Handler handler;
136     std::shared_ptr<IInputEventConsumer> consumer = nullptr;
137     handler.callback_ = consumer;
138     manager.monitorHandlers_[handlerId] = handler;
139     int32_t ret = manager.RemoveHandler(0);
140     EXPECT_EQ(ret, RET_ERR);
141     ASSERT_NO_FATAL_FAILURE(manager.RemoveHandler(1));
142 }
143 
144 /**
145  * @tc.name: PreMonitorManagerTest_AddLocal_001
146  * @tc.desc: Test AddLocal
147  * @tc.type: FUNC
148  * @tc.require:
149  */
150 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_AddLocal_001, TestSize.Level2)
151 {
152     CALL_TEST_DEBUG;
153     PreMonitorManager manager;
154     HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY;
155     int32_t handlerId = 1;
156     std::shared_ptr<IInputEventConsumer> consumer = nullptr;
157     std::vector<int32_t> keys = {1, 2, 3};
158     int32_t ret = manager.AddLocal(handlerId, eventType, keys, consumer);
159     EXPECT_EQ(ret, RET_OK);
160     ret = manager.AddLocal(handlerId, eventType, keys, consumer);
161     EXPECT_EQ(ret, RET_ERR);
162 }
163 
164 /**
165  * @tc.name: PreMonitorManagerTest_RemoveLocal_001
166  * @tc.desc: Test RemoveLocal
167  * @tc.type: FUNC
168  * @tc.require:
169  */
170 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_RemoveLocal_001, TestSize.Level1)
171 {
172     CALL_TEST_DEBUG;
173     PreMonitorManager manager;
174     HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY;
175     int32_t handlerId = 1;
176     std::shared_ptr<IInputEventConsumer> consumer = nullptr;
177     std::vector<int32_t> keys = {1, 2, 3};
178     int32_t ret = manager.AddLocal(handlerId, eventType, keys, consumer);
179     ret = manager.RemoveLocal(handlerId);
180     EXPECT_EQ(ret, RET_OK);
181     ret = manager.RemoveLocal(handlerId);
182     EXPECT_EQ(ret, RET_ERR);
183 }
184 
185 /**
186  * @tc.name: PreMonitorManagerTest_AddToServer_001
187  * @tc.desc: Test AddToServer
188  * @tc.type: FUNC
189  * @tc.require:
190  */
191 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_AddToServer_001, TestSize.Level1)
192 {
193     CALL_TEST_DEBUG;
194     PreMonitorManager manager;
195     HandleEventType eventType = HANDLE_EVENT_TYPE_PRE_KEY;
196     int32_t handlerId = 1;
197     std::vector<int32_t> keys = {1, 2, 3};
198     ASSERT_NO_FATAL_FAILURE(manager.AddToServer(handlerId, eventType, keys));
199 }
200 
201 /**
202  * @tc.name: PreMonitorManagerTest_RemoveFromServer_001
203  * @tc.desc: Test RemoveFromServer
204  * @tc.type: FUNC
205  * @tc.require:
206  */
207 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_RemoveFromServer_001, TestSize.Level3)
208 {
209     CALL_TEST_DEBUG;
210     PreMonitorManager manager;
211     int32_t handlerId = 1;
212     ASSERT_NO_FATAL_FAILURE(manager.RemoveFromServer(handlerId));
213 }
214 
215 #ifdef OHOS_BUILD_ENABLE_KEYBOARD
216 /**
217  * @tc.name: PreMonitorManagerTest_OnPreKeyEvent_001
218  * @tc.desc: Test OnPreKeyEvent
219  * @tc.type: FUNC
220  * @tc.require:
221  */
222 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_OnPreKeyEvent_001, TestSize.Level1)
223 {
224     CALL_TEST_DEBUG;
225     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
226     ASSERT_NE(keyEvent, nullptr);
227     int32_t handlerId = 1;
228     ASSERT_NO_FATAL_FAILURE(PRE_MONITOR_MGR.OnPreKeyEvent(keyEvent, handlerId));
229 }
230 
231 /**
232  * @tc.name: PreMonitorManagerTest_OnPreKeyEvent_002
233  * @tc.desc: Test OnPreKeyEvent
234  * @tc.type: FUNC
235  * @tc.require:
236  */
237 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_OnPreKeyEvent_002, TestSize.Level1)
238 {
239     CALL_TEST_DEBUG;
240     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
241     ASSERT_NE(keyEvent, nullptr);
242     int32_t handlerId = -1;
243     PreMonitorManager manager;
244     ASSERT_NO_FATAL_FAILURE(manager.OnPreKeyEvent(keyEvent, handlerId));
245 
246     handlerId = 1;
247     PreMonitorManager::Handler handle;
248     handle.eventType_ = HANDLE_EVENT_TYPE_NONE;
249     handle.callback_  = std::make_shared<InputEventConsumerTest>();
250     ASSERT_NE(handle.callback_, nullptr);
251     manager.monitorHandlers_.insert(std::make_pair(handlerId, handle));
252     ASSERT_NO_FATAL_FAILURE(manager.OnPreKeyEvent(keyEvent, handlerId));
253 
254     handlerId = 2;
255     ASSERT_NO_FATAL_FAILURE(manager.OnPreKeyEvent(keyEvent, handlerId));
256 }
257 #endif
258 
259 /**
260  * @tc.name: PreMonitorManagerTest_GetNextId_001
261  * @tc.desc: Test GetNextId
262  * @tc.type: FUNC
263  * @tc.require:
264  */
265 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_GetNextId_001, TestSize.Level1)
266 {
267     CALL_TEST_DEBUG;
268     PreMonitorManager manager;
269     manager.nextId_ = std::numeric_limits<int32_t>::max();
270     int32_t result = manager.GetNextId();
271     ASSERT_EQ(result, INVALID_HANDLER_ID);
272 }
273 
274 /**
275  * @tc.name: PreMonitorManagerTest_GetNextId_002
276  * @tc.desc: Test GetNextId
277  * @tc.type: FUNC
278  * @tc.require:
279  */
280 HWTEST_F(PreMonitorManagerTest, PreMonitorManagerTest_GetNextId_002, TestSize.Level1)
281 {
282     CALL_TEST_DEBUG;
283     PreMonitorManager manager;
284     manager.nextId_ = 5;
285     int32_t result = manager.GetNextId();
286     ASSERT_EQ(result, 5);
287 }
288 } // namespace MMI
289 } // namespace OHOS
290