From 75c27bc232a4ee9e3c1cf743f9af97ab8f4da170 Mon Sep 17 00:00:00 2001 From: Shin Yamamoto Date: Fri, 5 Jul 2019 16:04:33 +0900 Subject: [PATCH] Add test_getBackdropAlpha() --- Framework/Sources/FloatingPanel.swift | 19 ++-- Framework/Tests/FloatingPanelTests.swift | 114 +++++++++++++++++++++++ 2 files changed, 126 insertions(+), 7 deletions(-) diff --git a/Framework/Sources/FloatingPanel.swift b/Framework/Sources/FloatingPanel.swift index 84b8664..1dfd7cb 100644 --- a/Framework/Sources/FloatingPanel.swift +++ b/Framework/Sources/FloatingPanel.swift @@ -142,11 +142,15 @@ class FloatingPanel: NSObject, UIGestureRecognizerDelegate { self.layoutAdapter.activateLayout(of: target) } - private func getBackdropAlpha(with translation: CGPoint) -> CGFloat { - let currentY = surfaceView.frame.minY + func getBackdropAlpha(at currentY: CGFloat, with translation: CGPoint) -> CGFloat { + let forwardY = (translation.y >= 0) + let segment = layoutAdapter.segument(at: currentY, forward: forwardY) + let lowerPos = segment.lower ?? layoutAdapter.topMostState + let upperPos = segment.upper ?? layoutAdapter.bottomMostState + + let pre = forwardY ? lowerPos : upperPos + let next = forwardY ? upperPos : lowerPos - let next = directionalPosition(at: currentY, with: translation) - let pre = redirectionalPosition(at: currentY, with: translation) let nextY = layoutAdapter.positionY(for: next) let preY = layoutAdapter.positionY(for: pre) @@ -417,17 +421,18 @@ class FloatingPanel: NSObject, UIGestureRecognizerDelegate { private func panningChange(with translation: CGPoint) { log.debug("panningChange -- translation = \(translation.y)") - let pre = surfaceView.frame.minY + let preY = surfaceView.frame.minY let dy = translation.y - initialTranslationY layoutAdapter.updateInteractiveTopConstraint(diff: dy, allowsTopBuffer: allowsTopBuffer(for: dy), with: behavior) - backdropView.alpha = getBackdropAlpha(with: translation) + let currentY = surfaceView.frame.minY + backdropView.alpha = getBackdropAlpha(at: currentY, with: translation) preserveContentVCLayoutIfNeeded() - let didMove = (pre != surfaceView.frame.minY) + let didMove = (preY != currentY) guard didMove else { return } viewcontroller.delegate?.floatingPanelDidMove(viewcontroller) diff --git a/Framework/Tests/FloatingPanelTests.swift b/Framework/Tests/FloatingPanelTests.swift index 6df5db2..519602c 100644 --- a/Framework/Tests/FloatingPanelTests.swift +++ b/Framework/Tests/FloatingPanelTests.swift @@ -60,6 +60,120 @@ class FloatingPanelTests: XCTestCase { XCTAssertEqual(contentVC2.tableView.bounces, false) } + func test_getBackdropAlpha_1positions() { + class FloatingPanelLayout1Positions: FloatingPanelTestLayout { + let initialPosition: FloatingPanelPosition = .full + let supportedPositions: Set = [.full] + } + let delegate = FloatingPanelTestDelegate() + delegate.layout = FloatingPanelLayout1Positions() + + let fpc = FloatingPanelController(delegate: delegate) + fpc.showForTest() + + let fullPos = fpc.originYOfSurface(for: .full) + + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos - 100.0, with: CGPoint(x: 0.0, y: -100.0)), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: CGPoint(x: 0.0, y: 0.0)), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + 100.0, with: CGPoint(x: 0.0, y: 100.0)), 0.3) // ok?? + } + + func test_getBackdropAlpha_2positions() { + class FloatingPanelLayout2Positions: FloatingPanelTestLayout { + let initialPosition: FloatingPanelPosition = .half + let supportedPositions: Set = [.half, .full] + } + let delegate = FloatingPanelTestDelegate() + delegate.layout = FloatingPanelLayout2Positions() + + let fpc = FloatingPanelController(delegate: delegate) + fpc.showForTest() + + let fullPos = fpc.originYOfSurface(for: .full) + let halfPos = fpc.originYOfSurface(for: .half) + let distance1 = abs(halfPos - fullPos) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: CGPoint(x: 0.0, y: 0.0)), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: CGPoint(x: 0.0, y: distance1 * 0.5)), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: CGPoint(x: 0.0, y: distance1)), 0.0) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: CGPoint(x: 0.0, y: 0.0)), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: CGPoint(x: 0.0, y: -0.5 * distance1)), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: CGPoint(x: 0.0, y: -1 * distance1)), 0.3) + } + + func test_getBackdropAlpha_2positionsWithHidden() { + class FloatingPanelLayout2Positions: FloatingPanelTestLayout { + let initialPosition: FloatingPanelPosition = .hidden + let supportedPositions: Set = [.hidden, .full] + } + let delegate = FloatingPanelTestDelegate() + delegate.layout = FloatingPanelLayout2Positions() + + let fpc = FloatingPanelController(delegate: delegate) + fpc.showForTest() + + let fullPos = fpc.originYOfSurface(for: .full) + let hiddenPos = fpc.originYOfSurface(for: .hidden) + + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos - 100.0, with: CGPoint(x: 0.0, y: -100.0)), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: CGPoint(x: 0.0, y: 0.0)), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: hiddenPos, with: CGPoint(x: 0.0, y: 100.0)), 0.0) + } + + func test_getBackdropAlpha_3positions() { + let fpc = FloatingPanelController() + fpc.showForTest() + + let fullPos = fpc.originYOfSurface(for: .full) + let halfPos = fpc.originYOfSurface(for: .half) + let tipPos = fpc.originYOfSurface(for: .tip) + let distance1 = abs(halfPos - fullPos) + let distance2 = abs(tipPos - halfPos) + + fpc.move(to: .full, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: CGPoint(x: 0.0, y: 0.0)), 0.3) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: CGPoint(x: 0.0, y: distance1 * 0.5)), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: CGPoint(x: 0.0, y: distance1)), 0.0) + + fpc.move(to: .half, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: CGPoint(x: 0.0, y: 0.0)), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos + distance1 * 0.5, with: CGPoint(x: 0.0, y: -0.5 * distance1)), 0.3 * 0.5) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: fullPos, with: CGPoint(x: 0.0, y: -1 * distance1)), 0.3) + + fpc.move(to: .tip, animated: false) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: tipPos, with: CGPoint(x: 0.0, y: 0.0)), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos + distance2 * 0.5, with: CGPoint(x: 0.0, y: -0.5 * distance2)), 0.0) + XCTAssertEqual(fpc.floatingPanel.getBackdropAlpha(at: halfPos, with: CGPoint(x: 0.0, y: -1 * distance2)), 0.0) + } + + func test_targetPosition_1positions() { + class FloatingPanelLayout1Positions: FloatingPanelTestLayout { + let initialPosition: FloatingPanelPosition = .full + let supportedPositions: Set = [.full] + } + let delegate = FloatingPanelTestDelegate() + delegate.layout = FloatingPanelLayout1Positions() + + let fpc = FloatingPanelController(delegate: delegate) + fpc.showForTest() + + let fullPos = fpc.originYOfSurface(for: .full) + + fpc.move(to: .full, animated: false) + assertTargetPosition(fpc.floatingPanel, with: [ + (#line, fullPos - 10.0, CGPoint(x: 0.0, y: 1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -1000.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: -100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: 0.0), .full), + (#line, fullPos, CGPoint(x: 0.0, y: 100.0), .full), // redirect + (#line, fullPos, CGPoint(x: 0.0, y: 1000.0), .full), // redirect + (#line, fullPos + 10.0, CGPoint(x: 0.0, y: 100.0), .full), // redirect + ]) + } + func test_targetPosition_2positions() { class FloatingPanelLayout2Positions: FloatingPanelTestLayout { let initialPosition: FloatingPanelPosition = .half