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