• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <semaphore.h>
16 #include <random>
17 #include <gtest/gtest.h>
18 
19 #include "pixel_map.h"
20 
21 #include "mmi_log.h"
22 #include "input_manager.h"
23 #include "system_info.h"
24 
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "UpdateDisplayInfoTest"
27 
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 using namespace testing::ext;
32 constexpr int32_t TIME_MS = 20;
33 constexpr int32_t DISPLAYINFO_WIDTH = 2;
34 constexpr int32_t DISPLAYINFO_HEIGHT = 2;
35 constexpr int32_t DISPLAYINFO_DPI = 240;
36 } // namespace
37 
38 class InputManagerUpdateDisplayInfoTest : public testing::Test {
39 public:
SetUpTestCase(void)40     static void SetUpTestCase(void) {}
TearDownTestCase(void)41     static void TearDownTestCase(void) {}
42     std::shared_ptr<OHOS::Media::PixelMap> MatrixToPixelmap(const std::vector<std::vector<uint32_t>> &windowMask) const;
43     std::vector<std::vector<uint32_t>> CreateMatrix(int32_t width, int32_t height) const;
44     void CheckMaskDisplayPoint(OHOS::Media::PixelMap *pixelMap, int32_t displayX, int32_t displayY) const;
45     DisplayInfo CreateDisplayInfo(int32_t id) const;
46 };
47 
MatrixToPixelmap(const std::vector<std::vector<uint32_t>> & windowMask) const48 std::shared_ptr<OHOS::Media::PixelMap> InputManagerUpdateDisplayInfoTest::MatrixToPixelmap(
49     const std::vector<std::vector<uint32_t>> &windowMask) const
50 {
51     MMI_HILOGD("MatrixToPixelmap--enter");
52     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
53     uint32_t maskHeight = windowMask.size();
54     if (maskHeight < 1) {
55         MMI_HILOGE("Failed to create a pixelMap, err maskHeight %{public}d <1", maskHeight);
56         return pixelMap;
57     }
58     uint32_t maskWidth = windowMask[0].size();
59     if (maskHeight < 1) {
60         MMI_HILOGE("Failed to create a pixelMap, err maskWidth %{public}d <1", maskWidth);
61         return pixelMap;
62     }
63     OHOS::Media::InitializationOptions ops;
64     ops.size.width = maskWidth;
65     ops.size.height = maskHeight;
66     ops.pixelFormat = OHOS::Media::PixelFormat::RGBA_8888;
67     ops.alphaType = OHOS::Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE;
68     ops.scaleMode = OHOS::Media::ScaleMode::FIT_TARGET_SIZE;
69     uint32_t length = maskHeight * maskWidth;
70     uint32_t *data = new (std::nothrow) uint32_t[length];
71     CHKPP(data);
72     for (uint32_t i = 0; i < maskHeight; i++) {
73         if (windowMask[i].size() != maskWidth) {
74             MMI_HILOGE(
75                 "Failed to create a pixelMap,row:%{public}d curSize:%{public}zu not equal frist row size:%{public}d",
76                 i, windowMask[i].size(), maskWidth);
77             return nullptr;
78         }
79         for (uint32_t j = 0; j < maskWidth; j++) {
80             uint32_t idx = i * maskWidth + j;
81             data[idx] = windowMask[i][j];
82         }
83     }
84     pixelMap = OHOS::Media::PixelMap::Create(data, length, ops);
85     delete[] data;
86     data = nullptr;
87     CHKPP(pixelMap);
88     MMI_HILOGD("MatrixToPixelmap--end");
89     return pixelMap;
90 }
91 
CreateMatrix(int32_t width,int32_t height) const92 std::vector<std::vector<uint32_t>> InputManagerUpdateDisplayInfoTest::CreateMatrix(int32_t width, int32_t height) const
93 {
94     MMI_HILOGD("CreateMatrix--enter");
95     if (width < 1) {
96         MMI_HILOGE("Failed to create a Matrix, err width %{public}d <1", width);
97         return std::vector<std::vector<uint32_t>>();
98     }
99     if (height < 1) {
100         MMI_HILOGE("Failed to create a Matrix, err height %{public}d <1", height);
101         return std::vector<std::vector<uint32_t>>();
102     }
103     std::vector<std::vector<uint32_t>> matrix(height, std::vector<uint32_t>(width, 0));
104     std::random_device rd;
105     std::mt19937 gen(rd());
106     std::uniform_int_distribution<int> dist(0, 1);
107     for (int32_t i = 0; i < height; i++) {
108         for (int32_t j = 0; j < width; j++) {
109             matrix[i][j] = dist(gen);
110         }
111     }
112     MMI_HILOGD("CreateMatrix--end");
113     return matrix;
114 }
115 
CheckMaskDisplayPoint(OHOS::Media::PixelMap * pixelMap,int32_t displayX,int32_t displayY) const116 void InputManagerUpdateDisplayInfoTest::CheckMaskDisplayPoint(OHOS::Media::PixelMap *pixelMap, int32_t displayX,
117     int32_t displayY) const
118 {
119     CHKPV(pixelMap);
120     uint32_t dst = 0;
121     OHOS::Media::Position pos { displayX, displayY };
122     pixelMap->ReadPixel(pos, dst);
123     if (dst == 0) {
124         MMI_HILOGI("It's transparent area, dst:%{public}d", dst);
125     } else {
126         MMI_HILOGI("It's non-transparent area, dst:%{public}d", dst);
127     }
128 }
129 
CreateDisplayInfo(int32_t id) const130 DisplayInfo InputManagerUpdateDisplayInfoTest::CreateDisplayInfo(int32_t id) const
131 {
132     DisplayInfo displayinfo;
133     displayinfo.id = id;
134     displayinfo.x = 1;
135     displayinfo.y = 1;
136     displayinfo.width = DISPLAYINFO_WIDTH;
137     displayinfo.height = DISPLAYINFO_HEIGHT;
138     displayinfo.dpi = DISPLAYINFO_DPI;
139     displayinfo.name = "pp";
140     displayinfo.direction = DIRECTION0;
141     return displayinfo;
142 }
143 
144 /**
145  * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_001
146  * @tc.desc: Update shaped window information for 1 display and 1 window
147  * @tc.type: FUNC
148  * @tc.require:
149  */
150 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_001, TestSize.Level1)
151 {
152     CALL_TEST_DEBUG;
153     std::vector<std::vector<uint32_t>> maskMatrix = { { 1, 1, 1, 1, 1, 1 }, { 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0 },
154         { 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
155     std::shared_ptr<OHOS::Media::PixelMap> pixelMap = MatrixToPixelmap(maskMatrix);
156     ASSERT_NE(pixelMap, nullptr);
157     DisplayGroupInfo displayGroupInfo;
158     displayGroupInfo.focusWindowId = 1;
159     int32_t dgw = 1000;
160     int32_t dgh = 2000;
161     DisplayInfo displayinfo = CreateDisplayInfo(0);
162     displayGroupInfo.displaysInfo.push_back(displayinfo);
163     WindowInfo info;
164     info.agentWindowId = 1;
165     info.id = 1;
166     info.pid = 1;
167     info.uid = 1;
168     info.area = { 1, 1, 1, 1 };
169     info.defaultHotAreas = { info.area };
170     info.pointerHotAreas = { info.area };
171     info.pointerChangeAreas = { 16, 5, 16, 5, 16, 5, 16, 5 };
172     info.transform = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
173     info.agentWindowId = 1;
174     info.flags = 0;
175     info.displayId = 0;
176     info.pixelMap = pixelMap.get();
177     displayGroupInfo.windowsInfo.push_back(info);
178 
179     UserScreenInfo userScreenInfo;
180     ScreenInfo screenInfo;
181     screenInfo.screenType =(ScreenType)info.windowType;
182     screenInfo.dpi = displayinfo.dpi;
183     screenInfo.height = info.area.height;
184     screenInfo.width = info.area.width;
185     screenInfo.physicalWidth = dgw;
186     screenInfo.physicalHeight = dgh;
187     screenInfo.id = displayinfo.id;
188     screenInfo.rotation = Rotation::ROTATION_0;
189     screenInfo.tpDirection = Direction::DIRECTION0;
190     screenInfo.uniqueId = displayinfo.name;
191     userScreenInfo.screens.push_back(screenInfo);
192     userScreenInfo.displayGroups.push_back(displayGroupInfo);
193     ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(userScreenInfo));
194 }
195 
196 /**
197  * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_002
198  * @tc.desc: Update shaped window information, for 1 display and 1 window
199  * @tc.type: FUNC
200  * @tc.require:
201  */
202 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_002, TestSize.Level1)
203 {
204     CALL_TEST_DEBUG;
205     int32_t height = 10;
206     ASSERT_GE(height, 1);
207     int32_t width = 20;
208     ASSERT_GE(width, 1);
209     std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
210     pixelMap = MatrixToPixelmap(CreateMatrix(width, height));
211     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_MS));
212     ASSERT_NE(pixelMap, nullptr);
213     DisplayGroupInfo displayGroupInfo;
214     displayGroupInfo.focusWindowId = 1;
215     int32_t dgw = 1000;
216     int32_t dgh = 2000;
217     DisplayInfo displayinfo = CreateDisplayInfo(0);
218     displayGroupInfo.displaysInfo.push_back(displayinfo);
219     WindowInfo info;
220     info.id = 1;
221     info.pid = 1;
222     info.uid = 1;
223     info.area = { 1, 1, 1, 1 };
224     info.defaultHotAreas = { info.area };
225     info.pointerHotAreas = { info.area };
226     info.pointerChangeAreas = { 16, 5, 16, 5, 16, 5, 16, 5 };
227     info.transform = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
228     info.agentWindowId = 1;
229     info.flags = 0;
230     info.displayId = 0;
231     info.pixelMap = pixelMap.get();
232     displayGroupInfo.windowsInfo.push_back(info);
233     int32_t displayX = width / 2;
234     int32_t displayY = height / 2;
235     CheckMaskDisplayPoint(pixelMap.get(), displayX, displayY);
236 
237     UserScreenInfo userScreenInfo;
238     ScreenInfo screenInfo;
239     screenInfo.screenType =(ScreenType)info.windowType;
240     screenInfo.dpi = displayinfo.dpi;
241     screenInfo.height = info.area.height;
242     screenInfo.width = info.area.width;
243     screenInfo.physicalWidth = dgw;
244     screenInfo.physicalHeight = dgh;
245     screenInfo.id = displayinfo.id;
246     screenInfo.rotation = Rotation::ROTATION_0;
247     screenInfo.tpDirection = Direction::DIRECTION0;
248     screenInfo.uniqueId = displayinfo.name;
249     userScreenInfo.screens.push_back(screenInfo);
250     userScreenInfo.displayGroups.push_back(displayGroupInfo);
251 
252     ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(userScreenInfo));
253 }
254 
255 /**
256  * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_003
257  * @tc.desc: Update shaped window information, for 1 display and max-windows
258  * @tc.type: FUNC
259  * @tc.require:
260  */
261 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_003, TestSize.Level1)
262 {
263     CALL_TEST_DEBUG;
264     DisplayGroupInfo displayGroupInfo;
265     displayGroupInfo.focusWindowId = 0;
266     int32_t dgw = 1000;
267     int32_t dgh = 2000;
268     DisplayInfo displayinfo = CreateDisplayInfo(0);
269     displayinfo.direction = DIRECTION0;
270     displayinfo.displayMode = DisplayMode::FULL;
271     displayGroupInfo.displaysInfo.push_back(displayinfo);
272     std::vector<std::vector<int32_t>> maskSizeMatrix = { { 100, 300 }, { 30, 30 }, { 50, 50 }, { 80, 80 }, { 10, 8 },
273         { 5, 20 }, { 6, 10 }, { 30, 30 }, { 20, 30 }, { 40, 10 } };
274     std::vector<std::shared_ptr<OHOS::Media::PixelMap>> vecPixelMap;
275     std::vector<ScreenInfo> screenInfos;
276     for (uint32_t i = 0; i < maskSizeMatrix.size(); i++) {
277         ASSERT_EQ(maskSizeMatrix[i].size(), 2);
278         ASSERT_GE(maskSizeMatrix[i][0], 2);
279         ASSERT_GE(maskSizeMatrix[i][1], 2);
280         MMI_HILOGD("Begin=========> i=%{public}d; width=%{public}d height=%{public}d", i, maskSizeMatrix[i][0],
281             maskSizeMatrix[i][1]);
282         WindowInfo info;
283         info.id = i + 1;
284         info.pid = 1;
285         info.uid = 1;
286         info.area = { 1, 1, 1, 1 };
287         info.defaultHotAreas = { info.area };
288         info.pointerHotAreas = { info.area };
289         info.pointerChangeAreas = { 16, 5, 16, 5, 16, 5, 16, 5 };
290         info.transform = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
291         info.agentWindowId = 1;
292         info.flags = 0;
293         info.displayId = 0;
294         info.zOrder = static_cast<float>(maskSizeMatrix.size() - i);
295         std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
296         MMI_HILOGD("CreateMatrix begin ");
297         auto maskMatrix = CreateMatrix(maskSizeMatrix[i][1], maskSizeMatrix[i][0]);
298         MMI_HILOGD("CreateMatrix end ");
299         pixelMap = MatrixToPixelmap(maskMatrix);
300         MMI_HILOGD("MatrixToPixelmap end ");
301         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_MS));
302         ASSERT_NE(pixelMap, nullptr);
303         CHKPV(pixelMap);
304         vecPixelMap.push_back(pixelMap);
305         info.pixelMap = pixelMap.get();
306         displayGroupInfo.windowsInfo.push_back(info);
307         int32_t displayX = maskSizeMatrix[i][0] / 2;
308         int32_t displayY = maskSizeMatrix[i][1] / 2;
309         CheckMaskDisplayPoint(pixelMap.get(), displayX, displayY);
310         ScreenInfo screenInfo;
311         screenInfo.screenType =(ScreenType)info.windowType;
312         screenInfo.dpi = displayinfo.dpi;
313         screenInfo.height = info.area.height;
314         screenInfo.width = info.area.width;
315         screenInfo.physicalWidth = dgw;
316         screenInfo.physicalHeight = dgh;
317         screenInfo.id = displayinfo.id;
318         screenInfo.rotation = Rotation::ROTATION_0;
319         screenInfo.tpDirection = Direction::DIRECTION0;
320         screenInfo.uniqueId = displayinfo.name;
321         screenInfos.push_back(screenInfo);
322     }
323     UserScreenInfo userScreenInfo;
324     userScreenInfo.displayGroups.push_back(displayGroupInfo);
325     userScreenInfo.screens = screenInfos;
326     ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(userScreenInfo));
327 }
328 
329 /**
330  * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_004
331  * @tc.desc: Update shaped window information, for 1 display and 1 window
332  * @tc.type: FUNC
333  * @tc.require:
334  */
335 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_004, TestSize.Level1)
336 {
337     CALL_TEST_DEBUG;
338     DisplayGroupInfo displayGroupInfo;
339     displayGroupInfo.focusWindowId = 1;
340     int32_t dgw = 1000;
341     int32_t dgh = 2000;
342     std::vector<std::shared_ptr<OHOS::Media::PixelMap>> vecPixelMap;
343     std::vector<ScreenInfo> screenInfos;
344     std::vector<std::vector<int32_t>> maskSizeMatrix = { { 800, 600 }, { 1024, 1024 } };
345     for (uint32_t i = 0; i < maskSizeMatrix.size(); i++) {
346         DisplayInfo displayinfo = CreateDisplayInfo(i);
347         displayGroupInfo.displaysInfo.push_back(displayinfo);
348     }
349     for (uint32_t i = 0; i < maskSizeMatrix.size(); i++) { // 2 widonw for 2 display
350         ASSERT_EQ(maskSizeMatrix[i].size(), 2);
351         ASSERT_GE(maskSizeMatrix[i][0], 2);
352         ASSERT_GE(maskSizeMatrix[i][1], 2);
353         WindowInfo info;
354         info.id = 1;
355         info.pid = 1;
356         info.uid = 1;
357         info.area = { 1, 1, 1, 1 };
358         info.defaultHotAreas = { info.area };
359         info.agentWindowId = 1;
360         info.flags = 0;
361         info.displayId = i;
362         std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
363         auto maskMatrix = CreateMatrix(maskSizeMatrix[i][1], maskSizeMatrix[i][0]);
364         pixelMap = MatrixToPixelmap(maskMatrix);
365         std::this_thread::sleep_for(std::chrono::milliseconds(TIME_MS));
366         ASSERT_NE(pixelMap, nullptr);
367         CHKPV(pixelMap);
368         vecPixelMap.push_back(pixelMap);
369         info.pixelMap = pixelMap.get();
370         displayGroupInfo.windowsInfo.push_back(info);
371         int32_t displayX = maskSizeMatrix[i][1] / 2;
372         int32_t displayY = maskSizeMatrix[i][0] / 2;
373         CheckMaskDisplayPoint(pixelMap.get(), displayX, displayY);
374 
375         ScreenInfo screenInfo;
376         screenInfo.screenType =(ScreenType)info.windowType;
377         screenInfo.dpi = displayGroupInfo.displaysInfo[i].dpi;
378         screenInfo.height = displayGroupInfo.displaysInfo[i].height;
379         screenInfo.width = displayGroupInfo.displaysInfo[i].width;
380         screenInfo.physicalWidth = dgw;
381         screenInfo.physicalHeight = dgh;
382         screenInfo.id = displayGroupInfo.displaysInfo[i].id;
383         screenInfo.rotation = Rotation::ROTATION_0;
384         screenInfo.tpDirection = Direction::DIRECTION0;
385         screenInfo.uniqueId = displayGroupInfo.displaysInfo[i].name;
386         screenInfos.push_back(screenInfo);
387     }
388 
389     UserScreenInfo userScreenInfo;
390     userScreenInfo.displayGroups.push_back(displayGroupInfo);
391     userScreenInfo.screens = screenInfos;
392     InputManager::GetInstance()->UpdateDisplayInfo(userScreenInfo);
393     ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(userScreenInfo));
394 }
395 } // namespace MMI
396 } // namespace OHOS
397