1/*
2 * Copyright (C) 2013 Adenilson Cavalcanti <cavalcantii@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if WK_HAVE_C_SPI
29
30#include "PlatformUtilities.h"
31#include "PlatformWebView.h"
32#include "Test.h"
33#include <WebKit/WKRetainPtr.h>
34
35namespace TestWebKitAPI {
36
37static void didFinishLoad(WKPageRef, WKNavigationRef, WKTypeRef, const void*);
38
39class WebKit2CrashLoader {
40public:
41 WebKit2CrashLoader()
42 : context(adoptWK(WKContextCreateWithConfiguration(nullptr)))
43 , webView(context.get())
44 , url(adoptWK(WKURLCreateWithUTF8CString("about:blank")))
45 , firstSuccessfulLoad(false)
46 , secondSuccessfulLoad(false)
47 {
48 memset(&loaderClient, 0, sizeof(loaderClient));
49
50 loaderClient.base.version = 0;
51 loaderClient.base.clientInfo = this;
52 loaderClient.didFinishNavigation = didFinishLoad;
53
54 WKPageSetPageNavigationClient(webView.page(), &loaderClient.base);
55 }
56
57 void loadUrl()
58 {
59 WKPageLoadURL(webView.page(), url.get());
60 }
61
62 void terminateWebProcess()
63 {
64 WKPageTerminate(webView.page());
65 }
66
67 WKRetainPtr<WKContextRef> context;
68 WKPageNavigationClientV0 loaderClient;
69 PlatformWebView webView;
70 WKRetainPtr<WKURLRef> url;
71
72 bool firstSuccessfulLoad;
73 bool secondSuccessfulLoad;
74};
75
76// We are going to have 2 load events intertwined by a simulated crash
77// (i.e. Load -> Crash -> Load).
78void didFinishLoad(WKPageRef page, WKNavigationRef, WKTypeRef userData, const void* clientInfo)
79{
80 WebKit2CrashLoader* testHelper = const_cast<WebKit2CrashLoader*>(static_cast<const WebKit2CrashLoader*>(clientInfo));
81
82 // First load worked, let's crash WebProcess.
83 if (!testHelper->firstSuccessfulLoad) {
84 testHelper->firstSuccessfulLoad = true;
85 return;
86 }
87
88 // Second load worked, we are done.
89 EXPECT_TRUE(testHelper->firstSuccessfulLoad);
90 if (!testHelper->secondSuccessfulLoad) {
91 testHelper->secondSuccessfulLoad = true;
92 return;
93 }
94}
95
96// This test will load a blank page and next kill WebProcess, the expected
97// result is that a call to page load should spawn a new WebProcess.
98TEST(WebKit, LoadPageAfterCrash)
99{
100 WebKit2CrashLoader helper;
101 helper.loadUrl();
102 Util::run(&helper.firstSuccessfulLoad);
103 helper.terminateWebProcess();
104 helper.loadUrl();
105 Util::run(&helper.secondSuccessfulLoad);
106}
107
108} // namespace TestWebKitAPI
109
110#endif
111