-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationKIFTestStep.m
88 lines (67 loc) · 3.02 KB
/
NotificationKIFTestStep.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#import "NotificationKIFTestStep.h"
@interface NotificationKIFTestStep()
@property (nonatomic, retain) NSMutableArray* timedNotificationTimes;
@property (nonatomic, retain) NSObject* timedNotificationObject;
@property (nonatomic, copy) NSString* timedNotificationName;
@property (nonatomic, assign) NSTimeInterval timedNotificationInterval;
- (void)notificationOccured:(NSNotification*)notification;
@end
@implementation NotificationKIFTestStep
@synthesize timedNotificationTimes;
@synthesize timedNotificationObject;
@synthesize timedNotificationName;
@synthesize timedNotificationInterval;
- (id)init{
self = [super init];
if (self) {
self.timedNotificationTimes = [NSMutableArray arrayWithCapacity:1];
}
return self;
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.timedNotificationName = nil;
self.timedNotificationTimes = nil;
self.timedNotificationObject = nil;
[super dealloc];
}
- (void)notificationOccured:(NSNotification*)notification{
if ([notification.name isEqualToString:self.timedNotificationName]) {
if (self.timedNotificationObject == nil || self.timedNotificationObject == notification.object) {
[self.timedNotificationTimes addObject:[NSDate date]];
}
}
}
- (KIFTestStepResult)executeAndReturnError:(NSError **)error;
{
BOOL notificationFiredOnTime = NO;
for (NSDate* fireTime in self.timedNotificationTimes) {
if (fabs([fireTime timeIntervalSinceNow]) <= self.timedNotificationInterval) {
notificationFiredOnTime = YES;
break;
}
}
if (error && !notificationFiredOnTime) {
*error = [NSError errorWithDomain:@"KIFTest"
code:KIFTestStepResultFailure
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"The notification was not fired within the specified time", NSLocalizedDescriptionKey, nil]];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
self.timedNotificationName = nil;
self.timedNotificationTimes = nil;
self.timedNotificationObject = nil;
return notificationFiredOnTime ? KIFTestStepResultSuccess : KIFTestStepResultFailure;
}
+ (KIFTestStep*)stepToDetectNotification:(NSString*)notificationName
object:(NSObject*)object
sentWithinTimeinterval:(NSTimeInterval)interval{
NSString *description = [NSString stringWithFormat:@"Checking if notification \"%@\" occured within the last %f seconds", notificationName, interval];
NotificationKIFTestStep *step = [[[NotificationKIFTestStep alloc] init] autorelease];
step.description = description;
step.timedNotificationName = notificationName;
step.timedNotificationObject = object;
step.timedNotificationInterval = interval;
[[NSNotificationCenter defaultCenter] addObserver:step selector:@selector(notificationOccured:) name:notificationName object:object];
return step;
}
@end