1/*
2 * Copyright (C) 2012 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#include <wtf/Vector.h>
35
36namespace TestWebKitAPI {
37
38static bool finished;
39
40static const char* expectedMessages[] = {
41"GlobalObjectIsAvailableForFrame called",
42"GlobalObjectIsAvailableForFrame called",
43"GlobalObjectIsAvailableForFrame called",
44"GlobalObjectIsAvailableForFrame called",
45"Subframe finished loading",
46"Extension states:\nFirst page, main frame, standard world - Connected\nFirst page, main frame, non-standard world - Connected\nFirst page, subframe, standard world - Connected\nFirst page, subframe, non-standard world - Connected\nSecond page, main frame, standard world - Uncreated\nSecond page, main frame, non-standard world - Uncreated",
47"Main frame finished loading",
48"Extension states:\nFirst page, main frame, standard world - Connected\nFirst page, main frame, non-standard world - Connected\nFirst page, subframe, standard world - Connected\nFirst page, subframe, non-standard world - Connected\nSecond page, main frame, standard world - Uncreated\nSecond page, main frame, non-standard world - Uncreated",
49"WillDisconnectDOMWindowExtensionFromGlobalObject called",
50"WillDisconnectDOMWindowExtensionFromGlobalObject called",
51"WillDisconnectDOMWindowExtensionFromGlobalObject called",
52"WillDisconnectDOMWindowExtensionFromGlobalObject called",
53"GlobalObjectIsAvailableForFrame called",
54"GlobalObjectIsAvailableForFrame called",
55"Main frame finished loading",
56"Extension states:\nFirst page, main frame, standard world - Disconnected\nFirst page, main frame, non-standard world - Disconnected\nFirst page, subframe, standard world - Disconnected\nFirst page, subframe, non-standard world - Disconnected\nSecond page, main frame, standard world - Connected\nSecond page, main frame, non-standard world - Connected",
57"WillDisconnectDOMWindowExtensionFromGlobalObject called",
58"WillDisconnectDOMWindowExtensionFromGlobalObject called",
59"DidReconnectDOMWindowExtensionToGlobalObject called",
60"DidReconnectDOMWindowExtensionToGlobalObject called",
61"DidReconnectDOMWindowExtensionToGlobalObject called",
62"DidReconnectDOMWindowExtensionToGlobalObject called",
63"Main frame finished loading",
64"Extension states:\nFirst page, main frame, standard world - Connected\nFirst page, main frame, non-standard world - Connected\nFirst page, subframe, standard world - Connected\nFirst page, subframe, non-standard world - Connected\nSecond page, main frame, standard world - Disconnected\nSecond page, main frame, non-standard world - Disconnected",
65"Extension states:\nFirst page, main frame, standard world - Removed\nFirst page, main frame, non-standard world - Removed\nFirst page, subframe, standard world - Removed\nFirst page, subframe, non-standard world - Removed\nSecond page, main frame, standard world - Removed\nSecond page, main frame, non-standard world - Removed",
66"TestComplete"
67};
68
69static Vector<WKRetainPtr<WKStringRef> > messages;
70
71static void didReceiveMessageFromInjectedBundle(WKContextRef, WKStringRef messageName, WKTypeRef messageBody, const void*)
72{
73 ASSERT_NOT_NULL(messageBody);
74 EXPECT_EQ(WKStringGetTypeID(), WKGetTypeID(messageBody));
75
76 WKStringRef bodyString = (WKStringRef)messageBody;
77 messages.append(bodyString);
78
79 if (WKStringIsEqualToUTF8CString(messageName, "DidFinishLoadForMainFrame") || WKStringIsEqualToUTF8CString(messageName, "TestComplete"))
80 finished = true;
81}
82
83TEST(WebKit, DISABLED_DOMWindowExtensionBasic)
84{
85 WKRetainPtr<WKPageGroupRef> pageGroup = adoptWK(WKPageGroupCreateWithIdentifier(WKStringCreateWithUTF8CString("DOMWindowExtensionBasicPageGroup")));
86
87 WKRetainPtr<WKContextRef> context = adoptWK(Util::createContextForInjectedBundleTest("DOMWindowExtensionBasic", pageGroup.get()));
88
89 WKContextInjectedBundleClientV0 injectedBundleClient;
90 memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
91
92 injectedBundleClient.base.version = 0;
93 injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
94
95 WKContextSetInjectedBundleClient(context.get(), &injectedBundleClient.base);
96
97 // The default cache model has a capacity of 0, so it is necessary to switch to a cache
98 // model that actually allows for a page cache.
99 WKContextSetCacheModel(context.get(), kWKCacheModelDocumentBrowser);
100
101 PlatformWebView webView(context.get(), pageGroup.get());
102
103 // Make sure the extensions for each frame are installed in each world.
104 WKRetainPtr<WKURLRef> url1 = adoptWK(Util::createURLForResource("simple-iframe", "html"));
105 WKPageLoadURL(webView.page(), url1.get());
106
107 Util::run(&finished);
108 finished = false;
109
110 // Make sure those first 4 extensions are disconnected, and 2 new ones are installed.
111 WKRetainPtr<WKURLRef> url2 = adoptWK(Util::createURLForResource("simple", "html"));
112 WKPageLoadURL(webView.page(), url2.get());
113
114 Util::run(&finished);
115 finished = false;
116
117 // Make sure those two are disconnected, and the first four are reconnected.
118 WKPageGoBack(webView.page());
119
120 Util::run(&finished);
121 finished = false;
122
123 // Make sure the 2 disconnected extensions in the page cache and the 4 active extensions are all removed.
124 WKPageClose(webView.page());
125
126 Util::run(&finished);
127
128 const size_t expectedSize = sizeof(expectedMessages) / sizeof(const char*);
129 EXPECT_EQ(expectedSize, messages.size());
130
131 if (messages.size() != expectedSize)
132 return;
133
134 for (size_t i = 0; i < messages.size(); ++i)
135 EXPECT_WK_STREQ(expectedMessages[i], messages[i].get());
136}
137
138TEST(WebKit, DOMWindowExtensionCrashOnReload)
139{
140 WKRetainPtr<WKPageGroupRef> pageGroup = adoptWK(WKPageGroupCreateWithIdentifier(WKStringCreateWithUTF8CString("DOMWindowExtensionBasicPageGroup")));
141
142 WKRetainPtr<WKContextRef> context = adoptWK(Util::createContextForInjectedBundleTest("DOMWindowExtensionBasic", pageGroup.get()));
143
144 WKContextInjectedBundleClientV0 injectedBundleClient;
145 memset(&injectedBundleClient, 0, sizeof(injectedBundleClient));
146
147 injectedBundleClient.base.version = 0;
148 injectedBundleClient.didReceiveMessageFromInjectedBundle = didReceiveMessageFromInjectedBundle;
149
150 WKContextSetInjectedBundleClient(context.get(), &injectedBundleClient.base);
151
152 // The default cache model has a capacity of 0, so it is necessary to switch to a cache
153 // model that actually allows for a page cache.
154 WKContextSetCacheModel(context.get(), kWKCacheModelDocumentBrowser);
155
156 PlatformWebView webView(context.get(), pageGroup.get());
157
158 finished = false;
159
160 // Make sure the extensions for each frame are installed in each world.
161 WKRetainPtr<WKURLRef> url1 = adoptWK(Util::createURLForResource("simple-iframe", "html"));
162 WKPageLoadURL(webView.page(), url1.get());
163
164 Util::run(&finished);
165 finished = false;
166
167 WKPageReload(webView.page());
168 Util::run(&finished);
169}
170
171} // namespace TestWebKitAPI
172
173#endif
174