Skip to content

Commit 6da04e9

Browse files
authored
Converting TwitterUtils to WKWebView (#1511)
* Converting UIWebView on TwitterUtils to WKWebView * Fixing Unit Tests
1 parent 6ca8f40 commit 6da04e9

File tree

3 files changed

+86
-47
lines changed

3 files changed

+86
-47
lines changed

ParseTwitterUtils/ParseTwitterUtils/Internal/Dialog/PFOAuth1FlowDialog.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#import <Foundation/Foundation.h>
1111
#import <UIKit/UIKit.h>
12-
12+
#import <WebKit/WebKit.h>
1313
@class PFOAuth1FlowDialog;
1414

1515
@protocol PFOAuth1FlowDialogDataSource <NSObject>
@@ -66,7 +66,7 @@ typedef void (^PFOAuth1FlowDialogCompletion)(BOOL succeeded, NSURL *url, NSError
6666

6767
@end
6868

69-
@interface PFOAuth1FlowDialog : UIView <UIWebViewDelegate, PFOAuth1FlowDialogInterface> {
69+
@interface PFOAuth1FlowDialog : UIView <WKNavigationDelegate, PFOAuth1FlowDialogInterface> {
7070
@public
7171
// Ensures that UI elements behind the dialog are disabled.
7272
UIView *_modalBackgroundView;
@@ -76,7 +76,7 @@ typedef void (^PFOAuth1FlowDialogCompletion)(BOOL succeeded, NSURL *url, NSError
7676

7777
UILabel *_titleLabel;
7878
UIButton *_closeButton;
79-
UIWebView *_webView;
79+
WKWebView *_webView;
8080
UIActivityIndicatorView *_activityIndicator;
8181

8282
UIInterfaceOrientation _orientation;

ParseTwitterUtils/ParseTwitterUtils/Internal/Dialog/PFOAuth1FlowDialog.m

+25-26
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
#import "PFOAuth1FlowDialog.h"
11-
11+
#import <WebKit/WebKit.h>
1212
#import <Parse/PFNetworkActivityIndicatorManager.h>
1313

1414
@implementation PFOAuth1FlowDialog
@@ -204,9 +204,9 @@ - (instancetype)init {
204204
_titleLabel.font = [UIFont boldSystemFontOfSize:titleLabelFontSize];
205205
[self addSubview:_titleLabel];
206206

207-
_webView = [[UIWebView alloc] initWithFrame:CGRectZero];
207+
_webView = [[WKWebView alloc] initWithFrame:CGRectZero];
208208
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
209-
_webView.delegate = self;
209+
_webView.navigationDelegate = self;
210210
[self addSubview:_webView];
211211

212212
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:
@@ -240,7 +240,7 @@ + (instancetype)dialogWithURL:(NSURL *)url queryParameters:(NSDictionary *)query
240240
#pragma mark Dealloc
241241

242242
- (void)dealloc {
243-
_webView.delegate = nil;
243+
_webView.navigationDelegate = nil;
244244

245245
[[NSNotificationCenter defaultCenter] removeObserver:self];
246246
}
@@ -408,41 +408,40 @@ - (void)loadURL:(NSURL *)url queryParameters:(NSDictionary *)parameters {
408408
}
409409

410410
#pragma mark -
411-
#pragma mark UIWebViewDelegate
412-
413-
- (BOOL)webView:(UIWebView *)webView
414-
shouldStartLoadWithRequest:(NSURLRequest *)request
415-
navigationType:(UIWebViewNavigationType)navigationType {
416-
NSURL *url = request.URL;
411+
#pragma mark WKWebViewNavigationDelegate
417412

418-
if ([url.absoluteString hasPrefix:self.redirectURLPrefix]) {
413+
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
414+
NSURL *url = navigationAction.request.URL;
415+
BOOL hasPrefix = [url.absoluteString hasPrefix:self.redirectURLPrefix];
416+
if (hasPrefix) {
419417
[self _dismissWithSuccess:YES url:url error:nil];
420-
return NO;
421-
} else if ([_loadingURL isEqual:url]) {
422-
return YES;
423-
} else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
424-
if ([self.dataSource dialog:self shouldOpenURLInExternalBrowser:url]) {
425-
[[UIApplication sharedApplication] openURL:url];
426-
} else {
427-
return YES;
428-
}
418+
decisionHandler(WKNavigationActionPolicyCancel);
419+
return;
420+
} else if (navigationAction.navigationType == UIWebViewNavigationTypeLinkClicked && [self.dataSource dialog:self shouldOpenURLInExternalBrowser:url]) {
421+
[[UIApplication sharedApplication] openURL:url];
422+
} else {
423+
decisionHandler(WKNavigationActionPolicyAllow);
429424
}
430-
431-
return YES;
432425
}
433426

434-
- (void)webViewDidStartLoad:(UIWebView *)webView {
427+
428+
429+
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
435430
[[PFNetworkActivityIndicatorManager sharedManager] incrementActivityCount];
436431
}
437432

438-
- (void)webViewDidFinishLoad:(UIWebView *)webView {
433+
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
439434
[[PFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
440435

441436
[_activityIndicator stopAnimating];
442-
self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
437+
[_webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable string, NSError * _Nullable error) {
438+
self.title = (NSString *) string;
439+
440+
}];
441+
443442
}
444443

445-
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
444+
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
446445
[[PFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
447446

448447
// 102 == WebKitErrorFrameLoadInterruptedByPolicyChange

ParseTwitterUtils/Tests/Unit/OAuth1FlowDialogTests.m

+58-18
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,36 @@
99

1010
#import "PFOAuth1FlowDialog.h"
1111
#import "PFTwitterTestCase.h"
12+
#import <WebKit/WebKit.h>
1213

1314
@interface UIActivityIndicatorView (Private)
1415

1516
- (void)_generateImages;
1617

1718
@end
1819

20+
@interface FakeWKNavigationAction : WKNavigationAction
21+
// Redefined WKNavigationAction properties as readwrite.
22+
@property(nullable, nonatomic, copy) WKFrameInfo* sourceFrame;
23+
@property(nullable, nonatomic, copy) WKFrameInfo* targetFrame;
24+
@property(nonatomic) WKNavigationType navigationType;
25+
@property(nullable, nonatomic, copy) NSURLRequest* request;
26+
27+
@end
28+
29+
@implementation FakeWKNavigationAction
30+
@synthesize sourceFrame, targetFrame, navigationType, request;
31+
32+
+ (Class)class {
33+
return [super class];
34+
}
35+
36+
+ (Class)_nilClass {
37+
return nil;
38+
}
39+
40+
@end
41+
1942
@interface OAuth1FlowDialogTests : PFTwitterTestCase
2043
@end
2144

@@ -106,33 +129,50 @@ - (void)testWebViewDelegate {
106129
NSURL *sampleURL = [NSURL URLWithString:@"http://foo.bar"];
107130
NSURL *successURL = [NSURL URLWithString:@"foo://success"];
108131

109-
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
132+
//XCTestExpectation *flowExpectation = [[XCTestExpectation alloc] initWithDescription: @"Waiting for redirect"];
133+
XCTestExpectation *flowExpectation = [self currentSelectorTestExpectation];
134+
110135
PFOAuth1FlowDialog *flowDialog = [[PFOAuth1FlowDialog alloc] initWithURL:sampleURL queryParameters:nil];
111136
flowDialog.redirectURLPrefix = @"foo://";
112137
flowDialog.completion = ^(BOOL succeeded, NSURL *url, NSError *error) {
113-
XCTAssertTrue(succeeded);
114-
XCTAssertNil(error);
115-
XCTAssertEqualObjects(url, successURL);
116-
117-
[expectation fulfill];
138+
XCTAssertTrue(succeeded, @"Flow Dialogue Failed");
139+
XCTAssertNil(error, @"error");
140+
XCTAssertEqualObjects(url, successURL, @"url's arent equal");
141+
[flowExpectation fulfill];
118142
};
119143

120144
[flowDialog showAnimated:NO];
121145

122-
id webView = PFStrictClassMock([UIWebView class]);
146+
id webView = PFClassMock([WKWebView class]);
123147

124148
NSURLRequest *request = [NSURLRequest requestWithURL:sampleURL];
125-
XCTAssertTrue([flowDialog webView:webView
126-
shouldStartLoadWithRequest:request
127-
navigationType:UIWebViewNavigationTypeOther]);
128-
129-
[flowDialog webViewDidStartLoad:webView];
130-
[flowDialog webViewDidFinishLoad:webView];
131-
132-
NSURLRequest *successRequest = [NSURLRequest requestWithURL:successURL];
133-
[flowDialog webView:webView shouldStartLoadWithRequest:successRequest navigationType:UIWebViewNavigationTypeOther];
134-
135-
[self waitForTestExpectations];
149+
XCTestExpectation *policyExpectation = [[XCTestExpectation alloc] initWithDescription:@"Waiting for allowed policy decision"];
150+
151+
FakeWKNavigationAction *navigationAction = [[FakeWKNavigationAction alloc] init];
152+
navigationAction.navigationType = WKNavigationTypeOther;
153+
navigationAction.request = request;
154+
155+
[flowDialog webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:^(WKNavigationActionPolicy policy) {
156+
XCTAssertTrue(policy == WKNavigationActionPolicyAllow, @"policy not allowed");
157+
[policyExpectation fulfill];
158+
}];
159+
160+
XCTestExpectation *canceledExpectation = [[XCTestExpectation alloc] initWithDescription:@"Waiting for canceled policy decision"];
161+
162+
WKNavigation* navigation = (WKNavigation *)([[NSObject alloc] init]);
163+
[flowDialog webView:webView didStartProvisionalNavigation:navigation];
164+
[flowDialog webView:webView didFinishNavigation:navigation];
165+
166+
FakeWKNavigationAction *successAction = [[FakeWKNavigationAction alloc] init];
167+
successAction.navigationType = WKNavigationTypeOther;
168+
successAction.request = [NSURLRequest requestWithURL:successURL];
169+
170+
[flowDialog webView:webView decidePolicyForNavigationAction:successAction decisionHandler:^(WKNavigationActionPolicy policy) {
171+
XCTAssertTrue(policy == WKNavigationActionPolicyCancel);
172+
[canceledExpectation fulfill];
173+
}];
174+
175+
[self waitForExpectations:@[policyExpectation, flowExpectation, canceledExpectation] timeout:20];
136176
}
137177

138178
@end

0 commit comments

Comments
 (0)