forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaviconsTests.swift
178 lines (136 loc) · 5.63 KB
/
FaviconsTests.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// FaviconsTests.swift
// UnitTests
//
// Copyright © 2020 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
@testable import Core
import Kingfisher
import CoreData
import Bookmarks
class FaviconsTests: XCTestCase {
private var favicons: Favicons!
private var mockObjectID: NSManagedObjectID!
private var inMemoryStore: NSPersistentContainer!
override func setUp() {
super.setUp()
inMemoryStore = CoreData.createInMemoryPersistentContainer(modelName: "BookmarksModel",
bundle: Bookmarks.bundle)
BookmarkUtils.prepareFoldersStructure(in: inMemoryStore.viewContext)
mockObjectID = BookmarkUtils.fetchRootFolder(inMemoryStore.viewContext)?.objectID
XCTAssertNotNil(mockObjectID)
favicons = Favicons(sourcesProvider: DefaultFaviconSourcesProvider(),
downloader: NotFoundCachingDownloader())
Favicons.Constants.tabsCache.clearDiskCache()
Favicons.Constants.tabsCache.clearMemoryCache()
Favicons.Constants.fireproofCache.clearDiskCache()
Favicons.Constants.fireproofCache.clearMemoryCache()
_ = DefaultUserAgentManager.shared
}
override func tearDownWithError() throws {
favicons = nil
mockObjectID = nil
inMemoryStore = nil
try super.tearDownWithError()
}
func testWhenGeneratingKingfisherOptionsThenOptionsAreConfiguredCorrectly() {
let options = favicons.kfOptions(forDomain: Constants.exampleDomain, usingCache: .tabs)
switch options?[0] {
case .downloader(let downloader):
XCTAssertTrue(downloader === favicons.downloader)
default:
XCTFail("Unexpected option")
}
switch options?[1] {
case .requestModifier(let modifier):
XCTAssertTrue(modifier as AnyObject === Favicons.Constants.requestModifier)
default:
XCTFail("Unexpected option")
}
switch options?[2] {
case .targetCache(let cache):
XCTAssertTrue(cache === Favicons.Constants.caches[.tabs])
default:
XCTFail("Unexpected option")
}
// release builds will set an explicit 7 day, test builds use a smaller expiry
switch options?[3] {
case .diskCacheExpiration: break
default:
XCTFail("Unexpected option")
}
switch options?[4] {
case .alternativeSources(let sources):
XCTAssertEqual(2, sources.count)
XCTAssertEqual(sources[0].url, URL(string: "https://example.com/favicon.ico"))
XCTAssertEqual(sources[1].url, URL(string: "http://example.com/favicon.ico"))
default:
XCTFail("Unexpected option")
}
}
func testWhenGeneratingKingfisherResourceThenCorrectKeyAndURLAreGenerated() {
let resource = favicons.defaultResource(forDomain: Constants.exampleDomain)
XCTAssertEqual(resource?.cacheKey, "\(Favicons.Constants.salt)\(Constants.exampleDomain)".sha256())
XCTAssertEqual(resource?.downloadURL, URL(string: "https://example.com/apple-touch-icon.png"))
}
func testWhenDomainIsBookmarkThenIsFaviconCached() {
let expectation = self.expectation(description: "isFaviconCachedForBookmarks")
guard let image = UIImage(named: "Logo"),
let resource = favicons.defaultResource(forDomain: Constants.exampleDomain) else {
XCTFail("Failed to load data needed for test")
return
}
Favicons.Constants.fireproofCache.store(image, forKey: resource.cacheKey) { _ in
XCTAssertTrue(Favicons.Constants.fireproofCache.isCached(forKey: resource.cacheKey))
expectation.fulfill()
}
waitForExpectations(timeout: 10.0, handler: nil)
}
func testWhenProvidedImageThenSizeIsValid() {
guard let image = UIImage(named: "Logo") else {
XCTFail("Failed to load image needed for test")
return
}
XCTAssertTrue(favicons.isValidImage(image, forMaxSize: CGSize(width: 128.0, height: 128.0)))
XCTAssertFalse(favicons.isValidImage(image, forMaxSize: CGSize(width: 64.0, height: 64.0)))
}
func testWhenProvidedImageThenImageIsResized() {
guard let image = UIImage(named: "Logo") else {
XCTFail("Failed to load image needed for test")
return
}
let size = CGSize(width: 64, height: 64)
XCTAssertTrue(image.size != size)
let resizedImage = favicons.resizedImage(image, toSize: size)
XCTAssertTrue(resizedImage.size == size)
}
}
struct MockSourcesProvider: FaviconSourcesProvider {
let mainSource: URL
let additionalSources: [URL]
func mainSource(forDomain: String) -> URL? {
return mainSource
}
func additionalSources(forDomain: String) -> [URL] {
return additionalSources
}
}
private extension FaviconsTests {
enum Constants {
static let exampleDomain = "example.com"
static let bookmarkURLString = "https://duckduckgo.com"
}
}