]> git.wincent.com - WOTest.git/blob - WOTestApplicationTestsController.m
Initial import
[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 //  $Id: WOTestApplicationTestsController.m 208 2007-07-07 19:02:28Z wincent $
22
23 // class header
24 #import "WOTestApplicationTestsController.h"
25
26 // other headers
27 #import "WOTest.h"
28
29 #ifdef WO_EXAMPLE_LOAD_METHOD_FOR_SUBCLASSES_TO_IMPLEMENT
30
31 #import <libkern/OSAtomic.h>        /* OSAtomicIncrement32Barrier() */
32
33 @implementation WOTestApplicationTestsController (WOExampleOnly)
34
35 + (void)load
36 {
37     static int32_t initialized = 0;
38     if (OSAtomicIncrement32Barrier(&initialized) != 1) return;  // do this once only
39         
40     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
41     (void)[[self alloc] initWithPath:__FILE__ keepComponents:3]; // will release self after running tests
42     [pool release];
43 }
44
45 @end
46
47 #endif /* WO_EXAMPLE_LOAD_METHOD_FOR_SUBCLASSES_TO_IMPLEMENT */
48
49 @implementation WOTestApplicationTestsController
50
51 // we weak import the AppKit framework to make actually linking to it optional
52 extern NSString *NSApplicationDidFinishLaunchingNotification;
53
54 - (id)initWithPath:(const char *)sourcePath keepComponents:(unsigned)count
55 {
56     NSParameterAssert(sourcePath != NULL);
57     if ((self = [super init]))
58     {
59         NSString *path = [NSString stringWithUTF8String:sourcePath];
60         NSAssert([path isAbsolutePath], @"sourcePath must be an absolute path");
61         NSArray *components = [path pathComponents];
62         unsigned componentCount = [components count];
63         NSAssert(componentCount > count, @"componentCount must be greater than count");
64         _trimPathComponents = componentCount - count;
65         
66         // wait until the app has finish launching before running the tests; means we can test stuff in nibs etc
67         NSAssert((NSApplicationDidFinishLaunchingNotification != NULL), @"NSApplicationDidFinishLaunchingNotification not defined");
68         [[NSNotificationCenter defaultCenter] addObserver:self 
69                                                  selector:@selector(runTests:) 
70                                                      name:NSApplicationDidFinishLaunchingNotification
71                                                    object:nil];
72         
73         [self performSelector:@selector(applicationFailedToFinishLaunching:) withObject:nil afterDelay:10.0];
74     }
75     return self;
76 }
77
78 - (void)runTests:(NSNotification *)aNotification
79 {
80     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(applicationFailedToFinishLaunching:) object:nil];
81     
82     WOTest *tester = [WOTest sharedInstance];
83     [tester setTrimInitialPathComponents:_trimPathComponents];
84     [tester runAllTests];
85     
86     [[NSNotificationCenter defaultCenter] removeObserver:self];
87     [self release];                                 // balance alloc/init in load method
88 }
89
90 - (void)applicationFailedToFinishLaunching:(id)ignored
91 {
92     NSLog(@"warning: NSApplicationDidFinishLaunchingNotification still not received after 10 seconds");
93 }
94
95 @end