Skip to content

Commit 503ccb1

Browse files
committed
CCPackageDownload will call delegate method if writing file fails.
1 parent 19686f5 commit 503ccb1

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

UnitTests/CCPackageDownloadTests.m

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ - (void)stopLoading
111111

112112
#pragma mark - test
113113

114-
115114
@interface CCPackageDownloadTests : XCTestCase <CCPackageDownloadDelegate>
116115

117116
@property (nonatomic, strong) CCPackageDownload *download;
@@ -133,7 +132,7 @@ - (void)setUp
133132

134133
[(AppController *)[UIApplication sharedApplication].delegate configureCocos2d];
135134
[[CCDirector sharedDirector] stopAnimation];
136-
// Sping the runloop a bit otherwise nondeterministic exceptions are thrown in the CCScheduler.
135+
// Spin the runloop a bit otherwise nondeterministic exceptions are thrown in the CCScheduler.
137136
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeInterval:0.2 sinceDate:[NSDate date]]];
138137

139138
[NSURLProtocol registerClass:[CCPackageDownloadTestURLProtocol class]];
@@ -256,6 +255,16 @@ - (void)testDownloadWith404Response
256255
XCTAssertNotNil(_downloadError);
257256
}
258257

258+
- (void)testDownloadFolderNotAccessible
259+
{
260+
// Writing to root level is supposed to fail
261+
[_download setValue:[NSURL fileURLWithPath:@"/test.zip"] forKey:NSStringFromSelector(@selector(localURL))];
262+
263+
[self startDownloadAndWaitForDelegateToReturn];
264+
265+
XCTAssertFalse(_downloadSuccessful);
266+
XCTAssertNotNil(_downloadError);
267+
}
259268

260269
#pragma mark - Helper
261270

cocos2d/CCPackageDownload.m

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ - (void)startDownloadAskingDelegateIfToResume:(BOOL)askDelegate
168168
return;
169169
}
170170

171-
if (![self createFileHandle])
171+
NSError *error;
172+
if (![self createFileHandle:&error])
172173
{
174+
[self connection:_connection didFailWithError:error];
173175
return;
174176
}
175177

@@ -264,16 +266,15 @@ - (NSURLRequest *)createRequest
264266
return result;
265267
}
266268

267-
- (BOOL)createFileHandle
269+
- (BOOL)createFileHandle:(NSError **)error
268270
{
269-
NSError *error;
270-
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingToURL:[NSURL fileURLWithPath:_tempPath] error:&error];
271+
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingToURL:[NSURL fileURLWithPath:_tempPath] error:error];
271272
CCLOGINFO(@"[PACKAGE/DOWNLOAD][INFO] Opening/Creating file for download: %@", _tempPath);
272273

273274
if (!fileHandle)
274275
{
275276
[[NSFileManager defaultManager] createFileAtPath:_tempPath contents:nil attributes:nil];
276-
fileHandle = [NSFileHandle fileHandleForWritingToURL:[NSURL fileURLWithPath:_tempPath] error:&error];
277+
fileHandle = [NSFileHandle fileHandleForWritingToURL:[NSURL fileURLWithPath:_tempPath] error:error];
277278
}
278279

279280
if (!fileHandle)

0 commit comments

Comments
 (0)