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
import UIKit
6
import XCTest
7
import Storage
8
import SDWebImage
9
import GCDWebServers
10
import Shared
11
12
@testable import Client
13
14
class UIImageViewExtensionsTests: XCTestCase {
15
16
override func setUp() {
17
SDWebImageDownloader.shared.config.urlCredential = WebServer.sharedInstance.credentials
18
}
19
20
func testsetIcon() {
21
let url = URL(string: "http://mozilla.com")
22
let imageView = UIImageView()
23
24
let goodIcon = FaviconFetcher.getDefaultFavicon(url!)
25
let correctColor = FaviconFetcher.getDefaultColor(url!)
26
imageView.setIcon(nil, forURL: url)
27
XCTAssertEqual(imageView.image!, goodIcon, "The correct default favicon should be applied")
28
XCTAssertEqual(imageView.backgroundColor, correctColor, "The correct default color should be applied")
29
30
imageView.setIcon(nil, forURL: URL(string: "http://mozilla.com/blahblah"))
31
XCTAssertEqual(imageView.image!, goodIcon, "The same icon should be applied to all urls with the same domain")
32
33
imageView.setIcon(nil, forURL: URL(string: "b"))
34
XCTAssertEqual(imageView.image, FaviconFetcher.defaultFavicon, "The default favicon should be applied when no information is given about the icon")
35
}
36
37
func testAsyncSetIcon() {
38
let originalImage = UIImage(named: "bookmark")!
39
40
WebServer.sharedInstance.registerHandlerForMethod("GET", module: "favicon", resource: "icon") { (request) -> GCDWebServerResponse in
41
return GCDWebServerDataResponse(data: originalImage.pngData()!, contentType: "image/png")
42
}
43
44
let favImageView = UIImageView()
45
favImageView.setIcon(Favicon(url: "http://localhost:\(AppInfo.webserverPort)/favicon/icon"), forURL: URL(string: "http://localhost:\(AppInfo.webserverPort)"))
46
47
let expect = expectation(description: "UIImageView async load")
48
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0) {
49
XCTAssert(originalImage.size.width * originalImage.scale == favImageView.image!.size.width * favImageView.image!.scale, "The correct favicon should be applied to the UIImageView")
50
expect.fulfill()
51
}
52
waitForExpectations(timeout: 5, handler: nil)
53
}
54
55
func testAsyncSetIconFail() {
56
let favImageView = UIImageView()
57
58
let gFavURL = URL(string: "https://www.nofavicon.com/noicon.ico")
59
let gURL = URL(string: "http://nofavicon.com")
60
let correctImage = FaviconFetcher.getDefaultFavicon(gURL!)
61
62
favImageView.setIcon(Favicon(url: gFavURL!.absoluteString), forURL: gURL)
63
64
let expect = expectation(description: "UIImageView async load")
65
66
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
67
XCTAssert(correctImage.size.width * correctImage.scale == favImageView.image!.size.width * favImageView.image!.scale, "The correct default favicon should be applied to the UIImageView")
68
expect.fulfill()
69
}
70
waitForExpectations(timeout: 5, handler: nil)
71
}
72
73
func testDefaultIcons() {
74
let favImageView = UIImageView()
75
76
let gFavURL = URL(string: "https://www.facebook.com/fav") //This will be fetched from tippy top sites
77
let gURL = URL(string: "http://www.facebook.com")!
78
let defaultItem = FaviconFetcher.defaultIcons[gURL.baseDomain!]!
79
let correctImage = UIImage(contentsOfFile: defaultItem.url)!
80
81
favImageView.setIcon(Favicon(url: gFavURL!.absoluteString), forURL: gURL)
82
83
let expect = expectation(description: "UIImageView async load")
84
85
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
86
XCTAssertEqual(favImageView.backgroundColor, defaultItem.color)
87
XCTAssert(correctImage.size.width * correctImage.scale == favImageView.image!.size.width * favImageView.image!.scale, "The correct default favicon should be applied to the UIImageView")
88
expect.fulfill()
89
}
90
waitForExpectations(timeout: 5, handler: nil)
91
}
92
}