Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Added automated test to Xcode project plus needed changes to SDL_RWFr…
Browse files Browse the repository at this point in the history
…omFile to be OS X bundle aware.

The Mac OS X project has a new target called testsdl which builds the automated test. I looked at using Xcode's native unit test support, but the impedance mismatch between the existing automated test structure and Apple's was more than I could handle.

As such, the testsdl application is a full blown proper OS X application, which means it is a self-contained .app bundle. This immediately revealed some problems from the automated test. The largest problem was the assumption about the current working directory and where to find resources. (I suspect Windows may have a similar problem depending on circumstance.) To open resources, the test was looking in directories relative to the SDL source directory, but this will not work well with self-contained .app bundles and Xcode which can place its built applications almost anywhere. And for real-world situations, this is pretty useless anyway.

So I modified SDL_RWFromFile to do special things on OS X. First, it will look for a file in the .app bundle. If not found, it will fallback and just call fopen as it used to do.

I also had to modify the automated test itself because it had a contrieved test which called fopen directly to do read from an existing FILE pointer. In addition, there was a write test. Since a .app bundle is likely going to be read-only, I added a special case for OS X to write to NSTemporaryDirectory.

I expect these changes should work for both Mac and iPhone OS (which includes iPad).

I will update the iPhone Xcode project next.

Finally, FYI, the X11 automated test seems to be failing. Below is my output.


Pending breakpoint 4 - "-[NSException raise]" resolved
Platform : All tests successful (2)
SDL_RWops : All tests successful (5)
Rect : All tests successful (1)
SDL_Surface : All tests successful (6)
Rendering with cocoa driver : All tests successful (3)
Assert Failed!
   Blit output not the same.
   Test Case 'Renderer x11'
   Test Suite 'Rendering with x11 driver'
   Last SDL error ''
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetWindowBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetWindowBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetWindowBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetWindowBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetWindowBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetWindowBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSGetSurfaceBounds
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Sat May  8 00:30:34 iMacAL.local testsdl[71586] <Error>: kCGErrorIllegalArgument: CGSBindSurface: Invalid window 0xa150
Rendering with x11 driver : Failed 1 out of 4 testcases!
Rendering with dummy driver : All tests successful (3)
SDL_Audio : All tests successful (1)
Tests run with SDL 1.3.0 revision 1095906
System is running Mac OS X and is little endian
  • Loading branch information
Eric Wing committed May 8, 2010
1 parent c644da5 commit a60ff48
Show file tree
Hide file tree
Showing 8 changed files with 550 additions and 6 deletions.
404 changes: 403 additions & 1 deletion Xcode/SDL/SDL.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions src/file/SDL_rwops.c
Expand Up @@ -28,6 +28,10 @@
#include "SDL_endian.h"
#include "SDL_rwops.h"

#ifdef __APPLE__
#include "SDL_rwopsbundlesupport.h"
#endif /* __APPLE__ */

#ifdef __NDS__
/* include libfat headers for fatInitDefault(). */
#include <fat.h>
Expand Down Expand Up @@ -459,9 +463,12 @@ SDL_RWFromFile(const char *file, const char *mode)
rwops->close = win32_file_close;

#elif HAVE_STDIO_H

fp = fopen(file, mode);
if (fp == NULL) {
#ifdef __APPLE__
fp = SDL_OpenFPFromBundleOrFallback(file, mode);
#else
fp = fopen(file, mode);
#endif
if (fp == NULL) {
SDL_SetError("Couldn't open %s", file);
} else {
rwops = SDL_RWFromFP(fp, 1);
Expand Down
9 changes: 9 additions & 0 deletions src/file/cocoa/SDL_rwopsbundlesupport.h
@@ -0,0 +1,9 @@
#ifdef __APPLE__

#include <stdio.h>

#ifndef SDL_rwopsbundlesupport_h
#define SDL_rwopsbundlesupport_h
FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode);
#endif
#endif
37 changes: 37 additions & 0 deletions src/file/cocoa/SDL_rwopsbundlesupport.m
@@ -0,0 +1,37 @@
#ifdef __APPLE__
#import <Foundation/Foundation.h>

/* For proper OS X applications, the resources are contained inside the application bundle.
So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
but we would somehow need to know what the bundle identifiers we need to search are.
Also, note the bundle layouts are different for iPhone and Mac.
*/
FILE* SDL_OpenFPFromBundleOrFallback(const char *file, const char *mode)
{
FILE* fp = NULL;

NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];


NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
if([file_manager fileExistsAtPath:full_path_with_file_to_try])
{
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
}
else
{
fp = fopen(file, mode);
}

[autorelease_pool drain];

return fp;
}
#endif
11 changes: 11 additions & 0 deletions test/automated/rwops/Test_rwopsbundlesupport.h
@@ -0,0 +1,11 @@
#ifdef __APPLE__

#include <stdio.h>

#ifndef SDL_rwopsbundlesupport_h
#define SDL_rwopsbundlesupport_h
FILE* Test_OpenFPFromBundleOrFallback(const char *file, const char *mode);
FILE* Test_OpenFPFromTemporaryDir(const char *file, const char *mode);

#endif
#endif
56 changes: 56 additions & 0 deletions test/automated/rwops/Test_rwopsbundlesupport.m
@@ -0,0 +1,56 @@
#ifdef __APPLE__
#import <Foundation/Foundation.h>

/* For proper OS X applications, the resources are contained inside the application bundle.
So the strategy is to first check the application bundle for the file, then fallback to the current working directory.
Note: One additional corner-case is if the resource is in a framework's resource bundle instead of the app.
We might want to use bundle identifiers, e.g. org.libsdl.sdl to get the bundle for the framework,
but we would somehow need to know what the bundle identifiers we need to search are.
Also, note the bundle layouts are different for iPhone and Mac.
*/
FILE* Test_OpenFPFromBundleOrFallback(const char *file, const char *mode)
{
FILE* fp = NULL;

NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];


NSFileManager* file_manager = [NSFileManager defaultManager];
NSString* resource_path = [[NSBundle mainBundle] resourcePath];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [resource_path stringByAppendingPathComponent:ns_string_file_component];
if([file_manager fileExistsAtPath:full_path_with_file_to_try])
{
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);
}
else
{
fp = fopen(file, mode);
}

[autorelease_pool drain];

return fp;
}

FILE* Test_OpenFPFromTemporaryDir(const char *file, const char *mode)
{
FILE* fp = NULL;

NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];


NSFileManager* file_manager = [NSFileManager defaultManager];

NSString* ns_string_file_component = [file_manager stringWithFileSystemRepresentation:file length:strlen(file)];

NSString* full_path_with_file_to_try = [NSTemporaryDirectory() stringByAppendingPathComponent:ns_string_file_component];
fp = fopen([full_path_with_file_to_try fileSystemRepresentation], mode);

[autorelease_pool drain];

return fp;
}
#endif
19 changes: 18 additions & 1 deletion test/automated/rwops/rwops.c
Expand Up @@ -9,10 +9,17 @@

#include "SDL.h"
#include "../SDL_at.h"

#if __APPLE__
#include "Test_rwopsbundlesupport.h"
#endif

#define RWOPS_READ "rwops/read"

#if __APPLE__
#define RWOPS_WRITE "write"
#else
#define RWOPS_WRITE "rwops/write"
#endif

static const char hello_world[] = "Hello World!";

Expand Down Expand Up @@ -219,7 +226,12 @@ static void rwops_testFP (void)
SDL_ATbegin( "SDL_RWFromFP" );

/* Run read tests. */
#if __APPLE__
/* Cheating: Using private API in SDL */
fp = Test_OpenFPFromBundleOrFallback( RWOPS_READ, "r" );
#else
fp = fopen( RWOPS_READ, "r" );
#endif
if (SDL_ATassert( "Failed to open file '"RWOPS_READ"'", fp != NULL))
return;
rw = SDL_RWFromFP( fp, 1 );
Expand All @@ -230,7 +242,12 @@ static void rwops_testFP (void)
SDL_FreeRW( rw );

/* Run write tests. */
#if __APPLE__
/* Cheating: Using private API in SDL */
fp = Test_OpenFPFromTemporaryDir( RWOPS_WRITE, "w+" );
#else
fp = fopen( RWOPS_WRITE, "w+" );
#endif
if (SDL_ATassert( "Failed to open file '"RWOPS_WRITE"'", fp != NULL))
return;
rw = SDL_RWFromFP( fp, 1 );
Expand Down
7 changes: 6 additions & 1 deletion test/automated/surface/surface.c
Expand Up @@ -50,7 +50,12 @@ static void surface_testLoad( SDL_Surface *testsur )
return;

/* Create the blit surface. */
face = SDL_LoadBMP("../icon.bmp");
#ifdef __APPLE__
face = SDL_LoadBMP("icon.bmp");
#else
face = SDL_LoadBMP("../icon.bmp");
#endif

if (SDL_ATassert( "SDL_CreateLoadBmp", face != NULL))
return;

Expand Down

0 comments on commit a60ff48

Please sign in to comment.