@@ -194,7 +194,10 @@ - (void)createDownloadFolder
194
194
195
195
- (void )testDownloadPackage
196
196
{
197
- [self startDownloadAndWaitForDelegateToReturn ];
197
+ [_download start ];
198
+ XCTAssertEqual (_package.status , CCPackageStatusDownloading);
199
+
200
+ [self waitForDelegateToReturn ];
198
201
199
202
NSFileManager *fileManager = [NSFileManager defaultManager ];
200
203
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
@@ -207,7 +210,10 @@ - (void)testResumeDownloadAKARangeRequest
207
210
{
208
211
[self setupPartialDownloadOnDisk ];
209
212
210
- [self startDownloadAndWaitForDelegateToReturn ];
213
+ [_download start ];
214
+ XCTAssertEqual (_package.status , CCPackageStatusDownloading);
215
+
216
+ [self waitForDelegateToReturn ];
211
217
NSFileManager *fileManager = [NSFileManager defaultManager ];
212
218
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
213
219
XCTAssertTrue (_downloadSuccessful);
@@ -221,7 +227,10 @@ - (void)testDownloadOfExistingFile
221
227
222
228
NSUInteger filesize = [self createDownloadFile ];
223
229
224
- [self startDownloadAndWaitForDelegateToReturn ];
230
+ [_download start ];
231
+ XCTAssertEqual (_package.status , CCPackageStatusDownloaded);
232
+
233
+ [self waitForDelegateToReturn ];
225
234
226
235
NSFileManager *fileManager = [NSFileManager defaultManager ];;
227
236
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
@@ -236,7 +245,10 @@ - (void)testOverwriteExistingDownload
236
245
237
246
[self createDownloadFile ];
238
247
239
- [self startDownloadAndWaitForDelegateToReturn ];
248
+ [_download start ];
249
+ XCTAssertEqual (_package.status , CCPackageStatusDownloading);
250
+
251
+ [self waitForDelegateToReturn ];
240
252
241
253
NSFileManager *fileManager = [NSFileManager defaultManager ];;
242
254
NSDictionary *attribs = [fileManager attributesOfItemAtPath: _localURL.path error: nil ];
@@ -249,21 +261,78 @@ - (void)testDownloadWith404Response
249
261
{
250
262
[_package setValue: [NSURL URLWithString: @" http://package.request.fake/DOES_NOT_EXIST.zip" ] forKey: NSStringFromSelector (@selector (remoteURL ))];
251
263
252
- [self startDownloadAndWaitForDelegateToReturn ];
264
+ [_download start ];
265
+ XCTAssertEqual (_package.status , CCPackageStatusDownloading);
266
+
267
+ [self waitForDelegateToReturn ];
253
268
254
269
XCTAssertFalse (_downloadSuccessful);
255
270
XCTAssertNotNil (_downloadError);
271
+ XCTAssertEqual (_package.status , CCPackageStatusDownloadFailed);
256
272
}
257
273
258
274
- (void )testDownloadFolderNotAccessible
259
275
{
260
276
// Writing to root level is supposed to fail
261
277
[_download setValue: [NSURL fileURLWithPath: @" /test.zip" ] forKey: NSStringFromSelector (@selector (localURL ))];
262
278
263
- [self startDownloadAndWaitForDelegateToReturn ];
279
+ [_download start ];
280
+
281
+ [self waitForDelegateToReturn ];
264
282
265
283
XCTAssertFalse (_downloadSuccessful);
266
284
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);
267
336
}
268
337
269
338
#pragma mark - Helper
@@ -277,10 +346,8 @@ - (void)setupPartialDownloadOnDisk
277
346
[data writeToFile: [[_localURL.path stringByDeletingLastPathComponent ] stringByAppendingPathComponent: tempName] atomically: YES ];
278
347
}
279
348
280
- - (void )startDownloadAndWaitForDelegateToReturn
349
+ - (void )waitForDelegateToReturn
281
350
{
282
- [_download start ];
283
-
284
351
while (!_downloadReturned)
285
352
{
286
353
[[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture ]];
0 commit comments