| 1 | /* |
| 2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 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 test1Done; |
| 38 | |
| 39 | struct State { |
| 40 | State() |
| 41 | : didDecidePolicyForNavigationAction(false) |
| 42 | , didStartProvisionalNavigation(false) |
| 43 | , didCommitNavigation(false) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | bool didDecidePolicyForNavigationAction; |
| 48 | bool didStartProvisionalNavigation; |
| 49 | bool didCommitNavigation; |
| 50 | }; |
| 51 | |
| 52 | static void didStartProvisionalNavigation(WKPageRef page, WKNavigationRef, WKTypeRef userData, const void* clientInfo) |
| 53 | { |
| 54 | State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); |
| 55 | EXPECT_TRUE(state->didDecidePolicyForNavigationAction); |
| 56 | EXPECT_FALSE(state->didCommitNavigation); |
| 57 | |
| 58 | // The commited URL should be null. |
| 59 | EXPECT_NULL(WKFrameCopyURL(WKPageGetMainFrame(page))); |
| 60 | |
| 61 | EXPECT_FALSE(state->didStartProvisionalNavigation); |
| 62 | |
| 63 | state->didStartProvisionalNavigation = true; |
| 64 | } |
| 65 | |
| 66 | static void didCommitNavigation(WKPageRef page, WKNavigationRef, WKTypeRef userData, const void* clientInfo) |
| 67 | { |
| 68 | State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); |
| 69 | EXPECT_TRUE(state->didDecidePolicyForNavigationAction); |
| 70 | EXPECT_TRUE(state->didStartProvisionalNavigation); |
| 71 | |
| 72 | // The provisional URL should be null. |
| 73 | EXPECT_NULL(WKFrameCopyProvisionalURL(WKPageGetMainFrame(page))); |
| 74 | |
| 75 | state->didCommitNavigation = true; |
| 76 | } |
| 77 | |
| 78 | static void didFinishNavigation(WKPageRef page, WKNavigationRef, WKTypeRef userData, const void* clientInfo) |
| 79 | { |
| 80 | State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); |
| 81 | EXPECT_TRUE(state->didDecidePolicyForNavigationAction); |
| 82 | EXPECT_TRUE(state->didStartProvisionalNavigation); |
| 83 | EXPECT_TRUE(state->didCommitNavigation); |
| 84 | |
| 85 | // The provisional URL should be null. |
| 86 | EXPECT_NULL(WKFrameCopyProvisionalURL(WKPageGetMainFrame(page))); |
| 87 | |
| 88 | test1Done = true; |
| 89 | } |
| 90 | |
| 91 | static void decidePolicyForNavigationAction(WKPageRef page, WKNavigationActionRef navigationAction, WKFramePolicyListenerRef listener, WKTypeRef userData, const void* clientInfo) |
| 92 | { |
| 93 | State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); |
| 94 | EXPECT_FALSE(state->didStartProvisionalNavigation); |
| 95 | EXPECT_FALSE(state->didCommitNavigation); |
| 96 | |
| 97 | state->didDecidePolicyForNavigationAction = true; |
| 98 | |
| 99 | WKFramePolicyListenerUse(listener); |
| 100 | } |
| 101 | |
| 102 | static void decidePolicyForResponse(WKPageRef page, WKNavigationResponseRef navigationResponse, WKFramePolicyListenerRef listener, WKTypeRef userData, const void* clientInfo) |
| 103 | { |
| 104 | WKFramePolicyListenerUse(listener); |
| 105 | } |
| 106 | |
| 107 | TEST(WebKit, PageLoadBasic) |
| 108 | { |
| 109 | State state; |
| 110 | |
| 111 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 112 | PlatformWebView webView(context.get()); |
| 113 | |
| 114 | WKPageNavigationClientV0 loaderClient; |
| 115 | memset(&loaderClient, 0, sizeof(loaderClient)); |
| 116 | |
| 117 | loaderClient.base.version = 0; |
| 118 | loaderClient.base.clientInfo = &state; |
| 119 | loaderClient.didStartProvisionalNavigation = didStartProvisionalNavigation; |
| 120 | loaderClient.didCommitNavigation = didCommitNavigation; |
| 121 | loaderClient.didFinishNavigation = didFinishNavigation; |
| 122 | loaderClient.decidePolicyForNavigationAction = decidePolicyForNavigationAction; |
| 123 | loaderClient.decidePolicyForNavigationResponse = decidePolicyForResponse; |
| 124 | |
| 125 | WKPageSetPageNavigationClient(webView.page(), &loaderClient.base); |
| 126 | |
| 127 | // Before loading anything, the active url should be null |
| 128 | EXPECT_NULL(WKPageCopyActiveURL(webView.page())); |
| 129 | |
| 130 | WKRetainPtr<WKURLRef> url = adoptWK(Util::createURLForResource("simple" , "html" )); |
| 131 | WKPageLoadURL(webView.page(), url.get()); |
| 132 | |
| 133 | // But immediately after starting a load, the active url should reflect the request |
| 134 | WKRetainPtr<WKURLRef> activeUrl = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 135 | ASSERT_NOT_NULL(activeUrl.get()); |
| 136 | EXPECT_TRUE(WKURLIsEqual(activeUrl.get(), url.get())); |
| 137 | |
| 138 | Util::run(&test1Done); |
| 139 | } |
| 140 | |
| 141 | TEST(WebKit, PageReload) |
| 142 | { |
| 143 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 144 | PlatformWebView webView(context.get()); |
| 145 | |
| 146 | // Reload test before url loading. |
| 147 | WKPageReload(webView.page()); |
| 148 | WKPageReload(webView.page()); |
| 149 | |
| 150 | WKRetainPtr<WKURLRef> url = adoptWK(Util::createURLForResource("simple" , "html" )); |
| 151 | WKPageLoadURL(webView.page(), url.get()); |
| 152 | |
| 153 | // Reload test after url loading. |
| 154 | WKPageReload(webView.page()); |
| 155 | |
| 156 | WKRetainPtr<WKURLRef> activeUrl = adoptWK(WKPageCopyActiveURL(webView.page())); |
| 157 | ASSERT_NOT_NULL(activeUrl.get()); |
| 158 | EXPECT_TRUE(WKURLIsEqual(activeUrl.get(), url.get())); |
| 159 | } |
| 160 | |
| 161 | TEST(WebKit, PageLoadTwiceAndReload) |
| 162 | { |
| 163 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
| 164 | PlatformWebView webView(context.get()); |
| 165 | |
| 166 | test1Done = false; |
| 167 | static unsigned loadsCount = 0; |
| 168 | |
| 169 | WKPageNavigationClientV0 loaderClient; |
| 170 | memset(&loaderClient, 0, sizeof(loaderClient)); |
| 171 | loaderClient.base.version = 0; |
| 172 | loaderClient.base.clientInfo = nullptr; |
| 173 | loaderClient.didFailProvisionalNavigation = [](WKPageRef page, WKNavigationRef, WKErrorRef error, WKTypeRef userData, const void *clientInfo) { |
| 174 | loadsCount++; |
| 175 | WKPageReload(page); |
| 176 | }; |
| 177 | loaderClient.didFinishNavigation = [](WKPageRef page, WKNavigationRef, WKTypeRef userData, const void* clientInfo) { |
| 178 | if (++loadsCount == 3) { |
| 179 | test1Done = true; |
| 180 | return; |
| 181 | } |
| 182 | WKPageReload(page); |
| 183 | }; |
| 184 | WKPageSetPageNavigationClient(webView.page(), &loaderClient.base); |
| 185 | |
| 186 | WKRetainPtr<WKURLRef> url1 = adoptWK(Util::createURLForResource("simple" , "html" )); |
| 187 | WKRetainPtr<WKURLRef> url2 = adoptWK(Util::createURLForResource("simple2" , "html" )); |
| 188 | WKPageLoadURL(webView.page(), url1.get()); |
| 189 | WKPageLoadURL(webView.page(), url2.get()); |
| 190 | |
| 191 | Util::run(&test1Done); |
| 192 | } |
| 193 | |
| 194 | } // namespace TestWebKitAPI |
| 195 | |
| 196 | #endif |
| 197 | |