e85617642d202705f698a2e4532495279579d6ff
[WebKit.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 "TestController.h"
27
28 #include "PlatformWebView.h"
29 #include "StringFunctions.h"
30 #include "TestInvocation.h"
31 #include <cstdio>
32 #include <WebKit2/WKPageGroup.h>
33 #include <WebKit2/WKContextPrivate.h>
34 #include <WebKit2/WKPreferencesPrivate.h>
35 #include <wtf/PassOwnPtr.h>
36
37 namespace WTR {
38
39 static const double defaultLongTimeout = 30;
40 static const double defaultShortTimeout = 5;
41
42 static WKURLRef blankURL()
43 {
44     static WKURLRef staticBlankURL = WKURLCreateWithUTF8CString("about:blank");
45     return staticBlankURL;
46 }
47
48 static TestController* controller;
49
50 TestController& TestController::shared()
51 {
52     ASSERT(controller);
53     return *controller;
54 }
55
56 TestController::TestController(int argc, const char* argv[])
57     : m_dumpPixels(false)
58     , m_verbose(false)
59     , m_printSeparators(false)
60     , m_usingServerMode(false)
61     , m_state(Initial)
62     , m_doneResetting(false)
63     , m_longTimeout(defaultLongTimeout)
64     , m_shortTimeout(defaultShortTimeout)
65 {
66     initialize(argc, argv);
67     controller = this;
68     run();
69     controller = 0;
70 }
71
72 TestController::~TestController()
73 {
74 }
75
76 static WKRect getWindowFrameMainPage(WKPageRef page, const void* clientInfo)
77 {
78     PlatformWebView* view = static_cast<TestController*>(const_cast<void*>(clientInfo))->mainWebView();
79     return view->windowFrame();
80 }
81
82 static void setWindowFrameMainPage(WKPageRef page, WKRect frame, const void* clientInfo)
83 {
84     PlatformWebView* view = static_cast<TestController*>(const_cast<void*>(clientInfo))->mainWebView();
85     view->setWindowFrame(frame);
86 }
87
88 static WKRect getWindowFrameOtherPage(WKPageRef page, const void* clientInfo)
89 {
90     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
91     return view->windowFrame();
92 }
93
94 static void setWindowFrameOtherPage(WKPageRef page, WKRect frame, const void* clientInfo)
95 {
96     PlatformWebView* view = static_cast<PlatformWebView*>(const_cast<void*>(clientInfo));
97     view->setWindowFrame(frame);
98 }
99
100 static bool runBeforeUnloadConfirmPanel(WKPageRef page, WKStringRef message, WKFrameRef frame, const void *clientInfo)
101 {
102     printf("%s\n", toSTD(message).c_str());
103     return true;
104 }
105
106 void TestController::runModal(WKPageRef page, const void* clientInfo)
107 {
108     runModal(static_cast<PlatformWebView*>(const_cast<void*>(clientInfo)));
109 }
110
111 static void closeOtherPage(WKPageRef page, const void* clientInfo)
112 {
113     WKPageClose(page);
114     const PlatformWebView* view = static_cast<const PlatformWebView*>(clientInfo);
115     delete view;
116 }
117
118 WKPageRef TestController::createOtherPage(WKPageRef oldPage, WKDictionaryRef, WKEventModifiers, WKEventMouseButton, const void*)
119 {
120     PlatformWebView* view = new PlatformWebView(WKPageGetContext(oldPage), WKPageGetPageGroup(oldPage));
121     WKPageRef newPage = view->page();
122
123     view->resizeTo(800, 600);
124
125     WKPageUIClient otherPageUIClient = {
126         0,
127         view,
128         createOtherPage,
129         0, // showPage
130         closeOtherPage,
131         0, // runJavaScriptAlert        
132         0, // runJavaScriptConfirm
133         0, // runJavaScriptPrompt
134         0, // setStatusText
135         0, // mouseDidMoveOverElement
136         0, // missingPluginButtonClicked
137         0, // didNotHandleKeyEvent
138         0, // toolbarsAreVisible
139         0, // setToolbarsAreVisible
140         0, // menuBarIsVisible
141         0, // setMenuBarIsVisible
142         0, // statusBarIsVisible
143         0, // setStatusBarIsVisible
144         0, // isResizable
145         0, // setIsResizable
146         getWindowFrameOtherPage,
147         setWindowFrameOtherPage,
148         runBeforeUnloadConfirmPanel,
149         0, // didDraw
150         0, // pageDidScroll
151         0, // exceededDatabaseQuota
152         0, // runOpenPanel
153         0, // decidePolicyForGeolocationPermissionRequest
154         0, // headerHeight
155         0, // footerHeight
156         0, // drawHeader
157         0, // drawFooter
158         0, // printFrame
159         runModal,
160     };
161     WKPageSetPageUIClient(newPage, &otherPageUIClient);
162
163     WKRetain(newPage);
164     return newPage;
165 }
166
167 void TestController::initialize(int argc, const char* argv[])
168 {
169     platformInitialize();
170
171     bool printSupportedFeatures = false;
172
173     for (int i = 1; i < argc; ++i) {
174         std::string argument(argv[i]);
175
176         if (argument == "--timeout" && i + 1 < argc) {
177             m_longTimeout = atoi(argv[++i]);
178             // Scale up the short timeout to match.
179             m_shortTimeout = defaultShortTimeout * m_longTimeout / defaultLongTimeout;
180             continue;
181         }
182         if (argument == "--pixel-tests") {
183             m_dumpPixels = true;
184             continue;
185         }
186         if (argument == "--verbose") {
187             m_verbose = true;
188             continue;
189         }
190         if (argument == "--print-supported-features") {
191             printSupportedFeatures = true;
192             break;
193         }
194
195         // Skip any other arguments that begin with '--'.
196         if (argument.length() >= 2 && argument[0] == '-' && argument[1] == '-')
197             continue;
198
199         m_paths.push_back(argument);
200     }
201
202     if (printSupportedFeatures) {
203         // FIXME: On Windows, DumpRenderTree uses this to expose whether it supports 3d
204         // transforms and accelerated compositing. When we support those features, we
205         // should match DRT's behavior.
206         exit(0);
207     }
208
209     m_usingServerMode = (m_paths.size() == 1 && m_paths[0] == "-");
210     if (m_usingServerMode)
211         m_printSeparators = true;
212     else
213         m_printSeparators = m_paths.size() > 1;
214
215     initializeInjectedBundlePath();
216     initializeTestPluginDirectory();
217
218     WKRetainPtr<WKStringRef> pageGroupIdentifier(AdoptWK, WKStringCreateWithUTF8CString("WebKitTestRunnerPageGroup"));
219     m_pageGroup.adopt(WKPageGroupCreateWithIdentifier(pageGroupIdentifier.get()));
220
221     m_context.adopt(WKContextCreateWithInjectedBundlePath(injectedBundlePath()));
222     platformInitializeContext();
223
224     WKContextInjectedBundleClient injectedBundleClient = {
225         0,
226         this,
227         didReceiveMessageFromInjectedBundle,
228         didReceiveSynchronousMessageFromInjectedBundle
229     };
230     WKContextSetInjectedBundleClient(m_context.get(), &injectedBundleClient);
231
232     _WKContextSetAdditionalPluginsDirectory(m_context.get(), testPluginDirectory());
233
234     m_mainWebView = adoptPtr(new PlatformWebView(m_context.get(), m_pageGroup.get()));
235
236     WKPageUIClient pageUIClient = {
237         0,
238         this,
239         createOtherPage,
240         0, // showPage
241         0, // close
242         0, // runJavaScriptAlert        
243         0, // runJavaScriptConfirm
244         0, // runJavaScriptPrompt
245         0, // setStatusText
246         0, // mouseDidMoveOverElement
247         0, // missingPluginButtonClicked
248         0, // didNotHandleKeyEvent
249         0, // toolbarsAreVisible
250         0, // setToolbarsAreVisible
251         0, // menuBarIsVisible
252         0, // setMenuBarIsVisible
253         0, // statusBarIsVisible
254         0, // setStatusBarIsVisible
255         0, // isResizable
256         0, // setIsResizable
257         getWindowFrameMainPage,
258         setWindowFrameMainPage,
259         runBeforeUnloadConfirmPanel,
260         0, // didDraw
261         0, // pageDidScroll
262         0, // exceededDatabaseQuota
263         0, // runOpenPanel
264         0, // decidePolicyForGeolocationPermissionRequest
265         0, // headerHeight
266         0, // footerHeight
267         0, // drawHeader
268         0, // drawFooter
269         0, // printFrame
270         0, // runModal
271     };
272     WKPageSetPageUIClient(m_mainWebView->page(), &pageUIClient);
273
274     WKPageLoaderClient pageLoaderClient = {
275         0,
276         this,
277         0, // didStartProvisionalLoadForFrame
278         0, // didReceiveServerRedirectForProvisionalLoadForFrame
279         0, // didFailProvisionalLoadWithErrorForFrame
280         0, // didCommitLoadForFrame
281         0, // didFinishDocumentLoadForFrame
282         didFinishLoadForFrame,
283         0, // didFailLoadWithErrorForFrame
284         0, // didSameDocumentNavigationForFrame
285         0, // didReceiveTitleForFrame
286         0, // didFirstLayoutForFrame
287         0, // didFirstVisuallyNonEmptyLayoutForFrame
288         0, // didRemoveFrameFromHierarchy
289         0, // didDisplayInsecureContentForFrame
290         0, // didRunInsecureContentForFrame
291         0, // canAuthenticateAgainstProtectionSpaceInFrame
292         0, // didReceiveAuthenticationChallengeInFrame
293         0, // didStartProgress
294         0, // didChangeProgress
295         0, // didFinishProgress
296         0, // didBecomeUnresponsive
297         0, // didBecomeResponsive
298         processDidCrash, // processDidCrash
299         0 // didChangeBackForwardList
300     };
301     WKPageSetPageLoaderClient(m_mainWebView->page(), &pageLoaderClient);
302 }
303
304 bool TestController::resetStateToConsistentValues()
305 {
306     m_state = Resetting;
307
308     WKRetainPtr<WKStringRef> messageName(AdoptWK, WKStringCreateWithUTF8CString("Reset"));
309     WKContextPostMessageToInjectedBundle(TestController::shared().context(), messageName.get(), 0);
310
311     // FIXME: This function should also ensure that there is only one page open.
312
313     // Reset preferences
314     WKPreferencesRef preferences = WKPageGroupGetPreferences(m_pageGroup.get());
315     WKPreferencesSetOfflineWebApplicationCacheEnabled(preferences, true);
316     WKPreferencesSetFontSmoothingLevel(preferences, kWKFontSmoothingLevelNoSubpixelAntiAliasing);
317     WKPreferencesSetXSSAuditorEnabled(preferences, false);
318     WKPreferencesSetDeveloperExtrasEnabled(preferences, true);
319     WKPreferencesSetJavaScriptCanOpenWindowsAutomatically(preferences, true);
320
321     static WKStringRef standardFontFamily = WKStringCreateWithUTF8CString("Times");
322     static WKStringRef cursiveFontFamily = WKStringCreateWithUTF8CString("Apple Chancery");
323     static WKStringRef fantasyFontFamily = WKStringCreateWithUTF8CString("Papyrus");
324     static WKStringRef fixedFontFamily = WKStringCreateWithUTF8CString("Courier");
325     static WKStringRef sansSerifFontFamily = WKStringCreateWithUTF8CString("Helvetica");
326     static WKStringRef serifFontFamily = WKStringCreateWithUTF8CString("Times");
327
328     WKPreferencesSetStandardFontFamily(preferences, standardFontFamily);
329     WKPreferencesSetCursiveFontFamily(preferences, cursiveFontFamily);
330     WKPreferencesSetFantasyFontFamily(preferences, fantasyFontFamily);
331     WKPreferencesSetFixedFontFamily(preferences, fixedFontFamily);
332     WKPreferencesSetSansSerifFontFamily(preferences, sansSerifFontFamily);
333     WKPreferencesSetSerifFontFamily(preferences, serifFontFamily);
334
335     m_mainWebView->focus();
336
337     // Reset main page back to about:blank
338     m_doneResetting = false;
339
340     WKPageLoadURL(m_mainWebView->page(), blankURL());
341     runUntil(m_doneResetting, ShortTimeout);
342     return m_doneResetting;
343 }
344
345 bool TestController::runTest(const char* test)
346 {
347     if (!resetStateToConsistentValues()) {
348         fputs("#CRASHED - WebProcess\n", stderr);
349         fflush(stderr);
350         return false;
351     }
352
353     m_state = RunningTest;
354     m_currentInvocation.set(new TestInvocation(test));
355     m_currentInvocation->invoke();
356     m_currentInvocation.clear();
357
358     return true;
359 }
360
361 void TestController::runTestingServerLoop()
362 {
363     char filenameBuffer[2048];
364     while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
365         char *newLineCharacter = strchr(filenameBuffer, '\n');
366         if (newLineCharacter)
367             *newLineCharacter = '\0';
368
369         if (strlen(filenameBuffer) == 0)
370             continue;
371
372         if (!runTest(filenameBuffer))
373             break;
374     }
375 }
376
377 void TestController::run()
378 {
379     if (m_usingServerMode)
380         runTestingServerLoop();
381     else {
382         for (size_t i = 0; i < m_paths.size(); ++i) {
383             if (!runTest(m_paths[i].c_str()))
384                 break;
385         }
386     }
387
388 }
389
390 void TestController::runUntil(bool& done, TimeoutDuration timeoutDuration)
391 {
392     platformRunUntil(done, timeoutDuration == ShortTimeout ? m_shortTimeout : m_longTimeout);
393 }
394
395 // WKContextInjectedBundleClient
396
397 void TestController::didReceiveMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, const void* clientInfo)
398 {
399     static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveMessageFromInjectedBundle(messageName, messageBody);
400 }
401
402 void TestController::didReceiveSynchronousMessageFromInjectedBundle(WKContextRef context, WKStringRef messageName, WKTypeRef messageBody, WKTypeRef* returnData, const void* clientInfo)
403 {
404     *returnData = static_cast<TestController*>(const_cast<void*>(clientInfo))->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody).leakRef();
405 }
406
407 void TestController::didReceiveMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
408 {
409     if (!m_currentInvocation)
410         return;
411     m_currentInvocation->didReceiveMessageFromInjectedBundle(messageName, messageBody);
412 }
413
414 WKRetainPtr<WKTypeRef> TestController::didReceiveSynchronousMessageFromInjectedBundle(WKStringRef messageName, WKTypeRef messageBody)
415 {
416     return m_currentInvocation->didReceiveSynchronousMessageFromInjectedBundle(messageName, messageBody);
417 }
418
419 // WKPageLoaderClient
420
421 void TestController::didFinishLoadForFrame(WKPageRef page, WKFrameRef frame, WKTypeRef, const void* clientInfo)
422 {
423     static_cast<TestController*>(const_cast<void*>(clientInfo))->didFinishLoadForFrame(page, frame);
424 }
425
426 void TestController::processDidCrash(WKPageRef page, const void* clientInfo)
427 {
428     static_cast<TestController*>(const_cast<void*>(clientInfo))->processDidCrash(page);
429 }
430
431 void TestController::didFinishLoadForFrame(WKPageRef page, WKFrameRef frame)
432 {
433     if (m_state != Resetting)
434         return;
435
436     if (!WKFrameIsMainFrame(frame))
437         return;
438
439     WKRetainPtr<WKURLRef> wkURL(AdoptWK, WKFrameCopyURL(frame));
440     if (!WKURLIsEqual(wkURL.get(), blankURL()))
441         return;
442
443     m_doneResetting = true;
444     shared().notifyDone();
445 }
446
447 void TestController::processDidCrash(WKPageRef page)
448 {
449     fputs("#CRASHED - WebProcess\n", stderr);
450     fflush(stderr);
451 }
452
453 } // namespace WTR