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