Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Add unit tests verifying that the presented controller's frame matche…
Browse files Browse the repository at this point in the history
…s the expected frame.
  • Loading branch information
Jeff Verkoeyen committed Nov 13, 2017
1 parent 2564bfd commit 9d17994
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions tests/unit/TransitionWithPresentationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ class TransitionWithPresentationTests: XCTestCase {

func testPresentationControllerIsQueriedAndCompletesWithoutAnimation() {
let presentedViewController = UIViewController()
presentedViewController.transitionController.transition =
PresentationTransition(presentationControllerType: TestingPresentationController.self)
presentedViewController.transitionController.transition = PresentationTransition()

let didComplete = expectation(description: "Did complete")
window.rootViewController!.present(presentedViewController, animated: true) {
window.rootViewController!.present(presentedViewController, animated: false) {
didComplete.fulfill()
}

Expand All @@ -47,8 +46,7 @@ class TransitionWithPresentationTests: XCTestCase {

func testPresentationControllerIsQueriedAndCompletesWithAnimation() {
let presentedViewController = UIViewController()
presentedViewController.transitionController.transition =
PresentationTransition(presentationControllerType: TransitionPresentationController.self)
presentedViewController.transitionController.transition = PresentationTransition()

let didComplete = expectation(description: "Did complete")
window.rootViewController!.present(presentedViewController, animated: true) {
Expand All @@ -57,27 +55,68 @@ class TransitionWithPresentationTests: XCTestCase {

waitForExpectations(timeout: 0.5)

XCTAssert(presentedViewController.presentationController is TransitionPresentationController)
XCTAssert(presentedViewController.presentationController is TestingPresentationController)
}

func testPresentedFrameMatchesWindowFrame() {
let presentedViewController = UIViewController()
let transition = InstantCompletionTransition()
presentedViewController.transitionController.transition = transition

let didComplete = expectation(description: "Did complete")
window.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
window.rootViewController!.present(presentedViewController, animated: true) {
didComplete.fulfill()
}

waitForExpectations(timeout: 0.1)

XCTAssertEqual(window.rootViewController!.presentedViewController, presentedViewController)
XCTAssertEqual(window.rootViewController!.presentedViewController?.view.bounds, window.bounds)
}

func testPresentedFrameMatchesPresentationFrame() {
let presentedViewController = UIViewController()
let transition = PresentationTransition()
transition.presentationFrame = CGRect(x: 100, y: 30, width: 50, height: 70)
presentedViewController.transitionController.transition = transition

let didComplete = expectation(description: "Did complete")
window.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
window.rootViewController!.present(presentedViewController, animated: true) {
didComplete.fulfill()
}

waitForExpectations(timeout: 0.1)

XCTAssertEqual(window.rootViewController!.presentedViewController, presentedViewController)
XCTAssertEqual(window.rootViewController!.presentedViewController?.view.frame,
transition.presentationFrame)
}
}

final class TestingPresentationController: UIPresentationController {
var presentationFrame: CGRect?
override var frameOfPresentedViewInContainerView: CGRect {
if let presentationFrame = presentationFrame {
return presentationFrame
}
return super.frameOfPresentedViewInContainerView
}
}

final class PresentationTransition: NSObject, TransitionWithPresentation {
let presentationControllerType: UIPresentationController.Type
init(presentationControllerType: UIPresentationController.Type) {
self.presentationControllerType = presentationControllerType

super.init()
}
var presentationFrame: CGRect?

func defaultModalPresentationStyle() -> UIModalPresentationStyle {
return .custom
}

func presentationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController?) -> UIPresentationController? {
return presentationControllerType.init(presentedViewController: presented, presenting: presenting)
let presentationController =
TestingPresentationController(presentedViewController: presented, presenting: presenting)
presentationController.presentationFrame = presentationFrame
return presentationController
}

func start(with context: TransitionContext) {
Expand Down

0 comments on commit 9d17994

Please sign in to comment.