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 "hot_area_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 "hot_area.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
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)132 MMI::PointerEvent::PointerItem HotAreaTest::CreatePointerItem(int32_t pointerId, int32_t deviceId,
133 const std::pair<int32_t, int32_t> &displayLocation, bool isPressed)
134 {
135 MMI::PointerEvent::PointerItem item;
136 item.SetPointerId(pointerId);
137 item.SetDeviceId(deviceId);
138 item.SetDisplayX(displayLocation.first);
139 item.SetDisplayY(displayLocation.second);
140 item.SetPressed(isPressed);
141 return item;
142 }
143
NotifyCooperate()144 void HotAreaTest::NotifyCooperate()
145 {
146 int32_t errCode { static_cast<int32_t>(CoordinationErrCode::COORDINATION_OK) };
147 EventManager::CooperateStateNotice cooperateStateNotice{IPCSkeleton::GetCallingPid(),
148 MessageId::COORDINATION_MESSAGE, 1, true, errCode};
149 g_contextOne->eventMgr_.NotifyCooperateState(cooperateStateNotice);
150 g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
151 g_socketSessionMgr.AddSession(g_session);
152 g_context->eventMgr_.NotifyCooperateState(cooperateStateNotice);
153 g_context->eventMgr_.GetCooperateState(cooperateStateNotice);
154 }
155
CheckInHot()156 void HotAreaTest::CheckInHot()
157 {
158 g_context->hotArea_.displayX_ = 0;
159 g_context->hotArea_.height_ = HOTAREA_500;
160 g_context->hotArea_.displayY_ = HOTAREA_250;
161 g_context->hotArea_.CheckInHotArea();
162 g_context->hotArea_.width_ = HOTAREA_200;
163 g_context->hotArea_.displayX_ = HOTAREA_150;
164 g_context->hotArea_.height_ = HOTAREA_500;
165 g_context->hotArea_.displayY_ = HOTAREA_250;
166 g_context->hotArea_.CheckInHotArea();
167 g_context->hotArea_.displayY_ = HOTAREA_50;
168 g_context->hotArea_.width_ = HOTAREA_500;
169 g_context->hotArea_.displayX_ = HOTAREA_250;
170 g_context->hotArea_.CheckInHotArea();
171 g_context->hotArea_.height_ = HOTAREA_500;
172 g_context->hotArea_.displayY_ = HOTAREA_500;
173 g_context->hotArea_.width_ = HOTAREA_500;
174 g_context->hotArea_.displayX_ = HOTAREA_250;
175 g_context->hotArea_.CheckInHotArea();
176 g_context->hotArea_.height_ = HOTAREA_500;
177 g_context->hotArea_.displayY_ = HOTAREA_NEGATIVE_500;
178 g_context->hotArea_.width_ = HOTAREA_500;
179 g_context->hotArea_.displayX_ = HOTAREA_NEGATIVE_200;
180 g_context->hotArea_.CheckInHotArea();
181 }
182
SetUpTestCase()183 void HotAreaTest::SetUpTestCase() {}
184
SetUp()185 void HotAreaTest::SetUp()
186 {
187 g_ddm = std::make_unique<DDMAdapter>();
188 g_input = std::make_unique<InputAdapter>();
189 g_dsoftbus = std::make_unique<DSoftbusAdapter>();
190 g_contextOne = std::make_shared<Context>(g_icontext);
191 auto env = ContextService::GetInstance();
192 g_context = std::make_shared<Context>(env);
193 int32_t moduleType = 1;
194 int32_t tokenType = 1;
195 int32_t uid = IPCSkeleton::GetCallingUid();
196 int32_t pid = IPCSkeleton::GetCallingPid();
197 int32_t sockFds[2] { -1, -1 };
198 g_session = std::make_shared<SocketSession>("test", moduleType, tokenType, sockFds[0], uid, pid);
199 }
200
TearDown()201 void HotAreaTest::TearDown()
202 {
203 g_context = nullptr;
204 g_contextOne = nullptr;
205 g_session = nullptr;
206 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
207 }
208
OnThreeStates(const CooperateEvent & event)209 void HotAreaTest::OnThreeStates(const CooperateEvent &event)
210 {
211 auto env = ContextService::GetInstance();
212 Context cooperateContext(env);
213 g_stateMachine = std::make_shared<Cooperate::StateMachine>(env);
214 g_stateMachine->current_ = CooperateState::COOPERATE_STATE_OUT;
215 g_stateMachine->OnEvent(cooperateContext, event);
216 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
217 g_stateMachine->current_ = CooperateState::COOPERATE_STATE_IN;
218 g_stateMachine->OnEvent(cooperateContext, event);
219 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
220 g_stateMachine->current_ = CooperateState::COOPERATE_STATE_FREE;
221 g_stateMachine->OnEvent(cooperateContext, event);
222 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
223 }
224
225 class CooperateObserver final : public ICooperateObserver {
226 public:
227 CooperateObserver() = default;
228 virtual ~CooperateObserver() = default;
229
IsAllowCooperate()230 virtual bool IsAllowCooperate()
231 {
232 return true;
233 }
OnStartCooperate(StartCooperateData & data)234 virtual void OnStartCooperate(StartCooperateData &data) {}
OnRemoteStartCooperate(RemoteStartCooperateData & data)235 virtual void OnRemoteStartCooperate(RemoteStartCooperateData &data) {}
OnStopCooperate(const std::string & remoteNetworkId)236 virtual void OnStopCooperate(const std::string &remoteNetworkId) {}
OnTransitionOut(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)237 virtual void OnTransitionOut(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnTransitionIn(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)238 virtual void OnTransitionIn(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnBack(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)239 virtual void OnBack(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnRelay(const std::string & remoteNetworkId,const CooperateInfo & cooperateInfo)240 virtual void OnRelay(const std::string &remoteNetworkId, const CooperateInfo &cooperateInfo) {}
OnReset()241 virtual void OnReset() {}
CloseDistributedFileConnection(const std::string & remoteNetworkId)242 virtual void CloseDistributedFileConnection(const std::string &remoteNetworkId) {}
243 };
244
245 /**
246 * @tc.name: HotAreaTest1
247 * @tc.desc: HotAreaTest
248 * @tc.type: FUNC
249 * @tc.require:
250 */
251 HWTEST_F(HotAreaTest, HotAreaTest001, TestSize.Level1)
252 {
253 CALL_TEST_DEBUG;
254 g_socketSessionMgr.Enable();
255 RegisterHotareaListenerEvent registerHotareaListenerEvent{IPCSkeleton::GetCallingPid(), 1};
256 g_context->hotArea_.AddListener(registerHotareaListenerEvent);
257 g_context->hotArea_.OnHotAreaMessage(HotAreaType::AREA_LEFT, true);
258 g_contextOne->hotArea_.AddListener(registerHotareaListenerEvent);
259 g_contextOne->hotArea_.OnHotAreaMessage(HotAreaType::AREA_LEFT, true);
260 g_socketSessionMgr.sessions_.clear();
261 g_context->hotArea_.OnHotAreaMessage(HotAreaType::AREA_LEFT, true);
262 g_context->hotArea_.RemoveListener(registerHotareaListenerEvent);
263 EnableCooperateEvent enableCooperateEvent{1, 1, 1};
264 g_context->hotArea_.EnableCooperate(enableCooperateEvent);
265 CheckInHot();
266 g_context->hotArea_.CheckPointerToEdge(HotAreaType::AREA_LEFT);
267 g_context->hotArea_.CheckPointerToEdge(HotAreaType::AREA_RIGHT);
268 g_context->hotArea_.CheckPointerToEdge(HotAreaType::AREA_TOP);
269 g_context->hotArea_.CheckPointerToEdge(HotAreaType::AREA_BOTTOM);
270 g_context->hotArea_.CheckPointerToEdge(HotAreaType::AREA_NONE);
271 g_context->hotArea_.NotifyMessage();
272
273 int32_t ret = g_context->hotArea_.ProcessData(nullptr);
274 EXPECT_EQ(ret, RET_ERR);
275 ret = g_context->hotArea_.ProcessData(MMI::PointerEvent::Create());
276 EXPECT_EQ(ret, RET_ERR);
277 auto pointerEvent = MMI::PointerEvent::Create();
278 MMI::PointerEvent::PointerItem pointerItem;
279 pointerEvent->SetPointerId(1);
280 MMI::PointerEvent::PointerItem curPointerItem = CreatePointerItem(1, 1, { 0, 0 }, true);
281 pointerEvent->AddPointerItem(curPointerItem);
282 ret = g_context->hotArea_.ProcessData(pointerEvent);
283 EXPECT_EQ(ret, RET_OK);
284 }
285 } // namespace DeviceStatus
286 } // namespace Msdp
287 } // namespace OHOS