/* The simplest of tests for the NSNotification and NSNotificationCenter
   classes.  These tests should be expanded. 

   (The Tcp*Port classes, however, do test the notification mechanism 
    further.) */

#include <Foundation/NSRunLoop.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSNotificationQueue.h>
#include <Foundation/NSString.h>
#include <Foundation/NSAutoreleasePool.h>

@interface Observer : NSObject
- (void) gotNotificationFoo: not;
@end

@implementation Observer

- (void) gotNotificationFoo:(NSNotification*)not
{
  printf ("Got %s\n", [[not name] cString]);
}

- (void) gotNotificationFooNoObject:(NSNotification*)not
{
  printf ("Got %s without object\n", [[not name] cString]);
}

@end

id foo = @"NotificationTestFoo";
id fooQueue = @"NSNotificationFooQueue";

int main ()
{
id o1 = [NSObject new];
id observer1 = [Observer new];
id arp = [NSAutoreleasePool new];
NSNotification *n;

	[[NSNotificationCenter defaultCenter] 
		addObserver: observer1
		selector: @selector(gotNotificationFoo:)
		name: fooQueue
		object: o1];

	[[NSNotificationCenter defaultCenter] 
		addObserver: observer1
		selector: @selector(gotNotificationFoo:)
		name: foo
		object: o1];

  [[NSNotificationCenter defaultCenter]
    addObserver: observer1
    selector: @selector(gotNotificationFooNoObject:)
    name: foo
    object: nil];


  /* This will cause two messages to be printed, one for each request above. */
	[[NSNotificationCenter defaultCenter] postNotificationName:foo object:o1];

  /* This will cause one message to be printed. */
	[[NSNotificationCenter defaultCenter] postNotificationName:foo object:nil];

  
	[[NSNotificationCenter defaultCenter] removeObserver: observer1
										  name: nil
										  object: o1];

// This will cause message to be printed.
	[[NSNotificationCenter defaultCenter] postNotificationName:foo object:o1];

	printf("queue notification NSPostASAP\n");
	n = [NSNotification notificationWithName:fooQueue object:o1 userInfo:nil];
	[[NSNotificationQueue defaultQueue] enqueueNotification:n
							 postingStyle:NSPostASAP
							 coalesceMask:NSNotificationNoCoalescing
							 forModes:nil];

	[[NSRunLoop currentRunLoop] run];

	printf("remove notification observer\n");
	[[NSNotificationCenter defaultCenter] removeObserver: observer1];

// This will cause no messages to be printed.
	[[NSNotificationCenter defaultCenter] postNotificationName:foo object:o1];

	[arp release];
	printf("nsnotification test complete\n");

	exit (0);
}
