From dcb89f58c3c3b5dccdee8c1753af74c06817dae3 Mon Sep 17 00:00:00 2001 From: Shin Yamamoto Date: Sat, 17 Feb 2024 09:04:09 +0900 Subject: [PATCH] Add CoreTest.test_handleGesture_endWithoutAttraction() --- Sources/Core.swift | 20 ++++++++++++-------- Tests/CoreTests.swift | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/Sources/Core.swift b/Sources/Core.swift index f0481896..10f8e25b 100644 --- a/Sources/Core.swift +++ b/Sources/Core.swift @@ -736,14 +736,7 @@ class Core: NSObject, UIGestureRecognizerDelegate { } guard shouldAttract(to: target) else { - self.state = target - self.updateLayout(to: target) - self.unlockScrollView() - // The `floatingPanelDidEndDragging(_:willAttract:)` must be called after the state property changes. - // This allows library users to get the correct state in the delegate method. - if let vc = ownerVC { - vc.delegate?.floatingPanelDidEndDragging?(vc, willAttract: false) - } + self.endWithoutAttraction(target) return } @@ -886,6 +879,17 @@ class Core: NSObject, UIGestureRecognizerDelegate { return true } + func endWithoutAttraction(_ target: FloatingPanelState) { + self.state = target + self.updateLayout(to: target) + self.unlockScrollView() + // The `floatingPanelDidEndDragging(_:willAttract:)` must be called after the state property changes. + // This allows library users to get the correct state in the delegate method. + if let vc = ownerVC { + vc.delegate?.floatingPanelDidEndDragging?(vc, willAttract: false) + } + } + private func startAttraction(to state: FloatingPanelState, with velocity: CGPoint, completion: @escaping (() -> Void)) { os_log(msg, log: devLog, type: .debug, "startAnimation to \(state) -- velocity = \(value(of: velocity))") guard let vc = ownerVC else { return } diff --git a/Tests/CoreTests.swift b/Tests/CoreTests.swift index 81fb5b5e..83850b13 100644 --- a/Tests/CoreTests.swift +++ b/Tests/CoreTests.swift @@ -913,6 +913,27 @@ class CoreTests: XCTestCase { ) } } + + func test_handleGesture_endWithoutAttraction() throws { + class Delegate: FloatingPanelControllerDelegate { + var willAttract: Bool? + func floatingPanelDidEndDragging(_ fpc: FloatingPanelController, willAttract attract: Bool) { + willAttract = attract + } + } + let fpc = FloatingPanelController() + let scrollView = UIScrollView() + let delegate = Delegate() + fpc.showForTest() + fpc.delegate = delegate + XCTAssertEqual(fpc.state, .half) + + fpc.floatingPanel.endWithoutAttraction(.full) + + XCTAssertEqual(fpc.state, .full) + XCTAssertEqual(fpc.surfaceLocation(for: .full).y, fpc.surfaceLocation.y) + XCTAssertEqual(delegate.willAttract, false) + } } private class FloatingPanelLayout3Positions: FloatingPanelTestLayout {