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

Add unit tests verifying that the presented controller's frame matches the expected frame #58

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 89 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.mdm_transitionController.transition =
PresentationTransition(presentationControllerType: TestingPresentationController.self)
presentedViewController.mdm_transitionController.transition = TestingPresentationTransition()

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.mdm_transitionController.transition =
PresentationTransition(presentationControllerType: TransitionPresentationController.self)
presentedViewController.mdm_transitionController.transition = TestingPresentationTransition()

let didComplete = expectation(description: "Did complete")
window.rootViewController!.present(presentedViewController, animated: true) {
Expand All @@ -57,27 +55,105 @@ 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.rootViewController!.view.bounds)
}

func testPresentedFrameMatchesPresentationFrame() {
let presentedViewController = UIViewController()
let transition = TestingPresentationTransition()
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)
}

func testNoFramesModifiedWhenThereIsAPresentationView() {
let presentedViewController = UIViewController()
let transition = TestingPresentationTransition()
let presentationFrame = CGRect(x: 0, y: 0, width: 100, height: 100)
let presentationView = UIView(frame: presentationFrame)
transition.presentationView = presentationView
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(presentationView.frame, presentationFrame)
XCTAssertEqual(presentedViewController.view.frame, UIScreen.main.bounds)
}
}

final class TestingPresentationController: UIPresentationController {
}
var presentationFrame: CGRect?
var presentationView: UIView?
override var frameOfPresentedViewInContainerView: CGRect {
if let presentationFrame = presentationFrame {
return presentationFrame
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hurts my head. I need to learn more swift.

return super.frameOfPresentedViewInContainerView
}

final class PresentationTransition: NSObject, TransitionWithPresentation {
let presentationControllerType: UIPresentationController.Type
init(presentationControllerType: UIPresentationController.Type) {
self.presentationControllerType = presentationControllerType
override var presentedView: UIView? {
return presentationView
}

override func presentationTransitionWillBegin() {
super.presentationTransitionWillBegin()

super.init()
if let presentationView = presentationView {
containerView?.addSubview(presentationView)
}
}
}

final class TestingPresentationTransition: NSObject, TransitionWithPresentation {
var presentationFrame: CGRect?
var presentationView: UIView?

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
presentationController.presentationView = presentationView
return presentationController
}

func start(with context: TransitionContext) {
Expand Down