Skip to content
Browse files

Implemented support for observing a notification emitted during the e…

…xecution of a child step.
  • Loading branch information...
1 parent 7d78430 commit 70028b55fb869f80156697c94a0f1e284e2712e4 @blakewatters blakewatters committed Jan 7, 2012
Showing with 40 additions and 1 deletion.
  1. +11 −0 Classes/KIFTestStep.h
  2. +29 −1 Classes/KIFTestStep.m
View
11 Classes/KIFTestStep.h
@@ -275,6 +275,17 @@ typedef KIFTestStepResult (^KIFTestStepExecutionBlock)(KIFTestStep *step, NSErro
+ (id)stepToWaitForNotificationName:(NSString*)name object:(id)object;
/*!
+ @method stepToWaitForNotificationName:object:whileExecutingStep:
+ @abstract A step that waits for an NSNotification emitted during execution of a child step
+ @discussion Useful when step execution causes a notification to be emitted, but executes too quickly for stepToWaitForNotificationName: to observe it.
+ An observer will be registered for the notification before the observedStep is executed.
+ @param name The name of the NSNotification
+ @param object The object to which the step should listen. Nil value will listen to all objects.
+ @result A configured test step.
+ */
++ (id)stepToWaitForNotificationName:(NSString *)name object:(id)object whileExecutingStep:(KIFTestStep *)childStep;
+
+/*!
@method stepToTapViewWithAccessibilityLabel:
@abstract A step that taps a particular view in the view hierarchy.
@discussion The view or accessibility element with the given label is searched for in the view hierarchy. If the element isn't found or isn't currently tappable, then the step will attempt to wait until it is. Once the view is present and tappable, a tap event is simulated in the center of the view or element.
View
30 Classes/KIFTestStep.m
@@ -26,6 +26,7 @@ @interface KIFTestStep ()
@property (nonatomic, retain) id notificationObject;
@property BOOL notificationOccurred;
@property BOOL observingForNotification;
+@property (nonatomic, retain) KIFTestStep *childStep;
+ (BOOL)_isUserInteractionEnabledForView:(UIView *)view;
@@ -47,6 +48,7 @@ @implementation KIFTestStep
@synthesize notificationOccurred;
@synthesize observingForNotification;
@synthesize timeout;
+@synthesize childStep;
#pragma mark Class Methods
@@ -224,12 +226,36 @@ + (id)stepToWaitForNotificationName:(NSString *)name object:(id)object;
[[NSNotificationCenter defaultCenter] addObserver:step selector:@selector(_onObservedNotification:) name:name object:object];
}
- KIFTestWaitCondition(step.notificationOccurred, error, @"Waiting for notification \"%@\"", name);
+ KIFTestWaitCondition(step.notificationOccurred, error, @"Waiting for notification \"%@\"", name);
return KIFTestStepResultSuccess;
}];
return step;
}
++ (id)stepToWaitForNotificationName:(NSString *)name object:(id)object whileExecutingStep:(KIFTestStep *)childStep;
+{
+ NSString *description = [NSString stringWithFormat:@"Wait for notification \"%@\" while executing child step \"%@\"", name, childStep];
+
+ KIFTestStep *step = [self stepWithDescription:description executionBlock:^(KIFTestStep *step, NSError **error) {
+ if (!step.observingForNotification) {
+ step.notificationName = name;
+ step.notificationObject = object;
+ step.observingForNotification = YES;
+ [[NSNotificationCenter defaultCenter] addObserver:step selector:@selector(_onObservedNotification:) name:name object:object];
+ }
+
+ // Execute the step we are observing for changes
+ KIFTestStepResult result = [step.childStep executeAndReturnError:error];
+ KIFTestWaitCondition(result != KIFTestStepResultWait, error, @"Waiting for completion of child step \"%@\"", step.childStep);
+
+ // Wait for the actual notification
+ KIFTestWaitCondition(step.notificationOccurred, error, @"Waiting for notification \"%@\"", name);
+ return KIFTestStepResultSuccess;
+ }];
+ step.childStep = childStep;
+ return step;
+}
+
+ (id)stepToTapViewWithAccessibilityLabel:(NSString *)label;
{
return [self stepToTapViewWithAccessibilityLabel:label traits:UIAccessibilityTraitNone];
@@ -588,6 +614,8 @@ - (void)dealloc;
notificationName = nil;
[notificationObject release];
notificationObject = nil;
+ [childStep release];
+ childStep = nil;
[super dealloc];
}

0 comments on commit 70028b5

Please sign in to comment.
Something went wrong with that request. Please try again.