[Release] Webkit-EFL Ver. 2.0_beta_118996_0.6.24
[framework/web/webkit-efl.git] / Tools / WebKitTestRunner / TestController.cpp
1 /*
2  * Copyright (C) 2010 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 "PlatformWebView.h"
30 #include "StringFunctions.h"
31 #include "TestInvocation.h"
32 #include <WebKit2/WKContextPrivate.h>
33 #include <WebKit2/WKNumber.h>
34 #include <WebKit2/WKPageGroup.h>
35 #include <WebKit2/WKPagePrivate.h>
36 #include <WebKit2/WKPreferencesPrivate.h>
37 #include <WebKit2/WKRetainPtr.h>
38 #include <cstdio>
39 #include <wtf/PassOwnPtr.h>
40
41 #if PLATFORM(MAC)
42 #include <WebKit2/WKPagePrivateMac.h>
43 #endif
44
45 #if PLATFORM(MAC) || PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
46 #include "EventSenderProxy.h"
47 #endif
48
49 namespace WTR {
50
51 static const double defaultLongTimeout = 30;
52 static const double defaultShortTimeout = 15;
53 static const double defaultNoTimeout = -1;
54
55 static WKURLRef blankURL()
56 {
57     static WKURLRef staticBlankURL = WKURLCreateWithUTF8CString("about:blank");
58     return staticBlankURL;
59 }
60
61 static TestController* controller;
62
63 TestController& TestController::shared()
64 {
65     ASSERT(controller);
66     return *controller;
67 }
68
69 TestController::TestController(int argc, const char* argv[])
70     : m_dumpPixels(false)
71     , m_verbose(false)
72     , m_printSeparators(false)
73     , m_usingServerMode(false)
74     , m_gcBetweenTests(false)
75     , m_state(Initial)
76     , m_doneResetting(false)
77     , m_longTimeout(defaultLongTimeout)
78     , m_shortTimeout(defaultShortTimeout)
79     , m_noTimeout(defaultNoTimeout)
80     , m_useWaitToDumpWatchdogTimer(true)
81     , m_didPrintWebProcessCrashedMessage(false)
82     , m_shouldExitWhenWebProcessCrashes(true)
83     , m_beforeUnloadReturnValue(true)
84 #if PLATFORM(MAC) || PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
85     , m_eventSenderProxy(new EventSenderProxy(this))
86 #endif
87 {
88     initialize(argc, argv);
89     controller = this;
90     run();
91     controller = 0;
92 }
93
94 TestController::~TestController()
95 {
96 }
97
98 static WKRect getWindowFrameMainPage(WKPageRef page, const void* clientInfo)
99 {
100     PlatformWebView* view = static_cast<TestController*>(const_cast<void*>(clientInfo))->mainWebView();
101     return view->windowFrame();
102 }
103
104 static void setWindowFrameMainPage(WKPageRef page, WKRect frame, const void* clientInfo)
105 {
106     PlatformWebView* view = static_cast<TestController*>(const_cast<void*>(clientInfo))->mainWebView();
107     view->setWindowFrame(frame);
108 }
109
110 static WKRect getWindowFrameOtherPage(WKPageRef page, const void* clientInfo)
111 {
112     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
113     return view->windowFrame();
114 }
115
116 static void setWindowFrameOtherPage(WKPageRef page, WKRect frame, const void* clientInfo)
117 {
118     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
119     view->setWindowFrame(frame);
120 }
121
122 static bool runBeforeUnloadConfirmPanel(WKPageRef page, WKStringRef message, WKFrameRef frame, const void *clientInfo)
123 {
124     TestController* testController = static_cast<TestController*>(const_cast<void*>(clientInfo));
125     printf("CONFIRM NAVIGATION: %s\n", toSTD(message).c_str());
126     return testController->beforeUnloadReturnValue();
127 }
128
129 static unsigned long long exceededDatabaseQuota(WKPageRef, WKFrameRef, WKSecurityOriginRef, WKStringRef, WKStringRef, unsigned long long, unsigned long long, unsigned long long, unsigned long long, const void*)
130 {
131     static const unsigned long long defaultQuota = 5 * 1024 * 1024;
132     return defaultQuota;
133 }
134
135
136 void TestController::runModal(WKPageRef page, const void* clientInfo)
137 {
138     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
139     view->setWindowIsKey(false);
140     runModal(view);
141     view->setWindowIsKey(true);
142 }
143
144 static void closeOtherPage(WKPageRef page, const void* clientInfo)
145 {
146     WKPageClose(page);
147     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
148     delete view;
149 }
150
151 static void focus(WKPageRef page, const void* clientInfo)
152 {
153     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
154     view->setWindowIsKey(true);
155 }
156
157 static void unfocus(WKPageRef page, const void* clientInfo)
158 {
159     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
160     view->setWindowIsKey(false);
161 }
162
163 WKPageRef TestController::createOtherPage(WKPageRef oldPage, WKURLRequestRef, WKDictionaryRef, WKEventModifiers, WKEventMouseButton, const void*)
164 {
165     PlatformWebView* view = new PlatformWebView(WKPageGetContext(oldPage), WKPageGetPageGroup(oldPage));
166     WKPageRef newPage = view->page();
167
168     view->resizeTo(800, 600);
169
170     WKPageUIClient otherPageUIClient = {
171         kWKPageUIClientCurrentVersion,
172         view,
173         0, // createNewPage_deprecatedForUseWithV0
174         0, // showPage
175         closeOtherPage,
176         0, // takeFocus
177         focus,
178         unfocus,
179         0, // runJavaScriptAlert
180         0, // runJavaScriptConfirm
181         0, // runJavaScriptPrompt
182         0, // setStatusText
183         0, // mouseDidMoveOverElement_deprecatedForUseWithV0
184         0, // missingPluginButtonClicked
185         0, // didNotHandleKeyEvent
186         0, // didNotHandleWheelEvent
187         0, // toolbarsAreVisible
188         0, // setToolbarsAreVisible
189         0, // menuBarIsVisible
190         0, // setMenuBarIsVisible
191         0, // statusBarIsVisible
192         0, // setStatusBarIsVisible
193         0, // isResizable
194         0, // setIsResizable
195         getWindowFrameOtherPage,
196         setWindowFrameOtherPage,
197         runBeforeUnloadConfirmPanel,
198         0, // didDraw
199         0, // pageDidScroll
200         exceededDatabaseQuota,
201         0, // runOpenPanel
202         0, // decidePolicyForGeolocationPermissionRequest
203         0, // headerHeight
204         0, // footerHeight
205         0, // drawHeader
206         0, // drawFooter
207         0, // printFrame
208         runModal,
209         0, // didCompleteRubberBandForMainFrame
210         0, // saveDataToFileInDownloadsFolder
211         0, // shouldInterruptJavaScript
212         createOtherPage,
213         0, // mouseDidMoveOverElement
214         0, // decidePolicyForNotificationPermissionRequest
215         0, // unavailablePluginButtonClicked
216     };
217     WKPageSetPageUIClient(newPage, &otherPageUIClient);
218
219     WKRetain(newPage);
220     return newPage;
221 }
222
223 const char* TestController::libraryPathForTesting()
224 {
225     // FIXME: This may not be sufficient to prevent interactions/crashes
226     // when running more than one copy of DumpRenderTree.
227     // See https://bugs.webkit.org/show_bug.cgi?id=10906
228     char* dumpRenderTreeTemp = getenv("DUMPRENDERTREE_TEMP");
229     if (dumpRenderTreeTemp)
230         return dumpRenderTreeTemp;
231     return platformLibraryPathForTesting();
232 }
233
234
235 void TestController::initialize(int argc, const char* argv[])
236 {
237     platformInitialize();
238
239     if (argc < 2) {
240         fputs("Usage: WebKitTestRunner [options] filename [filename2..n]\n", stderr);
241         // FIXME: Refactor option parsing to allow us to print
242         // an auto-generated list of options.
243         exit(1);
244     }
245
246     bool printSupportedFeatures = false;
247
248     for (int i = 1; i < argc; ++i) {
249         std::string argument(argv[i]);
250
251         if (argument == "--timeout" && i + 1 < argc) {
252             m_longTimeout = atoi(argv[++i]);
253             // Scale up the short timeout to match.
254             m_shortTimeout = defaultShortTimeout * m_longTimeout / defaultLongTimeout;
255             continue;
256         }
257
258         if (argument == "--no-timeout") {
259             m_useWaitToDumpWatchdogTimer = false;
260             continue;
261         }
262
263         if (argument == "--pixel-tests") {
264             m_dumpPixels = true;
265             continue;
266         }
267         if (argument == "--verbose") {
268             m_verbose = true;
269             continue;
270         }
271         if (argument == "--gc-between-tests") {
272             m_gcBetweenTests = true;
273             continue;
274         }
275         if (argument == "--print-supported-features") {
276             printSupportedFeatures = true;
277             break;
278         }
279
280         // Skip any other arguments that begin with '--'.
281         if (argument.length() >= 2 && argument[0] == '-' && argument[1] == '-')
282             continue;
283
284         m_paths.push_back(argument);
285     }
286
287     if (printSupportedFeatures) {
288         // FIXME: On Windows, DumpRenderTree uses this to expose whether it supports 3d
289         // transforms and accelerated compositing. When we support those features, we
290         // should match DRT's behavior.
291         exit(0);
292     }
293
294     m_usingServerMode = (m_paths.size() == 1 && m_paths[0] == "-");
295     if (m_usingServerMode)
296         m_printSeparators = true;
297     else
298         m_printSeparators = m_paths.size() > 1;
299
300     initializeInjectedBundlePath();
301     initializeTestPluginDirectory();
302
303     WKRetainPtr<WKStringRef> pageGroupIdentifier(AdoptWK, WKStringCreateWithUTF8CString("WebKitTestRunnerPageGroup"));
304     m_pageGroup.adopt(WKPageGroupCreateWithIdentifier(pageGroupIdentifier.get()));
305
306     m_context.adopt(WKContextCreateWithInjectedBundlePath(injectedBundlePath()));
307
308     const char* path = libraryPathForTesting();
309     if (path) {
310         Vector<char> databaseDirectory(strlen(path) + strlen("/Databases") + 1);
311         sprintf(databaseDirectory.data(), "%s%s", path, "/Databases");
312         WKRetainPtr<WKStringRef> databaseDirectoryWK(AdoptWK, WKStringCreateWithUTF8CString(databaseDirectory.data()));
313         WKContextSetDatabaseDirectory(m_context.get(), databaseDirectoryWK.get());
314     }
315
316     platformInitializeContext();
317
318     WKContextInjectedBundleClient injectedBundleClient = {
319         kWKContextInjectedBundleClientCurrentVersion,
320         this,
321         didReceiveMessageFromInjectedBundle,
322         didReceiveSynchronousMessageFromInjectedBundle
323     };
324     WKContextSetInjectedBundleClient(m_context.get(), &injectedBundleClient);
325
326     WKContextSetAdditionalPluginsDirectory(m_context.get(), testPluginDirectory());
327
328     m_mainWebView = adoptPtr(new PlatformWebView(m_context.get(), m_pageGroup.get()));
329
330     WKPageUIClient pageUIClient = {
331         kWKPageUIClientCurrentVersion,
332         this,
333         0, // createNewPage_deprecatedForUseWithV0
334         0, // showPage
335         0, // close
336         0, // takeFocus
337         0, // focus
338         0, // unfocus
339         0, // runJavaScriptAlert
340         0, // runJavaScriptConfirm
341         0, // runJavaScriptPrompt
342         0, // setStatusText
343         0, // mouseDidMoveOverElement_deprecatedForUseWithV0
344         0, // missingPluginButtonClicked
345         0, // didNotHandleKeyEvent
346         0, // didNotHandleWheelEvent
347         0, // toolbarsAreVisible
348         0, // setToolbarsAreVisible
349         0, // menuBarIsVisible
350         0, // setMenuBarIsVisible
351         0, // statusBarIsVisible
352         0, // setStatusBarIsVisible
353         0, // isResizable
354         0, // setIsResizable
355         getWindowFrameMainPage,
356         setWindowFrameMainPage,
357         runBeforeUnloadConfirmPanel,
358         0, // didDraw
359         0, // pageDidScroll
360         exceededDatabaseQuota,
361         0, // runOpenPanel
362         0, // decidePolicyForGeolocationPermissionRequest
363         0, // headerHeight
364         0, // footerHeight
365         0, // drawHeader
366         0, // drawFooter
367         0, // printFrame
368         runModal,
369         0, // didCompleteRubberBandForMainFrame
370         0, // saveDataToFileInDownloadsFolder
371         0, // shouldInterruptJavaScript
372         createOtherPage,
373         0, // mouseDidMoveOverElement
374         0, // decidePolicyForNotificationPermissionRequest
375         0, // unavailablePluginButtonClicked
376     };
377     WKPageSetPageUIClient(m_mainWebView->page(), &pageUIClient);
378
379     WKPageLoaderClient pageLoaderClient = {
380         kWKPageLoaderClientCurrentVersion,
381         this,
382         0, // didStartProvisionalLoadForFrame
383         0, // didReceiveServerRedirectForProvisionalLoadForFrame
384         0, // didFailProvisionalLoadWithErrorForFrame
385         didCommitLoadForFrame,
386         0, // didFinishDocumentLoadForFrame
387         didFinishLoadForFrame,
388         0, // didFailLoadWithErrorForFrame
389         0, // didSameDocumentNavigationForFrame
390         0, // didReceiveTitleForFrame
391         0, // didFirstLayoutForFrame
392         0, // didFirstVisuallyNonEmptyLayoutForFrame
393         0, // didRemoveFrameFromHierarchy
394         0, // didFailToInitializePlugin
395         0, // didDisplayInsecureContentForFrame
396         0, // canAuthenticateAgainstProtectionSpaceInFrame
397         0, // didReceiveAuthenticationChallengeInFrame
398         0, // didStartProgress
399         0, // didChangeProgress
400         0, // didFinishProgress
401         0, // didBecomeUnresponsive
402         0, // didBecomeResponsive
403         processDidCrash,
404         0, // didChangeBackForwardList
405         0, // shouldGoToBackForwardListItem
406         0, // didRunInsecureContentForFrame
407         0, // didDetectXSSForFrame
408         0, // didNewFirstVisuallyNonEmptyLayout
409         0, // willGoToBackForwardListItem
410         0, // interactionOccurredWhileProcessUnresponsive
411         0, // pluginDidFail
412     };
413     WKPageSetPageLoaderClient(m_mainWebView->page(), &pageLoaderClient);
414 }
415
416 bool TestController::resetStateToConsistentValues()
417 {
418     m_state = Resetting;
419
420     m_beforeUnloadReturnValue = true;
421
422     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("Reset"));
423     WKRetainPtr<WKMutableDictionaryRef> resetMessageBody = adoptWK(WKMutableDictionaryCreate());
424
425     WKRetainPtr<WKStringRef> shouldGCKey = adoptWK(WKStringCreateWithUTF8CString("ShouldGC"));
426     WKRetainPtr<WKBooleanRef> shouldGCValue = adoptWK(WKBooleanCreate(m_gcBetweenTests));
427     WKDictionaryAddItem(resetMessageBody.get(), shouldGCKey.get(), shouldGCValue.get());
428
429     WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), resetMessageBody.get());
430
431     WKContextSetShouldUseFontSmoothing(TestController::shared().context(), false);
432
433     WKContextSetCacheModel(TestController::shared().context(), kWKCacheModelDocumentBrowser);
434
435     // FIXME: This function should also ensure that there is only one page open.
436
437     // Reset preferences
438     WKPreferencesRef preferences = WKPageGroupGetPreferences(m_pageGroup.get());
439     WKPreferencesResetTestRunnerOverrides(preferences);
440     WKPreferencesSetOfflineWebApplicationCacheEnabled(preferences, true);
441     WKPreferencesSetFontSmoothingLevel(preferences, kWKFontSmoothingLevelNoSubpixelAntiAliasing);
442     WKPreferencesSetXSSAuditorEnabled(preferences, false);
443     WKPreferencesSetWebAudioEnabled(preferences, true);
444     WKPreferencesSetDeveloperExtrasEnabled(preferences, true);
445     WKPreferencesSetJavaScriptExperimentsEnabled(preferences, true);
446     WKPreferencesSetJavaScriptCanOpenWindowsAutomatically(preferences, true);
447     WKPreferencesSetJavaScriptCanAccessClipboard(preferences, true);
448     WKPreferencesSetDOMPasteAllowed(preferences, true);
449     WKPreferencesSetUniversalAccessFromFileURLsAllowed(preferences, true);
450     WKPreferencesSetFileAccessFromFileURLsAllowed(preferences, true);
451 #if ENABLE(FULLSCREEN_API)
452     WKPreferencesSetFullScreenEnabled(preferences, true);
453 #endif
454     WKPreferencesSetPageCacheEnabled(preferences, false);
455
456 // [Qt][WK2]REGRESSION(r104881):It broke hundreds of tests
457 // FIXME: https://bugs.webkit.org/show_bug.cgi?id=76247
458 #if !PLATFORM(QT)
459     WKPreferencesSetMockScrollbarsEnabled(preferences, true);
460 #endif
461
462 #if !PLATFORM(QT)
463     static WKStringRef standardFontFamily = WKStringCreateWithUTF8CString("Times");
464     static WKStringRef cursiveFontFamily = WKStringCreateWithUTF8CString("Apple Chancery");
465     static WKStringRef fantasyFontFamily = WKStringCreateWithUTF8CString("Papyrus");
466     static WKStringRef fixedFontFamily = WKStringCreateWithUTF8CString("Courier");
467     static WKStringRef pictographFontFamily = WKStringCreateWithUTF8CString("Apple Color Emoji");
468     static WKStringRef sansSerifFontFamily = WKStringCreateWithUTF8CString("Helvetica");
469     static WKStringRef serifFontFamily = WKStringCreateWithUTF8CString("Times");
470
471     WKPreferencesSetStandardFontFamily(preferences, standardFontFamily);
472     WKPreferencesSetCursiveFontFamily(preferences, cursiveFontFamily);
473     WKPreferencesSetFantasyFontFamily(preferences, fantasyFontFamily);
474     WKPreferencesSetFixedFontFamily(preferences, fixedFontFamily);
475     WKPreferencesSetPictographFontFamily(preferences, pictographFontFamily);
476     WKPreferencesSetSansSerifFontFamily(preferences, sansSerifFontFamily);
477     WKPreferencesSetSerifFontFamily(preferences, serifFontFamily);
478 #endif
479     WKPreferencesSetInspectorUsesWebKitUserInterface(preferences, true);
480
481     // in the case that a test using the chrome input field failed, be sure to clean up for the next test
482     m_mainWebView->removeChromeInputField();
483     m_mainWebView->focus();
484
485     // Re-set to the default backing scale factor by setting the custom scale factor to 0.
486     WKPageSetCustomBackingScaleFactor(m_mainWebView->page(), 0);
487
488     // Reset main page back to about:blank
489     m_doneResetting = false;
490
491     WKPageLoadURL(m_mainWebView->page(), blankURL());
492     runUntil(m_doneResetting, ShortTimeout);
493     return m_doneResetting;
494 }
495
496 bool TestController::runTest(const char* test)
497 {
498     if (!resetStateToConsistentValues()) {
499 #if PLATFORM(MAC)
500         pid_t pid = WKPageGetProcessIdentifier(m_mainWebView->page());
501         fprintf(stderr, "#CRASHED - WebProcess (pid %ld)\n", static_cast<long>(pid));
502 #else
503         fputs("#CRASHED - WebProcess\n", stderr);
504 #endif
505         fflush(stderr);
506         return false;
507     }
508
509     std::string pathOrURL(test);
510     std::string expectedPixelHash;
511     size_t separatorPos = pathOrURL.find("'");
512     if (separatorPos != std::string::npos) {
513         pathOrURL = std::string(std::string(test), 0, separatorPos);
514         expectedPixelHash = std::string(std::string(test), separatorPos + 1);
515     }
516
517     m_state = RunningTest;
518
519     m_currentInvocation = adoptPtr(new TestInvocation(pathOrURL));
520     if (m_dumpPixels)
521         m_currentInvocation->setIsPixelTest(expectedPixelHash);
522
523     m_currentInvocation->invoke();
524     m_currentInvocation.clear();
525
526     return true;
527 }
528
529 void TestController::runTestingServerLoop()
530 {
531     char filenameBuffer[2048];
532     while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
533         char* newLineCharacter = strchr(filenameBuffer, '\n');
534         if (newLineCharacter)
535             *newLineCharacter = '\0';
536
537         if (strlen(filenameBuffer) == 0)
538             continue;
539
540         if (!runTest(filenameBuffer))
541             break;
542     }
543 }
544
545 void TestController::run()
546 {
547     if (m_usingServerMode)
548         runTestingServerLoop();
549     else {
550         for (size_t i = 0; i < m_paths.size(); ++i) {
551             if (!runTest(m_paths[i].c_str()))
552                 break;
553         }
554     }
555 }
556
557 void TestController::runUntil(bool& done, TimeoutDuration timeoutDuration)
558 {
559     double timeout;
560     switch (timeoutDuration) {
561     case ShortTimeout:
562         timeout = m_shortTimeout;
563         break;
564     case LongTimeout:
565         timeout = m_longTimeout;
566         break;
567     case NoTimeout:
568     default:
569         timeout = m_noTimeout;
570         break;
571     }
572
573     platformRunUntil(done, timeout);
574 }
575
576 // WKContextInjectedBundleClient
577
578 void TestController::didReceiveMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
579 {
580     static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveMessageFromInjectedBundle(messageName, messageBody);
581 }
582
583 void TestController::didReceiveSynchronousMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void* clientInfo)
584 {
585     *returnData = static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody).leakRef();
586 }
587
588 void TestController::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
589 {
590     if (!m_currentInvocation)
591         return;
592     m_currentInvocation->didReceiveMessageFromInjectedBundle(messageName, messageBody);
593 }
594
595 WKRetainPtr<WKTypeRef> TestController::didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
596 {
597 #if PLATFORM(MAC) || PLATFORM(QT) || PLATFORM(GTK) || PLATFORM(EFL)
598     if (WKStringIsEqualToUTF8CString(messageName, "EventSender")) {
599         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
600         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
601
602         WKRetainPtr<WKStringRef> subMessageKey(AdoptWK, WKStringCreateWithUTF8CString("SubMessage"));
603         WKStringRef subMessageName = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, subMessageKey.get()));
604
605         if (WKStringIsEqualToUTF8CString(subMessageName, "KeyDown")) {
606             WKRetainPtr<WKStringRef> keyKey = adoptWK(WKStringCreateWithUTF8CString("Key"));
607             WKStringRef key = static_cast<WKStringRef>(WKDictionaryGetItemForKey(messageBodyDictionary, keyKey.get()));
608
609             WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
610             WKEventModifiers modifiers = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifiersKey.get()))));
611
612             WKRetainPtr<WKStringRef> locationKey = adoptWK(WKStringCreateWithUTF8CString("Location"));
613             unsigned location = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, locationKey.get()))));
614
615             // Forward to WebProcess
616             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
617             m_eventSenderProxy->keyDown(key, modifiers, location);
618             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
619             return 0;
620         }
621
622         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseDown") || WKStringIsEqualToUTF8CString(subMessageName, "MouseUp")) {
623             WKRetainPtr<WKStringRef> buttonKey = adoptWK(WKStringCreateWithUTF8CString("Button"));
624             unsigned button = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, buttonKey.get()))));
625
626             WKRetainPtr<WKStringRef> modifiersKey = adoptWK(WKStringCreateWithUTF8CString("Modifiers"));
627             WKEventModifiers modifiers = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifiersKey.get()))));
628
629             // Forward to WebProcess
630             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
631             if (WKStringIsEqualToUTF8CString(subMessageName, "MouseDown"))
632                 m_eventSenderProxy->mouseDown(button, modifiers);
633             else
634                 m_eventSenderProxy->mouseUp(button, modifiers);
635             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
636             return 0;
637         }
638
639         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseMoveTo")) {
640             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
641             double x = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get())));
642
643             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
644             double y = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get())));
645
646             // Forward to WebProcess
647             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
648             m_eventSenderProxy->mouseMoveTo(x, y);
649             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
650             return 0;
651         }
652
653         if (WKStringIsEqualToUTF8CString(subMessageName, "MouseScrollBy")) {
654             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
655             double x = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get())));
656
657             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
658             double y = WKDoubleGetValue(static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get())));
659
660             // Forward to WebProcess
661             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
662             m_eventSenderProxy->mouseScrollBy(x, y);
663             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
664             return 0;
665         }
666
667         if (WKStringIsEqualToUTF8CString(subMessageName, "LeapForward")) {
668             WKRetainPtr<WKStringRef> timeKey = adoptWK(WKStringCreateWithUTF8CString("TimeInMilliseconds"));
669             unsigned time = static_cast<unsigned>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, timeKey.get()))));
670
671             m_eventSenderProxy->leapForward(time);
672             return 0;
673         }
674
675 #if ENABLE(TOUCH_EVENTS)
676         if (WKStringIsEqualToUTF8CString(subMessageName, "AddTouchPoint")) {
677             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
678             int x = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get()))));
679
680             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
681             int y = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get()))));
682
683             m_eventSenderProxy->addTouchPoint(x, y);
684             return 0;
685         }
686
687         if (WKStringIsEqualToUTF8CString(subMessageName, "UpdateTouchPoint")) {
688             WKRetainPtr<WKStringRef> indexKey = adoptWK(WKStringCreateWithUTF8CString("Index"));
689             int index = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, indexKey.get()))));
690
691             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("X"));
692             int x = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get()))));
693
694             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("Y"));
695             int y = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get()))));
696
697             m_eventSenderProxy->updateTouchPoint(index, x, y);
698             return 0;
699         }
700
701         if (WKStringIsEqualToUTF8CString(subMessageName, "SetTouchModifier")) {
702             WKRetainPtr<WKStringRef> modifierKey = adoptWK(WKStringCreateWithUTF8CString("Modifier"));
703             WKEventModifiers modifier = static_cast<WKEventModifiers>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, modifierKey.get()))));
704
705             WKRetainPtr<WKStringRef> enableKey = adoptWK(WKStringCreateWithUTF8CString("Enable"));
706             bool enable = static_cast<bool>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, enableKey.get()))));
707
708             m_eventSenderProxy->setTouchModifier(modifier, enable);
709             return 0;
710         }
711
712         if (WKStringIsEqualToUTF8CString(subMessageName, "SetTouchPointRadius")) {
713             WKRetainPtr<WKStringRef> xKey = adoptWK(WKStringCreateWithUTF8CString("RadiusX"));
714             int x = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, xKey.get()))));
715
716             WKRetainPtr<WKStringRef> yKey = adoptWK(WKStringCreateWithUTF8CString("RadiusY"));
717             int y = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, yKey.get()))));
718
719             m_eventSenderProxy->setTouchPointRadius(x, y);
720             return 0;
721         }
722
723         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchStart")) {
724             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
725             m_eventSenderProxy->touchStart();
726             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
727             return 0;
728         }
729
730         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchMove")) {
731             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
732             m_eventSenderProxy->touchMove();
733             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
734             return 0;
735         }
736
737         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchEnd")) {
738             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
739             m_eventSenderProxy->touchEnd();
740             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
741             return 0;
742         }
743
744         if (WKStringIsEqualToUTF8CString(subMessageName, "TouchCancel")) {
745             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), true);
746             m_eventSenderProxy->touchCancel();
747             WKPageSetShouldSendEventsSynchronously(mainWebView()->page(), false);
748             return 0;
749         }
750
751         if (WKStringIsEqualToUTF8CString(subMessageName, "ClearTouchPoints")) {
752             m_eventSenderProxy->clearTouchPoints();
753             return 0;
754         }
755
756         if (WKStringIsEqualToUTF8CString(subMessageName, "ReleaseTouchPoint")) {
757             WKRetainPtr<WKStringRef> indexKey = adoptWK(WKStringCreateWithUTF8CString("Index"));
758             int index = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, indexKey.get()))));
759             m_eventSenderProxy->releaseTouchPoint(index);
760             return 0;
761         }
762
763         if (WKStringIsEqualToUTF8CString(subMessageName, "CancelTouchPoint")) {
764             WKRetainPtr<WKStringRef> indexKey = adoptWK(WKStringCreateWithUTF8CString("Index"));
765             int index = static_cast<int>(WKUInt64GetValue(static_cast<WKUInt64Ref>(WKDictionaryGetItemForKey(messageBodyDictionary, indexKey.get()))));
766             m_eventSenderProxy->cancelTouchPoint(index);
767             return 0;
768         }
769 #endif
770         ASSERT_NOT_REACHED();
771     }
772 #endif
773     return m_currentInvocation->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody);
774 }
775
776 // WKPageLoaderClient
777
778 void TestController::didCommitLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef, const void* clientInfo)
779 {
780     static_cast<TestController*>(const_cast<void*>(clientInfo))->didCommitLoadForFrame(page, frame);
781 }
782
783 void TestController::didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef, const void* clientInfo)
784 {
785     static_cast<TestController*>(const_cast<void*>(clientInfo))->didFinishLoadForFrame(page, frame);
786 }
787
788 void TestController::processDidCrash(WKPageRef page, const void* clientInfo)
789 {
790     static_cast<TestController*>(const_cast<void*>(clientInfo))->processDidCrash();
791 }
792
793 void TestController::didCommitLoadForFrame(WKPageRef page, WKFrameRef frame)
794 {
795     if (!WKFrameIsMainFrame(frame))
796         return;
797
798     mainWebView()->focus();
799 }
800
801 void TestController::didFinishLoadForFrame(WKPageRef page, WKFrameRef frame)
802 {
803     if (m_state != Resetting)
804         return;
805
806     if (!WKFrameIsMainFrame(frame))
807         return;
808
809     WKRetainPtr<WKURLRef> wkURL(AdoptWK, WKFrameCopyURL(frame));
810     if (!WKURLIsEqual(wkURL.get(), blankURL()))
811         return;
812
813     m_doneResetting = true;
814     shared().notifyDone();
815 }
816
817 void TestController::processDidCrash()
818 {
819     // This function can be called multiple times when crash logs are being saved on Windows, so
820     // ensure we only print the crashed message once.
821     if (!m_didPrintWebProcessCrashedMessage) {
822 #if PLATFORM(MAC)
823         pid_t pid = WKPageGetProcessIdentifier(m_mainWebView->page());
824         fprintf(stderr, "#CRASHED - WebProcess (pid %ld)\n", static_cast<long>(pid));
825 #else
826         fputs("#CRASHED - WebProcess\n", stderr);
827 #endif
828         fflush(stderr);
829         m_didPrintWebProcessCrashedMessage = true;
830     }
831
832     if (m_shouldExitWhenWebProcessCrashes)
833         exit(1);
834 }
835
836 } // namespace WTR