9e491af14659548c25015266c8fcd72738d487c9
[WebKit-https.git] / Tools / WebKitTestRunner / TestInvocation.cpp
1 /*
2  * Copyright (C) 2010, 2011, 2012 Apple Inc. All rights reserved.
3  * Copyright (C) 2012 Intel Corporation. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28 #include "TestInvocation.h"
29
30 #include "PlatformWebView.h"
31 #include "StringFunctions.h"
32 #include "TestController.h"
33 #include <climits>
34 #include <cstdio>
35 #include <WebKit2/WKContextPrivate.h>
36 #include <WebKit2/WKData.h>
37 #include <WebKit2/WKDictionary.h>
38 #include <WebKit2/WKInspector.h>
39 #include <WebKit2/WKRetainPtr.h>
40 #include <wtf/PassOwnPtr.h>
41 #include <wtf/StdLibExtras.h>
42 #include <wtf/text/CString.h>
43
44 #if PLATFORM(MAC) && !PLATFORM(IOS)
45 #include <Carbon/Carbon.h>
46 #endif
47
48 #if PLATFORM(COCOA)
49 #include <WebKit2/WKPagePrivateMac.h>
50 #endif
51
52 #include <unistd.h> // For getcwd.
53
54 using namespace WebKit;
55 using namespace std;
56
57 namespace WTR {
58
59 static WKURLRef createWKURL(const char* pathOrURL)
60 {
61     if (strstr(pathOrURL, "http://") || strstr(pathOrURL, "https://") || strstr(pathOrURL, "file://"))
62         return WKURLCreateWithUTF8CString(pathOrURL);
63
64     // Creating from filesytem path.
65     size_t length = strlen(pathOrURL);
66     if (!length)
67         return 0;
68
69     const char separator = '/';
70     bool isAbsolutePath = pathOrURL[0] == separator;
71     const char* filePrefix = "file://";
72     static const size_t prefixLength = strlen(filePrefix);
73
74     std::unique_ptr<char[]> buffer;
75     if (isAbsolutePath) {
76         buffer = std::make_unique<char[]>(prefixLength + length + 1);
77         strcpy(buffer.get(), filePrefix);
78         strcpy(buffer.get() + prefixLength, pathOrURL);
79     } else {
80         buffer = std::make_unique<char[]>(prefixLength + PATH_MAX + length + 2); // 1 for the separator
81         strcpy(buffer.get(), filePrefix);
82         if (!getcwd(buffer.get() + prefixLength, PATH_MAX))
83             return 0;
84         size_t numCharacters = strlen(buffer.get());
85         buffer[numCharacters] = separator;
86         strcpy(buffer.get() + numCharacters + 1, pathOrURL);
87     }
88
89     return WKURLCreateWithUTF8CString(buffer.get());
90 }
91
92 TestInvocation::TestInvocation(const std::string& pathOrURL)
93     : m_url(AdoptWK, createWKURL(pathOrURL.c_str()))
94     , m_pathOrURL(pathOrURL)
95     , m_dumpPixels(false)
96     , m_timeout(0)
97     , m_gotInitialResponse(false)
98     , m_gotFinalMessage(false)
99     , m_gotRepaint(false)
100     , m_error(false)
101     , m_webProcessIsUnresponsive(false)
102 {
103 }
104
105 TestInvocation::~TestInvocation()
106 {
107 }
108
109 void TestInvocation::setIsPixelTest(const std::string& expectedPixelHash)
110 {
111     m_dumpPixels = true;
112     m_expectedPixelHash = expectedPixelHash;
113 }
114
115 void TestInvocation::setCustomTimeout(int timeout)
116 {
117     m_timeout = timeout;
118 }
119
120 static void sizeWebViewForCurrentTest(const char* pathOrURL)
121 {
122     bool isSVGW3CTest = strstr(pathOrURL, "svg/W3C-SVG-1.1") || strstr(pathOrURL, "svg\\W3C-SVG-1.1");
123
124     if (isSVGW3CTest)
125         TestController::shared().mainWebView()->resizeTo(TestController::w3cSVGViewWidth, TestController::w3cSVGViewHeight);
126     else
127         TestController::shared().mainWebView()->resizeTo(TestController::viewWidth, TestController::viewHeight);
128 }
129
130 static void changeWindowScaleIfNeeded(const char* pathOrURL)
131 {
132     WTF::String localPathOrUrl = String(pathOrURL);
133     bool needsHighDPIWindow = localPathOrUrl.findIgnoringCase("hidpi-") != notFound;
134     TestController::shared().mainWebView()->changeWindowScaleIfNeeded(needsHighDPIWindow ? 2 : 1);
135 }
136
137 static bool shouldLogFrameLoadDelegates(const char* pathOrURL)
138 {
139     return strstr(pathOrURL, "loading/");
140 }
141
142 #if PLATFORM(COCOA)
143 static bool shouldUseThreadedScrolling(const char* pathOrURL)
144 {
145     return strstr(pathOrURL, "tiled-drawing/") || strstr(pathOrURL, "tiled-drawing\\");
146 }
147 #endif
148
149 static void updateThreadedScrollingForCurrentTest(const char* pathOrURL)
150 {
151 #if PLATFORM(COCOA)
152     WKRetainPtr<WKMutableDictionaryRef> viewOptions = adoptWK(WKMutableDictionaryCreate());
153     WKRetainPtr<WKStringRef> useThreadedScrollingKey = adoptWK(WKStringCreateWithUTF8CString("ThreadedScrolling"));
154     WKRetainPtr<WKBooleanRef> useThreadedScrollingValue = adoptWK(WKBooleanCreate(shouldUseThreadedScrolling(pathOrURL)));
155     WKDictionarySetItem(viewOptions.get(), useThreadedScrollingKey.get(), useThreadedScrollingValue.get());
156
157     WKRetainPtr<WKStringRef> useRemoteLayerTreeKey = adoptWK(WKStringCreateWithUTF8CString("RemoteLayerTree"));
158     WKRetainPtr<WKBooleanRef> useRemoteLayerTreeValue = adoptWK(WKBooleanCreate(TestController::shared().shouldUseRemoteLayerTree()));
159     WKDictionarySetItem(viewOptions.get(), useRemoteLayerTreeKey.get(), useRemoteLayerTreeValue.get());
160
161     TestController::shared().ensureViewSupportsOptions(viewOptions.get());
162 #else
163     UNUSED_PARAM(pathOrURL);
164 #endif
165 }
166
167 static bool shouldUseFixedLayout(const char* pathOrURL)
168 {
169 #if ENABLE(CSS_DEVICE_ADAPTATION)
170     if (strstr(pathOrURL, "device-adapt/") || strstr(pathOrURL, "device-adapt\\"))
171         return true;
172 #endif
173
174 #if USE(TILED_BACKING_STORE) && PLATFORM(EFL)
175     if (strstr(pathOrURL, "sticky/") || strstr(pathOrURL, "sticky\\"))
176         return true;
177 #endif
178     return false;
179
180     UNUSED_PARAM(pathOrURL);
181 }
182
183 static void updateLayoutType(const char* pathOrURL)
184 {
185     WKRetainPtr<WKMutableDictionaryRef> viewOptions = adoptWK(WKMutableDictionaryCreate());
186     WKRetainPtr<WKStringRef> useFixedLayoutKey = adoptWK(WKStringCreateWithUTF8CString("UseFixedLayout"));
187     WKRetainPtr<WKBooleanRef> useFixedLayoutValue = adoptWK(WKBooleanCreate(shouldUseFixedLayout(pathOrURL)));
188     WKDictionarySetItem(viewOptions.get(), useFixedLayoutKey.get(), useFixedLayoutValue.get());
189
190     TestController::shared().ensureViewSupportsOptions(viewOptions.get());
191 }
192
193 void TestInvocation::invoke()
194 {
195     TestController::TimeoutDuration timeoutToUse = TestController::LongTimeout;
196     changeWindowScaleIfNeeded(m_pathOrURL.c_str());
197     sizeWebViewForCurrentTest(m_pathOrURL.c_str());
198     updateLayoutType(m_pathOrURL.c_str());
199     updateThreadedScrollingForCurrentTest(m_pathOrURL.c_str());
200
201     m_textOutput.clear();
202
203     WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("BeginTest"));
204     WKRetainPtr<WKMutableDictionaryRef> beginTestMessageBody = adoptWK(WKMutableDictionaryCreate());
205
206     WKRetainPtr<WKStringRef> dumpFrameLoadDelegatesKey = adoptWK(WKStringCreateWithUTF8CString("DumpFrameLoadDelegates"));
207     WKRetainPtr<WKBooleanRef> dumpFrameLoadDelegatesValue = adoptWK(WKBooleanCreate(shouldLogFrameLoadDelegates(m_pathOrURL.c_str())));
208     WKDictionarySetItem(beginTestMessageBody.get(), dumpFrameLoadDelegatesKey.get(), dumpFrameLoadDelegatesValue.get());
209
210     WKRetainPtr<WKStringRef> dumpPixelsKey = adoptWK(WKStringCreateWithUTF8CString("DumpPixels"));
211     WKRetainPtr<WKBooleanRef> dumpPixelsValue = adoptWK(WKBooleanCreate(m_dumpPixels));
212     WKDictionarySetItem(beginTestMessageBody.get(), dumpPixelsKey.get(), dumpPixelsValue.get());
213
214     WKRetainPtr<WKStringRef> useWaitToDumpWatchdogTimerKey = adoptWK(WKStringCreateWithUTF8CString("UseWaitToDumpWatchdogTimer"));
215     WKRetainPtr<WKBooleanRef> useWaitToDumpWatchdogTimerValue = adoptWK(WKBooleanCreate(TestController::shared().useWaitToDumpWatchdogTimer()));
216     WKDictionarySetItem(beginTestMessageBody.get(), useWaitToDumpWatchdogTimerKey.get(), useWaitToDumpWatchdogTimerValue.get());
217
218     WKRetainPtr<WKStringRef> timeoutKey = adoptWK(WKStringCreateWithUTF8CString("Timeout"));
219     WKRetainPtr<WKUInt64Ref> timeoutValue = adoptWK(WKUInt64Create(m_timeout));
220     WKDictionarySetItem(beginTestMessageBody.get(), timeoutKey.get(), timeoutValue.get());
221
222     WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), beginTestMessageBody.get());
223
224     TestController::shared().runUntil(m_gotInitialResponse, TestController::ShortTimeout);
225     if (!m_gotInitialResponse) {
226         m_errorMessage = "Timed out waiting for initial response from web process\n";
227         m_webProcessIsUnresponsive = true;
228         goto end;
229     }
230     if (m_error)
231         goto end;
232
233     WKPageLoadURL(TestController::shared().mainWebView()->page(), m_url.get());
234
235     if (TestController::shared().useWaitToDumpWatchdogTimer()) {
236         if (m_timeout > 0)
237             timeoutToUse = TestController::CustomTimeout;
238     } else
239         timeoutToUse = TestController::NoTimeout;
240     TestController::shared().runUntil(m_gotFinalMessage, timeoutToUse);
241
242     if (!m_gotFinalMessage) {
243         m_errorMessage = "Timed out waiting for final message from web process\n";
244         m_webProcessIsUnresponsive = true;
245         goto end;
246     }
247     if (m_error)
248         goto end;
249
250     dumpResults();
251
252 end:
253 #if ENABLE(INSPECTOR) && !PLATFORM(IOS)
254     if (m_gotInitialResponse)
255         WKInspectorClose(WKPageGetInspector(TestController::shared().mainWebView()->page()));
256 #endif // ENABLE(INSPECTOR)
257
258     if (m_webProcessIsUnresponsive)
259         dumpWebProcessUnresponsiveness();
260     else if (!TestController::shared().resetStateToConsistentValues()) {
261         m_errorMessage = "Timed out loading about:blank before the next test";
262         dumpWebProcessUnresponsiveness();
263     }
264 }
265
266 void TestInvocation::dumpWebProcessUnresponsiveness()
267 {
268     dumpWebProcessUnresponsiveness(m_errorMessage.c_str());
269 }
270
271 void TestInvocation::dumpWebProcessUnresponsiveness(const char* errorMessage)
272 {
273     const char* errorMessageToStderr = 0;
274 #if PLATFORM(COCOA)
275     char buffer[64];
276     pid_t pid = WKPageGetProcessIdentifier(TestController::shared().mainWebView()->page());
277     sprintf(buffer, "#PROCESS UNRESPONSIVE - WebProcess (pid %ld)\n", static_cast<long>(pid));
278     errorMessageToStderr = buffer;
279 #else
280     errorMessageToStderr = "#PROCESS UNRESPONSIVE - WebProcess";
281 #endif
282
283     dump(errorMessage, errorMessageToStderr, true);
284 }
285
286 void TestInvocation::dump(const char* textToStdout, const char* textToStderr, bool seenError)
287 {
288     printf("Content-Type: text/plain\n");
289     if (textToStdout)
290         fputs(textToStdout, stdout);
291     if (textToStderr)
292         fputs(textToStderr, stderr);
293
294     fputs("#EOF\n", stdout);
295     fputs("#EOF\n", stderr);
296     if (seenError)
297         fputs("#EOF\n", stdout);
298     fflush(stdout);
299     fflush(stderr);
300 }
301
302 void TestInvocation::forceRepaintDoneCallback(WKErrorRef, void* context)
303 {
304     TestInvocation* testInvocation = static_cast<TestInvocation*>(context);
305     testInvocation->m_gotRepaint = true;
306     TestController::shared().notifyDone();
307 }
308
309 void TestInvocation::dumpResults()
310 {
311     if (m_textOutput.length() || !m_audioResult)
312         dump(m_textOutput.toString().utf8().data());
313     else
314         dumpAudio(m_audioResult.get());
315
316     if (m_dumpPixels && m_pixelResult) {
317         if (PlatformWebView::windowSnapshotEnabled()) {
318             m_gotRepaint = false;
319             WKPageForceRepaint(TestController::shared().mainWebView()->page(), this, TestInvocation::forceRepaintDoneCallback);
320             TestController::shared().runUntil(m_gotRepaint, TestController::ShortTimeout);
321             if (!m_gotRepaint) {
322                 m_errorMessage = "Timed out waiting for pre-pixel dump repaint\n";
323                 m_webProcessIsUnresponsive = true;
324                 return;
325             }
326         }
327         dumpPixelsAndCompareWithExpected(m_pixelResult.get(), m_repaintRects.get());
328     }
329
330     fputs("#EOF\n", stdout);
331     fflush(stdout);
332     fflush(stderr);
333 }
334
335 void TestInvocation::dumpAudio(WKDataRef audioData)
336 {
337     size_t length = WKDataGetSize(audioData);
338     if (!length)
339         return;
340
341     const unsigned char* data = WKDataGetBytes(audioData);
342
343     printf("Content-Type: audio/wav\n");
344     printf("Content-Length: %lu\n", static_cast<unsigned long>(length));
345
346     fwrite(data, 1, length, stdout);
347     printf("#EOF\n");
348     fprintf(stderr, "#EOF\n");
349 }
350
351 bool TestInvocation::compareActualHashToExpectedAndDumpResults(const char actualHash[33])
352 {
353     // Compute the hash of the bitmap context pixels
354     fprintf(stdout, "\nActualHash: %s\n", actualHash);
355
356     if (!m_expectedPixelHash.length())
357         return false;
358
359     ASSERT(m_expectedPixelHash.length() == 32);
360     fprintf(stdout, "\nExpectedHash: %s\n", m_expectedPixelHash.c_str());
361
362     // FIXME: Do case insensitive compare.
363     return m_expectedPixelHash == actualHash;
364 }
365
366 void TestInvocation::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
367 {
368     if (WKStringIsEqualToUTF8CString(messageName, "Error")) {
369         // Set all states to true to stop spinning the runloop.
370         m_gotInitialResponse = true;
371         m_gotFinalMessage = true;
372         m_error = true;
373         m_errorMessage = "FAIL\n";
374         TestController::shared().notifyDone();
375         return;
376     }
377
378     if (WKStringIsEqualToUTF8CString(messageName, "Ack")) {
379         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
380         WKStringRef messageBodyString = static_cast<WKStringRef>(messageBody);
381         if (WKStringIsEqualToUTF8CString(messageBodyString, "BeginTest")) {
382             m_gotInitialResponse = true;
383             TestController::shared().notifyDone();
384             return;
385         }
386
387         ASSERT_NOT_REACHED();
388     }
389
390     if (WKStringIsEqualToUTF8CString(messageName, "Done")) {
391         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
392         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
393
394         WKRetainPtr<WKStringRef> pixelResultKey = adoptWK(WKStringCreateWithUTF8CString("PixelResult"));
395         m_pixelResult = static_cast<WKImageRef>(WKDictionaryGetItemForKey(messageBodyDictionary, pixelResultKey.get()));
396         ASSERT(!m_pixelResult || m_dumpPixels);
397
398         WKRetainPtr<WKStringRef> repaintRectsKey = adoptWK(WKStringCreateWithUTF8CString("RepaintRects"));
399         m_repaintRects = static_cast<WKArrayRef>(WKDictionaryGetItemForKey(messageBodyDictionary, repaintRectsKey.get()));
400
401         WKRetainPtr<WKStringRef> audioResultKey =  adoptWK(WKStringCreateWithUTF8CString("AudioResult"));
402         m_audioResult = static_cast<WKDataRef>(WKDictionaryGetItemForKey(messageBodyDictionary, audioResultKey.get()));
403
404         m_gotFinalMessage = true;
405         TestController::shared().notifyDone();
406         return;
407     }
408
409     if (WKStringIsEqualToUTF8CString(messageName, "TextOutput")) {
410         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
411         WKStringRef textOutput = static_cast<WKStringRef>(messageBody);
412         m_textOutput.append(toWTFString(textOutput));
413         return;
414     }
415
416     if (WKStringIsEqualToUTF8CString(messageName, "BeforeUnloadReturnValue")) {
417         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
418         WKBooleanRef beforeUnloadReturnValue = static_cast<WKBooleanRef>(messageBody);
419         TestController::shared().setBeforeUnloadReturnValue(WKBooleanGetValue(beforeUnloadReturnValue));
420         return;
421     }
422     
423     if (WKStringIsEqualToUTF8CString(messageName, "AddChromeInputField")) {
424         TestController::shared().mainWebView()->addChromeInputField();
425         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallAddChromeInputFieldCallback"));
426         WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), 0);
427         return;
428     }
429
430     if (WKStringIsEqualToUTF8CString(messageName, "RemoveChromeInputField")) {
431         TestController::shared().mainWebView()->removeChromeInputField();
432         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallRemoveChromeInputFieldCallback"));
433         WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), 0);
434         return;
435     }
436     
437     if (WKStringIsEqualToUTF8CString(messageName, "FocusWebView")) {
438         TestController::shared().mainWebView()->makeWebViewFirstResponder();
439         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallFocusWebViewCallback"));
440         WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), 0);
441         return;
442     }
443
444     if (WKStringIsEqualToUTF8CString(messageName, "SetBackingScaleFactor")) {
445         ASSERT(WKGetTypeID(messageBody) == WKDoubleGetTypeID());
446         double backingScaleFactor = WKDoubleGetValue(static_cast<WKDoubleRef>(messageBody));
447         WKPageSetCustomBackingScaleFactor(TestController::shared().mainWebView()->page(), backingScaleFactor);
448
449         WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("CallSetBackingScaleFactorCallback"));
450         WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), 0);
451         return;
452     }
453
454     if (WKStringIsEqualToUTF8CString(messageName, "SimulateWebNotificationClick")) {
455         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
456         uint64_t notificationID = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
457         TestController::shared().simulateWebNotificationClick(notificationID);
458         return;
459     }
460
461     if (WKStringIsEqualToUTF8CString(messageName, "SetGeolocationPermission")) {
462         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
463         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(messageBody);
464         TestController::shared().setGeolocationPermission(WKBooleanGetValue(enabledWK));
465         return;
466     }
467
468     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGeolocationPosition")) {
469         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
470         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
471
472         WKRetainPtr<WKStringRef> latitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("latitude"));
473         WKDoubleRef latitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, latitudeKeyWK.get()));
474         double latitude = WKDoubleGetValue(latitudeWK);
475
476         WKRetainPtr<WKStringRef> longitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("longitude"));
477         WKDoubleRef longitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, longitudeKeyWK.get()));
478         double longitude = WKDoubleGetValue(longitudeWK);
479
480         WKRetainPtr<WKStringRef> accuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("accuracy"));
481         WKDoubleRef accuracyWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, accuracyKeyWK.get()));
482         double accuracy = WKDoubleGetValue(accuracyWK);
483
484         WKRetainPtr<WKStringRef> providesAltitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitude"));
485         WKBooleanRef providesAltitudeWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesAltitudeKeyWK.get()));
486         bool providesAltitude = WKBooleanGetValue(providesAltitudeWK);
487
488         WKRetainPtr<WKStringRef> altitudeKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitude"));
489         WKDoubleRef altitudeWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, altitudeKeyWK.get()));
490         double altitude = WKDoubleGetValue(altitudeWK);
491
492         WKRetainPtr<WKStringRef> providesAltitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesAltitudeAccuracy"));
493         WKBooleanRef providesAltitudeAccuracyWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesAltitudeAccuracyKeyWK.get()));
494         bool providesAltitudeAccuracy = WKBooleanGetValue(providesAltitudeAccuracyWK);
495
496         WKRetainPtr<WKStringRef> altitudeAccuracyKeyWK(AdoptWK, WKStringCreateWithUTF8CString("altitudeAccuracy"));
497         WKDoubleRef altitudeAccuracyWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, altitudeAccuracyKeyWK.get()));
498         double altitudeAccuracy = WKDoubleGetValue(altitudeAccuracyWK);
499
500         WKRetainPtr<WKStringRef> providesHeadingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesHeading"));
501         WKBooleanRef providesHeadingWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesHeadingKeyWK.get()));
502         bool providesHeading = WKBooleanGetValue(providesHeadingWK);
503
504         WKRetainPtr<WKStringRef> headingKeyWK(AdoptWK, WKStringCreateWithUTF8CString("heading"));
505         WKDoubleRef headingWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, headingKeyWK.get()));
506         double heading = WKDoubleGetValue(headingWK);
507
508         WKRetainPtr<WKStringRef> providesSpeedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("providesSpeed"));
509         WKBooleanRef providesSpeedWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, providesSpeedKeyWK.get()));
510         bool providesSpeed = WKBooleanGetValue(providesSpeedWK);
511
512         WKRetainPtr<WKStringRef> speedKeyWK(AdoptWK, WKStringCreateWithUTF8CString("speed"));
513         WKDoubleRef speedWK = static_cast<WKDoubleRef>(WKDictionaryGetItemForKey(messageBodyDictionary, speedKeyWK.get()));
514         double speed = WKDoubleGetValue(speedWK);
515
516         TestController::shared().setMockGeolocationPosition(latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed);
517         return;
518     }
519
520     if (WKStringIsEqualToUTF8CString(messageName, "SetMockGeolocationPositionUnavailableError")) {
521         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
522         WKStringRef errorMessage = static_cast<WKStringRef>(messageBody);
523         TestController::shared().setMockGeolocationPositionUnavailableError(errorMessage);
524         return;
525     }
526
527     if (WKStringIsEqualToUTF8CString(messageName, "SetCustomPolicyDelegate")) {
528         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
529         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
530
531         WKRetainPtr<WKStringRef> enabledKeyWK(AdoptWK, WKStringCreateWithUTF8CString("enabled"));
532         WKBooleanRef enabledWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, enabledKeyWK.get()));
533         bool enabled = WKBooleanGetValue(enabledWK);
534
535         WKRetainPtr<WKStringRef> permissiveKeyWK(AdoptWK, WKStringCreateWithUTF8CString("permissive"));
536         WKBooleanRef permissiveWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, permissiveKeyWK.get()));
537         bool permissive = WKBooleanGetValue(permissiveWK);
538
539         TestController::shared().setCustomPolicyDelegate(enabled, permissive);
540         return;
541     }
542
543     if (WKStringIsEqualToUTF8CString(messageName, "SetHidden")) {
544         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
545         WKDictionaryRef messageBodyDictionary = static_cast<WKDictionaryRef>(messageBody);
546
547         WKRetainPtr<WKStringRef> isInitialKeyWK(AdoptWK, WKStringCreateWithUTF8CString("hidden"));
548         WKBooleanRef hiddenWK = static_cast<WKBooleanRef>(WKDictionaryGetItemForKey(messageBodyDictionary, isInitialKeyWK.get()));
549         bool hidden = WKBooleanGetValue(hiddenWK);
550
551         TestController::shared().setHidden(hidden);
552         return;
553     }
554
555     if (WKStringIsEqualToUTF8CString(messageName, "ProcessWorkQueue")) {
556         if (TestController::shared().workQueueManager().processWorkQueue()) {
557             WKRetainPtr<WKStringRef> messageName = adoptWK(WKStringCreateWithUTF8CString("WorkQueueProcessedCallback"));
558             WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), 0);
559         }
560         return;
561     }
562
563     if (WKStringIsEqualToUTF8CString(messageName, "QueueBackNavigation")) {
564         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
565         uint64_t stepCount = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
566         TestController::shared().workQueueManager().queueBackNavigation(stepCount);
567         return;
568     }
569
570     if (WKStringIsEqualToUTF8CString(messageName, "QueueForwardNavigation")) {
571         ASSERT(WKGetTypeID(messageBody) == WKUInt64GetTypeID());
572         uint64_t stepCount = WKUInt64GetValue(static_cast<WKUInt64Ref>(messageBody));
573         TestController::shared().workQueueManager().queueForwardNavigation(stepCount);
574         return;
575     }
576
577     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoad")) {
578         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
579         WKDictionaryRef loadDataDictionary = static_cast<WKDictionaryRef>(messageBody);
580
581         WKRetainPtr<WKStringRef> urlKey(AdoptWK, WKStringCreateWithUTF8CString("url"));
582         WKStringRef urlWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, urlKey.get()));
583
584         WKRetainPtr<WKStringRef> targetKey(AdoptWK, WKStringCreateWithUTF8CString("target"));
585         WKStringRef targetWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, targetKey.get()));
586
587         TestController::shared().workQueueManager().queueLoad(toWTFString(urlWK), toWTFString(targetWK));
588         return;
589     }
590
591     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoadHTMLString")) {
592         ASSERT(WKGetTypeID(messageBody) == WKDictionaryGetTypeID());
593         WKDictionaryRef loadDataDictionary = static_cast<WKDictionaryRef>(messageBody);
594
595         WKRetainPtr<WKStringRef> contentKey(AdoptWK, WKStringCreateWithUTF8CString("content"));
596         WKStringRef contentWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, contentKey.get()));
597
598         WKRetainPtr<WKStringRef> baseURLKey(AdoptWK, WKStringCreateWithUTF8CString("baseURL"));
599         WKStringRef baseURLWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, baseURLKey.get()));
600
601         WKRetainPtr<WKStringRef> unreachableURLKey(AdoptWK, WKStringCreateWithUTF8CString("unreachableURL"));
602         WKStringRef unreachableURLWK = static_cast<WKStringRef>(WKDictionaryGetItemForKey(loadDataDictionary, unreachableURLKey.get()));
603
604         TestController::shared().workQueueManager().queueLoadHTMLString(toWTFString(contentWK), baseURLWK ? toWTFString(baseURLWK) : String(), unreachableURLWK ? toWTFString(unreachableURLWK) : String());
605         return;
606     }
607
608     if (WKStringIsEqualToUTF8CString(messageName, "QueueReload")) {
609         TestController::shared().workQueueManager().queueReload();
610         return;
611     }
612
613     if (WKStringIsEqualToUTF8CString(messageName, "QueueLoadingScript")) {
614         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
615         WKStringRef script = static_cast<WKStringRef>(messageBody);
616         TestController::shared().workQueueManager().queueLoadingScript(toWTFString(script));
617         return;
618     }
619
620     if (WKStringIsEqualToUTF8CString(messageName, "QueueNonLoadingScript")) {
621         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
622         WKStringRef script = static_cast<WKStringRef>(messageBody);
623         TestController::shared().workQueueManager().queueNonLoadingScript(toWTFString(script));
624         return;
625     }
626
627     if (WKStringIsEqualToUTF8CString(messageName, "SetHandlesAuthenticationChallenge")) {
628         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
629         WKBooleanRef value = static_cast<WKBooleanRef>(messageBody);
630         TestController::shared().setHandlesAuthenticationChallenges(WKBooleanGetValue(value));
631         return;
632     }
633
634     if (WKStringIsEqualToUTF8CString(messageName, "SetAuthenticationUsername")) {
635         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
636         WKStringRef username = static_cast<WKStringRef>(messageBody);
637         TestController::shared().setAuthenticationUsername(toWTFString(username));
638         return;
639     }
640
641     if (WKStringIsEqualToUTF8CString(messageName, "SetAuthenticationPassword")) {
642         ASSERT(WKGetTypeID(messageBody) == WKStringGetTypeID());
643         WKStringRef password = static_cast<WKStringRef>(messageBody);
644         TestController::shared().setAuthenticationPassword(toWTFString(password));
645         return;
646     }
647
648     if (WKStringIsEqualToUTF8CString(messageName, "SetBlockAllPlugins")) {
649         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
650         WKBooleanRef shouldBlock = static_cast<WKBooleanRef>(messageBody);
651         TestController::shared().setBlockAllPlugins(WKBooleanGetValue(shouldBlock));
652         return;
653     }
654
655     ASSERT_NOT_REACHED();
656 }
657
658 WKRetainPtr<WKTypeRef> TestInvocation::didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
659 {
660     if (WKStringIsEqualToUTF8CString(messageName, "SetWindowIsKey")) {
661         ASSERT(WKGetTypeID(messageBody) == WKBooleanGetTypeID());
662         WKBooleanRef isKeyValue = static_cast<WKBooleanRef>(messageBody);
663         TestController::shared().mainWebView()->setWindowIsKey(WKBooleanGetValue(isKeyValue));
664         return 0;
665     }
666
667     if (WKStringIsEqualToUTF8CString(messageName, "IsWorkQueueEmpty")) {
668         bool isEmpty = TestController::shared().workQueueManager().isWorkQueueEmpty();
669         WKRetainPtr<WKTypeRef> result(AdoptWK, WKBooleanCreate(isEmpty));
670         return result;
671     }
672
673     if (WKStringIsEqualToUTF8CString(messageName, "SecureEventInputIsEnabled")) {
674 #if PLATFORM(MAC) && !PLATFORM(IOS)
675         WKRetainPtr<WKBooleanRef> result(AdoptWK, WKBooleanCreate(IsSecureEventInputEnabled()));
676 #else
677         WKRetainPtr<WKBooleanRef> result(AdoptWK, WKBooleanCreate(false));
678 #endif
679         return result;
680     }
681     ASSERT_NOT_REACHED();
682     return 0;
683 }
684
685 void TestInvocation::outputText(const WTF::String& text)
686 {
687     m_textOutput.append(text);
688 }
689
690 } // namespace WTR