add some notification tests

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@36488 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2013-04-08 08:27:58 +00:00
parent fec7f6689d
commit 82bcca4326
3 changed files with 171 additions and 0 deletions

View file

View file

@ -0,0 +1,39 @@
#import <Foundation/NSNotification.h>
#import <Foundation/NSAutoreleasePool.h>
#import "ObjectTesting.h"
@implementation NSNotification (Testing)
- (BOOL) isEqualForTestcase: (id)other
{
if (NO == [other isKindOfClass: [NSNotification class]])
return NO;
if ([self name] != [other name]
&& NO == [[self name] isEqual: [other name]])
return NO;
if ([self object] != [other object]
&& NO == [[self object] isEqual: [other object]])
return NO;
if ([self userInfo] != [other userInfo]
&& NO == [[self userInfo] isEqual: [other userInfo]])
return NO;
return YES;
}
@end
int main()
{
NSNotification *obj;
NSMutableArray *testObjs = [[NSMutableArray alloc] init];
NSAutoreleasePool *arp = [NSAutoreleasePool new];
test_alloc(@"NSNotification");
obj = [NSNotification new];
[testObjs addObject: obj];
test_NSObject(@"NSNotification", testObjs);
test_NSCoding(testObjs);
test_keyed_NSCoding(testObjs);
test_NSCopying(@"NSNotification",@"NSNotification",testObjs,NO,NO);
[arp release]; arp = nil;
return 0;
}

View file

@ -0,0 +1,132 @@
#define FAKE_PROXY 1
#import <Foundation/Foundation.h>
#import "ObjectTesting.h"
static int notificationCounter = 0;
// Real object
@interface RealObject : NSObject
- (void) test: (NSNotification *)aNotification;
@end
@implementation RealObject
- (void) test: (NSNotification *)aNotification
{
notificationCounter++;
}
@end
// Proxy object
@interface ProxyObject : NSObject
{
RealObject *realObject;
}
- (id) initWithRealObject: (RealObject *)anObject;
@end
@interface ProxyObject(ForwardedMethods)
- (void) test: (NSNotification *)aNotification;
@end
@implementation ProxyObject
- (id) initWithRealObject: (RealObject *)anObject
{
if ((self = [super init]) != nil)
{
realObject = [anObject retain];
}
return self;
}
- (void) dealloc
{
[realObject release];
[super dealloc];
}
- (BOOL) respondsToSelector: (SEL)aSelector
{
return [super respondsToSelector: aSelector]
|| sel_isEqual(aSelector, @selector(test:));
}
- (NSMethodSignature *) methodSignatureForSelector: (SEL)aSelector
{
NSMethodSignature *methodSignature;
if ([super respondsToSelector: aSelector])
{
methodSignature = [super methodSignatureForSelector: aSelector];
}
else if (sel_isEqual(aSelector, @selector(test:)))
{
methodSignature = [realObject methodSignatureForSelector: aSelector];
}
else
{
methodSignature = nil;
}
return methodSignature;
}
- (void) forwardInvocation: (NSInvocation *)anInvocation
{
if (sel_isEqual([anInvocation selector], @selector(test:)))
{
[anInvocation invokeWithTarget: realObject];
}
else
{
[super forwardInvocation: anInvocation];
}
}
@end
// Test program
int
main(int argc, char **argv)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSObject *testObject = [[[NSObject alloc] init] autorelease];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSNotification *aNotification =
[NSNotification notificationWithName: @"TestNotification"
object: testObject];
// Create the real object and its proxy
RealObject *real = [[[RealObject alloc] init] autorelease];
ProxyObject *proxy =
[[[ProxyObject alloc] initWithRealObject: real] autorelease];
// Make the proxy object an observer for the sample notification
// NB It is important (for the test) to perform this with a local
// autorelease pool.
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSLog(@"Adding proxy observer");
[nc addObserver: proxy
selector: @selector(test:)
name: @"TestNotification"
object: testObject];
[nc postNotification: aNotification];
PASS(1 == notificationCounter, "notification via proxy works immediately")
[pool release];
}
// Post the notification
// NB It is not important that this code is performed with a
// local autorelease pool
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
[nc postNotification: aNotification];
PASS(2 == notificationCounter, "notification via proxy works after pool")
[pool release];
}
[nc postNotification: aNotification];
PASS(3 == notificationCounter, "notification via proxy works repeatedly")
[nc removeObserver: proxy];
[pool release];
return 0;
}