|
9 | 9 |
|
10 | 10 | #import "PFOAuth1FlowDialog.h"
|
11 | 11 | #import "PFTwitterTestCase.h"
|
| 12 | +#import <WebKit/WebKit.h> |
12 | 13 |
|
13 | 14 | @interface UIActivityIndicatorView (Private)
|
14 | 15 |
|
15 | 16 | - (void)_generateImages;
|
16 | 17 |
|
17 | 18 | @end
|
18 | 19 |
|
| 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 | + |
19 | 42 | @interface OAuth1FlowDialogTests : PFTwitterTestCase
|
20 | 43 | @end
|
21 | 44 |
|
@@ -106,33 +129,50 @@ - (void)testWebViewDelegate {
|
106 | 129 | NSURL *sampleURL = [NSURL URLWithString:@"http://foo.bar"];
|
107 | 130 | NSURL *successURL = [NSURL URLWithString:@"foo://success"];
|
108 | 131 |
|
109 |
| - XCTestExpectation *expectation = [self currentSelectorTestExpectation]; |
| 132 | + //XCTestExpectation *flowExpectation = [[XCTestExpectation alloc] initWithDescription: @"Waiting for redirect"]; |
| 133 | + XCTestExpectation *flowExpectation = [self currentSelectorTestExpectation]; |
| 134 | + |
110 | 135 | PFOAuth1FlowDialog *flowDialog = [[PFOAuth1FlowDialog alloc] initWithURL:sampleURL queryParameters:nil];
|
111 | 136 | flowDialog.redirectURLPrefix = @"foo://";
|
112 | 137 | 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]; |
118 | 142 | };
|
119 | 143 |
|
120 | 144 | [flowDialog showAnimated:NO];
|
121 | 145 |
|
122 |
| - id webView = PFStrictClassMock([UIWebView class]); |
| 146 | + id webView = PFClassMock([WKWebView class]); |
123 | 147 |
|
124 | 148 | 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]; |
136 | 176 | }
|
137 | 177 |
|
138 | 178 | @end
|
0 commit comments