12
12
#import " CCPackage.h"
13
13
#import " CCPackageDownloadDelegate.h"
14
14
#import " CCDirector.h"
15
+ #import " AppDelegate.h"
15
16
16
17
static NSUInteger __fileDownloadSize = 0 ;
17
18
static BOOL __support_range_request = YES ;
@@ -130,8 +131,10 @@ - (void)setUp
130
131
{
131
132
[super setUp ];
132
133
134
+ [(AppController *)[UIApplication sharedApplication ].delegate configureCocos2d ];
133
135
[[CCDirector sharedDirector ] stopAnimation ];
134
-
136
+ // Sping the runloop a bit otherwise nondeterministic exceptions are thrown in the CCScheduler.
137
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeInterval: 0.2 sinceDate: [NSDate date ]]];
135
138
136
139
[NSURLProtocol registerClass: [CCPackageDownloadTestURLProtocol class ]];
137
140
@@ -189,12 +192,10 @@ - (void)createDownloadFolder
189
192
190
193
191
194
#pragma mark - tests
195
+
192
196
- (void )testDownloadPackage
193
197
{
194
- [self waitForDelegateToReturnAfterRunningBlock: ^
195
- {
196
- [_download start ];
197
- }];
198
+ [self startDownloadAndWaitForDelegateToReturn ];
198
199
199
200
NSFileManager *fileManager = [NSFileManager defaultManager ];
200
201
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
@@ -203,54 +204,25 @@ - (void)testDownloadPackage
203
204
XCTAssertEqual ([attribs[NSFileSize ] unsignedIntegerValue ], __fileDownloadSize);
204
205
}
205
206
206
- - (void )waitForDelegateToReturnAfterRunningBlock : (dispatch_block_t )block
207
- {
208
- block ();
209
-
210
- while (!_downloadReturned)
211
- {
212
- [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture ]];
213
- }
214
- }
215
-
216
- /*
217
-
218
-
219
207
- (void )testResumeDownloadAKARangeRequest
220
208
{
221
209
[self setupPartialDownloadOnDisk ];
222
210
223
- [self waitForDelegateToReturnAfterRunningBlock:^
224
- {
225
- [_download start];
226
- }];
227
-
211
+ [self startDownloadAndWaitForDelegateToReturn ];
228
212
NSFileManager *fileManager = [NSFileManager defaultManager ];
229
213
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
230
214
XCTAssertTrue (_downloadSuccessful);
231
215
XCTAssertTrue ([fileManager fileExistsAtPath: _localURL.path]);
232
216
XCTAssertEqual ([attribs[NSFileSize ] unsignedIntegerValue ], __fileDownloadSize);
233
217
}
234
218
235
- - (void)setupPartialDownloadOnDisk
236
- {
237
- NSString *fileName = [_package.remoteURL lastPathComponent];
238
- NSString *pathToPackage = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Resources-shared/Packages/%@", fileName] ofType:nil];
239
- NSData *data = [[NSData dataWithContentsOfFile:pathToPackage] subdataWithRange:NSMakeRange(0, 5000)];
240
- NSString *tempName = [_download performSelector:@selector(createTempName)];
241
- [data writeToFile:[[_localURL.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:tempName] atomically:YES];
242
- }
243
-
244
219
- (void )testDownloadOfExistingFile
245
220
{
246
221
self.shouldOverwriteDownloadedFile = NO ;
247
222
248
223
NSUInteger filesize = [self createDownloadFile ];
249
224
250
- [self waitForDelegateToReturnAfterRunningBlock:^
251
- {
252
- [_download start];
253
- }];
225
+ [self startDownloadAndWaitForDelegateToReturn ];
254
226
255
227
NSFileManager *fileManager = [NSFileManager defaultManager ];;
256
228
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
@@ -265,10 +237,7 @@ - (void)testOverwriteExistingDownload
265
237
266
238
[self createDownloadFile ];
267
239
268
- [self waitForDelegateToReturnAfterRunningBlock:^
269
- {
270
- [_download start];
271
- }];
240
+ [self startDownloadAndWaitForDelegateToReturn ];
272
241
273
242
NSFileManager *fileManager = [NSFileManager defaultManager ];;
274
243
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
@@ -277,6 +246,38 @@ - (void)testOverwriteExistingDownload
277
246
XCTAssertEqual ([attribs[NSFileSize ] unsignedIntegerValue ], __fileDownloadSize);
278
247
}
279
248
249
+ - (void )testDownloadWith404Response
250
+ {
251
+ [_package setValue: [NSURL URLWithString: @" http://package.request.fake/DOES_NOT_EXIST.zip" ] forKey: NSStringFromSelector (@selector (remoteURL ))];
252
+
253
+ [self startDownloadAndWaitForDelegateToReturn ];
254
+
255
+ XCTAssertFalse (_downloadSuccessful);
256
+ XCTAssertNotNil (_downloadError);
257
+ }
258
+
259
+
260
+ #pragma mark - Helper
261
+
262
+ - (void )setupPartialDownloadOnDisk
263
+ {
264
+ NSString *fileName = [_package.remoteURL lastPathComponent ];
265
+ NSString *pathToPackage = [[NSBundle mainBundle ] pathForResource: [NSString stringWithFormat: @" Resources-shared/Packages/%@ " , fileName] ofType: nil ];
266
+ NSData *data = [[NSData dataWithContentsOfFile: pathToPackage] subdataWithRange: NSMakeRange (0 , 5000 )];
267
+ NSString *tempName = [_download performSelector: @selector (createTempName )];
268
+ [data writeToFile: [[_localURL.path stringByDeletingLastPathComponent ] stringByAppendingPathComponent: tempName] atomically: YES ];
269
+ }
270
+
271
+ - (void )startDownloadAndWaitForDelegateToReturn
272
+ {
273
+ [_download start ];
274
+
275
+ while (!_downloadReturned)
276
+ {
277
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture ]];
278
+ }
279
+ }
280
+
280
281
- (NSUInteger )createDownloadFile
281
282
{
282
283
NSFileManager *fileManager = [NSFileManager defaultManager ];
@@ -288,19 +289,6 @@ - (NSUInteger)createDownloadFile
288
289
return [attribs[NSFileSize ] unsignedIntegerValue ];
289
290
}
290
291
291
- - (void)testDownloadWith404Response
292
- {
293
- [_package setValue:[NSURL URLWithString:@"http://package.request.fake/DOES_NOT_EXIST.zip"] forKey:NSStringFromSelector(@selector(remoteURL))];
294
-
295
- [self waitForDelegateToReturnAfterRunningBlock:^
296
- {
297
- [_download start];
298
- }];
299
-
300
- XCTAssertFalse(_downloadSuccessful);
301
- XCTAssertNotNil(_downloadError);
302
- }
303
- */
304
292
305
293
#pragma mark - CCPackageDownloadDelegate
306
294
0 commit comments