forked from applidium/OverlayContainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverlayNotchHeightTest.swift
181 lines (159 loc) · 6.7 KB
/
OverlayNotchHeightTest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//
// OverlayNotchHeightTest.swift
// OverlayContainer_Tests
//
// Created by Gaétan Zanella on 24/12/2018.
// Copyright © 2018 Gaétan Zanella. All rights reserved.
//
import Nimble
import Quick
import OverlayContainer
private enum Notch: Int, CaseIterable {
case minimum, medium, maximum
}
private enum NewNotch: Int, CaseIterable {
case minimum, medium
}
private class OverlayContainerDelegateImplementation: OverlayContainerViewControllerDelegate {
var usesNewNotches = false
// MARK: - Public
func height(for notch: Notch) -> CGFloat {
switch notch {
case .minimum:
return 0
case .medium:
return 50
case .maximum:
return 100
}
}
func height(forNew notch: NewNotch) -> CGFloat {
switch notch {
case .minimum:
return 10
case .medium:
return 200
}
}
// MARK: - OverlayContainerViewControllerDelegate
func numberOfNotches(in containerViewController: OverlayContainerViewController) -> Int {
if usesNewNotches {
return NewNotch.allCases.count
}
return Notch.allCases.count
}
func overlayContainerViewController(_ containerViewController: OverlayContainerViewController,
heightForNotchAt index: Int,
availableSpace: CGFloat) -> CGFloat {
if usesNewNotches {
return height(forNew: NewNotch.allCases[index])
}
return height(for: Notch.allCases[index])
}
}
class OverlayNotchHeightTest: QuickSpec {
// MARK: - QuickSpec
override func spec() {
var overlayContainer: OverlayContainerViewController!
var delegate: OverlayContainerDelegateImplementation!
var overlay: UIViewController!
beforeEach {
overlayContainer = OverlayContainerViewController(style: .flexibleHeight)
delegate = OverlayContainerDelegateImplementation()
overlay = UIViewController()
overlayContainer.view.frame = CGRect(origin: .zero, size: CGSize(width: 300, height: 300))
}
it("should move the overlay to the specified notch") {
overlayContainer.delegate = delegate
overlayContainer.viewControllers = [overlay]
overlayContainer.loadViewIfNeeded()
Notch.allCases.forEach { notch in
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: false)
overlayContainer.view.layoutIfNeeded()
expect(overlay.view.frame.height).to(equal(delegate.height(for: notch)))
}
}
it("should invalidate the current notches") {
overlayContainer.delegate = delegate
overlayContainer.viewControllers = [overlay]
overlayContainer.loadViewIfNeeded()
Notch.allCases.forEach { notch in
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: false)
overlayContainer.view.layoutIfNeeded()
expect(overlay.view.frame.height).to(equal(delegate.height(for: notch)))
}
delegate.usesNewNotches = true
overlayContainer.invalidateNotchHeights()
NewNotch.allCases.forEach { notch in
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: false)
overlayContainer.view.layoutIfNeeded()
expect(overlay.view.frame.height).to(equal(delegate.height(forNew: notch)))
}
}
it("should call completion callback after animation is finished") {
overlayContainer.delegate = delegate
overlayContainer.viewControllers = [overlay]
overlayContainer.loadViewIfNeeded()
overlayContainer.view.layoutIfNeeded()
Notch.allCases.forEach { notch in
waitUntil(timeout: DispatchTimeInterval.seconds(1)) { done in
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: true) {
expect(true).to(beTrue())
done()
}
overlayContainer.view.layoutIfNeeded()
}
}
}
it("should call completion callback when animation is set to false") {
overlayContainer.delegate = delegate
overlayContainer.viewControllers = [overlay]
overlayContainer.loadViewIfNeeded()
overlayContainer.view.layoutIfNeeded()
Notch.allCases.forEach { notch in
waitUntil(timeout: DispatchTimeInterval.seconds(1)) { done in
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: false) {
expect(true).to(beTrue())
done()
}
overlayContainer.view.layoutIfNeeded()
}
}
}
it("should call completion callbacks in order") {
overlayContainer.delegate = delegate
overlayContainer.viewControllers = [overlay]
overlayContainer.loadViewIfNeeded()
overlayContainer.view.layoutIfNeeded()
waitUntil(timeout: DispatchTimeInterval.seconds(1)) { done in
var i = 0
overlayContainer.moveOverlay(toNotchAt: Notch.minimum.rawValue, animated: false) {
i += 1
}
overlayContainer.moveOverlay(toNotchAt: Notch.medium.rawValue, animated: false) {
i += 1
expect(i).to(equal(2))
expect(overlay.view.frame.height).to(equal(delegate.height(for: .medium)))
done()
}
overlayContainer.view.layoutIfNeeded()
}
}
it("should move the overlay even if the overlay is set before the delegate") {
let notch = Notch.maximum
overlayContainer.delegate = delegate
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: false)
overlayContainer.viewControllers = [overlay]
overlayContainer.view.layoutIfNeeded()
expect(overlay.view.frame.height).to(equal(delegate.height(for: notch)))
}
it("should move the overlay even if the overlay is set after the delegate") {
let notch = Notch.maximum
overlayContainer.viewControllers = [overlay]
overlayContainer.delegate = delegate
overlayContainer.moveOverlay(toNotchAt: notch.rawValue, animated: false)
overlayContainer.view.layoutIfNeeded()
expect(overlay.view.frame.height).to(equal(delegate.height(for: notch)))
}
}
}