Skip to content

Commit

Permalink
JasonLeyba: Changing WebViewController to track the number of page lo…
Browse files Browse the repository at this point in the history
…ads so that it can properly block on frame loads after a WebDriver.get() command. This fixes tests like PageLoadingTest.testShouldBlockUntilIFramesAreLoaded

r10750
  • Loading branch information
jleyba committed Dec 22, 2010
1 parent d448c33 commit 2e7aa95
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
10 changes: 4 additions & 6 deletions iphone/src/objc/WebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
@interface WebViewController : UIViewController<UIWebViewDelegate>
{
@private
// The spec states that the GET message shouldn't return until the new page
// is loaded. We need to lock the main thread to implement that. That'll
// happen by polling [view isLoaded] but we can break early if the delegate
// methods are fired. Note that subframes may still be being loaded.
NSCondition *loadLock_;

// Used to track the number of page loads. The view is considered loaded
// when there are no pending page loads.
int numPendingPageLoads_;

NSString *lastJSResult_;

NSURLRequestCachePolicy cachePolicy_;
Expand Down
28 changes: 22 additions & 6 deletions iphone/src/objc/WebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ - (void)viewDidLoad {
[[self webView] setScalesPageToFit:NO];
[[self webView] setDelegate:self];

loadLock_ = [[NSCondition alloc] init];
lastJSResult_ = nil;

// Creating a new session if auto-create is enabled
Expand Down Expand Up @@ -87,7 +86,6 @@ - (void)didReceiveMemoryWarning {

- (void)dealloc {
[[self webView] setDelegate:nil];
[loadLock_ release];
[lastJSResult_ release];
[super dealloc];
}
Expand All @@ -109,11 +107,16 @@ - (BOOL)webView:(UIWebView *)webView

- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"webViewDidStartLoad");
@synchronized(self) {
numPendingPageLoads_ += 1;
}
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"finished loading");
[loadLock_ signal];
@synchronized(self) {
numPendingPageLoads_ -= 1;
}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
Expand All @@ -134,7 +137,10 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
andStatusCode:EUNHANDLEDERROR];
}
}
[loadLock_ signal];

@synchronized(self) {
numPendingPageLoads_ -= 1;
}
}

#pragma mark Web view controls
Expand All @@ -154,11 +160,21 @@ - (void)waitForLoad {
// whether the page is loading content.

[NSThread sleepForTimeInterval:0.2f];

while ([[self webView] isLoading]) {
// Yield.
[NSThread sleepForTimeInterval:0.01f];
}
}

// The main view may be loaded, but there may be frames that are still
// loading.
while (true) {
@synchronized(self) {
if (numPendingPageLoads_ == 0) {
break;
}
}
}
}

// All method calls on the view need to be done from the main thread to avoid
Expand Down

0 comments on commit 2e7aa95

Please sign in to comment.