NSNotificationCenterRACSupportSpec.m 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // NSNotificationCenterRACSupportSpec.m
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2012-12-07.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Quick/Quick.h>
  9. #import <Nimble/Nimble.h>
  10. #import "NSNotificationCenter+RACSupport.h"
  11. #import "RACSignal.h"
  12. #import "RACCompoundDisposable.h"
  13. #import "RACDisposable.h"
  14. #import "NSObject+RACDeallocating.h"
  15. static NSString * const TestNotification = @"TestNotification";
  16. QuickSpecBegin(NSNotificationCenterRACSupportSpec)
  17. __block NSNotificationCenter *notificationCenter;
  18. qck_beforeEach(^{
  19. // The compiler gets confused and thinks you might be messaging
  20. // NSDistributedNotificationCenter otherwise. Wtf?
  21. notificationCenter = NSNotificationCenter.defaultCenter;
  22. });
  23. qck_it(@"should send the notification when posted by any object", ^{
  24. RACSignal *signal = [notificationCenter rac_addObserverForName:TestNotification object:nil];
  25. __block NSUInteger count = 0;
  26. [signal subscribeNext:^(NSNotification *notification) {
  27. ++count;
  28. expect(notification).to(beAKindOf(NSNotification.class));
  29. expect(notification.name).to(equal(TestNotification));
  30. }];
  31. expect(@(count)).to(equal(@0));
  32. [notificationCenter postNotificationName:TestNotification object:nil];
  33. expect(@(count)).to(equal(@1));
  34. [notificationCenter postNotificationName:TestNotification object:self];
  35. expect(@(count)).to(equal(@2));
  36. });
  37. qck_it(@"should send the notification when posted by a specific object", ^{
  38. RACSignal *signal = [notificationCenter rac_addObserverForName:TestNotification object:self];
  39. __block NSUInteger count = 0;
  40. [signal subscribeNext:^(NSNotification *notification) {
  41. ++count;
  42. expect(notification).to(beAKindOf(NSNotification.class));
  43. expect(notification.name).to(equal(TestNotification));
  44. expect(notification.object).to(beIdenticalTo(self));
  45. }];
  46. expect(@(count)).to(equal(@0));
  47. [notificationCenter postNotificationName:TestNotification object:nil];
  48. expect(@(count)).to(equal(@0));
  49. [notificationCenter postNotificationName:TestNotification object:self];
  50. expect(@(count)).to(equal(@1));
  51. });
  52. qck_it(@"shouldn't strongly capture the notification object", ^{
  53. RACSignal *signal __attribute__((objc_precise_lifetime, unused));
  54. __block BOOL dealloced = NO;
  55. @autoreleasepool {
  56. NSObject *notificationObject = [[NSObject alloc] init];
  57. [notificationObject.rac_deallocDisposable addDisposable:[RACDisposable disposableWithBlock:^{
  58. dealloced = YES;
  59. }]];
  60. signal = [notificationCenter rac_addObserverForName:TestNotification object:notificationObject];
  61. }
  62. expect(@(dealloced)).to(beTruthy());
  63. });
  64. QuickSpecEnd