forked from RxSwiftCommunity/RxWebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRxWebKitTests.swift
153 lines (125 loc) · 6.12 KB
/
RxWebKitTests.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
import WebKit
import Quick
import Nimble
import RxSwift
import RxTest
@testable import RxWebKit
class RxWebKitTests: QuickSpec {
override func spec() {
var scheduler: TestScheduler!
var sut: WKWebView!
let html = """
<!DOCTYPE html>
<html>
<head>
<title>RxWebKit</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
"""
beforeEach {
scheduler = TestScheduler(initialClock: 0)
sut = WKWebView(frame: CGRect.zero)
}
afterEach {
scheduler = nil
sut = nil
}
itBehavesLike(HasEventsBehavior<String?>.self) {
HasEventsBehaviorContext(scheduler, sut.rx.title, "")
}
itBehavesLike(HasEventsBehavior<Bool>.self) {
sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
return HasEventsBehaviorContext(scheduler, sut.rx.loading, true)
}
itBehavesLike(HasEventsBehavior<Bool>.self) {
HasEventsBehaviorContext(scheduler, sut.rx.loading, false)
}
itBehavesLike(HasEventsBehavior<Double>.self) {
HasEventsBehaviorContext(scheduler, sut.rx.estimatedProgress, 0.0)
}
itBehavesLike(HasEventsBehavior<Double>.self) {
sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
return HasEventsBehaviorContext(scheduler, sut.rx.estimatedProgress, 0.1)
}
itBehavesLike(HasEventsBehavior<Bool>.self) {
HasEventsBehaviorContext(scheduler, sut.rx.canGoBack, false)
}
itBehavesLike(HasEventsBehavior<Bool>.self) {
HasEventsBehaviorContext(scheduler, sut.rx.canGoForward, false)
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didTerminate) {
sut.navigationDelegate?.webViewWebContentProcessDidTerminate?(sut)
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didCommitNavigation) {
let navigation = sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
sut.navigationDelegate?.webView?(sut, didCommit: navigation)
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didFinishNavigation) {
let navigation = sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
sut.navigationDelegate?.webView?(sut, didFinish: navigation)
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didReceiveServerRedirectForProvisionalNavigation) {
let navigation = sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
sut.navigationDelegate?.webView?(sut, didReceiveServerRedirectForProvisionalNavigation: navigation)
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didFailNavigation) {
let navigation = sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
sut.navigationDelegate?.webView?(sut, didFail: navigation, withError: TestError.didFailNavigation)
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didFailProvisionalNavigation) {
let navigation = sut.loadHTMLString(html, baseURL: Bundle.main.resourceURL)
sut.navigationDelegate?.webView?(sut, didFailProvisionalNavigation: navigation, withError: TestError.didFailProvisionalNavigation)
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .didReceiveChallenge) {
let protectionSpace = URLProtectionSpace(host: "fake", port: 8443, protocol: nil, realm: nil, authenticationMethod: nil)
let credential = URLCredential(user: "bad-user", password: "bad-password", persistence: URLCredential.Persistence.forSession)
let sender = MockURLAuthenticationChallengeSender()
let challenge = URLAuthenticationChallenge(protectionSpace: protectionSpace, proposedCredential: credential, previousFailureCount: 0, failureResponse:nil, error: TestError.didReceiveChallenge, sender: sender)
sut.navigationDelegate?.webView?(sut, didReceive: challenge, completionHandler: { (_, _) in })
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .decidePolicyNavigationAction) {
let delegate:WKNavigationDelegate? = sut.navigationDelegate
delegate?.webView?(sut, decidePolicyFor: WKNavigationAction(), decisionHandler: { (_) in })
}
}
itBehavesLike(ForwardsEventsBehavior.self) {
ForwardsEventsBehaviorContext(sut, scheduler, .decidePolicyNavigationResponse) {
let delegate:WKNavigationDelegate? = sut.navigationDelegate
delegate?.webView?(sut, decidePolicyFor: WKNavigationResponse(), decisionHandler: { (_) in })
}
}
}
}
enum TestError: Error {
case didFailNavigation
case didFailProvisionalNavigation
case didReceiveChallenge
}
@objc class MockURLAuthenticationChallengeSender: NSObject, URLAuthenticationChallengeSender {
override func `self`() -> Self {
return self
}
override init() {}
func use(_ credential: URLCredential, for challenge: URLAuthenticationChallenge) { }
func continueWithoutCredential(for challenge: URLAuthenticationChallenge) {}
func cancel(_ challenge: URLAuthenticationChallenge) { }
}