• 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 #include "mouse_location_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 "ipc_skeleton.h"
28 #include "mouse_location.h"
29 #include "socket_session.h"
30 #include "state_machine.h"
31 
32 namespace OHOS {
33 namespace Msdp {
34 namespace DeviceStatus {
35 using namespace testing::ext;
36 using namespace Cooperate;
37 namespace {
38 const std::string TEST_DEV_NODE { "/dev/input/TestDeviceNode" };
39 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
40 constexpr int32_t HOTAREA_500 { 500 };
41 constexpr int32_t HOTAREA_NEGATIVE_500 { -500 };
42 constexpr int32_t HOTAREA_NEGATIVE_200 { -200 };
43 constexpr int32_t HOTAREA_250 { 250 };
44 constexpr int32_t HOTAREA_200 { 200 };
45 constexpr int32_t HOTAREA_150 { 150 };
46 constexpr int32_t HOTAREA_50 { 50 };
47 std::shared_ptr<Context> g_context { nullptr };
48 std::shared_ptr<Context> g_contextOne { nullptr };
49 std::shared_ptr<HotplugObserver> g_observer { nullptr };
50 ContextService *g_instance = nullptr;
51 IContext *g_icontext { nullptr };
52 std::shared_ptr<SocketSession> g_session { nullptr };
53 DelegateTasks g_delegateTasks;
54 DeviceManager g_devMgr;
55 TimerManager g_timerMgr;
56 DragManager g_dragMgr;
57 SocketSessionManager g_socketSessionMgr;
58 std::unique_ptr<IDDMAdapter> g_ddm { nullptr };
59 std::unique_ptr<IInputAdapter> g_input { nullptr };
60 std::unique_ptr<IPluginManager> g_pluginMgr { nullptr };
61 std::unique_ptr<IDSoftbusAdapter> g_dsoftbus { nullptr };
62 std::shared_ptr<Cooperate::StateMachine> g_stateMachine { nullptr };
63 const std::string LOCAL_NETWORKID { "testLocalNetworkId" };
64 const std::string REMOTE_NETWORKID { "testRemoteNetworkId" };
65 } // namespace
66 
ContextService()67 ContextService::ContextService()
68 {
69 }
70 
~ContextService()71 ContextService::~ContextService()
72 {
73 }
74 
GetDelegateTasks()75 IDelegateTasks& ContextService::GetDelegateTasks()
76 {
77     return g_delegateTasks;
78 }
79 
GetDeviceManager()80 IDeviceManager& ContextService::GetDeviceManager()
81 {
82     return g_devMgr;
83 }
84 
GetTimerManager()85 ITimerManager& ContextService::GetTimerManager()
86 {
87     return g_timerMgr;
88 }
89 
GetDragManager()90 IDragManager& ContextService::GetDragManager()
91 {
92     return g_dragMgr;
93 }
94 
GetInstance()95 ContextService* ContextService::GetInstance()
96 {
97     static std::once_flag flag;
98     std::call_once(flag, [&]() {
99         ContextService *cooContext = new (std::nothrow) ContextService();
100         CHKPL(cooContext);
101         g_instance = cooContext;
102     });
103     return g_instance;
104 }
105 
GetSocketSessionManager()106 ISocketSessionManager& ContextService::GetSocketSessionManager()
107 {
108     return g_socketSessionMgr;
109 }
110 
GetDDM()111 IDDMAdapter& ContextService::GetDDM()
112 {
113     return *g_ddm;
114 }
115 
GetPluginManager()116 IPluginManager& ContextService::GetPluginManager()
117 {
118     return *g_pluginMgr;
119 }
120 
GetInput()121 IInputAdapter& ContextService::GetInput()
122 {
123     return *g_input;
124 }
125 
GetDSoftbus()126 IDSoftbusAdapter& ContextService::GetDSoftbus()
127 {
128     return *g_dsoftbus;
129 }
130 
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)131 MMI::PointerEvent::PointerItem MouseLocationTest::CreatePointerItem(int32_t pointerId, int32_t deviceId,
132     const std::pair<int32_t, int32_t> &displayLocation, bool isPressed)
133 {
134     MMI::PointerEvent::PointerItem item;
135     item.SetPointerId(pointerId);
136     item.SetDeviceId(deviceId);
137     item.SetDisplayX(displayLocation.first);
138     item.SetDisplayY(displayLocation.second);
139     item.SetPressed(isPressed);
140     return item;
141 }
142 
NotifyCooperate()143 void MouseLocationTest::NotifyCooperate()
144 {
145     int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
146     EventManager::CooperateStateNotice cooperateStateNotice{IPCSkeleton::GetCallingPid(),
147         MessageId::COORDINATION_MESSAGE, 1, true, errCode};
148     g_contextOne->eventMgr_.NotifyCooperateState(cooperateStateNotice);
149     g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
150     g_socketSessionMgr.AddSession(g_session);
151     g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
152     g_context->eventMgr_.GetCooperateState(cooperateStateNotice);
153 }
154 
CheckInHot()155 void MouseLocationTest::CheckInHot()
156 {
157     g_context->hotArea_.displayX_ = 0;
158     g_context->hotArea_.height_ = HOTAREA_500;
159     g_context->hotArea_.displayY_ = HOTAREA_250;
160     g_context->hotArea_.CheckInHotArea();
161     g_context->hotArea_.width_ = HOTAREA_200;
162     g_context->hotArea_.displayX_ = HOTAREA_150;
163     g_context->hotArea_.height_ = HOTAREA_500;
164     g_context->hotArea_.displayY_ = HOTAREA_250;
165     g_context->hotArea_.CheckInHotArea();
166     g_context->hotArea_.displayY_ = HOTAREA_50;
167     g_context->hotArea_.width_ = HOTAREA_500;
168     g_context->hotArea_.displayX_ = HOTAREA_250;
169     g_context->hotArea_.CheckInHotArea();
170     g_context->hotArea_.height_ = HOTAREA_500;
171     g_context->hotArea_.displayY_ = HOTAREA_500;
172     g_context->hotArea_.width_ = HOTAREA_500;
173     g_context->hotArea_.displayX_ = HOTAREA_250;
174     g_context->hotArea_.CheckInHotArea();
175     g_context->hotArea_.height_ = HOTAREA_500;
176     g_context->hotArea_.displayY_ = HOTAREA_NEGATIVE_500;
177     g_context->hotArea_.width_ = HOTAREA_500;
178     g_context->hotArea_.displayX_ = HOTAREA_NEGATIVE_200;
179     g_context->hotArea_.CheckInHotArea();
180 }
181 
SetUpTestCase()182 void MouseLocationTest::SetUpTestCase() {}
183 
SetUp()184 void MouseLocationTest::SetUp()
185 {
186     g_ddm = std::make_unique<DDMAdapter>();
187     g_input = std::make_unique<InputAdapter>();
188     g_dsoftbus = std::make_unique<DSoftbusAdapter>();
189     g_contextOne = std::make_shared<Context>(g_icontext);
190     auto env = ContextService::GetInstance();
191     g_context = std::make_shared<Context>(env);
192     int32_t moduleType = 1;
193     int32_t tokenType = 1;
194     int32_t uid = IPCSkeleton::GetCallingUid();
195     int32_t pid = IPCSkeleton::GetCallingPid();
196     int32_t sockFds[2] { -1, -1 };
197     g_session = std::make_shared<SocketSession>("test", moduleType, tokenType, sockFds[0], uid, pid);
198 }
199 
TearDown()200 void MouseLocationTest::TearDown()
201 {
202     g_context = nullptr;
203     g_contextOne = nullptr;
204     g_session = nullptr;
205     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
206 }
207 
OnThreeStates(const CooperateEvent & event)208 void MouseLocationTest::OnThreeStates(const CooperateEvent &event)
209 {
210     auto env = ContextService::GetInstance();
211     Context cooperateContext(env);
212     g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
213     g_stateMachine->current_ = CooperateState::COOPERATE_STATE_OUT;
214     g_stateMachine->OnEvent(cooperateContext, event);
215     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
216     g_stateMachine->current_ = CooperateState::COOPERATE_STATE_IN;
217     g_stateMachine->OnEvent(cooperateContext, event);
218     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
219     g_stateMachine->current_ = CooperateState::COOPERATE_STATE_FREE;
220     g_stateMachine->OnEvent(cooperateContext, event);
221     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
222 }
223 
224 class CooperateObserver final : public ICooperateObserver {
225 public:
226     CooperateObserver() = default;
227     virtual ~CooperateObserver() = default;
228 
IsAllowCooperate()229     virtual bool IsAllowCooperate()
230     {
231         return true;
232     }
OnStartCooperate(StartCooperateData & data)233     virtual void OnStartCooperate(StartCooperateData &data) {}
OnRemoteStartCooperate(RemoteStartCooperateData & data)234     virtual void OnRemoteStartCooperate(RemoteStartCooperateData &data) {}
OnStopCooperate(const std::string & remoteNetworkId)235     virtual void OnStopCooperate(const std::string &remoteNetworkId) {}
OnTransitionOut(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)236     virtual void OnTransitionOut(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnTransitionIn(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)237     virtual void OnTransitionIn(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnBack(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)238     virtual void OnBack(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnRelay(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)239     virtual void OnRelay(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnReset()240     virtual void OnReset() {}
CloseDistributedFileConnection(const std::string & remoteNetworkId)241     virtual void CloseDistributedFileConnection(const std::string &remoteNetworkId) {}
242 };
243 
244 /**
245  * @tc.name: MouseLocationTest1
246  * @tc.desc: MouseLocation
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(MouseLocationTest, MouseLocationTest001, TestSize.Level1)
251 {
252     CALL_TEST_DEBUG;
253     Cooperate::RegisterEventListenerEvent registerEventListenerEvent {};
254     g_contextOne->mouseLocation_.AddListener(registerEventListenerEvent);
255     g_contextOne->mouseLocation_.RemoveListener(registerEventListenerEvent);
256     DSoftbusSubscribeMouseLocation dSoftbusSubscribeMouseLocation {};
257     g_contextOne->mouseLocation_.OnSubscribeMouseLocation(dSoftbusSubscribeMouseLocation);
258     g_contextOne->mouseLocation_.OnUnSubscribeMouseLocation(dSoftbusSubscribeMouseLocation);
259 
260     g_context->mouseLocation_.AddListener(registerEventListenerEvent);
261     g_context->mouseLocation_.RemoveListener(registerEventListenerEvent);
262 
263     Cooperate::RegisterEventListenerEvent registerEventListenerEvent1 {IPCSkeleton::GetCallingPid(), "test"};
264     g_context->mouseLocation_.AddListener(registerEventListenerEvent1);
265     g_context->mouseLocation_.ProcessData(MMI::PointerEvent::Create());
266     g_context->mouseLocation_.RemoveListener(registerEventListenerEvent1);
267     Cooperate::LocationInfo locationInfo {1, 1, 1, 1};
268 
269     DSoftbusSyncMouseLocation dSoftbusSyncMouseLocation{"test", "test", locationInfo};
270     g_context->mouseLocation_.OnRemoteMouseLocation(dSoftbusSyncMouseLocation);
271 
272     g_context->mouseLocation_.listeners_.clear();
273     g_context->mouseLocation_.RemoveListener(registerEventListenerEvent1);
274     g_context->mouseLocation_.OnRemoteMouseLocation(dSoftbusSyncMouseLocation);
275 
276     DSoftbusSubscribeMouseLocation dSoftbusSubscribeMouseLocation1 {"test", "test1"};
277     g_context->mouseLocation_.OnSubscribeMouseLocation(dSoftbusSubscribeMouseLocation1);
278     g_context->mouseLocation_.ProcessData(MMI::PointerEvent::Create());
279     g_context->mouseLocation_.OnUnSubscribeMouseLocation(dSoftbusSubscribeMouseLocation1);
280 
281     g_context->mouseLocation_.remoteSubscribers_.clear();
282     g_context->mouseLocation_.ProcessData(MMI::PointerEvent::Create());
283     g_context->mouseLocation_.OnUnSubscribeMouseLocation(dSoftbusSubscribeMouseLocation1);
284 
285     DSoftbusReplySubscribeMouseLocation dSoftbusReplySubscribeMouseLocation {"test", "test1", true};
286     g_context->mouseLocation_.OnReplySubscribeMouseLocation(dSoftbusReplySubscribeMouseLocation);
287     g_context->mouseLocation_.OnReplyUnSubscribeMouseLocation(dSoftbusReplySubscribeMouseLocation);
288     DSoftbusReplySubscribeMouseLocation dSoftbusReplySubscribeMouseLocation1 {"test", "test1", false};
289     g_context->mouseLocation_.OnReplySubscribeMouseLocation(dSoftbusReplySubscribeMouseLocation1);
290     g_context->mouseLocation_.OnReplyUnSubscribeMouseLocation(dSoftbusReplySubscribeMouseLocation1);
291 
292     int32_t ret = g_context->mouseLocation_.ReplySubscribeMouseLocation(dSoftbusReplySubscribeMouseLocation);
293     EXPECT_EQ(ret, RET_ERR);
294 }
295 
296 /**
297  * @tc.name: MouseLocationTest2
298  * @tc.desc: MouseLocation
299  * @tc.type: FUNC
300  * @tc.require:
301  */
302 HWTEST_F(MouseLocationTest, MouseLocationTest002, TestSize.Level1)
303 {
304     CALL_TEST_DEBUG;
305     Cooperate::RegisterEventListenerEvent registerEventListenerEvent1 {IPCSkeleton::GetCallingPid(), "test"};
306     g_context->mouseLocation_.AddListener(registerEventListenerEvent1);
307     g_socketSessionMgr.Enable();
308     g_socketSessionMgr.AddSession(g_session);
309     g_contextOne->mouseLocation_.ReportMouseLocationToListener("test", {}, IPCSkeleton::GetCallingPid());
310     g_context->mouseLocation_.ReportMouseLocationToListener("test", {}, IPCSkeleton::GetCallingPid());
311     g_context->mouseLocation_.localListeners_.clear();
312     bool ret = g_context->mouseLocation_.HasLocalListener();
313     EXPECT_FALSE(ret);
314 }
315 
316 /**
317  * @tc.name: MouseLocationTest3
318  * @tc.desc: MouseLocation
319  * @tc.type: FUNC
320  * @tc.require:
321  */
322 HWTEST_F(MouseLocationTest, MouseLocationTest003, TestSize.Level1)
323 {
324     CALL_TEST_DEBUG;
325     g_context->mouseLocation_.ProcessData(nullptr);
326     auto pointerEvent = MMI::PointerEvent::Create();
327     MMI::PointerEvent::PointerItem pointerItem;
328     pointerEvent->SetPointerId(1);
329     MMI::PointerEvent::PointerItem curPointerItem = CreatePointerItem(1, 1, { 0, 0 }, true);
330     pointerEvent->AddPointerItem(curPointerItem);
331     g_context->mouseLocation_.ProcessData(pointerEvent);
332     NetPacket pkt(MessageId::COORDINATION_MESSAGE);
333     int32_t ret = g_context->mouseLocation_.SendPacket("test", pkt);
334     EXPECT_EQ(ret, RET_ERR);
335 }
336 
337 /**
338  * @tc.name: MouseLocationTest4
339  * @tc.desc: cooperate plugin
340  * @tc.type: FUNC
341  * @tc.require:
342  */
343 HWTEST_F(MouseLocationTest, MouseLocationTest004, TestSize.Level1)
344 {
345     CALL_TEST_DEBUG;
346     Cooperate::RegisterEventListenerEvent registerEventListenerEvent1 {IPCSkeleton::GetCallingPid(), "test"};
347     g_context->mouseLocation_.AddListener(registerEventListenerEvent1);
348     int32_t pid = 1;
349     CooperateEvent event(CooperateEventType::APP_CLOSED,
350         ClientDiedEvent {
351             .pid = pid,
352         });
353     ClientDiedEvent notice = std::get<ClientDiedEvent>(event.event);
354     g_context->mouseLocation_.listeners_["test"].insert(registerEventListenerEvent1.pid);
355     g_context->mouseLocation_.OnClientDied(notice);
356     g_context->mouseLocation_.RemoveListener(registerEventListenerEvent1);
357     bool ret = g_context->mouseLocation_.HasLocalListener();
358     EXPECT_FALSE(ret);
359 }
360 
361 /**
362  * @tc.name: MouseLocationTest5
363  * @tc.desc: cooperate plugin
364  * @tc.type: FUNC
365  * @tc.require:
366  */
367 HWTEST_F(MouseLocationTest, MouseLocationTest005, TestSize.Level1)
368 {
369     CALL_TEST_DEBUG;
370     Cooperate::RegisterEventListenerEvent registerEventListenerEvent1 {IPCSkeleton::GetCallingPid(), "test"};
371     g_context->mouseLocation_.AddListener(registerEventListenerEvent1);
372     std::string remoteNetworkId("test");
373     std::string networkId("test");
374     CooperateEvent event(
375        CooperateEventType::DSOFTBUS_MOUSE_LOCATION,
376         DSoftbusSyncMouseLocation {
377             .networkId = networkId,
378             .remoteNetworkId = remoteNetworkId,
379             .mouseLocation = {
380                 .displayX = 50,
381                 .displayY = 50,
382                 .displayWidth = 25,
383                 .displayHeight = 25,
384             },
385         });
386     DSoftbusSyncMouseLocation notice = std::get<DSoftbusSyncMouseLocation>(event.event);
387     g_context->mouseLocation_.SyncMouseLocation(notice);
388     g_context->mouseLocation_.RemoveListener(registerEventListenerEvent1);
389     bool ret = g_context->mouseLocation_.HasLocalListener();
390     EXPECT_FALSE(ret);
391 }
392 
393 /**
394  * @tc.name: MouseLocationTest6
395  * @tc.desc: cooperate plugin
396  * @tc.type: FUNC
397  * @tc.require:
398  */
399 HWTEST_F(MouseLocationTest, MouseLocationTest006, TestSize.Level1)
400 {
401     CALL_TEST_DEBUG;
402     Cooperate::DSoftbusSubscribeMouseLocation subscribeMouseLocation {
403         .networkId = "test",
404     };
405     g_context->mouseLocation_.OnSubscribeMouseLocation(subscribeMouseLocation);
406     CooperateEvent event(CooperateEventType::APP_CLOSED,
407         DDMBoardOnlineEvent {
408         .networkId = "test",
409         .normal = true,
410     });
411     DDMBoardOnlineEvent notice = std::get<DDMBoardOnlineEvent>(event.event);
412     g_context->mouseLocation_.OnSoftbusSessionClosed(notice);
413     auto pointerEvent = MMI::PointerEvent::Create();
414     MMI::PointerEvent::PointerItem pointerItem;
415     pointerEvent->SetPointerId(1);
416     pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_MOUSE);
417     MMI::PointerEvent::PointerItem curPointerItem = CreatePointerItem(1, 1, { 0, 0 }, true);
418     pointerEvent->AddPointerItem(curPointerItem);
419     g_context->mouseLocation_.ProcessData(pointerEvent);
420     g_context->mouseLocation_.OnUnSubscribeMouseLocation(subscribeMouseLocation);
421     bool ret = g_context->mouseLocation_.HasLocalListener();
422     EXPECT_FALSE(ret);
423 }
424 
425 /**
426  * @tc.name: MouseLocationTest7
427  * @tc.desc: cooperate plugin
428  * @tc.type: FUNC
429  * @tc.require:
430  */
431 HWTEST_F(MouseLocationTest, MouseLocationTest007, TestSize.Level1)
432 {
433     CALL_TEST_DEBUG;
434     Cooperate::RegisterEventListenerEvent registerEventListenerEvent1 {IPCSkeleton::GetCallingPid(), "test"};
435     g_context->mouseLocation_.AddListener(registerEventListenerEvent1);
436     Cooperate::DSoftbusSubscribeMouseLocation subscribeMouseLocation {
437         .networkId = "test",
438     };
439     g_context->mouseLocation_.OnSubscribeMouseLocation(subscribeMouseLocation);
440     CooperateEvent event(CooperateEventType::APP_CLOSED,
441         DDMBoardOnlineEvent {
442         .networkId = "test",
443         .normal = true,
444     });
445     DDMBoardOnlineEvent notice = std::get<DDMBoardOnlineEvent>(event.event);
446     g_context->mouseLocation_.OnSoftbusSessionClosed(notice);
447     auto pointerEvent = MMI::PointerEvent::Create();
448     MMI::PointerEvent::PointerItem pointerItem;
449     pointerEvent->SetPointerId(1);
450     pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_MOUSE);
451     MMI::PointerEvent::PointerItem curPointerItem = CreatePointerItem(1, 1, { 0, 0 }, true);
452     pointerEvent->AddPointerItem(curPointerItem);
453     g_context->mouseLocation_.localListeners_.insert(registerEventListenerEvent1.pid);
454     g_context->mouseLocation_.remoteSubscribers_.insert(subscribeMouseLocation.networkId);
455     g_context->mouseLocation_.ProcessData(pointerEvent);
456     ASSERT_NO_FATAL_FAILURE(g_context->mouseLocation_.OnUnSubscribeMouseLocation(subscribeMouseLocation));
457     g_context->mouseLocation_.localListeners_.clear();
458     g_context->mouseLocation_.remoteSubscribers_.clear();
459 }
460 
461 } // namespace DeviceStatus
462 } // namespace Msdp
463 } // namespace OHOS