Skip to content

Commit 9164f13

Browse files
committed
CCPackageDownload more tests added.
CCPackageDownload is setting package status for failed downloads instead of download manager.
1 parent 503ccb1 commit 9164f13

File tree

2 files changed

+85
-14
lines changed

2 files changed

+85
-14
lines changed

UnitTests/CCPackageDownloadTests.m

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ - (void)createDownloadFolder
194194

195195
- (void)testDownloadPackage
196196
{
197-
[self startDownloadAndWaitForDelegateToReturn];
197+
[_download start];
198+
XCTAssertEqual(_package.status, CCPackageStatusDownloading);
199+
200+
[self waitForDelegateToReturn];
198201

199202
NSFileManager *fileManager = [NSFileManager defaultManager];
200203
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
@@ -207,7 +210,10 @@ - (void)testResumeDownloadAKARangeRequest
207210
{
208211
[self setupPartialDownloadOnDisk];
209212

210-
[self startDownloadAndWaitForDelegateToReturn];
213+
[_download start];
214+
XCTAssertEqual(_package.status, CCPackageStatusDownloading);
215+
216+
[self waitForDelegateToReturn];
211217
NSFileManager *fileManager = [NSFileManager defaultManager];
212218
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
213219
XCTAssertTrue(_downloadSuccessful);
@@ -221,7 +227,10 @@ - (void)testDownloadOfExistingFile
221227

222228
NSUInteger filesize = [self createDownloadFile];
223229

224-
[self startDownloadAndWaitForDelegateToReturn];
230+
[_download start];
231+
XCTAssertEqual(_package.status, CCPackageStatusDownloaded);
232+
233+
[self waitForDelegateToReturn];
225234

226235
NSFileManager *fileManager = [NSFileManager defaultManager];;
227236
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
@@ -236,7 +245,10 @@ - (void)testOverwriteExistingDownload
236245

237246
[self createDownloadFile];
238247

239-
[self startDownloadAndWaitForDelegateToReturn];
248+
[_download start];
249+
XCTAssertEqual(_package.status, CCPackageStatusDownloading);
250+
251+
[self waitForDelegateToReturn];
240252

241253
NSFileManager *fileManager = [NSFileManager defaultManager];;
242254
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
@@ -249,21 +261,78 @@ - (void)testDownloadWith404Response
249261
{
250262
[_package setValue:[NSURL URLWithString:@"http://package.request.fake/DOES_NOT_EXIST.zip"] forKey:NSStringFromSelector(@selector(remoteURL))];
251263

252-
[self startDownloadAndWaitForDelegateToReturn];
264+
[_download start];
265+
XCTAssertEqual(_package.status, CCPackageStatusDownloading);
266+
267+
[self waitForDelegateToReturn];
253268

254269
XCTAssertFalse(_downloadSuccessful);
255270
XCTAssertNotNil(_downloadError);
271+
XCTAssertEqual(_package.status, CCPackageStatusDownloadFailed);
256272
}
257273

258274
- (void)testDownloadFolderNotAccessible
259275
{
260276
// Writing to root level is supposed to fail
261277
[_download setValue:[NSURL fileURLWithPath:@"/test.zip"] forKey:NSStringFromSelector(@selector(localURL))];
262278

263-
[self startDownloadAndWaitForDelegateToReturn];
279+
[_download start];
280+
281+
[self waitForDelegateToReturn];
264282

265283
XCTAssertFalse(_downloadSuccessful);
266284
XCTAssertNotNil(_downloadError);
285+
XCTAssertEqual(_package.status, CCPackageStatusDownloadFailed);
286+
}
287+
288+
- (void)testCancelDownload
289+
{
290+
[_download start];
291+
[_download cancel];
292+
293+
// Can't wait for delegate since cancelling won't trigger them
294+
// Just wait a short amount of time and see if nothing has been written to disk
295+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]]];
296+
297+
NSFileManager *fileManager = [NSFileManager defaultManager];
298+
XCTAssertFalse([fileManager fileExistsAtPath:_download.localURL.path]);
299+
}
300+
301+
- (void)testPauseDownload
302+
{
303+
[_download start];
304+
[_download pause];
305+
306+
// Can't wait for delegate since cancelling won't trigger them
307+
// Just wait a short amount of time and see if nothing has been written to disk
308+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeInterval:0.5 sinceDate:[NSDate date]]];
309+
310+
XCTAssertEqual(_package.status, CCPackageStatusDownloadPaused);
311+
NSFileManager *fileManager = [NSFileManager defaultManager];
312+
NSString *tempName = [_download performSelector:@selector(createTempName)];
313+
314+
BOOL success = [fileManager fileExistsAtPath:[[_localURL.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:tempName]]
315+
|| [fileManager fileExistsAtPath:_download.localURL.path];
316+
317+
XCTAssertTrue(success, @"Temp file nor downloaded file exists.");
318+
}
319+
320+
- (void)testResumeDownload
321+
{
322+
[_download start];
323+
[_download pause];
324+
[_download resume];
325+
326+
while (!_downloadReturned)
327+
{
328+
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
329+
}
330+
331+
NSFileManager *fileManager = [NSFileManager defaultManager];
332+
NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
333+
XCTAssertTrue(_downloadSuccessful);
334+
XCTAssertTrue([fileManager fileExistsAtPath:_localURL.path]);
335+
XCTAssertEqual([attribs[NSFileSize] unsignedIntegerValue], __fileDownloadSize);
267336
}
268337

269338
#pragma mark - Helper
@@ -277,10 +346,8 @@ - (void)setupPartialDownloadOnDisk
277346
[data writeToFile:[[_localURL.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:tempName] atomically:YES];
278347
}
279348

280-
- (void)startDownloadAndWaitForDelegateToReturn
349+
- (void)waitForDelegateToReturn
281350
{
282-
[_download start];
283-
284351
while (!_downloadReturned)
285352
{
286353
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

cocos2d/CCPackageDownload.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
392392

393393
[self cancel];
394394

395+
[_package setValue:@(CCPackageStatusDownloadFailed) forKey:@"status"];
396+
395397
[_delegate downloadFailed:self error:error];
396398
}
397399

@@ -401,11 +403,13 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
401403
- (void)forwardResponseErrorToDelegate:(NSHTTPURLResponse *)httpResponse
402404
{
403405
NSError *error = [NSError errorWithDomain:@"Cocos2d"
404-
code:PACKAGE_ERROR_DOWNLOAD_SERVER_RESPONSE_NOT_OK
405-
userInfo:@{
406-
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Error: The host respondeded with status code %d.", [httpResponse statusCode]],
407-
@"HTTPResponse" : httpResponse
408-
}];
406+
code:PACKAGE_ERROR_DOWNLOAD_SERVER_RESPONSE_NOT_OK
407+
userInfo:@{
408+
NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Error: The host respondeded with status code %d.", [httpResponse statusCode]],
409+
@"HTTPResponse" : httpResponse
410+
}];
411+
412+
[_package setValue:@(CCPackageStatusDownloadFailed) forKey:@"status"];
409413

410414
[_delegate downloadFailed:self error:error];
411415
}

0 commit comments

Comments
 (0)