Files
cappuccino/Tests/Foundation/CPNotificationQueueTest.j
T
Alexandre Wilhelm 22b77af9d3 New: added the class CPNotificationQueue
This PR adds the feature of CPNotificationQueue.

Cappuccino provides a framework for sending messages between objects within
a process called notifications. CPNotificationQueue objects (or simply notification queues)
act as buffers for notification centers (instances of CPNotificationCenter).
Whereas a notification center distributes notifications when posted,
notifications placed into the queue can be delayed until the end of the current pass through the run loop
or until the run loop is idle. Duplicate notifications can also be coalesced so that only one notification
is sent although multiple notifications are posted. A notification queue maintains notifications
(instances of C¨Notification) generally in a first in first out (FIFO) order.
When a notification rises to the front of the queue, the queue posts it to the notification center,
which in turn dispatches the notification to all objects registered as observers.

More informations here :https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationQueue_Class/index.html#//apple_ref/occ/instm/NSNotificationQueue/enqueueNotification:postingStyle:coalesceMask:forModes:

Unit-Tests in Tests/Foundation/CPNotificationQueueTest.j
2015-10-08 16:06:16 -07:00

124 lines
5.4 KiB
Plaintext

@import <Foundation/CPNotificationQueue.j>
var TestNotification = @"TestNotification";
@implementation CPNotificationQueueTest : OJTestCase
{
int notificationCount;
}
- (void)setUp
{
notificationCount = 0;
}
/*!
Test ceation
*/
- (void)testCreate
{
var queue = [CPNotificationQueue defaultQueue];
[self assert:queue._notificationCenter equals:[CPNotificationCenter defaultCenter]];
var notificationCenter = [CPNotificationCenter new],
secondQueue = [[CPNotificationQueue alloc] initWithNotificationCenter:notificationCenter];
[self assert:secondQueue._notificationCenter equals:notificationCenter];
}
- (void)testNotificationPostNow
{
var queue = [CPNotificationQueue defaultQueue],
notification = [[CPNotification alloc] initWithName:@"TestNotification" object:self userInfo:nil];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(countNotification:) name:@"TestNotification" object:self];
[queue enqueueNotification:notification postingStyle:CPPostNow];
[self assert:notificationCount equals:1];
[queue enqueueNotification:notification postingStyle:CPPostNow];
[self assert:notificationCount equals:2];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification postingStyle:CPPostWhenIdle];
[queue enqueueNotification:notification postingStyle:CPPostNow];
[self assert:notificationCount equals:3];
[queue enqueueNotification:notification postingStyle:CPPostNow];
[queue enqueueNotification:notification postingStyle:CPPostNow];
[self assert:notificationCount equals:5];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:5];
}
- (void)testNotificationPostASAP
{
var queue = [CPNotificationQueue defaultQueue],
notification = [[CPNotification alloc] initWithName:@"TestNotification" object:self userInfo:nil],
notification2 = [[CPNotification alloc] initWithName:@"TestNotification" object:self userInfo:@{}];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(countNotification:) name:@"TestNotification" object:self];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[self assert:notificationCount equals:0];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:1];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:1];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:2];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification2 postingStyle:CPPostASAP coalesceMask:CPNotificationNoCoalescing forModes:nil];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:4];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification2 postingStyle:CPPostASAP coalesceMask:CPNotificationNoCoalescing forModes:nil];
[queue dequeueNotificationsMatching:notification2 coalesceMask:CPNotificationNoCoalescing];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:5];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification2 postingStyle:CPPostASAP coalesceMask:CPNotificationNoCoalescing forModes:nil];
[queue dequeueNotificationsMatching:notification coalesceMask:CPNotificationCoalescingOnName];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:5];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification2 postingStyle:CPPostASAP coalesceMask:CPNotificationNoCoalescing forModes:nil];
[queue dequeueNotificationsMatching:notification coalesceMask:CPNotificationCoalescingOnSender];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:5];
}
- (void)testNotificationWhenIdle
{
var queue = [CPNotificationQueue defaultQueue],
notification = [[CPNotification alloc] initWithName:@"TestNotification" object:self userInfo:nil],
notification2 = [[CPNotification alloc] initWithName:@"TestNotificationIdle" object:self userInfo:@{}];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(countNotification:) name:@"TestNotification" object:self];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(countNotificationIdle:) name:@"TestNotificationIdle" object:self];
[queue enqueueNotification:notification postingStyle:CPPostASAP];
[queue enqueueNotification:notification2 postingStyle:CPPostWhenIdle];
[self assert:notificationCount equals:0];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:1];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self assert:notificationCount equals:2];
}
- (void)countNotification:(CPNotification)aNotification
{
notificationCount++;
}
- (void)countNotificationIdle:(CPNotification)aNotification
{
notificationCount++;
}
@end