1 | /* |
2 | * Copyright (C) 2015 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 | |
34 | namespace TestWebKitAPI { |
35 | |
36 | static bool didFinishLoad; |
37 | static WKRetainPtr<WKSessionStateRef> sessionStateWithFirstItemRemoved; |
38 | static WKRetainPtr<WKSessionStateRef> sessionStateWithAllItemsRemoved; |
39 | |
40 | static void didFinishNavigation(WKPageRef, WKNavigationRef, WKTypeRef, const void*) |
41 | { |
42 | didFinishLoad = true; |
43 | } |
44 | |
45 | static void setPageLoaderClient(WKPageRef page) |
46 | { |
47 | WKPageNavigationClientV0 loaderClient; |
48 | memset(&loaderClient, 0, sizeof(loaderClient)); |
49 | |
50 | loaderClient.base.version = 0; |
51 | loaderClient.didFinishNavigation = didFinishNavigation; |
52 | |
53 | WKPageSetPageNavigationClient(page, &loaderClient.base); |
54 | } |
55 | |
56 | static bool filterFirstItemCallback(WKPageRef page, WKStringRef valueType, WKTypeRef value, void* context) |
57 | { |
58 | if (!WKStringIsEqual(valueType, WKPageGetSessionBackForwardListItemValueType())) |
59 | return true; |
60 | |
61 | ASSERT(WKGetTypeID(value) == WKBackForwardListItemGetTypeID()); |
62 | WKBackForwardListItemRef backForwardListItem = static_cast<WKBackForwardListItemRef>(value); |
63 | |
64 | WKRetainPtr<WKURLRef> url = adoptWK(WKBackForwardListItemCopyURL(backForwardListItem)); |
65 | WKRetainPtr<WKStringRef> path = adoptWK(WKURLCopyLastPathComponent(url.get())); |
66 | |
67 | return !WKStringIsEqualToUTF8CString(path.get(), "simple.html" ); |
68 | } |
69 | |
70 | static bool filterAllItemsCallback(WKPageRef page, WKStringRef valueType, WKTypeRef value, void* context) |
71 | { |
72 | return false; |
73 | } |
74 | |
75 | static void createSessionStates(WKContextRef context) |
76 | { |
77 | PlatformWebView webView(context); |
78 | setPageLoaderClient(webView.page()); |
79 | |
80 | WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple" , "html" )).get()); |
81 | Util::run(&didFinishLoad); |
82 | didFinishLoad = false; |
83 | |
84 | WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple2" , "html" )).get()); |
85 | Util::run(&didFinishLoad); |
86 | didFinishLoad = false; |
87 | |
88 | WKPageLoadURL(webView.page(), adoptWK(Util::createURLForResource("simple3" , "html" )).get()); |
89 | Util::run(&didFinishLoad); |
90 | didFinishLoad = false; |
91 | |
92 | WKPageGoBack(webView.page()); |
93 | Util::run(&didFinishLoad); |
94 | didFinishLoad = false; |
95 | |
96 | WKPageGoBack(webView.page()); |
97 | Util::run(&didFinishLoad); |
98 | didFinishLoad = false; |
99 | |
100 | // Should be back on simple.html at this point. |
101 | |
102 | sessionStateWithFirstItemRemoved = adoptWK(static_cast<WKSessionStateRef>(WKPageCopySessionState(webView.page(), reinterpret_cast<void*>(1), filterFirstItemCallback))); |
103 | sessionStateWithAllItemsRemoved = adoptWK(static_cast<WKSessionStateRef>(WKPageCopySessionState(webView.page(), reinterpret_cast<void*>(1), filterAllItemsCallback))); |
104 | } |
105 | |
106 | TEST(WebKit, WKPageCopySessionStateWithFiltering) |
107 | { |
108 | WKRetainPtr<WKContextRef> context = adoptWK(WKContextCreateWithConfiguration(nullptr)); |
109 | |
110 | createSessionStates(context.get()); |
111 | |
112 | EXPECT_NOT_NULL(sessionStateWithFirstItemRemoved); |
113 | PlatformWebView webView1(context.get()); |
114 | setPageLoaderClient(webView1.page()); |
115 | WKPageRestoreFromSessionState(webView1.page(), sessionStateWithFirstItemRemoved.get()); |
116 | Util::run(&didFinishLoad); |
117 | didFinishLoad = false; |
118 | |
119 | WKBackForwardListRef backForwardList1 = WKPageGetBackForwardList(webView1.page()); |
120 | EXPECT_EQ(0u, WKBackForwardListGetBackListCount(backForwardList1)); |
121 | EXPECT_EQ(1u, WKBackForwardListGetForwardListCount(backForwardList1)); |
122 | |
123 | EXPECT_NOT_NULL(sessionStateWithAllItemsRemoved); |
124 | PlatformWebView webView2(context.get()); |
125 | setPageLoaderClient(webView2.page()); |
126 | WKPageRestoreFromSessionState(webView2.page(), sessionStateWithAllItemsRemoved.get()); |
127 | // Because the session state ends up being empty, nothing is actually loaded. |
128 | |
129 | WKBackForwardListRef backForwardList2 = WKPageGetBackForwardList(webView2.page()); |
130 | EXPECT_EQ(0u, WKBackForwardListGetBackListCount(backForwardList2)); |
131 | EXPECT_EQ(0u, WKBackForwardListGetForwardListCount(backForwardList2)); |
132 | } |
133 | |
134 | } // namespace TestWebKitAPI |
135 | |
136 | #endif |
137 | |