forked from qodo-benchmark/firefox-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFxAWebViewModelTests.swift
More file actions
131 lines (114 loc) · 6.06 KB
/
FxAWebViewModelTests.swift
File metadata and controls
131 lines (114 loc) · 6.06 KB
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
// 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/
@testable import Client
import XCTest
import PDFKit
@MainActor
class FxAWebViewModelTests: XCTestCase {
var viewModel: FxAWebViewModel!
var deeplinkParams: FxALaunchParams!
override func setUp() async throws {
try await super.setUp()
deeplinkParams = FxALaunchParams(entrypoint: .browserMenu, query: ["test_key": "test_value"])
viewModel = FxAWebViewModel(pageType: .settingsPage,
profile: MockProfile(),
deepLinkParams: deeplinkParams,
telemetry: FxAWebViewTelemetry(telemetryWrapper: MockTelemetryWrapper()))
}
override func tearDown() async throws {
deeplinkParams = nil
viewModel = nil
try await super.tearDown()
}
func testCreateOutputURLWithValidFileNameAndExtension() {
let fileName = "testFile"
let fileExtension = "txt"
let expectedURL = createExpectedURL(with: fileName, and: fileExtension)
let resultURL = viewModel.createOutputURL(withFileName: fileName, withFileExtension: fileExtension)
XCTAssertEqual(resultURL, expectedURL, "The created URL is not valid")
}
func testCreateOutputURLWithEmptyFileNameAndExtension() {
let fileName = ""
let fileExtension = ""
let expectedURL = createExpectedURL(with: fileName, and: fileExtension)
let resultURL = viewModel.createOutputURL(withFileName: fileName, withFileExtension: fileExtension)
XCTAssertEqual(resultURL, expectedURL, "The created URL is not valid")
}
func testCreateOutputURLWithSpecialCharactersInFileName() {
let fileName = "test@File#1"
let fileExtension = "data"
let expectedURL = createExpectedURL(with: fileName, and: fileExtension)
let resultURL = viewModel.createOutputURL(withFileName: fileName, withFileExtension: fileExtension)
XCTAssertEqual(resultURL, expectedURL, "The created URL is not valid")
}
func testIsMozillaAccountPDFWithValidBlobURLAndCorrectHost() {
if let blobURL = URL(string: "blob://some/blob/url"),
let webViewURL = URL(string: "https://accounts.firefox.com") {
let result = viewModel.isMozillaAccountPDF(blobURL: blobURL, webViewURL: webViewURL)
XCTAssertTrue(result, "Should return true for a valid blob URL and a webView URL with the host accounts.firefox.com.")
}
}
func testIsMozillaAccountPDFWithValidBlobURLAndIncorrectHost() {
if let blobURL = URL(string: "blob://some/blob/url"),
let webViewURL = URL(string: "https://example.com") {
let result = viewModel.isMozillaAccountPDF(blobURL: blobURL, webViewURL: webViewURL)
XCTAssertFalse(result, "Should return false for a valid blob URL and a webView URL with a different host then accounts.firefox.com")
}
}
func testIsMozillaAccountPDFWithInvalidBlobURLAndCorrectHost() {
if let blobURL = URL(string: "https://example.com/blob"),
let webViewURL = URL(string: "https://accounts.firefox.com") {
let result = viewModel.isMozillaAccountPDF(blobURL: blobURL, webViewURL: webViewURL)
XCTAssertFalse(result, "Should return false for a wrong blob URL and a webView URL with the host accounts.firefox.com.")
}
}
func testIsMozillaAccountPDFWithValidBlobURLAndNilWebViewURL() {
if let blobURL = URL(string: "blob://some/blob/url") {
let webViewURL: URL? = nil
let result = viewModel.isMozillaAccountPDF(blobURL: blobURL, webViewURL: webViewURL)
XCTAssertFalse(result, "Should return false for a valid blob URL and a nil webView URL.")
}
}
func testIsMozillaAccountPDFWithInvalidBlobURLAndNilWebViewURL() {
if let blobURL = URL(string: "https://example.com/blob") {
let webViewURL: URL? = nil
let result = viewModel.isMozillaAccountPDF(blobURL: blobURL, webViewURL: webViewURL)
XCTAssertFalse(result, "Should return false for a wrong blob URL and a nil webView URL.")
}
}
func testCreateURLForPDFWithValidSuccessResult() {
let result: Result<Any?, Error> = .success(MockFxAWebViewModel().validPDFDataURL)
if let outputURL = viewModel.createURLForPDF(result: result) {
XCTAssertNotNil(outputURL, "Should return a valid URL.")
XCTAssertTrue(FileManager.default.fileExists(atPath: outputURL.path), "PDF File should exist.")
}
}
func testCreateURLForPDFWithInvalidSuccessResult() {
let invalidURLString = "invalidURL"
let result: Result<Any?, Error> = .success(invalidURLString)
let outputURL = viewModel.createURLForPDF(result: result)
XCTAssertNil(outputURL, "Should return nil on .success but with an invalid URL")
}
func testCreateURLForPDFWithFailureResult() {
let error = NSError(domain: "TestDomain", code: 1, userInfo: nil)
let result: Result<Any?, Error> = .failure(error)
let outputURL = viewModel.createURLForPDF(result: result)
XCTAssertNil(outputURL, "Should return nil for a .failure result")
}
func testCreateURLForPDFWithValidURLButInvalidPDFData() {
let result: Result<Any?, Error> = .success(MockFxAWebViewModel().invalidPDFDataURL)
let outputURL = viewModel.createURLForPDF(result: result)
XCTAssertNil(outputURL, "Should return nil on .success with an URL but not a PDF one.")
}
}
extension FxAWebViewModelTests {
private func createExpectedURL(with fileName: String, and fileExtension: String) -> URL? {
try? FileManager.default.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
.appendingPathComponent(fileName)
.appendingPathExtension(fileExtension)
}
}