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
3 // found in the LICENSE file.
5 #import "ios/web/test/wk_web_view_crash_utils.h"
7 #import <Foundation/Foundation.h>
9 #include "base/logging.h"
10 #import "base/mac/scoped_nsobject.h"
11 #include "ios/web/public/test/test_browser_state.h"
12 #import "ios/web/public/web_view_creation_util.h"
13 #import "third_party/ocmock/OCMock/NSInvocation+OCMAdditions.h"
14 #import "third_party/ocmock/OCMock/OCMock.h"
18 // Returns an OCMocked WKWebView whose |evaluateJavaScript:stringResultHandler:|
19 // method has been mocked to execute |block| instead. |block| cannot be nil.
20 WKWebView* CreateMockWKWebViewWithStubbedJSEvalFunction(
21 void (^block)(NSInvocation*)) {
23 web::TestBrowserState browser_state;
24 base::scoped_nsobject<WKWebView> webView(
25 web::CreateWKWebView(CGRectZero, &browser_state));
26 id mockWebView = [OCMockObject partialMockForObject:webView];
27 [[[mockWebView stub] andDo:^void(NSInvocation* invocation) {
29 }] evaluateJavaScript:OCMOCK_ANY completionHandler:OCMOCK_ANY];
30 return [mockWebView retain];
37 void SimulateWKWebViewCrash(WKWebView* webView) {
38 [webView performSelector:@selector(_processDidExit)];
41 WKWebView* CreateTerminatedWKWebView() {
42 id fail = ^void(NSInvocation* invocation) {
43 // Always fails with WKErrorWebContentProcessTerminated error.
45 [NSError errorWithDomain:WKErrorDomain
46 code:WKErrorWebContentProcessTerminated
49 void (^completionHandler)(id, NSError*) =
50 [invocation getArgumentAtIndexAsObject:3];
51 completionHandler(nil, error);
53 return CreateMockWKWebViewWithStubbedJSEvalFunction(fail);
56 WKWebView* CreateHealthyWKWebView() {
57 id succeed = ^void(NSInvocation* invocation) {
58 void (^completionHandler)(id, NSError*) =
59 [invocation getArgumentAtIndexAsObject:3];
60 // Always succceeds with nil result.
61 completionHandler(nil, nil);
63 return CreateMockWKWebViewWithStubbedJSEvalFunction(succeed);