From ac0272cda0a1bd821b9a606411d8cc7d7370ef81 Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Mon, 13 Nov 2017 16:39:23 -0500 Subject: [PATCH 1/3] Add unit tests verifying that the presented controller's frame matches the expected frame. --- .../TransitionWithPresentationTests.swift | 65 +++++++++++++++---- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/tests/unit/TransitionWithPresentationTests.swift b/tests/unit/TransitionWithPresentationTests.swift index d0888f4..e4af010 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 = PresentationTransition() 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 = PresentationTransition() let didComplete = expectation(description: "Did complete") window.rootViewController!.present(presentedViewController, animated: true) { @@ -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) { From 4fcaf2933e105b6e81bc3a1fb9210c37fc20dd7f Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Mon, 13 Nov 2017 16:45:02 -0500 Subject: [PATCH 2/3] Add presentedView tests. --- .../TransitionWithPresentationTests.swift | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/unit/TransitionWithPresentationTests.swift b/tests/unit/TransitionWithPresentationTests.swift index e4af010..7d13cf1 100644 --- a/tests/unit/TransitionWithPresentationTests.swift +++ b/tests/unit/TransitionWithPresentationTests.swift @@ -93,20 +93,54 @@ class TransitionWithPresentationTests: XCTestCase { XCTAssertEqual(window.rootViewController!.presentedViewController?.view.frame, transition.presentationFrame) } + + func testNoFramesModifiedWhenThereIsAPresentationView() { + let presentedViewController = UIViewController() + let transition = PresentationTransition() + let presentationView = UIView() + 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, .zero) + 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 } + + override var presentedView: UIView? { + return presentationView + } + + override func presentationTransitionWillBegin() { + super.presentationTransitionWillBegin() + + if let presentationView = presentationView { + containerView?.addSubview(presentationView) + } + } } final class PresentationTransition: NSObject, TransitionWithPresentation { var presentationFrame: CGRect? + var presentationView: UIView? func defaultModalPresentationStyle() -> UIModalPresentationStyle { return .custom @@ -116,6 +150,7 @@ final class PresentationTransition: NSObject, TransitionWithPresentation { let presentationController = TestingPresentationController(presentedViewController: presented, presenting: presenting) presentationController.presentationFrame = presentationFrame + presentationController.presentationView = presentationView return presentationController } From 00d1a74e51e9ec0ba6b5c3fb2511f0857cbe6c47 Mon Sep 17 00:00:00 2001 From: Jeff Verkoeyen Date: Tue, 14 Nov 2017 14:16:02 -0500 Subject: [PATCH 3/3] Cleanup. --- .../unit/TransitionWithPresentationTests.swift | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/unit/TransitionWithPresentationTests.swift b/tests/unit/TransitionWithPresentationTests.swift index 7d13cf1..6f8ba7e 100644 --- a/tests/unit/TransitionWithPresentationTests.swift +++ b/tests/unit/TransitionWithPresentationTests.swift @@ -32,7 +32,7 @@ class TransitionWithPresentationTests: XCTestCase { func testPresentationControllerIsQueriedAndCompletesWithoutAnimation() { let presentedViewController = UIViewController() - presentedViewController.mdm_transitionController.transition = PresentationTransition() + presentedViewController.mdm_transitionController.transition = TestingPresentationTransition() let didComplete = expectation(description: "Did complete") window.rootViewController!.present(presentedViewController, animated: false) { @@ -46,7 +46,7 @@ class TransitionWithPresentationTests: XCTestCase { func testPresentationControllerIsQueriedAndCompletesWithAnimation() { let presentedViewController = UIViewController() - presentedViewController.mdm_transitionController.transition = PresentationTransition() + presentedViewController.mdm_transitionController.transition = TestingPresentationTransition() let didComplete = expectation(description: "Did complete") window.rootViewController!.present(presentedViewController, animated: true) { @@ -72,12 +72,13 @@ class TransitionWithPresentationTests: XCTestCase { waitForExpectations(timeout: 0.1) XCTAssertEqual(window.rootViewController!.presentedViewController, presentedViewController) - XCTAssertEqual(window.rootViewController!.presentedViewController?.view.bounds, window.bounds) + XCTAssertEqual(window.rootViewController!.presentedViewController?.view.bounds, + window.rootViewController!.view.bounds) } func testPresentedFrameMatchesPresentationFrame() { let presentedViewController = UIViewController() - let transition = PresentationTransition() + let transition = TestingPresentationTransition() transition.presentationFrame = CGRect(x: 100, y: 30, width: 50, height: 70) presentedViewController.transitionController.transition = transition @@ -96,8 +97,9 @@ class TransitionWithPresentationTests: XCTestCase { func testNoFramesModifiedWhenThereIsAPresentationView() { let presentedViewController = UIViewController() - let transition = PresentationTransition() - let presentationView = UIView() + 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 @@ -110,7 +112,7 @@ class TransitionWithPresentationTests: XCTestCase { waitForExpectations(timeout: 0.1) XCTAssertEqual(window.rootViewController!.presentedViewController, presentedViewController) - XCTAssertEqual(presentationView.frame, .zero) + XCTAssertEqual(presentationView.frame, presentationFrame) XCTAssertEqual(presentedViewController.view.frame, UIScreen.main.bounds) } } @@ -138,7 +140,7 @@ final class TestingPresentationController: UIPresentationController { } } -final class PresentationTransition: NSObject, TransitionWithPresentation { +final class TestingPresentationTransition: NSObject, TransitionWithPresentation { var presentationFrame: CGRect? var presentationView: UIView?