Permalink
Browse files

Added a method for executing a block to determine the stubbed return …

…value for a JavaScript string.
  • Loading branch information...
1 parent 9403556 commit 25227c8283ac81e737711337b9269b350ae42fe5 Adam Milligan & Andrew Kitchen committed Oct 14, 2010
View
20 Spec/UI/UIWebViewSpec+Spec.m
@@ -232,6 +232,26 @@
assertThat(webView.executedJavaScripts, hasItem(js));
});
});
+
+ describe(@"with a return block", ^{
+ __block int value = 0;
+
+ beforeEach(^{
+ [webView setReturnBlock:^{ return (NSString *)[NSString stringWithFormat:@"%d", value]; } forJavaScript:js];
+ });
+
+ it(@"should return the value returned by the block", ^{
+ assertThat([webView stringByEvaluatingJavaScriptFromString:js], equalTo(@"0"));
+
+ ++value;
+ assertThat([webView stringByEvaluatingJavaScriptFromString:js], equalTo(@"1"));
+ });
+
+ it(@"should record the script", ^{
+ [webView stringByEvaluatingJavaScriptFromString:js];
+ assertThat(webView.executedJavaScripts, hasItem(js));
+ });
+ });
});
describe(@"frame", ^{
View
3 SpecHelperLib/Extensions/UIWebView+Spec.h
@@ -1,10 +1,13 @@
#import <UIKit/UIKit.h>
+typedef NSString *(^UIWebViewJavaScriptReturnBlock)();
+
@interface UIWebView (Spec)
- (void)sendClickRequest:(NSURLRequest *)request;
- (void)finishLoad;
- (void)setReturnValue:(NSString *)returnValue forJavaScript:(NSString *)javaScript;
+- (void)setReturnBlock:(UIWebViewJavaScriptReturnBlock)block forJavaScript:(NSString *)javaScript;
- (NSArray *)executedJavaScripts;
- (void)enableLogging;
- (void)disableLogging;
View
23 SpecHelperLib/Extensions/UIWebView+Spec.m
@@ -5,13 +5,13 @@ @interface UIWebViewAttributes : NSObject
@property (nonatomic, retain) NSURLRequest *request;
@property (nonatomic, assign) BOOL loading, logging;
@property (nonatomic, retain) NSMutableArray *javaScripts;
-@property (nonatomic, retain) NSMutableDictionary *returnValuesByJavaScript;
+@property (nonatomic, retain) NSMutableDictionary *returnValueBlocksByJavaScript;
+ (id)attributes;
@end
@implementation UIWebViewAttributes
@synthesize delegate = delegate_, request = request_, loading = loading_, logging = logging_,
- javaScripts = javaScripts_, returnValuesByJavaScript = returnValuesByJavaScript_;
+ javaScripts = javaScripts_, returnValueBlocksByJavaScript = returnValueBlocksByJavaScript_;
+ (id)attributes {
return [[[[self class] alloc] init] autorelease];
@@ -20,14 +20,14 @@ + (id)attributes {
- (id)init {
if (self = [super init]) {
self.javaScripts = [NSMutableArray array];
- self.returnValuesByJavaScript = [NSMutableDictionary dictionary];
+ self.returnValueBlocksByJavaScript = [NSMutableDictionary dictionary];
}
return self;
}
- (void)dealloc {
self.request = nil;
- self.returnValuesByJavaScript = nil;
+ self.returnValueBlocksByJavaScript = nil;
self.javaScripts = nil;
[super dealloc];
}
@@ -102,7 +102,12 @@ - (void)loadRequest:(NSURLRequest *)request {
- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)javaScript {
[self.attributes.javaScripts addObject:javaScript];
- return [self.attributes.returnValuesByJavaScript objectForKey:javaScript];
+ UIWebViewJavaScriptReturnBlock block = [self.attributes.returnValueBlocksByJavaScript objectForKey:javaScript];
+ if (block) {
+ return block();
+ } else {
+ return nil;
+ }
}
#pragma mark Additions
@@ -128,7 +133,13 @@ - (void)finishLoad {
}
- (void)setReturnValue:(NSString *)returnValue forJavaScript:(NSString *)javaScript {
- [self.attributes.returnValuesByJavaScript setObject:returnValue forKey:javaScript];
+ UIWebViewJavaScriptReturnBlock block = [[^{ return returnValue; } copy] autorelease];
+ [self.attributes.returnValueBlocksByJavaScript setObject:block forKey:javaScript];
+}
+
+- (void)setReturnBlock:(UIWebViewJavaScriptReturnBlock)block forJavaScript:(NSString *)javaScript {
+ UIWebViewJavaScriptReturnBlock copiedBlock = [[block copy] autorelease];
+ [self.attributes.returnValueBlocksByJavaScript setObject:copiedBlock forKey:javaScript];
}
- (NSArray *)executedJavaScripts {

0 comments on commit 25227c8

Please sign in to comment.