1 /* 2 * Copyright (c) 2021-2022 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 <gtest/gtest.h> 17 18 #include "input_event.h" 19 #include "window_info.h" 20 #include "mmi_log.h" 21 22 #undef MMI_LOG_TAG 23 #define MMI_LOG_TAG "InputEventTest" 24 25 namespace OHOS { 26 namespace MMI { 27 namespace { 28 using namespace testing::ext; 29 } // namespace 30 31 class InputEventTest : public testing::Test { 32 public: SetUpTestCase(void)33 static void SetUpTestCase(void) {} TearDownTestCase(void)34 static void TearDownTestCase(void) {} 35 }; 36 37 /** 38 * @tc.name: InputEventTest_EventTypeToString_001 39 * @tc.desc: Test EventTypeToString 40 * @tc.type: FUNC 41 * @tc.require: 42 */ 43 HWTEST_F(InputEventTest, InputEventTest_EventTypeToString_001, TestSize.Level1) 44 { 45 CALL_TEST_DEBUG; 46 auto InputEvent = InputEvent::Create(); 47 ASSERT_NE(InputEvent, nullptr); 48 int32_t eventType = InputEvent::EVENT_TYPE_BASE; 49 auto ret = InputEvent->EventTypeToString(eventType); 50 ASSERT_STREQ(ret, "base"); 51 eventType = InputEvent::EVENT_TYPE_KEY; 52 ret = InputEvent->EventTypeToString(eventType); 53 ASSERT_STREQ(ret, "key"); 54 eventType = InputEvent::EVENT_TYPE_POINTER; 55 ret = InputEvent->EventTypeToString(eventType); 56 ASSERT_STREQ(ret, "pointer"); 57 eventType = InputEvent::EVENT_TYPE_AXIS; 58 ret = InputEvent->EventTypeToString(eventType); 59 ASSERT_STREQ(ret, "axis"); 60 eventType = InputEvent::EVENT_TYPE_FINGERPRINT; 61 ret = InputEvent->EventTypeToString(eventType); 62 ASSERT_STREQ(ret, "fingerprint"); 63 eventType = InputEvent::ACTION_CANCEL; 64 ret = InputEvent->EventTypeToString(eventType); 65 ASSERT_STREQ(ret, "unknown"); 66 } 67 68 /** 69 * @tc.name: InputEventTest_MarkProcessed_001 70 * @tc.desc: Test MarkProcessed 71 * @tc.type: FUNC 72 * @tc.require: 73 */ 74 HWTEST_F(InputEventTest, InputEventTest_MarkProcessed_001, TestSize.Level2) 75 { 76 CALL_TEST_DEBUG; 77 auto InputEvent = InputEvent::Create(); 78 ASSERT_NE(InputEvent, nullptr); __anon7d77373c0202(int a, int b) 79 auto callback = [](int a, int b) {}; 80 InputEvent->processedCallback_ = callback; 81 InputEvent->processedCallback_(10, 20); 82 ASSERT_NE(InputEvent->processedCallback_, nullptr); 83 InputEvent->markEnabled_ = false; 84 ASSERT_NO_FATAL_FAILURE(InputEvent->MarkProcessed()); 85 InputEvent->markEnabled_ = true; 86 ASSERT_NO_FATAL_FAILURE(InputEvent->MarkProcessed()); 87 } 88 89 /** 90 * @tc.name: InputEventTest_SetExtraData_001 91 * @tc.desc: Test SetExtraData 92 * @tc.type: FUNC 93 * @tc.require: 94 */ 95 HWTEST_F(InputEventTest, InputEventTest_SetExtraData_001, TestSize.Level2) 96 { 97 CALL_TEST_DEBUG; 98 auto InputEvent = InputEvent::Create(); 99 ASSERT_NE(InputEvent, nullptr); 100 ASSERT_NO_FATAL_FAILURE(InputEvent->SetExtraData(nullptr, 0)); 101 ASSERT_NO_FATAL_FAILURE(InputEvent->SetExtraData(nullptr, 100)); 102 ASSERT_NO_FATAL_FAILURE(InputEvent->SetExtraData(nullptr, 1500)); 103 uint8_t datas[5] = {1, 2, 3, 4, 5}; __anon7d77373c0302(const uint8_t*) 104 std::shared_ptr<const uint8_t[]> sharedData(datas, [](const uint8_t*) {}); 105 ASSERT_NE(sharedData, nullptr); 106 ASSERT_NO_FATAL_FAILURE(InputEvent->SetExtraData(sharedData, 0)); 107 ASSERT_NO_FATAL_FAILURE(InputEvent->SetExtraData(sharedData, 100)); 108 ASSERT_NO_FATAL_FAILURE(InputEvent->SetExtraData(sharedData, 1500)); 109 } 110 111 /** 112 * @tc.name: InputEventTest_WriteToParcel_001 113 * @tc.desc: Test WriteToParcel 114 * @tc.type: FUNC 115 * @tc.require: 116 */ 117 HWTEST_F(InputEventTest, InputEventTest_WriteToParcel_001, TestSize.Level1) 118 { 119 CALL_TEST_DEBUG; 120 auto InputEvent = InputEvent::Create(); 121 ASSERT_NE(InputEvent, nullptr); 122 Parcel out; 123 InputEvent->extraData_ = nullptr; 124 InputEvent->extraDataLength_ = 0; 125 bool ret = InputEvent->WriteToParcel(out); 126 ASSERT_TRUE(ret); 127 InputEvent->extraDataLength_ = 5; 128 ret = InputEvent->WriteToParcel(out); 129 ASSERT_TRUE(ret); 130 uint8_t datas[5] = {1, 2, 3, 4, 5}; __anon7d77373c0402(const uint8_t*) 131 std::shared_ptr<const uint8_t[]> sharedData(datas, [](const uint8_t*) {}); 132 InputEvent->extraData_ = sharedData; 133 InputEvent->extraDataLength_ = 0; 134 ret = InputEvent->WriteToParcel(out); 135 ASSERT_TRUE(ret); 136 InputEvent->extraDataLength_ = 5; 137 ret = InputEvent->WriteToParcel(out); 138 ASSERT_TRUE(ret); 139 } 140 141 /** 142 * @tc.name: InputEventTest_ReadFromParcel_001 143 * @tc.desc: Test ReadFromParcel 144 * @tc.type: FUNC 145 * @tc.require: 146 */ 147 HWTEST_F(InputEventTest, InputEventTest_ReadFromParcel_001, TestSize.Level1) 148 { 149 CALL_TEST_DEBUG; 150 auto InputEvent = InputEvent::Create(); 151 ASSERT_NE(InputEvent, nullptr); 152 Parcel in; 153 InputEvent->extraDataLength_ = 0; 154 bool ret = InputEvent->ReadFromParcel(in); 155 ASSERT_FALSE(ret); 156 InputEvent->extraDataLength_ = 1050; 157 ret = InputEvent->ReadFromParcel(in); 158 ASSERT_FALSE(ret); 159 InputEvent->extraDataLength_ = 10; 160 ret = InputEvent->ReadFromParcel(in); 161 ASSERT_FALSE(ret); 162 } 163 164 /** 165 * @tc.name: InputEventTest_ActionToShortStr_001 166 * @tc.desc: Test ActionToShortStr 167 * @tc.type: FUNC 168 * @tc.require: 169 */ 170 HWTEST_F(InputEventTest, InputEventTest_ActionToShortStr_001, TestSize.Level1) 171 { 172 CALL_TEST_DEBUG; 173 auto InputEvent = InputEvent::Create(); 174 ASSERT_NE(InputEvent, nullptr); 175 int32_t action = InputEvent::ACTION_CANCEL; 176 std::string_view ret = InputEvent->ActionToShortStr(action); 177 ASSERT_EQ(ret, "B:C:"); 178 action = InputEvent::ACTION_UNKNOWN; 179 ret = InputEvent->ActionToShortStr(action); 180 ASSERT_EQ(ret, "B:UK:"); 181 action = InputEvent::EVENT_TYPE_AXIS; 182 ret = InputEvent->ActionToShortStr(action); 183 ASSERT_EQ(ret, "B:?:"); 184 } 185 186 /** 187 * @tc.name: InputEventTest_operator_001 188 * @tc.desc: Test operator 189 * @tc.type: FUNC 190 * @tc.require: 191 */ 192 HWTEST_F(InputEventTest, InputEventTest_operator_001, TestSize.Level1) 193 { 194 CALL_TEST_DEBUG; 195 LogTracer source1(300, 5, 6); 196 LogTracer destination; 197 destination = std::move(source1); 198 LogTracer source2(400, 7, 8); 199 destination = std::move(source2); 200 } 201 202 /** 203 * @tc.name: InputEventTest_DisplayBindInfo_Marshalling_001 204 * @tc.desc: Test Unmarshalling 205 * @tc.type: FUNC 206 * @tc.require: 207 */ 208 HWTEST_F(InputEventTest, InputEventTest_DisplayBindInfo_Marshalling_001, TestSize.Level1) 209 { 210 CALL_TEST_DEBUG; 211 DisplayBindInfo info; 212 Parcel out; 213 info.inputDeviceId = 1; 214 info.inputDeviceName = "testName"; 215 info.displayId = 1; 216 info.displayName = "testDisplayName"; 217 bool ret = info.Marshalling(out); 218 ASSERT_TRUE(ret); 219 } 220 221 /** 222 * @tc.name: InputEventTest_Marshalling_001 223 * @tc.desc: Test Marshalling 224 * @tc.type: FUNC 225 * @tc.require: 226 */ 227 HWTEST_F(InputEventTest, InputEventTest_Marshalling_001, TestSize.Level1) 228 { 229 CALL_TEST_DEBUG; 230 auto InputEvent = InputEvent::Create(); 231 ASSERT_NE(InputEvent, nullptr); 232 Parcel out; 233 InputEvent->extraData_ = nullptr; 234 InputEvent->extraDataLength_ = 0; 235 bool ret = InputEvent->Marshalling(out); 236 ASSERT_TRUE(ret); 237 InputEvent->extraDataLength_ = 5; 238 ret = InputEvent->Marshalling(out); 239 ASSERT_TRUE(ret); 240 uint8_t datas[5] = {1, 2, 3, 4, 5}; __anon7d77373c0502(const uint8_t*) 241 std::shared_ptr<const uint8_t[]> sharedData(datas, [](const uint8_t*) {}); 242 InputEvent->extraData_ = sharedData; 243 InputEvent->extraDataLength_ = 0; 244 ret = InputEvent->Marshalling(out); 245 ASSERT_TRUE(ret); 246 InputEvent->extraDataLength_ = 5; 247 ret = InputEvent->Marshalling(out); 248 ASSERT_TRUE(ret); 249 } 250 251 /** 252 * @tc.name: InputEventTest_Unmarshalling_001 253 * @tc.desc: Test Unmarshalling 254 * @tc.type: FUNC 255 * @tc.require: 256 */ 257 HWTEST_F(InputEventTest, InputEventTest_Unmarshalling_001, TestSize.Level1) 258 { 259 CALL_TEST_DEBUG; 260 auto InputEvent = InputEvent::Create(); 261 ASSERT_NE(InputEvent, nullptr); 262 Parcel in; 263 InputEvent->extraDataLength_ = 0; 264 auto ret = InputEvent->Unmarshalling(in); 265 ASSERT_TRUE(ret == nullptr); 266 } 267 268 /** 269 * @tc.name: InputEventTest_DisplayBindInfo_ReadFromParcel_001 270 * @tc.desc: Test ReadFromParcel 271 * @tc.type: FUNC 272 * @tc.require: 273 */ 274 HWTEST_F(InputEventTest, InputEventTest_DisplayBindInfo_ReadFromParcel_001, TestSize.Level1) 275 { 276 CALL_TEST_DEBUG; 277 DisplayBindInfo info; 278 Parcel in; 279 ASSERT_NO_FATAL_FAILURE(info.ReadFromParcel(in)); 280 } 281 282 /** 283 * @tc.name: InputEventTest_DisplayBindInfo_Unmarshalling_001 284 * @tc.desc: Test Unmarshalling 285 * @tc.type: FUNC 286 * @tc.require: 287 */ 288 HWTEST_F(InputEventTest, InputEventTest_DisplayBindInfo_Unmarshalling_001, TestSize.Level1) 289 { 290 CALL_TEST_DEBUG; 291 DisplayBindInfo info; 292 Parcel in; 293 ASSERT_NO_FATAL_FAILURE(info.Unmarshalling(in)); 294 } 295 296 /** 297 * @tc.name: InputEventTest_ReadFromParcel_002 298 * @tc.desc: Verify ReadFromParcel when extraDataLength_ == 0 299 * @tc.type: FUNC 300 * @tc.require: 301 */ 302 HWTEST_F(InputEventTest, InputEventTest_ReadFromParcel_002, TestSize.Level1) 303 { 304 CALL_TEST_DEBUG; 305 auto inputEvent = InputEvent::Create(); 306 ASSERT_NE(inputEvent, nullptr); 307 Parcel in; 308 in.WriteInt32(InputEvent::EVENT_TYPE_KEY); 309 in.WriteInt32(1); 310 in.WriteInt64(100); 311 in.WriteUint64(200); 312 in.WriteInt32(2); 313 in.WriteInt64(300); 314 in.WriteInt32(10); 315 in.WriteInt32(1); 316 in.WriteInt32(1); 317 in.WriteInt32(1); 318 in.WriteInt32(1); 319 in.WriteUint32(0); 320 in.WriteBool(true); 321 in.WriteUint32(0); 322 bool ret = inputEvent->ReadFromParcel(in); 323 EXPECT_TRUE(ret); 324 } 325 326 /** 327 * @tc.name: InputEventTest_ReadFromParcel_003 328 * @tc.desc: Verify ReadFromParcel when extraDataLength_ > DATA_LENGTH_LIMIT 329 * @tc.type: FUNC 330 * @tc.require: 331 */ 332 HWTEST_F(InputEventTest, InputEventTest_ReadFromParcel_003, TestSize.Level1) 333 { 334 CALL_TEST_DEBUG; 335 auto inputEvent = InputEvent::Create(); 336 ASSERT_NE(inputEvent, nullptr); 337 Parcel in; 338 in.WriteInt32(InputEvent::EVENT_TYPE_KEY); 339 in.WriteInt32(1); 340 in.WriteInt64(100); 341 in.WriteUint64(200); 342 in.WriteInt32(2); 343 in.WriteInt64(300); 344 in.WriteInt32(10); 345 in.WriteInt32(1); 346 in.WriteInt32(1); 347 in.WriteInt32(1); 348 in.WriteUint32(0); 349 in.WriteBool(true); 350 in.WriteUint32(1024 + 1); 351 bool ret = inputEvent->ReadFromParcel(in); 352 EXPECT_FALSE(ret); 353 } 354 355 /** 356 * @tc.name: InputEventTest_ReadFromParcel_004 357 * @tc.desc: Verify ReadFromParcel when ReadBuffer returns nullptr 358 * @tc.type: FUNC 359 * @tc.require: 360 */ 361 HWTEST_F(InputEventTest, InputEventTest_ReadFromParcel_004, TestSize.Level1) 362 { 363 CALL_TEST_DEBUG; 364 auto inputEvent = InputEvent::Create(); 365 ASSERT_NE(inputEvent, nullptr); 366 Parcel in; 367 in.WriteInt32(InputEvent::EVENT_TYPE_KEY); 368 in.WriteInt32(1); 369 in.WriteInt64(100); 370 in.WriteUint64(200); 371 in.WriteInt32(2); 372 in.WriteInt64(300); 373 in.WriteInt32(10); 374 in.WriteInt32(1); 375 in.WriteInt32(1); 376 in.WriteInt32(1); 377 in.WriteUint32(0); 378 in.WriteBool(true); 379 in.WriteUint32(5); 380 bool ret = inputEvent->ReadFromParcel(in); 381 EXPECT_FALSE(ret); 382 } 383 384 /** 385 * @tc.name: InputEventTest_StartLogTraceId_001 386 * @tc.desc: Verify StartLogTraceId does nothing when traceId is -1 387 * @tc.type: FUNC 388 * @tc.require: 389 */ 390 HWTEST_F(InputEventTest, InputEventTest_StartLogTraceId_001, TestSize.Level1) 391 { 392 CALL_TEST_DEBUG; 393 int64_t traceId = -1; 394 int32_t eventType = 100; 395 int32_t action = 200; 396 EXPECT_NO_FATAL_FAILURE(StartLogTraceId(traceId, eventType, action)); 397 } 398 399 /** 400 * @tc.name: InputEventTest_StartLogTraceId_002 401 * @tc.desc: Verify StartLogTraceId inserts first traceId correctly 402 * @tc.type: FUNC 403 * @tc.require: 404 */ 405 HWTEST_F(InputEventTest, InputEventTest_StartLogTraceId_002, TestSize.Level1) 406 { 407 CALL_TEST_DEBUG; 408 int64_t traceId = 123; 409 int32_t eventType = 1; 410 int32_t action = 2; 411 EXPECT_NO_FATAL_FAILURE(StartLogTraceId(traceId, eventType, action)); 412 } 413 414 /** 415 * @tc.name: InputEventTest_StartLogTraceId_003 416 * @tc.desc: Verify StartLogTraceId appends second traceId correctly 417 * @tc.type: FUNC 418 * @tc.require: 419 */ 420 HWTEST_F(InputEventTest, InputEventTest_StartLogTraceId_003, TestSize.Level1) 421 { 422 CALL_TEST_DEBUG; 423 StartLogTraceId(456, 1, 2); 424 EXPECT_NO_FATAL_FAILURE(StartLogTraceId(789, 3, 4)); 425 } 426 427 /** 428 * @tc.name: InputEventTest_StartLogTraceId_004 429 * @tc.desc: Verify StartLogTraceId updates existing traceId with new type and action 430 * @tc.type: FUNC 431 * @tc.require: 432 */ 433 HWTEST_F(InputEventTest, InputEventTest_StartLogTraceId_004, TestSize.Level1) 434 { 435 CALL_TEST_DEBUG; 436 int64_t traceId = 888; 437 StartLogTraceId(traceId, 10, 20); 438 EXPECT_NO_FATAL_FAILURE(StartLogTraceId(traceId, 11, 21)); 439 } 440 } // namespace MMI 441 } // namespace OHOS