|
| 1 | +#import <XCTest/XCTest.h> |
| 2 | +#import "cocos2d.h" |
| 3 | + |
| 4 | +#import "CCTextureCache.h" |
| 5 | + |
| 6 | +@interface CCTextureTests : XCTestCase |
| 7 | +@end |
| 8 | + |
| 9 | +@implementation CCTextureTests |
| 10 | + |
| 11 | +-(void)testTextureCache |
| 12 | +{ |
| 13 | + __weak CCTexture *textures[4]; |
| 14 | + __weak CCRenderState *renderStates[3]; |
| 15 | + |
| 16 | + @autoreleasepool { |
| 17 | + // Load some cached textures |
| 18 | + for(int i=0; i<4; i++){ |
| 19 | + NSString *name = [NSString stringWithFormat:@"Images/grossini_dance_0%d.png", i + 1]; |
| 20 | + textures[i] = [CCTexture textureWithFile:name]; |
| 21 | + } |
| 22 | + |
| 23 | + // Make sure the textures were loaded. |
| 24 | + for(int i=0; i<4; i++){ |
| 25 | + XCTAssertNotNil(textures[i], @"Texture %d not loaded.", i); |
| 26 | + } |
| 27 | + |
| 28 | + // Create render states for the textures. |
| 29 | + CCBlendMode *blend = [CCBlendMode premultipliedAlphaMode]; |
| 30 | + CCShader *shader = [CCShader positionColorShader]; |
| 31 | + |
| 32 | + // A cached render state.. |
| 33 | + renderStates[0] = [CCRenderState renderStateWithBlendMode:blend shader:shader mainTexture:textures[0]]; |
| 34 | + // An uncached, immutable render state. |
| 35 | + NSDictionary *uniforms1 = @{@"SomeTexture": textures[1]}; |
| 36 | + renderStates[1] = [CCRenderState renderStateWithBlendMode:blend shader:shader shaderUniforms:uniforms1 copyUniforms:YES]; |
| 37 | + // An uncached, mutable render state. |
| 38 | + NSMutableDictionary *uniforms2 = [NSMutableDictionary dictionaryWithObject:textures[2] forKey:@"SomeTexture"]; |
| 39 | + renderStates[2] = [CCRenderState renderStateWithBlendMode:blend shader:shader shaderUniforms:uniforms2 copyUniforms:NO]; |
| 40 | + // Leave textures[3] unused. |
| 41 | + |
| 42 | + // Make sure the render states were loaded |
| 43 | + for(int i=0; i<3; i++){ |
| 44 | + XCTAssertNotNil(renderStates[i], @"Render state %d not loaded.", i); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Flush the texture cache |
| 49 | + [[CCTextureCache sharedTextureCache] removeUnusedTextures]; |
| 50 | + |
| 51 | + // Make sure the textures were unloaded. |
| 52 | + for(int i=0; i<4; i++){ |
| 53 | + XCTAssertNil(textures[i], @"Texture %d still loaded.", i); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +@end |
0 commit comments