1 /*
2 * Copyright (c) 2023 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
16 #include <future>
17 #include <optional>
18 #include <utility>
19 #include <vector>
20
21 #include <unistd.h>
22
23 #include "accesstoken_kit.h"
24 #include "display_manager.h"
25 #include <gtest/gtest.h>
26 #include "input_device.h"
27 #include "input_manager.h"
28 #include "parcel.h"
29 #include "pointer_event.h"
30 #include "securec.h"
31
32 #include "coordination_message.h"
33 #include "devicestatus_define.h"
34 #include "devicestatus_errors.h"
35 #include "drag_data_util.h"
36 #include "interaction_manager.h"
37 #include "nativetoken_kit.h"
38 #include "token_setproc.h"
39
40 namespace OHOS {
41 namespace Msdp {
42 namespace DeviceStatus {
43 using namespace testing::ext;
44 namespace {
45 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "InteractionManagerTest" };
46 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
47 constexpr int32_t TIME_WAIT_FOR_INJECT_MS { 80 };
48 constexpr int32_t TIME_WAIT_FOR_TOUCH_DOWN_MS { 1000 };
49 constexpr int32_t PROMISE_WAIT_SPAN_MS { 2000 };
50 constexpr int32_t TEST_PIXEL_MAP_WIDTH { 200 };
51 constexpr int32_t TEST_PIXEL_MAP_HEIGHT { 200 };
52 constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
53 constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
54 constexpr int32_t MOUSE_POINTER_ID { 0 };
55 constexpr int32_t TOUCH_POINTER_ID { 1 };
56 constexpr int32_t DISPLAY_ID { 0 };
57 constexpr int32_t DRAG_SRC_X { 0 };
58 constexpr int32_t DRAG_SRC_Y { 0 };
59 constexpr int32_t DRAG_DST_X { 500 };
60 constexpr int32_t DRAG_DST_Y { 500 };
61 constexpr int32_t DRAG_NUM { 1 };
62 constexpr int32_t INT32_BYTE { 4 };
63 constexpr int32_t SUBSTR_UDKEY_LEN { 6 };
64 constexpr int32_t WINDOW_ID { -1 };
65 constexpr int32_t HOT_AREA_COOR { 220 };
66 constexpr int32_t HOT_AREA_STEP { 150 };
67 constexpr int32_t HOT_AREA_SPAN { 70 };
68 constexpr uint32_t RECIVE_LOOP_COUNT { 5 };
69 constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
70 constexpr bool HAS_CANCELED_ANIMATION { true };
71 constexpr bool HAS_CUSTOM_ANIMATION { true };
72 constexpr int32_t MOVE_STEP { 10 };
73 constexpr int32_t FOREGROUND_COLOR_IN { 0x33FF0000 };
74 constexpr int32_t FOREGROUND_COLOR_OUT { 0x00000000 };
75 constexpr int32_t ANIMATION_DURATION { 500 };
76 constexpr int64_t DOWN_TIME { 1 };
77 const std::string UD_KEY { "Unified data key" };
78 const std::string SYSTEM_CORE { "system_core" };
79 const std::string SYSTEM_BASIC { "system_basic" };
80 const std::string CURVE_NAME { "cubic-bezier" };
81 const std::string EXTRA_INFO { "{ \"drag_corner_radius\": 20, \"drag_allow_distributed\": false }" };
82 const std::string FILTER_INFO { "{ \"dip_scale\": 3.5 }" };
83 int32_t g_deviceMouseId { -1 };
84 int32_t g_deviceTouchId { -1 };
85 int32_t g_screenWidth { 720 };
86 int32_t g_screenHeight { 1280 };
87 uint64_t g_tokenID { 0 };
88 const char* g_cores[] = { "ohos.permission.INPUT_MONITORING" };
89 const char* g_basics[] = { "ohos.permission.COOPERATE_MANAGER" };
90 std::shared_ptr<MMI::KeyEvent> g_keyEvent = MMI::KeyEvent::Create();
91 } // namespace
92
93 class InteractionManagerTest : public testing::Test {
94 public:
95 void SetUp();
96 void TearDown();
97 static void SetUpTestCase();
98 static std::vector<int32_t> GetInputDeviceIds();
99 static std::shared_ptr<MMI::InputDevice> GetDevice(int32_t deviceId);
100 static std::pair<int32_t, int32_t> GetMouseAndTouch();
101 static std::shared_ptr<Media::PixelMap> CreatePixelMap(int32_t width, int32_t height);
102 static std::optional<DragData> CreateDragData(const std::pair<int32_t, int32_t> &pixelMapSize, int32_t sourceType,
103 int32_t pointerId, int32_t displayId, const std::pair<int32_t, int32_t> &location);
104 static MMI::PointerEvent::PointerItem CreatePointerItem(int32_t pointerId,
105 int32_t deviceId, const std::pair<int32_t, int32_t> &displayLocation, bool isPressed);
106 static std::shared_ptr<MMI::PointerEvent> SetupPointerEvent(const std::pair<int32_t, int32_t> &displayLocation,
107 int32_t action, int32_t sourceType, int32_t pointerId, bool isPressed);
108 static void SimulateDownPointerEvent(
109 const std::pair<int32_t, int32_t> &location, int32_t sourceType, int32_t pointerId);
110 static void SimulateUpPointerEvent(
111 const std::pair<int32_t, int32_t> &location, int32_t sourceType, int32_t pointerId);
112 static void SimulateMovePointerEvent(const std::pair<int32_t, int32_t> &srcLocation,
113 const std::pair<int32_t, int32_t> &dstLocation, int32_t sourceType, int32_t pointerId, bool isPressed);
114 static int32_t TestAddMonitor(std::shared_ptr<MMI::IInputEventConsumer> consumer);
115 static void TestRemoveMonitor(int32_t monitorId);
116 static void PrintDragData(const DragData &dragData);
117 static void SetPermission(const std::string &level, const char** perms, size_t permAmount);
118 static void RemovePermission();
119 static void SetupKeyEvent(int32_t action, int32_t key, bool isPressed);
120 static void ClearUpKeyEvent();
121 static void SimulateDownKeyEvent(int32_t key);
122 static void SimulateUpKeyEvent(int32_t key);
123 static void PrintDragAction(DragAction dragAction);
124 static void AssignToAnimation(PreviewAnimation &animation);
125 };
126
SetPermission(const std::string & level,const char ** perms,size_t permAmount)127 void InteractionManagerTest::SetPermission(const std::string &level, const char** perms, size_t permAmount)
128 {
129 CALL_DEBUG_ENTER;
130 if (perms == nullptr || permAmount == 0) {
131 FI_HILOGE("The perms is empty");
132 return;
133 }
134
135 NativeTokenInfoParams infoInstance = {
136 .dcapsNum = 0,
137 .permsNum = permAmount,
138 .aclsNum = 0,
139 .dcaps = nullptr,
140 .perms = perms,
141 .acls = nullptr,
142 .processName = "InteractionManagerTest",
143 .aplStr = level.c_str(),
144 };
145 g_tokenID = GetAccessTokenId(&infoInstance);
146 SetSelfTokenID(g_tokenID);
147 OHOS::Security::AccessToken::AccessTokenKit::AccessTokenKit::ReloadNativeTokenInfo();
148 }
149
RemovePermission()150 void InteractionManagerTest::RemovePermission()
151 {
152 CALL_DEBUG_ENTER;
153 int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::DeleteToken(g_tokenID);
154 if (ret != RET_OK) {
155 FI_HILOGE("Failed to remove permission");
156 return;
157 }
158 }
159
160 class DragListenerTest : public IDragListener {
161 public:
DragListenerTest()162 DragListenerTest() {}
DragListenerTest(const std::string & name)163 explicit DragListenerTest(const std::string& name) : moduleName_(name) {}
OnDragMessage(DragState state)164 void OnDragMessage(DragState state) override
165 {
166 if (moduleName_.empty()) {
167 moduleName_ = std::string("DragListenerTest");
168 }
169 FI_HILOGD("%{public}s, state:%{public}s", moduleName_.c_str(), PrintDragMessage(state).c_str());
170 }
171 private:
PrintDragMessage(DragState state)172 std::string PrintDragMessage(DragState state)
173 {
174 std::string type = "unknow";
175 const std::map<DragState, std::string> stateType = {
176 { DragState::ERROR, "error"},
177 { DragState::START, "start"},
178 { DragState::STOP, "stop"},
179 { DragState::CANCEL, "cancel"}
180 };
181 auto item = stateType.find(state);
182 if (item != stateType.end()) {
183 type = item->second;
184 }
185 return type;
186 }
187 private:
188 std::string moduleName_;
189 };
190
191 class SubscriptListenerTest : public ISubscriptListener {
192 public:
SubscriptListenerTest()193 SubscriptListenerTest() {}
SubscriptListenerTest(const std::string & name)194 explicit SubscriptListenerTest(const std::string& name) : moduleName_(name) {}
OnMessage(DragCursorStyle style)195 void OnMessage(DragCursorStyle style) override
196 {
197 SetDragSyle(style);
198 if (moduleName_.empty()) {
199 moduleName_ = std::string("SubscriptListenerTest");
200 }
201 FI_HILOGD("Received notification event for subscriptListener, %{public}s, state:%{public}s",
202 moduleName_.c_str(), PrintStyleMessage(style).c_str());
203 }
204
GetDragStyle()205 DragCursorStyle GetDragStyle()
206 {
207 return dragStyle_;
208 }
209
210 private:
SetDragSyle(DragCursorStyle style)211 void SetDragSyle(DragCursorStyle style)
212 {
213 dragStyle_ = style;
214 }
215
PrintStyleMessage(DragCursorStyle style)216 std::string PrintStyleMessage(DragCursorStyle style)
217 {
218 std::string type = "unknow";
219 const std::map<DragCursorStyle, std::string> cursorStyles = {
220 { DragCursorStyle::DEFAULT, "default"},
221 { DragCursorStyle::FORBIDDEN, "forbidden"},
222 { DragCursorStyle::COPY, "copy"},
223 { DragCursorStyle::MOVE, "move"}
224 };
225 auto item = cursorStyles.find(style);
226 if (item != cursorStyles.end()) {
227 type = item->second;
228 }
229 return type;
230 }
231
232 private:
233 DragCursorStyle dragStyle_ { DragCursorStyle::DEFAULT };
234 std::string moduleName_;
235 };
236
237 class UnitTestStartDragListener : public IStartDragListener {
238 public:
UnitTestStartDragListener(std::function<void (const DragNotifyMsg &)> function)239 explicit UnitTestStartDragListener(std::function<void(const DragNotifyMsg&)> function) : function_(function) { }
OnDragEndMessage(const DragNotifyMsg & msg)240 void OnDragEndMessage(const DragNotifyMsg &msg) override
241 {
242 FI_HILOGD("DisplayX:%{public}d, displayY:%{public}d, targetPid:%{public}d, result:%{public}d",
243 msg.displayX, msg.displayY, msg.targetPid, static_cast<int32_t>(msg.result));
244 if (function_ != nullptr) {
245 function_(msg);
246 }
247 FI_HILOGD("Unit test OnDragEndMessage");
248 }
249
OnHideIconMessage()250 void OnHideIconMessage() override
251 {
252 FI_HILOGI("Unit test OnHideIconMessage");
253 }
254 private:
255 std::function<void(const DragNotifyMsg&)> function_;
256 };
257
GetInputDeviceIds()258 std::vector<int32_t> InteractionManagerTest::GetInputDeviceIds()
259 {
260 std::vector<int32_t> realDeviceIds;
261 auto callback = [&realDeviceIds](std::vector<int32_t>& deviceIds) {
262 realDeviceIds = deviceIds;
263 };
264 int32_t ret = MMI::InputManager::GetInstance()->GetDeviceIds(callback);
265 if (ret != RET_OK) {
266 FI_HILOGE("Get device ids failed");
267 return {};
268 }
269 return realDeviceIds;
270 }
271
GetDevice(int32_t deviceId)272 std::shared_ptr<MMI::InputDevice> InteractionManagerTest::GetDevice(int32_t deviceId)
273 {
274 std::shared_ptr<MMI::InputDevice> inputDevice;
275 auto callback = [&inputDevice](std::shared_ptr<MMI::InputDevice> device) {
276 inputDevice = device;
277 };
278 int32_t ret = MMI::InputManager::GetInstance()->GetDevice(deviceId, callback);
279 if (ret != RET_OK || inputDevice == nullptr) {
280 FI_HILOGE("Get device failed");
281 return nullptr;
282 }
283 return inputDevice;
284 }
285
GetMouseAndTouch()286 std::pair<int32_t, int32_t> InteractionManagerTest::GetMouseAndTouch()
287 {
288 std::vector<int32_t> deviceIds = GetInputDeviceIds();
289 std::pair<int32_t, int32_t> mouseAndTouch { -1, -1 };
290 for (const auto& id : deviceIds) {
291 std::shared_ptr<MMI::InputDevice> device = GetDevice(id);
292 CHKPC(device);
293 if (device->HasCapability(MMI::InputDeviceCapability::INPUT_DEV_CAP_POINTER)) {
294 mouseAndTouch.first = device->GetId();
295 }
296 if (device->HasCapability(MMI::InputDeviceCapability::INPUT_DEV_CAP_TOUCH)) {
297 mouseAndTouch.second = device->GetId();
298 }
299 }
300 return mouseAndTouch;
301 }
302
SetUpTestCase()303 void InteractionManagerTest::SetUpTestCase()
304 {
305 auto mouseAndTouch = GetMouseAndTouch();
306 g_deviceMouseId = mouseAndTouch.first;
307 g_deviceTouchId = mouseAndTouch.second;
308 }
309
SetUp()310 void InteractionManagerTest::SetUp() {}
311
TearDown()312 void InteractionManagerTest::TearDown()
313 {
314 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
315 }
316
CreatePixelMap(int32_t width,int32_t height)317 std::shared_ptr<Media::PixelMap> InteractionManagerTest::CreatePixelMap(int32_t width, int32_t height)
318 {
319 CALL_DEBUG_ENTER;
320 OHOS::Media::InitializationOptions opts;
321 opts.size.width = width;
322 opts.size.height = height;
323 opts.pixelFormat = Media::PixelFormat::BGRA_8888;
324 opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
325 opts.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
326
327 int32_t colorLen = width * height;
328 uint32_t* colors = new (std::nothrow) uint32_t[colorLen];
329 CHKPP(colors);
330 int32_t colorByteCount = colorLen * INT32_BYTE;
331 if (memset_s(colors, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount) != EOK) {
332 delete[] colors;
333 FI_HILOGE("memset_s failed");
334 return nullptr;
335 }
336 std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(colors, colorLen, opts);
337 if (pixelMap == nullptr) {
338 delete[] colors;
339 FI_HILOGE("Create pixelMap failed");
340 return nullptr;
341 }
342 delete[] colors;
343 return pixelMap;
344 }
345
CreateDragData(const std::pair<int32_t,int32_t> & pixelMapSize,int32_t sourceType,int32_t pointerId,int32_t displayId,const std::pair<int32_t,int32_t> & location)346 std::optional<DragData> InteractionManagerTest::CreateDragData(const std::pair<int32_t, int32_t> &pixelMapSize,
347 int32_t sourceType, int32_t pointerId, int32_t displayId, const std::pair<int32_t, int32_t> &location)
348 {
349 CALL_DEBUG_ENTER;
350 std::shared_ptr<Media::PixelMap> pixelMap = CreatePixelMap(pixelMapSize.first, pixelMapSize.second);
351 if (pixelMap == nullptr) {
352 FI_HILOGE("Create pixelmap failed");
353 return std::nullopt;
354 }
355 ShadowInfo shadowInfo { pixelMap, 0, 0 };
356 DragData dragData;
357 dragData.shadowInfos.push_back(shadowInfo);
358 dragData.buffer = std::vector<uint8_t>(MAX_BUFFER_SIZE, 0);
359 dragData.udKey = UD_KEY;
360 dragData.sourceType = sourceType;
361 dragData.pointerId = pointerId;
362 dragData.dragNum = DRAG_NUM;
363 dragData.displayX = location.first;
364 dragData.displayY = location.second;
365 dragData.displayId = displayId;
366 dragData.hasCanceledAnimation = HAS_CANCELED_ANIMATION;
367 dragData.extraInfo = EXTRA_INFO;
368 dragData.filterInfo = FILTER_INFO;
369 return dragData;
370 }
371
CreatePointerItem(int32_t pointerId,int32_t deviceId,const std::pair<int32_t,int32_t> & displayLocation,bool isPressed)372 MMI::PointerEvent::PointerItem InteractionManagerTest::CreatePointerItem(int32_t pointerId, int32_t deviceId,
373 const std::pair<int32_t, int32_t> &displayLocation, bool isPressed)
374 {
375 MMI::PointerEvent::PointerItem item;
376 item.SetPointerId(pointerId);
377 item.SetDeviceId(deviceId);
378 item.SetDisplayX(displayLocation.first);
379 item.SetDisplayY(displayLocation.second);
380 item.SetPressed(isPressed);
381 return item;
382 }
383
SetupPointerEvent(const std::pair<int32_t,int32_t> & displayLocation,int32_t action,int32_t sourceType,int32_t pointerId,bool isPressed)384 std::shared_ptr<MMI::PointerEvent> InteractionManagerTest::SetupPointerEvent(const std::pair<int32_t, int32_t>
385 &displayLocation, int32_t action, int32_t sourceType, int32_t pointerId, bool isPressed)
386 {
387 CALL_DEBUG_ENTER;
388 auto pointerEvent = MMI::PointerEvent::Create();
389 CHKPP(pointerEvent);
390 pointerEvent->SetPointerAction(action);
391 pointerEvent->SetSourceType(sourceType);
392 pointerEvent->SetPointerId(pointerId);
393 MMI::PointerEvent::PointerItem curPointerItem;
394 if (sourceType == MMI::PointerEvent::SOURCE_TYPE_MOUSE) {
395 curPointerItem = CreatePointerItem(pointerId, g_deviceMouseId, displayLocation, isPressed);
396 } else if (sourceType == MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN) {
397 curPointerItem = CreatePointerItem(pointerId, g_deviceTouchId, displayLocation, isPressed);
398 } else {
399 FI_HILOGE("Unknow sourceType:%{public}d", sourceType);
400 return nullptr;
401 }
402 pointerEvent->AddPointerItem(curPointerItem);
403 return pointerEvent;
404 }
405
SimulateDownPointerEvent(const std::pair<int32_t,int32_t> & location,int32_t sourceType,int32_t pointerId)406 void InteractionManagerTest::SimulateDownPointerEvent(const std::pair<int32_t, int32_t> &location, int32_t sourceType,
407 int32_t pointerId)
408 {
409 CALL_DEBUG_ENTER;
410 std::shared_ptr<MMI::PointerEvent> pointerEvent =
411 SetupPointerEvent(location, MMI::PointerEvent::POINTER_ACTION_DOWN, sourceType, pointerId, true);
412 CHKPV(pointerEvent);
413 FI_HILOGD("TEST:sourceType:%{public}d, pointerId:%{public}d, pointerAction:%{public}d",
414 pointerEvent->GetSourceType(), pointerEvent->GetPointerId(), pointerEvent->GetPointerAction());
415 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
416 }
417
SimulateUpPointerEvent(const std::pair<int32_t,int32_t> & location,int32_t sourceType,int32_t pointerId)418 void InteractionManagerTest::SimulateUpPointerEvent(const std::pair<int32_t, int32_t> &location, int32_t sourceType,
419 int32_t pointerId)
420 {
421 CALL_DEBUG_ENTER;
422 std::shared_ptr<MMI::PointerEvent> pointerEvent = SetupPointerEvent(location,
423 MMI::PointerEvent::POINTER_ACTION_UP, sourceType, pointerId, false);
424 CHKPV(pointerEvent);
425 FI_HILOGD("TEST:sourceType:%{public}d, pointerId:%{public}d, pointerAction:%{public}d",
426 pointerEvent->GetSourceType(), pointerEvent->GetPointerId(), pointerEvent->GetPointerAction());
427 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
428 }
429
SimulateMovePointerEvent(const std::pair<int32_t,int32_t> & srcLocation,const std::pair<int32_t,int32_t> & dstLocation,int32_t sourceType,int32_t pointerId,bool isPressed)430 void InteractionManagerTest::SimulateMovePointerEvent(const std::pair<int32_t, int32_t> &srcLocation,
431 const std::pair<int32_t, int32_t> &dstLocation, int32_t sourceType, int32_t pointerId, bool isPressed)
432 {
433 CALL_DEBUG_ENTER;
434 int32_t srcX = srcLocation.first;
435 int32_t srcY = srcLocation.second;
436 int32_t dstX = dstLocation.first;
437 int32_t dstY = dstLocation.second;
438 std::vector<std::pair<int32_t, int32_t>> coordinates;
439 if (dstX == srcX) {
440 for (int32_t y = srcY; y <= dstY; y += MOVE_STEP) {
441 coordinates.push_back({srcX, y});
442 }
443 for (int32_t y = srcY; y > dstY; y -= MOVE_STEP) {
444 coordinates.push_back({srcX, y});
445 }
446 } else {
447 double ratio = (dstY - srcY) * 1.0 / (dstX - srcX);
448 for (int32_t x = srcX; x < dstX; x += MOVE_STEP) {
449 coordinates.push_back({x, srcY + static_cast<int32_t>(ratio * (x - srcX))});
450 }
451 for (int32_t x = srcX; x >= dstX; x -= MOVE_STEP) {
452 coordinates.push_back({x, srcY + static_cast<int32_t>(ratio * (x - srcX))});
453 }
454 coordinates.push_back({dstX, dstY});
455 }
456 for (const auto& pointer : coordinates) {
457 std::shared_ptr<MMI::PointerEvent> pointerEvent =
458 SetupPointerEvent(pointer, MMI::PointerEvent::POINTER_ACTION_MOVE, sourceType, pointerId, isPressed);
459 CHKPC(pointerEvent);
460 FI_HILOGD("TEST:sourceType:%{public}d, pointerId:%{public}d, pointerAction:%{public}d",
461 pointerEvent->GetSourceType(), pointerEvent->GetPointerId(), pointerEvent->GetPointerAction());
462 MMI::InputManager::GetInstance()->SimulateInputEvent(pointerEvent);
463 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_INJECT_MS));
464 }
465 }
466
TestAddMonitor(std::shared_ptr<MMI::IInputEventConsumer> consumer)467 int32_t InteractionManagerTest::TestAddMonitor(std::shared_ptr<MMI::IInputEventConsumer> consumer)
468 {
469 CHKPR(consumer, RET_ERR);
470 return MMI::InputManager::GetInstance()->AddMonitor(consumer);
471 }
472
TestRemoveMonitor(int32_t monitorId)473 void InteractionManagerTest::TestRemoveMonitor(int32_t monitorId)
474 {
475 MMI::InputManager::GetInstance()->RemoveMonitor(monitorId);
476 }
477
PrintDragData(const DragData & dragData)478 void InteractionManagerTest::PrintDragData(const DragData &dragData)
479 {
480 CALL_DEBUG_ENTER;
481 for (const auto &shadowInfo : dragData.shadowInfos) {
482 CHKPV(shadowInfo.pixelMap);
483 FI_HILOGD("PixelFormat:%{public}d, PixelAlphaType:%{public}d, PixelAllocatorType:%{public}d,"
484 " PixelWidth:%{public}d, PixelHeight:%{public}d, shadowX:%{public}d, shadowY:%{public}d",
485 static_cast<int32_t>(shadowInfo.pixelMap->GetPixelFormat()),
486 static_cast<int32_t>(shadowInfo.pixelMap->GetAlphaType()),
487 static_cast<int32_t>(shadowInfo.pixelMap->GetAllocatorType()),
488 shadowInfo.pixelMap->GetWidth(), shadowInfo.pixelMap->GetHeight(), shadowInfo.x, shadowInfo.y);
489 }
490 FI_HILOGD("SourceType:%{public}d, pointerId:%{public}d, displayId:%{public}d, displayX:%{public}d,"
491 " displayY:%{public}d, dragNum:%{public}d, hasCanceledAnimation:%{public}d, udKey:%{public}s",
492 dragData.sourceType, dragData.pointerId, dragData.displayId, dragData.displayX, dragData.displayY,
493 dragData.dragNum, dragData.hasCanceledAnimation, dragData.udKey.substr(0, SUBSTR_UDKEY_LEN).c_str());
494 }
495
SetupKeyEvent(int32_t action,int32_t key,bool isPressed)496 void InteractionManagerTest::SetupKeyEvent(
497 int32_t action, int32_t key, bool isPressed)
498 {
499 CALL_DEBUG_ENTER;
500 CHKPV(g_keyEvent);
501 MMI::KeyEvent::KeyItem keyItem;
502 keyItem.SetKeyCode(key);
503 keyItem.SetDeviceId(1);
504 keyItem.SetDownTime(DOWN_TIME);
505 g_keyEvent->SetKeyAction(action);
506 g_keyEvent->SetKeyCode(key);
507 if (action == MMI::KeyEvent::KEY_ACTION_DOWN) {
508 keyItem.SetPressed(isPressed);
509 } else if (action == MMI::KeyEvent::KEY_ACTION_UP) {
510 g_keyEvent->RemoveReleasedKeyItems(keyItem);
511 }
512 g_keyEvent->AddPressedKeyItems(keyItem);
513 }
514
ClearUpKeyEvent()515 void InteractionManagerTest::ClearUpKeyEvent()
516 {
517 CALL_DEBUG_ENTER;
518 CHKPV(g_keyEvent);
519 for (const auto& iter : g_keyEvent->GetKeyItems()) {
520 if (!iter.IsPressed()) {
521 FI_HILOGD("TEST:Clean keyItem, keyCode:%{public}d", iter.GetKeyCode());
522 g_keyEvent->RemoveReleasedKeyItems(iter);
523 return;
524 }
525 }
526 }
527
SimulateDownKeyEvent(int32_t key)528 void InteractionManagerTest::SimulateDownKeyEvent(int32_t key)
529 {
530 CALL_DEBUG_ENTER;
531 SetupKeyEvent(MMI::KeyEvent::KEY_ACTION_DOWN, key, true);
532 FI_HILOGD("TEST:keyCode:%{public}d, keyAction: KEY_ACTION_DOWN", key);
533 MMI::InputManager::GetInstance()->SimulateInputEvent(g_keyEvent);
534 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_INJECT_MS));
535 }
536
SimulateUpKeyEvent(int32_t key)537 void InteractionManagerTest::SimulateUpKeyEvent(int32_t key)
538 {
539 CALL_DEBUG_ENTER;
540 SetupKeyEvent(MMI::KeyEvent::KEY_ACTION_UP, key, false);
541 FI_HILOGD("TEST:keyCode:%{public}d, keyAction: KEY_ACTION_UP", key);
542 MMI::InputManager::GetInstance()->SimulateInputEvent(g_keyEvent);
543 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_INJECT_MS));
544 }
545
PrintDragAction(DragAction dragAction)546 void InteractionManagerTest::PrintDragAction(DragAction dragAction)
547 {
548 switch (dragAction) {
549 case DragAction::MOVE:{
550 FI_HILOGD("drag action: MOVE");
551 break;
552 }
553 case DragAction::COPY:{
554 FI_HILOGD("drag action: COPY");
555 break;
556 }
557 default:{
558 FI_HILOGD("drag action: UNKNOWN");
559 break;
560 }
561 }
562 }
563
AssignToAnimation(PreviewAnimation & animation)564 void InteractionManagerTest::AssignToAnimation(PreviewAnimation &animation)
565 {
566 animation.duration = ANIMATION_DURATION;
567 animation.curveName = CURVE_NAME;
568 animation.curve = { 0.33, 0, 0.67, 1 };
569 }
570
571 class InputEventCallbackTest : public MMI::IInputEventConsumer {
572 public:
InputEventCallbackTest()573 InputEventCallbackTest() {}
InputEventCallbackTest(std::function<void ()> callback)574 explicit InputEventCallbackTest(std::function<void()> callback) : callback_(callback) {}
OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const575 void OnInputEvent(std::shared_ptr<MMI::KeyEvent> keyEvent) const override {};
576 void OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const override;
OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const577 void OnInputEvent(std::shared_ptr<MMI::AxisEvent> axisEvent) const override {};
578 private:
579 std::function<void()> callback_ { nullptr };
580 };
581
OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const582 void InputEventCallbackTest::OnInputEvent(std::shared_ptr<MMI::PointerEvent> pointerEvent) const
583 {
584 CALL_DEBUG_ENTER;
585 ASSERT_TRUE(pointerEvent != nullptr);
586 auto pointerAction = pointerEvent->GetPointerAction();
587 ASSERT_TRUE(pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_MOVE ||
588 pointerAction == MMI::PointerEvent::POINTER_ACTION_PULL_UP);
589 ASSERT_TRUE(!pointerEvent->GetBuffer().empty());
590 MMI::PointerEvent::PointerItem pointerItem;
591 pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
592 if (callback_ != nullptr && pointerItem.GetDisplayX() == DRAG_DST_X && pointerItem.GetDisplayY() == DRAG_DST_Y) {
593 callback_();
594 }
595 }
596
597 /**
598 * @tc.name: InteractionManagerTest_RegisterCoordinationListener_001
599 * @tc.desc: Register coordination listener
600 * @tc.type: FUNC
601 * @tc.require:
602 */
603 HWTEST_F(InteractionManagerTest, InteractionManagerTest_RegisterCoordinationListener_001, TestSize.Level1)
604 {
605 CALL_TEST_DEBUG;
606 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
607 std::shared_ptr<ICoordinationListener> consumer = nullptr;
608 bool isCompatible = true;
609 int32_t ret = InteractionManager::GetInstance()->RegisterCoordinationListener(consumer, isCompatible);
610 #ifdef OHOS_BUILD_ENABLE_COORDINATION
611 ASSERT_EQ(ret, RET_ERR);
612 #else
613 ASSERT_EQ(ret, ERROR_UNSUPPORT);
614 #endif // OHOS_BUILD_ENABLE_COORDINATION
615 RemovePermission();
616 }
617
618 /**
619 * @tc.name: InteractionManagerTest_RegisterCoordinationListener_002
620 * @tc.desc: Register coordination listener
621 * @tc.type: FUNC
622 * @tc.require:
623 */
624 HWTEST_F(InteractionManagerTest, InteractionManagerTest_RegisterCoordinationListener_002, TestSize.Level1)
625 {
626 CALL_DEBUG_ENTER;
627 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
628 class CoordinationListenerTest : public ICoordinationListener {
629 public:
CoordinationListenerTest()630 CoordinationListenerTest() : ICoordinationListener() {}
OnCoordinationMessage(const std::string & networkId,CoordinationMessage msg)631 void OnCoordinationMessage(const std::string &networkId, CoordinationMessage msg) override
632 {
633 FI_HILOGD("Register coordination listener test");
634 (void) networkId;
635 };
636 };
637 std::shared_ptr<CoordinationListenerTest> consumer =
638 std::make_shared<CoordinationListenerTest>();
639 bool isCompatible = true;
640 int32_t ret = InteractionManager::GetInstance()->RegisterCoordinationListener(consumer, isCompatible);
641 #ifdef OHOS_BUILD_ENABLE_COORDINATION
642 ASSERT_EQ(ret, RET_OK);
643 #else
644 ASSERT_EQ(ret, ERROR_UNSUPPORT);
645 #endif // OHOS_BUILD_ENABLE_COORDINATION
646 ret = InteractionManager::GetInstance()->UnregisterCoordinationListener(consumer, isCompatible);
647 #ifdef OHOS_BUILD_ENABLE_COORDINATION
648 ASSERT_EQ(ret, RET_OK);
649 #else
650 ASSERT_EQ(ret, ERROR_UNSUPPORT);
651 #endif // OHOS_BUILD_ENABLE_COORDINATION
652 RemovePermission();
653 }
654
655 /**
656 * @tc.name: InteractionManagerTest_UnregisterCoordinationListener
657 * @tc.desc: Unregister coordination listener
658 * @tc.type: FUNC
659 * @tc.require:
660 */
661 HWTEST_F(InteractionManagerTest, InteractionManagerTest_UnregisterCoordinationListener, TestSize.Level1)
662 {
663 CALL_TEST_DEBUG;
664 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
665 std::shared_ptr<ICoordinationListener> consumer = nullptr;
666 bool isCompatible = true;
667 int32_t ret = InteractionManager::GetInstance()->UnregisterCoordinationListener(consumer, isCompatible);
668 #ifdef OHOS_BUILD_ENABLE_COORDINATION
669 ASSERT_EQ(ret, RET_OK);
670 #else
671 ASSERT_EQ(ret, ERROR_UNSUPPORT);
672 #endif // OHOS_BUILD_ENABLE_COORDINATION
673 RemovePermission();
674 }
675
676 /**
677 * @tc.name: InteractionManagerTest_PrepareCoordination
678 * @tc.desc: Prepare coordination
679 * @tc.type: FUNC
680 * @tc.require:
681 */
682 HWTEST_F(InteractionManagerTest, InteractionManagerTest_PrepareCoordination, TestSize.Level1)
683 {
684 CALL_TEST_DEBUG;
685 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
686 std::promise<bool> promiseFlag;
687 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0402(std::string listener, CoordinationMessage coordinationMessages) 688 auto fun = [&promiseFlag](std::string listener, CoordinationMessage coordinationMessages) {
689 FI_HILOGD("Prepare coordination success, listener:%{public}s", listener.c_str());
690 promiseFlag.set_value(true);
691 };
692 bool isCompatible = true;
693 int32_t ret = InteractionManager::GetInstance()->PrepareCoordination(fun, isCompatible);
694 #ifdef OHOS_BUILD_ENABLE_COORDINATION
695 ASSERT_EQ(ret, RET_OK);
696 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) != std::future_status::timeout);
697 #else
698 ASSERT_EQ(ret, ERROR_UNSUPPORT);
699 #endif // OHOS_BUILD_ENABLE_COORDINATION
700 RemovePermission();
701 }
702
703 /**
704 * @tc.name: InteractionManagerTest_UnprepareCoordination
705 * @tc.desc: Prepare coordination
706 * @tc.type: FUNC
707 * @tc.require:
708 */
709 HWTEST_F(InteractionManagerTest, InteractionManagerTest_UnprepareCoordination, TestSize.Level1)
710 {
711 CALL_TEST_DEBUG;
712 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
713 std::promise<bool> promiseFlag;
714 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0502(std::string listener, CoordinationMessage coordinationMessages) 715 auto fun = [&promiseFlag](std::string listener, CoordinationMessage coordinationMessages) {
716 FI_HILOGD("Prepare coordination success, listener:%{public}s", listener.c_str());
717 promiseFlag.set_value(true);
718 };
719 bool isCompatible = true;
720 int32_t ret = InteractionManager::GetInstance()->UnprepareCoordination(fun, isCompatible);
721 #ifdef OHOS_BUILD_ENABLE_COORDINATION
722 ASSERT_EQ(ret, RET_OK);
723 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) != std::future_status::timeout);
724 #else
725 ASSERT_EQ(ret, ERROR_UNSUPPORT);
726 #endif // OHOS_BUILD_ENABLE_COORDINATION
727 RemovePermission();
728 }
729
730 /**
731 * @tc.name: InteractionManagerTest_ActivateCoordination
732 * @tc.desc: Activate coordination
733 * @tc.type: FUNC
734 * @tc.require:
735 */
736 HWTEST_F(InteractionManagerTest, InteractionManagerTest_ActivateCoordination, TestSize.Level1)
737 {
738 CALL_TEST_DEBUG;
739 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
740 std::string remoteNetworkId("");
741 int32_t startDeviceId = -1;
__anona4ee5cad0602(std::string listener, CoordinationMessage coordinationMessages) 742 auto fun = [](std::string listener, CoordinationMessage coordinationMessages) {
743 FI_HILOGD("Start coordination success");
744 (void) listener;
745 };
746 bool isCompatible = true;
747 int32_t ret = InteractionManager::GetInstance()->ActivateCoordination(remoteNetworkId, startDeviceId,
748 fun, isCompatible);
749 #ifdef OHOS_BUILD_ENABLE_COORDINATION
750 ASSERT_NE(ret, RET_OK);
751 #else
752 ASSERT_EQ(ret, ERROR_UNSUPPORT);
753 #endif // OHOS_BUILD_ENABLE_COORDINATION
754 RemovePermission();
755 }
756
757 /**
758 * @tc.name: InteractionManagerTest_DeactivateCoordination
759 * @tc.desc: Deactivate coordination
760 * @tc.type: FUNC
761 * @tc.require:
762 */
763 HWTEST_F(InteractionManagerTest, InteractionManagerTest_DeactivateCoordination, TestSize.Level1)
764 {
765 CALL_TEST_DEBUG;
766 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
__anona4ee5cad0702(std::string listener, CoordinationMessage coordinationMessages) 767 auto fun = [](std::string listener, CoordinationMessage coordinationMessages) {
768 FI_HILOGD("Stop coordination success");
769 (void) listener;
770 };
771 bool isCompatible = true;
772 int32_t ret = InteractionManager::GetInstance()->DeactivateCoordination(false, fun, isCompatible);
773 #ifdef OHOS_BUILD_ENABLE_COORDINATION
774 ASSERT_NE(ret, RET_OK);
775 #else
776 ASSERT_EQ(ret, ERROR_UNSUPPORT);
777 ret = InteractionManager::GetInstance()->DeactivateCoordination(true, fun, isCompatible);
778 ASSERT_EQ(ret, ERROR_UNSUPPORT);
779 #endif // OHOS_BUILD_ENABLE_COORDINATION
780 RemovePermission();
781 }
782
783 /**
784 * @tc.name: InteractionManagerTest_GetCoordinationState_Abnormal
785 * @tc.desc: Get coordination state
786 * @tc.type: FUNC
787 * @tc.require:
788 */
789 HWTEST_F(InteractionManagerTest, InteractionManagerTest_GetCoordinationState_Abnormal, TestSize.Level1)
790 {
791 CALL_TEST_DEBUG;
792 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
793 const std::string networkId("");
__anona4ee5cad0802(bool state) 794 auto fun = [](bool state) {
795 FI_HILOGD("Get coordination state failed, state:%{public}d", state);
796 };
797 bool isCompatible = true;
798 int32_t ret = InteractionManager::GetInstance()->GetCoordinationState(networkId, fun, isCompatible);
799 #ifdef OHOS_BUILD_ENABLE_COORDINATION
800 ASSERT_NE(ret, RET_OK);
801 #else
802 ASSERT_EQ(ret, ERROR_UNSUPPORT);
803 #endif // OHOS_BUILD_ENABLE_COORDINATION
804 RemovePermission();
805 }
806
807 /**
808 * @tc.name: InteractionManagerTest_GetCoordinationState_Normal
809 * @tc.desc: Get coordination state
810 * @tc.type: FUNC
811 * @tc.require:
812 */
813 HWTEST_F(InteractionManagerTest, InteractionManagerTest_GetCoordinationState_Normal, TestSize.Level1)
814 {
815 CALL_TEST_DEBUG;
816 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
817 std::promise<bool> promiseFlag;
818 std::future<bool> futureFlag = promiseFlag.get_future();
819 const std::string networkId("networkId");
__anona4ee5cad0902(bool state) 820 auto fun = [&promiseFlag](bool state) {
821 FI_HILOGD("Get coordination state success, state:%{public}d", state);
822 promiseFlag.set_value(true);
823 };
824 bool isCompatible = true;
825 int32_t ret = InteractionManager::GetInstance()->GetCoordinationState(networkId, fun, isCompatible);
826 #ifdef OHOS_BUILD_ENABLE_COORDINATION
827 ASSERT_EQ(ret, RET_OK);
828 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) != std::future_status::timeout);
829 #else
830 ASSERT_EQ(ret, ERROR_UNSUPPORT);
831 #endif // OHOS_BUILD_ENABLE_COORDINATION
832 RemovePermission();
833 }
834
835 /**
836 * @tc.name: InteractionManagerTest_Draglistener_Mouse
837 * @tc.desc: Drag listener
838 * @tc.type: FUNC
839 * @tc.require:
840 */
841 HWTEST_F(InteractionManagerTest, InteractionManagerTest_Draglistener_Mouse, TestSize.Level1)
842 {
843 CALL_TEST_DEBUG;
844 if (g_deviceMouseId < 0) {
845 ASSERT_TRUE(g_deviceMouseId < 0);
846 } else {
847 auto listener = std::make_shared<DragListenerTest>(std::string("Draglistener_Mouse"));
848 int32_t ret = InteractionManager::GetInstance()->AddDraglistener(listener);
849 ASSERT_EQ(ret, RET_OK);
850 std::promise<bool> promiseFlag;
851 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0a02(const DragNotifyMsg& notifyMessage) 852 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
853 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
854 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
855 promiseFlag.set_value(true);
856 };
857 SimulateDownPointerEvent(
858 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
859 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
860 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
861 ASSERT_TRUE(dragData);
862 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
863 std::make_shared<UnitTestStartDragListener>(callback));
864 ASSERT_EQ(ret, RET_OK);
865 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
866 EXPECT_EQ(ret, RET_OK);
867 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
868 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, true);
869 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
870 InteractionManager::GetInstance()->StopDrag(dropResult);
871 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
872 std::future_status::timeout);
873 ret = InteractionManager::GetInstance()->RemoveDraglistener(listener);
874 ASSERT_EQ(ret, RET_OK);
875 }
876 }
877
878 /**
879 * @tc.name: InteractionManagerTest_Draglistener_Touch
880 * @tc.desc: Drag listener
881 * @tc.type: FUNC
882 * @tc.require:
883 */
884 HWTEST_F(InteractionManagerTest, InteractionManagerTest_Draglistener_Touch, TestSize.Level1)
885 {
886 CALL_TEST_DEBUG;
887 if (g_deviceTouchId < 0) {
888 ASSERT_TRUE(g_deviceTouchId < 0);
889 } else {
890 auto dragListener = std::make_shared<DragListenerTest>(std::string("Draglistener_Touch"));
891 int32_t ret = InteractionManager::GetInstance()->AddDraglistener(dragListener);
892 ASSERT_EQ(ret, RET_OK);
893 std::promise<bool> promiseFlag;
894 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0b02(const DragNotifyMsg& notifyMessage) 895 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
896 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, target:%{public}d, result:%{public}d",
897 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.targetPid, notifyMessage.result);
898 promiseFlag.set_value(true);
899 };
900 SimulateDownPointerEvent(
901 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
902 std::optional<DragData> dragData = CreateDragData({ MAX_PIXEL_MAP_WIDTH, MAX_PIXEL_MAP_HEIGHT },
903 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
904 ASSERT_TRUE(dragData);
905 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
906 std::make_shared<UnitTestStartDragListener>(callback));
907 ASSERT_EQ(ret, RET_OK);
908 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
909 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
910 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
911 InteractionManager::GetInstance()->StopDrag(dropResult);
912 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
913 std::future_status::timeout);
914 ret = InteractionManager::GetInstance()->RemoveDraglistener(dragListener);
915 ASSERT_EQ(ret, RET_OK);
916 }
917 }
918
919 /**
920 * @tc.name: InteractionManagerTest_Draglistener
921 * @tc.desc: Drag listener
922 * @tc.type: FUNC
923 * @tc.require:
924 */
925 HWTEST_F(InteractionManagerTest, InteractionManagerTest_Draglistener, TestSize.Level1)
926 {
927 CALL_TEST_DEBUG;
928 if (g_deviceTouchId < 0) {
929 ASSERT_TRUE(g_deviceTouchId < 0);
930 } else {
931 int32_t ret = RET_ERR;
932 std::vector<std::shared_ptr<DragListenerTest>> dragListeners;
933 constexpr size_t listenerCount = 5;
934 for (size_t i = 0; i < listenerCount; ++i) {
935 std::string moduleName = "Draglistener_" + std::to_string(i);
936 auto listener = std::make_shared<DragListenerTest>(moduleName);
937 ret = InteractionManager::GetInstance()->AddDraglistener(listener);
938 if (ret == RET_OK) {
939 dragListeners.push_back(listener);
940 }
941 }
942 SimulateDownPointerEvent(
943 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
944 std::optional<DragData> dragData = CreateDragData({ MAX_PIXEL_MAP_WIDTH, MAX_PIXEL_MAP_HEIGHT },
945 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
946 ASSERT_TRUE(dragData);
947 std::promise<bool> promiseFlag;
948 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0c02(const DragNotifyMsg& notifyMessage) 949 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
950 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
951 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
952 promiseFlag.set_value(true);
953 };
954 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
955 std::make_shared<UnitTestStartDragListener>(callback));
956 ASSERT_EQ(ret, RET_OK);
957 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
958 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
959 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
960 InteractionManager::GetInstance()->StopDrag(dropResult);
961 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
962 std::future_status::timeout);
963 for (const auto &listener : dragListeners) {
964 ret = InteractionManager::GetInstance()->RemoveDraglistener(listener);
965 EXPECT_EQ(ret, RET_OK);
966 }
967 }
968 }
969
970 /**
971 * @tc.name: InteractionManagerTest_SubscriptListener_001
972 * @tc.desc: SubscriptListener
973 * @tc.type: FUNC
974 * @tc.require:
975 */
976 HWTEST_F(InteractionManagerTest, InteractionManagerTest_SubscriptListener_001, TestSize.Level1)
977 {
978 CALL_TEST_DEBUG;
979 if (g_deviceTouchId < 0) {
980 ASSERT_TRUE(g_deviceTouchId < 0);
981 } else {
982 int32_t ret = RET_ERR;
983 std::vector<std::shared_ptr<SubscriptListenerTest>> subscriptListeners;
984 for (size_t i = 0; i < RECIVE_LOOP_COUNT; ++i) {
985 std::string moduleName = "SubscriptListener_" + std::to_string(i);
986 auto listener = std::make_shared<SubscriptListenerTest>(moduleName);
987 ret = InteractionManager::GetInstance()->AddSubscriptListener(listener);
988 EXPECT_EQ(ret, RET_OK);
989 subscriptListeners.push_back(listener);
990 }
991 std::promise<bool> promiseFlag;
992 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0d02(const DragNotifyMsg& notifyMessage) 993 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
994 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
995 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
996 promiseFlag.set_value(true);
997 };
998 std::optional<DragData> dragData = CreateDragData({ MAX_PIXEL_MAP_WIDTH, MAX_PIXEL_MAP_HEIGHT },
999 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1000 ASSERT_TRUE(dragData);
1001 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1002 std::make_shared<UnitTestStartDragListener>(callback));
1003 ASSERT_EQ(ret, RET_OK);
1004 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
1005 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1006 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1007 InteractionManager::GetInstance()->StopDrag(dropResult);
1008 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1009 std::future_status::timeout);
1010 for (auto listener : subscriptListeners) {
1011 ret = InteractionManager::GetInstance()->RemoveSubscriptListener(listener);
1012 EXPECT_EQ(ret, RET_OK);
1013 }
1014 }
1015 }
1016
1017 /**
1018 * @tc.name: InteractionManagerTest_SubscriptListener_002
1019 * @tc.desc: SubscriptListener
1020 * @tc.type: FUNC
1021 * @tc.require:
1022 */
1023 HWTEST_F(InteractionManagerTest, InteractionManagerTest_SubscriptListener_002, TestSize.Level1)
1024 {
1025 CALL_TEST_DEBUG;
1026 if (g_deviceTouchId < 0) {
1027 ASSERT_TRUE(g_deviceTouchId < 0);
1028 } else {
1029 int32_t ret = RET_ERR;
1030 auto listener = std::make_shared<SubscriptListenerTest>("SubscriptListener");
1031 ret = InteractionManager::GetInstance()->AddSubscriptListener(listener);
1032 ASSERT_EQ(ret, RET_OK);
1033 std::promise<bool> promiseFlag;
1034 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0e02(const DragNotifyMsg& notifyMessage) 1035 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1036 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1037 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1038 promiseFlag.set_value(true);
1039 };
1040 std::optional<DragData> dragData = CreateDragData({ MAX_PIXEL_MAP_WIDTH, MAX_PIXEL_MAP_HEIGHT },
1041 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1042 ASSERT_TRUE(dragData);
1043 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1044 std::make_shared<UnitTestStartDragListener>(callback));
1045 ASSERT_EQ(ret, RET_OK);
1046 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1047 ASSERT_EQ(ret, RET_OK);
1048 DragCursorStyle style = DragCursorStyle::COPY;
1049 ret = InteractionManager::GetInstance()->UpdateDragStyle(style);
1050 ASSERT_EQ(ret, RET_OK);
1051 style = DragCursorStyle::MOVE;
1052 ret = InteractionManager::GetInstance()->UpdateDragStyle(style);
1053 ASSERT_EQ(ret, RET_OK);
1054 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
1055 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1056 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1057 InteractionManager::GetInstance()->StopDrag(dropResult);
1058 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1059 std::future_status::timeout);
1060 DragCursorStyle recvStyle = listener->GetDragStyle();
1061 FI_HILOGD("Recived style:%{public}d, expected style:%{public}d", static_cast<int32_t>(recvStyle),
1062 static_cast<int32_t>(style));
1063 ret = InteractionManager::GetInstance()->RemoveSubscriptListener(listener);
1064 ASSERT_EQ(ret, RET_OK);
1065 }
1066 }
1067
1068 /**
1069 * @tc.name: InteractionManagerTest_StartDrag_Mouse
1070 * @tc.desc: Start Drag
1071 * @tc.type: FUNC
1072 * @tc.require:
1073 */
1074 HWTEST_F(InteractionManagerTest, InteractionManagerTest_StartDrag_Mouse, TestSize.Level1)
1075 {
1076 CALL_TEST_DEBUG;
1077 if (g_deviceMouseId < 0) {
1078 ASSERT_TRUE(g_deviceMouseId < 0);
1079 } else {
1080 std::promise<bool> promiseFlag;
1081 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad0f02(const DragNotifyMsg& notifyMessage) 1082 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1083 FI_HILOGD("Param displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1084 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1085 promiseFlag.set_value(true);
1086 };
1087 SimulateDownPointerEvent(
1088 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1089 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1090 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1091 ASSERT_TRUE(dragData);
1092 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1093 std::make_shared<UnitTestStartDragListener>(callback));
1094 ASSERT_EQ(ret, RET_OK);
1095 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1096 EXPECT_EQ(ret, RET_OK);
1097 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
1098 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, true);
1099 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1100 InteractionManager::GetInstance()->StopDrag(dropResult);
1101 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1102 std::future_status::timeout);
1103 }
1104 }
1105
1106 /**
1107 * @tc.name: InteractionManagerTest_StartDrag_Failed_Mouse
1108 * @tc.desc: Start Drag
1109 * @tc.type: FUNC
1110 * @tc.require:
1111 */
1112 HWTEST_F(InteractionManagerTest, InteractionManagerTest_StartDrag_Failed_Mouse, TestSize.Level1)
1113 {
1114 CALL_TEST_DEBUG;
1115 if (g_deviceMouseId < 0) {
1116 ASSERT_TRUE(g_deviceMouseId < 0);
1117 } else {
1118 std::promise<bool> promiseFlag;
1119 std::future<bool> futureFlag = promiseFlag.get_future();
1120 SimulateDownPointerEvent(
1121 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
__anona4ee5cad1002(const DragNotifyMsg& notifyMessage) 1122 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1123 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1124 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1125 promiseFlag.set_value(true);
1126 };
1127 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1128 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1129 ASSERT_TRUE(dragData);
1130
1131 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(), nullptr);
1132 ASSERT_EQ(ret, RET_ERR);
1133 dragData->shadowInfos = {};
1134 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1135 std::make_shared<UnitTestStartDragListener>(callback));
1136 ASSERT_EQ(ret, RET_ERR);
1137 }
1138 }
1139
1140 /**
1141 * @tc.name: InteractionManagerTest_StopDrag_Mouse
1142 * @tc.desc: Stop drag
1143 * @tc.type: FUNC
1144 * @tc.require:
1145 */
1146 HWTEST_F(InteractionManagerTest, InteractionManagerTest_StopDrag_Mouse, TestSize.Level1)
1147 {
1148 CALL_TEST_DEBUG;
1149 if (g_deviceMouseId < 0) {
1150 ASSERT_TRUE(g_deviceMouseId < 0);
1151 } else {
1152 std::promise<bool> promiseFlag;
1153 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1102(const DragNotifyMsg& notifyMessage) 1154 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1155 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, target:%{public}d, result:%{public}d",
1156 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.targetPid, notifyMessage.result);
1157 promiseFlag.set_value(true);
1158 };
1159 SimulateDownPointerEvent(
1160 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1161 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1162 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1163 ASSERT_TRUE(dragData);
1164 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1165 std::make_shared<UnitTestStartDragListener>(callback));
1166 ASSERT_EQ(ret, RET_OK);
1167 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1168 InteractionManager::GetInstance()->StopDrag(dropResult);
1169 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1170 std::future_status::timeout);
1171 }
1172 }
1173
1174 /**
1175 * @tc.name: InteractionManagerTest_StopDrag_Failed_Mouse
1176 * @tc.desc: Stop drag
1177 * @tc.type: FUNC
1178 * @tc.require:
1179 */
1180 HWTEST_F(InteractionManagerTest, InteractionManagerTest_StopDrag_Failed_Mouse, TestSize.Level1)
1181 {
1182 CALL_TEST_DEBUG;
1183 if (g_deviceMouseId < 0) {
1184 ASSERT_TRUE(g_deviceMouseId < 0);
1185 } else {
1186 SimulateDownPointerEvent(
1187 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1188 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1189 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1190 ASSERT_TRUE(dragData);
1191 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(), nullptr);
1192 ASSERT_EQ(ret, RET_ERR);
1193 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1194 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1195 ASSERT_EQ(ret, RET_ERR);
1196 }
1197 }
1198
1199 /**
1200 * @tc.name: InteractionManagerTest_StartDrag_Touch
1201 * @tc.desc: Start Drag
1202 * @tc.type: FUNC
1203 * @tc.require:
1204 */
1205 HWTEST_F(InteractionManagerTest, InteractionManagerTest_StartDrag_Touch, TestSize.Level1)
1206 {
1207 CALL_TEST_DEBUG;
1208 if (g_deviceTouchId < 0) {
1209 ASSERT_TRUE(g_deviceTouchId < 0);
1210 } else {
1211 SimulateDownPointerEvent(
1212 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1213 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1214 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1215 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1216 ASSERT_TRUE(dragData);
1217 std::promise<bool> promiseFlag;
1218 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1202(const DragNotifyMsg& notifyMessage) 1219 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1220 FI_HILOGD("Start drag, displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1221 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1222 promiseFlag.set_value(true);
1223 };
1224 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1225 std::make_shared<UnitTestStartDragListener>(callback));
1226 ASSERT_EQ(ret, RET_OK);
1227 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1228 EXPECT_EQ(ret, RET_OK);
1229 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
1230 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1231 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1232 InteractionManager::GetInstance()->StopDrag(dropResult);
1233 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1234 std::future_status::timeout);
1235 }
1236 }
1237
1238 /**
1239 * @tc.name: InteractionManagerTest_StopDrag_Touch
1240 * @tc.desc: Stop drag
1241 * @tc.type: FUNC
1242 * @tc.require:
1243 */
1244 HWTEST_F(InteractionManagerTest, InteractionManagerTest_StopDrag_Touch, TestSize.Level1)
1245 {
1246 CALL_TEST_DEBUG;
1247 if (g_deviceTouchId < 0) {
1248 ASSERT_TRUE(g_deviceTouchId < 0);
1249 } else {
1250 std::promise<bool> promiseFlag;
1251 std::future<bool> future = promiseFlag.get_future();
__anona4ee5cad1302(const DragNotifyMsg& notifyMessage) 1252 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1253 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1254 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1255 promiseFlag.set_value(true);
1256 };
1257 SimulateDownPointerEvent(
1258 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1259 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1260 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1261 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1262 ASSERT_TRUE(dragData);
1263 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1264 std::make_shared<UnitTestStartDragListener>(callback));
1265 ASSERT_EQ(ret, RET_OK);
1266 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1267 InteractionManager::GetInstance()->StopDrag(dropResult);
1268 ASSERT_TRUE(future.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1269 std::future_status::timeout);
1270 }
1271 }
1272
1273 /**
1274 * @tc.name: GetDragTargetPid_Mouse
1275 * @tc.desc: Get the target pid dragged by the mouse
1276 * @tc.type: FUNC
1277 * @tc.require:
1278 */
1279 HWTEST_F(InteractionManagerTest, GetDragTargetPid_Mouse, TestSize.Level1)
1280 {
1281 CALL_TEST_DEBUG;
1282 if (g_deviceMouseId < 0) {
1283 ASSERT_TRUE(g_deviceMouseId < 0);
1284 } else {
1285 std::promise<bool> promiseStopFlag;
1286 std::future<bool> futureStopFlag = promiseStopFlag.get_future();
__anona4ee5cad1402(const DragNotifyMsg& notifyMessage) 1287 auto callback = [&promiseStopFlag](const DragNotifyMsg& notifyMessage) {
1288 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1289 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1290 promiseStopFlag.set_value(true);
1291 };
1292 SimulateDownPointerEvent(
1293 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1294 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1295 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1296 ASSERT_TRUE(dragData);
1297 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1298 std::make_shared<UnitTestStartDragListener>(callback));
1299 ASSERT_EQ(ret, RET_OK);
1300 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1301 EXPECT_EQ(ret, RET_OK);
1302 ret = InteractionManager::GetInstance()->UpdateDragStyle(DragCursorStyle::DEFAULT);
1303 ASSERT_EQ(ret, RET_OK);
1304 int32_t pid = InteractionManager::GetInstance()->GetDragTargetPid();
1305 FI_HILOGI("Target pid:%{public}d", pid);
1306 ASSERT_TRUE(pid > 0);
1307 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1308 InteractionManager::GetInstance()->StopDrag(dropResult);
1309 ASSERT_TRUE(futureStopFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1310 std::future_status::timeout);
1311 }
1312 }
1313
1314 /**
1315 * @tc.name: GetDragTargetPid_Touch
1316 * @tc.desc: Get the target pid dragged by the touchscreen
1317 * @tc.type: FUNC
1318 * @tc.require:
1319 */
1320 HWTEST_F(InteractionManagerTest, GetDragTargetPid_Touch, TestSize.Level1)
1321 {
1322 CALL_TEST_DEBUG;
1323 if (g_deviceTouchId < 0) {
1324 ASSERT_TRUE(g_deviceTouchId < 0);
1325 } else {
1326 std::promise<bool> promiseStopFlag;
1327 std::future<bool> futureStopFlag = promiseStopFlag.get_future();
__anona4ee5cad1502(const DragNotifyMsg& notifyMessage) 1328 auto callback = [&promiseStopFlag](const DragNotifyMsg& notifyMessage) {
1329 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1330 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1331 promiseStopFlag.set_value(true);
1332 };
1333 SimulateDownPointerEvent(
1334 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1335 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1336 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1337 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1338 ASSERT_TRUE(dragData);
1339 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1340 std::make_shared<UnitTestStartDragListener>(callback));
1341 ASSERT_EQ(ret, RET_OK);
1342 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1343 EXPECT_EQ(ret, RET_OK);
1344 ret = InteractionManager::GetInstance()->UpdateDragStyle(DragCursorStyle::COPY);
1345 ASSERT_EQ(ret, RET_OK);
1346 int32_t pid = InteractionManager::GetInstance()->GetDragTargetPid();
1347 FI_HILOGI("Target pid:%{public}d", pid);
1348 ASSERT_TRUE(pid > 0);
1349 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1350 InteractionManager::GetInstance()->StopDrag(dropResult);
1351 ASSERT_TRUE(futureStopFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1352 std::future_status::timeout);
1353 }
1354 }
1355
1356 /**
1357 * @tc.name: TouchEventDispatch
1358 * @tc.desc: Dispatch the touchscreen events
1359 * @tc.type: FUNC
1360 * @tc.require:
1361 */
1362 HWTEST_F(InteractionManagerTest, TouchEventDispatch, TestSize.Level1)
1363 {
1364 CALL_TEST_DEBUG;
1365 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
1366 if (g_deviceTouchId < 0) {
1367 ASSERT_TRUE(g_deviceTouchId < 0);
1368 } else {
1369 std::promise<bool> promiseFlag;
1370 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1602(const DragNotifyMsg& notifyMessage) 1371 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1372 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1373 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1374 promiseFlag.set_value(true);
1375 };
1376 SimulateDownPointerEvent(
1377 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1378 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1379 std::optional<DragData> dragDataInfo = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1380 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1381 ASSERT_TRUE(dragDataInfo);
1382 int32_t result = InteractionManager::GetInstance()->StartDrag(dragDataInfo.value(),
1383 std::make_shared<UnitTestStartDragListener>(callback));
1384 ASSERT_EQ(result, RET_OK);
1385 std::promise<bool> promiseEventFlag;
1386 std::future<bool> futureEventFlag = promiseEventFlag.get_future();
1387 auto callbackPtr = std::make_shared<InputEventCallbackTest>(
__anona4ee5cad1702null1388 [&promiseEventFlag]{promiseEventFlag.set_value(true);});
1389 int32_t monitorId = TestAddMonitor(callbackPtr);
1390 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
1391 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1392 ASSERT_TRUE(futureEventFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1393 std::future_status::timeout);
1394 TestRemoveMonitor(monitorId);
1395 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1396 result = InteractionManager::GetInstance()->StopDrag(dropResult);
1397 ASSERT_EQ(result, RET_OK);
1398 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1399 std::future_status::timeout);
1400 }
1401 RemovePermission();
1402 }
1403
1404 /**
1405 * @tc.name: MouseEventDispatch
1406 * @tc.desc: Dispatch the mouse events
1407 * @tc.type: FUNC
1408 * @tc.require:
1409 */
1410 HWTEST_F(InteractionManagerTest, MouseEventDispatch, TestSize.Level1)
1411 {
1412 CALL_TEST_DEBUG;
1413 SetPermission(SYSTEM_CORE, g_cores, sizeof(g_cores) / sizeof(g_cores[0]));
1414 if (g_deviceMouseId < 0) {
1415 ASSERT_TRUE(g_deviceMouseId < 0);
1416 } else {
1417 std::promise<bool> promiseFlag;
1418 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1802(const DragNotifyMsg& notifyMessage) 1419 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1420 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1421 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1422 promiseFlag.set_value(true);
1423 };
1424 SimulateDownPointerEvent(
1425 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, 0);
1426 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1427 MMI::PointerEvent::SOURCE_TYPE_MOUSE, 0, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1428 ASSERT_TRUE(dragData);
1429 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1430 std::make_shared<UnitTestStartDragListener>(callback));
1431 ASSERT_EQ(ret, RET_OK);
1432 std::promise<bool> promiseEventFlag;
1433 std::future<bool> futureEventFlag = promiseEventFlag.get_future();
1434 auto callbackPtr = std::make_shared<InputEventCallbackTest>(
__anona4ee5cad1902null1435 [&promiseEventFlag]{promiseEventFlag.set_value(true);});
1436 int32_t monitorId = TestAddMonitor(callbackPtr);
1437 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
1438 MMI::PointerEvent::SOURCE_TYPE_MOUSE, TOUCH_POINTER_ID, true);
1439 ASSERT_TRUE(futureEventFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1440 std::future_status::timeout);
1441 TestRemoveMonitor(monitorId);
1442 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1443 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1444 ASSERT_EQ(ret, RET_OK);
1445 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1446 std::future_status::timeout);
1447 }
1448 RemovePermission();
1449 }
1450
1451 /**
1452 * @tc.name: InteractionManagerTest_SetDragWindowVisible
1453 * @tc.desc: Set Drag Window Visible
1454 * @tc.type: FUNC
1455 * @tc.require:
1456 */
1457 HWTEST_F(InteractionManagerTest, InteractionManagerTest_SetDragWindowVisible, TestSize.Level1)
1458 {
1459 CALL_TEST_DEBUG;
1460 int32_t ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1461 FI_HILOGD("ret:%{public}d", ret);
1462 ASSERT_EQ(ret, RET_ERR);
1463 ret = InteractionManager::GetInstance()->SetDragWindowVisible(false);
1464 FI_HILOGD("ret:%{public}d", ret);
1465 ASSERT_EQ(ret, RET_ERR);
1466 }
1467
1468 /**
1469 * @tc.name: InteractionManagerTest_GetShadowOffset
1470 * @tc.desc: Get Shadow Offset
1471 * @tc.type: FUNC
1472 * @tc.require:
1473 */
1474 HWTEST_F(InteractionManagerTest, InteractionManagerTest_GetShadowOffset, TestSize.Level1)
1475 {
1476 CALL_TEST_DEBUG;
1477 int32_t offsetX = 0;
1478 int32_t offsetY = 0;
1479 int32_t width = 0;
1480 int32_t height = 0;
1481 std::promise<bool> promise;
1482 std::future<bool> futureFlag = promise.get_future();
__anona4ee5cad1a02(const DragNotifyMsg& notifyMessage) 1483 auto callback = [&promise](const DragNotifyMsg& notifyMessage) {
1484 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1485 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1486 promise.set_value(true);
1487 };
1488 SimulateDownPointerEvent(
1489 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1490 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1491 std::optional<DragData> dragDataInfo = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1492 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1493 ASSERT_TRUE(dragDataInfo);
1494 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragDataInfo.value(),
1495 std::make_shared<UnitTestStartDragListener>(callback));
1496 ASSERT_EQ(ret, RET_OK);
1497 ret = InteractionManager::GetInstance()->GetShadowOffset(offsetX, offsetY, width, height);
1498 FI_HILOGD("offsetX:%{public}d, offsetY:%{public}d, width:%{public}d, height:%{public}d",
1499 offsetX, offsetY, width, height);
1500 ASSERT_EQ(ret, RET_OK);
1501 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1502 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1503 ASSERT_EQ(ret, RET_OK);
1504 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) != std::future_status::timeout);
1505 }
1506
1507 /**
1508 * @tc.name: GetUdKey_Mouse
1509 * @tc.desc: Get the udKey dragged by the mouse
1510 * @tc.type: FUNC
1511 * @tc.require:
1512 */
1513 HWTEST_F(InteractionManagerTest, GetUdKey_Mouse, TestSize.Level1)
1514 {
1515 CALL_TEST_DEBUG;
1516 if (g_deviceMouseId < 0) {
1517 ASSERT_TRUE(g_deviceMouseId < 0);
1518 } else {
1519 SimulateDownPointerEvent(
1520 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1521 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1522 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1523 ASSERT_TRUE(dragData);
1524 std::promise<bool> promiseFlag;
1525 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1b02(const DragNotifyMsg& notifyMessage) 1526 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1527 FI_HILOGD("Get ud key, displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1528 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1529 promiseFlag.set_value(true);
1530 };
1531 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1532 std::make_shared<UnitTestStartDragListener>(callback));
1533 ASSERT_EQ(ret, RET_OK);
1534 std::string udKey;
1535 ret = InteractionManager::GetInstance()->GetUdKey(udKey);
1536 ASSERT_EQ(ret, RET_OK);
1537 ASSERT_EQ(udKey, UD_KEY);
1538 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1539 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1540 ASSERT_EQ(ret, RET_OK);
1541 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1542 std::future_status::timeout);
1543 }
1544 }
1545
1546 /**
1547 * @tc.name: GetUdKey_Touch
1548 * @tc.desc: Get the udKey dragged by the touchscreen
1549 * @tc.type: FUNC
1550 * @tc.require:
1551 */
1552 HWTEST_F(InteractionManagerTest, GetUdKey_Touch, TestSize.Level1)
1553 {
1554 CALL_TEST_DEBUG;
1555 if (g_deviceTouchId < 0) {
1556 ASSERT_TRUE(g_deviceTouchId < 0);
1557 } else {
1558 std::promise<bool> promiseFlag;
1559 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1c02(const DragNotifyMsg& notifyMessage) 1560 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1561 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, target:%{public}d, result:%{public}d",
1562 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.targetPid, notifyMessage.result);
1563 promiseFlag.set_value(true);
1564 };
1565 SimulateDownPointerEvent(
1566 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1567 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1568 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1569 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1570 ASSERT_TRUE(dragData);
1571 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1572 std::make_shared<UnitTestStartDragListener>(callback));
1573 ASSERT_EQ(ret, RET_OK);
1574 std::string udKey;
1575 ret = InteractionManager::GetInstance()->GetUdKey(udKey);
1576 ASSERT_EQ(ret, RET_OK);
1577 ASSERT_EQ(udKey, UD_KEY);
1578 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1579 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1580 ASSERT_EQ(ret, RET_OK);
1581 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1582 std::future_status::timeout);
1583 }
1584 }
1585
1586 /**
1587 * @tc.name: GetDragData_Success
1588 * @tc.desc: Get the dragData from interface successfully
1589 * @tc.type: FUNC
1590 * @tc.require:
1591 */
1592 HWTEST_F(InteractionManagerTest, GetDragData_Success, TestSize.Level1)
1593 {
1594 CALL_TEST_DEBUG;
1595 if (g_deviceTouchId < 0) {
1596 ASSERT_TRUE(g_deviceTouchId < 0);
1597 } else {
1598 std::promise<bool> promiseFlag;
1599 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1d02(const DragNotifyMsg& notifyMessage) 1600 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1601 FI_HILOGD("Get drag data, displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1602 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1603 promiseFlag.set_value(true);
1604 };
1605 SimulateDownPointerEvent(
1606 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1607 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1608 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1609 ASSERT_TRUE(dragData);
1610 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1611 std::make_shared<UnitTestStartDragListener>(callback));
1612 ASSERT_EQ(ret, RET_OK);
1613
1614 DragData replyDragData;
1615 ret = InteractionManager::GetInstance()->GetDragData(replyDragData);
1616 ASSERT_EQ(ret, RET_OK);
1617 ASSERT_EQ(replyDragData, dragData.value());
1618 PrintDragData(replyDragData);
1619 SimulateUpPointerEvent(
1620 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1621 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1622 InteractionManager::GetInstance()->StopDrag(dropResult);
1623 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1624 std::future_status::timeout);
1625 }
1626 }
1627
1628 /**
1629 * @tc.name: GetDragData_Failed
1630 * @tc.desc: Get the dragData from interface failed
1631 * @tc.type: FUNC
1632 * @tc.require:
1633 */
1634 HWTEST_F(InteractionManagerTest, GetDragData_Failed, TestSize.Level1)
1635 {
1636 CALL_TEST_DEBUG;
1637 if (g_deviceTouchId < 0) {
1638 ASSERT_TRUE(g_deviceTouchId < 0);
1639 } else {
1640 SimulateDownPointerEvent(
1641 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1642 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1643 DragData dragData;
1644 int32_t ret = InteractionManager::GetInstance()->GetDragData(dragData);
1645 SimulateUpPointerEvent(
1646 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1647 ASSERT_EQ(ret, RET_ERR);
1648 }
1649 }
1650
1651 /**
1652 * @tc.name: GetDragState
1653 * @tc.desc: Get the dragState from interface
1654 * @tc.type: FUNC
1655 * @tc.require:
1656 */
1657 HWTEST_F(InteractionManagerTest, GetDragState, TestSize.Level1)
1658 {
1659 CALL_TEST_DEBUG;
1660 std::promise<bool> promiseFlag;
1661 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1e02(const DragNotifyMsg& notifyMessage) 1662 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1663 FI_HILOGD("Drag state, displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1664 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1665 promiseFlag.set_value(true);
1666 };
1667 SimulateDownPointerEvent(
1668 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1669 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1670 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1671 ASSERT_TRUE(dragData);
1672 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1673 std::make_shared<UnitTestStartDragListener>(callback));
1674 ASSERT_EQ(ret, RET_OK);
1675
1676 DragState dragState;
1677 ret = InteractionManager::GetInstance()->GetDragState(dragState);
1678 FI_HILOGD("InteractionManager::dragState:%{public}d", dragState);
1679 EXPECT_EQ(ret, RET_OK);
1680 EXPECT_EQ(dragState, DragState::START);
1681
1682 SimulateUpPointerEvent(
1683 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID);
1684 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1685 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1686 ASSERT_EQ(ret, RET_OK);
1687 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1688 std::future_status::timeout);
1689 ret = InteractionManager::GetInstance()->GetDragState(dragState);
1690 FI_HILOGD("dragState:%{public}d", dragState);
1691 EXPECT_EQ(ret, RET_OK);
1692 EXPECT_EQ(dragState, DragState::STOP);
1693 }
1694
1695 class HotAreaListenerTest : public IHotAreaListener {
1696 public:
HotAreaListenerTest()1697 HotAreaListenerTest() {}
HotAreaListenerTest(std::string name)1698 explicit HotAreaListenerTest(std::string name) : testName_(name) {}
OnHotAreaMessage(int32_t displayX,int32_t displayY,HotAreaType msg,bool isEdge)1699 void OnHotAreaMessage(int32_t displayX, int32_t displayY, HotAreaType msg, bool isEdge) override
1700 {
1701 if (testName_.empty()) {
1702 testName_ = std::string("HOT_AREA");
1703 }
1704 FI_HILOGD("%{public}s, type:%{public}s, isEdge:%{public}d, displayX:%{public}d, displayY:%{public}d",
1705 testName_.c_str(), ShowMessage(msg).c_str(), isEdge, displayX, displayY);
1706 };
1707
1708 private:
ShowMessage(HotAreaType msg)1709 std::string ShowMessage(HotAreaType msg)
1710 {
1711 std::string type = "none-area";
1712 const std::map<HotAreaType, std::string> areaType = {
1713 { HotAreaType::AREA_LEFT, "left-area"},
1714 { HotAreaType::AREA_RIGHT, "right-area"},
1715 { HotAreaType::AREA_TOP, "top-area"},
1716 { HotAreaType::AREA_BOTTOM, "bottom-area"}
1717 };
1718 auto item = areaType.find(msg);
1719 if (item != areaType.end()) {
1720 type = item->second;
1721 }
1722 return type;
1723 }
1724
1725 private:
1726 std::string testName_;
1727 };
1728
1729 /**
1730 * @tc.name: AddHotAreaListener_001
1731 * @tc.desc: Add hot area listener
1732 * @tc.type: FUNC
1733 * @tc.require:
1734 */
1735 HWTEST_F(InteractionManagerTest, AddHotAreaListener_001, TestSize.Level1)
1736 {
1737 CALL_TEST_DEBUG;
1738 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
1739 auto listener = std::make_shared<HotAreaListenerTest>(std::string("HOT_AREA"));
1740 int32_t ret = InteractionManager::GetInstance()->AddHotAreaListener(listener);
1741 #ifdef OHOS_BUILD_ENABLE_COORDINATION
1742 ASSERT_EQ(ret, RET_OK);
1743 #else
1744 ASSERT_EQ(ret, ERROR_UNSUPPORT);
1745 #endif // OHOS_BUILD_ENABLE_COORDINATION
1746 ret = InteractionManager::GetInstance()->RemoveHotAreaListener(listener);
1747 #ifdef OHOS_BUILD_ENABLE_COORDINATION
1748 ASSERT_EQ(ret, RET_OK);
1749 #else
1750 ASSERT_EQ(ret, ERROR_UNSUPPORT);
1751 #endif // OHOS_BUILD_ENABLE_COORDINATION
1752 RemovePermission();
1753 }
1754
1755 /**
1756 * @tc.name: AddHotAreaListener_002
1757 * @tc.desc: Add hot area listener
1758 * @tc.type: FUNC
1759 * @tc.require:
1760 */
1761 HWTEST_F(InteractionManagerTest, AddHotAreaListener_002, TestSize.Level1)
1762 {
1763 CALL_DEBUG_ENTER;
1764 SetPermission(SYSTEM_BASIC, g_basics, sizeof(g_basics) / sizeof(g_basics[0]));
1765 sptr<Rosen::Display> display = Rosen::DisplayManager::GetInstance().GetDisplayById(0);
1766 CHKPV(display);
1767 g_screenWidth = display->GetWidth();
1768 g_screenHeight = display->GetHeight();
1769 auto listener = std::make_shared<HotAreaListenerTest>(std::string("HOT_AREA"));
1770 int32_t ret = InteractionManager::GetInstance()->AddHotAreaListener(listener);
1771 #ifdef OHOS_BUILD_ENABLE_COORDINATION
1772 ASSERT_EQ(ret, RET_OK);
1773 #else
1774 ASSERT_EQ(ret, ERROR_UNSUPPORT);
1775 #endif // OHOS_BUILD_ENABLE_COORDINATION
1776 SimulateMovePointerEvent({ HOT_AREA_STEP, HOT_AREA_COOR }, { HOT_AREA_STEP - HOT_AREA_SPAN, HOT_AREA_COOR },
1777 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, true);
1778 SimulateMovePointerEvent({ HOT_AREA_COOR, HOT_AREA_STEP }, { HOT_AREA_COOR, HOT_AREA_STEP - HOT_AREA_SPAN },
1779 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, true);
1780 SimulateMovePointerEvent({ g_screenWidth - HOT_AREA_STEP, HOT_AREA_COOR },
1781 { g_screenWidth - HOT_AREA_SPAN, HOT_AREA_COOR },
1782 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, true);
1783 SimulateMovePointerEvent({ HOT_AREA_COOR, g_screenHeight - HOT_AREA_STEP },
1784 { HOT_AREA_COOR, g_screenHeight - HOT_AREA_SPAN },
1785 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, true);
1786 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_TOUCH_DOWN_MS));
1787 ret = InteractionManager::GetInstance()->RemoveHotAreaListener(listener);
1788 #ifdef OHOS_BUILD_ENABLE_COORDINATION
1789 ASSERT_EQ(ret, RET_OK);
1790 #else
1791 ASSERT_EQ(ret, ERROR_UNSUPPORT);
1792 #endif // OHOS_BUILD_ENABLE_COORDINATION
1793 RemovePermission();
1794 }
1795
1796 /**
1797 * @tc.name: InteractionManagerTest_GetDragSummary
1798 * @tc.desc: Get drag summarys
1799 * @tc.type: FUNC
1800 * @tc.require:
1801 */
1802 HWTEST_F(InteractionManagerTest, InteractionManagerTest_GetDragSummary, TestSize.Level1)
1803 {
1804 CALL_TEST_DEBUG;
1805 std::promise<bool> promiseFlag;
1806 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad1f02(const DragNotifyMsg& notifyMessage) 1807 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1808 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1809 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1810 promiseFlag.set_value(true);
1811 };
1812 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1813 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1814 ASSERT_TRUE(dragData);
1815 const std::string udType = "general.message";
1816 constexpr int64_t recordSize = 20;
1817 std::map<std::string, int64_t> summarys = { { udType, recordSize } };
1818 dragData.value().summarys = summarys;
1819 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1820 std::make_shared<UnitTestStartDragListener>(callback));
1821 ASSERT_EQ(ret, RET_OK);
1822 summarys.clear();
1823 ret = InteractionManager::GetInstance()->GetDragSummary(summarys);
1824 EXPECT_EQ(ret, RET_OK);
1825 EXPECT_EQ(summarys[udType], recordSize);
1826 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1827 InteractionManager::GetInstance()->StopDrag(dropResult);
1828 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1829 std::future_status::timeout);
1830 }
1831
1832 /**
1833 * @tc.name: InteractionManagerTest_UpdatePreviewStyle
1834 * @tc.desc: Update drag item style
1835 * @tc.type: FUNC
1836 * @tc.require:
1837 */
1838 HWTEST_F(InteractionManagerTest, InteractionManagerTest_UpdatePreviewStyle, TestSize.Level1)
1839 {
1840 CALL_TEST_DEBUG;
1841 std::promise<bool> promiseFlag;
1842 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2002(const DragNotifyMsg& notifyMessage) 1843 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1844 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1845 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1846 promiseFlag.set_value(true);
1847 };
1848 SimulateDownPointerEvent(
1849 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1850 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1851 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1852 ASSERT_TRUE(dragData);
1853 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1854 std::make_shared<UnitTestStartDragListener>(callback));
1855 ASSERT_EQ(ret, RET_OK);
1856 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1857 EXPECT_EQ(ret, RET_OK);
1858 std::pair<int32_t, int32_t> enterPos { 500, 50 };
1859 std::pair<int32_t, int32_t> leavePos { 500, 200 };
1860 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { enterPos.first, enterPos.second },
1861 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1862 PreviewStyle previewStyleIn;
1863 previewStyleIn.types = { PreviewType::FOREGROUND_COLOR };
1864 previewStyleIn.foregroundColor = FOREGROUND_COLOR_IN;
1865 ret = InteractionManager::GetInstance()->UpdatePreviewStyle(previewStyleIn);
1866 EXPECT_EQ(ret, RET_OK);
1867 SimulateMovePointerEvent({ enterPos.first, enterPos.second }, { leavePos.first, leavePos.second },
1868 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1869 PreviewStyle previewStyleOut;
1870 previewStyleOut.types = { PreviewType::FOREGROUND_COLOR };
1871 previewStyleOut.foregroundColor = FOREGROUND_COLOR_OUT;
1872 ret = InteractionManager::GetInstance()->UpdatePreviewStyle(previewStyleOut);
1873 EXPECT_EQ(ret, RET_OK);
1874 SimulateMovePointerEvent({ leavePos.first, leavePos.second }, { DRAG_DST_X, DRAG_DST_Y },
1875 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1876 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1877 InteractionManager::GetInstance()->StopDrag(dropResult);
1878 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1879 std::future_status::timeout);
1880 }
1881
1882
1883 /**
1884 * @tc.name: InteractionManagerTest_UpdatePreviewStyleWithAnimation
1885 * @tc.desc: Update drag item style with animation
1886 * @tc.type: FUNC
1887 * @tc.require:
1888 */
1889 HWTEST_F(InteractionManagerTest, InteractionManagerTest_UpdatePreviewStyleWithAnimation, TestSize.Level1)
1890 {
1891 CALL_TEST_DEBUG;
1892 std::promise<bool> promiseFlag;
1893 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2102(const DragNotifyMsg& notifyMessage) 1894 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1895 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1896 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1897 promiseFlag.set_value(true);
1898 };
1899 SimulateDownPointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1900 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1901 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1902 ASSERT_TRUE(dragData);
1903 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1904 std::make_shared<UnitTestStartDragListener>(callback));
1905 ASSERT_EQ(ret, RET_OK);
1906 ret = InteractionManager::GetInstance()->SetDragWindowVisible(true);
1907 EXPECT_EQ(ret, RET_OK);
1908 std::pair<int32_t, int32_t> enterPos { 500, 50 };
1909 std::pair<int32_t, int32_t> leavePos { 500, 200 };
1910 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { enterPos.first, enterPos.second },
1911 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1912 PreviewStyle previewStyleIn;
1913 previewStyleIn.types = { PreviewType::FOREGROUND_COLOR };
1914 previewStyleIn.foregroundColor = FOREGROUND_COLOR_IN;
1915 PreviewAnimation animationIn;
1916 AssignToAnimation(animationIn);
1917 ret = InteractionManager::GetInstance()->UpdatePreviewStyleWithAnimation(previewStyleIn, animationIn);
1918 EXPECT_EQ(ret, RET_OK);
1919 SimulateMovePointerEvent({ enterPos.first, enterPos.second }, { leavePos.first, leavePos.second },
1920 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1921 PreviewStyle previewStyleOut;
1922 previewStyleOut.types = { PreviewType::FOREGROUND_COLOR };
1923 previewStyleOut.foregroundColor = FOREGROUND_COLOR_OUT;
1924 PreviewAnimation animationOut;
1925 AssignToAnimation(animationOut);
1926 ret = InteractionManager::GetInstance()->UpdatePreviewStyleWithAnimation(previewStyleOut, animationOut);
1927 EXPECT_EQ(ret, RET_OK);
1928 SimulateMovePointerEvent({ leavePos.first, leavePos.second }, { DRAG_DST_X, DRAG_DST_Y },
1929 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
1930 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1931 InteractionManager::GetInstance()->StopDrag(dropResult);
1932 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1933 std::future_status::timeout);
1934 }
1935
1936 /**
1937 * @tc.name: InteractionManagerTest_GetExtraInfo
1938 * @tc.desc: Get extraInfo
1939 * @tc.type: FUNC
1940 * @tc.require:
1941 */
1942 HWTEST_F(InteractionManagerTest, InteractionManagerTest_GetExtraInfo, TestSize.Level1)
1943 {
1944 CALL_TEST_DEBUG;
1945 if (g_deviceMouseId >= 0) {
1946 SimulateDownPointerEvent(
1947 { DRAG_SRC_X, DRAG_SRC_Y }, MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID);
1948 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1949 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1950 ASSERT_TRUE(dragData);
1951 std::promise<bool> promiseFlag;
1952 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2202(const DragNotifyMsg& notifyMessage) 1953 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
1954 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
1955 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
1956 promiseFlag.set_value(true);
1957 };
1958 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
1959 std::make_shared<UnitTestStartDragListener>(callback));
1960 ASSERT_EQ(ret, RET_OK);
1961 std::string extraInfo;
1962 ret = InteractionManager::GetInstance()->GetExtraInfo(extraInfo);
1963 ASSERT_EQ(ret, RET_OK);
1964 ASSERT_EQ(extraInfo, EXTRA_INFO);
1965 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
1966 ret = InteractionManager::GetInstance()->StopDrag(dropResult);
1967 ASSERT_EQ(ret, RET_OK);
1968 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
1969 std::future_status::timeout);
1970 }
1971 }
1972
1973 /**
1974 * @tc.name: TestDragDataUtil_Packer
1975 * @tc.desc: Pack up dragData
1976 * @tc.type: FUNC
1977 * @tc.require:
1978 */
1979 HWTEST_F(InteractionManagerTest, TestDragDataUtil_Packer, TestSize.Level1)
1980 {
1981 CALL_TEST_DEBUG;
1982 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
1983 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
1984 ASSERT_TRUE(dragData);
1985 Parcel parcel;
1986 int32_t ret = DragDataUtil::Marshalling(dragData.value(), parcel, false);
1987 ASSERT_EQ(ret, RET_OK);
1988 DragData dragDataFromParcel;
1989 ret = DragDataUtil::UnMarshalling(parcel, dragDataFromParcel, false);
1990 ASSERT_EQ(ret, RET_OK);
1991 ASSERT_EQ(dragData.value(), dragDataFromParcel);
1992 }
1993
1994 /**
1995 * @tc.name: TestDragDataUtil_Packer_Cross
1996 * @tc.desc: Pack up dragData
1997 * @tc.type: FUNC
1998 * @tc.require:
1999 */
2000 HWTEST_F(InteractionManagerTest, TestDragDataUtil_Packer_Cross, TestSize.Level1)
2001 {
2002 CALL_TEST_DEBUG;
2003 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
2004 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
2005 ASSERT_TRUE(dragData);
2006 Parcel parcel;
2007 int32_t ret = DragDataUtil::Marshalling(dragData.value(), parcel, true);
2008 ASSERT_EQ(ret, RET_OK);
2009 DragData dragDataFromParcel;
2010 ret = DragDataUtil::UnMarshalling(parcel, dragDataFromParcel, true);
2011 ASSERT_EQ(ret, RET_OK);
2012 ASSERT_EQ(dragData.value(), dragDataFromParcel);
2013 }
2014
2015 /**
2016 * @tc.name: InteractionManagerTest_GetDragAction_001
2017 * @tc.desc: Get drag action with no keyboard events of keys in dragging
2018 * @tc.type: FUNC
2019 * @tc.require:
2020 */
2021 HWTEST_F(InteractionManagerTest, GetDragAction_001, TestSize.Level1)
2022 {
2023 CALL_TEST_DEBUG;
2024 std::promise<bool> promiseFlag;
2025 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2302(const DragNotifyMsg& notifyMessage) 2026 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
2027 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
2028 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
2029 promiseFlag.set_value(true);
2030 };
2031 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
2032 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
2033 ASSERT_TRUE(dragData);
2034 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
2035 std::make_shared<UnitTestStartDragListener>(callback));
2036 ASSERT_EQ(ret, RET_OK);
2037 DragAction dragAction { DragAction::INVALID };
2038 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2039 EXPECT_EQ(ret, RET_OK);
2040 EXPECT_EQ(dragAction, DragAction::MOVE);
2041 PrintDragAction(dragAction);
2042 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
2043 InteractionManager::GetInstance()->StopDrag(dropResult);
2044 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
2045 std::future_status::timeout);
2046 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2047 EXPECT_EQ(ret, RET_ERR);
2048 }
2049
2050 /**
2051 * @tc.name: InteractionManagerTest_GetDragAction_002
2052 * @tc.desc: Get drag action with simple press and release keyboard events of keys in dragging
2053 * @tc.type: FUNC
2054 * @tc.require:
2055 */
2056 HWTEST_F(InteractionManagerTest, GetDragAction_002, TestSize.Level1)
2057 {
2058 CALL_TEST_DEBUG;
2059 std::promise<bool> promiseFlag;
2060 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2402(const DragNotifyMsg& notifyMessage) 2061 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
2062 FI_HILOGD("DisplayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
2063 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
2064 promiseFlag.set_value(true);
2065 };
2066 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_INJECT_MS));
2067 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
2068 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
2069 ASSERT_TRUE(dragData);
2070 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
2071 std::make_shared<UnitTestStartDragListener>(callback));
2072 ASSERT_EQ(ret, RET_OK);
2073 ret = InteractionManager::GetInstance()->UpdateDragStyle(DragCursorStyle::MOVE);
2074 ASSERT_EQ(ret, RET_OK);
2075 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
2076 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
2077 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
2078 DragAction dragAction { DragAction::INVALID };
2079 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2080 EXPECT_EQ(ret, RET_OK);
2081 EXPECT_EQ(dragAction, DragAction::COPY);
2082 PrintDragAction(dragAction);
2083 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
2084 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2085 EXPECT_EQ(ret, RET_OK);
2086 EXPECT_EQ(dragAction, DragAction::MOVE);
2087 PrintDragAction(dragAction);
2088 ClearUpKeyEvent();
2089 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_A);
2090 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2091 EXPECT_EQ(ret, RET_OK);
2092 EXPECT_EQ(dragAction, DragAction::MOVE);
2093 PrintDragAction(dragAction);
2094 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_A);
2095 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2096 EXPECT_EQ(ret, RET_OK);
2097 EXPECT_EQ(dragAction, DragAction::MOVE);
2098 PrintDragAction(dragAction);
2099 ClearUpKeyEvent();
2100 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
2101 InteractionManager::GetInstance()->StopDrag(dropResult);
2102 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
2103 std::future_status::timeout);
2104 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2105 EXPECT_EQ(ret, RET_ERR);
2106 }
2107
2108 /**
2109 * @tc.name: InteractionManagerTest_GetDragAction_003
2110 * @tc.desc: Get drag action with simple press and release keyboard events of keys in start drag
2111 * @tc.type: FUNC
2112 * @tc.require:
2113 */
2114 HWTEST_F(InteractionManagerTest, GetDragAction_003, TestSize.Level1)
2115 {
2116 CALL_TEST_DEBUG;
2117 if (g_deviceMouseId < 0) {
2118 ASSERT_TRUE(g_deviceMouseId < 0);
2119 } else {
2120 int32_t ret = RET_ERR;
2121 std::promise<bool> promiseFlag;
2122 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2502(const DragNotifyMsg& notifyMessage) 2123 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
2124 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
2125 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
2126 promiseFlag.set_value(true);
2127 };
2128 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
2129 MMI::PointerEvent::SOURCE_TYPE_MOUSE, MOUSE_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
2130 ASSERT_TRUE(dragData);
2131 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
2132 std::make_shared<UnitTestStartDragListener>(callback));
2133 ASSERT_EQ(ret, RET_OK);
2134 ret = InteractionManager::GetInstance()->UpdateDragStyle(DragCursorStyle::MOVE);
2135 ASSERT_EQ(ret, RET_OK);
2136 DragAction dragAction { DragAction::INVALID };
2137 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2138 EXPECT_EQ(ret, RET_OK);
2139 EXPECT_EQ(dragAction, DragAction::MOVE);
2140 PrintDragAction(dragAction);
2141 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
2142 InteractionManager::GetInstance()->StopDrag(dropResult);
2143 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
2144 std::future_status::timeout);
2145 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2146 EXPECT_EQ(ret, RET_ERR);
2147 }
2148 }
2149
2150 /**
2151 * @tc.name: InteractionManagerTest_GetDragAction_004
2152 * @tc.desc: Get drag action with multiple press and release keyboard events of keys in dragging
2153 * @tc.type: FUNC
2154 * @tc.require:
2155 */
2156 HWTEST_F(InteractionManagerTest, GetDragAction_004, TestSize.Level1)
2157 {
2158 CALL_TEST_DEBUG;
2159 std::promise<bool> promiseFlag;
2160 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2602(const DragNotifyMsg& notifyMessage) 2161 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
2162 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
2163 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
2164 promiseFlag.set_value(true);
2165 };
2166 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
2167 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
2168 ASSERT_TRUE(dragData);
2169 int32_t ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
2170 std::make_shared<UnitTestStartDragListener>(callback));
2171 ASSERT_EQ(ret, RET_OK);
2172 ret = InteractionManager::GetInstance()->UpdateDragStyle(DragCursorStyle::MOVE);
2173 ASSERT_EQ(ret, RET_OK);
2174 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
2175 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
2176 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
2177 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_A);
2178 DragAction dragAction { DragAction::INVALID };
2179 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2180 EXPECT_EQ(ret, RET_OK);
2181 EXPECT_EQ(dragAction, DragAction::COPY);
2182 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_A);
2183 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2184 EXPECT_EQ(ret, RET_OK);
2185 EXPECT_EQ(dragAction, DragAction::COPY);
2186 ClearUpKeyEvent();
2187 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_B);
2188 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2189 EXPECT_EQ(ret, RET_OK);
2190 EXPECT_EQ(dragAction, DragAction::COPY);
2191 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
2192 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2193 EXPECT_EQ(ret, RET_OK);
2194 EXPECT_EQ(dragAction, DragAction::MOVE);
2195 ClearUpKeyEvent();
2196 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
2197 InteractionManager::GetInstance()->StopDrag(dropResult);
2198 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
2199 std::future_status::timeout);
2200 ret = InteractionManager::GetInstance()->GetDragAction(dragAction);
2201 EXPECT_EQ(ret, RET_ERR);
2202 }
2203
2204 /**
2205 * @tc.name: InteractionManagerTest_GetDragAction_005
2206 * @tc.desc: Get style notification with multiple press and release keyboard events of keys in dragging
2207 * @tc.type: FUNC
2208 * @tc.require:
2209 */
2210 HWTEST_F(InteractionManagerTest, GetDragAction_005, TestSize.Level1)
2211 {
2212 CALL_TEST_DEBUG;
2213 auto listener = std::make_shared<SubscriptListenerTest>("SubscriptListener");
2214 int32_t ret = InteractionManager::GetInstance()->AddSubscriptListener(listener);
2215 ASSERT_EQ(ret, RET_OK);
2216 std::promise<bool> promiseFlag;
2217 std::future<bool> futureFlag = promiseFlag.get_future();
__anona4ee5cad2702(const DragNotifyMsg& notifyMessage) 2218 auto callback = [&promiseFlag](const DragNotifyMsg& notifyMessage) {
2219 FI_HILOGD("displayX:%{public}d, displayY:%{public}d, result:%{public}d, target:%{public}d",
2220 notifyMessage.displayX, notifyMessage.displayY, notifyMessage.result, notifyMessage.targetPid);
2221 promiseFlag.set_value(true);
2222 };
2223 std::optional<DragData> dragData = CreateDragData({ TEST_PIXEL_MAP_WIDTH, TEST_PIXEL_MAP_HEIGHT },
2224 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, DISPLAY_ID, { DRAG_SRC_X, DRAG_SRC_Y });
2225 ASSERT_TRUE(dragData);
2226 ret = InteractionManager::GetInstance()->StartDrag(dragData.value(),
2227 std::make_shared<UnitTestStartDragListener>(callback));
2228 ASSERT_EQ(ret, RET_OK);
2229 ret = InteractionManager::GetInstance()->UpdateDragStyle(DragCursorStyle::MOVE);
2230 ASSERT_EQ(ret, RET_OK);
2231 SimulateMovePointerEvent({ DRAG_SRC_X, DRAG_SRC_Y }, { DRAG_DST_X, DRAG_DST_Y },
2232 MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN, TOUCH_POINTER_ID, true);
2233 DragCursorStyle recvStyle = listener->GetDragStyle();
2234 EXPECT_EQ(recvStyle, DragCursorStyle::MOVE);
2235 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
2236 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_B);
2237 recvStyle = listener->GetDragStyle();
2238 EXPECT_EQ(recvStyle, DragCursorStyle::COPY);
2239 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
2240 ClearUpKeyEvent();
2241 recvStyle = listener->GetDragStyle();
2242 EXPECT_EQ(recvStyle, DragCursorStyle::MOVE);
2243 SimulateDownKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_RIGHT);
2244 recvStyle = listener->GetDragStyle();
2245 EXPECT_EQ(recvStyle, DragCursorStyle::COPY);
2246 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_B);
2247 ClearUpKeyEvent();
2248 recvStyle = listener->GetDragStyle();
2249 EXPECT_EQ(recvStyle, DragCursorStyle::COPY);
2250 SimulateUpKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_RIGHT);
2251 ClearUpKeyEvent();
2252 recvStyle = listener->GetDragStyle();
2253 EXPECT_EQ(recvStyle, DragCursorStyle::MOVE);
2254 DragDropResult dropResult { DragResult::DRAG_SUCCESS, HAS_CUSTOM_ANIMATION, WINDOW_ID };
2255 InteractionManager::GetInstance()->StopDrag(dropResult);
2256 ASSERT_TRUE(futureFlag.wait_for(std::chrono::milliseconds(PROMISE_WAIT_SPAN_MS)) !=
2257 std::future_status::timeout);
2258 ret = InteractionManager::GetInstance()->RemoveSubscriptListener(listener);
2259 ASSERT_EQ(ret, RET_OK);
2260 }
2261 } // namespace DeviceStatus
2262 } // namespace Msdp
2263 } // namespace OHOS
2264