-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathDiskImageStoreTests.swift
76 lines (63 loc) · 2.62 KB
/
DiskImageStoreTests.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
/* 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/. */
import Shared
@testable import Storage
@testable import Client
import UIKit
import XCTest
class DiskImageStoreTests: XCTestCase {
var files: FileAccessor!
var store: DiskImageStore!
override func setUp() {
files = MockFiles()
store = DiskImageStore(files: files, namespace: "DiskImageStoreTests", quality: 1)
_ = store.clearExcluding(Set()).value
}
func testStore() {
var success = false
// Avoid image comparison and use size of the image for equality
let redImage = makeImageWithColor(UIColor.red, size: CGSize(width: 100, height: 100))
let blueImage = makeImageWithColor(UIColor.blue, size: CGSize(width: 17, height: 17))
[(key: "blue", image: blueImage), (key: "red", image: redImage)].forEach() { (key, image) in
XCTAssertNil(getImage(key), "\(key) key is nil")
success = putImage(key, image: image)
XCTAssert(success, "\(key) image added to store")
XCTAssertEqual(getImage(key)!.size.width, image.size.width, "Images are equal")
success = putImage(key, image: image)
XCTAssertFalse(success, "\(key) image not added again")
}
_ = store.clearExcluding(Set(["red"])).value
XCTAssertNotNil(getImage("red"), "Red image still exists")
XCTAssertNil(getImage("blue"), "Blue image cleared")
}
private func makeImageWithColor(_ color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(size: size)
UIGraphicsBeginImageContextWithOptions(size, false, 1.0)
color.setFill()
UIRectFill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
private func getImage(_ key: String) -> UIImage? {
let expectation = self.expectation(description: "Get succeeded")
var image: UIImage?
store.get(key).upon {
image = $0.successValue
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
return image
}
private func putImage(_ key: String, image: UIImage) -> Bool {
let expectation = self.expectation(description: "Put succeeded")
var success = false
store.put(key, image: image).upon {
success = $0.isSuccess
expectation.fulfill()
}
waitForExpectations(timeout: 10, handler: nil)
return success
}
}