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 "drag_data_manager_test.h"
17
18 #include <ipc_skeleton.h>
19
20 #include "pointer_event.h"
21 #include "securec.h"
22
23 #include "devicestatus_define.h"
24 #define private public
25 #include "drag_drawing.h"
26
27 namespace OHOS {
28 namespace Msdp {
29 namespace DeviceStatus {
30 using namespace testing::ext;
31 namespace {
32 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DragDataManagerTest" };
33 constexpr int32_t MAX_PIXEL_MAP_WIDTH { 600 };
34 constexpr int32_t MAX_PIXEL_MAP_HEIGHT { 600 };
35 constexpr int32_t PIXEL_MAP_WIDTH { 300 };
36 constexpr int32_t PIXEL_MAP_HEIGHT { 300 };
37 constexpr int32_t POINTER_ID { 0 };
38 constexpr int32_t DISPLAY_ID { 0 };
39 constexpr int32_t SHADOWINFO_X { 10 };
40 constexpr int32_t SHADOWINFO_Y { 10 };
41 constexpr int32_t DISPLAY_X { 50 };
42 constexpr int32_t DISPLAY_Y { 50 };
43 constexpr int32_t DRAG_NUM_ONE { 1 };
44 constexpr bool HAS_CANCELED_ANIMATION { true };
45 constexpr bool DRAG_WINDOW_VISIBLE { true };
46 constexpr int32_t INT32_BYTE { 4 };
47 constexpr uint32_t DEFAULT_ICON_COLOR { 0xFF };
48 constexpr int64_t START_TIME { 181154000809 };
49 const std::string UD_KEY { "Unified data key" };
50 }
SetUpTestCase()51 void DragDataManagerTest::SetUpTestCase() {}
52
TearDownTestCase()53 void DragDataManagerTest::TearDownTestCase() {}
54
SetUp()55 void DragDataManagerTest::SetUp() {}
56
TearDown()57 void DragDataManagerTest::TearDown() {}
58
CreatePixelMap(int32_t width,int32_t height)59 std::shared_ptr<Media::PixelMap> DragDataManagerTest::CreatePixelMap(int32_t width, int32_t height)
60 {
61 CALL_DEBUG_ENTER;
62 if (width <= 0 || width > MAX_PIXEL_MAP_WIDTH || height <= 0 || height > MAX_PIXEL_MAP_HEIGHT) {
63 FI_HILOGE("Invalid size, height:%{public}d, width:%{public}d}", height, width);
64 return nullptr;
65 }
66 Media::InitializationOptions options;
67 options.size.width = width;
68 options.size.height = height;
69 options.pixelFormat = Media::PixelFormat::BGRA_8888;
70 options.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
71 options.scaleMode = Media::ScaleMode::FIT_TARGET_SIZE;
72
73 int32_t colorLen = width * height;
74 uint32_t *colorPixels = new (std::nothrow) uint32_t[colorLen];
75 if (colorPixels == nullptr) {
76 FI_HILOGE("colorPixels is nullptr");
77 return nullptr;
78 }
79 int32_t colorByteCount = colorLen * INT32_BYTE;
80 auto ret = memset_s(colorPixels, colorByteCount, DEFAULT_ICON_COLOR, colorByteCount);
81 if (ret != EOK) {
82 FI_HILOGE("Call memset_s was a failure");
83 delete[] colorPixels;
84 return nullptr;
85 }
86 std::shared_ptr<Media::PixelMap> pixelMap = Media::PixelMap::Create(colorPixels, colorLen, options);
87 if (pixelMap == nullptr) {
88 FI_HILOGE("Failed to create pixelMap");
89 delete[] colorPixels;
90 return nullptr;
91 }
92 delete[] colorPixels;
93 return pixelMap;
94 }
95
CreateDragData(int32_t sourceType,int32_t pointerId,int32_t dragNum)96 std::optional<DragData> DragDataManagerTest::CreateDragData(int32_t sourceType,
97 int32_t pointerId, int32_t dragNum)
98 {
99 CALL_DEBUG_ENTER;
100 std::shared_ptr<Media::PixelMap> pixelMap = CreatePixelMap(PIXEL_MAP_WIDTH, PIXEL_MAP_HEIGHT);
101 if (pixelMap == nullptr) {
102 FI_HILOGE("Create pixelmap failed");
103 return std::nullopt;
104 }
105
106 DragData dragData;
107 dragData.shadowInfos.push_back({ pixelMap, 0, 0 });
108 dragData.buffer = std::vector<uint8_t>(MAX_BUFFER_SIZE, 0);
109 dragData.udKey = UD_KEY;
110 dragData.sourceType = sourceType;
111 dragData.pointerId = pointerId;
112 dragData.dragNum = dragNum;
113 dragData.displayX = DISPLAY_X;
114 dragData.displayY = DISPLAY_Y;
115 dragData.displayId = DISPLAY_ID;
116 dragData.hasCanceledAnimation = HAS_CANCELED_ANIMATION;
117 return dragData;
118 }
119
120 namespace {
121 /**
122 * @tc.name: DragDataManagerTest001
123 * @tc.desc: test normal SetDragStyle and GetDragStyle in devicestatus
124 * @tc.type: FUNC
125 */
126 HWTEST_F(DragDataManagerTest, DragDataManagerTest001, TestSize.Level0)
127 {
128 CALL_TEST_DEBUG;
129 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::DEFAULT);
130 EXPECT_TRUE(DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::DEFAULT);
131
132 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::FORBIDDEN);
133 EXPECT_TRUE(DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::FORBIDDEN);
134
135 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::COPY);
136 EXPECT_TRUE(DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::COPY);
137
138 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::MOVE);
139 EXPECT_TRUE(DRAG_DATA_MGR.GetDragStyle() == DragCursorStyle::MOVE);
140 }
141
142 /**
143 * @tc.name: DragDataManagerTest002
144 * @tc.desc: test abnormal SetDragStyle and GetDragStyle in devicestatus
145 * @tc.type: FUNC
146 */
147 HWTEST_F(DragDataManagerTest, DragDataManagerTest002, TestSize.Level0)
148 {
149 CALL_TEST_DEBUG;
150 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::DEFAULT);
151 EXPECT_FALSE(DRAG_DATA_MGR.GetDragStyle() != DragCursorStyle::DEFAULT);
152
153 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::FORBIDDEN);
154 EXPECT_FALSE(DRAG_DATA_MGR.GetDragStyle() != DragCursorStyle::FORBIDDEN);
155
156 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::COPY);
157 EXPECT_FALSE(DRAG_DATA_MGR.GetDragStyle() != DragCursorStyle::COPY);
158
159 DRAG_DATA_MGR.SetDragStyle(DragCursorStyle::MOVE);
160 EXPECT_FALSE(DRAG_DATA_MGR.GetDragStyle() != DragCursorStyle::MOVE);
161 }
162
163 /**
164 * @tc.name: DragDataManagerTest003
165 * @tc.desc: test normal get devicestatus data in ipc
166 * @tc.type: FUNC
167 */
168 HWTEST_F(DragDataManagerTest, DragDataManagerTest003, TestSize.Level0)
169 {
170 CALL_TEST_DEBUG;
171 int32_t targetTid = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
172 DRAG_DATA_MGR.SetTargetTid(targetTid);
173 EXPECT_TRUE(targetTid == DRAG_DATA_MGR.GetTargetTid());
174
175 int32_t targetPid = IPCSkeleton::GetCallingPid();
176 DRAG_DATA_MGR.SetTargetPid(targetPid);
177 EXPECT_TRUE(targetPid == DRAG_DATA_MGR.GetTargetPid());
178 }
179
180 /**
181 * @tc.name: DragDataManagerTest004
182 * @tc.desc: test abnormal get devicestatus data in ipc
183 * @tc.type: FUNC
184 */
185 HWTEST_F(DragDataManagerTest, DragDataManagerTest004, TestSize.Level0)
186 {
187 CALL_TEST_DEBUG;
188 int32_t targetTid = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
189 DRAG_DATA_MGR.SetTargetTid(targetTid);
190 EXPECT_FALSE(targetTid != DRAG_DATA_MGR.GetTargetTid());
191
192 int32_t targetPid = IPCSkeleton::GetCallingPid();
193 DRAG_DATA_MGR.SetTargetPid(targetPid);
194 EXPECT_FALSE(targetPid != DRAG_DATA_MGR.GetTargetPid());
195 }
196
197 /**
198 * @tc.name: DragDataManagerTest005
199 * @tc.desc: test get devicestatus drag data
200 * @tc.type: FUNC
201 */
202 HWTEST_F(DragDataManagerTest, DragDataManagerTest005, TestSize.Level0)
203 {
204 CALL_TEST_DEBUG;
205 std::shared_ptr<Media::PixelMap> pixelMap = CreatePixelMap(PIXEL_MAP_WIDTH, PIXEL_MAP_HEIGHT);
206 EXPECT_FALSE(pixelMap == nullptr);
207 DragData dragData;
208 dragData.shadowInfos.push_back({ pixelMap, SHADOWINFO_X, SHADOWINFO_Y });
209 dragData.displayX = DISPLAY_X;
210 dragData.displayY = DISPLAY_Y;
211 DRAG_DATA_MGR.Init(dragData);
212 DragData dragDataFirst = DRAG_DATA_MGR.GetDragData();
213 EXPECT_TRUE(dragDataFirst.displayX == DISPLAY_X);
214 EXPECT_TRUE(dragDataFirst.displayY == DISPLAY_Y);
215 int32_t offsetX = 0;
216 int32_t offsetY = 0;
217 int32_t width = 0;
218 int32_t height = 0;
219 DRAG_DATA_MGR.GetShadowOffset(offsetX, offsetY, width, height);
220 EXPECT_TRUE(offsetX == SHADOWINFO_X);
221 EXPECT_TRUE(offsetY == SHADOWINFO_Y);
222 EXPECT_TRUE(width == PIXEL_MAP_WIDTH);
223 EXPECT_TRUE(height == PIXEL_MAP_HEIGHT);
224 DRAG_DATA_MGR.ResetDragData();
225 DragData dragDataSecond = DRAG_DATA_MGR.GetDragData();
226 EXPECT_TRUE(dragDataSecond.displayX == -1);
227 EXPECT_TRUE(dragDataSecond.displayY == -1);
228 }
229
230 /**
231 * @tc.name: DragDataManagerTest006
232 * @tc.desc: test pixelMap is nullptr
233 * @tc.type: FUNC
234 */
235 HWTEST_F(DragDataManagerTest, DragDataManagerTest006, TestSize.Level0)
236 {
237 CALL_TEST_DEBUG;
238 DragData dragData;
239 dragData.shadowInfos.push_back({ nullptr, SHADOWINFO_X, SHADOWINFO_Y });
240 dragData.displayX = DISPLAY_X;
241 dragData.displayY = DISPLAY_Y;
242 DRAG_DATA_MGR.Init(dragData);
243 int32_t offsetX = 0;
244 int32_t offsetY = 0;
245 int32_t width = 0;
246 int32_t height = 0;
247 auto ret = DRAG_DATA_MGR.GetShadowOffset(offsetX, offsetY, width, height);
248 EXPECT_TRUE(ret == -1);
249 }
250
251 /**
252 * @tc.name: DragDataManagerTest007
253 * @tc.desc: abnormal test DragDrawing initialization
254 * @tc.type: FUNC
255 */
256 HWTEST_F(DragDataManagerTest, DragDataManagerTest007, TestSize.Level0)
257 {
258 CALL_TEST_DEBUG;
259 std::optional<DragData> dragData = CreateDragData(
260 MMI::PointerEvent::SOURCE_TYPE_TOUCHPAD, POINTER_ID, DRAG_NUM_ONE);
261 ASSERT_FALSE(dragData == std::nullopt);
262 DragDrawing dragDrawing;
263 dragDrawing.UpdateDragWindowState(DRAG_WINDOW_VISIBLE);
264 dragDrawing.InitDrawingInfo(dragData.value());
265 int32_t ret = dragDrawing.Init(dragData.value());
266 EXPECT_EQ(ret, INIT_CANCEL);
267 dragDrawing.UpdateDrawingState();
268 ret = dragDrawing.Init(dragData.value());
269 EXPECT_EQ(ret, INIT_FAIL);
270 dragDrawing.DestroyDragWindow();
271 EXPECT_EQ(dragDrawing.startNum_, START_TIME);
272 dragDrawing.UpdateDragWindowState(!DRAG_WINDOW_VISIBLE);
273 }
274
275 /**
276 * @tc.name: DragDataManagerTest008
277 * @tc.desc: abnormal test DragDrawing initialization
278 * @tc.type: FUNC
279 */
280 HWTEST_F(DragDataManagerTest, DragDataManagerTest008, TestSize.Level0)
281 {
282 CALL_TEST_DEBUG;
283 std::optional<DragData> dragData = CreateDragData(
284 MMI::PointerEvent::SOURCE_TYPE_TOUCHPAD, POINTER_ID, DRAG_NUM_ONE);
285 ASSERT_FALSE(dragData == std::nullopt);
286 DragDrawing dragDrawing;
287 dragDrawing.UpdateDragWindowState(DRAG_WINDOW_VISIBLE);
288 int32_t ret = dragDrawing.Init(dragData.value());
289 EXPECT_EQ(ret, INIT_FAIL);
290 dragDrawing.DestroyDragWindow();
291 EXPECT_EQ(dragDrawing.startNum_, START_TIME);
292 dragDrawing.UpdateDragWindowState(!DRAG_WINDOW_VISIBLE);
293 }
294
295 /**
296 * @tc.name: DragDataManagerTest009
297 * @tc.desc: normal test DragDrawing initialization
298 * @tc.type: FUNC
299 */
300 HWTEST_F(DragDataManagerTest, DragDataManagerTest009, TestSize.Level0)
301 {
302 CALL_TEST_DEBUG;
303 std::optional<DragData> dragData = CreateDragData(
304 MMI::PointerEvent::SOURCE_TYPE_MOUSE, POINTER_ID, DRAG_NUM_ONE);
305 ASSERT_FALSE(dragData == std::nullopt);
306 DragDrawing dragDrawing;
307 int32_t ret = dragDrawing.Init(dragData.value());
308 dragDrawing.UpdateDragWindowState(DRAG_WINDOW_VISIBLE);
309 EXPECT_EQ(ret, INIT_SUCCESS);
310 dragDrawing.DestroyDragWindow();
311 EXPECT_EQ(dragDrawing.startNum_, START_TIME);
312 dragDrawing.EraseMouseIcon();
313 dragDrawing.UpdateDragWindowState(!DRAG_WINDOW_VISIBLE);
314 }
315
316 /**
317 * @tc.name: DragDataManagerTest010
318 * @tc.desc: normal test DragDrawing initialization
319 * @tc.type: FUNC
320 */
321 HWTEST_F(DragDataManagerTest, DragDataManagerTest010, TestSize.Level0)
322 {
323 CALL_TEST_DEBUG;
324 std::optional<DragData> dragData = CreateDragData(
325 MMI::PointerEvent::SOURCE_TYPE_MOUSE, POINTER_ID, DRAG_NUM_ONE);
326 ASSERT_FALSE(dragData == std::nullopt);
327 DragDrawing dragDrawing;
328 int32_t ret = dragDrawing.Init(dragData.value());
329 dragDrawing.UpdateDragWindowState(DRAG_WINDOW_VISIBLE);
330 EXPECT_EQ(ret, INIT_CANCEL);
331 dragDrawing.DestroyDragWindow();
332 EXPECT_EQ(dragDrawing.startNum_, START_TIME);
333 dragDrawing.UpdateDragWindowState(!DRAG_WINDOW_VISIBLE);
334 }
335
336 /**
337 * @tc.name: DragDataManagerTest011
338 * @tc.desc: normal test DragDrawing drawing
339 * @tc.type: FUNC
340 */
341 HWTEST_F(DragDataManagerTest, DragDataManagerTest011, TestSize.Level0)
342 {
343 CALL_TEST_DEBUG;
344 auto pointerEvent = MMI::PointerEvent::Create();
345 EXPECT_FALSE(pointerEvent == nullptr);
346 MMI::PointerEvent::PointerItem pointerItem;
347 pointerEvent->GetPointerItem(pointerEvent->GetPointerId(), pointerItem);
348 FI_HILOGD("SourceType:%{public}d, pointerId:%{public}d, displayX:%{public}d, displayY:%{public}d",
349 pointerEvent->GetSourceType(), pointerEvent->GetPointerId(),
350 pointerItem.GetDisplayX(), pointerItem.GetDisplayY());
351 EXPECT_LT(pointerEvent->GetTargetDisplayId(), 0);
352 DragDrawing dragDrawing;
353 dragDrawing.Draw(pointerEvent->GetTargetDisplayId(), pointerItem.GetDisplayX(), pointerItem.GetDisplayY());
354 dragDrawing.DestroyDragWindow();
355 EXPECT_EQ(dragDrawing.startNum_, START_TIME);
356 }
357 } // namespace
358 } // namespace DeviceStatus
359 } // namespace Msdp
360 } // namespace OHOS