-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathViewControllerTests.swift
139 lines (97 loc) · 3 KB
/
ViewControllerTests.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
// Copyright © 2021 Saleem Abdulrasool <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause
import XCTest
@testable import SwiftWin32
final class ViewControllerTests: XCTestCase {
func testLazyViewLoading() {
let vc = ViewController()
XCTAssertNil(vc.viewIfLoaded)
XCTAssertFalse(vc.isViewLoaded)
_ = vc.view
XCTAssertNotNil(vc.viewIfLoaded)
XCTAssertTrue(vc.isViewLoaded)
}
func testManualViewLoading() {
let vc = ViewController()
XCTAssertNil(vc.viewIfLoaded)
XCTAssertFalse(vc.isViewLoaded)
vc.loadViewIfNeeded()
XCTAssertNotNil(vc.viewIfLoaded)
XCTAssertTrue(vc.isViewLoaded)
}
func testTitleGetterAndSetter() {
let vc = ViewController()
// XCTAssertNil(vc.title) // This is currently failing, but the initial value of `title` should be `nil`
vc.title = "Title"
XCTAssertEqual(vc.title, "Title")
}
func testValueOfDisablesAutomaticKeyboardDismissal() {
let vc = MockViewController()
vc.modalPresentationStyleGetter = {
return .automatic
}
XCTAssertFalse(vc.disablesAutomaticKeyboardDismissal)
vc.modalPresentationStyleGetter = {
return .formSheet
}
XCTAssertTrue(vc.disablesAutomaticKeyboardDismissal)
}
func testNextResponder() {
let expectedResult = View(frame: .zero)
let vc = ViewController()
XCTAssertNil(vc.next)
expectedResult.addSubview(vc.view)
XCTAssert(vc.next === expectedResult)
}
func testViewDidLoadMethodCalledAfterLazyViewLoad() {
let vc = MockViewController()
let expectation = self.expectation(description: "viewDidLoad should be called")
vc.viewDidLoadBlock = {
expectation.fulfill()
}
_ = vc.view
wait(for: [expectation], timeout: 0.1)
}
func testViewDidLoadMethodCalledAfterManualViewLoad() {
let vc = MockViewController()
let expectation = self.expectation(description: "viewDidLoad should be called")
vc.viewDidLoadBlock = {
expectation.fulfill()
}
vc.loadViewIfNeeded()
wait(for: [expectation], timeout: 0.1)
}
func testViewDidLoadMethodNotCalledIfViewNotLoaded() {
let vc = MockViewController()
let expectation = self.expectation(description: "viewDidLoad should not be called")
expectation.isInverted = true
vc.viewDidLoadBlock = {
expectation.fulfill()
}
wait(for: [expectation], timeout: 0.1)
}
}
final class MockViewController: ViewController {
var modalPresentationStyleGetter: () -> ModalPresentationStyle = {
XCTFail("Not implemented")
return .automatic
}
var modalPresentationStyleSetter: (ModalPresentationStyle) -> Void = { _ in
XCTFail("Not implemented")
}
var viewDidLoadBlock: () -> Void = {
XCTFail("Not implemented")
}
override var modalPresentationStyle: ModalPresentationStyle {
get {
return modalPresentationStyleGetter()
}
set {
modalPresentationStyleSetter(newValue)
}
}
override func viewDidLoad() {
super.viewDidLoad()
viewDidLoadBlock()
}
}