diff --git a/tests/unit/TransitionWithPresentationTests.swift b/tests/unit/TransitionWithPresentationTests.swift index d0888f4..6f8ba7e 100644 --- a/tests/unit/TransitionWithPresentationTests.swift +++ b/tests/unit/TransitionWithPresentationTests.swift @@ -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() } @@ -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) { @@ -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 + } + 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) {