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 16 #include <fstream> 17 18 #include <gtest/gtest.h> 19 20 #include "gesture_monitor_handler.h" 21 #include "pointer_event.h" 22 #include "mmi_log.h" 23 24 #undef MMI_LOG_TAG 25 #define MMI_LOG_TAG "GestureMonitorHandlerTest" 26 namespace OHOS { 27 namespace MMI { 28 namespace { 29 using namespace testing::ext; 30 } // namespace 31 32 class GestureMonitorHandlerTest : public testing::Test { 33 public: SetUpTestCase(void)34 static void SetUpTestCase(void) {} TearDownTestCase(void)35 static void TearDownTestCase(void) {} 36 }; 37 38 /** 39 * @tc.name: EventMonitorHandlerTest_CheckMonitorValid_001 40 * @tc.desc: Test the funcation CheckMonitorValid 41 * @tc.type: FUNC 42 * @tc.require: 43 */ 44 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_CheckMonitorValid_001, TestSize.Level1) 45 { 46 CALL_TEST_DEBUG; 47 GestureMonitorHandler handler; 48 TouchGestureType type = 2; 49 int32_t fingers = 0; 50 bool ret = handler.CheckMonitorValid(type, fingers); 51 ASSERT_TRUE(ret); 52 fingers = 4; 53 ret = handler.CheckMonitorValid(type, fingers); 54 ASSERT_TRUE(ret); 55 type = 1; 56 ret = handler.CheckMonitorValid(type, fingers); 57 ASSERT_TRUE(ret); 58 fingers = 10; 59 type = 0; 60 ret = handler.CheckMonitorValid(type, fingers); 61 ASSERT_FALSE(ret); 62 type = 5; 63 ret = handler.CheckMonitorValid(type, fingers); 64 ASSERT_FALSE(ret); 65 } 66 67 /** 68 * @tc.name: EventMonitorHandlerTest_CheckMonitorValid_002 69 * @tc.desc: Test the funcation CheckMonitorValid 70 * @tc.type: FUNC 71 * @tc.require: 72 */ 73 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_CheckMonitorValid_002, TestSize.Level1) 74 { 75 CALL_TEST_DEBUG; 76 GestureMonitorHandler handler; 77 TouchGestureType type = TOUCH_GESTURE_TYPE_ALL; 78 int32_t fingers = FOUR_FINGER_COUNT; 79 bool ret = handler.CheckMonitorValid(type, fingers); 80 ASSERT_TRUE(ret); 81 type = TOUCH_GESTURE_TYPE_PINCH; 82 fingers = THREE_FINGER_COUNT; 83 ret = handler.CheckMonitorValid(type, fingers); 84 ASSERT_FALSE(ret); 85 type = TOUCH_GESTURE_TYPE_SWIPE; 86 fingers = 2; 87 ret = handler.CheckMonitorValid(type, fingers); 88 ASSERT_FALSE(ret); 89 type = TOUCH_GESTURE_TYPE_ALL; 90 fingers = MAX_FINGERS_COUNT; 91 ret = handler.CheckMonitorValid(type, fingers); 92 ASSERT_TRUE(ret); 93 type = 0xFFFFFFFF; 94 fingers = FOUR_FINGER_COUNT; 95 ret = handler.CheckMonitorValid(type, fingers); 96 ASSERT_FALSE(ret); 97 type = TOUCH_GESTURE_TYPE_ALL; 98 fingers = -1; 99 ret = handler.CheckMonitorValid(type, fingers); 100 ASSERT_FALSE(ret); 101 } 102 103 104 /** 105 * @tc.name: EventMonitorHandlerTest_IsTouchGestureEvent_001 106 * @tc.desc: Test the funcation IsTouchGestureEvent 107 * @tc.type: FUNC 108 * @tc.require: 109 */ 110 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_IsTouchGestureEvent_001, TestSize.Level1) 111 { 112 CALL_TEST_DEBUG; 113 GestureMonitorHandler handler; 114 int32_t pointerAction = PointerEvent::TOUCH_ACTION_SWIPE_DOWN; 115 bool ret = handler.IsTouchGestureEvent(pointerAction); 116 ASSERT_TRUE(ret); 117 pointerAction = PointerEvent::TOUCH_ACTION_SWIPE_UP; 118 ret = handler.IsTouchGestureEvent(pointerAction); 119 ASSERT_TRUE(ret); 120 pointerAction = PointerEvent::TOUCH_ACTION_SWIPE_RIGHT; 121 ret = handler.IsTouchGestureEvent(pointerAction); 122 ASSERT_TRUE(ret); 123 pointerAction = PointerEvent::TOUCH_ACTION_SWIPE_LEFT; 124 ret = handler.IsTouchGestureEvent(pointerAction); 125 ASSERT_TRUE(ret); 126 pointerAction = PointerEvent::TOUCH_ACTION_PINCH_OPENED; 127 ret = handler.IsTouchGestureEvent(pointerAction); 128 ASSERT_TRUE(ret); 129 pointerAction = PointerEvent::TOUCH_ACTION_PINCH_CLOSEED; 130 ret = handler.IsTouchGestureEvent(pointerAction); 131 ASSERT_TRUE(ret); 132 pointerAction = PointerEvent::TOUCH_ACTION_GESTURE_END; 133 ret = handler.IsTouchGestureEvent(pointerAction); 134 ASSERT_TRUE(ret); 135 pointerAction = 99; 136 ret = handler.IsTouchGestureEvent(pointerAction); 137 ASSERT_FALSE(ret); 138 } 139 140 /** 141 * @tc.name: EventMonitorHandlerTest_IsMatchGesture_002 142 * @tc.desc: Test the funcation IsMatchGesture 143 * @tc.type: FUNC 144 * @tc.require: 145 */ 146 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_IsMatchGesture_002, TestSize.Level1) 147 { 148 CALL_TEST_DEBUG; 149 GestureMonitorHandler handler; 150 int32_t action = PointerEvent::TOUCH_ACTION_GESTURE_END; 151 int32_t count = 0; 152 bool ret = handler.IsMatchGesture(action, count); 153 ASSERT_TRUE(ret); 154 action = PointerEvent::TOUCH_ACTION_SWIPE_DOWN; 155 ret = handler.IsMatchGesture(action, count); 156 ASSERT_FALSE(ret); 157 action = PointerEvent::TOUCH_ACTION_SWIPE_UP; 158 ret = handler.IsMatchGesture(action, count); 159 ASSERT_FALSE(ret); 160 action = PointerEvent::TOUCH_ACTION_SWIPE_RIGHT; 161 ret = handler.IsMatchGesture(action, count); 162 ASSERT_FALSE(ret); 163 action = PointerEvent::TOUCH_ACTION_SWIPE_LEFT; 164 ret = handler.IsMatchGesture(action, count); 165 ASSERT_FALSE(ret); 166 action = PointerEvent::TOUCH_ACTION_PINCH_OPENED; 167 ret = handler.IsMatchGesture(action, count); 168 ASSERT_FALSE(ret); 169 action = PointerEvent::TOUCH_ACTION_PINCH_CLOSEED; 170 ret = handler.IsMatchGesture(action, count); 171 ASSERT_FALSE(ret); 172 action = 90; 173 ret = handler.IsMatchGesture(action, count); 174 ASSERT_FALSE(ret); 175 } 176 177 /** 178 * @tc.name: EventMonitorHandlerTest_IsMatchGesture_003 179 * @tc.desc: Test the funcation IsMatchGesture 180 * @tc.type: FUNC 181 * @tc.require: 182 */ 183 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_IsMatchGesture_003, TestSize.Level1) 184 { 185 CALL_TEST_DEBUG; 186 GestureMonitorHandler handler; 187 int32_t action = PointerEvent::TOUCH_ACTION_SWIPE_DOWN; 188 int32_t count = 1; 189 handler.touchGestureInfo_.insert(std::make_pair(100, std::set<int32_t>{1, 2, 3})); 190 bool ret = handler.IsMatchGesture(action, count); 191 ASSERT_FALSE(ret); 192 action = PointerEvent::TOUCH_ACTION_PINCH_OPENED; 193 ret = handler.IsMatchGesture(action, count); 194 ASSERT_FALSE(ret); 195 } 196 197 /** 198 * @tc.name: EventMonitorHandlerTest_IsMatchGesture_004 199 * @tc.desc: Test the funcation IsMatchGesture 200 * @tc.type: FUNC 201 * @tc.require: 202 */ 203 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_IsMatchGesture_004, TestSize.Level1) 204 { 205 CALL_TEST_DEBUG; 206 GestureMonitorHandler handler; 207 int32_t action = PointerEvent::TOUCH_ACTION_SWIPE_DOWN; 208 int32_t count = 1; 209 handler.gestureType_ = TOUCH_GESTURE_TYPE_SWIPE; 210 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_SWIPE, std::set<int32_t>{1, 2, 3})); 211 bool ret = handler.IsMatchGesture(action, count); 212 ASSERT_TRUE(ret); 213 handler.gestureType_ = TOUCH_GESTURE_TYPE_PINCH; 214 ret = handler.IsMatchGesture(action, count); 215 ASSERT_FALSE(ret); 216 handler.gestureType_ = TOUCH_GESTURE_TYPE_SWIPE; 217 auto range = handler.touchGestureInfo_.equal_range(TOUCH_GESTURE_TYPE_SWIPE); 218 handler.touchGestureInfo_.erase(range.first, range.second); 219 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_SWIPE, std::set<int32_t>{2, 3})); 220 ret = handler.IsMatchGesture(action, count); 221 ASSERT_FALSE(ret); 222 action = PointerEvent::TOUCH_ACTION_PINCH_OPENED; 223 handler.gestureType_ = TOUCH_GESTURE_TYPE_PINCH; 224 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_PINCH, std::set<int32_t>{1, 2, 3})); 225 ret = handler.IsMatchGesture(action, count); 226 ASSERT_TRUE(ret); 227 handler.gestureType_ = TOUCH_GESTURE_TYPE_SWIPE; 228 ret = handler.IsMatchGesture(action, count); 229 ASSERT_FALSE(ret); 230 handler.gestureType_ = TOUCH_GESTURE_TYPE_PINCH; 231 range = handler.touchGestureInfo_.equal_range(TOUCH_GESTURE_TYPE_PINCH); 232 handler.touchGestureInfo_.erase(range.first, range.second); 233 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_PINCH, std::set<int32_t>{2, 3})); 234 ret = handler.IsMatchGesture(action, count); 235 ASSERT_FALSE(ret); 236 } 237 238 /** 239 * @tc.name: EventMonitorHandlerTest_IsMatchGesture_005 240 * @tc.desc: Test the funcation IsMatchGesture 241 * @tc.type: FUNC 242 * @tc.require: 243 */ 244 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_IsMatchGesture_005, TestSize.Level1) 245 { 246 CALL_TEST_DEBUG; 247 GestureMonitorHandler handler; 248 int32_t action = PointerEvent::TOUCH_ACTION_SWIPE_DOWN; 249 int32_t count = 1; 250 handler.gestureType_ = TOUCH_GESTURE_TYPE_SWIPE; 251 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_ALL, std::set<int32_t>{1, 2, 3})); 252 bool ret = handler.IsMatchGesture(action, count); 253 ASSERT_TRUE(ret); 254 action = PointerEvent::TOUCH_ACTION_PINCH_OPENED; 255 handler.gestureType_ = TOUCH_GESTURE_TYPE_PINCH; 256 ret = handler.IsMatchGesture(action, count); 257 ASSERT_TRUE(ret); 258 handler.gestureType_ = TOUCH_GESTURE_TYPE_ALL; 259 ret = handler.IsMatchGesture(action, count); 260 ASSERT_TRUE(ret); 261 handler.gestureType_ = TOUCH_GESTURE_TYPE_NONE; 262 ret = handler.IsMatchGesture(action, count); 263 ASSERT_FALSE(ret); 264 } 265 266 /** 267 * @tc.name: EventMonitorHandlerTest_IsMatchGesture_006 268 * @tc.desc: Test the funcation IsMatchGesture 269 * @tc.type: FUNC 270 * @tc.require: 271 */ 272 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_IsMatchGesture_006, TestSize.Level1) 273 { 274 CALL_TEST_DEBUG; 275 GestureMonitorHandler handler; 276 int32_t action = PointerEvent::TOUCH_ACTION_SWIPE_DOWN; 277 int32_t count = 1; 278 handler.gestureType_ = TOUCH_GESTURE_TYPE_SWIPE; 279 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_SWIPE, std::set<int32_t>{ALL_FINGER_COUNT})); 280 bool ret = handler.IsMatchGesture(action, count); 281 ASSERT_TRUE(ret); 282 handler.gestureType_ = TOUCH_GESTURE_TYPE_PINCH; 283 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_PINCH, std::set<int32_t>{ALL_FINGER_COUNT})); 284 action = PointerEvent::TOUCH_ACTION_PINCH_OPENED; 285 ret = handler.IsMatchGesture(action, count); 286 ASSERT_TRUE(ret); 287 } 288 289 /** 290 * @tc.name: EventMonitorHandlerTest_AddGestureMonitor 291 * @tc.desc: Test the funcation AddGestureMonitor 292 * @tc.type: FUNC 293 * @tc.require: 294 */ 295 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_AddGestureMonitor, TestSize.Level1) 296 { 297 CALL_TEST_DEBUG; 298 GestureMonitorHandler handler; 299 TouchGestureType type = TOUCH_GESTURE_TYPE_NONE; 300 int32_t fingers = THREE_FINGER_COUNT; 301 EXPECT_NO_FATAL_FAILURE(handler.AddGestureMonitor(type, fingers)); 302 type = TOUCH_GESTURE_TYPE_PINCH; 303 handler.touchGestureInfo_.insert(std::make_pair(2, std::set<int32_t>{1, 2})); 304 EXPECT_NO_FATAL_FAILURE(handler.AddGestureMonitor(type, fingers)); 305 type = TOUCH_GESTURE_TYPE_SWIPE; 306 EXPECT_NO_FATAL_FAILURE(handler.AddGestureMonitor(type, fingers)); 307 } 308 309 /** 310 * @tc.name: EventMonitorHandlerTest_AddGestureMonitor_001 311 * @tc.desc: Test the funcation AddGestureMonitor 312 * @tc.type: FUNC 313 * @tc.require: 314 */ 315 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_AddGestureMonitor_001, TestSize.Level1) 316 { 317 CALL_TEST_DEBUG; 318 GestureMonitorHandler handler; 319 TouchGestureType type = TOUCH_GESTURE_TYPE_NONE; 320 int32_t fingers = THREE_FINGER_COUNT; 321 EXPECT_NO_FATAL_FAILURE(handler.AddGestureMonitor(type, fingers)); 322 type = TOUCH_GESTURE_TYPE_PINCH; 323 handler.touchGestureInfo_.insert(std::make_pair(2, std::set<int32_t>{1, 2})); 324 EXPECT_NO_FATAL_FAILURE(handler.AddGestureMonitor(type, fingers)); 325 type = TOUCH_GESTURE_TYPE_SWIPE; 326 EXPECT_NO_FATAL_FAILURE(handler.AddGestureMonitor(type, fingers)); 327 } 328 329 /** 330 * @tc.name: EventMonitorHandlerTest_RemoveGestureMonitor_001 331 * @tc.desc: Test the funcation RemoveGestureMonitor 332 * @tc.type: FUNC 333 * @tc.require: 334 */ 335 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_RemoveGestureMonitor_001, TestSize.Level1) 336 { 337 CALL_TEST_DEBUG; 338 GestureMonitorHandler handler; 339 TouchGestureType type = TOUCH_GESTURE_TYPE_PINCH; 340 int32_t fingers = THREE_FINGER_COUNT; 341 bool ret = handler.RemoveGestureMonitor(type, fingers); 342 ASSERT_FALSE(ret); 343 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_PINCH, std::set<int32_t>{})); 344 ret = handler.RemoveGestureMonitor(type, fingers); 345 ASSERT_FALSE(ret); 346 } 347 348 /** 349 * @tc.name: EventMonitorHandlerTest_RemoveGestureMonitor_002 350 * @tc.desc: Test the funcation RemoveGestureMonitor 351 * @tc.type: FUNC 352 * @tc.require: 353 */ 354 HWTEST_F(GestureMonitorHandlerTest, EventMonitorHandlerTest_RemoveGestureMonitor_002, TestSize.Level1) 355 { 356 CALL_TEST_DEBUG; 357 GestureMonitorHandler handler; 358 TouchGestureType type = TOUCH_GESTURE_TYPE_PINCH; 359 int32_t fingers = THREE_FINGER_COUNT; 360 handler.touchGestureInfo_.insert(std::make_pair(type, std::set<int32_t>{ fingers, FOUR_FINGER_COUNT})); 361 handler.touchGestureInfo_.insert(std::make_pair(TOUCH_GESTURE_TYPE_SWIPE, std::set<int32_t>{ FOUR_FINGER_COUNT})); 362 bool ret = handler.RemoveGestureMonitor(TOUCH_GESTURE_TYPE_SWIPE, FOUR_FINGER_COUNT); 363 ASSERT_FALSE(ret); 364 ret = handler.RemoveGestureMonitor(type, FOUR_FINGER_COUNT); 365 ASSERT_FALSE(ret); 366 ret = handler.RemoveGestureMonitor(type, fingers); 367 ASSERT_TRUE(ret); 368 } 369 } // namespace MMI 370 } // namespace MMI