From 6006079dc8709a57df8c6f280955fc84d0a199c9 Mon Sep 17 00:00:00 2001 From: Soojin Ro Date: Sat, 11 Dec 2021 15:58:19 -0500 Subject: [PATCH] Fix FingerUp causing UITouch to end in start location (#35) --- .../EventGenerator+Hand.swift | 1 + Sources/Hammer/Utilties/TouchStorage.swift | 6 +++++ Tests/HammerTests/DragTests.swift | 24 +++++++++++++++++++ .../HammerTests/Utilities/TouchTestView.swift | 13 ++++++++++ 4 files changed, 44 insertions(+) create mode 100644 Tests/HammerTests/DragTests.swift create mode 100644 Tests/HammerTests/Utilities/TouchTestView.swift diff --git a/Sources/Hammer/EventGenerator/EventGenerator+Hand/EventGenerator+Hand.swift b/Sources/Hammer/EventGenerator/EventGenerator+Hand/EventGenerator+Hand.swift index beac887..a184d0d 100644 --- a/Sources/Hammer/EventGenerator/EventGenerator+Hand/EventGenerator+Hand.swift +++ b/Sources/Hammer/EventGenerator/EventGenerator+Hand/EventGenerator+Hand.swift @@ -498,6 +498,7 @@ extension EventGenerator { throw HammerError.touchForFingerDoesNotExist(index: finger.fingerIndex) } + try self.activeTouches.update(finger: finger, forIdentifier: existingIdentifier) return existingIdentifier case .ended, .cancelled: guard let existingIdentifier = existingIdentifier else { diff --git a/Sources/Hammer/Utilties/TouchStorage.swift b/Sources/Hammer/Utilties/TouchStorage.swift index 6b707a5..bb620a1 100644 --- a/Sources/Hammer/Utilties/TouchStorage.swift +++ b/Sources/Hammer/Utilties/TouchStorage.swift @@ -54,4 +54,10 @@ struct TouchStorage { self.stylusStore = nil } } + + mutating func update(finger: FingerInfo, forIdentifier identifier: UInt32) throws { + if let index = self.fingerStore.firstIndex(where: { $0.identifier == identifier }) { + self.fingerStore[index].finger = finger + } + } } diff --git a/Tests/HammerTests/DragTests.swift b/Tests/HammerTests/DragTests.swift new file mode 100644 index 0000000..75ca891 --- /dev/null +++ b/Tests/HammerTests/DragTests.swift @@ -0,0 +1,24 @@ +import Hammer +import XCTest +import Foundation + +final class DragTests: XCTestCase { + + func test_drag() throws { + let view = TouchTestView() + + view.widthAnchor.constraint(equalToConstant: 300).isActive = true + view.heightAnchor.constraint(equalToConstant: 300).isActive = true + + let eventGenerator = try EventGenerator(view: view) + try eventGenerator.waitUntilHittable(timeout: 1) + + try eventGenerator.fingerDown(at: view.frame.center.offset(x: -50, y: 0)) + try eventGenerator.fingerMove(to: view.frame.center.offset(x: 50, y: 0), duration: 0.3) + try eventGenerator.fingerUp() + + let endPoint = view.touches.first!.location(in: view) + XCTAssertEqual(endPoint, CGPoint(x: 200, y: 150), accuracy: 0.001) + } + +} diff --git a/Tests/HammerTests/Utilities/TouchTestView.swift b/Tests/HammerTests/Utilities/TouchTestView.swift new file mode 100644 index 0000000..fe02490 --- /dev/null +++ b/Tests/HammerTests/Utilities/TouchTestView.swift @@ -0,0 +1,13 @@ +import UIKit + +final class TouchTestView: UIView { + + var touches: Set = .init() + + override func touchesBegan(_ touches: Set, with event: UIEvent?) { + super.touchesBegan(touches, with: event) + + touches.forEach { self.touches.insert($0) } + } + +}