2 * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * (C) 2007 Graham Dennis (graham.dennis@gmail.com)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #import "DumpRenderTree.h"
33 #import "AccessibilityController.h"
34 #import "CheckedMalloc.h"
35 #import "DumpRenderTreeDraggingInfo.h"
36 #import "DumpRenderTreePasteboard.h"
37 #import "DumpRenderTreeWindow.h"
38 #import "EditingDelegate.h"
39 #import "EventSendingController.h"
40 #import "FrameLoadDelegate.h"
41 #import "HistoryDelegate.h"
42 #import "JavaScriptThreading.h"
43 #import "LayoutTestController.h"
44 #import "MockGeolocationProvider.h"
45 #import "NavigationController.h"
46 #import "ObjCPlugin.h"
47 #import "ObjCPluginFunction.h"
48 #import "PixelDumpSupport.h"
49 #import "PolicyDelegate.h"
50 #import "ResourceLoadDelegate.h"
51 #import "UIDelegate.h"
52 #import "WebArchiveDumpSupport.h"
54 #import "WorkQueueItem.h"
55 #import <Carbon/Carbon.h>
56 #import <CoreFoundation/CoreFoundation.h>
57 #import <WebCore/FoundationExtras.h>
58 #import <WebKit/DOMElement.h>
59 #import <WebKit/DOMExtensions.h>
60 #import <WebKit/DOMRange.h>
61 #import <WebKit/WebArchive.h>
62 #import <WebKit/WebBackForwardList.h>
63 #import <WebKit/WebCache.h>
64 #import <WebKit/WebCoreStatistics.h>
65 #import <WebKit/WebDataSourcePrivate.h>
66 #import <WebKit/WebDatabaseManagerPrivate.h>
67 #import <WebKit/WebDocumentPrivate.h>
68 #import <WebKit/WebDeviceOrientationProviderMock.h>
69 #import <WebKit/WebEditingDelegate.h>
70 #import <WebKit/WebFrameView.h>
71 #import <WebKit/WebHistory.h>
72 #import <WebKit/WebHistoryItemPrivate.h>
73 #import <WebKit/WebInspector.h>
74 #import <WebKit/WebKitNSStringExtras.h>
75 #import <WebKit/WebPluginDatabase.h>
76 #import <WebKit/WebPreferences.h>
77 #import <WebKit/WebPreferencesPrivate.h>
78 #import <WebKit/WebPreferenceKeysPrivate.h>
79 #import <WebKit/WebResourceLoadDelegate.h>
80 #import <WebKit/WebTypesInternal.h>
81 #import <WebKit/WebViewPrivate.h>
83 #import <objc/objc-runtime.h>
84 #import <wtf/Assertions.h>
85 #import <wtf/RetainPtr.h>
86 #import <wtf/Threading.h>
87 #import <wtf/OwnPtr.h>
90 #import <mach-o/getsect.h>
95 @interface DumpRenderTreeApplication : NSApplication
98 @interface DumpRenderTreeEvent : NSEvent
101 @interface NSURLRequest (PrivateThingsWeShouldntReallyUse)
102 +(void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString *)host;
105 static void runTest(const string& testPathOrURL);
107 // Deciding when it's OK to dump out the state is a bit tricky. All these must be true:
108 // - There is no load in progress
109 // - There is no work queued up (see workQueue var, below)
110 // - waitToDump==NO. This means either waitUntilDone was never called, or it was called
111 // and notifyDone was called subsequently.
112 // Note that the call to notifyDone and the end of the load can happen in either order.
116 NavigationController* gNavigationController = 0;
117 RefPtr<LayoutTestController> gLayoutTestController;
119 WebFrame *mainFrame = 0;
120 // This is the topmost frame that is loading, during a given load, or nil when no load is
121 // in progress. Usually this is the same as the main frame, but not always. In the case
122 // where a frameset is loaded, and then new content is loaded into one of the child frames,
123 // that child frame is the "topmost frame that is loading".
124 WebFrame *topLoadingFrame = nil; // !nil iff a load is in progress
127 CFMutableSetRef disallowedURLs = 0;
128 CFRunLoopTimerRef waitToDumpWatchdog = 0;
131 static FrameLoadDelegate *frameLoadDelegate;
132 static UIDelegate *uiDelegate;
133 static EditingDelegate *editingDelegate;
134 static ResourceLoadDelegate *resourceLoadDelegate;
135 static HistoryDelegate *historyDelegate;
136 PolicyDelegate *policyDelegate;
138 static int dumpPixels;
140 static int dumpTree = YES;
141 static int forceComplexText;
142 static BOOL printSeparators;
143 static RetainPtr<CFStringRef> persistentUserStyleSheetLocation;
145 static WebHistoryItem *prevTestBFItem = nil; // current b/f item at the end of the previous test
148 static void swizzleAllMethods(Class imposter, Class original)
150 unsigned int imposterMethodCount;
151 Method* imposterMethods = class_copyMethodList(imposter, &imposterMethodCount);
153 unsigned int originalMethodCount;
154 Method* originalMethods = class_copyMethodList(original, &originalMethodCount);
156 for (unsigned int i = 0; i < imposterMethodCount; i++) {
157 SEL imposterMethodName = method_getName(imposterMethods[i]);
159 // Attempt to add the method to the original class. If it fails, the method already exists and we should
160 // instead exchange the implementations.
161 if (class_addMethod(original, imposterMethodName, method_getImplementation(imposterMethods[i]), method_getTypeEncoding(imposterMethods[i])))
165 for (; j < originalMethodCount; j++) {
166 SEL originalMethodName = method_getName(originalMethods[j]);
167 if (sel_isEqual(imposterMethodName, originalMethodName))
171 // If class_addMethod failed above then the method must exist on the original class.
172 ASSERT(j < originalMethodCount);
173 method_exchangeImplementations(imposterMethods[i], originalMethods[j]);
176 free(imposterMethods);
177 free(originalMethods);
181 static void poseAsClass(const char* imposter, const char* original)
183 Class imposterClass = objc_getClass(imposter);
184 Class originalClass = objc_getClass(original);
187 class_poseAs(imposterClass, originalClass);
190 // Swizzle instance methods
191 swizzleAllMethods(imposterClass, originalClass);
192 // and then class methods
193 swizzleAllMethods(object_getClass(imposterClass), object_getClass(originalClass));
197 void setPersistentUserStyleSheetLocation(CFStringRef url)
199 persistentUserStyleSheetLocation = url;
202 static bool shouldIgnoreWebCoreNodeLeaks(const string& URLString)
204 static char* const ignoreSet[] = {
205 // Keeping this infrastructure around in case we ever need it again.
207 static const int ignoreSetCount = sizeof(ignoreSet) / sizeof(char*);
209 for (int i = 0; i < ignoreSetCount; i++) {
210 // FIXME: ignore case
211 string curIgnore(ignoreSet[i]);
212 // Match at the end of the URLString
213 if (!URLString.compare(URLString.length() - curIgnore.length(), curIgnore.length(), curIgnore))
219 static void activateFonts()
221 #if defined(BUILDING_ON_LEOPARD) || defined(BUILDING_ON_TIGER)
222 static const char* fontSectionNames[] = {
236 for (unsigned i = 0; fontSectionNames[i]; ++i) {
237 unsigned long fontDataLength;
238 char* fontData = getsectdata("__DATA", fontSectionNames[i], &fontDataLength);
240 fprintf(stderr, "Failed to locate the %s font.\n", fontSectionNames[i]);
244 ATSFontContainerRef fontContainer;
245 OSStatus status = ATSFontActivateFromMemory(fontData, fontDataLength, kATSFontContextLocal, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault, &fontContainer);
247 if (status != noErr) {
248 fprintf(stderr, "Failed to activate the %s font.\n", fontSectionNames[i]);
254 // Work around <rdar://problem/6698023> by activating fonts from disk
255 // FIXME: This code can be removed once <rdar://problem/6698023> is addressed.
257 static const char* fontFileNames[] = {
260 "WebKitWeightWatcher100.ttf",
261 "WebKitWeightWatcher200.ttf",
262 "WebKitWeightWatcher300.ttf",
263 "WebKitWeightWatcher400.ttf",
264 "WebKitWeightWatcher500.ttf",
265 "WebKitWeightWatcher600.ttf",
266 "WebKitWeightWatcher700.ttf",
267 "WebKitWeightWatcher800.ttf",
268 "WebKitWeightWatcher900.ttf",
272 NSMutableArray *fontURLs = [NSMutableArray array];
273 NSURL *resourcesDirectory = [NSURL URLWithString:@"DumpRenderTree.resources" relativeToURL:[[NSBundle mainBundle] executableURL]];
274 for (unsigned i = 0; fontFileNames[i]; ++i) {
275 NSURL *fontURL = [resourcesDirectory URLByAppendingPathComponent:[NSString stringWithUTF8String:fontFileNames[i]]];
276 [fontURLs addObject:[fontURL absoluteURL]];
279 CFArrayRef errors = 0;
280 if (!CTFontManagerRegisterFontsForURLs((CFArrayRef)fontURLs, kCTFontManagerScopeProcess, &errors)) {
281 NSLog(@"Failed to activate fonts: %@", errors);
288 WebView *createWebViewAndOffscreenWindow()
290 NSRect rect = NSMakeRect(0, 0, LayoutTestController::maxViewWidth, LayoutTestController::maxViewHeight);
291 WebView *webView = [[WebView alloc] initWithFrame:rect frameName:nil groupName:@"org.webkit.DumpRenderTree"];
293 [webView setUIDelegate:uiDelegate];
294 [webView setFrameLoadDelegate:frameLoadDelegate];
295 [webView setEditingDelegate:editingDelegate];
296 [webView setResourceLoadDelegate:resourceLoadDelegate];
297 [webView _setGeolocationProvider:[MockGeolocationProvider shared]];
298 [webView _setDeviceOrientationProvider:[WebDeviceOrientationProviderMock shared]];
300 // Register the same schemes that Safari does
301 [WebView registerURLSchemeAsLocal:@"feed"];
302 [WebView registerURLSchemeAsLocal:@"feeds"];
303 [WebView registerURLSchemeAsLocal:@"feedsearch"];
305 [webView setContinuousSpellCheckingEnabled:YES];
307 // To make things like certain NSViews, dragging, and plug-ins work, put the WebView a window, but put it off-screen so you don't see it.
308 // Put it at -10000, -10000 in "flipped coordinates", since WebCore and the DOM use flipped coordinates.
309 NSRect windowRect = NSOffsetRect(rect, -10000, [[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
310 DumpRenderTreeWindow *window = [[DumpRenderTreeWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
312 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
313 [window setColorSpace:[[NSScreen mainScreen] colorSpace]];
316 [[window contentView] addSubview:webView];
317 [window orderBack:nil];
318 [window setAutodisplay:NO];
320 [window startListeningForAcceleratedCompositingChanges];
322 // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
323 // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
324 NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
325 [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
330 void testStringByEvaluatingJavaScriptFromString()
332 // maps expected result <= JavaScript expression
333 NSDictionary *expressions = [NSDictionary dictionaryWithObjectsAndKeys:
338 @"", @"new String()",
339 @"", @"new String('0')",
345 @"", @"(function() { throw 'error'; })()",
353 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
354 WebView *webView = [[WebView alloc] initWithFrame:NSZeroRect frameName:@"" groupName:@""];
356 NSEnumerator *enumerator = [expressions keyEnumerator];
358 while ((expression = [enumerator nextObject])) {
359 NSString *expectedResult = [expressions objectForKey:expression];
360 NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression];
361 assert([result isEqualToString:expectedResult]);
369 static NSString *libraryPathForDumpRenderTree()
371 //FIXME: This may not be sufficient to prevent interactions/crashes
372 //when running more than one copy of DumpRenderTree.
373 //See https://bugs.webkit.org/show_bug.cgi?id=10906
374 char* dumpRenderTreeTemp = getenv("DUMPRENDERTREE_TEMP");
375 if (dumpRenderTreeTemp)
376 return [[NSFileManager defaultManager] stringWithFileSystemRepresentation:dumpRenderTreeTemp length:strlen(dumpRenderTreeTemp)];
378 return [@"~/Library/Application Support/DumpRenderTree" stringByExpandingTildeInPath];
381 // Called before each test.
382 static void resetDefaultsToConsistentValues()
384 // Give some clear to undocumented defaults values
385 static const int NoFontSmoothing = 0;
386 static const int BlueTintedAppearance = 1;
388 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
389 [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"]; // smallest font size to CG should perform antialiasing on
390 [defaults setInteger:NoFontSmoothing forKey:@"AppleFontSmoothing"];
391 [defaults setInteger:BlueTintedAppearance forKey:@"AppleAquaColorVariant"];
392 [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
393 [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
394 [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
395 [defaults setBool:YES forKey:WebKitEnableFullDocumentTeardownPreferenceKey];
396 [defaults setBool:YES forKey:WebKitFullScreenEnabledPreferenceKey];
398 // Scrollbars are drawn either using AppKit (which uses NSUserDefaults) or using HIToolbox (which uses CFPreferences / kCFPreferencesAnyApplication / kCFPreferencesCurrentUser / kCFPreferencesAnyHost)
399 [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
400 RetainPtr<CFTypeRef> initialValue = CFPreferencesCopyValue(CFSTR("AppleScrollBarVariant"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
401 CFPreferencesSetValue(CFSTR("AppleScrollBarVariant"), CFSTR("DoubleMax"), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
403 // See <rdar://problem/6347388>.
404 ThemeScrollBarArrowStyle style;
405 GetThemeScrollBarArrowStyle(&style); // Force HIToolbox to read from CFPreferences
408 [defaults setBool:NO forKey:@"AppleScrollAnimationEnabled"];
409 [defaults setBool:NO forKey:@"NSOverlayScrollersEnabled"];
410 [defaults setObject:@"Always" forKey:@"AppleShowScrollBars"];
413 CFPreferencesSetValue(CFSTR("AppleScrollBarVariant"), initialValue.get(), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
415 NSString *path = libraryPathForDumpRenderTree();
416 [defaults setObject:[path stringByAppendingPathComponent:@"Databases"] forKey:WebDatabaseDirectoryDefaultsKey];
417 [defaults setObject:[path stringByAppendingPathComponent:@"LocalCache"] forKey:WebKitLocalCacheDefaultsKey];
419 WebPreferences *preferences = [WebPreferences standardPreferences];
421 [preferences setAllowUniversalAccessFromFileURLs:YES];
422 [preferences setAllowFileAccessFromFileURLs:YES];
423 [preferences setStandardFontFamily:@"Times"];
424 [preferences setFixedFontFamily:@"Courier"];
425 [preferences setSerifFontFamily:@"Times"];
426 [preferences setSansSerifFontFamily:@"Helvetica"];
427 [preferences setCursiveFontFamily:@"Apple Chancery"];
428 [preferences setFantasyFontFamily:@"Papyrus"];
429 [preferences setDefaultFontSize:16];
430 [preferences setDefaultFixedFontSize:13];
431 [preferences setMinimumFontSize:0];
432 [preferences setJavaEnabled:NO];
433 [preferences setJavaScriptEnabled:YES];
434 [preferences setEditableLinkBehavior:WebKitEditableLinkOnlyLiveWithShiftKey];
435 [preferences setTabsToLinks:NO];
436 [preferences setDOMPasteAllowed:YES];
437 [preferences setShouldPrintBackgrounds:YES];
438 [preferences setCacheModel:WebCacheModelDocumentBrowser];
439 [preferences setXSSAuditorEnabled:NO];
440 [preferences setExperimentalNotificationsEnabled:NO];
441 [preferences setPluginAllowedRunTime:1];
442 [preferences setPlugInsEnabled:YES];
444 [preferences setPrivateBrowsingEnabled:NO];
445 [preferences setAuthorAndUserStylesEnabled:YES];
446 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
447 [preferences setJavaScriptCanAccessClipboard:YES];
448 [preferences setOfflineWebApplicationCacheEnabled:YES];
449 [preferences setDeveloperExtrasEnabled:NO];
450 [preferences setLoadsImagesAutomatically:YES];
451 [preferences setFrameFlatteningEnabled:NO];
452 [preferences setSpatialNavigationEnabled:NO];
453 [preferences setEditingBehavior:WebKitEditingMacBehavior];
454 if (persistentUserStyleSheetLocation) {
455 [preferences setUserStyleSheetLocation:[NSURL URLWithString:(NSString *)(persistentUserStyleSheetLocation.get())]];
456 [preferences setUserStyleSheetEnabled:YES];
458 [preferences setUserStyleSheetEnabled:NO];
460 // The back/forward cache is causing problems due to layouts during transition from one page to another.
461 // So, turn it off for now, but we might want to turn it back on some day.
462 [preferences setUsesPageCache:NO];
463 [preferences setAcceleratedCompositingEnabled:YES];
464 [preferences setWebGLEnabled:NO];
465 [preferences setUsePreHTML5ParserQuirks:NO];
466 [preferences setAsynchronousSpellCheckingEnabled:NO];
468 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain];
470 LayoutTestController::setSerializeHTTPLoads(false);
472 setlocale(LC_ALL, "");
475 // Called once on DumpRenderTree startup.
476 static void setDefaultsToConsistentValuesForTesting()
478 resetDefaultsToConsistentValues();
480 NSString *path = libraryPathForDumpRenderTree();
481 NSURLCache *sharedCache =
482 [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024
484 diskPath:[path stringByAppendingPathComponent:@"URLCache"]];
485 [NSURLCache setSharedURLCache:sharedCache];
486 [sharedCache release];
490 static void* runThread(void* arg)
492 static ThreadIdentifier previousId = 0;
493 ThreadIdentifier currentId = currentThread();
494 // Verify 2 successive threads do not get the same Id.
495 ASSERT(previousId != currentId);
496 previousId = currentId;
500 static void testThreadIdentifierMap()
502 // Imitate 'foreign' threads that are not created by WTF.
504 pthread_create(&pthread, 0, &runThread, 0);
505 pthread_join(pthread, 0);
507 pthread_create(&pthread, 0, &runThread, 0);
508 pthread_join(pthread, 0);
510 // Now create another thread using WTF. On OSX, it will have the same pthread handle
511 // but should get a different ThreadIdentifier.
512 createThread(runThread, 0, "DumpRenderTree: test");
515 static void crashHandler(int sig)
517 char *signalName = strsignal(sig);
518 write(STDERR_FILENO, signalName, strlen(signalName));
519 write(STDERR_FILENO, "\n", 1);
520 restoreMainDisplayColorProfile(0);
524 static void installSignalHandlers()
526 signal(SIGILL, crashHandler); /* 4: illegal instruction (not reset when caught) */
527 signal(SIGTRAP, crashHandler); /* 5: trace trap (not reset when caught) */
528 signal(SIGEMT, crashHandler); /* 7: EMT instruction */
529 signal(SIGFPE, crashHandler); /* 8: floating point exception */
530 signal(SIGBUS, crashHandler); /* 10: bus error */
531 signal(SIGSEGV, crashHandler); /* 11: segmentation violation */
532 signal(SIGSYS, crashHandler); /* 12: bad argument to system call */
533 signal(SIGPIPE, crashHandler); /* 13: write on a pipe with no reader */
534 signal(SIGXCPU, crashHandler); /* 24: exceeded CPU time limit */
535 signal(SIGXFSZ, crashHandler); /* 25: exceeded file size limit */
538 static void allocateGlobalControllers()
540 // FIXME: We should remove these and move to the ObjC standard [Foo sharedInstance] model
541 gNavigationController = [[NavigationController alloc] init];
542 frameLoadDelegate = [[FrameLoadDelegate alloc] init];
543 uiDelegate = [[UIDelegate alloc] init];
544 editingDelegate = [[EditingDelegate alloc] init];
545 resourceLoadDelegate = [[ResourceLoadDelegate alloc] init];
546 policyDelegate = [[PolicyDelegate alloc] init];
547 historyDelegate = [[HistoryDelegate alloc] init];
550 // ObjC++ doens't seem to let me pass NSObject*& sadly.
551 static inline void releaseAndZero(NSObject** object)
557 static void releaseGlobalControllers()
559 releaseAndZero(&gNavigationController);
560 releaseAndZero(&frameLoadDelegate);
561 releaseAndZero(&editingDelegate);
562 releaseAndZero(&resourceLoadDelegate);
563 releaseAndZero(&uiDelegate);
564 releaseAndZero(&policyDelegate);
567 static void initializeGlobalsFromCommandLineOptions(int argc, const char *argv[])
569 struct option options[] = {
570 {"notree", no_argument, &dumpTree, NO},
571 {"pixel-tests", no_argument, &dumpPixels, YES},
572 {"tree", no_argument, &dumpTree, YES},
573 {"threaded", no_argument, &threaded, YES},
574 {"complex-text", no_argument, &forceComplexText, YES},
579 while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1) {
581 case '?': // unknown or ambiguous option
582 case ':': // missing argument
589 static void addTestPluginsToPluginSearchPath(const char* executablePath)
591 NSString *pwd = [[NSString stringWithUTF8String:executablePath] stringByDeletingLastPathComponent];
592 [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
593 [[WebPluginDatabase sharedDatabase] refresh];
596 static bool useLongRunningServerMode(int argc, const char *argv[])
598 // This assumes you've already called getopt_long
599 return (argc == optind+1 && strcmp(argv[optind], "-") == 0);
602 static void runTestingServerLoop()
604 // When DumpRenderTree run in server mode, we just wait around for file names
605 // to be passed to us and read each in turn, passing the results back to the client
606 char filenameBuffer[2048];
607 while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
608 char *newLineCharacter = strchr(filenameBuffer, '\n');
609 if (newLineCharacter)
610 *newLineCharacter = '\0';
612 if (strlen(filenameBuffer) == 0)
615 runTest(filenameBuffer);
619 static void prepareConsistentTestingEnvironment()
621 poseAsClass("DumpRenderTreePasteboard", "NSPasteboard");
622 poseAsClass("DumpRenderTreeEvent", "NSEvent");
624 setDefaultsToConsistentValuesForTesting();
628 setupMainDisplayColorProfile();
629 allocateGlobalControllers();
631 makeLargeMallocFailSilently();
634 void dumpRenderTree(int argc, const char *argv[])
636 initializeGlobalsFromCommandLineOptions(argc, argv);
637 prepareConsistentTestingEnvironment();
638 addTestPluginsToPluginSearchPath(argv[0]);
640 installSignalHandlers();
642 if (forceComplexText)
643 [WebView _setAlwaysUsesComplexTextCodePath:YES];
645 WebView *webView = createWebViewAndOffscreenWindow();
646 mainFrame = [webView mainFrame];
648 [[NSURLCache sharedURLCache] removeAllCachedResponses];
651 // <http://webkit.org/b/31200> In order to prevent extra frame load delegate logging being generated if the first test to use SSL
652 // is set to log frame load delegate calls we ignore SSL certificate errors on localhost and 127.0.0.1.
653 #if BUILDING_ON_TIGER
654 // Initialize internal NSURLRequest data for setAllowsAnyHTTPSCertificate:forHost: to work properly.
655 [[[[NSURLRequest alloc] init] autorelease] HTTPMethod];
657 [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"localhost"];
658 [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"127.0.0.1"];
660 // <rdar://problem/5222911>
661 testStringByEvaluatingJavaScriptFromString();
663 // http://webkit.org/b/32689
664 testThreadIdentifierMap();
667 startJavaScriptThreads();
669 if (useLongRunningServerMode(argc, argv)) {
670 printSeparators = YES;
671 runTestingServerLoop();
673 printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
674 for (int i = optind; i != argc; ++i)
679 stopJavaScriptThreads();
681 NSWindow *window = [webView window];
685 // Work around problem where registering drag types leaves an outstanding
686 // "perform selector" on the window, which retains the window. It's a bit
687 // inelegant and perhaps dangerous to just blow them all away, but in practice
688 // it probably won't cause any trouble (and this is just a test tool, after all).
689 [NSObject cancelPreviousPerformRequestsWithTarget:window];
691 [window close]; // releases when closed
694 releaseGlobalControllers();
696 [DumpRenderTreePasteboard releaseLocalPasteboards];
698 // FIXME: This should be moved onto LayoutTestController and made into a HashSet
699 if (disallowedURLs) {
700 CFRelease(disallowedURLs);
705 restoreMainDisplayColorProfile(0);
708 int main(int argc, const char *argv[])
710 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
711 [DumpRenderTreeApplication sharedApplication]; // Force AppKit to init itself
712 dumpRenderTree(argc, argv);
713 [WebCoreStatistics garbageCollectJavaScriptObjects];
714 [WebCoreStatistics emptyCache]; // Otherwise SVGImages trigger false positives for Frame/Node counts
719 static NSInteger compareHistoryItems(id item1, id item2, void *context)
721 return [[item1 target] caseInsensitiveCompare:[item2 target]];
724 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
731 for (int i = start; i < indent; i++)
734 NSString *urlString = [item URLString];
735 if ([[NSURL URLWithString:urlString] isFileURL]) {
736 NSRange range = [urlString rangeOfString:@"/LayoutTests/"];
737 urlString = [@"(file test):" stringByAppendingString:[urlString substringFromIndex:(range.length + range.location)]];
740 printf("%s", [urlString UTF8String]);
741 NSString *target = [item target];
742 if (target && [target length] > 0)
743 printf(" (in frame \"%s\")", [target UTF8String]);
744 if ([item isTargetItem])
745 printf(" **nav target**");
747 NSArray *kids = [item children];
749 // must sort to eliminate arbitrary result ordering which defeats reproducible testing
750 kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
751 for (unsigned i = 0; i < [kids count]; i++)
752 dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
756 static void dumpFrameScrollPosition(WebFrame *f)
758 WebScriptObject* scriptObject = [f windowObject];
759 NSPoint scrollPosition = NSMakePoint(
760 [[scriptObject valueForKey:@"pageXOffset"] floatValue],
761 [[scriptObject valueForKey:@"pageYOffset"] floatValue]);
762 if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
763 if ([f parentFrame] != nil)
764 printf("frame '%s' ", [[f name] UTF8String]);
765 printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
768 if (gLayoutTestController->dumpChildFrameScrollPositions()) {
769 NSArray *kids = [f childFrames];
771 for (unsigned i = 0; i < [kids count]; i++)
772 dumpFrameScrollPosition([kids objectAtIndex:i]);
776 static NSString *dumpFramesAsText(WebFrame *frame)
778 DOMDocument *document = [frame DOMDocument];
779 DOMElement *documentElement = [document documentElement];
781 if (!documentElement)
784 NSMutableString *result = [[[NSMutableString alloc] init] autorelease];
786 // Add header for all but the main frame.
787 if ([frame parentFrame])
788 result = [NSMutableString stringWithFormat:@"\n--------\nFrame: '%@'\n--------\n", [frame name]];
790 [result appendFormat:@"%@\n", [documentElement innerText]];
792 if (gLayoutTestController->dumpChildFramesAsText()) {
793 NSArray *kids = [frame childFrames];
795 for (unsigned i = 0; i < [kids count]; i++)
796 [result appendString:dumpFramesAsText([kids objectAtIndex:i])];
803 static NSData *dumpFrameAsPDF(WebFrame *frame)
808 // Sadly we have to dump to a file and then read from that file again
809 // +[NSPrintOperation PDFOperationWithView:insideRect:] requires a rect and prints to a single page
810 // likewise +[NSView dataWithPDFInsideRect:] also prints to a single continuous page
811 // The goal of this function is to test "real" printing across multiple pages.
812 // FIXME: It's possible there might be printing SPI to let us print a multi-page PDF to an NSData object
813 NSString *path = [libraryPathForDumpRenderTree() stringByAppendingPathComponent:@"test.pdf"];
815 NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]];
816 [printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
817 [printInfoDict setObject:path forKey:NSPrintSavePath];
819 NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
820 [printInfo setHorizontalPagination:NSAutoPagination];
821 [printInfo setVerticalPagination:NSAutoPagination];
822 [printInfo setVerticallyCentered:NO];
824 NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:[frame frameView] printInfo:printInfo];
825 [printOperation setShowPanels:NO];
826 [printOperation runOperation];
830 NSData *pdfData = [NSData dataWithContentsOfFile:path];
831 [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
836 static void dumpBackForwardListForWebView(WebView *view)
838 printf("\n============== Back Forward List ==============\n");
839 WebBackForwardList *bfList = [view backForwardList];
841 // Print out all items in the list after prevTestBFItem, which was from the previous test
842 // Gather items from the end of the list, the print them out from oldest to newest
843 NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
844 for (int i = [bfList forwardListCount]; i > 0; i--) {
845 WebHistoryItem *item = [bfList itemAtIndex:i];
846 // something is wrong if the item from the last test is in the forward part of the b/f list
847 assert(item != prevTestBFItem);
848 [itemsToPrint addObject:item];
851 assert([bfList currentItem] != prevTestBFItem);
852 [itemsToPrint addObject:[bfList currentItem]];
853 int currentItemIndex = [itemsToPrint count] - 1;
855 for (int i = -1; i >= -[bfList backListCount]; i--) {
856 WebHistoryItem *item = [bfList itemAtIndex:i];
857 if (item == prevTestBFItem)
859 [itemsToPrint addObject:item];
862 for (int i = [itemsToPrint count]-1; i >= 0; i--)
863 dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
865 [itemsToPrint release];
866 printf("===============================================\n");
869 static void sizeWebViewForCurrentTest()
871 // W3C SVG tests expect to be 480x360
872 bool isSVGW3CTest = (gLayoutTestController->testPathOrURL().find("svg/W3C-SVG-1.1") != string::npos);
874 [[mainFrame webView] setFrameSize:NSMakeSize(480, 360)];
876 [[mainFrame webView] setFrameSize:NSMakeSize(LayoutTestController::maxViewWidth, LayoutTestController::maxViewHeight)];
879 static const char *methodNameStringForFailedTest()
881 const char *errorMessage;
882 if (gLayoutTestController->dumpAsText())
883 errorMessage = "[documentElement innerText]";
884 else if (gLayoutTestController->dumpDOMAsWebArchive())
885 errorMessage = "[[mainFrame DOMDocument] webArchive]";
886 else if (gLayoutTestController->dumpSourceAsWebArchive())
887 errorMessage = "[[mainFrame dataSource] webArchive]";
889 errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
894 static void dumpBackForwardListForAllWindows()
896 CFArrayRef openWindows = (CFArrayRef)[DumpRenderTreeWindow openWindows];
897 unsigned count = CFArrayGetCount(openWindows);
898 for (unsigned i = 0; i < count; i++) {
899 NSWindow *window = (NSWindow *)CFArrayGetValueAtIndex(openWindows, i);
900 WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
901 dumpBackForwardListForWebView(webView);
905 static void invalidateAnyPreviousWaitToDumpWatchdog()
907 if (waitToDumpWatchdog) {
908 CFRunLoopTimerInvalidate(waitToDumpWatchdog);
909 CFRelease(waitToDumpWatchdog);
910 waitToDumpWatchdog = 0;
916 invalidateAnyPreviousWaitToDumpWatchdog();
919 NSString *resultString = nil;
920 NSData *resultData = nil;
921 NSString *resultMimeType = @"text/plain";
923 if ([[[mainFrame dataSource] _responseMIMEType] isEqualToString:@"text/plain"]) {
924 gLayoutTestController->setDumpAsText(true);
925 gLayoutTestController->setGeneratePixelResults(false);
927 if (gLayoutTestController->dumpAsText()) {
928 resultString = dumpFramesAsText(mainFrame);
929 } else if (gLayoutTestController->dumpAsPDF()) {
930 resultData = dumpFrameAsPDF(mainFrame);
931 resultMimeType = @"application/pdf";
932 } else if (gLayoutTestController->dumpDOMAsWebArchive()) {
933 WebArchive *webArchive = [[mainFrame DOMDocument] webArchive];
934 resultString = HardAutorelease(createXMLStringFromWebArchiveData((CFDataRef)[webArchive data]));
935 resultMimeType = @"application/x-webarchive";
936 } else if (gLayoutTestController->dumpSourceAsWebArchive()) {
937 WebArchive *webArchive = [[mainFrame dataSource] webArchive];
938 resultString = HardAutorelease(createXMLStringFromWebArchiveData((CFDataRef)[webArchive data]));
939 resultMimeType = @"application/x-webarchive";
941 sizeWebViewForCurrentTest();
942 resultString = [mainFrame renderTreeAsExternalRepresentationForPrinting:gLayoutTestController->isPrinting()];
945 if (resultString && !resultData)
946 resultData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
948 printf("Content-Type: %s\n", [resultMimeType UTF8String]);
951 fwrite([resultData bytes], 1, [resultData length], stdout);
953 if (!gLayoutTestController->dumpAsText() && !gLayoutTestController->dumpDOMAsWebArchive() && !gLayoutTestController->dumpSourceAsWebArchive())
954 dumpFrameScrollPosition(mainFrame);
956 if (gLayoutTestController->dumpBackForwardList())
957 dumpBackForwardListForAllWindows();
959 printf("ERROR: nil result from %s", methodNameStringForFailedTest());
961 // Stop the watchdog thread before we leave this test to make sure it doesn't
962 // fire in between tests causing the next test to fail.
963 // This is a speculative fix for: https://bugs.webkit.org/show_bug.cgi?id=32339
964 invalidateAnyPreviousWaitToDumpWatchdog();
966 if (printSeparators) {
967 puts("#EOF"); // terminate the content block
968 fputs("#EOF\n", stderr);
972 if (dumpPixels && gLayoutTestController->generatePixelResults())
973 // FIXME: when isPrinting is set, dump the image with page separators.
974 dumpWebViewAsPixelsAndCompareWithExpected(gLayoutTestController->expectedPixelHash());
976 puts("#EOF"); // terminate the (possibly empty) pixels block
984 static bool shouldLogFrameLoadDelegates(const char* pathOrURL)
986 return strstr(pathOrURL, "loading/");
989 static bool shouldLogHistoryDelegates(const char* pathOrURL)
991 return strstr(pathOrURL, "globalhistory/");
994 static bool shouldOpenWebInspector(const char* pathOrURL)
996 return strstr(pathOrURL, "inspector/");
999 static bool shouldDumpAsText(const char* pathOrURL)
1001 return strstr(pathOrURL, "dumpAsText/");
1004 static bool shouldEnableDeveloperExtras(const char* pathOrURL)
1009 static void resetWebViewToConsistentStateBeforeTesting()
1011 WebView *webView = [mainFrame webView];
1012 [webView setEditable:NO];
1013 [(EditingDelegate *)[webView editingDelegate] setAcceptsEditing:YES];
1014 [webView makeTextStandardSize:nil];
1015 [webView resetPageZoom:nil];
1016 [webView setTabKeyCyclesThroughElements:YES];
1017 [webView setPolicyDelegate:nil];
1018 [policyDelegate setPermissive:NO];
1019 [policyDelegate setControllerToNotifyDone:0];
1020 [frameLoadDelegate resetToConsistentState];
1021 [webView _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:NO];
1022 [webView _clearMainFrameName];
1023 [[webView undoManager] removeAllActions];
1024 [WebView _removeAllUserContentFromGroup:[webView groupName]];
1025 [[webView window] setAutodisplay:NO];
1026 [webView _setMinimumTimerInterval:[WebView _defaultMinimumTimerInterval]];
1028 resetDefaultsToConsistentValues();
1030 [[mainFrame webView] setSmartInsertDeleteEnabled:YES];
1031 [[[mainFrame webView] inspector] setJavaScriptProfilingEnabled:NO];
1033 [WebView _setUsesTestModeFocusRingColor:YES];
1034 [WebView _resetOriginAccessWhitelists];
1036 [[MockGeolocationProvider shared] stopTimer];
1038 // Clear the contents of the general pasteboard
1039 [[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
1041 [mainFrame _clearOpener];
1044 static void runTest(const string& testPathOrURL)
1046 ASSERT(!testPathOrURL.empty());
1048 // Look for "'" as a separator between the path or URL, and the pixel dump hash that follows.
1049 string pathOrURL(testPathOrURL);
1050 string expectedPixelHash;
1052 size_t separatorPos = pathOrURL.find("'");
1053 if (separatorPos != string::npos) {
1054 pathOrURL = string(testPathOrURL, 0, separatorPos);
1055 expectedPixelHash = string(testPathOrURL, separatorPos + 1);
1058 NSString *pathOrURLString = [NSString stringWithUTF8String:pathOrURL.c_str()];
1059 if (!pathOrURLString) {
1060 fprintf(stderr, "Failed to parse \"%s\" as UTF-8\n", pathOrURL.c_str());
1065 if ([pathOrURLString hasPrefix:@"http://"] || [pathOrURLString hasPrefix:@"https://"])
1066 url = [NSURL URLWithString:pathOrURLString];
1068 url = [NSURL fileURLWithPath:pathOrURLString];
1070 fprintf(stderr, "Failed to parse \"%s\" as a URL\n", pathOrURL.c_str());
1074 const string testURL([[url absoluteString] UTF8String]);
1076 resetWebViewToConsistentStateBeforeTesting();
1078 gLayoutTestController = LayoutTestController::create(testURL, expectedPixelHash);
1079 topLoadingFrame = nil;
1080 ASSERT(!draggingInfo); // the previous test should have called eventSender.mouseUp to drop!
1081 releaseAndZero(&draggingInfo);
1084 gLayoutTestController->setIconDatabaseEnabled(false);
1087 CFSetRemoveAllValues(disallowedURLs);
1088 if (shouldLogFrameLoadDelegates(pathOrURL.c_str()))
1089 gLayoutTestController->setDumpFrameLoadCallbacks(true);
1091 if (shouldLogHistoryDelegates(pathOrURL.c_str()))
1092 [[mainFrame webView] setHistoryDelegate:historyDelegate];
1094 [[mainFrame webView] setHistoryDelegate:nil];
1096 if (shouldEnableDeveloperExtras(pathOrURL.c_str())) {
1097 gLayoutTestController->setDeveloperExtrasEnabled(true);
1098 if (shouldOpenWebInspector(pathOrURL.c_str()))
1099 gLayoutTestController->showWebInspector();
1100 if (shouldDumpAsText(pathOrURL.c_str())) {
1101 gLayoutTestController->setDumpAsText(true);
1102 gLayoutTestController->setGeneratePixelResults(false);
1106 if ([WebHistory optionalSharedHistory])
1107 [WebHistory setOptionalSharedHistory:nil];
1108 lastMousePosition = NSZeroPoint;
1109 lastClickPosition = NSZeroPoint;
1111 [prevTestBFItem release];
1112 prevTestBFItem = [[[[mainFrame webView] backForwardList] currentItem] retain];
1114 WorkQueue::shared()->clear();
1115 WorkQueue::shared()->setFrozen(false);
1117 bool ignoreWebCoreNodeLeaks = shouldIgnoreWebCoreNodeLeaks(testURL);
1118 if (ignoreWebCoreNodeLeaks)
1119 [WebCoreStatistics startIgnoringWebCoreNodeLeaks];
1121 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1122 [mainFrame loadRequest:[NSURLRequest requestWithURL:url]];
1126 pool = [[NSAutoreleasePool alloc] init];
1127 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1131 pool = [[NSAutoreleasePool alloc] init];
1132 [EventSendingController clearSavedEvents];
1133 [[mainFrame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1135 WorkQueue::shared()->clear();
1137 if (gLayoutTestController->closeRemainingWindowsWhenComplete()) {
1138 NSArray* array = [DumpRenderTreeWindow openWindows];
1140 unsigned count = [array count];
1141 for (unsigned i = 0; i < count; i++) {
1142 NSWindow *window = [array objectAtIndex:i];
1144 // Don't try to close the main window
1145 if (window == [[mainFrame webView] window])
1148 WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
1155 // If developer extras enabled Web Inspector may have been open by the test.
1156 if (shouldEnableDeveloperExtras(pathOrURL.c_str())) {
1157 gLayoutTestController->closeWebInspector();
1158 gLayoutTestController->setDeveloperExtrasEnabled(false);
1161 resetWebViewToConsistentStateBeforeTesting();
1163 [mainFrame loadHTMLString:@"<html></html>" baseURL:[NSURL URLWithString:@"about:blank"]];
1164 [mainFrame stopLoading];
1168 // We should only have our main window left open when we're done
1169 ASSERT(CFArrayGetCount(openWindowsRef) == 1);
1170 ASSERT(CFArrayGetValueAtIndex(openWindowsRef, 0) == [[mainFrame webView] window]);
1172 gLayoutTestController.clear();
1174 if (ignoreWebCoreNodeLeaks)
1175 [WebCoreStatistics stopIgnoringWebCoreNodeLeaks];
1178 void displayWebView()
1180 NSView *webView = [mainFrame webView];
1182 [webView lockFocus];
1183 [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
1184 NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
1185 [webView unlockFocus];
1188 @implementation DumpRenderTreeEvent
1190 + (NSPoint)mouseLocation
1192 return [[[mainFrame webView] window] convertBaseToScreen:lastMousePosition];
1197 @implementation DumpRenderTreeApplication
1201 // <rdar://problem/7686123> Java plug-in freezes unless NSApplication is running