-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathSTPPaymentMethod+CardArtImageTest.swift
More file actions
73 lines (58 loc) · 2.6 KB
/
Copy pathSTPPaymentMethod+CardArtImageTest.swift
File metadata and controls
73 lines (58 loc) · 2.6 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
//
// STPPaymentMethod+CardArtImageTest.swift
// StripePaymentSheetTests
//
@testable @_spi(STP) import StripePaymentSheet
import OHHTTPStubs
import OHHTTPStubsSwift
@_spi(STP) @testable import StripeCore
@_spi(STP) import StripeCoreTestUtils
import StripePaymentsObjcTestUtils
import XCTest
final class STPPaymentMethodCardArtImageTest: APIStubbedTestCase {
var urlSessionConfig: URLSessionConfiguration!
var downloadManager: DownloadManager!
override func setUp() {
super.setUp()
urlSessionConfig = APIStubbedTestCase.stubbedURLSessionConfig()
urlSessionConfig.urlCache = URLCache(memoryCapacity: 5_000_000, diskCapacity: 0)
downloadManager = DownloadManager(urlSessionConfiguration: urlSessionConfig, isTesting: true)
downloadManager.resetCache()
}
// MARK: - STPPaymentMethod.cardArtImage
func testCardArtImage_returnsNilWhenNoCardArt() {
let pm = STPPaymentMethod._testCard()
XCTAssertNil(pm.cachedCardArtImage(downloadManager: downloadManager))
}
func testCardArtImage_returnsNilWhenImageNotCached() {
let pm = STPPaymentMethod._testCardWithCardArt()
XCTAssertNil(pm.cachedCardArtImage(downloadManager: downloadManager))
}
func testCardArtImage_returnsImageWhenCached() {
let pm = STPPaymentMethod._testCardWithCardArt()
let cardArtURL = pm.cardArtCDNURL()!
// Pre-seed the URL cache so downloadImage returns the real image
let imageData = generateUIImage(size: CGSize(width: 100, height: 26)).pngData()!
seedURLCache(url: cardArtURL, data: imageData)
let result = pm.cachedCardArtImage(downloadManager: downloadManager)
XCTAssertNotNil(result)
// The returned image should be the same as the one we created
XCTAssertEqual(result?.size, CGSize(width: 100, height: 26))
}
// MARK: - Helpers
private func seedURLCache(url: URL, data: Data) {
let request = URLRequest(url: url)
let response = HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil)!
let cachedResponse = CachedURLResponse(response: response, data: data)
urlSessionConfig.urlCache?.storeCachedResponse(cachedResponse, for: request)
}
private func generateUIImage(size: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
UIColor.clear.set()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}