1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <fstream>
17 #include <cJSON.h>
18 #include <gtest/gtest.h>
19
20 #include "config_policy_utils.h"
21 #include "define_multimodal.h"
22 #include "multimodal_event_handler.h"
23
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "MultimodalEventHandlerTest"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 char g_cfgName[] { "custom_input_product_config.json" };
31 }
32 using namespace testing::ext;
33 using namespace testing;
34
35 class MultimodalEventHandlerTest : public testing::Test {
36 public:
SetUpTestCase()37 static void SetUpTestCase() {}
TearDownTestCase()38 static void TearDownTestCase() {}
39
40 private:
41 void SerializeInputProductConfig(cJSON *jsonProductConfig);
42 template<typename T>
43 void BuildInputProductConfig(T maxTouchPoints);
44 void BuildInputProductConfig3();
45 void BuildInputProductConfig6();
46 void BuildInputProductConfig7();
47 void BuildInputProductConfig8();
48 };
49
SerializeInputProductConfig(cJSON * jsonProductConfig)50 void MultimodalEventHandlerTest::SerializeInputProductConfig(cJSON *jsonProductConfig)
51 {
52 CHKPV(jsonProductConfig);
53 auto sProductConfig = std::unique_ptr<char, std::function<void(char *)>>(
54 cJSON_Print(jsonProductConfig),
55 [](char *object) {
56 if (object != nullptr) {
57 cJSON_free(object);
58 }
59 });
60 std::ofstream ofs(g_cfgName, std::ios_base::out);
61 if (ofs.is_open()) {
62 ofs << sProductConfig.get();
63 ofs.flush();
64 ofs.close();
65 }
66 }
67
68 template<typename T>
BuildInputProductConfig(T maxTouchPoints)69 void MultimodalEventHandlerTest::BuildInputProductConfig(T maxTouchPoints)
70 {
71 auto jsonProductConfig = std::unique_ptr<cJSON, std::function<void(cJSON *)>>(
72 cJSON_CreateObject(),
73 [](cJSON *object) {
74 if (object != nullptr) {
75 cJSON_Delete(object);
76 }
77 });
78 CHKPV(jsonProductConfig);
79 auto jsonTouchscreen = cJSON_CreateObject();
80 CHKPV(jsonTouchscreen);
81 if (!cJSON_AddItemToObject(jsonProductConfig.get(), "touchscreen", jsonTouchscreen)) {
82 cJSON_Delete(jsonTouchscreen);
83 return;
84 }
85 cJSON *jsonMaxTouchPoints = nullptr;
86
87 if constexpr(std::is_integral_v<T> || std::is_floating_point_v<T>) {
88 jsonMaxTouchPoints = cJSON_CreateNumber(maxTouchPoints);
89 } else if (std::is_same_v<std::remove_const_t<T>, char*>) {
90 jsonMaxTouchPoints = cJSON_CreateRaw(maxTouchPoints);
91 }
92 CHKPV(jsonMaxTouchPoints);
93 if (!cJSON_AddItemToObject(jsonTouchscreen, "MaxTouchPoints", jsonMaxTouchPoints)) {
94 cJSON_Delete(jsonMaxTouchPoints);
95 return;
96 }
97 SerializeInputProductConfig(jsonProductConfig.get());
98 }
99
BuildInputProductConfig3()100 void MultimodalEventHandlerTest::BuildInputProductConfig3()
101 {
102 const std::ofstream::pos_type tailPos { 4096 };
103 std::ofstream ofs(g_cfgName, std::ios_base::out);
104 if (ofs.is_open()) {
105 ofs.seekp(tailPos);
106 ofs << "tail";
107 ofs.flush();
108 ofs.close();
109 }
110 }
111
BuildInputProductConfig6()112 void MultimodalEventHandlerTest::BuildInputProductConfig6()
113 {
114 int32_t maxTouchPoints { 9 };
115 auto jsonMaxTouchPoints = std::unique_ptr<cJSON, std::function<void(cJSON *)>>(
116 cJSON_CreateNumber(maxTouchPoints),
117 [](cJSON *object) {
118 if (object != nullptr) {
119 cJSON_Delete(object);
120 }
121 });
122 SerializeInputProductConfig(jsonMaxTouchPoints.get());
123 }
124
BuildInputProductConfig7()125 void MultimodalEventHandlerTest::BuildInputProductConfig7()
126 {
127 auto jsonProductConfig = std::unique_ptr<cJSON, std::function<void(cJSON *)>>(
128 cJSON_CreateObject(),
129 [](cJSON *object) {
130 if (object != nullptr) {
131 cJSON_Delete(object);
132 }
133 });
134 SerializeInputProductConfig(jsonProductConfig.get());
135 }
136
BuildInputProductConfig8()137 void MultimodalEventHandlerTest::BuildInputProductConfig8()
138 {
139 auto jsonProductConfig = std::unique_ptr<cJSON, std::function<void(cJSON *)>>(
140 cJSON_CreateObject(),
141 [](cJSON *object) {
142 if (object != nullptr) {
143 cJSON_Delete(object);
144 }
145 });
146 CHKPV(jsonProductConfig);
147 auto jsonTouchscreen = cJSON_CreateObject();
148 CHKPV(jsonTouchscreen);
149 if (!cJSON_AddItemToObject(jsonProductConfig.get(), "touchscreen", jsonTouchscreen)) {
150 cJSON_Delete(jsonTouchscreen);
151 return;
152 }
153 SerializeInputProductConfig(jsonProductConfig.get());
154 }
155
156 /**
157 * @tc.name: MultimodalEventHandlerTest_ReadMaxMultiTouchPointNum_001
158 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
159 * @tc.type: FUNC
160 * @tc.require:
161 */
162 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_001, TestSize.Level1)
163 {
164 CALL_TEST_DEBUG;
165 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
166 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(nullptr));
167
168 int32_t maxMultiTouchPointNum {};
169 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
170 EXPECT_EQ(maxMultiTouchPointNum, -1);
171 }
172
173 /**
174 * @tc.name: ReadMaxMultiTouchPointNum_002
175 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
176 * @tc.type: FUNC
177 * @tc.require:
178 */
179 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_002, TestSize.Level1)
180 {
181 CALL_TEST_DEBUG;
182 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
183 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
184
185 int32_t maxMultiTouchPointNum {};
186 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
187 EXPECT_EQ(maxMultiTouchPointNum, -1);
188 }
189
190 /**
191 * @tc.name: ReadMaxMultiTouchPointNum_003
192 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
193 * @tc.type: FUNC
194 * @tc.require:
195 */
196 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_003, TestSize.Level1)
197 {
198 CALL_TEST_DEBUG;
199 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
200 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
201
202 BuildInputProductConfig3();
203 std::error_code ec {};
204 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
205
206 int32_t maxMultiTouchPointNum {};
207 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
208 EXPECT_EQ(maxMultiTouchPointNum, -1);
209 std::filesystem::remove(g_cfgName);
210 }
211
212 /**
213 * @tc.name: ReadMaxMultiTouchPointNum_004
214 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
215 * @tc.type: FUNC
216 * @tc.require:
217 */
218 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_004, TestSize.Level1)
219 {
220 CALL_TEST_DEBUG;
221 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
222 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
223
224 auto uid = ::getuid();
225 int32_t inputUid { 6606 };
226 ::setuid(inputUid);
227 int32_t maxTouchPoints { 11 };
228 BuildInputProductConfig(maxTouchPoints);
229 std::error_code ec {};
230 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
231 EXPECT_EQ(::chmod(g_cfgName, 0), 0);
232
233 int32_t panglaiUid { 7655 };
234 ::setuid(panglaiUid);
235 int32_t maxMultiTouchPointNum {};
236 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
237 EXPECT_EQ(maxMultiTouchPointNum, -1);
238 ::setuid(uid);
239 std::filesystem::remove(g_cfgName);
240 }
241
242 /**
243 * @tc.name: ReadMaxMultiTouchPointNum_005
244 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
245 * @tc.type: FUNC
246 * @tc.require:
247 */
248 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_005, TestSize.Level1)
249 {
250 CALL_TEST_DEBUG;
251 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
252 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
253
254 char maxTouchPoints[] { "9a" };
255 BuildInputProductConfig(maxTouchPoints);
256 std::error_code ec {};
257 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
258
259 int32_t maxMultiTouchPointNum {};
260 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
261 EXPECT_EQ(maxMultiTouchPointNum, -1);
262 std::filesystem::remove(g_cfgName);
263 }
264
265 /**
266 * @tc.name: ReadMaxMultiTouchPointNum_006
267 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
268 * @tc.type: FUNC
269 * @tc.require:
270 */
271 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_006, TestSize.Level1)
272 {
273 CALL_TEST_DEBUG;
274 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
275 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
276
277 BuildInputProductConfig6();
278 std::error_code ec {};
279 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
280
281 int32_t maxMultiTouchPointNum {};
282 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
283 EXPECT_EQ(maxMultiTouchPointNum, -1);
284 std::filesystem::remove(g_cfgName);
285 }
286
287 /**
288 * @tc.name: ReadMaxMultiTouchPointNum_007
289 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
290 * @tc.type: FUNC
291 * @tc.require:
292 */
293 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_007, TestSize.Level1)
294 {
295 CALL_TEST_DEBUG;
296 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
297 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
298
299 BuildInputProductConfig7();
300 std::error_code ec {};
301 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
302
303 int32_t maxMultiTouchPointNum {};
304 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
305 EXPECT_EQ(maxMultiTouchPointNum, -1);
306 std::filesystem::remove(g_cfgName);
307 }
308
309 /**
310 * @tc.name: ReadMaxMultiTouchPointNum_008
311 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
312 * @tc.type: FUNC
313 * @tc.require:
314 */
315 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_008, TestSize.Level1)
316 {
317 CALL_TEST_DEBUG;
318 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
319 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
320
321 BuildInputProductConfig8();
322 std::error_code ec {};
323 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
324
325 int32_t maxMultiTouchPointNum {};
326 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
327 EXPECT_EQ(maxMultiTouchPointNum, -1);
328 std::filesystem::remove(g_cfgName);
329 }
330
331 /**
332 * @tc.name: ReadMaxMultiTouchPointNum_009
333 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
334 * @tc.type: FUNC
335 * @tc.require:
336 */
337 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_009, TestSize.Level1)
338 {
339 CALL_TEST_DEBUG;
340 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
341 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
342
343 double maxTouchPoints { 9.01 };
344 BuildInputProductConfig(maxTouchPoints);
345 std::error_code ec {};
346 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
347
348 int32_t maxMultiTouchPointNum {};
349 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
350 EXPECT_EQ(maxMultiTouchPointNum, -1);
351 std::filesystem::remove(g_cfgName);
352 }
353
354 /**
355 * @tc.name: ReadMaxMultiTouchPointNum_010
356 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
357 * @tc.type: FUNC
358 * @tc.require:
359 */
360 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_010, TestSize.Level1)
361 {
362 CALL_TEST_DEBUG;
363 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
364 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
365
366 int32_t maxTouchPoints { 11 };
367 BuildInputProductConfig(maxTouchPoints);
368 std::error_code ec {};
369 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
370
371 int32_t maxMultiTouchPointNum {};
372 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
373 EXPECT_EQ(maxMultiTouchPointNum, -1);
374 std::filesystem::remove(g_cfgName);
375 }
376
377 /**
378 * @tc.name: ReadMaxMultiTouchPointNum_011
379 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
380 * @tc.type: FUNC
381 * @tc.require:
382 */
383 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_011, TestSize.Level1)
384 {
385 CALL_TEST_DEBUG;
386 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
387 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
388
389 int32_t maxTouchPoints { -1 };
390 BuildInputProductConfig(maxTouchPoints);
391 std::error_code ec {};
392 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
393
394 int32_t maxMultiTouchPointNum {};
395 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
396 EXPECT_EQ(maxMultiTouchPointNum, -1);
397 std::filesystem::remove(g_cfgName);
398 }
399
400 /**
401 * @tc.name: ReadMaxMultiTouchPointNum_012
402 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
403 * @tc.type: FUNC
404 * @tc.require:
405 */
406 HWTEST_F(MultimodalEventHandlerTest, ReadMaxMultiTouchPointNum_012, TestSize.Level1)
407 {
408 CALL_TEST_DEBUG;
409 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
410 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
411
412 int32_t maxTouchPoints { 10 };
413 BuildInputProductConfig(maxTouchPoints);
414 std::error_code ec {};
415 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
416
417 int32_t maxMultiTouchPointNum {};
418 MultimodalEventHandler::ReadMaxMultiTouchPointNum(maxMultiTouchPointNum);
419 EXPECT_EQ(maxMultiTouchPointNum, maxTouchPoints);
420 std::filesystem::remove(g_cfgName);
421 }
422
423 /**
424 * @tc.name: GetMaxMultiTouchPointNum_001
425 * @tc.desc: Test MultimodalEventHandler::ReadMaxMultiTouchPointNum
426 * @tc.type: FUNC
427 * @tc.require:
428 */
429 HWTEST_F(MultimodalEventHandlerTest, GetMaxMultiTouchPointNum_001, TestSize.Level1)
430 {
431 CALL_TEST_DEBUG;
432 NiceMock<ConfigPolicyUtilsMock> cfgPolicyUtils;
433 EXPECT_CALL(cfgPolicyUtils, GetOneCfgFile).WillOnce(testing::Return(g_cfgName));
434
435 constexpr int32_t expectedMaxTouchPoints { 10 };
436 BuildInputProductConfig(expectedMaxTouchPoints);
437 std::error_code ec {};
438 EXPECT_TRUE(std::filesystem::exists(g_cfgName, ec));
439
440 int32_t maxTouchPoints {};
441 auto ret = MultimodalEventHandler::GetMaxMultiTouchPointNum(maxTouchPoints);
442 EXPECT_EQ(ret, RET_OK);
443 EXPECT_EQ(maxTouchPoints, expectedMaxTouchPoints);
444 std::filesystem::remove(g_cfgName);
445 }
446 } // namespace MMI
447 } // namespace OHOS
448