Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
@testable import Client
6
import XCTest
7
8
class DownloadQueueTests: XCTestCase {
9
10
let didStartDownload = "downloadQueue(_:didStartDownload:)"
11
let didDownloadCombinedBytes = "downloadQueue(_:didDownloadCombinedBytes:combinedTotalBytesExpected:)"
12
let didCompleteWithError = "downloadQueue(_:didCompleteWithError:)"
13
let didFinishDownloadingTo = "downloadQueue(_:download:didFinishDownloadingTo:)"
14
15
var queue: DownloadQueue!
16
var download: MockDownload!
17
18
override func setUp() {
19
queue = DownloadQueue()
20
download = MockDownload()
21
22
super.setUp()
23
}
24
25
func testDownloadQueueIsEmpty() {
26
XCTAssertTrue(queue.isEmpty)
27
}
28
29
func testDownloadQueueIsNotEmpty() {
30
queue.downloads = [download]
31
XCTAssertTrue(!queue.isEmpty)
32
}
33
34
func testEnqueueDownloadShouldAppendDownloadAndTriggerResume() {
35
queue.enqueueDownload(download)
36
XCTAssertTrue(download.downloadTriggered)
37
}
38
39
func testEnqueueDownloadShouldCallDownloadQueueDidStartDownload() {
40
let mockQueueDelegate = MockDownloadQueueDelegate()
41
queue.delegate = mockQueueDelegate
42
queue.enqueueDownload(download)
43
XCTAssertEqual(mockQueueDelegate.methodCalled, didStartDownload)
44
}
45
46
func testCancelAllDownload() {
47
queue.downloads = [download]
48
queue.cancelAllDownloads()
49
XCTAssertTrue(download.downloadCanceled)
50
}
51
52
func testDidDownloadBytes() {
53
let mockQueueDelegate = MockDownloadQueueDelegate()
54
queue.delegate = mockQueueDelegate
55
queue.downloads = [download]
56
queue.download(download, didDownloadBytes: 0)
57
XCTAssertEqual(mockQueueDelegate.methodCalled, didDownloadCombinedBytes)
58
}
59
60
func testDidFinishDownloadingToWithOneElementsInQueue() {
61
let mockQueueDelegate = MockDownloadQueueDelegate()
62
queue.delegate = mockQueueDelegate
63
queue.downloads = [download]
64
queue.download(download, didFinishDownloadingTo: url)
65
XCTAssertEqual(mockQueueDelegate.methodCalled, didCompleteWithError)
66
}
67
68
func testDidFinishDownloadingToWithTwoElementsInQueue() {
69
let mockQueueDelegate = MockDownloadQueueDelegate()
70
queue.delegate = mockQueueDelegate
71
queue.downloads = [download, MockDownload()]
72
queue.download(download, didFinishDownloadingTo: url)
73
XCTAssertEqual(mockQueueDelegate.methodCalled, didFinishDownloadingTo)
74
}
75
76
func testDidFinishDownloadingToWithNoElementsInQueue() {
77
let mockQueueDelegate = MockDownloadQueueDelegate()
78
queue.delegate = mockQueueDelegate
79
queue.download(download, didFinishDownloadingTo: url)
80
XCTAssertEqual(mockQueueDelegate.methodCalled, "noneOfMethodWasCalled")
81
}
82
83
func testDidCompleteWithError() {
84
let mockQueueDelegate = MockDownloadQueueDelegate()
85
queue.delegate = mockQueueDelegate
86
queue.downloads = [download]
87
queue.download(download, didCompleteWithError: DownloadTestError.NoError("OK"))
88
XCTAssertEqual(mockQueueDelegate.methodCalled, didCompleteWithError)
89
}
90
91
}
92
93
private enum DownloadTestError: Error {
94
case NoError(String)
95
}
96
97
private let url = URL(string: "http://mozilla.org")!
98
99
class MockDownload: Download {
100
var downloadTriggered: Bool = false
101
var downloadCanceled: Bool = false
102
103
override func resume() {
104
downloadTriggered = true
105
}
106
107
override func cancel() {
108
downloadCanceled = true
109
}
110
111
init() {
112
let urlRequest = URLRequest(url: url)
113
let urlResponse = URLResponse(url: url, mimeType: nil, expectedContentLength: 0, textEncodingName: nil)
114
115
super.init(preflightResponse: urlResponse, request: urlRequest)
116
}
117
118
}
119
120
class MockDownloadQueueDelegate: DownloadQueueDelegate {
121
var methodCalled: String = "noneOfMethodWasCalled"
122
123
func downloadQueue(_ downloadQueue: DownloadQueue, didStartDownload download: Download) {
124
methodCalled = #function
125
}
126
127
func downloadQueue(_ downloadQueue: DownloadQueue, didDownloadCombinedBytes combinedBytesDownloaded: Int64, combinedTotalBytesExpected: Int64?) {
128
methodCalled = #function
129
}
130
131
func downloadQueue(_ downloadQueue: DownloadQueue, download: Download, didFinishDownloadingTo location: URL) {
132
methodCalled = #function
133
}
134
135
func downloadQueue(_ downloadQueue: DownloadQueue, didCompleteWithError error: Error?) {
136
methodCalled = #function
137
}
138
}