0

I am writing some automated tests that will test some actions happening inside my root UIViewController. As of now I'm using SenTestCase. I am using storyboards also. How can I do this? And how can I easily listen for delegate methods on the view controller's UIWebView?

What I've tried is to store a block which runs after web view is completed loading its content.

@interface FirstViewController : UIViewController
typedef void (^webViewDoneLoadingBlock)(void);
@property (nonatomic,strong) webViewDoneLoadingBlock doneLoading;
@end

I run this block in First view controller's UIWebView's delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    self.doneLoading();
}

In setUp I fetch out the first view controller from the storyboard:

- (void)setUp
{
    [super setUp];
    UIStoryboard *storyboard = 
      [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UINavigationController *navController = [storyboard instantiateInitialViewController];
    FirstViewController *firstVC = (id) navController.topViewController;
    self.firstViewController = firstVC;

}

The test (I use dispatchSemaphoreInBlock: to get the test to wait till thenResume is called to say if the test failed or not):

- (void)testExample
{

    [self dispatchSemaphoreInBlock:^(void (^thenResume)(void)) {

        self.firstViewController.doneLoading = ^{
            NSLog(@"done LOADING");
            thenResume();
        };
    }];
}

What fails is EXC_BAD_ACCESS when hitting self.doneLoading(); in webViewDidFinishLoad:

Am I storing the block wrong here? Or is there another way to hang on to the FirstViewController's delegate methods from the SenTestCase class?

Code for the dispatch semaphore (this is code I've used through many other projects, so I know this isn't the reason why it fails):

- (void)dispatchSemaphoreInBlock:(void (^)(void (^resume)(void)))theBlock
{
    dispatch_semaphore_t theSemaphore = dispatch_semaphore_create(0);
    theBlock(^{
        dispatch_semaphore_signal(theSemaphore);
    });

    while ( dispatch_semaphore_wait(theSemaphore, DISPATCH_TIME_NOW) ) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode 
                                 beforeDate:[NSDate distantFuture]];
    }
}
bogen
  • 9,954
  • 9
  • 50
  • 89
  • 2
    You're welcome for the feedback. For future reference, changing your question in such a way as to invalidate existing answers is not ideal: http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions – Carl Veazey Sep 12 '13 at 09:14
  • @CarlVeazey I agree, might be better to create a new question. It's still the same problem, the FirstViewController can't run its block so the SenTestCase can know the ViewView finished loadin. – bogen Sep 12 '13 at 09:18
  • @abbood Yes always :-) – bogen Sep 12 '13 at 09:21
  • just for fun, can you change the property of `doneLoading` from `@property (nonatomic,strong) webViewDoneLoadingBlock doneLoading;` to `@property (nonatomic,copy) webViewDoneLoadingBlock doneLoading;` see if it works.. – abbood Sep 12 '13 at 09:25
  • You set the `doneLoading` in the block that `dispatchSemaphoreInBlock:` is executing. Where, exactly, are you starting the web view to load? Which executes first - the code setting `doneLoading`, or `webViewDidFinishLoad:`? – Carl Veazey Sep 12 '13 at 09:28
  • @abbood setting copy did not work. – bogen Sep 12 '13 at 09:30
  • in your implementation of `dispatchSemaphoreInBlock` i never see the`resume` block being called? how come? – abbood Sep 12 '13 at 09:32
  • @CarlVeazey The dispatchSemaphoreInBlock: method definately get called before the webview finishes loading – bogen Sep 12 '13 at 10:21
  • 1
    Perhaps I wasn't clear. Which executes first - the line inside the block passed to the semaphore method where you assign the doneLoading property, or the wevViewDidFinish... Method? To restate, I'm interested in the timing of when you set the block property and when you call it. Also, what's the value of doneLoading when it's called? – Carl Veazey Sep 12 '13 at 11:43
  • yap.. i agree with @CarlVeazey .. seems like a race condition to me.. – abbood Sep 12 '13 at 11:56

0 Answers0