]> git.wincent.com - WOTest.git/blob - WOTestApplicationTestsController.m
cb27dca6f94051ccac74d3a3ba62f3a83358e385
[WOTest.git] / WOTestApplicationTestsController.m
1 //
2 //  WOTestApplicationTestsController.m
3 //  WOTest
4 //
5 //  Created by Wincent Colaiuta on 15 December 2006.
6 //
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.
12 //
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.
17 //
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/>.
20 //
21
22 // class header
23 #import "WOTestApplicationTestsController.h"
24
25 // other headers
26 #import "WOTest.h"
27
28 #ifdef WO_EXAMPLE_LOAD_METHOD_FOR_SUBCLASSES_TO_IMPLEMENT
29
30 #import <libkern/OSAtomic.h>        /* OSAtomicIncrement32Barrier() */
31
32 @implementation WOTestApplicationTestsController (WOExampleOnly)
33
34 + (void)load
35 {
36     static int32_t initialized = 0;
37     if (OSAtomicIncrement32Barrier(&initialized) != 1) return;  // do this once only
38
39     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
40     (void)[[self alloc] initWithPath:__FILE__ keepComponents:3]; // will release self after running tests
41     [pool release];
42 }
43
44 @end
45
46 #endif /* WO_EXAMPLE_LOAD_METHOD_FOR_SUBCLASSES_TO_IMPLEMENT */
47
48 @implementation WOTestApplicationTestsController
49
50 // we weak import the AppKit framework to make actually linking to it optional
51 extern NSString *NSApplicationDidFinishLaunchingNotification;
52
53 - (id)initWithPath:(const char *)sourcePath keepComponents:(unsigned)count
54 {
55     NSParameterAssert(sourcePath != NULL);
56     if ((self = [super init]))
57     {
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;
64
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
70                                                    object:nil];
71
72         [self performSelector:@selector(applicationFailedToFinishLaunching:) withObject:nil afterDelay:10.0];
73     }
74     return self;
75 }
76
77 - (void)runTests:(NSNotification *)aNotification
78 {
79     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(applicationFailedToFinishLaunching:) object:nil];
80
81     WOTest *tester = [WOTest sharedInstance];
82     [tester setTrimInitialPathComponents:_trimPathComponents];
83     [tester runAllTests];
84
85     [[NSNotificationCenter defaultCenter] removeObserver:self];
86     [self release];                                 // balance alloc/init in load method
87 }
88
89 - (void)applicationFailedToFinishLaunching:(id)ignored
90 {
91     NSLog(@"warning: NSApplicationDidFinishLaunchingNotification still not received after 10 seconds");
92 }
93
94 @end