16
16
// limitations under the License.
17
17
//
18
18
19
- import XCTest
20
19
import Combine
20
+ import Navigation
21
+ import XCTest
22
+
21
23
@testable import DuckDuckGo_Privacy_Browser
22
24
23
- @MainActor
24
25
final class TabViewModelTests : XCTestCase {
25
26
26
27
var cancellables = Set < AnyCancellable > ( )
27
28
28
29
// MARK: - Can reload
29
30
31
+ @MainActor
30
32
func testWhenURLIsNilThenCanReloadIsFalse( ) {
31
33
let tabViewModel = TabViewModel . aTabViewModel
32
34
33
35
XCTAssertFalse ( tabViewModel. canReload)
34
36
}
35
37
38
+ @MainActor
36
39
func testWhenURLIsNotNilThenCanReloadIsTrue( ) {
37
40
let tabViewModel = TabViewModel . forTabWithURL ( . duckDuckGo)
38
41
@@ -46,19 +49,22 @@ final class TabViewModelTests: XCTestCase {
46
49
47
50
// MARK: - AddressBarString
48
51
52
+ @MainActor
49
53
func testWhenURLIsNilThenAddressBarStringIsEmpty( ) {
50
54
let tabViewModel = TabViewModel . aTabViewModel
51
55
52
56
XCTAssertEqual ( tabViewModel. addressBarString, " " )
53
57
}
54
58
59
+ @MainActor
55
60
func testWhenURLIsSetThenAddressBarIsUpdated( ) {
56
61
let urlString = " http://spreadprivacy.com "
57
- let tabViewModel = TabViewModel . forTabWithURL ( . makeURL( from: urlString) !)
62
+ let url = URL . makeURL ( from: urlString) !
63
+ let tabViewModel = TabViewModel . forTabWithURL ( url)
58
64
59
65
let addressBarStringExpectation = expectation ( description: " Address bar string " )
60
66
61
- tabViewModel. simulateLoadingCompletion ( )
67
+ tabViewModel. simulateLoadingCompletion ( url , in : tabViewModel . tab . webView )
62
68
63
69
tabViewModel. $addressBarString. debounce ( for: 0.5 , scheduler: RunLoop . main) . sink { _ in
64
70
XCTAssertEqual ( tabViewModel. addressBarString, urlString)
@@ -67,13 +73,15 @@ final class TabViewModelTests: XCTestCase {
67
73
waitForExpectations ( timeout: 1 , handler: nil )
68
74
}
69
75
76
+ @MainActor
70
77
func testWhenURLIsFileURLThenAddressBarIsFilePath( ) {
71
78
let urlString = " file:///Users/Dax/file.txt "
72
- let tabViewModel = TabViewModel . forTabWithURL ( . makeURL( from: urlString) !)
79
+ let url = URL . makeURL ( from: urlString) !
80
+ let tabViewModel = TabViewModel . forTabWithURL ( url)
73
81
74
82
let addressBarStringExpectation = expectation ( description: " Address bar string " )
75
83
76
- tabViewModel. simulateLoadingCompletion ( )
84
+ tabViewModel. simulateLoadingCompletion ( url , in : tabViewModel . tab . webView )
77
85
78
86
tabViewModel. $addressBarString. debounce ( for: 0.1 , scheduler: RunLoop . main) . sink { _ in
79
87
XCTAssertEqual ( tabViewModel. addressBarString, urlString)
@@ -83,13 +91,15 @@ final class TabViewModelTests: XCTestCase {
83
91
waitForExpectations ( timeout: 1 , handler: nil )
84
92
}
85
93
94
+ @MainActor
86
95
func testWhenURLIsDataURLThenAddressBarIsDataURL( ) {
87
96
let urlString = " data:,Hello%2C%20World%21 "
88
- let tabViewModel = TabViewModel . forTabWithURL ( . makeURL( from: urlString) !)
97
+ let url = URL . makeURL ( from: urlString) !
98
+ let tabViewModel = TabViewModel . forTabWithURL ( url)
89
99
90
100
let addressBarStringExpectation = expectation ( description: " Address bar string " )
91
101
92
- tabViewModel. simulateLoadingCompletion ( )
102
+ tabViewModel. simulateLoadingCompletion ( url , in : tabViewModel . tab . webView )
93
103
94
104
tabViewModel. $addressBarString. debounce ( for: 0.1 , scheduler: RunLoop . main) . sink { _ in
95
105
XCTAssertEqual ( tabViewModel. addressBarString, urlString)
@@ -99,6 +109,7 @@ final class TabViewModelTests: XCTestCase {
99
109
waitForExpectations ( timeout: 1 , handler: nil )
100
110
}
101
111
112
+ @MainActor
102
113
func testWhenURLIsBlobURLWithBasicAuthThenAddressBarStripsBasicAuth( ) {
103
114
let urlStrings = [ " blob:https://spoofed.domain.com%20%20%20%20%20%20%20%20%20@attacker.com " ,
104
115
" blob:ftp://another.spoofed.domain.com%20%20%20%20%20%20%20%20%20@attacker.com " ,
@@ -109,9 +120,10 @@ final class TabViewModelTests: XCTestCase {
109
120
let uuidRegex = try ! NSRegularExpression ( pattern: uuidPattern, options: [ ] )
110
121
111
122
for i in 0 ..< urlStrings. count {
112
- let tabViewModel = TabViewModel . forTabWithURL ( . makeURL( from: urlStrings [ i] ) !)
123
+ let url = URL . makeURL ( from: urlStrings [ i] ) !
124
+ let tabViewModel = TabViewModel . forTabWithURL ( url)
113
125
let addressBarStringExpectation = expectation ( description: " Address bar string " )
114
- tabViewModel. simulateLoadingCompletion ( )
126
+ tabViewModel. simulateLoadingCompletion ( url , in : tabViewModel . tab . webView )
115
127
116
128
tabViewModel. $addressBarString. debounce ( for: 0.1 , scheduler: RunLoop . main) . sink { _ in
117
129
XCTAssertTrue ( tabViewModel. addressBarString. starts ( with: expectedStarts [ i] ) )
@@ -128,12 +140,14 @@ final class TabViewModelTests: XCTestCase {
128
140
129
141
// MARK: - Title
130
142
143
+ @MainActor
131
144
func testWhenURLIsNilThenTitleIsNewTab( ) {
132
145
let tabViewModel = TabViewModel . aTabViewModel
133
146
134
- XCTAssertEqual ( tabViewModel. title, " New Tab " )
147
+ XCTAssertEqual ( tabViewModel. title, UserText . tabHomeTitle )
135
148
}
136
149
150
+ @MainActor
137
151
func testWhenTabTitleIsNotNilThenTitleReflectsTabTitle( ) async throws {
138
152
let tabViewModel = TabViewModel . forTabWithURL ( . duckDuckGo)
139
153
let testTitle = " Test title "
@@ -153,6 +167,7 @@ final class TabViewModelTests: XCTestCase {
153
167
await fulfillment ( of: [ titleExpectation] , timeout: 0.5 )
154
168
}
155
169
170
+ @MainActor
156
171
func testWhenTabTitleIsNilThenTitleIsAddressBarString( ) {
157
172
let tabViewModel = TabViewModel . forTabWithURL ( . duckDuckGo)
158
173
@@ -167,13 +182,15 @@ final class TabViewModelTests: XCTestCase {
167
182
168
183
// MARK: - Favicon
169
184
185
+ @MainActor
170
186
func testWhenContentIsNoneThenFaviconIsNil( ) {
171
187
let tab = Tab ( content: . none)
172
188
let tabViewModel = TabViewModel ( tab: tab)
173
189
174
190
XCTAssertEqual ( tabViewModel. favicon, nil )
175
191
}
176
192
193
+ @MainActor
177
194
func testWhenContentIsHomeThenFaviconIsHome( ) {
178
195
let tabViewModel = TabViewModel . aTabViewModel
179
196
tabViewModel. tab. setContent ( . newtab)
@@ -194,13 +211,15 @@ final class TabViewModelTests: XCTestCase {
194
211
195
212
// MARK: - Zoom
196
213
214
+ @MainActor
197
215
func testThatDefaultValueForTabsWebViewIsOne( ) {
198
216
UserDefaultsWrapper< Any> . clearAll( )
199
217
let tabVM = TabViewModel ( tab: Tab ( ) , appearancePreferences: AppearancePreferences ( ) )
200
218
201
219
XCTAssertEqual ( tabVM. tab. webView. zoomLevel, DefaultZoomValue . percent100)
202
220
}
203
221
222
+ @MainActor
204
223
func testWhenAppearancePreferencesZoomLevelIsSetThenTabsWebViewZoomLevelIsUpdated( ) {
205
224
UserDefaultsWrapper< Any> . clearAll( )
206
225
let tabVM = TabViewModel ( tab: Tab ( ) )
@@ -210,6 +229,7 @@ final class TabViewModelTests: XCTestCase {
210
229
XCTAssertEqual ( tabVM. tab. webView. zoomLevel, randomZoomLevel)
211
230
}
212
231
232
+ @MainActor
213
233
func testWhenAppearancePreferencesZoomLevelIsSetAndANewTabIsOpenThenItsWebViewHasTheLatestValueOfZoomLevel( ) {
214
234
UserDefaultsWrapper< Any> . clearAll( )
215
235
let randomZoomLevel = DefaultZoomValue . allCases. randomElement ( ) !
@@ -236,8 +256,11 @@ extension TabViewModel {
236
256
return TabViewModel ( tab: tab)
237
257
}
238
258
239
- func simulateLoadingCompletion( ) {
240
- self . tab. webViewDidCommitNavigationPublisher. send ( ( ) )
259
+ @MainActor
260
+ func simulateLoadingCompletion( _ url: URL , in webView: WKWebView ) {
261
+ let navAction = NavigationAction ( request: URLRequest ( url: url) , navigationType: . other, currentHistoryItemIdentity: nil , redirectHistory: nil , isUserInitiated: nil , sourceFrame: . mainFrame( for: webView) , targetFrame: . mainFrame( for: webView) , shouldDownload: false , mainFrameNavigation: nil )
262
+ let navigation = Navigation ( identity: . init( nil ) , responders: . init( ) , state: . started, redirectHistory: [ navAction] , isCurrent: true , isCommitted: true )
263
+ self . tab. didCommit ( navigation)
241
264
}
242
265
243
266
}
0 commit comments