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

Commit ac0272c

Browse files
author
Jeff Verkoeyen
committed
Add unit tests verifying that the presented controller's frame matches the expected frame.
1 parent ab3147f commit ac0272c

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

tests/unit/TransitionWithPresentationTests.swift

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ class TransitionWithPresentationTests: XCTestCase {
3232

3333
func testPresentationControllerIsQueriedAndCompletesWithoutAnimation() {
3434
let presentedViewController = UIViewController()
35-
presentedViewController.mdm_transitionController.transition =
36-
PresentationTransition(presentationControllerType: TestingPresentationController.self)
35+
presentedViewController.mdm_transitionController.transition = PresentationTransition()
3736

3837
let didComplete = expectation(description: "Did complete")
39-
window.rootViewController!.present(presentedViewController, animated: true) {
38+
window.rootViewController!.present(presentedViewController, animated: false) {
4039
didComplete.fulfill()
4140
}
4241

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

4847
func testPresentationControllerIsQueriedAndCompletesWithAnimation() {
4948
let presentedViewController = UIViewController()
50-
presentedViewController.mdm_transitionController.transition =
51-
PresentationTransition(presentationControllerType: TransitionPresentationController.self)
49+
presentedViewController.mdm_transitionController.transition = PresentationTransition()
5250

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

5856
waitForExpectations(timeout: 0.5)
5957

60-
XCTAssert(presentedViewController.presentationController is TransitionPresentationController)
58+
XCTAssert(presentedViewController.presentationController is TestingPresentationController)
59+
}
60+
61+
func testPresentedFrameMatchesWindowFrame() {
62+
let presentedViewController = UIViewController()
63+
let transition = InstantCompletionTransition()
64+
presentedViewController.transitionController.transition = transition
65+
66+
let didComplete = expectation(description: "Did complete")
67+
window.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
68+
window.rootViewController!.present(presentedViewController, animated: true) {
69+
didComplete.fulfill()
70+
}
71+
72+
waitForExpectations(timeout: 0.1)
73+
74+
XCTAssertEqual(window.rootViewController!.presentedViewController, presentedViewController)
75+
XCTAssertEqual(window.rootViewController!.presentedViewController?.view.bounds, window.bounds)
76+
}
77+
78+
func testPresentedFrameMatchesPresentationFrame() {
79+
let presentedViewController = UIViewController()
80+
let transition = PresentationTransition()
81+
transition.presentationFrame = CGRect(x: 100, y: 30, width: 50, height: 70)
82+
presentedViewController.transitionController.transition = transition
83+
84+
let didComplete = expectation(description: "Did complete")
85+
window.frame = CGRect(x: 0, y: 0, width: 300, height: 200)
86+
window.rootViewController!.present(presentedViewController, animated: true) {
87+
didComplete.fulfill()
88+
}
89+
90+
waitForExpectations(timeout: 0.1)
91+
92+
XCTAssertEqual(window.rootViewController!.presentedViewController, presentedViewController)
93+
XCTAssertEqual(window.rootViewController!.presentedViewController?.view.frame,
94+
transition.presentationFrame)
6195
}
6296
}
6397

6498
final class TestingPresentationController: UIPresentationController {
99+
var presentationFrame: CGRect?
100+
override var frameOfPresentedViewInContainerView: CGRect {
101+
if let presentationFrame = presentationFrame {
102+
return presentationFrame
103+
}
104+
return super.frameOfPresentedViewInContainerView
105+
}
65106
}
66107

67108
final class PresentationTransition: NSObject, TransitionWithPresentation {
68-
let presentationControllerType: UIPresentationController.Type
69-
init(presentationControllerType: UIPresentationController.Type) {
70-
self.presentationControllerType = presentationControllerType
71-
72-
super.init()
73-
}
109+
var presentationFrame: CGRect?
74110

75111
func defaultModalPresentationStyle() -> UIModalPresentationStyle {
76112
return .custom
77113
}
78114

79115
func presentationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController?) -> UIPresentationController? {
80-
return presentationControllerType.init(presentedViewController: presented, presenting: presenting)
116+
let presentationController =
117+
TestingPresentationController(presentedViewController: presented, presenting: presenting)
118+
presentationController.presentationFrame = presentationFrame
119+
return presentationController
81120
}
82121

83122
func start(with context: TransitionContext) {

0 commit comments

Comments
 (0)