Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -33,10 +33,11 @@ OFMethodSignatureTests.m \ OFMutableArrayTests.m \ OFMutableSetTests.m \ OFMutableStringTests.m \ OFMutableUTF8StringTests.m \ + OFNotificationCenterTests.m \ OFNumberTests.m \ OFObjectTests.m \ OFPBKDF2Tests.m \ OFPropertyListTests.m \ OFScryptTests.m \ ADDED new_tests/OFNotificationCenterTests.m Index: new_tests/OFNotificationCenterTests.m ================================================================== --- new_tests/OFNotificationCenterTests.m +++ new_tests/OFNotificationCenterTests.m @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "ObjFW.h" +#import "ObjFWTest.h" + +static const OFNotificationName notificationName = + @"OFNotificationCenterTestName"; +static const OFNotificationName otherNotificationName = + @"OFNotificationCenterTestOtherName"; + +@interface OFNotificationCenterTests: OTTestCase +@end + +@interface OFNotificationCenterTestClass: OFObject +{ +@public + id _expectedObject; + int _received; +} + +- (void)handleNotification: (OFNotification *)notification; +@end + +@implementation OFNotificationCenterTestClass +- (void)handleNotification: (OFNotification *)notification +{ + OFEnsure([notification.name isEqual: notificationName]); + OFEnsure(_expectedObject == nil || + notification.object == _expectedObject); + + _received++; +} +@end + +@implementation OFNotificationCenterTests +- (void)testNotificationCenter +{ + OFNotificationCenter *center = [OFNotificationCenter defaultCenter]; + OFNotificationCenterTestClass *test1, *test2, *test3, *test4; + OFNotification *notification; + + test1 = [[[OFNotificationCenterTestClass alloc] init] autorelease]; + test1->_expectedObject = self; + test2 = [[[OFNotificationCenterTestClass alloc] init] autorelease]; + test3 = [[[OFNotificationCenterTestClass alloc] init] autorelease]; + test3->_expectedObject = self; + test4 = [[[OFNotificationCenterTestClass alloc] init] autorelease]; + + /* First one intentionally added twice to test deduplication. */ + [center addObserver: test1 + selector: @selector(handleNotification:) + name: notificationName + object: self]; + [center addObserver: test1 + selector: @selector(handleNotification:) + name: notificationName + object: self]; + [center addObserver: test2 + selector: @selector(handleNotification:) + name: notificationName + object: nil]; + [center addObserver: test3 + selector: @selector(handleNotification:) + name: otherNotificationName + object: self]; + [center addObserver: test4 + selector: @selector(handleNotification:) + name: otherNotificationName + object: nil]; + + notification = [OFNotification notificationWithName: notificationName + object: nil]; + [center postNotification: notification]; + OTAssertEqual(test1->_received, 0); + OTAssertEqual(test2->_received, 1); + OTAssertEqual(test3->_received, 0); + OTAssertEqual(test4->_received, 0); + + notification = [OFNotification notificationWithName: notificationName + object: self]; + [center postNotification: notification]; + OTAssertEqual(test1->_received, 1); + OTAssertEqual(test2->_received, 2); + OTAssertEqual(test3->_received, 0); + OTAssertEqual(test4->_received, 0); + + notification = [OFNotification notificationWithName: notificationName + object: @"foo"]; + [center postNotification: notification]; + OTAssertEqual(test1->_received, 1); + OTAssertEqual(test2->_received, 3); + OTAssertEqual(test3->_received, 0); + OTAssertEqual(test4->_received, 0); + +#ifdef OF_HAVE_BLOCKS + __block bool received = false; + id handle; + + notification = [OFNotification notificationWithName: notificationName + object: self]; + handle = [center addObserverForName: notificationName + object: self + usingBlock: ^ (OFNotification *notification_) { + OTAssertEqual(notification_, notification); + OTAssertFalse(received); + received = true; + }]; + [center postNotification: notification]; + OTAssertTrue(received); + OTAssertEqual(test1->_received, 2); + OTAssertEqual(test2->_received, 4); + OTAssertEqual(test3->_received, 0); + OTAssertEqual(test4->_received, 0); + + /* Act like the block test didn't happen. */ + [center removeObserver: handle]; + test1->_received--; + test2->_received--; +#endif + + [center removeObserver: test1 + selector: @selector(handleNotification:) + name: notificationName + object: self]; + [center removeObserver: test2 + selector: @selector(handleNotification:) + name: notificationName + object: nil]; + [center removeObserver: test3 + selector: @selector(handleNotification:) + name: otherNotificationName + object: self]; + [center removeObserver: test4 + selector: @selector(handleNotification:) + name: otherNotificationName + object: nil]; + + notification = [OFNotification notificationWithName: notificationName + object: self]; + [center postNotification: notification]; + OTAssertEqual(test1->_received, 1); + OTAssertEqual(test2->_received, 3); + OTAssertEqual(test3->_received, 0); + OTAssertEqual(test4->_received, 0); +} +@end Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -15,11 +15,10 @@ STATIC_LIB_NOINST = ${TESTS_STATIC_LIB} SRCS = OFDataTests.m \ OFDictionaryTests.m \ OFListTests.m \ OFMemoryStreamTests.m \ - OFNotificationCenterTests.m \ OFStreamTests.m \ OFValueTests.m \ OFXMLNodeTests.m \ OFXMLParserTests.m \ TestsAppDelegate.m \ DELETED tests/OFNotificationCenterTests.m Index: tests/OFNotificationCenterTests.m ================================================================== --- tests/OFNotificationCenterTests.m +++ tests/OFNotificationCenterTests.m @@ -1,159 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFNotificationCenter"; -static const OFNotificationName notificationName = - @"OFNotificationCenterTestName"; -static const OFNotificationName otherNotificationName = - @"OFNotificationCenterTestOtherName"; - -@interface OFNotificationCenterTest: OFObject -{ -@public - id _expectedObject; - int _received; -} - -- (void)handleNotification: (OFNotification *)notification; -@end - -@implementation OFNotificationCenterTest -- (void)handleNotification: (OFNotification *)notification -{ - OFEnsure([notification.name isEqual: notificationName]); - OFEnsure(_expectedObject == nil || - notification.object == _expectedObject); - - _received++; -} -@end - -@implementation TestsAppDelegate (OFNotificationCenterTests) -- (void)notificationCenterTests -{ - void *pool = objc_autoreleasePoolPush(); - OFNotificationCenter *center = [OFNotificationCenter defaultCenter]; - OFNotificationCenterTest *test1, *test2, *test3, *test4; - OFNotification *notification; - - test1 = - [[[OFNotificationCenterTest alloc] init] autorelease]; - test1->_expectedObject = self; - test2 = - [[[OFNotificationCenterTest alloc] init] autorelease]; - test3 = - [[[OFNotificationCenterTest alloc] init] autorelease]; - test3->_expectedObject = self; - test4 = - [[[OFNotificationCenterTest alloc] init] autorelease]; - - /* First one intentionally added twice to test deduplication. */ - TEST(@"-[addObserver:selector:name:object:]", - R([center addObserver: test1 - selector: @selector(handleNotification:) - name: notificationName - object: self]) && - R([center addObserver: test1 - selector: @selector(handleNotification:) - name: notificationName - object: self]) && - R([center addObserver: test2 - selector: @selector(handleNotification:) - name: notificationName - object: nil]) && - R([center addObserver: test3 - selector: @selector(handleNotification:) - name: otherNotificationName - object: self]) && - R([center addObserver: test4 - selector: @selector(handleNotification:) - name: otherNotificationName - object: nil])) - - notification = [OFNotification notificationWithName: notificationName - object: nil]; - TEST(@"-[postNotification:] #1", - R([center postNotification: notification]) && - test1->_received == 0 && test2->_received == 1 && - test3->_received == 0 && test4->_received == 0) - - notification = [OFNotification notificationWithName: notificationName - object: self]; - TEST(@"-[postNotification:] #2", - R([center postNotification: notification]) && - test1->_received == 1 && test2->_received == 2 && - test3->_received == 0 && test4->_received == 0) - - notification = [OFNotification notificationWithName: notificationName - object: @"foo"]; - TEST(@"-[postNotification:] #3", - R([center postNotification: notification]) && - test1->_received == 1 && test2->_received == 3 && - test3->_received == 0 && test4->_received == 0) - -#ifdef OF_HAVE_BLOCKS - __block bool received = false; - id handle; - - notification = [OFNotification notificationWithName: notificationName - object: self]; - TEST(@"-[addObserverForName:object:usingBlock:]", - (handle = [center addObserverForName: notificationName - object: self - usingBlock: ^ (OFNotification *notif) { - OFEnsure(notif == notification && !received); - received = true; - }]) && R([center postNotification: notification]) && received && - test1->_received == 2 && test2->_received == 4 && - test3->_received == 0 && test4->_received == 0) - - /* Act like the block test didn't happen. */ - [center removeObserver: handle]; - test1->_received--; - test2->_received--; -#endif - - TEST(@"-[removeObserver:selector:name:object:]", - R([center removeObserver: test1 - selector: @selector(handleNotification:) - name: notificationName - object: self]) && - R([center removeObserver: test2 - selector: @selector(handleNotification:) - name: notificationName - object: nil]) && - R([center removeObserver: test3 - selector: @selector(handleNotification:) - name: otherNotificationName - object: self]) && - R([center removeObserver: test4 - selector: @selector(handleNotification:) - name: otherNotificationName - object: nil])) - - notification = [OFNotification notificationWithName: notificationName - object: self]; - TEST(@"-[postNotification:] with no observers", - R([center postNotification: notification]) && - test1->_received == 1 && test2->_received == 3 && - test3->_received == 0 && test4->_received == 0) - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -89,14 +89,10 @@ @interface TestsAppDelegate (OFMemoryStreamTests) - (void)memoryStreamTests; @end -@interface TestsAppDelegate (OFNotificationCenterTests) -- (void)notificationCenterTests; -@end - @interface TestsAppDelegate (OFStreamTests) - (void)streamTests; @end @interface TestsAppDelegate (OFValueTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -374,11 +374,10 @@ [self dictionaryTests]; [self listTests]; [self valueTests]; [self streamTests]; [self memoryStreamTests]; - [self notificationCenterTests]; #ifdef OF_HAVE_SOCKETS [self kernelEventObserverTests]; #endif #if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_THREADS) [self HTTPClientTests];