• 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 "dsoftbus_handler_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 "dsoftbus_handler.h"
25 #include "i_device.h"
26 #include "i_cooperate_state.h"
27 #include "input_adapter.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 std::shared_ptr<Context> g_context { nullptr };
49 std::shared_ptr<Context> g_contextOne { nullptr };
50 std::shared_ptr<HotplugObserver> g_observer { nullptr };
51 ContextService *g_instance = nullptr;
52 IContext *g_icontext { nullptr };
53 std::shared_ptr<SocketSession> g_session { nullptr };
54 DelegateTasks g_delegateTasks;
55 DeviceManager g_devMgr;
56 TimerManager g_timerMgr;
57 DragManager g_dragMgr;
58 SocketSessionManager g_socketSessionMgr;
59 std::unique_ptr<IDDMAdapter> g_ddm { nullptr };
60 std::unique_ptr<IInputAdapter> g_input { nullptr };
61 std::unique_ptr<IPluginManager> g_pluginMgr { nullptr };
62 std::unique_ptr<IDSoftbusAdapter> g_dsoftbus { nullptr };
63 std::shared_ptr<Cooperate::StateMachine> g_stateMachine { nullptr };
64 const std::string LOCAL_NETWORKID { "testLocalNetworkId" };
65 const std::string REMOTE_NETWORKID { "testRemoteNetworkId" };
66 } // namespace
67 
ContextService()68 ContextService::ContextService()
69 {
70 }
71 
~ContextService()72 ContextService::~ContextService()
73 {
74 }
75 
GetDelegateTasks()76 IDelegateTasks& ContextService::GetDelegateTasks()
77 {
78     return g_delegateTasks;
79 }
80 
GetDeviceManager()81 IDeviceManager& ContextService::GetDeviceManager()
82 {
83     return g_devMgr;
84 }
85 
GetTimerManager()86 ITimerManager& ContextService::GetTimerManager()
87 {
88     return g_timerMgr;
89 }
90 
GetDragManager()91 IDragManager& ContextService::GetDragManager()
92 {
93     return g_dragMgr;
94 }
95 
GetInstance()96 ContextService* ContextService::GetInstance()
97 {
98     static std::once_flag flag;
99     std::call_once(flag, [&]() {
100         ContextService *cooContext = new (std::nothrow) ContextService();
101         CHKPL(cooContext);
102         g_instance = cooContext;
103     });
104     return g_instance;
105 }
106 
GetSocketSessionManager()107 ISocketSessionManager& ContextService::GetSocketSessionManager()
108 {
109     return g_socketSessionMgr;
110 }
111 
GetDDM()112 IDDMAdapter& ContextService::GetDDM()
113 {
114     return *g_ddm;
115 }
116 
GetPluginManager()117 IPluginManager& ContextService::GetPluginManager()
118 {
119     return *g_pluginMgr;
120 }
121 
GetInput()122 IInputAdapter& ContextService::GetInput()
123 {
124     return *g_input;
125 }
126 
GetDSoftbus()127 IDSoftbusAdapter& ContextService::GetDSoftbus()
128 {
129     return *g_dsoftbus;
130 }
131 
NotifyCooperate()132 void DsoftbusHanderTest::NotifyCooperate()
133 {
134     int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
135     EventManager::CooperateStateNotice cooperateStateNotice{IPCSkeleton::GetCallingPid(),
136         MessageId::COORDINATION_MESSAGE, 1, true, errCode};
137     g_contextOne->eventMgr_.NotifyCooperateState(cooperateStateNotice);
138     g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
139     g_socketSessionMgr.AddSession(g_session);
140     g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
141     g_context->eventMgr_.GetCooperateState(cooperateStateNotice);
142 }
143 
CheckInHot()144 void DsoftbusHanderTest::CheckInHot()
145 {
146     g_context->hotArea_.displayX_ = 0;
147     g_context->hotArea_.height_ = HOTAREA_500;
148     g_context->hotArea_.displayY_ = HOTAREA_250;
149     g_context->hotArea_.CheckInHotArea();
150     g_context->hotArea_.width_ = HOTAREA_200;
151     g_context->hotArea_.displayX_ = HOTAREA_150;
152     g_context->hotArea_.height_ = HOTAREA_500;
153     g_context->hotArea_.displayY_ = HOTAREA_250;
154     g_context->hotArea_.CheckInHotArea();
155     g_context->hotArea_.displayY_ = HOTAREA_50;
156     g_context->hotArea_.width_ = HOTAREA_500;
157     g_context->hotArea_.displayX_ = HOTAREA_250;
158     g_context->hotArea_.CheckInHotArea();
159     g_context->hotArea_.height_ = HOTAREA_500;
160     g_context->hotArea_.displayY_ = HOTAREA_500;
161     g_context->hotArea_.width_ = HOTAREA_500;
162     g_context->hotArea_.displayX_ = HOTAREA_250;
163     g_context->hotArea_.CheckInHotArea();
164     g_context->hotArea_.height_ = HOTAREA_500;
165     g_context->hotArea_.displayY_ = HOTAREA_NEGATIVE_500;
166     g_context->hotArea_.width_ = HOTAREA_500;
167     g_context->hotArea_.displayX_ = HOTAREA_NEGATIVE_200;
168     g_context->hotArea_.CheckInHotArea();
169 }
170 
SetUpTestCase()171 void DsoftbusHanderTest::SetUpTestCase() {}
172 
SetUp()173 void DsoftbusHanderTest::SetUp()
174 {
175     g_ddm = std::make_unique<DDMAdapter>();
176     g_input = std::make_unique<InputAdapter>();
177     g_dsoftbus = std::make_unique<DSoftbusAdapter>();
178     g_contextOne = std::make_shared<Context>(g_icontext);
179     auto env = ContextService::GetInstance();
180     g_context = std::make_shared<Context>(env);
181     int32_t moduleType = 1;
182     int32_t tokenType = 1;
183     int32_t uid = IPCSkeleton::GetCallingUid();
184     int32_t pid = IPCSkeleton::GetCallingPid();
185     int32_t sockFds[2] { -1, -1 };
186     g_session = std::make_shared<SocketSession>("test", moduleType, tokenType, sockFds[0], uid, pid);
187 }
188 
TearDown()189 void DsoftbusHanderTest::TearDown()
190 {
191     g_context = nullptr;
192     g_contextOne = nullptr;
193     g_session = nullptr;
194     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
195 }
196 
OnThreeStates(const CooperateEvent & event)197 void DsoftbusHanderTest::OnThreeStates(const CooperateEvent &event)
198 {
199     auto env = ContextService::GetInstance();
200     Context cooperateContext(env);
201     g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
202     g_stateMachine->current_ = CooperateState::COOPERATE_STATE_OUT;
203     g_stateMachine->OnEvent(cooperateContext, event);
204     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
205     g_stateMachine->current_ = CooperateState::COOPERATE_STATE_IN;
206     g_stateMachine->OnEvent(cooperateContext, event);
207     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
208     g_stateMachine->current_ = CooperateState::COOPERATE_STATE_FREE;
209     g_stateMachine->OnEvent(cooperateContext, event);
210     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
211 }
212 
213 class CooperateObserver final : public ICooperateObserver {
214 public:
215     CooperateObserver() = default;
216     virtual ~CooperateObserver() = default;
217 
IsAllowCooperate()218     virtual bool IsAllowCooperate()
219     {
220         return true;
221     }
OnStartCooperate(StartCooperateData & data)222     virtual void OnStartCooperate(StartCooperateData &data) {}
OnRemoteStartCooperate(RemoteStartCooperateData & data)223     virtual void OnRemoteStartCooperate(RemoteStartCooperateData &data) {}
OnStopCooperate(const std::string & remoteNetworkId)224     virtual void OnStopCooperate(const std::string &remoteNetworkId) {}
OnTransitionOut(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)225     virtual void OnTransitionOut(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnTransitionIn(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)226     virtual void OnTransitionIn(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnBack(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)227     virtual void OnBack(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnRelay(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)228     virtual void OnRelay(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnReset()229     virtual void OnReset() {}
CloseDistributedFileConnection(const std::string & remoteNetworkId)230     virtual void CloseDistributedFileConnection(const std::string &remoteNetworkId) {}
231 };
232 
233 /**
234  * @tc.name: DsoftbusHanderTest001
235  * @tc.desc: cooperate plugin
236  * @tc.type: FUNC
237  * @tc.require:
238  */
239 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest001, TestSize.Level1)
240 {
241     CALL_TEST_DEBUG;
242     auto [sender, receiver] = Channel<CooperateEvent>::OpenChannel();
243     g_context->dsoftbus_.AttachSender(sender);
244     int32_t ret = g_context->dsoftbus_.OpenSession("test");
245     EXPECT_EQ(ret, RET_ERR);
246     g_context->dsoftbus_.CloseSession("test");
247     g_context->dsoftbus_.CloseAllSessions();
248 }
249 
250 /**
251  * @tc.name: DsoftbusHanderTest002
252  * @tc.desc: cooperate plugin
253  * @tc.type: FUNC
254  * @tc.require:
255  */
256 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest002, TestSize.Level1)
257 {
258     CALL_TEST_DEBUG;
259     int32_t ret = g_context->dsoftbus_.StartCooperate("test", {});
260     EXPECT_EQ(ret, RET_ERR);
261 }
262 
263 /**
264  * @tc.name: DsoftbusHanderTest003
265  * @tc.desc: cooperate plugin
266  * @tc.type: FUNC
267  * @tc.require:
268  */
269 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest003, TestSize.Level1)
270 {
271     CALL_TEST_DEBUG;
272     int32_t ret = g_context->dsoftbus_.StopCooperate("test", {});
273     EXPECT_EQ(ret, RET_ERR);
274 }
275 
276 /**
277  * @tc.name: DsoftbusHanderTest004
278  * @tc.desc: cooperate plugin
279  * @tc.type: FUNC
280  * @tc.require:
281  */
282 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest004, TestSize.Level1)
283 {
284     CALL_TEST_DEBUG;
285     int32_t ret = g_context->dsoftbus_.ComeBack("test", {});
286     EXPECT_EQ(ret, RET_ERR);
287 }
288 
289 /**
290  * @tc.name: DsoftbusHanderTest005
291  * @tc.desc: cooperate plugin
292  * @tc.type: FUNC
293  * @tc.require:
294  */
295 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest005, TestSize.Level1)
296 {
297     CALL_TEST_DEBUG;
298     int32_t ret = g_context->dsoftbus_.RelayCooperate("test", {});
299     EXPECT_EQ(ret, RET_ERR);
300 }
301 
302 /**
303  * @tc.name: DsoftbusHanderTest006
304  * @tc.desc: cooperate plugin
305  * @tc.type: FUNC
306  * @tc.require:
307  */
308 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest006, TestSize.Level1)
309 {
310     CALL_TEST_DEBUG;
311     g_context->dsoftbus_.GetLocalNetworkId();
312     g_context->dsoftbus_.OnBind("test");
313     g_context->dsoftbus_.OnShutdown("test");
314     int32_t ret = g_context->dsoftbus_.RelayCooperateFinish("test", {});
315     EXPECT_EQ(ret, RET_ERR);
316 }
317 
318 /**
319  * @tc.name: DsoftbusHanderTest007
320  * @tc.desc: cooperate plugin
321  * @tc.type: FUNC
322  * @tc.require:
323  */
324 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest007, TestSize.Level1)
325 {
326     CALL_TEST_DEBUG;
327     CooperateEvent event{};
328     NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
329     g_context->dsoftbus_.SendEvent(event);
330     g_context->dsoftbus_.OnCommunicationFailure("test");
331     g_context->dsoftbus_.OnStartCooperate("test", packet);
332     g_context->dsoftbus_.OnStopCooperate("test", packet);
333     g_context->dsoftbus_.OnComeBack("test", packet);
334     g_context->dsoftbus_.OnRelayCooperate("test", packet);
335     g_context->dsoftbus_.OnRelayCooperateFinish("test", packet);
336     g_context->dsoftbus_.OnSubscribeMouseLocation("test", packet);
337     g_context->dsoftbus_.OnUnSubscribeMouseLocation("test", packet);
338     g_context->dsoftbus_.OnReplySubscribeLocation("test", packet);
339     g_context->dsoftbus_.OnReplyUnSubscribeLocation("test", packet);
340     g_context->dsoftbus_.OnRemoteMouseLocation("test", packet);
341     bool ret = g_context->dsoftbus_.OnPacket("test", packet);
342     EXPECT_TRUE(ret);
343 }
344 /**
345  * @tc.name: DsoftbusHanderTest008
346  * @tc.desc: Test cooperate plugin
347  * @tc.type: FUNC
348  * @tc.require:
349  */
350 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest008, TestSize.Level1)
351 {
352     CALL_TEST_DEBUG;
353     std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
354     ASSERT_NO_FATAL_FAILURE(g_context->dsoftbus_.OnConnected(localNetworkId));
355 }
356 
357 /**
358  * @tc.name: DsoftbusHanderTest009
359  * @tc.desc: Test cooperate plugin
360  * @tc.type: FUNC
361  * @tc.require:
362  */
363 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest009, TestSize.Level1)
364 {
365     CALL_TEST_DEBUG;
366     NetPacket pkt(MessageId::DSOFTBUS_START_COOPERATE);
367     std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
368     ASSERT_NO_FATAL_FAILURE(g_context->dsoftbus_.OnRemoteInputDevice(localNetworkId, pkt));
369 }
370 
371 /**
372  * @tc.name: DsoftbusHanderTest010
373  * @tc.desc: Test cooperate plugin
374  * @tc.type: FUNC
375  * @tc.require:
376  */
377 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest010, TestSize.Level1)
378 {
379     CALL_TEST_DEBUG;
380     NetPacket pkt(MessageId::DSOFTBUS_START_COOPERATE);
381     int32_t testData = 10;
382     pkt << testData;
383     std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
384     ASSERT_NO_FATAL_FAILURE(g_context->dsoftbus_.OnRemoteInputDevice(localNetworkId, pkt));
385 }
386 
387 /**
388  * @tc.name: DsoftbusHanderTest011
389  * @tc.desc: Test cooperate plugin
390  * @tc.type: FUNC
391  * @tc.require:
392  */
393 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest011, TestSize.Level1)
394 {
395     CALL_TEST_DEBUG;
396     NetPacket pkt(MessageId::DSOFTBUS_START_COOPERATE);
397     std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
398     ASSERT_NO_FATAL_FAILURE(g_context->dsoftbus_.OnRemoteHotPlug(localNetworkId, pkt));
399 }
400 
401 /**
402  * @tc.name: DsoftbusHanderTest012
403  * @tc.desc: Test cooperate plugin
404  * @tc.type: FUNC
405  * @tc.require:
406  */
407 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest012, TestSize.Level1)
408 {
409     CALL_TEST_DEBUG;
410     NetPacket pkt(MessageId::DSOFTBUS_START_COOPERATE);
411     std::string localNetworkId = g_context->dsoftbus_.GetLocalNetworkId();
412     ASSERT_NO_FATAL_FAILURE(g_context->dsoftbus_.OnRemoteHotPlug(localNetworkId, pkt));
413 }
414 
415 /**
416  * @tc.name: DsoftbusHanderTest013
417  * @tc.name: StateMachineTest_ResetCooperate
418  * @tc.desc: cooperate plugin
419  * @tc.type: FUNC
420  * @tc.require:
421  */
422 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest013, TestSize.Level1)
423 {
424     CALL_TEST_DEBUG;
425     int32_t ret = g_context->dsoftbus_.StartCooperateWithOptions("test", {});
426     EXPECT_EQ(ret, RET_ERR);
427 }
428 
429 /**
430  * @tc.name: DsoftbusHanderTest014
431  * @tc.desc: cooperate plugin
432  * @tc.type: FUNC
433  * @tc.require:
434  */
435 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest014, TestSize.Level1)
436 {
437     CALL_TEST_DEBUG;
438     int32_t ret = g_context->dsoftbus_.ComeBackWithOptions("test", {});
439     EXPECT_EQ(ret, RET_ERR);
440 }
441 
442 /**
443  * @tc.name: DsoftbusHanderTest015
444  * @tc.desc: cooperate plugin
445  * @tc.type: FUNC
446  * @tc.require:
447  */
448 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest015, TestSize.Level1)
449 {
450     CALL_TEST_DEBUG;
451     int32_t ret = g_context->dsoftbus_.RelayCooperateWithOptions("test", {});
452     EXPECT_EQ(ret, RET_ERR);
453 }
454 
455 /**
456  * @tc.name: DsoftbusHanderTest016
457  * @tc.desc: cooperate plugin
458  * @tc.type: FUNC
459  * @tc.require:
460  */
461 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest016, TestSize.Level1)
462 {
463     CALL_TEST_DEBUG;
464     NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
465     g_context->dsoftbus_.OnStartCooperateWithOptions("test", packet);
466     g_context->dsoftbus_.OnComeBackWithOptions("test", packet);
467     g_context->dsoftbus_.OnRelayCooperateWithOptions("test", packet);
468     g_context->dsoftbus_.OnRelayCooperateWithOptionsFinish("test", packet);
469     int32_t ret = g_context->dsoftbus_.RelayCooperateWithOptionsFinish("test", {});
470     EXPECT_EQ(ret, RET_ERR);
471 }
472 
473 /**
474  * @tc.name: DsoftbusHanderTest017
475  * @tc.desc: cooperate plugin
476  * @tc.type: FUNC
477  * @tc.require:
478  */
479 HWTEST_F(DsoftbusHanderTest, DsoftbusHanderTest017, TestSize.Level1)
480 {
481     CALL_TEST_DEBUG;
482     NetPacket packet(MessageId::DSOFTBUS_START_COOPERATE);
483     g_context->dsoftbus_.OnStartCooperateWithOptions("test", packet);
484     g_context->dsoftbus_.OnComeBackWithOptions("test", packet);
485     g_context->dsoftbus_.OnRelayCooperateWithOptions("test", packet);
486     g_context->dsoftbus_.OnRelayCooperateWithOptionsFinish("test", packet);
487     int32_t ret = g_context->dsoftbus_.RelayCooperateWithOptionsFinish("test", {});
488     EXPECT_EQ(ret, RET_ERR);
489 }
490 } // namespace DeviceStatus
491 } // namespace Msdp
492 } // namespace OHOS