| 1 | /* |
| 2 | * Copyright (C) 2014 Igalia S.L. |
| 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 | |
| 35 | namespace TestWebKitAPI { |
| 36 | |
| 37 | static bool done; |
| 38 | |
| 39 | TEST(WebKit, PendingAPIRequestURL) |
| 40 | { |
| 41 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 42 | PlatformWebView webView(context.get()); |
| 43 | |
| 44 | WKPageNavigationClientV0 loaderClient; |
| 45 | memset(&loaderClient, 0, sizeof(loaderClient)); |
| 46 | loaderClient.base.version = 0; |
| 47 | loaderClient.didFinishNavigation = [](WKPageRef, WKNavigationRef, WKTypeRef, const void*) { |
| 48 | done = true; |
| 49 | }; |
| 50 | WKPageSetPageNavigationClient(webView.page(), &loaderClient.base); |
| 51 | |
| 52 | WKRetainPtr<WKURLRef> activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 53 | EXPECT_NULL(activeURL.get()); |
| 54 | |
| 55 | WKRetainPtr<WKURLRef> url = adoptWK(Util::createURLForResource("simple" , "html" )); |
| 56 | WKPageLoadURL(webView.page(), url.get()); |
| 57 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 58 | ASSERT_NOT_NULL(activeURL.get()); |
| 59 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 60 | Util::run(&done); |
| 61 | done = false; |
| 62 | |
| 63 | WKPageReload(webView.page()); |
| 64 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 65 | ASSERT_NOT_NULL(activeURL.get()); |
| 66 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 67 | Util::run(&done); |
| 68 | done = false; |
| 69 | |
| 70 | WKRetainPtr<WKStringRef> htmlString = Util::toWK("<body>Hello, World</body>" ); |
| 71 | WKRetainPtr<WKURLRef> blankURL = adoptWK(WKURLCreateWithUTF8CString("about:blank" )); |
| 72 | WKPageLoadHTMLString(webView.page(), htmlString.get(), nullptr); |
| 73 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 74 | ASSERT_NOT_NULL(activeURL.get()); |
| 75 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), blankURL.get())); |
| 76 | Util::run(&done); |
| 77 | done = false; |
| 78 | |
| 79 | WKPageReload(webView.page()); |
| 80 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 81 | ASSERT_NOT_NULL(activeURL.get()); |
| 82 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), blankURL.get())); |
| 83 | Util::run(&done); |
| 84 | done = false; |
| 85 | |
| 86 | url = adoptWK(Util::createURLForResource("simple2" , "html" )); |
| 87 | WKPageLoadHTMLString(webView.page(), htmlString.get(), url.get()); |
| 88 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 89 | ASSERT_NOT_NULL(activeURL.get()); |
| 90 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 91 | Util::run(&done); |
| 92 | done = false; |
| 93 | |
| 94 | WKPageReload(webView.page()); |
| 95 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 96 | ASSERT_NOT_NULL(activeURL.get()); |
| 97 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 98 | Util::run(&done); |
| 99 | done = false; |
| 100 | |
| 101 | WKRetainPtr<WKDataRef> data = adoptWK(WKDataCreate(nullptr, 0)); |
| 102 | WKPageLoadData(webView.page(), data.get(), nullptr, nullptr, nullptr); |
| 103 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 104 | ASSERT_NOT_NULL(activeURL.get()); |
| 105 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), blankURL.get())); |
| 106 | Util::run(&done); |
| 107 | done = false; |
| 108 | |
| 109 | WKPageReload(webView.page()); |
| 110 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 111 | ASSERT_NOT_NULL(activeURL.get()); |
| 112 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), blankURL.get())); |
| 113 | Util::run(&done); |
| 114 | done = false; |
| 115 | |
| 116 | WKPageLoadData(webView.page(), data.get(), nullptr, nullptr, url.get()); |
| 117 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 118 | ASSERT_NOT_NULL(activeURL.get()); |
| 119 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 120 | Util::run(&done); |
| 121 | done = false; |
| 122 | |
| 123 | WKPageReload(webView.page()); |
| 124 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 125 | ASSERT_NOT_NULL(activeURL.get()); |
| 126 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 127 | Util::run(&done); |
| 128 | done = false; |
| 129 | |
| 130 | WKPageLoadAlternateHTMLString(webView.page(), htmlString.get(), nullptr, url.get()); |
| 131 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 132 | ASSERT_NOT_NULL(activeURL.get()); |
| 133 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 134 | Util::run(&done); |
| 135 | done = false; |
| 136 | |
| 137 | WKPageReload(webView.page()); |
| 138 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 139 | ASSERT_NOT_NULL(activeURL.get()); |
| 140 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 141 | Util::run(&done); |
| 142 | done = false; |
| 143 | |
| 144 | WKRetainPtr<WKStringRef> plainTextString = Util::toWK("Hello, World" ); |
| 145 | WKPageLoadPlainTextString(webView.page(), plainTextString.get()); |
| 146 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 147 | ASSERT_NOT_NULL(activeURL.get()); |
| 148 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), blankURL.get())); |
| 149 | Util::run(&done); |
| 150 | done = false; |
| 151 | |
| 152 | WKPageReload(webView.page()); |
| 153 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 154 | ASSERT_NOT_NULL(activeURL.get()); |
| 155 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), blankURL.get())); |
| 156 | Util::run(&done); |
| 157 | done = false; |
| 158 | |
| 159 | url = adoptWK(WKURLCreateWithUTF8CString("file:///tmp/index.html" )); |
| 160 | WKPageLoadFile(webView.page(), url.get(), nullptr); |
| 161 | activeURL = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 162 | ASSERT_NOT_NULL(activeURL.get()); |
| 163 | EXPECT_TRUE(WKURLIsEqual(activeURL.get(), url.get())); |
| 164 | WKPageStopLoading(webView.page()); |
| 165 | } |
| 166 | |
| 167 | } // namespace TestWebKitAPI |
| 168 | |
| 169 | #endif |
| 170 | |