77
88#import < XCTest/XCTest.h>
99#import " cocos2d.h"
10-
10+ # import " CCUnitTestAssertions.h "
1111
1212
1313@interface CCFileUtilTests : XCTestCase
@@ -20,10 +20,29 @@ +(void) resetSingleton;
2020
2121@implementation CCFileUtilTests
2222
23+ - (void )setUp
24+ {
25+ [super setUp ];
26+
27+ [CCFileUtils resetSingleton ];
28+
29+ CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils ];
30+
31+ sharedFileUtils.directoriesDict =
32+ [@{CCFileUtilsSuffixiPad : @" resources-tablet" ,
33+ CCFileUtilsSuffixiPadHD : @" resources-tablethd" ,
34+ CCFileUtilsSuffixiPhone : @" resources-phone" ,
35+ CCFileUtilsSuffixiPhoneHD : @" resources-phonehd" ,
36+ CCFileUtilsSuffixiPhone5 : @" resources-phone" ,
37+ CCFileUtilsSuffixiPhone5HD : @" resources-phonehd" ,
38+ CCFileUtilsSuffixDefault : @" " } mutableCopy];
39+
40+ sharedFileUtils.searchMode = CCFileUtilsSearchModeDirectory;
41+ [sharedFileUtils buildSearchResolutionsOrder ];
42+ }
2343
2444-(void )testFullPathForFilenameMissingFile
2545{
26- [CCFileUtils resetSingleton ];
2746 CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils ];
2847
2948 NSString *path = [sharedFileUtils fullPathForFilename: @" file that does not exist" ];
@@ -33,8 +52,6 @@ -(void)testFullPathForFilenameMissingFile
3352 // File does not exist in this directory
3453 path = [sharedFileUtils fullPathForFilename: @" powered.png" ];
3554 XCTAssertTrue (path == nil , @" " );
36-
37-
3855}
3956
4057// XCode Unit tests look inside the target's test application bundle - not the unit test app bundle, but the "cocos2d-tests-ios.app" bundle.
@@ -48,20 +65,64 @@ -(void)testFullPathForFilename
4865 XCTAssertTrue (path != nil , @" " );
4966}
5067
68+ - (void )testFullPathsOfFileNameInAllSearchPaths
69+ {
70+ NSString *pathToUnzippedPackage = [[[NSBundle mainBundle ] bundlePath ] stringByAppendingPathComponent: @" Resources-shared/Packages/testpackage-iOS-phonehd_unzipped/testpackage-iOS-phonehd" ];
71+ NSString *pathToUnzippedPackage2 = [[[NSBundle mainBundle ] bundlePath ] stringByAppendingPathComponent: @" Resources-shared/Packages/anotherpackage-iOS-phonehd_unzipped/anotherpackage-iOS-phonehd" ];
72+
73+ [CCFileUtils sharedFileUtils ].searchPath = @[pathToUnzippedPackage, pathToUnzippedPackage2];
74+
75+ NSArray *paths = [[CCFileUtils sharedFileUtils ] fullPathsOfFileNameInAllSearchPaths: @" fileLookup.plist" ];
76+ XCTAssertEqual (paths.count , 2 );
77+ }
78+
79+ - (void )testLoadFileNameLookupsInAllSearchPaths
80+ {
81+ NSString *pathToPackage = [NSTemporaryDirectory () stringByAppendingPathComponent: @" pack1" ];
82+ NSString *pathToPackage2 = [NSTemporaryDirectory () stringByAppendingPathComponent: @" pack2" ];
83+
84+ [CCFileUtils sharedFileUtils ].searchPath = @[pathToPackage, pathToPackage2];
85+
86+ NSFileManager *fileManager = [NSFileManager defaultManager ];
87+ [fileManager createDirectoryAtPath: pathToPackage withIntermediateDirectories: YES attributes: nil error: nil ];
88+ [fileManager createDirectoryAtPath: pathToPackage2 withIntermediateDirectories: YES attributes: nil error: nil ];
89+
90+ NSDictionary *lookup1 = @{
91+ @" filenames" : @{
92+ @" foo.wav" : @" foo.mp4"
93+ },
94+ @" metadata" : @{
95+ @" version" : @1
96+ }
97+ };
98+ [lookup1 writeToFile: [pathToPackage stringByAppendingPathComponent: @" fileLookup.plist" ] atomically: YES ];
99+
100+ NSDictionary *lookup2 = @{
101+ @" filenames" : @{
102+ @" baa.psd" : @" baa.png"
103+ },
104+ @" metadata" : @{
105+ @" version" : @1
106+ }
107+ };
108+ [lookup2 writeToFile: [pathToPackage2 stringByAppendingPathComponent: @" fileLookup.plist" ] atomically: YES ];
109+
110+ [[CCFileUtils sharedFileUtils ] loadFileNameLookupsInAllSearchPathsWithName: @" fileLookup.plist" ];
111+ NSDictionary *filenameLookup = [CCFileUtils sharedFileUtils ].filenameLookup ;
112+ CCAssertEqualStrings (filenameLookup[@" baa.psd" ], @" baa.png" );
113+ CCAssertEqualStrings (filenameLookup[@" foo.wav" ], @" foo.mp4" );
114+ }
115+
51116-(void )testCCFileUtilsSearchModeSuffix
52117{
53- [CCFileUtils resetSingleton ];
54118 CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils ];
55119 sharedFileUtils.searchMode = CCFileUtilsSearchModeSuffix;
56120
57121 XCTAssertTrue ([sharedFileUtils fullPathForFilename: @" Images/powered.png" ] != nil , @" " );
58-
59-
60122}
61123
62124-(void )testCCFileUtilsSearchModeDirectory
63125{
64- [CCFileUtils resetSingleton ];
65126 CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils ];
66127 sharedFileUtils.searchMode = CCFileUtilsSearchModeDirectory;
67128
@@ -80,20 +141,16 @@ -(void)testCCFileUtilsSearchModeDirectory
80141
81142-(void )testCustomSearchPathsForExtensions
82143{
83- [CCFileUtils resetSingleton ];
84144 CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils ];
85145 [sharedFileUtils purgeCachedEntries ];
86146 [sharedFileUtils setSearchPath: @[ @" Images" , kCCFileUtilsDefaultSearchPath ] ];
87147
88148 XCTAssertTrue ( [sharedFileUtils fullPathForFilename: @" Images/powered.png" contentScale: nil ] != nil , @" " );
89149 XCTAssertTrue ( [sharedFileUtils fullPathForFilename: @" powered.png" contentScale: nil ] != nil , @" Search path 'Images' didn't work." );
90-
91150}
92151
93-
94152-(void )testContentScaleLoading
95153{
96- [CCFileUtils resetSingleton ];
97154 CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils ];
98155 sharedFileUtils.searchMode = CCFileUtilsSearchModeSuffix;
99156
@@ -103,15 +160,11 @@ -(void)testContentScaleLoading
103160 [ (NSMutableArray *) [sharedFileUtils searchResolutionsOrder ] removeAllObjects ];
104161 [ (NSMutableArray *) [sharedFileUtils searchResolutionsOrder ] addObject: CCFileUtilsSuffixiPhoneHD];
105162
106-
107163 CGFloat scale = 0.0 ;
108164
109165 NSString *path1 = [sharedFileUtils fullPathForFilename: @" Images/blocks.png" contentScale: &scale];
110166 XCTAssertTrue (scale == 2.0 , @" " );
111167 XCTAssertTrue (path1 != nil , @" " );
112-
113168}
114169
115-
116-
117170@end
0 commit comments