-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TabManagerNavDelegateTests.swift
249 lines (196 loc) · 10 KB
/
TabManagerNavDelegateTests.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import XCTest
import WebKit
@testable import Client
class TabManagerNavDelegateTests: XCTestCase {
let navigation = WKNavigation()
func test_webViewDidCommit_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didCommit: navigation)
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidCommit])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDidCommit])
}
func test_webViewDidFail_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didFail: navigation, withError: anyError())
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidFail])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDidFail])
}
func test_webViewDidFailProvisionalNavigation_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didFailProvisionalNavigation: navigation, withError: anyError())
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidFailProvisionalNavigation])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDidFailProvisionalNavigation])
}
func test_webViewDidFinish_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didFinish: navigation)
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidFinish])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDidFinish])
}
func test_webViewWebContentProcessDidTerminate_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webViewWebContentProcessDidTerminate(anyWebView())
XCTAssertEqual(delegate1.receivedMessages, [.webViewWebContentProcessDidTerminate])
XCTAssertEqual(delegate2.receivedMessages, [.webViewWebContentProcessDidTerminate])
}
func test_webViewDidReceiveAuthenticationChallenge_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didReceive: anyAuthenticationChallenge()) { (_, _) in }
// This message is send only to the first delegate that respond to to authentication challenge (BVC)
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidReceiveAuthenticationChallenge])
XCTAssertEqual(delegate2.receivedMessages, [])
}
func test_webViewDidReceiveServerRedirectForProvisionalNavigation_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didReceiveServerRedirectForProvisionalNavigation: navigation)
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidReceiveServerRedirectForProvisionalNavigation])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDidReceiveServerRedirectForProvisionalNavigation])
}
func test_webViewDidStartProvisionalNavigation_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(), didStartProvisionalNavigation: navigation)
XCTAssertEqual(delegate1.receivedMessages, [.webViewDidStartProvisionalNavigation])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDidStartProvisionalNavigation])
}
func test_webViewDecidePolicyFor_actionPolicy_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(),
decidePolicyFor: WKNavigationAction(),
decisionHandler: { _ in })
XCTAssertEqual(delegate1.receivedMessages, [.webViewDecidePolicyWithActionPolicy])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDecidePolicyWithActionPolicy])
}
func test_webViewDecidePolicyFor_responsePolicy_sendsCorrectMessage() {
let subjectConstructor = createSubject()
let subject = subjectConstructor.subject
let delegate1 = subjectConstructor.delegate1
let delegate2 = subjectConstructor.delegate2
subject.insert(delegate1)
subject.insert(delegate2)
subject.webView(anyWebView(),
decidePolicyFor: WKNavigationResponse(),
decisionHandler: { _ in })
XCTAssertEqual(delegate1.receivedMessages, [.webViewDecidePolicyWithResponsePolicy])
XCTAssertEqual(delegate2.receivedMessages, [.webViewDecidePolicyWithResponsePolicy])
}
}
// MARK: - Helpers
private extension TabManagerNavDelegateTests {
struct Subject {
let subject: TabManagerNavDelegate
let delegate1: WKNavigationDelegateSpy
let delegate2: WKNavigationDelegateSpy
}
func createSubject(file: StaticString = #file, line: UInt = #line) -> Subject {
let subject = TabManagerNavDelegate()
let delegate1 = WKNavigationDelegateSpy()
let delegate2 = WKNavigationDelegateSpy()
trackForMemoryLeaks(subject, file: file, line: line)
trackForMemoryLeaks(delegate1, file: file, line: line)
trackForMemoryLeaks(delegate2, file: file, line: line)
return Subject(subject: subject, delegate1: delegate1, delegate2: delegate2)
}
func anyWebView() -> WKWebView {
return WKWebView(frame: CGRect(width: 100, height: 100))
}
func anyError() -> NSError {
return NSError(domain: "any error", code: 0)
}
func anyAuthenticationChallenge() -> URLAuthenticationChallenge {
return URLAuthenticationChallenge()
}
}
// MARK: - WKNavigationDelegateSpy
private class WKNavigationDelegateSpy: NSObject, WKNavigationDelegate {
enum Message {
case webViewDidCommit
case webViewDidFail
case webViewDidFailProvisionalNavigation
case webViewDidFinish
case webViewWebContentProcessDidTerminate
case webViewDidReceiveAuthenticationChallenge
case webViewDidReceiveServerRedirectForProvisionalNavigation
case webViewDidStartProvisionalNavigation
case webViewDecidePolicyWithActionPolicy
case webViewDecidePolicyWithResponsePolicy
}
var receivedMessages = [Message]()
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
receivedMessages.append(.webViewDidCommit)
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
receivedMessages.append(.webViewDidFail)
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
receivedMessages.append(.webViewDidFailProvisionalNavigation)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
receivedMessages.append(.webViewDidFinish)
}
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
receivedMessages.append(.webViewWebContentProcessDidTerminate)
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
receivedMessages.append(.webViewDidReceiveAuthenticationChallenge)
}
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
receivedMessages.append(.webViewDidReceiveServerRedirectForProvisionalNavigation)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
receivedMessages.append(.webViewDidStartProvisionalNavigation)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
receivedMessages.append(.webViewDecidePolicyWithActionPolicy)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
receivedMessages.append(.webViewDecidePolicyWithResponsePolicy)
}
}