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" | 10 #include "base/ios/ios_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #import "base/mac/scoped_nsobject.h" | 12 #import "base/mac/scoped_nsobject.h" |
13 #include "ios/web/public/test/test_browser_state.h" | 13 #include "ios/web/public/test/test_browser_state.h" |
14 #import "ios/web/public/web_view_creation_util.h" | 14 #import "ios/web/public/web_view_creation_util.h" |
15 #import "third_party/ocmock/OCMock/NSInvocation+OCMAdditions.h" | 15 #import "third_party/ocmock/OCMock/NSInvocation+OCMAdditions.h" |
16 #import "third_party/ocmock/OCMock/OCMock.h" | 16 #import "third_party/ocmock/OCMock/OCMock.h" |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Returns an OCMocked WKWebView whose |evaluateJavaScript:stringResultHandler:| | 20 // Returns an OCMocked WKWebView whose |evaluateJavaScript:stringResultHandler:| |
21 // method has been mocked to execute |block| instead. |block| cannot be nil. | 21 // method has been mocked to execute |block| instead. |block| cannot be nil. |
22 WKWebView* CreateMockWKWebViewWithStubbedJSEvalFunction( | 22 WKWebView* CreateMockWKWebViewWithStubbedJSEvalFunction( |
23 void (^block)(NSInvocation*)) { | 23 void (^block)(NSInvocation*)) NS_RETURNS_RETAINED { |
24 DCHECK(block); | 24 DCHECK(block); |
25 web::TestBrowserState browser_state; | 25 web::TestBrowserState browser_state; |
26 base::scoped_nsobject<WKWebView> webView( | 26 base::scoped_nsobject<WKWebView> webView( |
27 web::CreateWKWebView(CGRectZero, &browser_state)); | 27 web::CreateWKWebView(CGRectZero, &browser_state)); |
28 id mockWebView = [OCMockObject partialMockForObject:webView]; | 28 id mockWebView = [OCMockObject partialMockForObject:webView]; |
29 [[[mockWebView stub] andDo:^void(NSInvocation* invocation) { | 29 [[[mockWebView stub] andDo:^void(NSInvocation* invocation) { |
30 block(invocation); | 30 block(invocation); |
31 }] evaluateJavaScript:OCMOCK_ANY completionHandler:OCMOCK_ANY]; | 31 }] evaluateJavaScript:OCMOCK_ANY completionHandler:OCMOCK_ANY]; |
32 return [mockWebView retain]; | 32 return [mockWebView retain]; |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 namespace web { | 37 namespace web { |
38 | 38 |
39 void SimulateWKWebViewCrash(WKWebView* webView) { | 39 void SimulateWKWebViewCrash(WKWebView* webView) { |
40 if (base::ios::IsRunningOnIOS9OrLater()) { | 40 if (base::ios::IsRunningOnIOS9OrLater()) { |
41 SEL selector = @selector(webViewWebContentProcessDidTerminate:); | 41 SEL selector = @selector(webViewWebContentProcessDidTerminate:); |
42 if ([webView.navigationDelegate respondsToSelector:selector]) { | 42 if ([webView.navigationDelegate respondsToSelector:selector]) { |
43 [webView.navigationDelegate performSelector:selector withObject:webView]; | 43 [webView.navigationDelegate performSelector:selector withObject:webView]; |
44 } | 44 } |
45 } | 45 } |
46 [webView performSelector:@selector(_processDidExit)]; | 46 [webView performSelector:@selector(_processDidExit)]; |
47 } | 47 } |
48 | 48 |
49 WKWebView* CreateTerminatedWKWebView() { | 49 WKWebView* CreateTerminatedWKWebView() NS_RETURNS_RETAINED { |
50 id fail = ^void(NSInvocation* invocation) { | 50 id fail = ^void(NSInvocation* invocation) { |
51 // Always fails with WKErrorWebContentProcessTerminated error. | 51 // Always fails with WKErrorWebContentProcessTerminated error. |
52 NSError* error = | 52 NSError* error = |
53 [NSError errorWithDomain:WKErrorDomain | 53 [NSError errorWithDomain:WKErrorDomain |
54 code:WKErrorWebContentProcessTerminated | 54 code:WKErrorWebContentProcessTerminated |
55 userInfo:nil]; | 55 userInfo:nil]; |
56 | 56 |
57 void (^completionHandler)(id, NSError*) = | 57 void (^completionHandler)(id, NSError*) = |
58 [invocation getArgumentAtIndexAsObject:3]; | 58 [invocation getArgumentAtIndexAsObject:3]; |
59 completionHandler(nil, error); | 59 completionHandler(nil, error); |
60 }; | 60 }; |
61 return CreateMockWKWebViewWithStubbedJSEvalFunction(fail); | 61 return CreateMockWKWebViewWithStubbedJSEvalFunction(fail); |
62 } | 62 } |
63 | 63 |
64 WKWebView* CreateHealthyWKWebView() { | 64 WKWebView* CreateHealthyWKWebView() NS_RETURNS_RETAINED { |
65 id succeed = ^void(NSInvocation* invocation) { | 65 id succeed = ^void(NSInvocation* invocation) { |
66 void (^completionHandler)(id, NSError*) = | 66 void (^completionHandler)(id, NSError*) = |
67 [invocation getArgumentAtIndexAsObject:3]; | 67 [invocation getArgumentAtIndexAsObject:3]; |
68 // Always succceeds with nil result. | 68 // Always succceeds with nil result. |
69 completionHandler(nil, nil); | 69 completionHandler(nil, nil); |
70 }; | 70 }; |
71 return CreateMockWKWebViewWithStubbedJSEvalFunction(succeed); | 71 return CreateMockWKWebViewWithStubbedJSEvalFunction(succeed); |
72 } | 72 } |
73 | 73 |
74 } // namespace web | 74 } // namespace web |
OLD | NEW |