7
7
//
8
8
9
9
#import < XCTest/XCTest.h>
10
+ #import " CCPackageDownloadManager.h"
11
+ #import " CCPackageDownloadManagerDelegate.h"
12
+ #import " CCPackage.h"
13
+ #import " CCDirector.h"
14
+ #import " AppDelegate.h"
15
+ #import " CCUnitTestAssertions.h"
16
+ #import " CCPackageInstallData.h"
17
+ #import " CCPackage+InstallData.h"
10
18
11
- @interface CCPackageDownloadManagerTests : XCTestCase
19
+ @interface CCPackageDownloadManagerTestURLProtocol : NSURLProtocol @end
20
+
21
+ @implementation CCPackageDownloadManagerTestURLProtocol
22
+
23
+ + (BOOL )canInitWithRequest : (NSURLRequest *)theRequest
24
+ {
25
+ return [theRequest.URL.scheme caseInsensitiveCompare: @" http" ] == NSOrderedSame;
26
+ }
27
+
28
+ + (NSURLRequest *)canonicalRequestForRequest : (NSURLRequest *)theRequest
29
+ {
30
+ return theRequest;
31
+ }
32
+
33
+ - (void )startLoading
34
+ {
35
+ // just send back what was received in URL as last path component
36
+ NSString *payload = [self .request.URL lastPathComponent ];
37
+ NSData *data = [payload dataUsingEncoding: NSUTF8StringEncoding];
38
+
39
+ NSHTTPURLResponse *response;
40
+ response = [[NSHTTPURLResponse alloc ] initWithURL: self .request.URL
41
+ statusCode: 200
42
+ HTTPVersion: @" HTTP/1.1"
43
+ headerFields: nil ];
44
+
45
+ id <NSURLProtocolClient > client = [self client ];
46
+ [client URLProtocol: self didReceiveResponse: response cacheStoragePolicy: NSURLCacheStorageNotAllowed];
47
+ [client URLProtocol: self didLoadData: data];
48
+ [client URLProtocolDidFinishLoading: self ];
49
+ }
50
+
51
+ - (void )stopLoading
52
+ {
53
+ // Nothing to do
54
+ }
55
+
56
+ @end
57
+
58
+ @interface CCPackageDownloadManagerTests : XCTestCase <CCPackageDownloadManagerDelegate>
59
+
60
+ @property (nonatomic , strong ) CCPackageDownloadManager *downloadManager;
61
+ @property (nonatomic ) BOOL allDownloadsReturned;
62
+ @property (nonatomic , copy ) NSString *downloadPath;
12
63
13
64
@end
14
65
@@ -17,18 +68,106 @@ @implementation CCPackageDownloadManagerTests
17
68
- (void )setUp
18
69
{
19
70
[super setUp ];
20
- // Put setup code here. This method is called before the invocation of each test method in the class.
71
+
72
+ [(AppController *)[UIApplication sharedApplication ].delegate configureCocos2d ];
73
+ [[CCDirector sharedDirector ] stopAnimation ];
74
+ // Spin the runloop a bit otherwise nondeterministic exceptions are thrown in the CCScheduler.
75
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeInterval: 0.2 sinceDate: [NSDate date ]]];
76
+
77
+ [NSURLProtocol registerClass: [CCPackageDownloadManagerTestURLProtocol class ]];
78
+
79
+ self.downloadManager = [[CCPackageDownloadManager alloc ] init ];
80
+ self.allDownloadsReturned = NO ;
81
+
82
+ self.downloadPath = [NSTemporaryDirectory () stringByAppendingPathComponent: @" Downloads" ];
83
+
84
+ [self deleteOldDownloads ];
85
+
86
+ _downloadManager.downloadPath = _downloadPath;
87
+ _downloadManager.delegate = self;
21
88
}
22
89
23
90
- (void )tearDown
24
91
{
25
- // Put teardown code here. This method is called after the invocation of each test method in the class.
92
+ [ NSURLProtocol unregisterClass: [CCPackageDownloadManagerTestURLProtocol class ]];
26
93
[super tearDown ];
27
94
}
28
95
29
- - (void )testExample
96
+ - (void )deleteOldDownloads
97
+ {
98
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
99
+ [fileManager removeItemAtPath: _downloadPath error: nil ];
100
+ }
101
+
102
+ - (void )testSetDownloadPath
103
+ {
104
+ NSString *newPath = [NSTemporaryDirectory () stringByAppendingPathComponent: @" NewDownloads" ];
105
+ _downloadManager.downloadPath = newPath;
106
+
107
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
108
+ BOOL isDir;
109
+
110
+ XCTAssert ([fileManager fileExistsAtPath: newPath isDirectory: &isDir]);
111
+ XCTAssertTrue (isDir);
112
+ CCAssertEqualStrings (newPath, _downloadManager.downloadPath );
113
+ }
114
+
115
+ - (void )testTwoDownloads
116
+ {
117
+ CCPackage *package1 = [[CCPackage alloc ] initWithName: @" package1" resolution: @" phonehd" os: @" iOS" remoteURL: [NSURL URLWithString: @" http://package.fake/package1" ]];
118
+ CCPackageInstallData *installData1 = [[CCPackageInstallData alloc ] init ];
119
+ [package1 setInstallData: installData1];
120
+
121
+ CCPackage *package2 = [[CCPackage alloc ] initWithName: @" package2" resolution: @" phonehd" os: @" iOS" remoteURL: [NSURL URLWithString: @" http://package.fake/package2" ]];
122
+ CCPackageInstallData *installData2 = [[CCPackageInstallData alloc ] init ];
123
+ [package2 setInstallData: installData2];
124
+
125
+ [_downloadManager enqueuePackageForDownload: package1];
126
+ [_downloadManager enqueuePackageForDownload: package2];
127
+
128
+ while (!_allDownloadsReturned)
129
+ {
130
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture ]];
131
+ }
132
+
133
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
134
+ XCTAssertTrue ([fileManager fileExistsAtPath: [package1 installData ].localDownloadURL.path]);
135
+ XCTAssertTrue ([fileManager fileExistsAtPath: [package2 installData ].localDownloadURL.path]);
136
+
137
+ CCAssertEqualStrings (@" package1" , [NSString stringWithContentsOfFile: [package1 installData ].localDownloadURL.path encoding: NSUTF8StringEncoding error: nil ]);
138
+ CCAssertEqualStrings (@" package2" , [NSString stringWithContentsOfFile: [package2 installData ].localDownloadURL.path encoding: NSUTF8StringEncoding error: nil ]);
139
+ }
140
+
141
+ - (void )testCancelDownload
142
+ {
143
+ CCPackage *package1 = [[CCPackage alloc ] initWithName: @" package1" resolution: @" phonehd" os: @" iOS" remoteURL: [NSURL URLWithString: @" http://package.fake/package1" ]];
144
+ CCPackageInstallData *installData1 = [[CCPackageInstallData alloc ] init ];
145
+ [package1 setInstallData: installData1];
146
+
147
+ [_downloadManager enqueuePackageForDownload: package1];
148
+ [_downloadManager cancelDownloadOfPackage: package1];
149
+
150
+ // Can't wait for delegate since cancelling won't trigger them
151
+ // Just wait a short amount of time and see if nothing has been written to disk
152
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeInterval: 0.5 sinceDate: [NSDate date ]]];
153
+
154
+ NSFileManager *fileManager = [NSFileManager defaultManager ];
155
+ XCTAssertFalse ([fileManager fileExistsAtPath: [package1 installData ].localDownloadURL.path]);
156
+ }
157
+
158
+
159
+ #pragma mark -
160
+
161
+ - (void )downloadFinishedOfPackage : (CCPackage *)package
162
+ {
163
+ NSLog (@" %@ finished" , package);
164
+ self.allDownloadsReturned = _downloadManager.allDownloads .count == 0 ;
165
+ }
166
+
167
+ - (void )downloadFailedOfPackage : (CCPackage *)package error : (NSError *)error
30
168
{
31
- XCTFail (@" No implementation for \" %s \" " , __PRETTY_FUNCTION__);
169
+ NSLog (@" %@ failed" , package);
170
+ self.allDownloadsReturned = _downloadManager.allDownloads .count == 0 ;
32
171
}
33
172
34
173
@end
0 commit comments