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("Filed 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("Filed 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 "Filed 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("Filed 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("Filed 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.uniq = "pp";
141 displayinfo.direction = DIRECTION0;
142 return displayinfo;
143 }
144
145 /**
146 * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_001
147 * @tc.desc: Update shaped window information for 1 display and 1 window
148 * @tc.type: FUNC
149 * @tc.require:
150 */
151 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_001, TestSize.Level1)
152 {
153 CALL_TEST_DEBUG;
154 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 },
155 { 0, 0, 1, 1, 0, 0 }, { 0, 0, 1, 1, 0, 0 }, { 0, 0, 0, 0, 0, 0 } };
156 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = MatrixToPixelmap(maskMatrix);
157 ASSERT_NE(pixelMap, nullptr);
158 DisplayGroupInfo displayGroupInfo;
159 displayGroupInfo.focusWindowId = 1;
160 displayGroupInfo.width = 1000;
161 displayGroupInfo.height = 2000;
162 DisplayInfo displayinfo = CreateDisplayInfo(0);
163 displayGroupInfo.displaysInfo.push_back(displayinfo);
164 WindowInfo info;
165 info.agentWindowId = 1;
166 info.id = 1;
167 info.pid = 1;
168 info.uid = 1;
169 info.area = { 1, 1, 1, 1 };
170 info.defaultHotAreas = { info.area };
171 info.pointerHotAreas = { info.area };
172 info.pointerChangeAreas = { 16, 5, 16, 5, 16, 5, 16, 5 };
173 info.transform = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
174 info.agentWindowId = 1;
175 info.flags = 0;
176 info.displayId = 0;
177 info.pixelMap = pixelMap.get();
178 displayGroupInfo.windowsInfo.push_back(info);
179 ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo));
180 }
181
182 /**
183 * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_002
184 * @tc.desc: Update shaped window information, for 1 display and 1 window
185 * @tc.type: FUNC
186 * @tc.require:
187 */
188 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_002, TestSize.Level1)
189 {
190 CALL_TEST_DEBUG;
191 int32_t height = 10;
192 ASSERT_GE(height, 1);
193 int32_t width = 20;
194 ASSERT_GE(width, 1);
195 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
196 pixelMap = MatrixToPixelmap(CreateMatrix(width, height));
197 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_MS));
198 ASSERT_NE(pixelMap, nullptr);
199 DisplayGroupInfo displayGroupInfo;
200 displayGroupInfo.focusWindowId = 1;
201 displayGroupInfo.width = 1000;
202 displayGroupInfo.height = 2000;
203 DisplayInfo displayinfo = CreateDisplayInfo(0);
204 displayGroupInfo.displaysInfo.push_back(displayinfo);
205 WindowInfo info;
206 info.id = 1;
207 info.pid = 1;
208 info.uid = 1;
209 info.area = { 1, 1, 1, 1 };
210 info.defaultHotAreas = { info.area };
211 info.pointerHotAreas = { info.area };
212 info.pointerChangeAreas = { 16, 5, 16, 5, 16, 5, 16, 5 };
213 info.transform = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
214 info.agentWindowId = 1;
215 info.flags = 0;
216 info.displayId = 0;
217 info.pixelMap = pixelMap.get();
218 displayGroupInfo.windowsInfo.push_back(info);
219 int32_t displayX = width / 2;
220 int32_t displayY = height / 2;
221 CheckMaskDisplayPoint(pixelMap.get(), displayX, displayY);
222 ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo));
223 }
224
225 /**
226 * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_003
227 * @tc.desc: Update shaped window information, for 1 display and max-windows
228 * @tc.type: FUNC
229 * @tc.require:
230 */
231 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_003, TestSize.Level1)
232 {
233 CALL_TEST_DEBUG;
234 DisplayGroupInfo displayGroupInfo;
235 displayGroupInfo.focusWindowId = 0;
236 displayGroupInfo.width = 1000;
237 displayGroupInfo.height = 2000;
238 DisplayInfo displayinfo = CreateDisplayInfo(0);
239 displayinfo.direction = DIRECTION0;
240 displayinfo.displayMode = DisplayMode::FULL;
241 displayGroupInfo.displaysInfo.push_back(displayinfo);
242 std::vector<std::vector<int32_t>> maskSizeMatrix = { { 100, 300 }, { 30, 30 }, { 50, 50 }, { 80, 80 }, { 10, 8 },
243 { 5, 20 }, { 6, 10 }, { 30, 30 }, { 20, 30 }, { 40, 10 } };
244 std::vector<std::shared_ptr<OHOS::Media::PixelMap>> vecPixelMap;
245 for (uint32_t i = 0; i < maskSizeMatrix.size(); i++) {
246 ASSERT_EQ(maskSizeMatrix[i].size(), 2);
247 ASSERT_GE(maskSizeMatrix[i][0], 2);
248 ASSERT_GE(maskSizeMatrix[i][1], 2);
249 MMI_HILOGD("Begin=========> i=%{public}d; width=%{public}d height=%{public}d", i, maskSizeMatrix[i][0],
250 maskSizeMatrix[i][1]);
251 WindowInfo info;
252 info.id = i + 1;
253 info.pid = 1;
254 info.uid = 1;
255 info.area = { 1, 1, 1, 1 };
256 info.defaultHotAreas = { info.area };
257 info.pointerHotAreas = { info.area };
258 info.pointerChangeAreas = { 16, 5, 16, 5, 16, 5, 16, 5 };
259 info.transform = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f };
260 info.agentWindowId = 1;
261 info.flags = 0;
262 info.displayId = 0;
263 info.zOrder = static_cast<float>(maskSizeMatrix.size() - i);
264 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
265 MMI_HILOGD("CreateMatrix begin ");
266 auto maskMatrix = CreateMatrix(maskSizeMatrix[i][1], maskSizeMatrix[i][0]);
267 MMI_HILOGD("CreateMatrix end ");
268 pixelMap = MatrixToPixelmap(maskMatrix);
269 MMI_HILOGD("MatrixToPixelmap end ");
270 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_MS));
271 ASSERT_NE(pixelMap, nullptr);
272 CHKPV(pixelMap);
273 vecPixelMap.push_back(pixelMap);
274 info.pixelMap = pixelMap.get();
275 displayGroupInfo.windowsInfo.push_back(info);
276 int32_t displayX = maskSizeMatrix[i][0] / 2;
277 int32_t displayY = maskSizeMatrix[i][1] / 2;
278 CheckMaskDisplayPoint(pixelMap.get(), displayX, displayY);
279 }
280 ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo));
281 }
282
283 /**
284 * @tc.name: InputManagerTest_UpdateDisplayInfoShaped_004
285 * @tc.desc: Update shaped window information, for 1 display and 1 window
286 * @tc.type: FUNC
287 * @tc.require:
288 */
289 HWTEST_F(InputManagerUpdateDisplayInfoTest, InputManagerTest_UpdateDisplayInfoShaped_004, TestSize.Level1)
290 {
291 CALL_TEST_DEBUG;
292 DisplayGroupInfo displayGroupInfo;
293 displayGroupInfo.focusWindowId = 1;
294 displayGroupInfo.width = 1000;
295 displayGroupInfo.height = 2000;
296 std::vector<std::shared_ptr<OHOS::Media::PixelMap>> vecPixelMap;
297 std::vector<std::vector<int32_t>> maskSizeMatrix = { { 800, 600 }, { 1024, 1024 } };
298 for (uint32_t i = 0; i < maskSizeMatrix.size(); i++) {
299 DisplayInfo displayinfo = CreateDisplayInfo(i);
300 displayGroupInfo.displaysInfo.push_back(displayinfo);
301 }
302 for (uint32_t i = 0; i < maskSizeMatrix.size(); i++) { // 2 widonw for 2 display
303 ASSERT_EQ(maskSizeMatrix[i].size(), 2);
304 ASSERT_GE(maskSizeMatrix[i][0], 2);
305 ASSERT_GE(maskSizeMatrix[i][1], 2);
306 WindowInfo info;
307 info.id = 1;
308 info.pid = 1;
309 info.uid = 1;
310 info.area = { 1, 1, 1, 1 };
311 info.defaultHotAreas = { info.area };
312 info.agentWindowId = 1;
313 info.flags = 0;
314 info.displayId = i;
315 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
316 auto maskMatrix = CreateMatrix(maskSizeMatrix[i][1], maskSizeMatrix[i][0]);
317 pixelMap = MatrixToPixelmap(maskMatrix);
318 std::this_thread::sleep_for(std::chrono::milliseconds(TIME_MS));
319 ASSERT_NE(pixelMap, nullptr);
320 CHKPV(pixelMap);
321 vecPixelMap.push_back(pixelMap);
322 info.pixelMap = pixelMap.get();
323 displayGroupInfo.windowsInfo.push_back(info);
324 int32_t displayX = maskSizeMatrix[i][1] / 2;
325 int32_t displayY = maskSizeMatrix[i][0] / 2;
326 CheckMaskDisplayPoint(pixelMap.get(), displayX, displayY);
327 }
328 ASSERT_NO_FATAL_FAILURE(InputManager::GetInstance()->UpdateDisplayInfo(displayGroupInfo));
329 }
330 } // namespace MMI
331 } // namespace OHOS
332