/
UIKitConsumerLaunchTests.swift
394 lines (310 loc) · 16.3 KB
/
UIKitConsumerLaunchTests.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//
// UIKitConsumerLaunchTests.swift
// WorkflowUIKitTests
//
// Created by Richard Gist on 5/12/21.
// Copyright © 2021 WWT and Tyler Thompson. All rights reserved.
//
import Foundation
import XCTest
import UIUTest
// Do *not* change to @testable import, we want tests driven from the consumer standpoint
import SwiftCurrent
import SwiftCurrent_UIKit
class UIKitConsumerLaunchTests: XCTestCase {
static var testCallbackCalled = false
let testCallback = {
UIKitConsumerLaunchTests.testCallbackCalled = true
}
override func setUpWithError() throws {
Self.testCallbackCalled = false
UIView.setAnimationsEnabled(false)
UIViewController.initializeTestable()
}
override func tearDownWithError() throws {
UIViewController.flushPendingTestArtifacts()
UIView.setAnimationsEnabled(true)
}
func testWorkflowCanLaunchViewController() {
class FR1: UIViewController, FlowRepresentable {
weak var _workflowPointer: AnyFlowRepresentable?
}
let flow = Workflow(FR1.self)
let root = UIViewController()
root.loadForTesting()
root.launchInto(flow)
XCTAssert(UIApplication.topViewController() is FR1)
}
func testWorkflowCanPushOntoExistingNavController() {
class FR1: UIViewController, FlowRepresentable {
weak var _workflowPointer: AnyFlowRepresentable?
}
let root = UIViewController()
root.view.backgroundColor = .blue
let nav = UINavigationController(rootViewController: root)
nav.loadForTesting()
root.launchInto(Workflow(FR1.self))
XCTAssert(UIApplication.topViewController() is FR1)
XCTAssert(nav.visibleViewController is FR1)
XCTAssert(nav.viewControllers.last is FR1)
}
func testLaunchWorkflowWithArguments() {
class FR1: UIWorkflowItem<Int, Never>, FlowRepresentable {
static var shouldLoadCalled = false
required init(with args: Int) {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
func shouldLoad() -> Bool {
FR1.shouldLoadCalled = true
return true
}
}
let root = UIViewController()
root.view.backgroundColor = .blue
root.loadForTesting()
let wf = Workflow(FR1.self)
root.launchInto(wf, args: 1)
XCTAssertUIViewControllerDisplayed(ofType: FR1.self)
XCTAssert(FR1.shouldLoadCalled)
}
func testFlowPresentsOnNavStackWhenNavHasNoRoot() {
class FR1: TestViewController { }
let nav = UINavigationController()
nav.loadForTesting()
nav.launchInto(Workflow(FR1.self))
XCTAssertUIViewControllerDisplayed(ofType: FR1.self)
XCTAssertNil(nav.mostRecentlyPresentedViewController)
XCTAssertNotNil(UIApplication.topViewController()?.navigationController)
XCTAssertEqual(UIApplication.topViewController()?.navigationController?.viewControllers.count, 1)
XCTAssert(UIApplication.topViewController()?.navigationController?.visibleViewController is FR1)
}
func testFlowPresentsOnNavStackWhenNavHasNoRootAndNavigationStackLaunchStyle() {
class FR1: TestViewController { }
let nav = UINavigationController()
nav.loadForTesting()
nav.launchInto(Workflow(FR1.self, launchStyle: .navigationStack), withLaunchStyle: .navigationStack)
XCTAssertUIViewControllerDisplayed(ofType: FR1.self)
XCTAssertNil(nav.mostRecentlyPresentedViewController)
XCTAssertNotNil(UIApplication.topViewController()?.navigationController)
XCTAssert(UIApplication.topViewController()?.navigationController === nav)
XCTAssertEqual(UIApplication.topViewController()?.navigationController?.viewControllers.count, 1)
XCTAssert(UIApplication.topViewController()?.navigationController?.visibleViewController is FR1)
}
func testWorkflowLaunchModally() {
class ExpectedModal: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
controller.loadForTesting()
rootController.launchInto(Workflow(ExpectedModal.self), withLaunchStyle: .modal)
RunLoop.current.singlePass()
XCTAssertEqual(controller.viewControllers.count, 1)
XCTAssert(rootController.mostRecentlyPresentedViewController is ExpectedModal, "mostRecentlyPresentedViewController should be ExpectedModal: \(String(describing: controller.mostRecentlyPresentedViewController))")
}
func testWorkflowLaunchModallyButSecondViewPrefersANavController() {
class ExpectedModal: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
override func viewDidAppear(_ animated: Bool) {
proceedInWorkflow()
}
}
class ExpectedModalPreferNav: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .blue
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
controller.loadForTesting()
rootController.launchInto(Workflow(ExpectedModal.self)
.thenProceed(with: ExpectedModalPreferNav.self, launchStyle: .navigationStack),
withLaunchStyle: .modal)
RunLoop.current.singlePass()
XCTAssertEqual(controller.viewControllers.count, 1)
XCTAssert(rootController.mostRecentlyPresentedViewController is ExpectedModal, "mostRecentlyPresentedViewController should be ExpectedModal: \(String(describing: controller.mostRecentlyPresentedViewController))")
XCTAssertUIViewControllerDisplayed(ofType: ExpectedModalPreferNav.self)
XCTAssertNotNil(UIApplication.topViewController()?.navigationController)
}
func testFluentWorkflowLaunchModallyButSecondViewPrefersANavController() {
class ExpectedModal: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
override func viewDidAppear(_ animated: Bool) {
proceedInWorkflow()
}
}
class ExpectedModalPreferNav: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .blue
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
controller.loadForTesting()
rootController.launchInto(
Workflow(ExpectedModal.self)
.thenProceed(with: ExpectedModalPreferNav.self,
launchStyle: .navigationStack),
withLaunchStyle: .modal)
RunLoop.current.singlePass()
XCTAssertEqual(controller.viewControllers.count, 1)
XCTAssert(rootController.mostRecentlyPresentedViewController is ExpectedModal, "mostRecentlyPresentedViewController should be ExpectedModal: \(String(describing: controller.mostRecentlyPresentedViewController))")
XCTAssertUIViewControllerDisplayed(ofType: ExpectedModalPreferNav.self)
XCTAssertNotNil(UIApplication.topViewController()?.navigationController)
}
func testWorkflowLaunchModallyButFirstViewHasANavController() {
class ExpectedModal: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
let firstView = UIViewController()
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
firstView.loadForTesting()
firstView.present(controller, animated: false)
let workflow = Workflow(ExpectedModal.self, launchStyle: .navigationStack)
rootController.launchInto(workflow, withLaunchStyle: .modal)
RunLoop.current.singlePass()
XCTAssertEqual(controller.viewControllers.count, 1)
XCTAssert(rootController.mostRecentlyPresentedViewController is UINavigationController, "mostRecentlyPresentedViewController should be UINavigationController: \(String(describing: rootController.mostRecentlyPresentedViewController))")
XCTAssertEqual((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.count, 1)
XCTAssert((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.first is ExpectedModal, "rootViewController should be ExpectedModal: \(String(describing: (rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.first))")
}
func testFluentWorkflowLaunchModallyButFirstViewHasANavController() {
class ExpectedNav: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
let firstView = UIViewController()
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
firstView.loadForTesting()
firstView.present(controller, animated: false)
let workflow = Workflow(ExpectedNav.self, launchStyle: .navigationStack)
rootController.launchInto(workflow, withLaunchStyle: .modal)
RunLoop.current.singlePass()
XCTAssertEqual(controller.viewControllers.count, 1)
XCTAssert(rootController.mostRecentlyPresentedViewController is UINavigationController, "mostRecentlyPresentedViewController should be UINavigationController: \(String(describing: rootController.mostRecentlyPresentedViewController))")
XCTAssertEqual((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.count, 1)
XCTAssert((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.first is ExpectedNav, "rootViewController should be ExpectedNav: \(String(describing: (rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.first))")
}
func testWorkflowLaunchModallyButFirstViewHasANavControllerAndThenDismiss() {
class ExpectedModal: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
controller.loadForTesting()
let workflow = Workflow(TestViewController.self)
.thenProceed(with: ExpectedModal.self, launchStyle: .modal)
rootController.launchInto(workflow)
XCTAssertUIViewControllerDisplayed(ofType: TestViewController.self)
(UIApplication.topViewController() as? TestViewController)?.proceedInWorkflow(nil)
XCTAssertUIViewControllerDisplayed(ofType: ExpectedModal.self)
workflow.abandon(animated: false, onFinish: testCallback)
XCTAssertUIViewControllerDisplayed(isInstance: rootController)
XCTAssertTrue(Self.testCallbackCalled)
}
func testWorkflowLaunchWithNavigationStack() {
class ExpectedController: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
controller.loadForTesting()
rootController.launchInto(Workflow(ExpectedController.self), withLaunchStyle: .navigationStack)
RunLoop.current.singlePass()
XCTAssertEqual(controller.viewControllers.count, 2)
XCTAssertFalse(rootController.mostRecentlyPresentedViewController is ExpectedController, "mostRecentlyPresentedViewController should not be ExpectedModal: \(String(describing: controller.mostRecentlyPresentedViewController))")
}
func testWorkflowLaunchWithNavigationStackWhenLauncherDoesNotHavNavController() {
class ExpectedController: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
rootController.loadForTesting()
rootController.launchInto(Workflow(ExpectedController.self), withLaunchStyle: .navigationStack)
RunLoop.current.singlePass()
XCTAssert(rootController.mostRecentlyPresentedViewController is UINavigationController, "mostRecentlyPresentedViewController should be nav controller: \(String(describing: rootController.mostRecentlyPresentedViewController))")
XCTAssertEqual((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.count, 1)
XCTAssert((rootController.mostRecentlyPresentedViewController as? UINavigationController)?.viewControllers.first is ExpectedController)
}
func testWorkflowLaunchesWithNavButHasAViewThatPrefersModalBecauseItCan() {
class ExpectedModal: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .green
}
required init?(coder: NSCoder) { nil }
}
class ExpectedNav: UIWorkflowItem<Never, Never>, FlowRepresentable {
required init() {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .blue
}
required init?(coder: NSCoder) { nil }
}
let rootController = UIViewController()
let controller = UINavigationController(rootViewController: rootController)
controller.loadForTesting()
rootController.launchInto(Workflow(ExpectedNav.self)
.thenProceed(with: ExpectedModal.self, launchStyle: .modal))
XCTAssertUIViewControllerDisplayed(ofType: ExpectedNav.self)
XCTAssertEqual(controller.viewControllers.count, 2)
(UIApplication.topViewController() as? ExpectedNav)?.proceedInWorkflow()
XCTAssertUIViewControllerDisplayed(ofType: ExpectedModal.self)
XCTAssertNil((UIApplication.topViewController() as? ExpectedModal)?.navigationController, "You didn't present modally")
}
}
extension UIKitConsumerLaunchTests {
class TestViewController: UIWorkflowItem<AnyWorkflow.PassedArgs, Any?>, FlowRepresentable {
var data: Any?
required init(with args: AnyWorkflow.PassedArgs) {
super.init(nibName: nil, bundle: nil)
view.backgroundColor = .red
data = args.extractArgs(defaultValue: nil)
}
required init?(coder: NSCoder) { nil }
// See important documentation on FlowRepresentable
func shouldLoad() -> Bool { true }
func next() {
proceedInWorkflow(data)
}
}
}