LoadPageOnCrash.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "config.h"
  26. #include "PlatformUtilities.h"
  27. #include "PlatformWebView.h"
  28. #include "Test.h"
  29. #include <WebKit2/WKRetainPtr.h>
  30. namespace TestWebKitAPI {
  31. static void didFinishLoad(WKPageRef, WKFrameRef, WKTypeRef, const void*);
  32. class WebKit2CrashLoader {
  33. public:
  34. WebKit2CrashLoader()
  35. : context(AdoptWK, WKContextCreate())
  36. , webView(context.get())
  37. , url(adoptWK(WKURLCreateWithUTF8CString("about:blank")))
  38. , firstSuccessfulLoad(false)
  39. , secondSuccessfulLoad(false)
  40. {
  41. memset(&loaderClient, 0, sizeof(loaderClient));
  42. loaderClient.clientInfo = this;
  43. loaderClient.didFinishLoadForFrame = didFinishLoad;
  44. WKPageSetPageLoaderClient(webView.page(), &loaderClient);
  45. }
  46. void loadUrl()
  47. {
  48. WKPageLoadURL(webView.page(), url.get());
  49. }
  50. void terminateWebProcess()
  51. {
  52. WKPageTerminate(webView.page());
  53. }
  54. WKRetainPtr<WKContextRef> context;
  55. WKPageLoaderClient loaderClient;
  56. PlatformWebView webView;
  57. WKRetainPtr<WKURLRef> url;
  58. bool firstSuccessfulLoad;
  59. bool secondSuccessfulLoad;
  60. };
  61. // We are going to have 2 load events intertwined by a simulated crash
  62. // (i.e. Load -> Crash -> Load).
  63. void didFinishLoad(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo)
  64. {
  65. WebKit2CrashLoader* testHelper = const_cast<WebKit2CrashLoader*>(static_cast<const WebKit2CrashLoader*>(clientInfo));
  66. // First load worked, let's crash WebProcess.
  67. if (!testHelper->firstSuccessfulLoad) {
  68. testHelper->firstSuccessfulLoad = true;
  69. return;
  70. }
  71. // Second load worked, we are done.
  72. EXPECT_TRUE(testHelper->firstSuccessfulLoad);
  73. if (!testHelper->secondSuccessfulLoad) {
  74. testHelper->secondSuccessfulLoad = true;
  75. return;
  76. }
  77. }
  78. // This test will load a blank page and next kill WebProcess, the expected
  79. // result is that a call to page load should spawn a new WebProcess.
  80. TEST(WebKit2, LoadPageAfterCrash)
  81. {
  82. WebKit2CrashLoader helper;
  83. helper.loadUrl();
  84. Util::run(&helper.firstSuccessfulLoad);
  85. helper.terminateWebProcess();
  86. helper.loadUrl();
  87. Util::run(&helper.secondSuccessfulLoad);
  88. }
  89. } // namespace TestWebKitAPI