Move URL from WebCore to WTF
[WebKit-https.git] / Source / WebCore / testing / js / WebCoreTestSupport.cpp
1 /*
2  * Copyright (C) 2011, 2015 Google Inc. All rights reserved.
3  * Copyright (C) 2016 Apple Inc. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "WebCoreTestSupport.h"
29
30 #include "Frame.h"
31 #include "InternalSettings.h"
32 #include "Internals.h"
33 #include "JSDocument.h"
34 #include "JSInternals.h"
35 #include "JSServiceWorkerInternals.h"
36 #include "JSWorkerGlobalScope.h"
37 #include "LogInitialization.h"
38 #include "MockGamepadProvider.h"
39 #include "Page.h"
40 #include "SWContextManager.h"
41 #include "ServiceWorkerGlobalScope.h"
42 #include "WheelEventTestTrigger.h"
43 #include <JavaScriptCore/APICast.h>
44 #include <JavaScriptCore/CallFrame.h>
45 #include <JavaScriptCore/IdentifierInlines.h>
46 #include <JavaScriptCore/JSValueRef.h>
47 #include <wtf/URLParser.h>
48
49 namespace WebCoreTestSupport {
50 using namespace JSC;
51 using namespace WebCore;
52
53 void injectInternalsObject(JSContextRef context)
54 {
55     ExecState* exec = toJS(context);
56     JSLockHolder lock(exec);
57     JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
58     ScriptExecutionContext* scriptContext = globalObject->scriptExecutionContext();
59     if (is<Document>(*scriptContext)) {
60         VM& vm = exec->vm();
61         globalObject->putDirect(vm, Identifier::fromString(&vm, Internals::internalsId), toJS(exec, globalObject, Internals::create(downcast<Document>(*scriptContext))));
62         globalObject->exposeDollarVM(vm);
63     }
64 }
65
66 void resetInternalsObject(JSContextRef context)
67 {
68     ExecState* exec = toJS(context);
69     JSLockHolder lock(exec);
70     JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
71     ScriptExecutionContext* scriptContext = globalObject->scriptExecutionContext();
72     Page* page = downcast<Document>(scriptContext)->frame()->page();
73     Internals::resetToConsistentState(*page);
74     InternalSettings::from(page)->resetToConsistentState();
75 }
76
77 void monitorWheelEvents(WebCore::Frame& frame)
78 {
79     Page* page = frame.page();
80     if (!page)
81         return;
82
83     page->ensureTestTrigger();
84 }
85
86 void setTestCallbackAndStartNotificationTimer(WebCore::Frame& frame, JSContextRef context, JSObjectRef jsCallbackFunction)
87 {
88     Page* page = frame.page();
89     if (!page || !page->expectsWheelEventTriggers())
90         return;
91
92     JSValueProtect(context, jsCallbackFunction);
93     
94     page->ensureTestTrigger().setTestCallbackAndStartNotificationTimer([=](void) {
95         JSObjectCallAsFunction(context, jsCallbackFunction, nullptr, 0, nullptr, nullptr);
96         JSValueUnprotect(context, jsCallbackFunction);
97     });
98 }
99
100 void clearWheelEventTestTrigger(WebCore::Frame& frame)
101 {
102     Page* page = frame.page();
103     if (!page)
104         return;
105     
106     page->clearTrigger();
107 }
108
109 void setLogChannelToAccumulate(const String& name)
110 {
111 #if !LOG_DISABLED
112     WebCore::setLogChannelToAccumulate(name);
113 #else
114     UNUSED_PARAM(name);
115 #endif
116 }
117
118 void initializeLogChannelsIfNecessary()
119 {
120 #if !LOG_DISABLED || !RELEASE_LOG_DISABLED
121     WebCore::initializeLogChannelsIfNecessary();
122 #endif
123 }
124
125 void setAllowsAnySSLCertificate(bool allowAnySSLCertificate)
126 {
127     InternalSettings::setAllowsAnySSLCertificate(allowAnySSLCertificate);
128 }
129
130 void installMockGamepadProvider()
131 {
132 #if ENABLE(GAMEPAD)
133     GamepadProvider::setSharedProvider(MockGamepadProvider::singleton());
134 #endif
135 }
136
137 void connectMockGamepad(unsigned gamepadIndex)
138 {
139 #if ENABLE(GAMEPAD)
140     MockGamepadProvider::singleton().connectMockGamepad(gamepadIndex);
141 #else
142     UNUSED_PARAM(gamepadIndex);
143 #endif
144 }
145
146 void disconnectMockGamepad(unsigned gamepadIndex)
147 {
148 #if ENABLE(GAMEPAD)
149     MockGamepadProvider::singleton().disconnectMockGamepad(gamepadIndex);
150 #else
151     UNUSED_PARAM(gamepadIndex);
152 #endif
153 }
154
155 void setMockGamepadDetails(unsigned gamepadIndex, const WTF::String& gamepadID, unsigned axisCount, unsigned buttonCount)
156 {
157 #if ENABLE(GAMEPAD)
158     MockGamepadProvider::singleton().setMockGamepadDetails(gamepadIndex, gamepadID, axisCount, buttonCount);
159 #else
160     UNUSED_PARAM(gamepadIndex);
161     UNUSED_PARAM(gamepadID);
162     UNUSED_PARAM(axisCount);
163     UNUSED_PARAM(buttonCount);
164 #endif
165 }
166
167 void setMockGamepadAxisValue(unsigned gamepadIndex, unsigned axisIndex, double axisValue)
168 {
169 #if ENABLE(GAMEPAD)
170     MockGamepadProvider::singleton().setMockGamepadAxisValue(gamepadIndex, axisIndex, axisValue);
171 #else
172     UNUSED_PARAM(gamepadIndex);
173     UNUSED_PARAM(axisIndex);
174     UNUSED_PARAM(axisValue);
175 #endif
176 }
177
178 void setMockGamepadButtonValue(unsigned gamepadIndex, unsigned buttonIndex, double buttonValue)
179 {
180 #if ENABLE(GAMEPAD)
181     MockGamepadProvider::singleton().setMockGamepadButtonValue(gamepadIndex, buttonIndex, buttonValue);
182 #else
183     UNUSED_PARAM(gamepadIndex);
184     UNUSED_PARAM(buttonIndex);
185     UNUSED_PARAM(buttonValue);
186 #endif
187 }
188
189 void setupNewlyCreatedServiceWorker(uint64_t serviceWorkerIdentifier)
190 {
191 #if ENABLE(SERVICE_WORKER)
192     auto identifier = makeObjectIdentifier<ServiceWorkerIdentifierType>(serviceWorkerIdentifier);
193     SWContextManager::singleton().postTaskToServiceWorker(identifier, [identifier] (ServiceWorkerGlobalScope& globalScope) {
194         auto* script = globalScope.script();
195         if (!script)
196             return;
197
198         auto& state = *globalScope.execState();
199         JSLockHolder locker(state.vm());
200         auto* contextWrapper = script->workerGlobalScopeWrapper();
201         contextWrapper->putDirect(state.vm(), Identifier::fromString(&state, Internals::internalsId), toJS(&state, contextWrapper, ServiceWorkerInternals::create(identifier)));
202     });
203 #else
204     UNUSED_PARAM(serviceWorkerIdentifier);
205 #endif
206 }
207
208 }