11
11
#import " CCPackageDownload.h"
12
12
#import " CCPackage.h"
13
13
#import " CCPackageDownloadDelegate.h"
14
+ #import " CCDirector.h"
14
15
15
16
static NSUInteger __fileDownloadSize = 0 ;
16
17
static BOOL __support_range_request = YES ;
@@ -58,7 +59,6 @@ - (void)startLoading
58
59
{
59
60
NSMutableDictionary *headers = [NSMutableDictionary dictionary ];
60
61
61
-
62
62
NSFileManager *fileManager = [NSFileManager defaultManager ];
63
63
NSString *fileName = [self .request.URL lastPathComponent ];
64
64
NSString *pathToPackage = [[NSBundle mainBundle ] pathForResource: [NSString stringWithFormat: @" Resources-shared/Packages/%@ " , fileName] ofType: nil ];
@@ -120,6 +120,7 @@ @interface CCPackageDownloadTests : XCTestCase <CCPackageDownloadDelegate>
120
120
@property (nonatomic ) BOOL downloadSuccessful;
121
121
@property (nonatomic , strong ) NSError *downloadError;
122
122
@property (nonatomic , copy ) NSURL *localURL;
123
+ @property (nonatomic ) BOOL shouldOverwriteDownloadedFile;
123
124
124
125
@end
125
126
@@ -129,6 +130,9 @@ - (void)setUp
129
130
{
130
131
[super setUp ];
131
132
133
+ [[CCDirector sharedDirector ] stopAnimation ];
134
+
135
+
132
136
[NSURLProtocol registerClass: [CCPackageDownloadTestURLProtocol class ]];
133
137
134
138
self.downloadPath = [NSTemporaryDirectory () stringByAppendingPathComponent: @" Downloads" ];
@@ -139,6 +143,7 @@ - (void)setUp
139
143
self.downloadReturned = NO ;
140
144
self.downloadError = nil ;
141
145
self.downloadSuccessful = NO ;
146
+ self.shouldOverwriteDownloadedFile ;
142
147
143
148
self.package = [[CCPackage alloc ] initWithName: @" testpackage"
144
149
resolution: @" phonehd"
@@ -154,6 +159,8 @@ - (void)tearDown
154
159
{
155
160
[NSURLProtocol unregisterClass: [CCPackageDownloadTestURLProtocol class ]];
156
161
162
+ [[CCDirector sharedDirector ] startAnimation ];
163
+
157
164
[super tearDown ];
158
165
}
159
166
@@ -182,7 +189,6 @@ - (void)createDownloadFolder
182
189
183
190
184
191
#pragma mark - tests
185
-
186
192
- (void )testDownloadPackage
187
193
{
188
194
[self waitForDelegateToReturnAfterRunningBlock: ^
@@ -207,7 +213,10 @@ - (void)waitForDelegateToReturnAfterRunningBlock:(dispatch_block_t)block
207
213
}
208
214
}
209
215
210
- - (void )testRangeRequest
216
+ /*
217
+
218
+
219
+ - (void)testResumeDownloadAKARangeRequest
211
220
{
212
221
[self setupPartialDownloadOnDisk];
213
222
@@ -232,18 +241,52 @@ - (void)setupPartialDownloadOnDisk
232
241
[data writeToFile:[[_localURL.path stringByDeletingLastPathComponent] stringByAppendingPathComponent:tempName] atomically:YES];
233
242
}
234
243
235
- /*
236
- - (void)testResumeDownloadedPackage
244
+ - (void)testDownloadOfExistingFile
237
245
{
246
+ self.shouldOverwriteDownloadedFile = NO;
247
+
248
+ NSUInteger filesize = [self createDownloadFile];
238
249
250
+ [self waitForDelegateToReturnAfterRunningBlock:^
251
+ {
252
+ [_download start];
253
+ }];
254
+
255
+ NSFileManager *fileManager = [NSFileManager defaultManager];;
256
+ NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
257
+ XCTAssertTrue(_downloadSuccessful);
258
+ XCTAssertTrue([fileManager fileExistsAtPath:_localURL.path]);
259
+ XCTAssertEqual([attribs[NSFileSize] unsignedIntegerValue], filesize);
239
260
}
240
261
241
- - (void)testPauseDownload
262
+ - (void)testOverwriteExistingDownload
242
263
{
264
+ self.shouldOverwriteDownloadedFile = YES;
265
+
266
+ [self createDownloadFile];
267
+
268
+ [self waitForDelegateToReturnAfterRunningBlock:^
269
+ {
270
+ [_download start];
271
+ }];
243
272
273
+ NSFileManager *fileManager = [NSFileManager defaultManager];;
274
+ NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
275
+ XCTAssertTrue(_downloadSuccessful);
276
+ XCTAssertTrue([fileManager fileExistsAtPath:_localURL.path]);
277
+ XCTAssertEqual([attribs[NSFileSize] unsignedIntegerValue], __fileDownloadSize);
244
278
}
245
- */
246
279
280
+ - (NSUInteger)createDownloadFile
281
+ {
282
+ NSFileManager *fileManager = [NSFileManager defaultManager];
283
+ [fileManager createFileAtPath:_localURL.path
284
+ contents:[@"nothing in here, really" dataUsingEncoding:NSUTF8StringEncoding]
285
+ attributes:nil];
286
+
287
+ NSDictionary *attribs = [fileManager attributesOfItemAtPath:_localURL.path error:nil];
288
+ return [attribs[NSFileSize] unsignedIntegerValue];
289
+ }
247
290
248
291
- (void)testDownloadWith404Response
249
292
{
@@ -257,7 +300,7 @@ - (void)testDownloadWith404Response
257
300
XCTAssertFalse(_downloadSuccessful);
258
301
XCTAssertNotNil(_downloadError);
259
302
}
260
-
303
+ */
261
304
262
305
#pragma mark - CCPackageDownloadDelegate
263
306
@@ -279,4 +322,10 @@ - (BOOL)shouldResumeDownload:(CCPackageDownload *)download
279
322
return YES ;
280
323
}
281
324
325
+ - (BOOL )shouldOverwriteDownloadedFile : (CCPackageDownload *)download
326
+ {
327
+ return _shouldOverwriteDownloadedFile;
328
+ }
329
+
330
+
282
331
@end
0 commit comments