-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebImageDownloaderTest.m
137 lines (108 loc) · 5.77 KB
/
WebImageDownloaderTest.m
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
//
// WebImageDownloaderTest.m
// TwitterStreamExample
//
// Created by Davide De Franceschi on 17/08/14.
// Copyright (c) 2014 Davide De Franceschi. All rights reserved.
//
@import XCTest;
#import "WebImageDownloader.h"
#define TEST_TIMEOUT_TIME_S 2.0
#define TEST_TIMEOUT_TIME_NS ((int64_t)(TEST_TIMEOUT_TIME_S * NSEC_PER_SEC))
#pragma mark -
@interface WebImageDownloaderTest : XCTestCase
@property (strong, nonatomic) WebImageDownloader *exampleDownloader;
@property (strong, nonatomic) NSURL *exampleImageURL;
@property (strong, nonatomic) NSURL *exampleDifferentImageURL;
@property (strong, nonatomic) NSURL *exampleWrongURL;
@end
@implementation WebImageDownloaderTest
- (void)setUp
{
[super setUp];
self.exampleDownloader = [WebImageDownloader new];
self.exampleImageURL = [NSURL URLWithString:@"http://www.google.com/trends/resources/2327917647-google-icon.png"];
self.exampleDifferentImageURL = [NSURL URLWithString:@"http://www.google.com/intl/en-GB/homepage/images/google_favicon_64.png"];
self.exampleWrongURL = [NSURL URLWithString:@"http://www.example.com/test_img_url.jpg"];
}
- (void)testDownloaderInitialization
{
WebImageDownloader *downloader;
downloader = [WebImageDownloader new];
XCTAssertNotNil(downloader, @"Should have returned a not nil WebImageDownloader.");
downloader = [[WebImageDownloader alloc] initWithCache:nil];
XCTAssertNotNil(downloader, @"Should have returned a not nil WebImageDownloader.");
downloader = [[WebImageDownloader alloc] initWithCache:[NSCache new]];
XCTAssertNotNil(downloader, @"Should have returned a not nil WebImageDownloader.");
}
- (void)testDownloaderWithValidURL
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
[self.exampleDownloader downloadImageFromURL:self.exampleImageURL completion:^(UIImage *downloadedImage) {
XCTAssertNotNil(downloadedImage, @"Should have returned a not nil UIImage. To test if the image is still available online go to %@", [self.exampleImageURL absoluteString]);
dispatch_semaphore_signal(semaphore);
}];
if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
}
- (void)testDownloaderWithInvalidURL
{
dispatch_group_t semaphores = dispatch_group_create();
dispatch_group_enter(semaphores);
[self.exampleDownloader downloadImageFromURL:nil completion:^(UIImage *downloadedImage) {
XCTAssertNil(downloadedImage, @"Should have returned a nil UIImage");
dispatch_group_leave(semaphores);
}];
dispatch_group_enter(semaphores);
[self.exampleDownloader downloadImageFromURL:self.exampleWrongURL completion:^(UIImage *downloadedImage) {
XCTAssertNil(downloadedImage, @"Should have returned a nil UIImage");
dispatch_group_leave(semaphores);
}];
if (dispatch_group_wait(semaphores, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
}
- (void)testDownloaderCacheHit
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block UIImage *testImage;
[self.exampleDownloader downloadImageFromURL:self.exampleImageURL completion:^(UIImage *downloadedImage) {
XCTAssertNotNil(downloadedImage, @"Should have returned a not nil UIImage. To test if the image is still available online go to %@", [self.exampleImageURL absoluteString]);
testImage = downloadedImage;
dispatch_semaphore_signal(semaphore);
}];
if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
[self.exampleDownloader downloadImageFromURL:self.exampleImageURL completion:^(UIImage *downloadedImage) {
XCTAssertNotNil(downloadedImage, @"Should have returned a not nil UIImage. It should have been retrieved from the downloader's cache.");
XCTAssertEqual(downloadedImage, testImage, @"The UIImage returned should be the same that was just dowloaded.");
dispatch_semaphore_signal(semaphore);
}];
if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
}
- (void)testDownloaderCacheMiss
{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block UIImage *testImage;
[self.exampleDownloader downloadImageFromURL:self.exampleImageURL completion:^(UIImage *downloadedImage) {
XCTAssertNotNil(downloadedImage, @"Should have returned a not nil UIImage. To test if the image is still available online go to %@", [self.exampleImageURL absoluteString]);
testImage = downloadedImage;
dispatch_semaphore_signal(semaphore);
}];
if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
[self.exampleDownloader downloadImageFromURL:self.exampleDifferentImageURL completion:^(UIImage *downloadedImage) {
XCTAssertNotNil(downloadedImage, @"Should have returned a not nil UIImage. To test if the image is still available online go to %@", [self.exampleDifferentImageURL absoluteString]);
XCTAssertNotEqual(downloadedImage, testImage, @"The UIImage returned should be different from the one which was just dowloaded.");
dispatch_semaphore_signal(semaphore);
}];
if (dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT_TIME_NS))) {
XCTFail(@"Asynchronous test %s did not finish within the timeout of %f seconds.", __PRETTY_FUNCTION__, TEST_TIMEOUT_TIME_S);
}
}
@end