2 // WOTestApplicationTestsController.m
5 // Created by Wincent Colaiuta on 15 December 2006.
7 // Copyright 2006-2007 Wincent Colaiuta.
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #import "WOTestApplicationTestsController.h"
28 #ifdef WO_EXAMPLE_LOAD_METHOD_FOR_SUBCLASSES_TO_IMPLEMENT
30 #import <libkern/OSAtomic.h> /* OSAtomicIncrement32Barrier() */
32 @implementation WOTestApplicationTestsController (WOExampleOnly)
36 static int32_t initialized = 0;
37 if (OSAtomicIncrement32Barrier(&initialized) != 1) return; // do this once only
39 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
40 (void)[[self alloc] initWithPath:__FILE__ keepComponents:3]; // will release self after running tests
46 #endif /* WO_EXAMPLE_LOAD_METHOD_FOR_SUBCLASSES_TO_IMPLEMENT */
48 @implementation WOTestApplicationTestsController
50 // we weak import the AppKit framework to make actually linking to it optional
51 extern NSString *NSApplicationDidFinishLaunchingNotification;
53 - (id)initWithPath:(const char *)sourcePath keepComponents:(unsigned)count
55 NSParameterAssert(sourcePath != NULL);
56 if ((self = [super init]))
58 NSString *path = [NSString stringWithUTF8String:sourcePath];
59 NSAssert([path isAbsolutePath], @"sourcePath must be an absolute path");
60 NSArray *components = [path pathComponents];
61 unsigned componentCount = [components count];
62 NSAssert(componentCount > count, @"componentCount must be greater than count");
63 _trimPathComponents = componentCount - count;
65 // wait until the app has finish launching before running the tests; means we can test stuff in nibs etc
66 NSAssert((NSApplicationDidFinishLaunchingNotification != NULL), @"NSApplicationDidFinishLaunchingNotification not defined");
67 [[NSNotificationCenter defaultCenter] addObserver:self
68 selector:@selector(runTests:)
69 name:NSApplicationDidFinishLaunchingNotification
72 [self performSelector:@selector(applicationFailedToFinishLaunching:) withObject:nil afterDelay:10.0];
77 - (void)runTests:(NSNotification *)aNotification
79 [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(applicationFailedToFinishLaunching:) object:nil];
81 WOTest *tester = [WOTest sharedInstance];
82 tester.trimInitialPathComponents = _trimPathComponents;
85 [[NSNotificationCenter defaultCenter] removeObserver:self];
86 [self release]; // balance alloc/init in load method
89 - (void)applicationFailedToFinishLaunching:(id)ignored
91 NSLog(@"warning: NSApplicationDidFinishLaunchingNotification still not received after 10 seconds");