Skip to content

Commit

Permalink
Create RushingForwardTargetNotchPolicyTests
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanzanella committed Apr 21, 2021
1 parent 9043fd8 commit 5538766
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions OverlayContainer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
E78486542201EA9400DE64C3 /* OverlayNotchHeightTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E78485C42201E66500DE64C3 /* OverlayNotchHeightTest.swift */; };
E791BAA4225F284800124D27 /* GroundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = E791BAA3225F284800124D27 /* GroundView.swift */; };
E791BAAD225F771400124D27 /* CGFloat+Round.swift in Sources */ = {isa = PBXBuildFile; fileRef = E791BAAC225F771400124D27 /* CGFloat+Round.swift */; };
E79AE411262F095000BFFBF3 /* RushingForwardTargetNotchPolicyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E79AE40B262F094600BFFBF3 /* RushingForwardTargetNotchPolicyTests.swift */; };
E79D75E126035ED900C942F7 /* Quick.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = E79D75DF26035ED900C942F7 /* Quick.xcframework */; };
E79D75E226035ED900C942F7 /* Nimble.xcframework in Frameworks */ = {isa = PBXBuildFile; fileRef = E79D75E026035ED900C942F7 /* Nimble.xcframework */; };
E7A6A3AC24460D1A00E51F7A /* OverlayContainerSheetDismissalPolicyTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = E7A6A3AA24460B5E00E51F7A /* OverlayContainerSheetDismissalPolicyTest.swift */; };
Expand Down Expand Up @@ -117,6 +118,7 @@
E78A7A582603424B0088569A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
E791BAA3225F284800124D27 /* GroundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GroundView.swift; sourceTree = "<group>"; };
E791BAAC225F771400124D27 /* CGFloat+Round.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CGFloat+Round.swift"; sourceTree = "<group>"; };
E79AE40B262F094600BFFBF3 /* RushingForwardTargetNotchPolicyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RushingForwardTargetNotchPolicyTests.swift; sourceTree = "<group>"; };
E79D75DF26035ED900C942F7 /* Quick.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = Quick.xcframework; path = Carthage/Build/Quick.xcframework; sourceTree = "<group>"; };
E79D75E026035ED900C942F7 /* Nimble.xcframework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcframework; name = Nimble.xcframework; path = Carthage/Build/Nimble.xcframework; sourceTree = "<group>"; };
E7A6A3AA24460B5E00E51F7A /* OverlayContainerSheetDismissalPolicyTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OverlayContainerSheetDismissalPolicyTest.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -269,6 +271,7 @@
E78485C32201E66500DE64C3 /* OverlayInsertionTest.swift */,
E78485C42201E66500DE64C3 /* OverlayNotchHeightTest.swift */,
E7A6A3AA24460B5E00E51F7A /* OverlayContainerSheetDismissalPolicyTest.swift */,
E79AE40B262F094600BFFBF3 /* RushingForwardTargetNotchPolicyTests.swift */,
E78485C52201E66500DE64C3 /* Info.plist */,
);
path = Tests;
Expand Down Expand Up @@ -445,6 +448,7 @@
files = (
E7A6A3AC24460D1A00E51F7A /* OverlayContainerSheetDismissalPolicyTest.swift in Sources */,
E78486522201EA9400DE64C3 /* OverlayScrollViewDelegateTest.swift in Sources */,
E79AE411262F095000BFFBF3 /* RushingForwardTargetNotchPolicyTests.swift in Sources */,
E78486532201EA9400DE64C3 /* OverlayInsertionTest.swift in Sources */,
E78486542201EA9400DE64C3 /* OverlayNotchHeightTest.swift in Sources */,
);
Expand Down
117 changes: 117 additions & 0 deletions Tests/RushingForwardTargetNotchPolicyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//
// RushingForwardTargetNotchPolicyTests.swift
// OverlayContainer
//
// Created by Gaétan Zanella on 20/04/2021.
// Copyright © 2021 Gaétan Zanella. All rights reserved.
//

import Quick
import Nimble
@testable import OverlayContainer

private struct TargetContext: OverlayContainerContextTargetNotchPolicy {

var heightByIndex: [Int: CGFloat] = [:]
var isDragging = false
var velocity: CGPoint = .zero
var overlayTranslationHeight: CGFloat = 0.0
var reachableIndexes: [Int] = []
var notchIndexes: Range<Int> {
0..<heightByIndex.count
}

let overlayViewController = UIViewController()

func height(forNotchAt index: Int) -> CGFloat {
heightByIndex[index] ?? 0.0
}
}

class RushingForwardTargetNotchPolicyTests: QuickSpec {

override func spec() {

var context: TargetContext!
var policy: RushingForwardTargetNotchPolicy!

beforeEach {
context = TargetContext()
policy = RushingForwardTargetNotchPolicy()
}

it("should target expected index when stationnary") {
context.heightByIndex = [
0: 100.0,
1: 200.0,
2: 300.0,
]
context.reachableIndexes = Array(context.notchIndexes)
context.velocity = .zero
let expectedIndexByHeight: [CGFloat: Int] = [
0.0: 0,
50.0: 0,
100.0: 0,
149.0: 0,
151.0: 1,
200.0: 1,
249.0: 1,
251.0: 2,
300.0: 2,
310.0: 2,
100000.0: 2,
]
expectedIndexByHeight.forEach { height, index in
context.overlayTranslationHeight = height
let target = policy.targetNotchIndex(using: context)
XCTAssertEqual(target, index, "\(target) for height:\(height) does notch match \(index)")
}
}

it("should bypass indexes at high velocity") {
context.heightByIndex = [
0: 100.0,
1: 200.0,
2: 300.0,
]
context.reachableIndexes = Array(context.notchIndexes)
let expectedIndexByVelocity: [(CGPoint, CGFloat, Int)] = [
(CGPoint(x: 0.0, y: -100.0), 125, 0),
(CGPoint(x: 0.0, y: -200.0), 125, 0),
(CGPoint(x: 0.0, y: -500.0), 125, 1),
(CGPoint(x: 0.0, y: 100.0), 251, 2),
(CGPoint(x: 0.0, y: 200.0), 251, 2),
(CGPoint(x: 0.0, y: 500.0), 251, 1),
]
expectedIndexByVelocity.forEach { velocity, height, index in
context.velocity = velocity
context.overlayTranslationHeight = height
policy.minimumDuration = 0.0
policy.minimumVelocity = 400.0
let target = policy.targetNotchIndex(using: context)
XCTAssertEqual(target, index, "\(target) for velocity:(\(velocity.x), \(velocity.y)) does notch match \(index)")
}
}

it("should not target disabled indexes") {
context.heightByIndex = [
0: 100.0,
1: 200.0,
2: 300.0,
]
context.reachableIndexes = [1, 2]
context.velocity = .zero
let expectedIndexByHeight: [CGFloat: Int] = [
0.0: 1,
50.0: 1,
100.0: 1,
149.0: 1,
]
expectedIndexByHeight.forEach { height, index in
context.overlayTranslationHeight = height
let target = policy.targetNotchIndex(using: context)
XCTAssertEqual(target, index, "\(target) for height:\(height) does notch match \(index)")
}
}
}
}

0 comments on commit 5538766

Please sign in to comment.