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 "Test.h" 27 28 #include "PlatformUtilities.h" 29 #include "PlatformWebView.h" 30 #include <WebKit2/WebKit2.h> 31 #include <WebKit2/WKRetainPtr.h> 32 33 namespace TestWebKitAPI { 34 35 static bool test1Done; 36 37 struct State { 38 State() 39 : didDecidePolicyForNavigationAction(false) 40 , didStartProvisionalLoadForFrame(false) 41 , didCommitLoadForFrame(false) 42 { 43 } 44 45 bool didDecidePolicyForNavigationAction; 46 bool didStartProvisionalLoadForFrame; 47 bool didCommitLoadForFrame; 48 }; 49 50 static void didStartProvisionalLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo) 51 { 52 State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); 53 TEST_ASSERT(state->didDecidePolicyForNavigationAction); 54 TEST_ASSERT(!state->didCommitLoadForFrame); 55 56 // The commited URL should be null. 57 TEST_ASSERT(!WKFrameCopyURL(frame)); 58 59 TEST_ASSERT(!state->didStartProvisionalLoadForFrame); 60 61 62 state->didStartProvisionalLoadForFrame = true; 63 } 64 65 static void didCommitLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo) 66 { 67 State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); 68 TEST_ASSERT(state->didDecidePolicyForNavigationAction); 69 TEST_ASSERT(state->didStartProvisionalLoadForFrame); 70 71 // The provisional URL should be null. 72 TEST_ASSERT(!WKFrameCopyProvisionalURL(frame)); 73 74 state->didCommitLoadForFrame = true; 75 } 76 77 static void didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef userData, const void* clientInfo) 78 { 79 State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); 80 TEST_ASSERT(state->didDecidePolicyForNavigationAction); 81 TEST_ASSERT(state->didStartProvisionalLoadForFrame); 82 TEST_ASSERT(state->didCommitLoadForFrame); 83 84 // The provisional URL should be null. 85 TEST_ASSERT(!WKFrameCopyProvisionalURL(frame)); 86 87 test1Done = true; 88 } 89 90 static void decidePolicyForNavigationAction(WKPageRef page, WKFrameRef frame, WKFrameNavigationType navigationType, WKEventModifiers modifiers, WKEventMouseButton mouseButton, WKURLRequestRef request, WKFramePolicyListenerRef listener, WKTypeRef userData, const void* clientInfo) 91 { 92 State* state = reinterpret_cast<State*>(const_cast<void*>(clientInfo)); 93 TEST_ASSERT(!state->didStartProvisionalLoadForFrame); 94 TEST_ASSERT(!state->didCommitLoadForFrame); 95 96 state->didDecidePolicyForNavigationAction = true; 97 98 WKFramePolicyListenerUse(listener); 99 } 100 101 static void decidePolicyForNewWindowAction(WKPageRef page, WKFrameRef frame, WKFrameNavigationType navigationType, WKEventModifiers modifiers, WKEventMouseButton mouseButton, WKURLRequestRef request, WKStringRef frameName, WKFramePolicyListenerRef listener, WKTypeRef userData, const void* clientInfo) 102 { 103 WKFramePolicyListenerUse(listener); 104 } 105 106 static void decidePolicyForResponse(WKPageRef page, WKFrameRef frame, WKURLResponseRef response, WKURLRequestRef request, WKFramePolicyListenerRef listener, WKTypeRef userData, const void* clientInfo) 107 { 108 WKFramePolicyListenerUse(listener); 109 } 110 111 TEST(WebKit2, PageLoadBasic) 112 { 113 State state; 114 115 WKRetainPtr<WKContextRef> context(AdoptWK, WKContextCreate()); 116 PlatformWebView webView(context.get()); 117 118 WKPageLoaderClient loaderClient; 119 memset(&loaderClient, 0, sizeof(loaderClient)); 120 121 loaderClient.version = 0; 122 loaderClient.clientInfo = &state; 123 loaderClient.didStartProvisionalLoadForFrame = didStartProvisionalLoadForFrame; 124 loaderClient.didCommitLoadForFrame = didCommitLoadForFrame; 125 loaderClient.didFinishLoadForFrame = didFinishLoadForFrame; 126 WKPageSetPageLoaderClient(webView.page(), &loaderClient); 127 128 WKPagePolicyClient policyClient; 129 memset(&policyClient, 0, sizeof(policyClient)); 130 131 policyClient.version = 0; 132 policyClient.clientInfo = &state; 133 policyClient.decidePolicyForNavigationAction = decidePolicyForNavigationAction; 134 policyClient.decidePolicyForNewWindowAction = decidePolicyForNewWindowAction; 135 policyClient.decidePolicyForResponse = decidePolicyForResponse; 136 WKPageSetPagePolicyClient(webView.page(), &policyClient); 137 138 WKRetainPtr<WKURLRef> url(AdoptWK, Util::createURLForResource("simple", "html")); 139 WKPageLoadURL(webView.page(), url.get()); 140 141 Util::run(&test1Done); 142 } 143 144 } // namespace TestWebKitAPI 145