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