c2310766177e4bd5646043219e9eb6463aece0e5
[WebKit-https.git] / Tools / WebKitTestRunner / TestController.cpp
1 /*
2  * Copyright (C) 2010, 2014-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 #include "TestController.h"
28
29 #include "EventSenderProxy.h"
30 #include "Options.h"
31 #include "PlatformWebView.h"
32 #include "StringFunctions.h"
33 #include "TestInvocation.h"
34 #include <WebKit/WKArray.h>
35 #include <WebKit/WKAuthenticationChallenge.h>
36 #include <WebKit/WKAuthenticationDecisionListener.h>
37 #include <WebKit/WKContextConfigurationRef.h>
38 #include <WebKit/WKContextPrivate.h>
39 #include <WebKit/WKCookieManager.h>
40 #include <WebKit/WKCredential.h>
41 #include <WebKit/WKIconDatabase.h>
42 #include <WebKit/WKNavigationResponseRef.h>
43 #include <WebKit/WKNotification.h>
44 #include <WebKit/WKNotificationManager.h>
45 #include <WebKit/WKNotificationPermissionRequest.h>
46 #include <WebKit/WKNumber.h>
47 #include <WebKit/WKPageGroup.h>
48 #include <WebKit/WKPageInjectedBundleClient.h>
49 #include <WebKit/WKPagePrivate.h>
50 #include <WebKit/WKPluginInformation.h>
51 #include <WebKit/WKPreferencesRefPrivate.h>
52 #include <WebKit/WKProtectionSpace.h>
53 #include <WebKit/WKRetainPtr.h>
54 #include <algorithm>
55 #include <cstdio>
56 #include <ctype.h>
57 #include <stdlib.h>
58 #include <string>
59 #include <wtf/text/CString.h>
60
61 #if PLATFORM(COCOA)
62 #include <WebKit/WKContextPrivateMac.h>
63 #include <WebKit/WKPagePrivateMac.h>
64 #endif
65
66 #if !PLATFORM(COCOA)
67 #include <WebKit/WKTextChecker.h>
68 #endif
69
70 namespace WTR {
71
72 const unsigned TestController::viewWidth = 800;
73 const unsigned TestController::viewHeight = 600;
74
75 const unsigned TestController::w3cSVGViewWidth = 480;
76 const unsigned TestController::w3cSVGViewHeight = 360;
77
78 #if ASAN_ENABLED
79 const double TestController::shortTimeout = 10.0;
80 #else
81 const double TestController::shortTimeout = 5.0;
82 #endif
83
84 const double TestController::noTimeout = -1;
85
86 static WKURLRef blankURL()
87 {
88     static WKURLRef staticBlankURL = WKURLCreateWithUTF8CString("about:blank");
89     return staticBlankURL;
90 }
91
92 static WKDataRef copyWebCryptoMasterKey(WKPageRef, const void*)
93 {
94     // Any 128 bit key would do, all we need for testing is to implement the callback.
95     return WKDataCreate((const uint8_t*)"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
96 }
97
98 static TestController* controller;
99
100 TestController& TestController::singleton()
101 {
102     ASSERT(controller);
103     return *controller;
104 }
105
106 TestController::TestController(int argc, const char* argv[])
107     : m_verbose(false)
108     , m_printSeparators(false)
109     , m_usingServerMode(false)
110     , m_gcBetweenTests(false)
111     , m_shouldDumpPixelsForAllTests(false)
112     , m_state(Initial)
113     , m_doneResetting(false)
114     , m_useWaitToDumpWatchdogTimer(true)
115     , m_forceNoTimeout(false)
116     , m_didPrintWebProcessCrashedMessage(false)
117     , m_shouldExitWhenWebProcessCrashes(true)
118     , m_beforeUnloadReturnValue(true)
119     , m_isGeolocationPermissionSet(false)
120     , m_isGeolocationPermissionAllowed(false)
121     , m_policyDelegateEnabled(false)
122     , m_policyDelegatePermissive(false)
123     , m_handlesAuthenticationChallenges(false)
124     , m_shouldBlockAllPlugins(false)
125     , m_forceComplexText(false)
126     , m_shouldUseAcceleratedDrawing(false)
127     , m_shouldUseRemoteLayerTree(false)
128     , m_shouldLogHistoryClientCallbacks(false)
129     , m_shouldShowWebView(false)
130 {
131     initialize(argc, argv);
132     controller = this;
133     run();
134     controller = 0;
135 }
136
137 TestController::~TestController()
138 {
139     WKIconDatabaseClose(WKContextGetIconDatabase(m_context.get()));
140
141     platformDestroy();
142 }
143
144 static WKRect getWindowFrame(WKPageRef page, const void* clientInfo)
145 {
146     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
147     return view->windowFrame();
148 }
149
150 static void setWindowFrame(WKPageRef page, WKRect frame, const void* clientInfo)
151 {
152     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
153     view->setWindowFrame(frame);
154 }
155
156 static bool runBeforeUnloadConfirmPanel(WKPageRef page, WKStringRef message, WKFrameRef frame, const void*)
157 {
158     printf("CONFIRM NAVIGATION: %s\n", toSTD(message).c_str());
159     return TestController::singleton().beforeUnloadReturnValue();
160 }
161
162 void TestController::runModal(WKPageRef page, const void* clientInfo)
163 {
164     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
165     view->setWindowIsKey(false);
166     runModal(view);
167     view->setWindowIsKey(true);
168 }
169
170 static void closeOtherPage(WKPageRef page, const void* clientInfo)
171 {
172     WKPageClose(page);
173     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
174     delete view;
175 }
176
177 static void focus(WKPageRef page, const void* clientInfo)
178 {
179     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
180     view->focus();
181     view->setWindowIsKey(true);
182 }
183
184 static void unfocus(WKPageRef page, const void* clientInfo)
185 {
186     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
187     view->setWindowIsKey(false);
188 }
189
190 static void decidePolicyForGeolocationPermissionRequest(WKPageRef, WKFrameRef, WKSecurityOriginRef, WKGeolocationPermissionRequestRef permissionRequest, const void* clientInfo)
191 {
192     TestController::singleton().handleGeolocationPermissionRequest(permissionRequest);
193 }
194
195 static void decidePolicyForUserMediaPermissionRequest(WKPageRef, WKFrameRef, WKSecurityOriginRef, WKUserMediaPermissionRequestRef permissionRequest, const void* clientInfo)
196 {
197     TestController::singleton().handleUserMediaPermissionRequest(permissionRequest);
198 }
199
200 WKPageRef TestController::createOtherPage(WKPageRef oldPage, WKPageConfigurationRef configuration, WKNavigationActionRef navigationAction, WKWindowFeaturesRef windowFeatures, const void *clientInfo)
201 {
202     PlatformWebView* parentView = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
203
204     PlatformWebView* view = platformCreateOtherPage(parentView, configuration, parentView->options());
205     WKPageRef newPage = view->page();
206
207     view->resizeTo(800, 600);
208
209     WKPageUIClientV6 otherPageUIClient = {
210         { 6, view },
211         0, // createNewPage_deprecatedForUseWithV0
212         0, // showPage
213         closeOtherPage,
214         0, // takeFocus
215         focus,
216         unfocus,
217         0, // runJavaScriptAlert_deprecatedForUseWithV0
218         0, // runJavaScriptAlert_deprecatedForUseWithV0
219         0, // runJavaScriptAlert_deprecatedForUseWithV0
220         0, // setStatusText
221         0, // mouseDidMoveOverElement_deprecatedForUseWithV0
222         0, // missingPluginButtonClicked
223         0, // didNotHandleKeyEvent
224         0, // didNotHandleWheelEvent
225         0, // toolbarsAreVisible
226         0, // setToolbarsAreVisible
227         0, // menuBarIsVisible
228         0, // setMenuBarIsVisible
229         0, // statusBarIsVisible
230         0, // setStatusBarIsVisible
231         0, // isResizable
232         0, // setIsResizable
233         getWindowFrame,
234         setWindowFrame,
235         runBeforeUnloadConfirmPanel,
236         0, // didDraw
237         0, // pageDidScroll
238         0, // exceededDatabaseQuota
239         0, // runOpenPanel
240         decidePolicyForGeolocationPermissionRequest,
241         0, // headerHeight
242         0, // footerHeight
243         0, // drawHeader
244         0, // drawFooter
245         0, // printFrame
246         runModal,
247         0, // didCompleteRubberBandForMainFrame
248         0, // saveDataToFileInDownloadsFolder
249         0, // shouldInterruptJavaScript
250         0, // createNewPage_deprecatedForUseWithV1
251         0, // mouseDidMoveOverElement
252         0, // decidePolicyForNotificationPermissionRequest
253         0, // unavailablePluginButtonClicked_deprecatedForUseWithV1
254         0, // showColorPicker
255         0, // hideColorPicker
256         0, // unavailablePluginButtonClicked
257         0, // pinnedStateDidChange
258         0, // didBeginTrackingPotentialLongMousePress
259         0, // didRecognizeLongMousePress
260         0, // didCancelTrackingPotentialLongMousePress
261         0, // isPlayingAudioDidChange
262         decidePolicyForUserMediaPermissionRequest,
263         0, // didClickAutofillButton
264         0, // runJavaScriptAlert
265         0, // runJavaScriptConfirm
266         0, // runJavaScriptPrompt
267         0, // mediaSessionMetadataDidChange
268         createOtherPage,
269     };
270     WKPageSetPageUIClient(newPage, &otherPageUIClient.base);
271     
272     WKPageNavigationClientV0 pageNavigationClient = {
273         { 0, &TestController::singleton() },
274         decidePolicyForNavigationAction,
275         decidePolicyForNavigationResponse,
276         decidePolicyForPluginLoad,
277         0, // didStartProvisionalNavigation
278         0, // didReceiveServerRedirectForProvisionalNavigation
279         0, // didFailProvisionalNavigation
280         0, // didCommitNavigation
281         0, // didFinishNavigation
282         0, // didFailNavigation
283         0, // didFailProvisionalLoadInSubframe
284         0, // didFinishDocumentLoad
285         0, // didSameDocumentNavigation
286         0, // renderingProgressDidChange
287         canAuthenticateAgainstProtectionSpace,
288         didReceiveAuthenticationChallenge,
289         processDidCrash,
290         copyWebCryptoMasterKey,
291     };
292     WKPageSetPageNavigationClient(newPage, &pageNavigationClient.base);
293
294     view->didInitializeClients();
295
296     TestController::singleton().updateWindowScaleForTest(view, *TestController::singleton().m_currentInvocation);
297
298     WKRetain(newPage);
299     return newPage;
300 }
301
302 const char* TestController::libraryPathForTesting()
303 {
304     // FIXME: This may not be sufficient to prevent interactions/crashes
305     // when running more than one copy of DumpRenderTree.
306     // See https://bugs.webkit.org/show_bug.cgi?id=10906
307     char* dumpRenderTreeTemp = getenv("DUMPRENDERTREE_TEMP");
308     if (dumpRenderTreeTemp)
309         return dumpRenderTreeTemp;
310     return platformLibraryPathForTesting();
311 }
312
313 void TestController::initialize(int argc, const char* argv[])
314 {
315     platformInitialize();
316
317     Options options;
318     OptionsHandler optionsHandler(options);
319
320     if (argc < 2) {
321         optionsHandler.printHelp();
322         exit(1);
323     }
324     if (!optionsHandler.parse(argc, argv))
325         exit(1);
326
327     m_useWaitToDumpWatchdogTimer = options.useWaitToDumpWatchdogTimer;
328     m_forceNoTimeout = options.forceNoTimeout;
329     m_verbose = options.verbose;
330     m_gcBetweenTests = options.gcBetweenTests;
331     m_shouldDumpPixelsForAllTests = options.shouldDumpPixelsForAllTests;
332     m_forceComplexText = options.forceComplexText;
333     m_shouldUseAcceleratedDrawing = options.shouldUseAcceleratedDrawing;
334     m_shouldUseRemoteLayerTree = options.shouldUseRemoteLayerTree;
335     m_paths = options.paths;
336     m_allowedHosts = options.allowedHosts;
337     m_shouldShowWebView = options.shouldShowWebView;
338
339     if (options.printSupportedFeatures) {
340         // FIXME: On Windows, DumpRenderTree uses this to expose whether it supports 3d
341         // transforms and accelerated compositing. When we support those features, we
342         // should match DRT's behavior.
343         exit(0);
344     }
345
346     m_usingServerMode = (m_paths.size() == 1 && m_paths[0] == "-");
347     if (m_usingServerMode)
348         m_printSeparators = true;
349     else
350         m_printSeparators = m_paths.size() > 1;
351
352     initializeInjectedBundlePath();
353     initializeTestPluginDirectory();
354
355     WKRetainPtr<WKStringRef> pageGroupIdentifier(AdoptWK, WKStringCreateWithUTF8CString("WebKitTestRunnerPageGroup"));
356     m_pageGroup.adopt(WKPageGroupCreateWithIdentifier(pageGroupIdentifier.get()));
357
358     auto configuration = adoptWK(WKContextConfigurationCreate());
359     WKContextConfigurationSetInjectedBundlePath(configuration.get(), injectedBundlePath());
360     WKContextConfigurationSetFullySynchronousModeIsAllowedForTesting(configuration.get(), true);
361
362     if (const char* dumpRenderTreeTemp = libraryPathForTesting()) {
363         String temporaryFolder = String::fromUTF8(dumpRenderTreeTemp);
364
365         const char separator = '/';
366
367         WKContextConfigurationSetApplicationCacheDirectory(configuration.get(), toWK(temporaryFolder + separator + "ApplicationCache").get());
368         WKContextConfigurationSetDiskCacheDirectory(configuration.get(), toWK(temporaryFolder + separator + "Cache").get());
369         WKContextConfigurationSetIndexedDBDatabaseDirectory(configuration.get(), toWK(temporaryFolder + separator + "Databases" + separator + "IndexedDB").get());
370         WKContextConfigurationSetLocalStorageDirectory(configuration.get(), toWK(temporaryFolder + separator + "LocalStorage").get());
371         WKContextConfigurationSetWebSQLDatabaseDirectory(configuration.get(), toWK(temporaryFolder + separator + "Databases" + separator + "WebSQL").get());
372         WKContextConfigurationSetMediaKeysStorageDirectory(configuration.get(), toWK(temporaryFolder + separator + "MediaKeys").get());
373     }
374     m_context = platformAdjustContext(adoptWK(WKContextCreateWithConfiguration(configuration.get())).get(), configuration.get());
375
376     m_geolocationProvider = std::make_unique<GeolocationProviderMock>(m_context.get());
377
378 #if PLATFORM(EFL)
379     WKContextSetUsesNetworkProcess(m_context.get(), false);
380     WKContextSetProcessModel(m_context.get(), kWKProcessModelSharedSecondaryProcess);
381 #endif
382
383     if (const char* dumpRenderTreeTemp = libraryPathForTesting()) {
384         String temporaryFolder = String::fromUTF8(dumpRenderTreeTemp);
385
386         // FIXME: This should be migrated to WKContextConfigurationRef.
387         // Disable icon database to avoid fetching <http://127.0.0.1:8000/favicon.ico> and making tests flaky.
388         // Invividual tests can enable it using testRunner.setIconDatabaseEnabled, although it's not currently supported in WebKitTestRunner.
389         WKContextSetIconDatabasePath(m_context.get(), toWK(emptyString()).get());
390     }
391
392     WKContextUseTestingNetworkSession(m_context.get());
393     WKContextSetCacheModel(m_context.get(), kWKCacheModelDocumentBrowser);
394
395     platformInitializeContext();
396
397     WKContextInjectedBundleClientV1 injectedBundleClient = {
398         { 1, this },
399         didReceiveMessageFromInjectedBundle,
400         didReceiveSynchronousMessageFromInjectedBundle,
401         0 // getInjectedBundleInitializationUserData
402     };
403     WKContextSetInjectedBundleClient(m_context.get(), &injectedBundleClient.base);
404
405     WKContextClientV1 contextClient = {
406         { 1, this },
407         0, // plugInAutoStartOriginHashesChanged
408         networkProcessDidCrash,
409         0, // plugInInformationBecameAvailable
410         0, // copyWebCryptoMasterKey
411     };
412     WKContextSetClient(m_context.get(), &contextClient.base);
413
414     WKContextHistoryClientV0 historyClient = {
415         { 0, this },
416         didNavigateWithNavigationData,
417         didPerformClientRedirect,
418         didPerformServerRedirect,
419         didUpdateHistoryTitle,
420         0, // populateVisitedLinks
421     };
422     WKContextSetHistoryClient(m_context.get(), &historyClient.base);
423
424     WKNotificationManagerRef notificationManager = WKContextGetNotificationManager(m_context.get());
425     WKNotificationProviderV0 notificationKit = m_webNotificationProvider.provider();
426     WKNotificationManagerSetProvider(notificationManager, &notificationKit.base);
427
428     if (testPluginDirectory())
429         WKContextSetAdditionalPluginsDirectory(m_context.get(), testPluginDirectory());
430
431     if (m_forceComplexText)
432         WKContextSetAlwaysUsesComplexTextCodePath(m_context.get(), true);
433
434     m_configuration = adoptWK(WKPageConfigurationCreate());
435     WKPageConfigurationSetContext(m_configuration.get(), m_context.get());
436     WKPageConfigurationSetPageGroup(m_configuration.get(), m_pageGroup.get());
437     WKPageConfigurationSetUserContentController(m_configuration.get(), adoptWK(WKUserContentControllerCreate()).get());
438
439     // Some preferences (notably mock scroll bars setting) currently cannot be re-applied to an existing view, so we need to set them now.
440     resetPreferencesToConsistentValues();
441 }
442
443 void TestController::createWebViewWithOptions(const ViewOptions& options)
444 {
445     platformCreateWebView(m_configuration.get(), options);
446     WKPageUIClientV6 pageUIClient = {
447         { 6, m_mainWebView.get() },
448         0, // createNewPage_deprecatedForUseWithV0
449         0, // showPage
450         0, // close
451         0, // takeFocus
452         focus,
453         unfocus,
454         0, // runJavaScriptAlert_deprecatedForUseWithV0
455         0, // runJavaScriptAlert_deprecatedForUseWithV0
456         0, // runJavaScriptAlert_deprecatedForUseWithV0
457         0, // setStatusText
458         0, // mouseDidMoveOverElement_deprecatedForUseWithV0
459         0, // missingPluginButtonClicked
460         0, // didNotHandleKeyEvent
461         0, // didNotHandleWheelEvent
462         0, // toolbarsAreVisible
463         0, // setToolbarsAreVisible
464         0, // menuBarIsVisible
465         0, // setMenuBarIsVisible
466         0, // statusBarIsVisible
467         0, // setStatusBarIsVisible
468         0, // isResizable
469         0, // setIsResizable
470         getWindowFrame,
471         setWindowFrame,
472         runBeforeUnloadConfirmPanel,
473         0, // didDraw
474         0, // pageDidScroll
475         0, // exceededDatabaseQuota,
476         0, // runOpenPanel
477         decidePolicyForGeolocationPermissionRequest,
478         0, // headerHeight
479         0, // footerHeight
480         0, // drawHeader
481         0, // drawFooter
482         0, // printFrame
483         runModal,
484         0, // didCompleteRubberBandForMainFrame
485         0, // saveDataToFileInDownloadsFolder
486         0, // shouldInterruptJavaScript
487         0, // createNewPage_deprecatedForUseWithV1
488         0, // mouseDidMoveOverElement
489         decidePolicyForNotificationPermissionRequest, // decidePolicyForNotificationPermissionRequest
490         0, // unavailablePluginButtonClicked_deprecatedForUseWithV1
491         0, // showColorPicker
492         0, // hideColorPicker
493         unavailablePluginButtonClicked,
494         0, // pinnedStateDidChange
495         0, // didBeginTrackingPotentialLongMousePress
496         0, // didRecognizeLongMousePress
497         0, // didCancelTrackingPotentialLongMousePress
498         0, // isPlayingAudioDidChange
499         decidePolicyForUserMediaPermissionRequest,
500         0, // didClickAutofillButton
501         0, // runJavaScriptAlert
502         0, // runJavaScriptConfirm
503         0, // runJavaScriptPrompt
504         0, // mediaSessionMetadataDidChange
505         createOtherPage,
506     };
507     WKPageSetPageUIClient(m_mainWebView->page(), &pageUIClient.base);
508
509     WKPageNavigationClientV0 pageNavigationClient = {
510         { 0, this },
511         decidePolicyForNavigationAction,
512         decidePolicyForNavigationResponse,
513         decidePolicyForPluginLoad,
514         0, // didStartProvisionalNavigation
515         0, // didReceiveServerRedirectForProvisionalNavigation
516         0, // didFailProvisionalNavigation
517         didCommitNavigation,
518         didFinishNavigation,
519         0, // didFailNavigation
520         0, // didFailProvisionalLoadInSubframe
521         0, // didFinishDocumentLoad
522         0, // didSameDocumentNavigation
523         0, // renderingProgressDidChange
524         canAuthenticateAgainstProtectionSpace,
525         didReceiveAuthenticationChallenge,
526         processDidCrash,
527         copyWebCryptoMasterKey,
528     };
529     WKPageSetPageNavigationClient(m_mainWebView->page(), &pageNavigationClient.base);
530
531
532     // this should just be done on the page?
533     WKPageInjectedBundleClientV0 injectedBundleClient = {
534         { 0, this },
535         didReceivePageMessageFromInjectedBundle,
536         didReceiveSynchronousPageMessageFromInjectedBundle
537     };
538     WKPageSetPageInjectedBundleClient(m_mainWebView->page(), &injectedBundleClient.base);
539
540     m_mainWebView->didInitializeClients();
541
542     // Generally, the tests should default to running at 1x. updateWindowScaleForTest() will adjust the scale to
543     // something else for specific tests that need to run at a different window scale.
544     m_mainWebView->changeWindowScaleIfNeeded(1);
545 }
546
547 void TestController::ensureViewSupportsOptionsForTest(const TestInvocation& test)
548 {
549     auto viewOptions = viewOptionsForTest(test);
550
551     if (m_mainWebView) {
552         if (m_mainWebView->viewSupportsOptions(viewOptions))
553             return;
554
555         WKPageSetPageUIClient(m_mainWebView->page(), nullptr);
556         WKPageSetPageNavigationClient(m_mainWebView->page(), nullptr);
557         WKPageClose(m_mainWebView->page());
558
559         m_mainWebView = nullptr;
560     }
561
562     createWebViewWithOptions(viewOptions);
563
564     if (!resetStateToConsistentValues())
565         TestInvocation::dumpWebProcessUnresponsiveness("<unknown> - TestController::run - Failed to reset state to consistent values\n");
566 }
567
568 void TestController::resetPreferencesToConsistentValues()
569 {
570     // Reset preferences
571     WKPreferencesRef preferences = platformPreferences();
572     WKPreferencesResetTestRunnerOverrides(preferences);
573     WKPreferencesSetPageVisibilityBasedProcessSuppressionEnabled(preferences, false);
574     WKPreferencesSetOfflineWebApplicationCacheEnabled(preferences, true);
575     WKPreferencesSetFontSmoothingLevel(preferences, kWKFontSmoothingLevelNoSubpixelAntiAliasing);
576     WKPreferencesSetAntialiasedFontDilationEnabled(preferences, false);
577     WKPreferencesSetXSSAuditorEnabled(preferences, false);
578     WKPreferencesSetWebAudioEnabled(preferences, true);
579     WKPreferencesSetMediaStreamEnabled(preferences, true);
580     WKPreferencesSetDeveloperExtrasEnabled(preferences, true);
581     WKPreferencesSetJavaScriptRuntimeFlags(preferences, kWKJavaScriptRuntimeFlagsAllEnabled);
582     WKPreferencesSetJavaScriptCanOpenWindowsAutomatically(preferences, true);
583     WKPreferencesSetJavaScriptCanAccessClipboard(preferences, true);
584     WKPreferencesSetDOMPasteAllowed(preferences, true);
585     WKPreferencesSetUniversalAccessFromFileURLsAllowed(preferences, true);
586     WKPreferencesSetFileAccessFromFileURLsAllowed(preferences, true);
587 #if ENABLE(FULLSCREEN_API)
588     WKPreferencesSetFullScreenEnabled(preferences, true);
589 #endif
590     WKPreferencesSetPageCacheEnabled(preferences, false);
591     WKPreferencesSetAsynchronousPluginInitializationEnabled(preferences, false);
592     WKPreferencesSetAsynchronousPluginInitializationEnabledForAllPlugins(preferences, false);
593     WKPreferencesSetArtificialPluginInitializationDelayEnabled(preferences, false);
594     WKPreferencesSetTabToLinksEnabled(preferences, false);
595     WKPreferencesSetInteractiveFormValidationEnabled(preferences, true);
596     WKPreferencesSetMockScrollbarsEnabled(preferences, true);
597
598     static WKStringRef defaultTextEncoding = WKStringCreateWithUTF8CString("ISO-8859-1");
599     WKPreferencesSetDefaultTextEncodingName(preferences, defaultTextEncoding);
600
601     static WKStringRef standardFontFamily = WKStringCreateWithUTF8CString("Times");
602     static WKStringRef cursiveFontFamily = WKStringCreateWithUTF8CString("Apple Chancery");
603     static WKStringRef fantasyFontFamily = WKStringCreateWithUTF8CString("Papyrus");
604     static WKStringRef fixedFontFamily = WKStringCreateWithUTF8CString("Courier");
605     static WKStringRef pictographFontFamily = WKStringCreateWithUTF8CString("Apple Color Emoji");
606     static WKStringRef sansSerifFontFamily = WKStringCreateWithUTF8CString("Helvetica");
607     static WKStringRef serifFontFamily = WKStringCreateWithUTF8CString("Times");
608
609     WKPreferencesSetStandardFontFamily(preferences, standardFontFamily);
610     WKPreferencesSetCursiveFontFamily(preferences, cursiveFontFamily);
611     WKPreferencesSetFantasyFontFamily(preferences, fantasyFontFamily);
612     WKPreferencesSetFixedFontFamily(preferences, fixedFontFamily);
613     WKPreferencesSetPictographFontFamily(preferences, pictographFontFamily);
614     WKPreferencesSetSansSerifFontFamily(preferences, sansSerifFontFamily);
615     WKPreferencesSetSerifFontFamily(preferences, serifFontFamily);
616     WKPreferencesSetAsynchronousSpellCheckingEnabled(preferences, false);
617 #if ENABLE(WEB_AUDIO)
618     WKPreferencesSetMediaSourceEnabled(preferences, true);
619 #endif
620
621     WKPreferencesSetHiddenPageDOMTimerThrottlingEnabled(preferences, false);
622     WKPreferencesSetHiddenPageCSSAnimationSuspensionEnabled(preferences, false);
623
624     WKPreferencesSetAcceleratedDrawingEnabled(preferences, m_shouldUseAcceleratedDrawing);
625     // FIXME: We should be testing the default.
626     WKPreferencesSetStorageBlockingPolicy(preferences, kWKAllowAllStorage);
627
628     WKCookieManagerDeleteAllCookies(WKContextGetCookieManager(m_context.get()));
629
630     platformResetPreferencesToConsistentValues();
631 }
632
633 bool TestController::resetStateToConsistentValues()
634 {
635     m_state = Resetting;
636
637     m_beforeUnloadReturnValue = true;
638
639     // This setting differs between the antique and modern Mac WebKit2 API.
640     // For now, maintain the antique behavior, because some tests depend on it!
641     // FIXME: We should be testing the default.
642     WKPageSetBackgroundExtendsBeyondPage(m_mainWebView->page(), false);
643
644     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("Reset"));
645     WKRetainPtr<WKMutableDictionaryRef> resetMessageBody = adoptWK(WKMutableDictionaryCreate());
646
647     WKRetainPtr<WKStringRef> shouldGCKey = adoptWK(WKStringCreateWithUTF8CString("ShouldGC"));
648     WKRetainPtr<WKBooleanRef> shouldGCValue = adoptWK(WKBooleanCreate(m_gcBetweenTests));
649     WKDictionarySetItem(resetMessageBody.get(), shouldGCKey.get(), shouldGCValue.get());
650
651     WKRetainPtr<WKStringRef> allowedHostsKey = adoptWK(WKStringCreateWithUTF8CString("AllowedHosts"));
652     WKRetainPtr<WKMutableArrayRef> allowedHostsValue = adoptWK(WKMutableArrayCreate());
653     for (auto& host : m_allowedHosts) {
654         WKRetainPtr<WKStringRef> wkHost = adoptWK(WKStringCreateWithUTF8CString(host.c_str()));
655         WKArrayAppendItem(allowedHostsValue.get(), wkHost.get());
656     }
657     WKDictionarySetItem(resetMessageBody.get(), allowedHostsKey.get(), allowedHostsValue.get());
658
659     WKPagePostMessageToInjectedBundle(TestController::singleton().mainWebView()->page(), messageName.get(), resetMessageBody.get());
660
661     WKContextSetShouldUseFontSmoothing(TestController::singleton().context(), false);
662
663     WKContextSetCacheModel(TestController::singleton().context(), kWKCacheModelDocumentBrowser);
664
665     // FIXME: This function should also ensure that there is only one page open.
666
667     // Reset the EventSender for each test.
668     m_eventSenderProxy = std::make_unique<EventSenderProxy>(this);
669
670     // FIXME: Is this needed? Nothing in TestController changes preferences during tests, and if there is
671     // some other code doing this, it should probably be responsible for cleanup too.
672     resetPreferencesToConsistentValues();
673
674 #if !PLATFORM(COCOA)
675     WKTextCheckerContinuousSpellCheckingEnabledStateChanged(true);
676 #endif
677
678     // In the case that a test using the chrome input field failed, be sure to clean up for the next test.
679     m_mainWebView->removeChromeInputField();
680     m_mainWebView->focus();
681
682     // Re-set to the default backing scale factor by setting the custom scale factor to 0.
683     WKPageSetCustomBackingScaleFactor(m_mainWebView->page(), 0);
684
685     WKPageClearWheelEventTestTrigger(m_mainWebView->page());
686
687 #if PLATFORM(EFL)
688     // EFL use a real window while other ports such as Qt don't.
689     // In EFL, we need to resize the window to the original size after calls to window.resizeTo.
690     WKRect rect = m_mainWebView->windowFrame();
691     m_mainWebView->setWindowFrame(WKRectMake(rect.origin.x, rect.origin.y, TestController::viewWidth, TestController::viewHeight));
692 #endif
693
694     // Reset notification permissions
695     m_webNotificationProvider.reset();
696
697     // Reset Geolocation permissions.
698     m_geolocationPermissionRequests.clear();
699     m_isGeolocationPermissionSet = false;
700     m_isGeolocationPermissionAllowed = false;
701
702     // Reset UserMedia permissions.
703     m_userMediaPermissionRequests.clear();
704     m_isUserMediaPermissionSet = false;
705     m_isUserMediaPermissionAllowed = false;
706
707     // Reset Custom Policy Delegate.
708     setCustomPolicyDelegate(false, false);
709
710     m_workQueueManager.clearWorkQueue();
711
712     m_handlesAuthenticationChallenges = false;
713     m_authenticationUsername = String();
714     m_authenticationPassword = String();
715
716     m_shouldBlockAllPlugins = false;
717
718     m_shouldLogHistoryClientCallbacks = false;
719
720     setHidden(false);
721
722     platformResetStateToConsistentValues();
723
724     // Reset main page back to about:blank
725     m_doneResetting = false;
726
727     WKPageLoadURL(m_mainWebView->page(), blankURL());
728     runUntil(m_doneResetting, shortTimeout);
729     return m_doneResetting;
730 }
731
732 void TestController::terminateWebContentProcess()
733 {
734     WKPageTerminate(m_mainWebView->page());
735 }
736
737 void TestController::reattachPageToWebProcess()
738 {
739     // Loading a web page is the only way to reattach an existing page to a process.
740     m_doneResetting = false;
741     WKPageLoadURL(m_mainWebView->page(), blankURL());
742     runUntil(m_doneResetting, shortTimeout);
743 }
744
745 const char* TestController::webProcessName()
746 {
747     // FIXME: Find a way to not hardcode the process name.
748 #if PLATFORM(COCOA)
749     return "com.apple.WebKit.WebContent.Development";
750 #else
751     return "WebProcess";
752 #endif
753 }
754
755 const char* TestController::networkProcessName()
756 {
757     // FIXME: Find a way to not hardcode the process name.
758 #if PLATFORM(COCOA)
759     return "com.apple.WebKit.Networking.Development";
760 #else
761     return "NetworkProcess";
762 #endif
763 }
764
765 static bool shouldUseFixedLayout(const TestInvocation& test)
766 {
767 #if ENABLE(CSS_DEVICE_ADAPTATION)
768         if (test.urlContains("device-adapt/") || test.urlContains("device-adapt\\"))
769             return true;
770 #endif
771
772     return false;
773 }
774
775 ViewOptions TestController::viewOptionsForTest(const TestInvocation& test) const
776 {
777     ViewOptions viewOptions;
778
779     viewOptions.useRemoteLayerTree = m_shouldUseRemoteLayerTree;
780     viewOptions.shouldShowWebView = m_shouldShowWebView;
781     viewOptions.useFixedLayout = shouldUseFixedLayout(test);
782
783     updatePlatformSpecificViewOptionsForTest(viewOptions, test);
784
785     return viewOptions;
786 }
787
788 void TestController::updateWebViewSizeForTest(const TestInvocation& test)
789 {
790     bool isSVGW3CTest = test.urlContains("svg/W3C-SVG-1.1") || test.urlContains("svg\\W3C-SVG-1.1");
791
792     unsigned width = viewWidth;
793     unsigned height = viewHeight;
794     if (isSVGW3CTest) {
795         width = w3cSVGViewWidth;
796         height = w3cSVGViewHeight;
797     }
798
799     mainWebView()->resizeTo(width, height);
800 }
801
802 void TestController::updateWindowScaleForTest(PlatformWebView* view, const TestInvocation& test)
803 {
804     bool needsHighDPIWindow = test.urlContains("/hidpi-");
805     view->changeWindowScaleIfNeeded(needsHighDPIWindow ? 2 : 1);
806 }
807
808 void TestController::configureViewForTest(const TestInvocation& test)
809 {
810     ensureViewSupportsOptionsForTest(test);
811     updateWebViewSizeForTest(test);
812     updateWindowScaleForTest(mainWebView(), test);
813
814     platformConfigureViewForTest(test);
815 }
816
817 struct TestCommand {
818     TestCommand() : shouldDumpPixels(false), timeout(0) { }
819
820     std::string pathOrURL;
821     bool shouldDumpPixels;
822     std::string expectedPixelHash;
823     int timeout;
824 };
825
826 class CommandTokenizer {
827 public:
828     explicit CommandTokenizer(const std::string& input)
829         : m_input(input)
830         , m_posNextSeparator(0)
831     {
832         pump();
833     }
834
835     bool hasNext() const;
836     std::string next();
837
838 private:
839     void pump();
840     static const char kSeparator = '\'';
841     const std::string& m_input;
842     std::string m_next;
843     size_t m_posNextSeparator;
844 };
845
846 void CommandTokenizer::pump()
847 {
848     if (m_posNextSeparator == std::string::npos || m_posNextSeparator == m_input.size()) {
849         m_next = std::string();
850         return;
851     }
852     size_t start = m_posNextSeparator ? m_posNextSeparator + 1 : 0;
853     m_posNextSeparator = m_input.find(kSeparator, start);
854     size_t size = m_posNextSeparator == std::string::npos ? std::string::npos : m_posNextSeparator - start;
855     m_next = std::string(m_input, start, size);
856 }
857
858 std::string CommandTokenizer::next()
859 {
860     ASSERT(hasNext());
861
862     std::string oldNext = m_next;
863     pump();
864     return oldNext;
865 }
866
867 bool CommandTokenizer::hasNext() const
868 {
869     return !m_next.empty();
870 }
871
872 NO_RETURN static void die(const std::string& inputLine)
873 {
874     fprintf(stderr, "Unexpected input line: %s\n", inputLine.c_str());
875     exit(1);
876 }
877
878 TestCommand parseInputLine(const std::string& inputLine)
879 {
880     TestCommand result;
881     CommandTokenizer tokenizer(inputLine);
882     if (!tokenizer.hasNext())
883         die(inputLine);
884
885     std::string arg = tokenizer.next();
886     result.pathOrURL = arg;
887     while (tokenizer.hasNext()) {
888         arg = tokenizer.next();
889         if (arg == std::string("--timeout")) {
890             std::string timeoutToken = tokenizer.next();
891             result.timeout = atoi(timeoutToken.c_str());
892         } else if (arg == std::string("-p") || arg == std::string("--pixel-test")) {
893             result.shouldDumpPixels = true;
894             if (tokenizer.hasNext())
895                 result.expectedPixelHash = tokenizer.next();
896         } else
897             die(inputLine);
898     }
899     return result;
900 }
901
902 bool TestController::runTest(const char* inputLine)
903 {
904     TestCommand command = parseInputLine(std::string(inputLine));
905
906     m_state = RunningTest;
907
908     m_currentInvocation = std::make_unique<TestInvocation>(command.pathOrURL);
909     if (command.shouldDumpPixels || m_shouldDumpPixelsForAllTests)
910         m_currentInvocation->setIsPixelTest(command.expectedPixelHash);
911     if (command.timeout > 0)
912         m_currentInvocation->setCustomTimeout(command.timeout);
913
914     platformWillRunTest(*m_currentInvocation);
915
916     m_currentInvocation->invoke();
917     m_currentInvocation = nullptr;
918
919     return true;
920 }
921
922 void TestController::runTestingServerLoop()
923 {
924     char filenameBuffer[2048];
925     while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
926         char* newLineCharacter = strchr(filenameBuffer, '\n');
927         if (newLineCharacter)
928             *newLineCharacter = '\0';
929
930         if (strlen(filenameBuffer) == 0)
931             continue;
932
933         if (!runTest(filenameBuffer))
934             break;
935     }
936 }
937
938 void TestController::run()
939 {
940     if (m_usingServerMode)
941         runTestingServerLoop();
942     else {
943         for (size_t i = 0; i < m_paths.size(); ++i) {
944             if (!runTest(m_paths[i].c_str()))
945                 break;
946         }
947     }
948 }
949
950 void TestController::runUntil(bool& done, double timeout)
951 {
952     if (m_forceNoTimeout)
953         timeout = noTimeout;
954
955     platformRunUntil(done, timeout);
956 }
957
958 // WKContextInjectedBundleClient
959
960 void TestController::didReceiveMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
961 {
962     static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveMessageFromInjectedBundle(messageName, messageBody);
963 }
964
965 void TestController::didReceiveSynchronousMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void* clientInfo)
966 {
967     *returnData = static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody).leakRef();
968 }
969
970 // WKPageInjectedBundleClient
971
972 void TestController::didReceivePageMessageFromInjectedBundle(WKPageRef page, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
973 {
974     static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveMessageFromInjectedBundle(messageName, messageBody);
975 }
976
977 void TestController::didReceiveSynchronousPageMessageFromInjectedBundle(WKPageRef page, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void* clientInfo)
978 {
979     *returnData = static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody).leakRef();
980 }
981
982 void TestController::networkProcessDidCrash(WKContextRef context, const void *clientInfo)
983 {
984     static_cast<TestController*>(const_cast<void*>(clientInfo))->networkProcessDidCrash();
985 }
986
987 void TestController::didReceiveKeyDownMessageFromInjectedBundle(WKDictionaryRef messageBodyDictionary, bool synchronous)
988 {
989     WKRetainPtr<WKStringRef> keyKey = adoptWK(WKStringCreateWithUTF8CString("Key"));
990     WKStringRef key = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, keyKey.get()));
991
992     WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
993     WKEventModifiers modifiers = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifiersKey.get()))));
994
995     WKRetainPtr<WKStringRef> locationKey = adoptWK(WKStringCreateWithUTF8CString("Location"));
996     unsigned location = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, locationKey.get()))));
997
998     m_eventSenderProxy->keyDown(key, modifiers, location);
999 }
1000
1001 void TestController::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
1002 {
1003     if (WKStringIsEqualToUTF8CString(messageName, "EventSender")) {
1004         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1005         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1006
1007         WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
1008         WKStringRef subMessageName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, subMessageKey.get()));
1009
1010         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseDown") || WKStringIsEqualToUTF8CString(subMessageName, "MouseUp")) {
1011             WKRetainPtr<WKStringRef> buttonKey = adoptWK(WKStringCreateWithUTF8CString("Button"));
1012             unsigned button = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, buttonKey.get()))));
1013
1014             WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
1015             WKEventModifiers modifiers = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifiersKey.get()))));
1016
1017             // Forward to WebProcess
1018             if (WKStringIsEqualToUTF8CString(subMessageName, "MouseDown"))
1019                 m_eventSenderProxy->mouseDown(button, modifiers);
1020             else
1021                 m_eventSenderProxy->mouseUp(button, modifiers);
1022
1023             return;
1024         }
1025
1026         if (WKStringIsEqualToUTF8CString(subMessageName, "KeyDown")) {
1027             didReceiveKeyDownMessageFromInjectedBundle(messageBodyDictionary, false);
1028
1029             return;
1030         }
1031
1032         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseScrollBy")) {
1033             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
1034             double x = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get())));
1035
1036             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
1037             double y = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get())));
1038
1039             // Forward to WebProcess
1040             m_eventSenderProxy->mouseScrollBy(x, y);
1041             return;
1042         }
1043
1044         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseScrollByWithWheelAndMomentumPhases")) {
1045             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
1046             double x = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get())));
1047             
1048             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
1049             double y = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get())));
1050             
1051             WKRetainPtr<WKStringRef> phaseKey = adoptWK(WKStringCreateWithUTF8CString("Phase"));
1052             int phase = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, phaseKey.get()))));
1053             WKRetainPtr<WKStringRef> momentumKey = adoptWK(WKStringCreateWithUTF8CString("Momentum"));
1054             int momentum = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, momentumKey.get()))));
1055             
1056             // Forward to WebProcess
1057             m_eventSenderProxy->mouseScrollByWithWheelAndMomentumPhases(x, y, phase, momentum);
1058
1059             return;
1060         }
1061
1062         ASSERT_NOT_REACHED();
1063     }
1064
1065     if (!m_currentInvocation)
1066         return;
1067
1068     m_currentInvocation->didReceiveMessageFromInjectedBundle(messageName, messageBody);
1069 }
1070
1071 WKRetainPtr<WKTypeRef> TestController::didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
1072 {
1073     if (WKStringIsEqualToUTF8CString(messageName, "EventSender")) {
1074         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
1075         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
1076
1077         WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
1078         WKStringRef subMessageName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, subMessageKey.get()));
1079
1080         if (WKStringIsEqualToUTF8CString(subMessageName, "KeyDown")) {
1081             didReceiveKeyDownMessageFromInjectedBundle(messageBodyDictionary, true);
1082
1083             return 0;
1084         }
1085
1086         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseDown") || WKStringIsEqualToUTF8CString(subMessageName, "MouseUp")) {
1087             WKRetainPtr<WKStringRef> buttonKey = adoptWK(WKStringCreateWithUTF8CString("Button"));
1088             unsigned button = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, buttonKey.get()))));
1089
1090             WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
1091             WKEventModifiers modifiers = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifiersKey.get()))));
1092
1093             // Forward to WebProcess
1094             if (WKStringIsEqualToUTF8CString(subMessageName, "MouseDown"))
1095                 m_eventSenderProxy->mouseDown(button, modifiers);
1096             else
1097                 m_eventSenderProxy->mouseUp(button, modifiers);
1098             return 0;
1099         }
1100
1101         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseMoveTo")) {
1102             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
1103             double x = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get())));
1104
1105             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
1106             double y = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get())));
1107
1108             // Forward to WebProcess
1109             m_eventSenderProxy->mouseMoveTo(x, y);
1110             return 0;
1111         }
1112
1113 #if PLATFORM(MAC)
1114         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseForceDown")) {
1115             m_eventSenderProxy->mouseForceDown();
1116             return 0;
1117         }
1118
1119         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseForceUp")) {
1120             m_eventSenderProxy->mouseForceUp();
1121             return 0;
1122         }
1123
1124         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseForceChanged")) {
1125             WKRetainPtr<WKStringRef> forceKey = adoptWK(WKStringCreateWithUTF8CString("Force"));
1126             double force = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, forceKey.get())));
1127
1128             m_eventSenderProxy->mouseForceChanged(force);
1129             return 0;
1130         }
1131 #endif // PLATFORM(MAC)
1132
1133         if (WKStringIsEqualToUTF8CString(subMessageName, "ContinuousMouseScrollBy")) {
1134             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
1135             double x = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get())));
1136
1137             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
1138             double y = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get())));
1139
1140             WKRetainPtr<WKStringRef> pagedKey = adoptWK(WKStringCreateWithUTF8CString("Paged"));
1141             bool paged = static_cast<bool>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, pagedKey.get()))));
1142
1143             // Forward to WebProcess
1144             m_eventSenderProxy->continuousMouseScrollBy(x, y, paged);
1145             return 0;
1146         }
1147
1148         if (WKStringIsEqualToUTF8CString(subMessageName, "LeapForward")) {
1149             WKRetainPtr<WKStringRef> timeKey = adoptWK(WKStringCreateWithUTF8CString("TimeInMilliseconds"));
1150             unsigned time = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, timeKey.get()))));
1151
1152             m_eventSenderProxy->leapForward(time);
1153             return 0;
1154         }
1155
1156 #if ENABLE(TOUCH_EVENTS)
1157         if (WKStringIsEqualToUTF8CString(subMessageName, "AddTouchPoint")) {
1158             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
1159             int x = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get()))));
1160
1161             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
1162             int y = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get()))));
1163
1164             m_eventSenderProxy->addTouchPoint(x, y);
1165             return 0;
1166         }
1167
1168         if (WKStringIsEqualToUTF8CString(subMessageName, "UpdateTouchPoint")) {
1169             WKRetainPtr<WKStringRef> indexKey = adoptWK(WKStringCreateWithUTF8CString("Index"));
1170             int index = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, indexKey.get()))));
1171
1172             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
1173             int x = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get()))));
1174
1175             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
1176             int y = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get()))));
1177
1178             m_eventSenderProxy->updateTouchPoint(index, x, y);
1179             return 0;
1180         }
1181
1182         if (WKStringIsEqualToUTF8CString(subMessageName, "SetTouchModifier")) {
1183             WKRetainPtr<WKStringRef> modifierKey = adoptWK(WKStringCreateWithUTF8CString("Modifier"));
1184             WKEventModifiers modifier = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifierKey.get()))));
1185
1186             WKRetainPtr<WKStringRef> enableKey = adoptWK(WKStringCreateWithUTF8CString("Enable"));
1187             bool enable = static_cast<bool>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, enableKey.get()))));
1188
1189             m_eventSenderProxy->setTouchModifier(modifier, enable);
1190             return 0;
1191         }
1192
1193         if (WKStringIsEqualToUTF8CString(subMessageName, "SetTouchPointRadius")) {
1194             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("RadiusX"));
1195             int x = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get()))));
1196
1197             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("RadiusY"));
1198             int y = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get()))));
1199
1200             m_eventSenderProxy->setTouchPointRadius(x, y);
1201             return 0;
1202         }
1203
1204         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchStart")) {
1205             m_eventSenderProxy->touchStart();
1206             return 0;
1207         }
1208
1209         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchMove")) {
1210             m_eventSenderProxy->touchMove();
1211             return 0;
1212         }
1213
1214         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchEnd")) {
1215             m_eventSenderProxy->touchEnd();
1216             return 0;
1217         }
1218
1219         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchCancel")) {
1220             m_eventSenderProxy->touchCancel();
1221             return 0;
1222         }
1223
1224         if (WKStringIsEqualToUTF8CString(subMessageName, "ClearTouchPoints")) {
1225             m_eventSenderProxy->clearTouchPoints();
1226             return 0;
1227         }
1228
1229         if (WKStringIsEqualToUTF8CString(subMessageName, "ReleaseTouchPoint")) {
1230             WKRetainPtr<WKStringRef> indexKey = adoptWK(WKStringCreateWithUTF8CString("Index"));
1231             int index = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, indexKey.get()))));
1232             m_eventSenderProxy->releaseTouchPoint(index);
1233             return 0;
1234         }
1235
1236         if (WKStringIsEqualToUTF8CString(subMessageName, "CancelTouchPoint")) {
1237             WKRetainPtr<WKStringRef> indexKey = adoptWK(WKStringCreateWithUTF8CString("Index"));
1238             int index = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, indexKey.get()))));
1239             m_eventSenderProxy->cancelTouchPoint(index);
1240             return 0;
1241         }
1242 #endif
1243         ASSERT_NOT_REACHED();
1244     }
1245     return m_currentInvocation->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody);
1246 }
1247
1248 // WKContextClient
1249
1250 void TestController::networkProcessDidCrash()
1251 {
1252 #if PLATFORM(COCOA)
1253     pid_t pid = WKContextGetNetworkProcessIdentifier(m_context.get());
1254     fprintf(stderr, "#CRASHED - %s (pid %ld)\n", networkProcessName(), static_cast<long>(pid));
1255 #else
1256     fprintf(stderr, "#CRASHED - %s\n", networkProcessName());
1257 #endif
1258     exit(1);
1259 }
1260
1261 // WKPageNavigationClient
1262
1263 void TestController::didCommitNavigation(WKPageRef page, WKNavigationRef navigation, WKTypeRef, const void* clientInfo)
1264 {
1265     static_cast<TestController*>(const_cast<void*>(clientInfo))->didCommitNavigation(page, navigation);
1266 }
1267
1268 void TestController::didFinishNavigation(WKPageRef page, WKNavigationRef navigation, WKTypeRef, const void* clientInfo)
1269 {
1270     static_cast<TestController*>(const_cast<void*>(clientInfo))->didFinishNavigation(page, navigation);
1271 }
1272
1273 bool TestController::canAuthenticateAgainstProtectionSpace(WKPageRef, WKProtectionSpaceRef protectionSpace, const void*)
1274 {
1275     WKProtectionSpaceAuthenticationScheme authenticationScheme = WKProtectionSpaceGetAuthenticationScheme(protectionSpace);
1276
1277     if (authenticationScheme == kWKProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested) {
1278         std::string host = toSTD(adoptWK(WKProtectionSpaceCopyHost(protectionSpace)).get());
1279         return host == "localhost" || host == "127.0.0.1";
1280     }
1281
1282     return authenticationScheme <= kWKProtectionSpaceAuthenticationSchemeHTTPDigest;
1283 }
1284
1285 void TestController::didReceiveAuthenticationChallenge(WKPageRef page, WKAuthenticationChallengeRef authenticationChallenge, const void *clientInfo)
1286 {
1287     static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveAuthenticationChallenge(page, /*frame,*/ authenticationChallenge);
1288 }
1289
1290 void TestController::processDidCrash(WKPageRef page, const void* clientInfo)
1291 {
1292     static_cast<TestController*>(const_cast<void*>(clientInfo))->processDidCrash();
1293 }
1294
1295 WKPluginLoadPolicy TestController::decidePolicyForPluginLoad(WKPageRef page, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription, const void* clientInfo)
1296 {
1297     return static_cast<TestController*>(const_cast<void*>(clientInfo))->decidePolicyForPluginLoad(page, currentPluginLoadPolicy, pluginInformation, unavailabilityDescription);
1298 }
1299
1300 WKPluginLoadPolicy TestController::decidePolicyForPluginLoad(WKPageRef, WKPluginLoadPolicy currentPluginLoadPolicy, WKDictionaryRef pluginInformation, WKStringRef* unavailabilityDescription)
1301 {
1302     if (m_shouldBlockAllPlugins)
1303         return kWKPluginLoadPolicyBlocked;
1304
1305 #if PLATFORM(MAC)
1306     WKStringRef bundleIdentifier = (WKStringRef)WKDictionaryGetItemForKey(pluginInformation, WKPluginInformationBundleIdentifierKey());
1307     if (!bundleIdentifier)
1308         return currentPluginLoadPolicy;
1309
1310     if (WKStringIsEqualToUTF8CString(bundleIdentifier, "com.apple.QuickTime Plugin.plugin"))
1311         return currentPluginLoadPolicy;
1312
1313     if (WKStringIsEqualToUTF8CString(bundleIdentifier, "com.apple.testnetscapeplugin"))
1314         return currentPluginLoadPolicy;
1315
1316     RELEASE_ASSERT_NOT_REACHED(); // Please don't use any other plug-ins in tests, as they will not be installed on all machines.
1317 #else
1318     return currentPluginLoadPolicy;
1319 #endif
1320 }
1321
1322 void TestController::didCommitNavigation(WKPageRef page, WKNavigationRef navigation)
1323 {
1324     mainWebView()->focus();
1325 }
1326
1327 void TestController::didFinishNavigation(WKPageRef page, WKNavigationRef navigation)
1328 {
1329     if (m_state != Resetting)
1330         return;
1331
1332     WKRetainPtr<WKURLRef> wkURL(AdoptWK, WKFrameCopyURL(WKPageGetMainFrame(page)));
1333     if (!WKURLIsEqual(wkURL.get(), blankURL()))
1334         return;
1335
1336     m_doneResetting = true;
1337     singleton().notifyDone();
1338 }
1339
1340 void TestController::didReceiveAuthenticationChallenge(WKPageRef page, WKAuthenticationChallengeRef authenticationChallenge)
1341 {
1342     WKProtectionSpaceRef protectionSpace = WKAuthenticationChallengeGetProtectionSpace(authenticationChallenge);
1343     WKAuthenticationDecisionListenerRef decisionListener = WKAuthenticationChallengeGetDecisionListener(authenticationChallenge);
1344
1345     if (WKProtectionSpaceGetAuthenticationScheme(protectionSpace) == kWKProtectionSpaceAuthenticationSchemeServerTrustEvaluationRequested) {
1346         // Any non-empty credential signals to accept the server trust. Since the cross-platform API
1347         // doesn't expose a way to create a credential from server trust, we use a password credential.
1348
1349         WKRetainPtr<WKCredentialRef> credential = adoptWK(WKCredentialCreate(toWK("accept server trust").get(), toWK("").get(), kWKCredentialPersistenceNone));
1350         WKAuthenticationDecisionListenerUseCredential(decisionListener, credential.get());
1351         return;
1352     }
1353
1354     std::string host = toSTD(adoptWK(WKProtectionSpaceCopyHost(protectionSpace)).get());
1355     int port = WKProtectionSpaceGetPort(protectionSpace);
1356     String message = String::format("%s:%d - didReceiveAuthenticationChallenge - ", host.c_str(), port);
1357     if (!m_handlesAuthenticationChallenges)
1358         message.append("Simulating cancelled authentication sheet\n");
1359     else
1360         message.append(String::format("Responding with %s:%s\n", m_authenticationUsername.utf8().data(), m_authenticationPassword.utf8().data()));
1361     m_currentInvocation->outputText(message);
1362
1363     if (!m_handlesAuthenticationChallenges) {
1364         WKAuthenticationDecisionListenerUseCredential(decisionListener, 0);
1365         return;
1366     }
1367     WKRetainPtr<WKStringRef> username(AdoptWK, WKStringCreateWithUTF8CString(m_authenticationUsername.utf8().data()));
1368     WKRetainPtr<WKStringRef> password(AdoptWK, WKStringCreateWithUTF8CString(m_authenticationPassword.utf8().data()));
1369     WKRetainPtr<WKCredentialRef> credential(AdoptWK, WKCredentialCreate(username.get(), password.get(), kWKCredentialPersistenceForSession));
1370     WKAuthenticationDecisionListenerUseCredential(decisionListener, credential.get());
1371 }
1372
1373 void TestController::processDidCrash()
1374 {
1375     // This function can be called multiple times when crash logs are being saved on Windows, so
1376     // ensure we only print the crashed message once.
1377     if (!m_didPrintWebProcessCrashedMessage) {
1378 #if PLATFORM(COCOA)
1379         pid_t pid = WKPageGetProcessIdentifier(m_mainWebView->page());
1380         fprintf(stderr, "#CRASHED - %s (pid %ld)\n", webProcessName(), static_cast<long>(pid));
1381 #else
1382         fprintf(stderr, "#CRASHED - %s\n", webProcessName());
1383 #endif
1384         fflush(stderr);
1385         m_didPrintWebProcessCrashedMessage = true;
1386     }
1387
1388     if (m_shouldExitWhenWebProcessCrashes)
1389         exit(1);
1390 }
1391
1392 void TestController::simulateWebNotificationClick(uint64_t notificationID)
1393 {
1394     m_webNotificationProvider.simulateWebNotificationClick(notificationID);
1395 }
1396
1397 void TestController::setGeolocationPermission(bool enabled)
1398 {
1399     m_isGeolocationPermissionSet = true;
1400     m_isGeolocationPermissionAllowed = enabled;
1401     decidePolicyForGeolocationPermissionRequestIfPossible();
1402 }
1403
1404 void TestController::setMockGeolocationPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed)
1405 {
1406     m_geolocationProvider->setPosition(latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed);
1407 }
1408
1409 void TestController::setMockGeolocationPositionUnavailableError(WKStringRef errorMessage)
1410 {
1411     m_geolocationProvider->setPositionUnavailableError(errorMessage);
1412 }
1413
1414 void TestController::handleGeolocationPermissionRequest(WKGeolocationPermissionRequestRef geolocationPermissionRequest)
1415 {
1416     m_geolocationPermissionRequests.append(geolocationPermissionRequest);
1417     decidePolicyForGeolocationPermissionRequestIfPossible();
1418 }
1419
1420 bool TestController::isGeolocationProviderActive() const
1421 {
1422     return m_geolocationProvider->isActive();
1423 }
1424
1425 void TestController::setUserMediaPermission(bool enabled)
1426 {
1427     m_isUserMediaPermissionSet = true;
1428     m_isUserMediaPermissionAllowed = enabled;
1429     decidePolicyForUserMediaPermissionRequestIfPossible();
1430 }
1431
1432 void TestController::handleUserMediaPermissionRequest(WKUserMediaPermissionRequestRef userMediaPermissionRequest)
1433 {
1434     m_userMediaPermissionRequests.append(userMediaPermissionRequest);
1435     decidePolicyForUserMediaPermissionRequestIfPossible();
1436 }
1437
1438 void TestController::decidePolicyForUserMediaPermissionRequestIfPossible()
1439 {
1440     if (!m_isUserMediaPermissionSet)
1441         return;
1442
1443     for (auto& request : m_userMediaPermissionRequests) {
1444         if (m_isUserMediaPermissionAllowed) {
1445             if (WKArrayGetSize(WKUserMediaPermissionRequestDeviceNamesVideo(request.get())) || WKArrayGetSize(WKUserMediaPermissionRequestDeviceNamesAudio(request.get())))
1446                 WKUserMediaPermissionRequestAllow(request.get(), WKUserMediaPermissionRequestFirstVideoDeviceUID(request.get()), WKUserMediaPermissionRequestFirstAudioDeviceUID(request.get()));
1447         } else
1448             WKUserMediaPermissionRequestDeny(request.get());
1449     }
1450     m_userMediaPermissionRequests.clear();
1451 }
1452
1453 void TestController::setCustomPolicyDelegate(bool enabled, bool permissive)
1454 {
1455     m_policyDelegateEnabled = enabled;
1456     m_policyDelegatePermissive = permissive;
1457 }
1458
1459 void TestController::decidePolicyForGeolocationPermissionRequestIfPossible()
1460 {
1461     if (!m_isGeolocationPermissionSet)
1462         return;
1463
1464     for (size_t i = 0; i < m_geolocationPermissionRequests.size(); ++i) {
1465         WKGeolocationPermissionRequestRef permissionRequest = m_geolocationPermissionRequests[i].get();
1466         if (m_isGeolocationPermissionAllowed)
1467             WKGeolocationPermissionRequestAllow(permissionRequest);
1468         else
1469             WKGeolocationPermissionRequestDeny(permissionRequest);
1470     }
1471     m_geolocationPermissionRequests.clear();
1472 }
1473
1474 void TestController::decidePolicyForNotificationPermissionRequest(WKPageRef page, WKSecurityOriginRef origin, WKNotificationPermissionRequestRef request, const void*)
1475 {
1476     TestController::singleton().decidePolicyForNotificationPermissionRequest(page, origin, request);
1477 }
1478
1479 void TestController::decidePolicyForNotificationPermissionRequest(WKPageRef, WKSecurityOriginRef, WKNotificationPermissionRequestRef request)
1480 {
1481     WKNotificationPermissionRequestAllow(request);
1482 }
1483
1484 void TestController::unavailablePluginButtonClicked(WKPageRef, WKPluginUnavailabilityReason, WKDictionaryRef, const void*)
1485 {
1486     printf("MISSING PLUGIN BUTTON PRESSED\n");
1487 }
1488
1489 void TestController::decidePolicyForNavigationAction(WKPageRef, WKNavigationActionRef navigationAction, WKFramePolicyListenerRef listener, WKTypeRef, const void* clientInfo)
1490 {
1491     static_cast<TestController*>(const_cast<void*>(clientInfo))->decidePolicyForNavigationAction(listener);
1492 }
1493
1494 void TestController::decidePolicyForNavigationAction(WKFramePolicyListenerRef listener)
1495 {
1496     if (m_policyDelegateEnabled && !m_policyDelegatePermissive) {
1497         WKFramePolicyListenerIgnore(listener);
1498         return;
1499     }
1500
1501     WKFramePolicyListenerUse(listener);
1502 }
1503
1504 void TestController::decidePolicyForNavigationResponse(WKPageRef, WKNavigationResponseRef navigationResponse, WKFramePolicyListenerRef listener, WKTypeRef, const void* clientInfo)
1505 {
1506     static_cast<TestController*>(const_cast<void*>(clientInfo))->decidePolicyForNavigationResponse(navigationResponse, listener);
1507 }
1508
1509 void TestController::decidePolicyForNavigationResponse(WKNavigationResponseRef navigationResponse, WKFramePolicyListenerRef listener)
1510 {
1511     // Even though Response was already checked by WKBundlePagePolicyClient, the check did not include plugins
1512     // so we have to re-check again.
1513     if (WKNavigationResponseCanShowMIMEType(navigationResponse)) {
1514         WKFramePolicyListenerUse(listener);
1515         return;
1516     }
1517
1518     WKFramePolicyListenerIgnore(listener);
1519 }
1520
1521 void TestController::didNavigateWithNavigationData(WKContextRef, WKPageRef, WKNavigationDataRef navigationData, WKFrameRef frame, const void* clientInfo)
1522 {
1523     static_cast<TestController*>(const_cast<void*>(clientInfo))->didNavigateWithNavigationData(navigationData, frame);
1524 }
1525
1526 void TestController::didNavigateWithNavigationData(WKNavigationDataRef navigationData, WKFrameRef)
1527 {
1528     if (m_state != RunningTest)
1529         return;
1530
1531     if (!m_shouldLogHistoryClientCallbacks)
1532         return;
1533
1534     // URL
1535     WKRetainPtr<WKURLRef> urlWK = adoptWK(WKNavigationDataCopyURL(navigationData));
1536     WKRetainPtr<WKStringRef> urlStringWK = adoptWK(WKURLCopyString(urlWK.get()));
1537     // Title
1538     WKRetainPtr<WKStringRef> titleWK = adoptWK(WKNavigationDataCopyTitle(navigationData));
1539     // HTTP method
1540     WKRetainPtr<WKURLRequestRef> requestWK = adoptWK(WKNavigationDataCopyOriginalRequest(navigationData));
1541     WKRetainPtr<WKStringRef> methodWK = adoptWK(WKURLRequestCopyHTTPMethod(requestWK.get()));
1542
1543     // FIXME: Determine whether the navigation was successful / a client redirect rather than hard-coding the message here.
1544     m_currentInvocation->outputText(String::format("WebView navigated to url \"%s\" with title \"%s\" with HTTP equivalent method \"%s\".  The navigation was successful and was not a client redirect.\n",
1545         toSTD(urlStringWK).c_str(), toSTD(titleWK).c_str(), toSTD(methodWK).c_str()));
1546 }
1547
1548 void TestController::didPerformClientRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef frame, const void* clientInfo)
1549 {
1550     static_cast<TestController*>(const_cast<void*>(clientInfo))->didPerformClientRedirect(sourceURL, destinationURL, frame);
1551 }
1552
1553 void TestController::didPerformClientRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef)
1554 {
1555     if (m_state != RunningTest)
1556         return;
1557
1558     if (!m_shouldLogHistoryClientCallbacks)
1559         return;
1560
1561     WKRetainPtr<WKStringRef> sourceStringWK = adoptWK(WKURLCopyString(sourceURL));
1562     WKRetainPtr<WKStringRef> destinationStringWK = adoptWK(WKURLCopyString(destinationURL));
1563
1564     m_currentInvocation->outputText(String::format("WebView performed a client redirect from \"%s\" to \"%s\".\n", toSTD(sourceStringWK).c_str(), toSTD(destinationStringWK).c_str()));
1565 }
1566
1567 void TestController::didPerformServerRedirect(WKContextRef, WKPageRef, WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef frame, const void* clientInfo)
1568 {
1569     static_cast<TestController*>(const_cast<void*>(clientInfo))->didPerformServerRedirect(sourceURL, destinationURL, frame);
1570 }
1571
1572 void TestController::didPerformServerRedirect(WKURLRef sourceURL, WKURLRef destinationURL, WKFrameRef)
1573 {
1574     if (m_state != RunningTest)
1575         return;
1576
1577     if (!m_shouldLogHistoryClientCallbacks)
1578         return;
1579
1580     WKRetainPtr<WKStringRef> sourceStringWK = adoptWK(WKURLCopyString(sourceURL));
1581     WKRetainPtr<WKStringRef> destinationStringWK = adoptWK(WKURLCopyString(destinationURL));
1582
1583     m_currentInvocation->outputText(String::format("WebView performed a server redirect from \"%s\" to \"%s\".\n", toSTD(sourceStringWK).c_str(), toSTD(destinationStringWK).c_str()));
1584 }
1585
1586 void TestController::didUpdateHistoryTitle(WKContextRef, WKPageRef, WKStringRef title, WKURLRef URL, WKFrameRef frame, const void* clientInfo)
1587 {
1588     static_cast<TestController*>(const_cast<void*>(clientInfo))->didUpdateHistoryTitle(title, URL, frame);
1589 }
1590
1591 void TestController::didUpdateHistoryTitle(WKStringRef title, WKURLRef URL, WKFrameRef)
1592 {
1593     if (m_state != RunningTest)
1594         return;
1595
1596     if (!m_shouldLogHistoryClientCallbacks)
1597         return;
1598
1599     WKRetainPtr<WKStringRef> urlStringWK(AdoptWK, WKURLCopyString(URL));
1600     m_currentInvocation->outputText(String::format("WebView updated the title for history URL \"%s\" to \"%s\".\n", toSTD(urlStringWK).c_str(), toSTD(title).c_str()));
1601 }
1602
1603 #if !PLATFORM(COCOA)
1604 void TestController::platformWillRunTest(const TestInvocation&)
1605 {
1606 }
1607
1608 void TestController::platformCreateWebView(WKPageConfigurationRef configuration, const ViewOptions& options)
1609 {
1610     m_mainWebView = std::make_unique<PlatformWebView>(configuration, options);
1611 }
1612
1613 PlatformWebView* TestController::platformCreateOtherPage(PlatformWebView* parentView, WKPageConfigurationRef configuration, const ViewOptions& options)
1614 {
1615     return new PlatformWebView(configuration, options);
1616 }
1617
1618 WKContextRef TestController::platformAdjustContext(WKContextRef context, WKContextConfigurationRef contextConfiguration)
1619 {
1620     return context;
1621 }
1622
1623 void TestController::platformResetStateToConsistentValues()
1624 {
1625
1626 }
1627 #endif
1628
1629 } // namespace WTR