1 /*
2 * Copyright (c) 2024-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 "cooperate_test.h"
16
17 #include "cooperate.h"
18 #include "ddm_adapter.h"
19 #include "input_adapter.h"
20 #include "i_cooperate.h"
21 #include "ipc_skeleton.h"
22 #include "dsoftbus_adapter.h"
23 #include "plugin_manager.h"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 using namespace testing::ext;
29 using namespace Cooperate;
30 namespace {
31 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
32
33 DelegateTasks g_delegateTasks;
34 DeviceManager g_devMgr;
35 TimerManager g_timerMgr;
36 DragManager g_dragMgr;
37 ContextService *g_instance = nullptr;
38 SocketSessionManager g_socketSessionMgr;
39 std::unique_ptr<IDDMAdapter> g_ddm;
40 std::unique_ptr<IInputAdapter> g_input;
41 std::unique_ptr<IPluginManager> g_pluginMgr;
42 std::unique_ptr<IDSoftbusAdapter> g_dsoftbus;
43 ICooperate* g_cooperate { nullptr };
44 Channel<CooperateEvent>::Sender g_sender;
45 } // namespace
46
ContextService()47 ContextService::ContextService()
48 {
49 }
50
~ContextService()51 ContextService::~ContextService()
52 {
53 }
54
GetDelegateTasks()55 IDelegateTasks& ContextService::GetDelegateTasks()
56 {
57 return g_delegateTasks;
58 }
59
GetDeviceManager()60 IDeviceManager& ContextService::GetDeviceManager()
61 {
62 return g_devMgr;
63 }
64
GetTimerManager()65 ITimerManager& ContextService::GetTimerManager()
66 {
67 return g_timerMgr;
68 }
69
GetDragManager()70 IDragManager& ContextService::GetDragManager()
71 {
72 return g_dragMgr;
73 }
74
GetInstance()75 ContextService* ContextService::GetInstance()
76 {
77 static std::once_flag flag;
78 std::call_once(flag, [&]() {
79 ContextService *cooContext = new (std::nothrow) ContextService();
80 CHKPL(cooContext);
81 g_instance = cooContext;
82 });
83 return g_instance;
84 }
85
GetSocketSessionManager()86 ISocketSessionManager& ContextService::GetSocketSessionManager()
87 {
88 return g_socketSessionMgr;
89 }
90
GetDDM()91 IDDMAdapter& ContextService::GetDDM()
92 {
93 return *g_ddm;
94 }
95
GetPluginManager()96 IPluginManager& ContextService::GetPluginManager()
97 {
98 return *g_pluginMgr;
99 }
100
GetInput()101 IInputAdapter& ContextService::GetInput()
102 {
103 return *g_input;
104 }
105
GetDSoftbus()106 IDSoftbusAdapter& ContextService::GetDSoftbus()
107 {
108 return *g_dsoftbus;
109 }
110
111 class CooperateObserver final : public ICooperateObserver {
112 public:
113 CooperateObserver() = default;
114 virtual ~CooperateObserver() = default;
115
IsAllowCooperate()116 virtual bool IsAllowCooperate()
117 {
118 return true;
119 }
OnStartCooperate(StartCooperateData & data)120 virtual void OnStartCooperate(StartCooperateData &data) {}
OnRemoteStartCooperate(RemoteStartCooperateData & data)121 virtual void OnRemoteStartCooperate(RemoteStartCooperateData &data) {}
OnStopCooperate(const std::string & remoteNetworkId)122 virtual void OnStopCooperate(const std::string &remoteNetworkId) {}
OnTransitionOut(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)123 virtual void OnTransitionOut(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnTransitionIn(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)124 virtual void OnTransitionIn(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnBack(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)125 virtual void OnBack(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnRelay(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)126 virtual void OnRelay(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnReset()127 virtual void OnReset() {}
CloseDistributedFileConnection(const std::string & remoteNetworkId)128 virtual void CloseDistributedFileConnection(const std::string &remoteNetworkId) {}
129 };
130
SetUpTestCase()131 void CooperateTest::SetUpTestCase() {}
132
SetUp()133 void CooperateTest::SetUp()
134 {
135 g_ddm = std::make_unique<DDMAdapter>();
136 g_input = std::make_unique<InputAdapter>();
137 g_dsoftbus = std::make_unique<DSoftbusAdapter>();
138 auto env = ContextService::GetInstance();
139 g_pluginMgr = std::make_unique<MockPluginManager>(env);
140 g_cooperate = env->GetPluginManager().LoadCooperate();
141 }
142
TearDown()143 void CooperateTest::TearDown()
144 {
145 }
146
TearDownTestCase()147 void CooperateTest::TearDownTestCase()
148 {
149 if (g_cooperate == nullptr) {
150 GTEST_LOG_(INFO) << "g_cooperate is nullptr";
151 return;
152 }
153 ContextService::GetInstance()->GetPluginManager().UnloadCooperate();
154 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
155 }
156
MockPluginManager(IContext * context)157 MockPluginManager::MockPluginManager(IContext *context)
158 {
159 pluginMgr_ = std::make_unique<PluginManager>(context);
160 }
161
LoadCooperate()162 ICooperate* MockPluginManager::LoadCooperate()
163 {
164 return pluginMgr_->LoadCooperate();
165 }
166
UnloadCooperate()167 void MockPluginManager::UnloadCooperate()
168 {
169 pluginMgr_->UnloadCooperate();
170 }
171
LoadMotionDrag()172 IMotionDrag* MockPluginManager::LoadMotionDrag()
173 {
174 return nullptr;
175 }
176
UnloadMotionDrag()177 void MockPluginManager::UnloadMotionDrag()
178 {}
179
180 /**
181 * @tc.name: CooperateTest1
182 * @tc.desc: cooperate plugin
183 * @tc.type: FUNC
184 * @tc.require:
185 */
186 HWTEST_F(CooperateTest, CooperateTest1, TestSize.Level0)
187 {
188 CALL_TEST_DEBUG;
189 int32_t ret = RET_ERR;
190 if (g_cooperate != nullptr) {
191 std::shared_ptr<ICooperateObserver> observer = std::make_shared<CooperateObserver>();
192 g_cooperate->AddObserver(observer);
193 g_cooperate->RemoveObserver(observer);
194 ret = g_cooperate->RegisterListener(IPCSkeleton::GetCallingPid());
195 EXPECT_EQ(ret, RET_OK);
196 ret = g_cooperate->UnregisterListener(IPCSkeleton::GetCallingPid());
197 EXPECT_EQ(ret, RET_OK);
198 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
199 } else {
200 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
201 EXPECT_EQ(!ret, RET_OK);
202 }
203 }
204
205 /**
206 * @tc.name: CooperateTest2
207 * @tc.desc: cooperate plugin
208 * @tc.type: FUNC
209 * @tc.require:
210 */
211 HWTEST_F(CooperateTest, CooperateTest2, TestSize.Level0)
212 {
213 CALL_TEST_DEBUG;
214 int32_t ret = RET_ERR;
215 if (g_cooperate != nullptr) {
216 int32_t ret = g_cooperate->RegisterHotAreaListener(IPCSkeleton::GetCallingPid());
217 EXPECT_EQ(ret, RET_OK);
218 ret = g_cooperate->UnregisterHotAreaListener(IPCSkeleton::GetCallingPid());
219 EXPECT_EQ(ret, RET_OK);
220 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
221 } else {
222 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
223 EXPECT_EQ(!ret, RET_OK);
224 }
225 }
226
227 /**
228 * @tc.name: CooperateTest3
229 * @tc.desc: cooperate plugin
230 * @tc.type: FUNC
231 * @tc.require:
232 */
233 HWTEST_F(CooperateTest, CooperateTest3, TestSize.Level0)
234 {
235 CALL_TEST_DEBUG;
236 int32_t ret = RET_ERR;
237 if (g_cooperate != nullptr) {
238 int32_t ret = g_cooperate->Enable(1, IPCSkeleton::GetCallingPid(), 1);
239 EXPECT_EQ(ret, RET_OK);
240 ret = g_cooperate->Disable(IPCSkeleton::GetCallingPid(), 1);
241 EXPECT_EQ(ret, RET_OK);
242 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
243 } else {
244 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
245 EXPECT_EQ(!ret, RET_OK);
246 }
247 }
248
249 /**
250 * @tc.name: CooperateTest4
251 * @tc.desc: cooperate plugin
252 * @tc.type: FUNC
253 * @tc.require:
254 */
255 HWTEST_F(CooperateTest, CooperateTest4, TestSize.Level0)
256 {
257 CALL_TEST_DEBUG;
258 int32_t ret = RET_ERR;
259 if (g_cooperate != nullptr) {
260 int32_t ret = g_cooperate->Start(IPCSkeleton::GetCallingPid(), 1, "test", 1);
261 EXPECT_GE(ret, 0);
262 ret = g_cooperate->Stop(IPCSkeleton::GetCallingPid(), 1, true);
263 EXPECT_EQ(ret, RET_OK);
264 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
265 } else {
266 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
267 EXPECT_EQ(!ret, RET_OK);
268 }
269 }
270
271 /**
272 * @tc.name: CooperateTest5
273 * @tc.desc: cooperate plugin
274 * @tc.type: FUNC
275 * @tc.require:
276 */
277 HWTEST_F(CooperateTest, CooperateTest5, TestSize.Level0)
278 {
279 CALL_TEST_DEBUG;
280 int32_t ret = RET_ERR;
281 if (g_cooperate != nullptr) {
282 int32_t ret = g_cooperate->GetCooperateState(IPCSkeleton::GetCallingPid(), 1, "test");
283 EXPECT_EQ(ret, RET_OK);
284 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
285 } else {
286 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
287 EXPECT_EQ(!ret, RET_OK);
288 }
289 }
290
291 /**
292 * @tc.name: CooperateTest6
293 * @tc.desc: cooperate plugin
294 * @tc.type: FUNC
295 * @tc.require:
296 */
297 HWTEST_F(CooperateTest, CooperateTest6, TestSize.Level0)
298 {
299 CALL_TEST_DEBUG;
300 int32_t ret = RET_ERR;
301 if (g_cooperate != nullptr) {
302 int32_t ret = g_cooperate->RegisterEventListener(1, "test");
303 EXPECT_EQ(ret, RET_OK);
304 ret = g_cooperate->UnregisterEventListener(1, "test");
305 EXPECT_EQ(ret, RET_OK);
306 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
307 } else {
308 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
309 EXPECT_EQ(!ret, RET_OK);
310 }
311 }
312
313 /**
314 * @tc.name: CooperateTest7
315 * @tc.desc: cooperate plugin
316 * @tc.type: FUNC
317 * @tc.require:
318 */
319 HWTEST_F(CooperateTest, CooperateTest7, TestSize.Level0)
320 {
321 CALL_TEST_DEBUG;
322 int32_t ret = RET_ERR;
323 if (g_cooperate != nullptr) {
324 g_cooperate->Dump(1);
325 std::string udId { "default" };
326 bool state { false };
327 int32_t ret = g_cooperate->GetCooperateState(udId, state);
328 EXPECT_EQ(ret, RET_OK);
329 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
330 } else {
331 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
332 EXPECT_EQ(!ret, RET_OK);
333 }
334 }
335
336 /**
337 * @tc.name: CooperateTest8
338 * @tc.desc: cooperate plugin
339 * @tc.type: FUNC
340 * @tc.require:
341 */
342 HWTEST_F(CooperateTest, CooperateTest8, TestSize.Level0)
343 {
344 CALL_TEST_DEBUG;
345 int32_t ret = RET_ERR;
346 if (g_cooperate != nullptr) {
347 CooperateOptions withOptions;
348 withOptions.displayX = 500;
349 withOptions.displayY = 500;
350 withOptions.displayId = 1;
351 int32_t pid = IPCSkeleton::GetCallingPid();
352 int32_t userData = 1;
353 int32_t startDeviceId = 0;
354 const std::string &remoteNetworkId = "test";
355 ret = g_cooperate->StartWithOptions(pid, userData, remoteNetworkId,
356 startDeviceId, withOptions);
357 EXPECT_EQ(ret, RET_OK);
358 } else {
359 GTEST_LOG_(INFO) << "The product does not intention_cooperate so";
360 EXPECT_EQ(!ret, RET_OK);
361 }
362 }
363 } // namespace DeviceStatus
364 } // namespace Msdp
365 } // namespace OHOS
366