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 #include "input_device_mgr_test.h"
16
17 #include "cooperate_context.h"
18 #include "cooperate_free.h"
19 #include "cooperate_in.h"
20 #include "cooperate_out.h"
21 #include "ddm_adapter.h"
22 #include "device.h"
23 #include "dsoftbus_adapter.h"
24 #include "i_device.h"
25 #include "i_cooperate_state.h"
26 #include "input_adapter.h"
27 #include "input_device_mgr.h"
28 #include "ipc_skeleton.h"
29 #include "mouse_location.h"
30 #include "socket_session.h"
31 #include "state_machine.h"
32
33 namespace OHOS {
34 namespace Msdp {
35 namespace DeviceStatus {
36 using namespace testing::ext;
37 using namespace Cooperate;
38 namespace {
39 const std::string TEST_DEV_NODE { "/dev/input/TestDeviceNode" };
40 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
41 constexpr int32_t HOTAREA_500 { 500 };
42 constexpr int32_t HOTAREA_NEGATIVE_500 { -500 };
43 constexpr int32_t HOTAREA_NEGATIVE_200 { -200 };
44 constexpr int32_t HOTAREA_250 { 250 };
45 constexpr int32_t HOTAREA_200 { 200 };
46 constexpr int32_t HOTAREA_150 { 150 };
47 constexpr int32_t HOTAREA_50 { 50 };
48 constexpr int32_t VREMOTE_NETWORKID { 987654321 };
49 std::shared_ptr<Context> g_context { nullptr };
50 std::shared_ptr<Context> g_contextOne { nullptr };
51 std::shared_ptr<HotplugObserver> g_observer { nullptr };
52 ContextService *g_instance = nullptr;
53 IContext *g_icontext { nullptr };
54 std::shared_ptr<SocketSession> g_session { nullptr };
55 DelegateTasks g_delegateTasks;
56 DeviceManager g_devMgr;
57 TimerManager g_timerMgr;
58 DragManager g_dragMgr;
59 SocketSessionManager g_socketSessionMgr;
60 std::unique_ptr<IDDMAdapter> g_ddm { nullptr };
61 std::unique_ptr<IInputAdapter> g_input { nullptr };
62 std::unique_ptr<IPluginManager> g_pluginMgr { nullptr };
63 std::unique_ptr<IDSoftbusAdapter> g_dsoftbus { nullptr };
64 std::shared_ptr<Cooperate::StateMachine> g_stateMachine { nullptr };
65 const std::string LOCAL_NETWORKID { "testLocalNetworkId" };
66 const std::string REMOTE_NETWORKID { "testRemoteNetworkId" };
67 } // namespace
68
ContextService()69 ContextService::ContextService()
70 {
71 }
72
~ContextService()73 ContextService::~ContextService()
74 {
75 }
76
GetDelegateTasks()77 IDelegateTasks& ContextService::GetDelegateTasks()
78 {
79 return g_delegateTasks;
80 }
81
GetDeviceManager()82 IDeviceManager& ContextService::GetDeviceManager()
83 {
84 return g_devMgr;
85 }
86
GetTimerManager()87 ITimerManager& ContextService::GetTimerManager()
88 {
89 return g_timerMgr;
90 }
91
GetDragManager()92 IDragManager& ContextService::GetDragManager()
93 {
94 return g_dragMgr;
95 }
96
GetInstance()97 ContextService* ContextService::GetInstance()
98 {
99 static std::once_flag flag;
100 std::call_once(flag, [&]() {
101 ContextService *cooContext = new (std::nothrow) ContextService();
102 CHKPL(cooContext);
103 g_instance = cooContext;
104 });
105 return g_instance;
106 }
107
GetSocketSessionManager()108 ISocketSessionManager& ContextService::GetSocketSessionManager()
109 {
110 return g_socketSessionMgr;
111 }
112
GetDDM()113 IDDMAdapter& ContextService::GetDDM()
114 {
115 return *g_ddm;
116 }
117
GetPluginManager()118 IPluginManager& ContextService::GetPluginManager()
119 {
120 return *g_pluginMgr;
121 }
122
GetInput()123 IInputAdapter& ContextService::GetInput()
124 {
125 return *g_input;
126 }
127
GetDSoftbus()128 IDSoftbusAdapter& ContextService::GetDSoftbus()
129 {
130 return *g_dsoftbus;
131 }
132
NotifyCooperate()133 void InputDeviceMgrTest::NotifyCooperate()
134 {
135 int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
136 EventManager::CooperateStateNotice cooperateStateNotice{IPCSkeleton::GetCallingPid(),
137 MessageId::COORDINATION_MESSAGE, 1, true, errCode};
138 g_contextOne->eventMgr_.NotifyCooperateState(cooperateStateNotice);
139 g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
140 g_socketSessionMgr.AddSession(g_session);
141 g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
142 g_context->eventMgr_.GetCooperateState(cooperateStateNotice);
143 }
144
CheckInHot()145 void InputDeviceMgrTest::CheckInHot()
146 {
147 g_context->hotArea_.displayX_ = 0;
148 g_context->hotArea_.height_ = HOTAREA_500;
149 g_context->hotArea_.displayY_ = HOTAREA_250;
150 g_context->hotArea_.CheckInHotArea();
151 g_context->hotArea_.width_ = HOTAREA_200;
152 g_context->hotArea_.displayX_ = HOTAREA_150;
153 g_context->hotArea_.height_ = HOTAREA_500;
154 g_context->hotArea_.displayY_ = HOTAREA_250;
155 g_context->hotArea_.CheckInHotArea();
156 g_context->hotArea_.displayY_ = HOTAREA_50;
157 g_context->hotArea_.width_ = HOTAREA_500;
158 g_context->hotArea_.displayX_ = HOTAREA_250;
159 g_context->hotArea_.CheckInHotArea();
160 g_context->hotArea_.height_ = HOTAREA_500;
161 g_context->hotArea_.displayY_ = HOTAREA_500;
162 g_context->hotArea_.width_ = HOTAREA_500;
163 g_context->hotArea_.displayX_ = HOTAREA_250;
164 g_context->hotArea_.CheckInHotArea();
165 g_context->hotArea_.height_ = HOTAREA_500;
166 g_context->hotArea_.displayY_ = HOTAREA_NEGATIVE_500;
167 g_context->hotArea_.width_ = HOTAREA_500;
168 g_context->hotArea_.displayX_ = HOTAREA_NEGATIVE_200;
169 g_context->hotArea_.CheckInHotArea();
170 }
171
SetUpTestCase()172 void InputDeviceMgrTest::SetUpTestCase() {}
173
SetUp()174 void InputDeviceMgrTest::SetUp()
175 {
176 g_ddm = std::make_unique<DDMAdapter>();
177 g_input = std::make_unique<InputAdapter>();
178 g_dsoftbus = std::make_unique<DSoftbusAdapter>();
179 g_contextOne = std::make_shared<Context>(g_icontext);
180 auto env = ContextService::GetInstance();
181 g_context = std::make_shared<Context>(env);
182 int32_t moduleType = 1;
183 int32_t tokenType = 1;
184 int32_t uid = IPCSkeleton::GetCallingUid();
185 int32_t pid = IPCSkeleton::GetCallingPid();
186 int32_t sockFds[2] { -1, -1 };
187 g_session = std::make_shared<SocketSession>("test", moduleType, tokenType, sockFds[0], uid, pid);
188 }
189
TearDown()190 void InputDeviceMgrTest::TearDown()
191 {
192 g_context = nullptr;
193 g_contextOne = nullptr;
194 g_session = nullptr;
195 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
196 }
197
OnThreeStates(const CooperateEvent & event)198 void InputDeviceMgrTest::OnThreeStates(const CooperateEvent &event)
199 {
200 auto env = ContextService::GetInstance();
201 Context cooperateContext(env);
202 g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
203 g_stateMachine->current_ = CooperateState::COOPERATE_STATE_OUT;
204 g_stateMachine->OnEvent(cooperateContext, event);
205 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
206 g_stateMachine->current_ = CooperateState::COOPERATE_STATE_IN;
207 g_stateMachine->OnEvent(cooperateContext, event);
208 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
209 g_stateMachine->current_ = CooperateState::COOPERATE_STATE_FREE;
210 g_stateMachine->OnEvent(cooperateContext, event);
211 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
212 }
213
214 class CooperateObserver final : public ICooperateObserver {
215 public:
216 CooperateObserver() = default;
217 virtual ~CooperateObserver() = default;
218
IsAllowCooperate()219 virtual bool IsAllowCooperate()
220 {
221 return true;
222 }
OnStartCooperate(StartCooperateData & data)223 virtual void OnStartCooperate(StartCooperateData &data) {}
OnRemoteStartCooperate(RemoteStartCooperateData & data)224 virtual void OnRemoteStartCooperate(RemoteStartCooperateData &data) {}
OnStopCooperate(const std::string & remoteNetworkId)225 virtual void OnStopCooperate(const std::string &remoteNetworkId) {}
OnTransitionOut(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)226 virtual void OnTransitionOut(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnTransitionIn(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)227 virtual void OnTransitionIn(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnBack(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)228 virtual void OnBack(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnRelay(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)229 virtual void OnRelay(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnReset()230 virtual void OnReset() {}
CloseDistributedFileConnection(const std::string & remoteNetworkId)231 virtual void CloseDistributedFileConnection(const std::string &remoteNetworkId) {}
232 };
233
234 /**
235 * @tc.name: inputDevcieMgr_test001
236 * @tc.desc: Test cooperate plugin
237 * @tc.type: FUNC
238 * @tc.require:
239 */
240 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test001, TestSize.Level1)
241 {
242 CALL_TEST_DEBUG;
243 auto [sender, receiver] = Channel<CooperateEvent>::OpenChannel();
244 g_context->AttachSender(sender);
245 g_context->inputDevMgr_.Enable(sender);
246 bool switchStatus = false;
247 DSoftbusSessionOpened notice = {
248 .networkId = LOCAL_NETWORKID,
249 .normal = switchStatus,
250 };
251 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.OnSoftbusSessionOpened(notice));
252 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.OnSoftbusSessionClosed(notice));
253 }
254
255 /**
256 * @tc.name: inputDevcieMgr_test002
257 * @tc.desc: Test cooperate plugin
258 * @tc.type: FUNC
259 * @tc.require:
260 */
261 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test002, TestSize.Level1)
262 {
263 CALL_TEST_DEBUG;
264 DSoftbusSyncInputDevice dSoftbusSyncInputDevice {};
265 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.OnRemoteInputDevice(dSoftbusSyncInputDevice));
266 DSoftbusHotPlugEvent dSoftbusHotPlugEvent {};
267 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.OnRemoteHotPlug(dSoftbusHotPlugEvent));
268 }
269
270 /**
271 * @tc.name: inputDevcieMgr_test003
272 * @tc.desc: Test cooperate plugin HandleRemoteHotPlug
273 * @tc.type: FUNC
274 * @tc.require:
275 */
276 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test003, TestSize.Level1)
277 {
278 CALL_TEST_DEBUG;
279 DSoftbusHotPlugEvent notice1 = {
280 .networkId = LOCAL_NETWORKID,
281 .type = InputHotplugType::PLUG,
282 .device = std::make_shared<Device>(VREMOTE_NETWORKID),
283 };
284 DSoftbusHotPlugEvent notice2 = {
285 .networkId = LOCAL_NETWORKID,
286 .type = InputHotplugType::UNPLUG,
287 .device = std::make_shared<Device>(VREMOTE_NETWORKID),
288 };
289 g_context->inputDevMgr_.AddVirtualInputDevice(notice1.networkId, 987654321);
290 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.HandleRemoteHotPlug(notice2));
291 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.HandleRemoteHotPlug(notice1));
292 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.HandleRemoteHotPlug(notice2));
293 }
294
295 /**
296 * @tc.name: inputDevcieMgr_test004
297 * @tc.desc: Test cooperate plugin
298 * @tc.type: FUNC
299 * @tc.require:
300 */
301 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test004, TestSize.Level1)
302 {
303 CALL_TEST_DEBUG;
304 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.NotifyInputDeviceToRemote(REMOTE_NETWORKID));
305 std::shared_ptr<IDevice> g_device = std::make_shared<Device>(VREMOTE_NETWORKID);
306 g_context->inputDevMgr_.AddRemoteInputDevice(LOCAL_NETWORKID, g_device);
307 g_context->inputDevMgr_.AddRemoteInputDevice(LOCAL_NETWORKID, g_device);
308 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.RemoveRemoteInputDevice(LOCAL_NETWORKID, g_device));
309 g_context->inputDevMgr_.AddRemoteInputDevice(LOCAL_NETWORKID, g_device);
310 g_context->inputDevMgr_.RemoveAllRemoteInputDevice(REMOTE_NETWORKID);
311 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.RemoveAllRemoteInputDevice(LOCAL_NETWORKID));
312 g_context->inputDevMgr_.DumpRemoteInputDevice(LOCAL_NETWORKID);
313 g_context->inputDevMgr_.AddRemoteInputDevice(LOCAL_NETWORKID, g_device);
314 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.DumpRemoteInputDevice(LOCAL_NETWORKID));
315 NetPacket packet(MessageId::DSOFTBUS_INPUT_DEV_SYNC);
316 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.SerializeDevice(g_device, packet));
317 g_device = nullptr;
318 }
319
320 /**
321 * @tc.name: inputDevcieMgr_test005
322 * @tc.desc: Test cooperate plugin HandleRemoteHotPlug
323 * @tc.type: FUNC
324 * @tc.require:
325 */
326 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test005, TestSize.Level1)
327 {
328 CALL_TEST_DEBUG;
329 std::shared_ptr<IDevice> g_device = std::make_shared<Device>(VREMOTE_NETWORKID);
330 NetPacket pkt1(MessageId::INVALID);
331 int32_t ret = g_context->dsoftbus_.DeserializeDevice(g_device, pkt1);
332 EXPECT_EQ(ret, RET_ERR);
333 NetPacket pkt2(MessageId::DSOFTBUS_INPUT_DEV_SYNC);
334 ret = g_context->dsoftbus_.DeserializeDevice(g_device, pkt2);
335 EXPECT_EQ(ret, RET_ERR);
336 NetPacket pkt3(MessageId::DSOFTBUS_INPUT_DEV_HOT_PLUG);
337 ret = g_context->dsoftbus_.DeserializeDevice(g_device, pkt3);
338 EXPECT_EQ(ret, RET_ERR);
339 }
340
341 /**
342 * @tc.name: inputDevcieMgr_test006
343 * @tc.desc: Test cooperate plugin
344 * @tc.type: FUNC
345 * @tc.require:
346 */
347 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test006, TestSize.Level1)
348 {
349 CALL_TEST_DEBUG;
350 std::shared_ptr<IDevice> g_device = std::make_shared<Device>(VREMOTE_NETWORKID);
351 auto inputDev = std::make_shared<MMI::InputDevice>();
352 g_device->AddCapability(Device::Capability::DEVICE_CAP_MAX);
353 inputDev = g_context->inputDevMgr_.Transform(g_device);
354 g_device->AddCapability(Device::Capability::DEVICE_CAP_KEYBOARD);
355 inputDev = g_context->inputDevMgr_.Transform(g_device);
356 EXPECT_NE(inputDev, nullptr);
357 g_device->AddCapability(Device::Capability::DEVICE_CAP_POINTER);
358 inputDev = g_context->inputDevMgr_.Transform(g_device);
359 EXPECT_NE(inputDev, nullptr);
360 }
361
362 /**
363 * @tc.name: inputDevcieMgr_test007
364 * @tc.desc: Test cooperate plugin GetRemoteDeviceById
365 * @tc.type: FUNC
366 * @tc.require:
367 */
368 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test007, TestSize.Level1)
369 {
370 CALL_TEST_DEBUG;
371 std::shared_ptr<IDevice> g_device = std::make_shared<Device>(VREMOTE_NETWORKID);
372 auto ret = g_context->inputDevMgr_.GetRemoteDeviceById(LOCAL_NETWORKID, VREMOTE_NETWORKID);
373 EXPECT_EQ(ret, nullptr);
374 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.RemoveVirtualInputDevice(LOCAL_NETWORKID, VREMOTE_NETWORKID));
375 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.AddRemoteInputDevice(LOCAL_NETWORKID, g_device));
376 ret = g_context->inputDevMgr_.GetRemoteDeviceById(LOCAL_NETWORKID, VREMOTE_NETWORKID);
377 EXPECT_NE(ret, nullptr);
378 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.AddVirtualInputDevice(LOCAL_NETWORKID, VREMOTE_NETWORKID));
379 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.RemoveVirtualInputDevice(LOCAL_NETWORKID, VREMOTE_NETWORKID));
380 }
381
382 /**
383 * @tc.name: inputDevcieMgr_test008
384 * @tc.desc: Test cooperate plugin
385 * @tc.type: FUNC
386 * @tc.require:
387 */
388 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test008, TestSize.Level1)
389 {
390 CALL_TEST_DEBUG;
391 auto env = ContextService::GetInstance();
392 std::string TEST_DEV_NODE { "/dev/input/TestDeviceNode" };
393 env->devMgr_.AddDevice(TEST_DEV_NODE);
394 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.NotifyInputDeviceToRemote(REMOTE_NETWORKID));
395 }
396
397 /**
398 * @tc.name: inputDevcieMgr_test009
399 * @tc.desc: Test cooperate plugin
400 * @tc.type: FUNC
401 * @tc.require:
402 */
403 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test009, TestSize.Level1)
404 {
405 CALL_TEST_DEBUG;
406 DeviceStatus::InputHotplugEvent inputHotplugEvent;
407 inputHotplugEvent.isKeyboard = true;
408 inputHotplugEvent.deviceId = 1;
409 inputHotplugEvent.type = InputHotplugType::UNPLUG;
410 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.BroadcastHotPlugToRemote(inputHotplugEvent));
411 }
412
413 /**
414 * @tc.name: inputDevcieMgr_test010
415 * @tc.desc: Test cooperate plugin
416 * @tc.type: FUNC
417 * @tc.require:
418 */
419 HWTEST_F(InputDeviceMgrTest, inputDevcieMgr_test010, TestSize.Level1)
420 {
421 CALL_TEST_DEBUG;
422 DeviceStatus::InputHotplugEvent inputHotplugEvent;
423 inputHotplugEvent.isKeyboard = true;
424 inputHotplugEvent.deviceId = 1;
425 inputHotplugEvent.type = InputHotplugType::PLUG;
426 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.BroadcastHotPlugToRemote(inputHotplugEvent));
427 }
428
429 /**
430 * @tc.name: stateMachine_test011
431 * @tc.desc: Test cooperate plugin
432 * @tc.type: FUNC
433 * @tc.require:
434 */
435 HWTEST_F(InputDeviceMgrTest, stateMachine_test011, TestSize.Level1)
436 {
437 CALL_TEST_DEBUG;
438 g_context->inputDevMgr_.enable_ = true;
439 auto [sender, receiver] = Channel<CooperateEvent>::OpenChannel();
440 g_context->AttachSender(sender);
441 ASSERT_NO_FATAL_FAILURE(g_context->inputDevMgr_.Enable(sender));
442 }
443 } // namespace DeviceStatus
444 } // namespace Msdp
445 } // namespace OHOS