OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/web/test/wk_web_view_crash_utils.h" | 5 #import "ios/web/test/wk_web_view_crash_utils.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
9 | 9 |
10 #include "base/ios/ios_util.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #import "base/mac/scoped_nsobject.h" | 11 #import "base/mac/scoped_nsobject.h" |
13 #include "ios/web/public/test/test_browser_state.h" | 12 #include "ios/web/public/test/test_browser_state.h" |
14 #import "ios/web/public/web_view_creation_util.h" | 13 #import "ios/web/public/web_view_creation_util.h" |
15 #import "third_party/ocmock/OCMock/NSInvocation+OCMAdditions.h" | 14 #import "third_party/ocmock/OCMock/NSInvocation+OCMAdditions.h" |
16 #import "third_party/ocmock/OCMock/OCMock.h" | 15 #import "third_party/ocmock/OCMock/OCMock.h" |
17 | 16 |
18 namespace { | 17 namespace { |
19 | 18 |
20 // Returns an OCMocked WKWebView whose |evaluateJavaScript:completionHandler:| | 19 // Returns an OCMocked WKWebView whose |evaluateJavaScript:completionHandler:| |
21 // method has been mocked to execute |block| instead. |block| cannot be nil. | 20 // method has been mocked to execute |block| instead. |block| cannot be nil. |
22 WKWebView* CreateMockWKWebViewWithStubbedJSEvalFunction( | 21 WKWebView* CreateMockWKWebViewWithStubbedJSEvalFunction( |
23 void (^block)(NSInvocation*)) { | 22 void (^block)(NSInvocation*)) { |
24 DCHECK(block); | 23 DCHECK(block); |
25 web::TestBrowserState browser_state; | 24 web::TestBrowserState browser_state; |
26 base::scoped_nsobject<WKWebView> webView( | 25 base::scoped_nsobject<WKWebView> webView( |
27 web::CreateWKWebView(CGRectZero, &browser_state)); | 26 web::CreateWKWebView(CGRectZero, &browser_state)); |
28 id mockWebView = [OCMockObject partialMockForObject:webView]; | 27 id mockWebView = [OCMockObject partialMockForObject:webView]; |
29 [[[mockWebView stub] andDo:^void(NSInvocation* invocation) { | 28 [[[mockWebView stub] andDo:^void(NSInvocation* invocation) { |
30 block(invocation); | 29 block(invocation); |
31 }] evaluateJavaScript:OCMOCK_ANY completionHandler:OCMOCK_ANY]; | 30 }] evaluateJavaScript:OCMOCK_ANY completionHandler:OCMOCK_ANY]; |
32 return [mockWebView retain]; | 31 return [mockWebView retain]; |
33 } | 32 } |
34 | 33 |
35 } // namespace | 34 } // namespace |
36 | 35 |
37 namespace web { | 36 namespace web { |
38 | 37 |
39 void SimulateWKWebViewCrash(WKWebView* webView) { | 38 void SimulateWKWebViewCrash(WKWebView* webView) { |
40 if (base::ios::IsRunningOnIOS9OrLater()) { | 39 SEL selector = @selector(webViewWebContentProcessDidTerminate:); |
41 SEL selector = @selector(webViewWebContentProcessDidTerminate:); | 40 if ([webView.navigationDelegate respondsToSelector:selector]) { |
42 if ([webView.navigationDelegate respondsToSelector:selector]) { | 41 [webView.navigationDelegate performSelector:selector withObject:webView]; |
43 [webView.navigationDelegate performSelector:selector withObject:webView]; | |
44 } | |
45 } | 42 } |
46 [webView performSelector:@selector(_processDidExit)]; | 43 [webView performSelector:@selector(_processDidExit)]; |
47 } | 44 } |
48 | 45 |
49 WKWebView* CreateTerminatedWKWebView() { | 46 WKWebView* CreateTerminatedWKWebView() { |
50 id fail = ^void(NSInvocation* invocation) { | 47 id fail = ^void(NSInvocation* invocation) { |
51 // Always fails with WKErrorWebContentProcessTerminated error. | 48 // Always fails with WKErrorWebContentProcessTerminated error. |
52 NSError* error = | 49 NSError* error = |
53 [NSError errorWithDomain:WKErrorDomain | 50 [NSError errorWithDomain:WKErrorDomain |
54 code:WKErrorWebContentProcessTerminated | 51 code:WKErrorWebContentProcessTerminated |
(...skipping 10 matching lines...) Expand all Loading... |
65 id succeed = ^void(NSInvocation* invocation) { | 62 id succeed = ^void(NSInvocation* invocation) { |
66 void (^completionHandler)(id, NSError*) = | 63 void (^completionHandler)(id, NSError*) = |
67 [invocation getArgumentAtIndexAsObject:3]; | 64 [invocation getArgumentAtIndexAsObject:3]; |
68 // Always succceeds with nil result. | 65 // Always succceeds with nil result. |
69 completionHandler(nil, nil); | 66 completionHandler(nil, nil); |
70 }; | 67 }; |
71 return CreateMockWKWebViewWithStubbedJSEvalFunction(succeed); | 68 return CreateMockWKWebViewWithStubbedJSEvalFunction(succeed); |
72 } | 69 } |
73 | 70 |
74 } // namespace web | 71 } // namespace web |
OLD | NEW |