15f63312ace618cdf6dd023fcefc4b15da8052c6
[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 "URLParser.h"
43 #include "WheelEventTestTrigger.h"
44 #include <JavaScriptCore/APICast.h>
45 #include <JavaScriptCore/JSValueRef.h>
46 #include <interpreter/CallFrame.h>
47 #include <runtime/IdentifierInlines.h>
48
49 using namespace WebCore;
50
51 namespace WebCoreTestSupport {
52 using namespace JSC;
53
54 void injectInternalsObject(JSContextRef context)
55 {
56     ExecState* exec = toJS(context);
57     JSLockHolder lock(exec);
58     JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
59     ScriptExecutionContext* scriptContext = globalObject->scriptExecutionContext();
60     if (is<Document>(*scriptContext))
61         globalObject->putDirect(exec->vm(), Identifier::fromString(exec, Internals::internalsId), toJS(exec, globalObject, Internals::create(downcast<Document>(*scriptContext))));
62 }
63
64 void resetInternalsObject(JSContextRef context)
65 {
66     ExecState* exec = toJS(context);
67     JSLockHolder lock(exec);
68     JSDOMGlobalObject* globalObject = jsCast<JSDOMGlobalObject*>(exec->lexicalGlobalObject());
69     ScriptExecutionContext* scriptContext = globalObject->scriptExecutionContext();
70     Page* page = downcast<Document>(scriptContext)->frame()->page();
71     Internals::resetToConsistentState(*page);
72     InternalSettings::from(page)->resetToConsistentState();
73 }
74
75 void monitorWheelEvents(WebCore::Frame& frame)
76 {
77     Page* page = frame.page();
78     if (!page)
79         return;
80
81     page->ensureTestTrigger();
82 }
83
84 void setTestCallbackAndStartNotificationTimer(WebCore::Frame& frame, JSContextRef context, JSObjectRef jsCallbackFunction)
85 {
86     Page* page = frame.page();
87     if (!page || !page->expectsWheelEventTriggers())
88         return;
89
90     JSValueProtect(context, jsCallbackFunction);
91     
92     page->ensureTestTrigger().setTestCallbackAndStartNotificationTimer([=](void) {
93         JSObjectCallAsFunction(context, jsCallbackFunction, nullptr, 0, nullptr, nullptr);
94         JSValueUnprotect(context, jsCallbackFunction);
95     });
96 }
97
98 void clearWheelEventTestTrigger(WebCore::Frame& frame)
99 {
100     Page* page = frame.page();
101     if (!page)
102         return;
103     
104     page->clearTrigger();
105 }
106
107 void setLogChannelToAccumulate(const String& name)
108 {
109 #if !LOG_DISABLED
110     WebCore::setLogChannelToAccumulate(name);
111 #else
112     UNUSED_PARAM(name);
113 #endif
114 }
115
116 void initializeLogChannelsIfNecessary()
117 {
118 #if !LOG_DISABLED || !RELEASE_LOG_DISABLED
119     WebCore::initializeLogChannelsIfNecessary();
120 #endif
121 }
122
123 void setAllowsAnySSLCertificate(bool allowAnySSLCertificate)
124 {
125     InternalSettings::setAllowsAnySSLCertificate(allowAnySSLCertificate);
126 }
127
128 void installMockGamepadProvider()
129 {
130 #if ENABLE(GAMEPAD)
131     GamepadProvider::setSharedProvider(MockGamepadProvider::singleton());
132 #endif
133 }
134
135 void connectMockGamepad(unsigned gamepadIndex)
136 {
137 #if ENABLE(GAMEPAD)
138     MockGamepadProvider::singleton().connectMockGamepad(gamepadIndex);
139 #else
140     UNUSED_PARAM(gamepadIndex);
141 #endif
142 }
143
144 void disconnectMockGamepad(unsigned gamepadIndex)
145 {
146 #if ENABLE(GAMEPAD)
147     MockGamepadProvider::singleton().disconnectMockGamepad(gamepadIndex);
148 #else
149     UNUSED_PARAM(gamepadIndex);
150 #endif
151 }
152
153 void setMockGamepadDetails(unsigned gamepadIndex, const WTF::String& gamepadID, unsigned axisCount, unsigned buttonCount)
154 {
155 #if ENABLE(GAMEPAD)
156     MockGamepadProvider::singleton().setMockGamepadDetails(gamepadIndex, gamepadID, axisCount, buttonCount);
157 #else
158     UNUSED_PARAM(gamepadIndex);
159     UNUSED_PARAM(gamepadID);
160     UNUSED_PARAM(axisCount);
161     UNUSED_PARAM(buttonCount);
162 #endif
163 }
164
165 void setMockGamepadAxisValue(unsigned gamepadIndex, unsigned axisIndex, double axisValue)
166 {
167 #if ENABLE(GAMEPAD)
168     MockGamepadProvider::singleton().setMockGamepadAxisValue(gamepadIndex, axisIndex, axisValue);
169 #else
170     UNUSED_PARAM(gamepadIndex);
171     UNUSED_PARAM(axisIndex);
172     UNUSED_PARAM(axisValue);
173 #endif
174 }
175
176 void setMockGamepadButtonValue(unsigned gamepadIndex, unsigned buttonIndex, double buttonValue)
177 {
178 #if ENABLE(GAMEPAD)
179     MockGamepadProvider::singleton().setMockGamepadButtonValue(gamepadIndex, buttonIndex, buttonValue);
180 #else
181     UNUSED_PARAM(gamepadIndex);
182     UNUSED_PARAM(buttonIndex);
183     UNUSED_PARAM(buttonValue);
184 #endif
185 }
186
187 void setupNewlyCreatedServiceWorker(uint64_t serviceWorkerIdentifier)
188 {
189 #if ENABLE(SERVICE_WORKER)
190     auto identifier = makeObjectIdentifier<ServiceWorkerIdentifierType>(serviceWorkerIdentifier);
191     SWContextManager::singleton().postTaskToServiceWorker(identifier, [identifier] (ServiceWorkerGlobalScope& globalScope) {
192         auto* script = globalScope.script();
193         if (!script)
194             return;
195
196         auto& state = *globalScope.execState();
197         JSLockHolder locker(state.vm());
198         auto* contextWrapper = script->workerGlobalScopeWrapper();
199         contextWrapper->putDirect(state.vm(), Identifier::fromString(&state, Internals::internalsId), toJS(&state, contextWrapper, ServiceWorkerInternals::create(identifier)));
200     });
201 #endif
202 }
203
204 }