1010#import " CCPackageManager.h"
1111#import " CCPackage.h"
1212#import " CCFileUtils.h"
13+ #import " CCPackage_private.h"
14+ #import " CCPackageConstants.h"
15+ #import " CCPackageManagerDelegate.h"
1316
14- @interface CCPackageManagerTests : XCTestCase
17+ static NSString *const PACKAGE_BASE_URL = @" http://manager.test" ;
18+
19+ @interface CCPackageManagerTestURLProtocol : NSURLProtocol @end
20+
21+ @implementation CCPackageManagerTestURLProtocol
22+
23+ + (BOOL )canInitWithRequest : (NSURLRequest *)theRequest
24+ {
25+ return [theRequest.URL.scheme caseInsensitiveCompare: @" http" ] == NSOrderedSame;
26+ }
27+
28+ + (NSURLRequest *)canonicalRequestForRequest : (NSURLRequest *)theRequest
29+ {
30+ return theRequest;
31+ }
32+
33+ - (void )startLoading
34+ {
35+ NSData *data;
36+ NSHTTPURLResponse *response;
37+ if ([self .request.URL.absoluteString rangeOfString: PACKAGE_BASE_URL].location != NSNotFound )
38+ {
39+ NSString *pathToPackage = [[NSBundle mainBundle ] pathForResource: @" Resources-shared/Packages/testpackage-iOS-phonehd.zip" ofType: nil ];
40+ data = [NSData dataWithContentsOfFile: pathToPackage];
41+
42+ response = [[NSHTTPURLResponse alloc ] initWithURL: self .request.URL
43+ statusCode: 200
44+ HTTPVersion: @" HTTP/1.1"
45+ headerFields: nil ];
46+ }
47+ else
48+ {
49+ response = [[NSHTTPURLResponse alloc ] initWithURL: self .request.URL
50+ statusCode: 404
51+ HTTPVersion: @" HTTP/1.1"
52+ headerFields: nil ];
53+ }
54+
55+ id <NSURLProtocolClient > client = [self client ];
56+ [client URLProtocol: self didReceiveResponse: response cacheStoragePolicy: NSURLCacheStorageNotAllowed];
57+ [client URLProtocol: self didLoadData: data];
58+ [client URLProtocolDidFinishLoading: self ];
59+ }
60+
61+ - (void )stopLoading
62+ {
63+ // Nothing to do
64+ }
65+
66+ @end
67+
68+
69+ @interface CCPackageManagerTests : XCTestCase <CCPackageManagerDelegate>
1570
1671@property (nonatomic , strong ) CCPackageManager *packageManager;
72+ @property (nonatomic ) BOOL managerReturnedSuccessfully;
73+ @property (nonatomic ) BOOL managerReturnedFailed;
74+ @property (nonatomic , copy ) NSString *customFolderName;
75+ @property (nonatomic , strong ) NSError *managerReturnedWithError;
1776
1877@end
1978
79+
2080@implementation CCPackageManagerTests
2181
2282- (void )setUp
2383{
2484 [super setUp ];
2585 self.packageManager = [[CCPackageManager alloc ] init ];
86+ self.managerReturnedSuccessfully = NO ;
87+ self.managerReturnedFailed = NO ;
88+
89+ [[NSUserDefaults standardUserDefaults ] setObject: nil forKey: PACKAGE_STORAGE_USERDEFAULTS_KEY];
90+
91+ [NSURLProtocol registerClass: [CCPackageManagerTestURLProtocol class ]];
2692}
2793
94+ - (void )tearDown
95+ {
96+ [NSURLProtocol unregisterClass: [CCPackageManagerTestURLProtocol class ]];
97+
98+ // Delete all relevant folders: Download, unzip, install
99+ NSFileManager *fileManager = [NSFileManager defaultManager ];
100+ [fileManager removeItemAtPath: [NSTemporaryDirectory () stringByAppendingPathComponent: PACKAGE_REL_UNZIP_FOLDER] error: nil ];
101+ [fileManager removeItemAtPath: [NSTemporaryDirectory () stringByAppendingPathComponent: PACKAGE_REL_DOWNLOAD_FOLDER] error: nil ];
102+ [fileManager removeItemAtPath: _packageManager.installedPackagesPath error: nil ];
103+
104+ [super tearDown ];
105+ }
106+
107+
108+ #pragma mark - Tests
109+
28110- (void )testPackageWithName
29111{
30112 [CCFileUtils sharedFileUtils ].searchResolutionsOrder = [@[CCFileUtilsSuffixiPadHD] mutableCopy ];
@@ -41,4 +123,199 @@ - (void)testPackageWithName
41123 XCTAssertEqual (aPackage, result);
42124}
43125
126+ - (void )testSavePackages
127+ {
128+ CCPackage *package1 = [[CCPackage alloc ] initWithName: @" DLC1"
129+ resolution: @" phonehd"
130+ os: @" iOS"
131+ remoteURL: [NSURL URLWithString: @" http://foo.fake" ]];
132+ package1.installURL = [NSURL fileURLWithPath: @" /packages/DLC1-iOS-phonehd" ];
133+ package1.status = CCPackageStatusInitial;
134+
135+
136+ CCPackage *package2 = [[CCPackage alloc ] initWithName: @" DLC2"
137+ resolution: @" tablethd"
138+ os: @" iOS"
139+ remoteURL: [NSURL URLWithString: @" http://baa.fake" ]];
140+ package2.installURL = [NSURL fileURLWithPath: @" /packages/DLC2-iOS-tablethd" ];
141+ package2.status = CCPackageStatusInitial;
142+
143+ [_packageManager addPackage: package1];
144+ [_packageManager addPackage: package2];
145+
146+ [_packageManager savePackages ];
147+
148+ NSArray *packages = [[NSUserDefaults standardUserDefaults ] objectForKey: PACKAGE_STORAGE_USERDEFAULTS_KEY];
149+
150+ XCTAssertEqual (packages.count , 2 );
151+ // Note: Persistency of CCPackage is tested in CCPackageTests
152+ }
153+
154+ - (void )testLoadPackages
155+ {
156+ XCTFail (@" Not implemented yet." );
157+ }
158+
159+ - (void )testDownloadWithNameAndBaseURL
160+ {
161+ [CCFileUtils sharedFileUtils ].searchResolutionsOrder = [@[CCFileUtilsSuffixiPhoneHD] mutableCopy ];
162+
163+ _packageManager.baseURL = [NSURL URLWithString: PACKAGE_BASE_URL];
164+ _packageManager.delegate = self;
165+ CCPackage *package = [_packageManager downloadPackageWithName: @" testpackage" enableAfterDownload: YES ];
166+
167+ [self waitForDelegateToReturn ];
168+
169+ XCTAssertNotNil (package);
170+ XCTAssertTrue (_managerReturnedSuccessfully);
171+ }
172+
173+ - (void )testDownloadWithCustomFolderNameInPackage
174+ {
175+ // The installer used by the package manager will look into the unzipped contents and expect a folder
176+ // named after the standard identifier: Foo-iOS-phonehd.
177+ // Since the testpackage-iOS-phonehd is downloaded the delegate is used to correct this.
178+
179+ [CCFileUtils sharedFileUtils ].searchResolutionsOrder = [@[CCFileUtilsSuffixiPhoneHD] mutableCopy ];
180+
181+ _packageManager.baseURL = [NSURL URLWithString: PACKAGE_BASE_URL];
182+ _packageManager.delegate = self;
183+
184+ self.customFolderName = @" testpackage-iOS-phonehd" ;
185+
186+ CCPackage *package = [_packageManager downloadPackageWithName: @" Foo" enableAfterDownload: YES ];
187+
188+ [self waitForDelegateToReturn ];
189+
190+ XCTAssertNotNil (package);
191+ XCTAssertTrue (_managerReturnedSuccessfully);
192+ }
193+
194+ - (void )testCannotDetermineFolderNameWhenUnzipping
195+ {
196+ // Like in testDownloadWithCustomFolderNameInPackage but this time we expect an error and a failing delegate method
197+
198+ _packageManager.baseURL = [NSURL URLWithString: PACKAGE_BASE_URL];
199+ _packageManager.delegate = self;
200+
201+ CCPackage *package = [_packageManager downloadPackageWithName: @" Foo" enableAfterDownload: YES ];
202+
203+ [self waitForDelegateToReturn ];
204+
205+ XCTAssertNotNil (package);
206+ XCTAssertTrue (_managerReturnedFailed);
207+ XCTAssertEqual (_managerReturnedWithError.code , PACKAGE_ERROR_INSTALL_PACKAGE_FOLDER_NAME_NOT_FOUND);
208+ }
209+
210+ - (void )testDownloadWithoutBaseURLShouldFail
211+ {
212+ _packageManager.delegate = self;
213+ CCPackage *package = [_packageManager downloadPackageWithName: @" testpackage" enableAfterDownload: YES ];
214+
215+ [self waitForDelegateToReturn ];
216+
217+ XCTAssertNil (package);
218+ XCTAssertTrue (_managerReturnedFailed);
219+ XCTAssertEqual (_managerReturnedWithError.code , PACKAGE_ERROR_MANAGER_NO_BASE_URL_SET);
220+ }
221+
222+ - (void )testDownloadWithNameAndWithoutBaseURLUnzipOnACustomQueue
223+ {
224+ // Use a custom queue for unzipping
225+ XCTFail (@" Not implemented yet." );
226+ }
227+
228+ - (void )testDownloadOfPackageWithDifferentInstallPath
229+ {
230+ // add CCPackage and download:package
231+ // use different installPath
232+
233+ XCTFail (@" Not implemented yet." );
234+ }
235+
236+ - (void )testDisablePackage
237+ {
238+ XCTFail (@" Not implemented yet." );
239+ }
240+
241+ - (void )testEnablePackage
242+ {
243+ XCTFail (@" Not implemented yet." );
244+ }
245+
246+ - (void )testDeletePackage
247+ {
248+ XCTFail (@" Not implemented yet." );
249+ }
250+
251+ - (void )testCancelDownload
252+ {
253+ XCTFail (@" Not implemented yet." );
254+ }
255+
256+ - (void )testAllOtherDownloadRelatedMethods
257+ {
258+ /* - (void)resumeAllDownloads;
259+ * - (void)pauseAllDownloads;
260+ * - (void)pauseDownloadOfPackage:(CCPackage *)package;
261+ * - (void)resumeDownloadOfPackage:(CCPackage *)package;
262+ *
263+ * These should be already tests in the CCPackageDownloadManagerTests class as CCPackageManager is just delegating the class to that class.
264+ */
265+ }
266+
267+
268+ #pragma mark - CCPackageManagerDelegate
269+
270+ - (void )packageInstallationFinished : (CCPackage *)package
271+ {
272+ self.managerReturnedSuccessfully = YES ;
273+ }
274+
275+ - (void )packageInstallationFailed : (CCPackage *)package error : (NSError *)error
276+ {
277+ self.managerReturnedFailed = YES ;
278+ self.managerReturnedWithError = error;
279+ }
280+
281+ - (void )packageDownloadFinished : (CCPackage *)package
282+ {
283+ // Nothing to do at the moment
284+ }
285+
286+ - (void )packageDownloadFailed : (CCPackage *)package error : (NSError *)error
287+ {
288+ self.managerReturnedFailed = YES ;
289+ self.managerReturnedWithError = error;
290+ }
291+
292+ - (void )packageUnzippingFinished : (CCPackage *)package
293+ {
294+ // Nothing to do at the moment
295+ }
296+
297+ - (void )packageUnzippingFailed : (CCPackage *)package error : (NSError *)error
298+ {
299+ self.managerReturnedFailed = YES ;
300+ self.managerReturnedWithError = error;
301+ }
302+
303+ - (NSString *)customFolderName : (CCPackage *)package packageContents : (NSArray *)packageContents
304+ {
305+ return _customFolderName;
306+ }
307+
308+
309+
310+ #pragma mark - helper
311+
312+ - (void )waitForDelegateToReturn
313+ {
314+ while (!_managerReturnedFailed
315+ && !_managerReturnedSuccessfully)
316+ {
317+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture ]];
318+ }
319+ }
320+
44321@end
0 commit comments