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) {}
OnTransitionOut(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos)123 virtual void OnTransitionOut(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos) {}
OnTransitionIn(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos)124 virtual void OnTransitionIn(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos) {}
OnBack(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos)125 virtual void OnBack(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos) {}
OnRelay(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos)126 virtual void OnRelay(const std::string &remoteNetworkId, const NormalizedCoordinate &cursorPos) {}
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 */
HWTEST_F(CooperateTest, CooperateTest1, TestSize.Level0)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 */
HWTEST_F(CooperateTest, CooperateTest2, TestSize.Level0)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 */
HWTEST_F(CooperateTest, CooperateTest3, TestSize.Level0)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 */
HWTEST_F(CooperateTest, CooperateTest4, TestSize.Level0)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 */
HWTEST_F(CooperateTest, CooperateTest5, TestSize.Level0)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 */
HWTEST_F(CooperateTest, CooperateTest6, TestSize.Level0)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 */
HWTEST_F(CooperateTest, CooperateTest7, TestSize.Level0)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 GetCooperateStateSyncParam param;
326 bool state { false };
327 int32_t ret = g_cooperate->GetCooperateState(param.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 } // namespace DeviceStatus
336 } // namespace Msdp
337 } // namespace OHOS
338