Skip to content

Commit 50c0d01

Browse files
committed
Packages loaded from bundle, no need for an internet connection anymore.
Test packages added to bundle.
1 parent 9dfa2d7 commit 50c0d01

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

cocos2d-ui-tests/tests/CCPackageTest.m

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,65 @@
1313
#import "CCPackageConstants.h"
1414
#import "AppDelegate.h"
1515

16+
17+
18+
@interface CCPackageTestURLProtocol : NSURLProtocol
19+
20+
@end
21+
22+
23+
@implementation CCPackageTestURLProtocol
24+
25+
+ (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
26+
{
27+
return [theRequest.URL.scheme caseInsensitiveCompare:@"http"] == NSOrderedSame;
28+
}
29+
30+
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)theRequest
31+
{
32+
return theRequest;
33+
}
34+
35+
- (void)startLoading
36+
{
37+
NSString *fileName = [self.request.URL lastPathComponent];
38+
39+
NSString *pathToPackage = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Resources-shared/Packages/%@", fileName] ofType:nil];
40+
NSData *data = [NSData dataWithContentsOfFile:pathToPackage];
41+
42+
NSHTTPURLResponse *response;
43+
if (pathToPackage)
44+
{
45+
response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
46+
statusCode:200
47+
HTTPVersion:@"HTTP/1.1"
48+
headerFields:nil];
49+
}
50+
else
51+
{
52+
response = [[NSHTTPURLResponse alloc] initWithURL:self.request.URL
53+
statusCode:404
54+
HTTPVersion:@"HTTP/1.1"
55+
headerFields:nil];
56+
}
57+
58+
59+
id<NSURLProtocolClient> client = [self client];
60+
[client URLProtocol:self didLoadData:data];
61+
[client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
62+
[client URLProtocolDidFinishLoading:self];
63+
}
64+
65+
- (void)stopLoading
66+
{
67+
NSLog(@"Package test: Something went wrong.");
68+
}
69+
70+
@end
71+
72+
73+
#pragma mark - Test class
74+
1675
@interface CCPackageTest : TestBase <CCPackageManagerDelegate>
1776

1877
@property (nonatomic, strong) CCPackage *package;
@@ -24,10 +83,11 @@ @implementation CCPackageTest
2483

2584
- (void) setupPackageTest
2685
{
86+
[self setupLocalHTTPRequests];
87+
2788
[self.contentNode removeAllChildren];
2889

29-
[[NSUserDefaults standardUserDefaults] removeObjectForKey:PACKAGE_STORAGE_USERDEFAULTS_KEY];
30-
[[NSUserDefaults standardUserDefaults] synchronize];
90+
[self removePersistedPackages];
3191

3292
[self removeAllPackages];
3393

@@ -41,10 +101,21 @@ - (void) setupPackageTest
41101

42102
self.package = [[CCPackageManager sharedManager] downloadPackageWithName:@"testpackage"
43103
resolution:@"phonehd"
44-
remoteURL:[NSURL URLWithString:@"https://github.com/NickyWeber/cocos2d_test_resources/raw/master/testpackage-iOS-phonehd.zip?raw=true"]
104+
remoteURL:[NSURL URLWithString:@"http://package.request.fake/testpackage-iOS-phonehd.zip"]
45105
enableAfterDownload:YES];
46106
}
47107

108+
- (void)setupLocalHTTPRequests
109+
{
110+
[NSURLProtocol registerClass:[CCPackageTestURLProtocol class]];
111+
}
112+
113+
- (void)removePersistedPackages
114+
{
115+
[[NSUserDefaults standardUserDefaults] removeObjectForKey:PACKAGE_STORAGE_USERDEFAULTS_KEY];
116+
[[NSUserDefaults standardUserDefaults] synchronize];
117+
}
118+
48119
- (void)addLabels
49120
{
50121
CGSize winSize = [CCDirector sharedDirector].viewSize;
@@ -138,26 +209,31 @@ - (void)packageInstallationFinished:(CCPackage *)package
138209
- (void)packageInstallationFailed:(CCPackage *)package error:(NSError *)error
139210
{
140211
self.subTitle = [NSString stringWithFormat:@"Test failed: installation failed."];
212+
[NSURLProtocol unregisterClass:[CCPackageTestURLProtocol class]];
141213
}
142214

143215
- (void)packageDownloadFinished:(CCPackage *)package
144216
{
145217
self.subTitle = [NSString stringWithFormat:@"Download finished"];
218+
[NSURLProtocol unregisterClass:[CCPackageTestURLProtocol class]];
146219
}
147220

148221
- (void)packageDownloadFailed:(CCPackage *)package error:(NSError *)error
149222
{
150223
self.subTitle = [NSString stringWithFormat:@"Test failed: download failed."];
224+
[NSURLProtocol unregisterClass:[CCPackageTestURLProtocol class]];
151225
}
152226

153227
- (void)packageUnzippingFinished:(CCPackage *)package
154228
{
155229
self.subTitle = [NSString stringWithFormat:@"Unzip finished"];
230+
[NSURLProtocol unregisterClass:[CCPackageTestURLProtocol class]];
156231
}
157232

158233
- (void)packageUnzippingFailed:(CCPackage *)package error:(NSError *)error
159234
{
160235
self.subTitle = [NSString stringWithFormat:@"Test failed: unzipping failed."];
236+
[NSURLProtocol unregisterClass:[CCPackageTestURLProtocol class]];
161237
}
162238

163239
- (void)packageDownloadProgress:(CCPackage *)package downloadedBytes:(NSUInteger)downloadedBytes totalBytes:(NSUInteger)totalBytes
@@ -170,5 +246,4 @@ - (void)packageUnzippingProgress:(CCPackage *)package unzippedBytes:(NSUInteger)
170246
NSLog(@"unzipping... %u / %u", unzippedBytes, totalBytes);
171247
}
172248

173-
174249
@end

0 commit comments

Comments
 (0)