REGRESSION (r201263): Some tests have become flaky timeouts.
[WebKit-https.git] / Tools / WebKitTestRunner / TestInvocation.cpp
1 /*
2  * Copyright (C) 2010-2016 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Intel Corporation. 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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "TestInvocation.h"
29
30 #include "PlatformWebView.h"
31 #include "StringFunctions.h"
32 #include "TestController.h"
33 #include "UIScriptController.h"
34 #include <WebKit/WKContextPrivate.h>
35 #include <WebKit/WKCookieManager.h>
36 #include <WebKit/WKData.h>
37 #include <WebKit/WKDictionary.h>
38 #include <WebKit/WKInspector.h>
39 #include <WebKit/WKPagePrivate.h>
40 #include <WebKit/WKRetainPtr.h>
41 #include <WebKit/WKWebsiteDataStoreRef.h>
42 #include <climits>
43 #include <cstdio>
44 #include <wtf/StdLibExtras.h>
45 #include <wtf/text/CString.h>
46
47 #if PLATFORM(MAC) && !PLATFORM(IOS)
48 #include <Carbon/Carbon.h>
49 #endif
50
51 #if PLATFORM(COCOA)
52 #include <WebKit/WKPagePrivateMac.h>
53 #endif
54
55 using namespace JSC;
56 using namespace WebKit;
57 using namespace std;
58
59 namespace WTR {
60
61 TestInvocation::TestInvocation(WKURLRef url, const TestOptions& options)
62     : m_options(options)
63     , m_url(url)
64 {
65     WKRetainPtr<WKStringRef> urlString = adoptWK(WKURLCopyString(m_url.get()));
66
67     size_t stringLength = WKStringGetLength(urlString.get());
68
69     Vector<char> urlVector;
70     urlVector.resize(stringLength + 1);
71
72     WKStringGetUTF8CString(urlString.get(), urlVector.data(), stringLength + 1);
73
74     m_urlString = String(urlVector.data(), stringLength);
75 }
76
77 TestInvocation::~TestInvocation()
78 {
79     if (m_pendingUIScriptInvocationData)
80         m_pendingUIScriptInvocationData->testInvocation = nullptr;
81 }
82
83 WKURLRef TestInvocation::url() const
84 {
85     return m_url.get();
86 }
87
88 bool TestInvocation::urlContains(const char* searchString) const
89 {
90     return m_urlString.contains(searchString, false);
91 }
92
93 void TestInvocation::setIsPixelTest(const std::string& expectedPixelHash)
94 {
95     m_dumpPixels = true;
96     m_expectedPixelHash = expectedPixelHash;
97 }
98
99 bool TestInvocation::shouldLogFrameLoadDelegates() const
100 {
101     return urlContains("loading/");
102 }
103
104 bool TestInvocation::shouldLogHistoryClientCallbacks() const
105 {
106     return urlContains("globalhistory/");
107 }
108
109 void TestInvocation::invoke()
110 {
111     TestController::singleton().configureViewForTest(*this);
112
113     WKPageSetAddsVisitedLinks(TestController::singleton().mainWebView()->page(), false);
114
115     m_textOutput.clear();
116
117     TestController::singleton().setShouldLogHistoryClientCallbacks(shouldLogHistoryClientCallbacks());
118
119     WKCookieManagerSetHTTPCookieAcceptPolicy(WKContextGetCookieManager(TestController::singleton().context()), kWKHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain);
120
121     // FIXME: We should clear out visited links here.
122
123     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("BeginTest"));
124     WKRetainPtr<WKMutableDictionaryRef> beginTestMessageBody = adoptWK(WKMutableDictionaryCreate());
125
126     WKRetainPtr<WKStringRef> dumpFrameLoadDelegatesKey = adoptWK(WKStringCreateWithUTF8CString("DumpFrameLoadDelegates"));
127     WKRetainPtr<WKBooleanRef> dumpFrameLoadDelegatesValue = adoptWK(WKBooleanCreate(shouldLogFrameLoadDelegates()));
128     WKDictionarySetItem(beginTestMessageBody.get(), dumpFrameLoadDelegatesKey.get(), dumpFrameLoadDelegatesValue.get());
129
130     WKRetainPtr<WKStringRef> useFlexibleViewportKey = adoptWK(WKStringCreateWithUTF8CString("UseFlexibleViewport"));
131     WKRetainPtr<WKBooleanRef> useFlexibleViewportValue = adoptWK(WKBooleanCreate(options().useFlexibleViewport));
132     WKDictionarySetItem(beginTestMessageBody.get(), useFlexibleViewportKey.get(), useFlexibleViewportValue.get());
133
134     WKRetainPtr<WKStringRef> dumpPixelsKey = adoptWK(WKStringCreateWithUTF8CString("DumpPixels"));
135     WKRetainPtr<WKBooleanRef> dumpPixelsValue = adoptWK(WKBooleanCreate(m_dumpPixels));
136     WKDictionarySetItem(beginTestMessageBody.get(), dumpPixelsKey.get(), dumpPixelsValue.get());
137
138     WKRetainPtr<WKStringRef> useWaitToDumpWatchdogTimerKey = adoptWK(WKStringCreateWithUTF8CString("UseWaitToDumpWatchdogTimer"));
139     WKRetainPtr<WKBooleanRef> useWaitToDumpWatchdogTimerValue = adoptWK(WKBooleanCreate(TestController::singleton().useWaitToDumpWatchdogTimer()));
140     WKDictionarySetItem(beginTestMessageBody.get(), useWaitToDumpWatchdogTimerKey.get(), useWaitToDumpWatchdogTimerValue.get());
141
142     WKRetainPtr<WKStringRef> timeoutKey = adoptWK(WKStringCreateWithUTF8CString("Timeout"));
143     WKRetainPtr<WKUInt64Ref> timeoutValue = adoptWK(WKUInt64Create(m_timeout));
144     WKDictionarySetItem(beginTestMessageBody.get(), timeoutKey.get(), timeoutValue.get());
145
146     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), beginTestMessageBody.get());
147
148     bool shouldOpenExternalURLs = false;
149
150     TestController::singleton().runUntil(m_gotInitialResponse, TestController::shortTimeout);
151     if (!m_gotInitialResponse) {
152         m_errorMessage = "Timed out waiting for initial response from web process\n";
153         m_webProcessIsUnresponsive = true;
154         goto end;
155     }
156     if (m_error)
157         goto end;
158
159     WKPageLoadURLWithShouldOpenExternalURLsPolicy(TestController::singleton().mainWebView()->page(), m_url.get(), shouldOpenExternalURLs);
160
161     TestController::singleton().runUntil(m_gotFinalMessage, TestController::noTimeout);
162     if (m_error)
163         goto end;
164
165     dumpResults();
166
167 end:
168 #if !PLATFORM(IOS)
169     if (m_gotInitialResponse)
170         WKInspectorClose(WKPageGetInspector(TestController::singleton().mainWebView()->page()));
171 #endif // !PLATFORM(IOS)
172
173     if (m_webProcessIsUnresponsive)
174         dumpWebProcessUnresponsiveness();
175     else if (TestController::singleton().resetStateToConsistentValues(m_options))
176         return;
177
178     // The process is unresponsive, so let's start a new one.
179     TestController::singleton().terminateWebContentProcess();
180     // Make sure that we have a process, as invoke() will need one to send bundle messages for the next test.
181     TestController::singleton().reattachPageToWebProcess();
182 }
183
184 void TestInvocation::dumpWebProcessUnresponsiveness()
185 {
186     dumpWebProcessUnresponsiveness(m_errorMessage.c_str());
187 }
188
189 void TestInvocation::dumpWebProcessUnresponsiveness(const char* errorMessage)
190 {
191     fprintf(stderr, "%s", errorMessage);
192     char errorMessageToStderr[1024];
193 #if PLATFORM(COCOA)
194     pid_t pid = WKPageGetProcessIdentifier(TestController::singleton().mainWebView()->page());
195     sprintf(errorMessageToStderr, "#PROCESS UNRESPONSIVE - %s (pid %ld)\n", TestController::webProcessName(), static_cast<long>(pid));
196 #else
197     sprintf(errorMessageToStderr, "#PROCESS UNRESPONSIVE - %s", TestController::webProcessName());
198 #endif
199
200     dump(errorMessage, errorMessageToStderr, true);
201 }
202
203 void TestInvocation::dump(const char* textToStdout, const char* textToStderr, bool seenError)
204 {
205     printf("Content-Type: text/plain\n");
206     if (textToStdout)
207         fputs(textToStdout, stdout);
208     if (textToStderr)
209         fputs(textToStderr, stderr);
210
211     fputs("#EOF\n", stdout);
212     fputs("#EOF\n", stderr);
213     if (seenError)
214         fputs("#EOF\n", stdout);
215     fflush(stdout);
216     fflush(stderr);
217 }
218
219 void TestInvocation::forceRepaintDoneCallback(WKErrorRef error, void* context)
220 {
221     // The context may not be valid any more, e.g. if WebKit is invalidating callbacks at process exit.
222     if (error)
223         return;
224
225     TestInvocation* testInvocation = static_cast<TestInvocation*>(context);
226     RELEASE_ASSERT(TestController::singleton().isCurrentInvocation(testInvocation));
227
228     testInvocation->m_gotRepaint = true;
229     TestController::singleton().notifyDone();
230 }
231
232 void TestInvocation::dumpResults()
233 {
234     if (m_textOutput.length() || !m_audioResult)
235         dump(m_textOutput.toString().utf8().data());
236     else
237         dumpAudio(m_audioResult.get());
238
239     if (m_dumpPixels) {
240         if (m_pixelResult)
241             dumpPixelsAndCompareWithExpected(m_pixelResult.get(), m_repaintRects.get(), TestInvocation::SnapshotResultType::WebContents);
242         else if (m_pixelResultIsPending) {
243             m_gotRepaint = false;
244             WKPageForceRepaint(TestController::singleton().mainWebView()->page(), this, TestInvocation::forceRepaintDoneCallback);
245             TestController::singleton().runUntil(m_gotRepaint, TestController::shortTimeout);
246             if (!m_gotRepaint) {
247                 m_errorMessage = "Timed out waiting for pre-pixel dump repaint\n";
248                 m_webProcessIsUnresponsive = true;
249                 return;
250             }
251             WKRetainPtr<WKImageRef> windowSnapshot = TestController::singleton().mainWebView()->windowSnapshotImage();
252             ASSERT(windowSnapshot);
253             dumpPixelsAndCompareWithExpected(windowSnapshot.get(), m_repaintRects.get(), TestInvocation::SnapshotResultType::WebView);
254         }
255     }
256
257     fputs("#EOF\n", stdout);
258     fflush(stdout);
259     fflush(stderr);
260 }
261
262 void TestInvocation::dumpAudio(WKDataRef audioData)
263 {
264     size_t length = WKDataGetSize(audioData);
265     if (!length)
266         return;
267
268     const unsigned char* data = WKDataGetBytes(audioData);
269
270     printf("Content-Type: audio/wav\n");
271     printf("Content-Length: %lu\n", static_cast<unsigned long>(length));
272
273     fwrite(data, 1, length, stdout);
274     printf("#EOF\n");
275     fprintf(stderr, "#EOF\n");
276 }
277
278 bool TestInvocation::compareActualHashToExpectedAndDumpResults(const char actualHash[33])
279 {
280     // Compute the hash of the bitmap context pixels
281     fprintf(stdout, "\nActualHash: %s\n", actualHash);
282
283     if (!m_expectedPixelHash.length())
284         return false;
285
286     ASSERT(m_expectedPixelHash.length() == 32);
287     fprintf(stdout, "\nExpectedHash: %s\n", m_expectedPixelHash.c_str());
288
289     // FIXME: Do case insensitive compare.
290     return m_expectedPixelHash == actualHash;
291 }
292
293 void TestInvocation::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
294 {
295     if (WKStringIsEqualToUTF8CString(messageName, "Error")) {
296         // Set all states to true to stop spinning the runloop.
297         m_gotInitialResponse = true;
298         m_gotFinalMessage = true;
299         m_error = true;
300         m_errorMessage = "FAIL\n";
301         TestController::singleton().notifyDone();
302         return;
303     }
304
305     if (WKStringIsEqualToUTF8CString(messageName, "Ack")) {
306         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
307         WKStringRef messageBodyString = static_cast<WKStringRef>(messageBody);
308         if (WKStringIsEqualToUTF8CString(messageBodyString, "BeginTest")) {
309             m_gotInitialResponse = true;
310             TestController::singleton().notifyDone();
311             return;
312         }
313
314         ASSERT_NOT_REACHED();
315     }
316
317     if (WKStringIsEqualToUTF8CString(messageName, "Done")) {
318         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
319         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
320
321         WKRetainPtr<WKStringRef> pixelResultIsPendingKey = adoptWK(WKStringCreateWithUTF8CString("PixelResultIsPending"));
322         WKBooleanRef pixelResultIsPending = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, pixelResultIsPendingKey.get()));
323         m_pixelResultIsPending = WKBooleanGetValue(pixelResultIsPending);
324
325         if (!m_pixelResultIsPending) {
326             WKRetainPtr<WKStringRef> pixelResultKey = adoptWK(WKStringCreateWithUTF8CString("PixelResult"));
327             m_pixelResult = static_cast<WKImageRef>(WKDictionaryGetItemForKey(messageBodyDictionary, pixelResultKey.get()));
328             ASSERT(!m_pixelResult || m_dumpPixels);
329         }
330
331         WKRetainPtr<WKStringRef> repaintRectsKey = adoptWK(WKStringCreateWithUTF8CString("RepaintRects"));
332         m_repaintRects = static_cast<WKArrayRef>(WKDictionaryGetItemForKey(messageBodyDictionary, repaintRectsKey.get()));
333
334         WKRetainPtr<WKStringRef> audioResultKey =  adoptWK(WKStringCreateWithUTF8CString("AudioResult"));
335         m_audioResult = static_cast<WKDataRef>(WKDictionaryGetItemForKey(messageBodyDictionary, audioResultKey.get()));
336
337         m_gotFinalMessage = true;
338         TestController::singleton().notifyDone();
339         return;
340     }
341
342     if (WKStringIsEqualToUTF8CString(messageName, "TextOutput")) {
343         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
344         WKStringRef textOutput = static_cast<WKStringRef>(messageBody);
345         m_textOutput.append(toWTFString(textOutput));
346         return;
347     }
348
349     if (WKStringIsEqualToUTF8CString(messageName, "BeforeUnloadReturnValue")) {
350         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
351         WKBooleanRef beforeUnloadReturnValue = static_cast<WKBooleanRef>(messageBody);
352         TestController::singleton().setBeforeUnloadReturnValue(WKBooleanGetValue(beforeUnloadReturnValue));
353         return;
354     }
355     
356     if (WKStringIsEqualToUTF8CString(messageName, "AddChromeInputField")) {
357         TestController::singleton().mainWebView()->addChromeInputField();
358         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallAddChromeInputFieldCallback"));
359         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
360         return;
361     }
362
363     if (WKStringIsEqualToUTF8CString(messageName, "RemoveChromeInputField")) {
364         TestController::singleton().mainWebView()->removeChromeInputField();
365         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallRemoveChromeInputFieldCallback"));
366         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
367         return;
368     }
369     
370     if (WKStringIsEqualToUTF8CString(messageName, "FocusWebView")) {
371         TestController::singleton().mainWebView()->makeWebViewFirstResponder();
372         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallFocusWebViewCallback"));
373         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
374         return;
375     }
376
377     if (WKStringIsEqualToUTF8CString(messageName, "SetBackingScaleFactor")) {
378         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
379         double backingScaleFactor = WKDoubleGetValue(static_cast<WKDoubleRef>(messageBody));
380         WKPageSetCustomBackingScaleFactor(TestController::singleton().mainWebView()->page(), backingScaleFactor);
381
382         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallSetBackingScaleFactorCallback"));
383         WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
384         return;
385     }
386
387     if (WKStringIsEqualToUTF8CString(messageName, "SimulateWebNotificationClick")) {
388         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
389         uint64_t notificationID = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
390         TestController::singleton().simulateWebNotificationClick(notificationID);
391         return;
392     }
393
394     if (WKStringIsEqualToUTF8CString(messageName, "SetAddsVisitedLinks")) {
395         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
396         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
397         WKPageSetAddsVisitedLinks(TestController::singleton().mainWebView()->page(), WKBooleanGetValue(enabledWK));
398         return;
399     }
400
401     if (WKStringIsEqualToUTF8CString(messageName, "SetGeolocationPermission")) {
402         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
403         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
404         TestController::singleton().setGeolocationPermission(WKBooleanGetValue(enabledWK));
405         return;
406     }
407
408     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGeolocationPosition")) {
409         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
410         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
411
412         WKRetainPtr<WKStringRef> latitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("latitude"));
413         WKDoubleRef latitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, latitudeKeyWK.get()));
414         double latitude = WKDoubleGetValue(latitudeWK);
415
416         WKRetainPtr<WKStringRef> longitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("longitude"));
417         WKDoubleRef longitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, longitudeKeyWK.get()));
418         double longitude = WKDoubleGetValue(longitudeWK);
419
420         WKRetainPtr<WKStringRef> accuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("accuracy"));
421         WKDoubleRef accuracyWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, accuracyKeyWK.get()));
422         double accuracy = WKDoubleGetValue(accuracyWK);
423
424         WKRetainPtr<WKStringRef> providesAltitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitude"));
425         WKBooleanRef providesAltitudeWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesAltitudeKeyWK.get()));
426         bool providesAltitude = WKBooleanGetValue(providesAltitudeWK);
427
428         WKRetainPtr<WKStringRef> altitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitude"));
429         WKDoubleRef altitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, altitudeKeyWK.get()));
430         double altitude = WKDoubleGetValue(altitudeWK);
431
432         WKRetainPtr<WKStringRef> providesAltitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitudeAccuracy"));
433         WKBooleanRef providesAltitudeAccuracyWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesAltitudeAccuracyKeyWK.get()));
434         bool providesAltitudeAccuracy = WKBooleanGetValue(providesAltitudeAccuracyWK);
435
436         WKRetainPtr<WKStringRef> altitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitudeAccuracy"));
437         WKDoubleRef altitudeAccuracyWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, altitudeAccuracyKeyWK.get()));
438         double altitudeAccuracy = WKDoubleGetValue(altitudeAccuracyWK);
439
440         WKRetainPtr<WKStringRef> providesHeadingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesHeading"));
441         WKBooleanRef providesHeadingWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesHeadingKeyWK.get()));
442         bool providesHeading = WKBooleanGetValue(providesHeadingWK);
443
444         WKRetainPtr<WKStringRef> headingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("heading"));
445         WKDoubleRef headingWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, headingKeyWK.get()));
446         double heading = WKDoubleGetValue(headingWK);
447
448         WKRetainPtr<WKStringRef> providesSpeedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesSpeed"));
449         WKBooleanRef providesSpeedWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesSpeedKeyWK.get()));
450         bool providesSpeed = WKBooleanGetValue(providesSpeedWK);
451
452         WKRetainPtr<WKStringRef> speedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("speed"));
453         WKDoubleRef speedWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, speedKeyWK.get()));
454         double speed = WKDoubleGetValue(speedWK);
455
456         TestController::singleton().setMockGeolocationPosition(latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed);
457         return;
458     }
459
460     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGeolocationPositionUnavailableError")) {
461         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
462         WKStringRef errorMessage = static_cast<WKStringRef>(messageBody);
463         TestController::singleton().setMockGeolocationPositionUnavailableError(errorMessage);
464         return;
465     }
466
467     if (WKStringIsEqualToUTF8CString(messageName, "SetUserMediaPermission")) {
468         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
469         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
470         TestController::singleton().setUserMediaPermission(WKBooleanGetValue(enabledWK));
471         return;
472     }
473
474     if (WKStringIsEqualToUTF8CString(messageName, "SetUserMediaPermissionForOrigin")) {
475         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
476         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
477
478         WKRetainPtr<WKStringRef> permissionKeyWK(AdoptWK, WKStringCreateWithUTF8CString("permission"));
479         WKBooleanRef permissionWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, permissionKeyWK.get()));
480         bool permission = WKBooleanGetValue(permissionWK);
481
482         WKRetainPtr<WKStringRef> originKey(AdoptWK, WKStringCreateWithUTF8CString("origin"));
483         WKStringRef originWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, originKey.get()));
484
485         WKRetainPtr<WKStringRef> parentOriginKey(AdoptWK, WKStringCreateWithUTF8CString("parentOrigin"));
486         WKStringRef parentOriginWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, parentOriginKey.get()));
487
488         TestController::singleton().setUserMediaPermissionForOrigin(permission, originWK, parentOriginWK);
489         return;
490     }
491
492     if (WKStringIsEqualToUTF8CString(messageName, "SetCacheModel")) {
493         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
494         uint64_t model = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
495         WKContextSetCacheModel(TestController::singleton().context(), model);
496         return;
497     }
498
499     if (WKStringIsEqualToUTF8CString(messageName, "SetCustomPolicyDelegate")) {
500         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
501         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
502
503         WKRetainPtr<WKStringRef> enabledKeyWK(AdoptWK, WKStringCreateWithUTF8CString("enabled"));
504         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, enabledKeyWK.get()));
505         bool enabled = WKBooleanGetValue(enabledWK);
506
507         WKRetainPtr<WKStringRef> permissiveKeyWK(AdoptWK, WKStringCreateWithUTF8CString("permissive"));
508         WKBooleanRef permissiveWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, permissiveKeyWK.get()));
509         bool permissive = WKBooleanGetValue(permissiveWK);
510
511         TestController::singleton().setCustomPolicyDelegate(enabled, permissive);
512         return;
513     }
514
515     if (WKStringIsEqualToUTF8CString(messageName, "SetHidden")) {
516         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
517         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
518
519         WKRetainPtr<WKStringRef> isInitialKeyWK(AdoptWK, WKStringCreateWithUTF8CString("hidden"));
520         WKBooleanRef hiddenWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, isInitialKeyWK.get()));
521         bool hidden = WKBooleanGetValue(hiddenWK);
522
523         TestController::singleton().setHidden(hidden);
524         return;
525     }
526
527     if (WKStringIsEqualToUTF8CString(messageName, "ProcessWorkQueue")) {
528         if (TestController::singleton().workQueueManager().processWorkQueue()) {
529             WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("WorkQueueProcessedCallback"));
530             WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
531         }
532         return;
533     }
534
535     if (WKStringIsEqualToUTF8CString(messageName, "QueueBackNavigation")) {
536         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
537         uint64_t stepCount = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
538         TestController::singleton().workQueueManager().queueBackNavigation(stepCount);
539         return;
540     }
541
542     if (WKStringIsEqualToUTF8CString(messageName, "QueueForwardNavigation")) {
543         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
544         uint64_t stepCount = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
545         TestController::singleton().workQueueManager().queueForwardNavigation(stepCount);
546         return;
547     }
548
549     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoad")) {
550         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
551         WKDictionaryRef loadDataDictionary = static_cast<WKDictionaryRef>(messageBody);
552
553         WKRetainPtr<WKStringRef> urlKey(AdoptWK, WKStringCreateWithUTF8CString("url"));
554         WKStringRef urlWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, urlKey.get()));
555
556         WKRetainPtr<WKStringRef> targetKey(AdoptWK, WKStringCreateWithUTF8CString("target"));
557         WKStringRef targetWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, targetKey.get()));
558
559         WKRetainPtr<WKStringRef> shouldOpenExternalURLsKey(AdoptWK, WKStringCreateWithUTF8CString("shouldOpenExternalURLs"));
560         WKBooleanRef shouldOpenExternalURLsValueWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(loadDataDictionary, shouldOpenExternalURLsKey.get()));
561
562         TestController::singleton().workQueueManager().queueLoad(toWTFString(urlWK), toWTFString(targetWK), WKBooleanGetValue(shouldOpenExternalURLsValueWK));
563         return;
564     }
565
566     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoadHTMLString")) {
567         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
568         WKDictionaryRef loadDataDictionary = static_cast<WKDictionaryRef>(messageBody);
569
570         WKRetainPtr<WKStringRef> contentKey(AdoptWK, WKStringCreateWithUTF8CString("content"));
571         WKStringRef contentWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, contentKey.get()));
572
573         WKRetainPtr<WKStringRef> baseURLKey(AdoptWK, WKStringCreateWithUTF8CString("baseURL"));
574         WKStringRef baseURLWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, baseURLKey.get()));
575
576         WKRetainPtr<WKStringRef> unreachableURLKey(AdoptWK, WKStringCreateWithUTF8CString("unreachableURL"));
577         WKStringRef unreachableURLWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, unreachableURLKey.get()));
578
579         TestController::singleton().workQueueManager().queueLoadHTMLString(toWTFString(contentWK), baseURLWK ? toWTFString(baseURLWK) : String(), unreachableURLWK ? toWTFString(unreachableURLWK) : String());
580         return;
581     }
582
583     if (WKStringIsEqualToUTF8CString(messageName, "QueueReload")) {
584         TestController::singleton().workQueueManager().queueReload();
585         return;
586     }
587
588     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoadingScript")) {
589         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
590         WKStringRef script = static_cast<WKStringRef>(messageBody);
591         TestController::singleton().workQueueManager().queueLoadingScript(toWTFString(script));
592         return;
593     }
594
595     if (WKStringIsEqualToUTF8CString(messageName, "QueueNonLoadingScript")) {
596         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
597         WKStringRef script = static_cast<WKStringRef>(messageBody);
598         TestController::singleton().workQueueManager().queueNonLoadingScript(toWTFString(script));
599         return;
600     }
601
602     if (WKStringIsEqualToUTF8CString(messageName, "SetHandlesAuthenticationChallenge")) {
603         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
604         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
605         TestController::singleton().setHandlesAuthenticationChallenges(WKBooleanGetValue(value));
606         return;
607     }
608
609     if (WKStringIsEqualToUTF8CString(messageName, "SetAuthenticationUsername")) {
610         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
611         WKStringRef username = static_cast<WKStringRef>(messageBody);
612         TestController::singleton().setAuthenticationUsername(toWTFString(username));
613         return;
614     }
615
616     if (WKStringIsEqualToUTF8CString(messageName, "SetAuthenticationPassword")) {
617         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
618         WKStringRef password = static_cast<WKStringRef>(messageBody);
619         TestController::singleton().setAuthenticationPassword(toWTFString(password));
620         return;
621     }
622
623     if (WKStringIsEqualToUTF8CString(messageName, "SetBlockAllPlugins")) {
624         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
625         WKBooleanRef shouldBlock = static_cast<WKBooleanRef>(messageBody);
626         TestController::singleton().setBlockAllPlugins(WKBooleanGetValue(shouldBlock));
627         return;
628     }
629
630     if (WKStringIsEqualToUTF8CString(messageName, "SetShouldDecideNavigationPolicyAfterDelay")) {
631         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
632         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
633         TestController::singleton().setShouldDecideNavigationPolicyAfterDelay(WKBooleanGetValue(value));
634         return;
635     }
636
637     if (WKStringIsEqualToUTF8CString(messageName, "SetNavigationGesturesEnabled")) {
638         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
639         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
640         TestController::singleton().setNavigationGesturesEnabled(WKBooleanGetValue(value));
641         return;
642     }
643
644     if (WKStringIsEqualToUTF8CString(messageName, "RunUIProcessScript")) {
645         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
646         WKRetainPtr<WKStringRef> scriptKey(AdoptWK, WKStringCreateWithUTF8CString("Script"));
647         WKRetainPtr<WKStringRef> callbackIDKey(AdoptWK, WKStringCreateWithUTF8CString("CallbackID"));
648
649         UIScriptInvocationData* invocationData = new UIScriptInvocationData();
650         invocationData->testInvocation = this;
651         invocationData->callbackID = (unsigned)WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, callbackIDKey.get())));
652         invocationData->scriptString = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, scriptKey.get()));
653         m_pendingUIScriptInvocationData = invocationData;
654         WKPageCallAfterNextPresentationUpdate(TestController::singleton().mainWebView()->page(), invocationData, runUISideScriptAfterUpdateCallback);
655         return;
656     }
657
658     ASSERT_NOT_REACHED();
659 }
660
661 WKRetainPtr<WKTypeRef> TestInvocation::didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
662 {
663     if (WKStringIsEqualToUTF8CString(messageName, "SetWindowIsKey")) {
664         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
665         WKBooleanRef isKeyValue = static_cast<WKBooleanRef>(messageBody);
666         TestController::singleton().mainWebView()->setWindowIsKey(WKBooleanGetValue(isKeyValue));
667         return nullptr;
668     }
669
670     if (WKStringIsEqualToUTF8CString(messageName, "SetViewSize")) {
671         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
672
673         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
674         WKRetainPtr<WKStringRef> widthKey(AdoptWK, WKStringCreateWithUTF8CString("width"));
675         WKRetainPtr<WKStringRef> heightKey(AdoptWK, WKStringCreateWithUTF8CString("height"));
676
677         WKDoubleRef widthWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, widthKey.get()));
678         WKDoubleRef heightWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, heightKey.get()));
679
680         TestController::singleton().mainWebView()->resizeTo(WKDoubleGetValue(widthWK), WKDoubleGetValue(heightWK));
681         return nullptr;
682     }
683
684     if (WKStringIsEqualToUTF8CString(messageName, "IsGeolocationClientActive")) {
685         bool isActive = TestController::singleton().isGeolocationProviderActive();
686         WKRetainPtr<WKTypeRef> result(AdoptWK, WKBooleanCreate(isActive));
687         return result;
688     }
689
690     if (WKStringIsEqualToUTF8CString(messageName, "IsWorkQueueEmpty")) {
691         bool isEmpty = TestController::singleton().workQueueManager().isWorkQueueEmpty();
692         WKRetainPtr<WKTypeRef> result(AdoptWK, WKBooleanCreate(isEmpty));
693         return result;
694     }
695
696     if (WKStringIsEqualToUTF8CString(messageName, "SecureEventInputIsEnabled")) {
697 #if PLATFORM(MAC) && !PLATFORM(IOS)
698         WKRetainPtr<WKBooleanRef> result(AdoptWK, WKBooleanCreate(IsSecureEventInputEnabled()));
699 #else
700         WKRetainPtr<WKBooleanRef> result(AdoptWK, WKBooleanCreate(false));
701 #endif
702         return result;
703     }
704
705     if (WKStringIsEqualToUTF8CString(messageName, "SetAlwaysAcceptCookies")) {
706         WKBooleanRef accept = static_cast<WKBooleanRef>(messageBody);
707         WKHTTPCookieAcceptPolicy policy = WKBooleanGetValue(accept) ? kWKHTTPCookieAcceptPolicyAlways : kWKHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain;
708         // FIXME: This updates the policy in WebProcess and in NetworkProcess asynchronously, which might break some tests' expectations.
709         WKCookieManagerSetHTTPCookieAcceptPolicy(WKContextGetCookieManager(TestController::singleton().context()), policy);
710         return nullptr;
711     }
712
713     if (WKStringIsEqualToUTF8CString(messageName, "ImageCountInGeneralPasteboard")) {
714         unsigned count = TestController::singleton().imageCountInGeneralPasteboard();
715         WKRetainPtr<WKUInt64Ref> result(AdoptWK, WKUInt64Create(count));
716         return result;
717     }
718     
719     if (WKStringIsEqualToUTF8CString(messageName, "DeleteAllIndexedDatabases")) {
720         WKWebsiteDataStoreRemoveAllIndexedDatabases(WKContextGetWebsiteDataStore(TestController::singleton().context()));
721         return nullptr;
722     }
723
724     ASSERT_NOT_REACHED();
725     return nullptr;
726 }
727
728 void TestInvocation::runUISideScriptAfterUpdateCallback(WKErrorRef, void* context)
729 {
730     UIScriptInvocationData* data = static_cast<UIScriptInvocationData*>(context);
731     if (TestInvocation* invocation = data->testInvocation) {
732         RELEASE_ASSERT(TestController::singleton().isCurrentInvocation(invocation));
733         invocation->runUISideScript(data->scriptString.get(), data->callbackID);
734     }
735     delete data;
736 }
737
738 void TestInvocation::runUISideScript(WKStringRef script, unsigned scriptCallbackID)
739 {
740     m_pendingUIScriptInvocationData = nullptr;
741
742     if (!m_UIScriptContext)
743         m_UIScriptContext = std::make_unique<UIScriptContext>(*this);
744     
745     m_UIScriptContext->runUIScript(script, scriptCallbackID);
746 }
747
748 void TestInvocation::uiScriptDidComplete(WKStringRef result, unsigned scriptCallbackID)
749 {
750     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallUISideScriptCallback"));
751
752     WKRetainPtr<WKMutableDictionaryRef> messageBody(AdoptWK, WKMutableDictionaryCreate());
753     WKRetainPtr<WKStringRef> resultKey(AdoptWK, WKStringCreateWithUTF8CString("Result"));
754     WKRetainPtr<WKStringRef> callbackIDKey(AdoptWK, WKStringCreateWithUTF8CString("CallbackID"));
755     WKRetainPtr<WKUInt64Ref> callbackIDValue = adoptWK(WKUInt64Create(scriptCallbackID));
756
757     WKDictionarySetItem(messageBody.get(), resultKey.get(), result);
758     WKDictionarySetItem(messageBody.get(), callbackIDKey.get(), callbackIDValue.get());
759
760     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), messageBody.get());
761 }
762
763 void TestInvocation::outputText(const WTF::String& text)
764 {
765     m_textOutput.append(text);
766 }
767
768 void TestInvocation::didBeginSwipe()
769 {
770     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidBeginSwipeCallback"));
771     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
772 }
773
774 void TestInvocation::willEndSwipe()
775 {
776     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallWillEndSwipeCallback"));
777     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
778 }
779
780 void TestInvocation::didEndSwipe()
781 {
782     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidEndSwipeCallback"));
783     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
784 }
785
786 void TestInvocation::didRemoveSwipeSnapshot()
787 {
788     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallDidRemoveSwipeSnapshotCallback"));
789     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
790 }
791
792 void TestInvocation::notifyDownloadDone()
793 {
794     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("NotifyDownloadDone"));
795     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), 0);
796 }
797
798 } // namespace WTR