OSDN Git Service

am 06295b42: (-s ours) am 4ce956cc: am 3d487c65: Merge "DO NOT MERGE-Move check for...
[android-x86/external-webkit.git] / Tools / DumpRenderTree / mac / DumpRenderTree.mm
1 /*
2  * Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  *           (C) 2007 Graham Dennis (graham.dennis@gmail.com)
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  *
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. 
17  *
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.
28  */
29
30 #import "config.h"
31 #import "DumpRenderTree.h"
32
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"
53 #import "WorkQueue.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>
82 #import <getopt.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>
88
89 extern "C" {
90 #import <mach-o/getsect.h>
91 }
92
93 using namespace std;
94
95 @interface DumpRenderTreeApplication : NSApplication
96 @end
97
98 @interface DumpRenderTreeEvent : NSEvent
99 @end
100
101 @interface NSURLRequest (PrivateThingsWeShouldntReallyUse)
102 +(void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString *)host;
103 @end
104
105 static void runTest(const string& testPathOrURL);
106
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.
113
114 volatile bool done;
115
116 NavigationController* gNavigationController = 0;
117 RefPtr<LayoutTestController> gLayoutTestController;
118
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
125
126
127 CFMutableSetRef disallowedURLs = 0;
128 CFRunLoopTimerRef waitToDumpWatchdog = 0;
129
130 // Delegates
131 static FrameLoadDelegate *frameLoadDelegate;
132 static UIDelegate *uiDelegate;
133 static EditingDelegate *editingDelegate;
134 static ResourceLoadDelegate *resourceLoadDelegate;
135 static HistoryDelegate *historyDelegate;
136 PolicyDelegate *policyDelegate;
137
138 static int dumpPixels;
139 static int threaded;
140 static int dumpTree = YES;
141 static int forceComplexText;
142 static BOOL printSeparators;
143 static RetainPtr<CFStringRef> persistentUserStyleSheetLocation;
144
145 static WebHistoryItem *prevTestBFItem = nil;  // current b/f item at the end of the previous test
146
147 #if __OBJC2__
148 static void swizzleAllMethods(Class imposter, Class original)
149 {
150     unsigned int imposterMethodCount;
151     Method* imposterMethods = class_copyMethodList(imposter, &imposterMethodCount);
152
153     unsigned int originalMethodCount;
154     Method* originalMethods = class_copyMethodList(original, &originalMethodCount);
155
156     for (unsigned int i = 0; i < imposterMethodCount; i++) {
157         SEL imposterMethodName = method_getName(imposterMethods[i]);
158
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])))
162             continue;
163
164         unsigned int j = 0;
165         for (; j < originalMethodCount; j++) {
166             SEL originalMethodName = method_getName(originalMethods[j]);
167             if (sel_isEqual(imposterMethodName, originalMethodName))
168                 break;
169         }
170
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]);
174     }
175
176     free(imposterMethods);
177     free(originalMethods);
178 }
179 #endif
180
181 static void poseAsClass(const char* imposter, const char* original)
182 {
183     Class imposterClass = objc_getClass(imposter);
184     Class originalClass = objc_getClass(original);
185
186 #if !__OBJC2__
187     class_poseAs(imposterClass, originalClass);
188 #else
189
190     // Swizzle instance methods
191     swizzleAllMethods(imposterClass, originalClass);
192     // and then class methods
193     swizzleAllMethods(object_getClass(imposterClass), object_getClass(originalClass));
194 #endif
195 }
196
197 void setPersistentUserStyleSheetLocation(CFStringRef url)
198 {
199     persistentUserStyleSheetLocation = url;
200 }
201
202 static bool shouldIgnoreWebCoreNodeLeaks(const string& URLString)
203 {
204     static char* const ignoreSet[] = {
205         // Keeping this infrastructure around in case we ever need it again.
206     };
207     static const int ignoreSetCount = sizeof(ignoreSet) / sizeof(char*);
208     
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))
214             return true;
215     }
216     return false;
217 }
218
219 static void activateFonts()
220 {
221 #if defined(BUILDING_ON_LEOPARD) || defined(BUILDING_ON_TIGER)
222     static const char* fontSectionNames[] = {
223         "Ahem",
224         "WeightWatcher100",
225         "WeightWatcher200",
226         "WeightWatcher300",
227         "WeightWatcher400",
228         "WeightWatcher500",
229         "WeightWatcher600",
230         "WeightWatcher700",
231         "WeightWatcher800",
232         "WeightWatcher900",
233         0
234     };
235
236     for (unsigned i = 0; fontSectionNames[i]; ++i) {
237         unsigned long fontDataLength;
238         char* fontData = getsectdata("__DATA", fontSectionNames[i], &fontDataLength);
239         if (!fontData) {
240             fprintf(stderr, "Failed to locate the %s font.\n", fontSectionNames[i]);
241             exit(1);
242         }
243
244         ATSFontContainerRef fontContainer;
245         OSStatus status = ATSFontActivateFromMemory(fontData, fontDataLength, kATSFontContextLocal, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault, &fontContainer);
246
247         if (status != noErr) {
248             fprintf(stderr, "Failed to activate the %s font.\n", fontSectionNames[i]);
249             exit(1);
250         }
251     }
252 #else
253
254     // Work around <rdar://problem/6698023> by activating fonts from disk
255     // FIXME: This code can be removed once <rdar://problem/6698023> is addressed.
256
257     static const char* fontFileNames[] = {
258         "AHEM____.TTF",
259         "ColorBits.ttf",
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",
269         0
270     };
271
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]];
277     }
278
279     CFArrayRef errors = 0;
280     if (!CTFontManagerRegisterFontsForURLs((CFArrayRef)fontURLs, kCTFontManagerScopeProcess, &errors)) {
281         NSLog(@"Failed to activate fonts: %@", errors);
282         CFRelease(errors);
283         exit(1);
284     }
285 #endif
286 }
287
288 WebView *createWebViewAndOffscreenWindow()
289 {
290     NSRect rect = NSMakeRect(0, 0, LayoutTestController::maxViewWidth, LayoutTestController::maxViewHeight);
291     WebView *webView = [[WebView alloc] initWithFrame:rect frameName:nil groupName:@"org.webkit.DumpRenderTree"];
292         
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]];
299
300     // Register the same schemes that Safari does
301     [WebView registerURLSchemeAsLocal:@"feed"];
302     [WebView registerURLSchemeAsLocal:@"feeds"];
303     [WebView registerURLSchemeAsLocal:@"feedsearch"];
304     
305     [webView setContinuousSpellCheckingEnabled:YES];
306     
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];
311
312 #if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
313     [window setColorSpace:[[NSScreen mainScreen] colorSpace]];
314 #endif
315
316     [[window contentView] addSubview:webView];
317     [window orderBack:nil];
318     [window setAutodisplay:NO];
319
320     [window startListeningForAcceleratedCompositingChanges];
321     
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];
326         
327     return webView;
328 }
329
330 void testStringByEvaluatingJavaScriptFromString()
331 {
332     // maps expected result <= JavaScript expression
333     NSDictionary *expressions = [NSDictionary dictionaryWithObjectsAndKeys:
334         @"0", @"0", 
335         @"0", @"'0'", 
336         @"", @"",
337         @"", @"''", 
338         @"", @"new String()", 
339         @"", @"new String('0')", 
340         @"", @"throw 1", 
341         @"", @"{ }", 
342         @"", @"[ ]", 
343         @"", @"//", 
344         @"", @"a.b.c", 
345         @"", @"(function() { throw 'error'; })()", 
346         @"", @"null",
347         @"", @"undefined",
348         @"true", @"true",
349         @"false", @"false",
350         nil
351     ];
352
353     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
354     WebView *webView = [[WebView alloc] initWithFrame:NSZeroRect frameName:@"" groupName:@""];
355
356     NSEnumerator *enumerator = [expressions keyEnumerator];
357     id expression;
358     while ((expression = [enumerator nextObject])) {
359         NSString *expectedResult = [expressions objectForKey:expression];
360         NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression];
361         assert([result isEqualToString:expectedResult]);
362     }
363
364     [webView close];
365     [webView release];
366     [pool release];
367 }
368
369 static NSString *libraryPathForDumpRenderTree()
370 {
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)];
377     else
378         return [@"~/Library/Application Support/DumpRenderTree" stringByExpandingTildeInPath];
379 }
380
381 // Called before each test.
382 static void resetDefaultsToConsistentValues()
383 {
384     // Give some clear to undocumented defaults values
385     static const int NoFontSmoothing = 0;
386     static const int BlueTintedAppearance = 1;
387
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];
397
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);
402 #ifndef __LP64__
403     // See <rdar://problem/6347388>.
404     ThemeScrollBarArrowStyle style;
405     GetThemeScrollBarArrowStyle(&style); // Force HIToolbox to read from CFPreferences
406 #endif
407
408     [defaults setBool:NO forKey:@"AppleScrollAnimationEnabled"];
409     [defaults setBool:NO forKey:@"NSOverlayScrollersEnabled"];
410
411     if (initialValue)
412         CFPreferencesSetValue(CFSTR("AppleScrollBarVariant"), initialValue.get(), kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
413
414     NSString *path = libraryPathForDumpRenderTree();
415     [defaults setObject:[path stringByAppendingPathComponent:@"Databases"] forKey:WebDatabaseDirectoryDefaultsKey];
416     [defaults setObject:[path stringByAppendingPathComponent:@"LocalCache"] forKey:WebKitLocalCacheDefaultsKey];
417
418     WebPreferences *preferences = [WebPreferences standardPreferences];
419
420     [preferences setAllowUniversalAccessFromFileURLs:YES];
421     [preferences setAllowFileAccessFromFileURLs:YES];
422     [preferences setStandardFontFamily:@"Times"];
423     [preferences setFixedFontFamily:@"Courier"];
424     [preferences setSerifFontFamily:@"Times"];
425     [preferences setSansSerifFontFamily:@"Helvetica"];
426     [preferences setCursiveFontFamily:@"Apple Chancery"];
427     [preferences setFantasyFontFamily:@"Papyrus"];
428     [preferences setDefaultFontSize:16];
429     [preferences setDefaultFixedFontSize:13];
430     [preferences setMinimumFontSize:0];
431     [preferences setJavaEnabled:NO];
432     [preferences setJavaScriptEnabled:YES];
433     [preferences setEditableLinkBehavior:WebKitEditableLinkOnlyLiveWithShiftKey];
434     [preferences setTabsToLinks:NO];
435     [preferences setDOMPasteAllowed:YES];
436     [preferences setShouldPrintBackgrounds:YES];
437     [preferences setCacheModel:WebCacheModelDocumentBrowser];
438     [preferences setXSSAuditorEnabled:NO];
439     [preferences setExperimentalNotificationsEnabled:NO];
440     [preferences setPluginAllowedRunTime:1];
441     [preferences setPlugInsEnabled:YES];
442
443     [preferences setPrivateBrowsingEnabled:NO];
444     [preferences setAuthorAndUserStylesEnabled:YES];
445     [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
446     [preferences setJavaScriptCanAccessClipboard:YES];
447     [preferences setOfflineWebApplicationCacheEnabled:YES];
448     [preferences setDeveloperExtrasEnabled:NO];
449     [preferences setLoadsImagesAutomatically:YES];
450     [preferences setFrameFlatteningEnabled:NO];
451     [preferences setSpatialNavigationEnabled:NO];
452     [preferences setEditingBehavior:WebKitEditingMacBehavior];
453     if (persistentUserStyleSheetLocation) {
454         [preferences setUserStyleSheetLocation:[NSURL URLWithString:(NSString *)(persistentUserStyleSheetLocation.get())]];
455         [preferences setUserStyleSheetEnabled:YES];
456     } else
457         [preferences setUserStyleSheetEnabled:NO];
458
459     // The back/forward cache is causing problems due to layouts during transition from one page to another.
460     // So, turn it off for now, but we might want to turn it back on some day.
461     [preferences setUsesPageCache:NO];
462     [preferences setAcceleratedCompositingEnabled:YES];
463     [preferences setWebGLEnabled:NO];
464     [preferences setUsePreHTML5ParserQuirks:NO];
465     [preferences setAsynchronousSpellCheckingEnabled:NO];
466
467     [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain];
468     
469     LayoutTestController::setSerializeHTTPLoads(false);
470
471     setlocale(LC_ALL, "");
472 }
473
474 // Called once on DumpRenderTree startup.
475 static void setDefaultsToConsistentValuesForTesting()
476 {
477     resetDefaultsToConsistentValues();
478
479     NSString *path = libraryPathForDumpRenderTree();
480     NSURLCache *sharedCache =
481         [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024
482                                       diskCapacity:0
483                                           diskPath:[path stringByAppendingPathComponent:@"URLCache"]];
484     [NSURLCache setSharedURLCache:sharedCache];
485     [sharedCache release];
486
487 }
488
489 static void* runThread(void* arg)
490 {
491     static ThreadIdentifier previousId = 0;
492     ThreadIdentifier currentId = currentThread();
493     // Verify 2 successive threads do not get the same Id.
494     ASSERT(previousId != currentId);
495     previousId = currentId;
496     return 0;
497 }
498
499 static void testThreadIdentifierMap()
500 {
501     // Imitate 'foreign' threads that are not created by WTF.
502     pthread_t pthread;
503     pthread_create(&pthread, 0, &runThread, 0);
504     pthread_join(pthread, 0);
505
506     pthread_create(&pthread, 0, &runThread, 0);
507     pthread_join(pthread, 0);
508
509     // Now create another thread using WTF. On OSX, it will have the same pthread handle
510     // but should get a different ThreadIdentifier.
511     createThread(runThread, 0, "DumpRenderTree: test");
512 }
513
514 static void crashHandler(int sig)
515 {
516     char *signalName = strsignal(sig);
517     write(STDERR_FILENO, signalName, strlen(signalName));
518     write(STDERR_FILENO, "\n", 1);
519     restoreMainDisplayColorProfile(0);
520     exit(128 + sig);
521 }
522
523 static void installSignalHandlers()
524 {
525     signal(SIGILL, crashHandler);    /* 4:   illegal instruction (not reset when caught) */
526     signal(SIGTRAP, crashHandler);   /* 5:   trace trap (not reset when caught) */
527     signal(SIGEMT, crashHandler);    /* 7:   EMT instruction */
528     signal(SIGFPE, crashHandler);    /* 8:   floating point exception */
529     signal(SIGBUS, crashHandler);    /* 10:  bus error */
530     signal(SIGSEGV, crashHandler);   /* 11:  segmentation violation */
531     signal(SIGSYS, crashHandler);    /* 12:  bad argument to system call */
532     signal(SIGPIPE, crashHandler);   /* 13:  write on a pipe with no reader */
533     signal(SIGXCPU, crashHandler);   /* 24:  exceeded CPU time limit */
534     signal(SIGXFSZ, crashHandler);   /* 25:  exceeded file size limit */
535 }
536
537 static void allocateGlobalControllers()
538 {
539     // FIXME: We should remove these and move to the ObjC standard [Foo sharedInstance] model
540     gNavigationController = [[NavigationController alloc] init];
541     frameLoadDelegate = [[FrameLoadDelegate alloc] init];
542     uiDelegate = [[UIDelegate alloc] init];
543     editingDelegate = [[EditingDelegate alloc] init];
544     resourceLoadDelegate = [[ResourceLoadDelegate alloc] init];
545     policyDelegate = [[PolicyDelegate alloc] init];
546     historyDelegate = [[HistoryDelegate alloc] init];
547 }
548
549 // ObjC++ doens't seem to let me pass NSObject*& sadly.
550 static inline void releaseAndZero(NSObject** object)
551 {
552     [*object release];
553     *object = nil;
554 }
555
556 static void releaseGlobalControllers()
557 {
558     releaseAndZero(&gNavigationController);
559     releaseAndZero(&frameLoadDelegate);
560     releaseAndZero(&editingDelegate);
561     releaseAndZero(&resourceLoadDelegate);
562     releaseAndZero(&uiDelegate);
563     releaseAndZero(&policyDelegate);
564 }
565
566 static void initializeGlobalsFromCommandLineOptions(int argc, const char *argv[])
567 {
568     struct option options[] = {
569         {"notree", no_argument, &dumpTree, NO},
570         {"pixel-tests", no_argument, &dumpPixels, YES},
571         {"tree", no_argument, &dumpTree, YES},
572         {"threaded", no_argument, &threaded, YES},
573         {"complex-text", no_argument, &forceComplexText, YES},
574         {NULL, 0, NULL, 0}
575     };
576     
577     int option;
578     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1) {
579         switch (option) {
580             case '?':   // unknown or ambiguous option
581             case ':':   // missing argument
582                 exit(1);
583                 break;
584         }
585     }
586 }
587
588 static void addTestPluginsToPluginSearchPath(const char* executablePath)
589 {
590     NSString *pwd = [[NSString stringWithUTF8String:executablePath] stringByDeletingLastPathComponent];
591     [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
592     [[WebPluginDatabase sharedDatabase] refresh];
593 }
594
595 static bool useLongRunningServerMode(int argc, const char *argv[])
596 {
597     // This assumes you've already called getopt_long
598     return (argc == optind+1 && strcmp(argv[optind], "-") == 0);
599 }
600
601 static void runTestingServerLoop()
602 {
603     // When DumpRenderTree run in server mode, we just wait around for file names
604     // to be passed to us and read each in turn, passing the results back to the client
605     char filenameBuffer[2048];
606     while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
607         char *newLineCharacter = strchr(filenameBuffer, '\n');
608         if (newLineCharacter)
609             *newLineCharacter = '\0';
610
611         if (strlen(filenameBuffer) == 0)
612             continue;
613
614         runTest(filenameBuffer);
615     }
616 }
617
618 static void prepareConsistentTestingEnvironment()
619 {
620     poseAsClass("DumpRenderTreePasteboard", "NSPasteboard");
621     poseAsClass("DumpRenderTreeEvent", "NSEvent");
622
623     setDefaultsToConsistentValuesForTesting();
624     activateFonts();
625     
626     if (dumpPixels)
627         setupMainDisplayColorProfile();
628     allocateGlobalControllers();
629     
630     makeLargeMallocFailSilently();
631 }
632
633 void dumpRenderTree(int argc, const char *argv[])
634 {
635     initializeGlobalsFromCommandLineOptions(argc, argv);
636     prepareConsistentTestingEnvironment();
637     addTestPluginsToPluginSearchPath(argv[0]);
638     if (dumpPixels)
639         installSignalHandlers();
640
641     if (forceComplexText)
642         [WebView _setAlwaysUsesComplexTextCodePath:YES];
643
644     WebView *webView = createWebViewAndOffscreenWindow();
645     mainFrame = [webView mainFrame];
646
647     [[NSURLCache sharedURLCache] removeAllCachedResponses];
648     [WebCache empty];
649
650     // <http://webkit.org/b/31200> In order to prevent extra frame load delegate logging being generated if the first test to use SSL
651     // is set to log frame load delegate calls we ignore SSL certificate errors on localhost and 127.0.0.1.
652 #if BUILDING_ON_TIGER
653     // Initialize internal NSURLRequest data for setAllowsAnyHTTPSCertificate:forHost: to work properly.
654     [[[[NSURLRequest alloc] init] autorelease] HTTPMethod];
655 #endif
656     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"localhost"];
657     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"127.0.0.1"];
658
659     // <rdar://problem/5222911>
660     testStringByEvaluatingJavaScriptFromString();
661
662     // http://webkit.org/b/32689
663     testThreadIdentifierMap();
664
665     if (threaded)
666         startJavaScriptThreads();
667
668     if (useLongRunningServerMode(argc, argv)) {
669         printSeparators = YES;
670         runTestingServerLoop();
671     } else {
672         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
673         for (int i = optind; i != argc; ++i)
674             runTest(argv[i]);
675     }
676
677     if (threaded)
678         stopJavaScriptThreads();
679
680     NSWindow *window = [webView window];
681     [webView close];
682     mainFrame = nil;
683
684     // Work around problem where registering drag types leaves an outstanding
685     // "perform selector" on the window, which retains the window. It's a bit
686     // inelegant and perhaps dangerous to just blow them all away, but in practice
687     // it probably won't cause any trouble (and this is just a test tool, after all).
688     [NSObject cancelPreviousPerformRequestsWithTarget:window];
689     
690     [window close]; // releases when closed
691     [webView release];
692     
693     releaseGlobalControllers();
694     
695     [DumpRenderTreePasteboard releaseLocalPasteboards];
696
697     // FIXME: This should be moved onto LayoutTestController and made into a HashSet
698     if (disallowedURLs) {
699         CFRelease(disallowedURLs);
700         disallowedURLs = 0;
701     }
702
703     if (dumpPixels)
704         restoreMainDisplayColorProfile(0);
705 }
706
707 int main(int argc, const char *argv[])
708 {
709     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
710     [DumpRenderTreeApplication sharedApplication]; // Force AppKit to init itself
711     dumpRenderTree(argc, argv);
712     [WebCoreStatistics garbageCollectJavaScriptObjects];
713     [WebCoreStatistics emptyCache]; // Otherwise SVGImages trigger false positives for Frame/Node counts    
714     [pool release];
715     return 0;
716 }
717
718 static NSInteger compareHistoryItems(id item1, id item2, void *context)
719 {
720     return [[item1 target] caseInsensitiveCompare:[item2 target]];
721 }
722
723 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
724 {
725     int start = 0;
726     if (current) {
727         printf("curr->");
728         start = 6;
729     }
730     for (int i = start; i < indent; i++)
731         putchar(' ');
732     
733     NSString *urlString = [item URLString];
734     if ([[NSURL URLWithString:urlString] isFileURL]) {
735         NSRange range = [urlString rangeOfString:@"/LayoutTests/"];
736         urlString = [@"(file test):" stringByAppendingString:[urlString substringFromIndex:(range.length + range.location)]];
737     }
738     
739     printf("%s", [urlString UTF8String]);
740     NSString *target = [item target];
741     if (target && [target length] > 0)
742         printf(" (in frame \"%s\")", [target UTF8String]);
743     if ([item isTargetItem])
744         printf("  **nav target**");
745     putchar('\n');
746     NSArray *kids = [item children];
747     if (kids) {
748         // must sort to eliminate arbitrary result ordering which defeats reproducible testing
749         kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
750         for (unsigned i = 0; i < [kids count]; i++)
751             dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
752     }
753 }
754
755 static void dumpFrameScrollPosition(WebFrame *f)
756 {
757     WebScriptObject* scriptObject = [f windowObject];
758     NSPoint scrollPosition = NSMakePoint(
759         [[scriptObject valueForKey:@"pageXOffset"] floatValue],
760         [[scriptObject valueForKey:@"pageYOffset"] floatValue]);
761     if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
762         if ([f parentFrame] != nil)
763             printf("frame '%s' ", [[f name] UTF8String]);
764         printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
765     }
766
767     if (gLayoutTestController->dumpChildFrameScrollPositions()) {
768         NSArray *kids = [f childFrames];
769         if (kids)
770             for (unsigned i = 0; i < [kids count]; i++)
771                 dumpFrameScrollPosition([kids objectAtIndex:i]);
772     }
773 }
774
775 static NSString *dumpFramesAsText(WebFrame *frame)
776 {
777     DOMDocument *document = [frame DOMDocument];
778     DOMElement *documentElement = [document documentElement];
779
780     if (!documentElement)
781         return @"";
782
783     NSMutableString *result = [[[NSMutableString alloc] init] autorelease];
784
785     // Add header for all but the main frame.
786     if ([frame parentFrame])
787         result = [NSMutableString stringWithFormat:@"\n--------\nFrame: '%@'\n--------\n", [frame name]];
788
789     [result appendFormat:@"%@\n", [documentElement innerText]];
790
791     if (gLayoutTestController->dumpChildFramesAsText()) {
792         NSArray *kids = [frame childFrames];
793         if (kids) {
794             for (unsigned i = 0; i < [kids count]; i++)
795                 [result appendString:dumpFramesAsText([kids objectAtIndex:i])];
796         }
797     }
798
799     return result;
800 }
801
802 static NSData *dumpFrameAsPDF(WebFrame *frame)
803 {
804     if (!frame)
805         return nil;
806
807     // Sadly we have to dump to a file and then read from that file again
808     // +[NSPrintOperation PDFOperationWithView:insideRect:] requires a rect and prints to a single page
809     // likewise +[NSView dataWithPDFInsideRect:] also prints to a single continuous page
810     // The goal of this function is to test "real" printing across multiple pages.
811     // FIXME: It's possible there might be printing SPI to let us print a multi-page PDF to an NSData object
812     NSString *path = [libraryPathForDumpRenderTree() stringByAppendingPathComponent:@"test.pdf"];
813
814     NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]];
815     [printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
816     [printInfoDict setObject:path forKey:NSPrintSavePath];
817
818     NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
819     [printInfo setHorizontalPagination:NSAutoPagination];
820     [printInfo setVerticalPagination:NSAutoPagination];
821     [printInfo setVerticallyCentered:NO];
822
823     NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:[frame frameView] printInfo:printInfo];
824     [printOperation setShowPanels:NO];
825     [printOperation runOperation];
826
827     [printInfo release];
828
829     NSData *pdfData = [NSData dataWithContentsOfFile:path];
830     [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
831
832     return pdfData;
833 }
834
835 static void dumpBackForwardListForWebView(WebView *view)
836 {
837     printf("\n============== Back Forward List ==============\n");
838     WebBackForwardList *bfList = [view backForwardList];
839
840     // Print out all items in the list after prevTestBFItem, which was from the previous test
841     // Gather items from the end of the list, the print them out from oldest to newest
842     NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
843     for (int i = [bfList forwardListCount]; i > 0; i--) {
844         WebHistoryItem *item = [bfList itemAtIndex:i];
845         // something is wrong if the item from the last test is in the forward part of the b/f list
846         assert(item != prevTestBFItem);
847         [itemsToPrint addObject:item];
848     }
849             
850     assert([bfList currentItem] != prevTestBFItem);
851     [itemsToPrint addObject:[bfList currentItem]];
852     int currentItemIndex = [itemsToPrint count] - 1;
853
854     for (int i = -1; i >= -[bfList backListCount]; i--) {
855         WebHistoryItem *item = [bfList itemAtIndex:i];
856         if (item == prevTestBFItem)
857             break;
858         [itemsToPrint addObject:item];
859     }
860
861     for (int i = [itemsToPrint count]-1; i >= 0; i--)
862         dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
863
864     [itemsToPrint release];
865     printf("===============================================\n");
866 }
867
868 static void sizeWebViewForCurrentTest()
869 {
870     // W3C SVG tests expect to be 480x360
871     bool isSVGW3CTest = (gLayoutTestController->testPathOrURL().find("svg/W3C-SVG-1.1") != string::npos);
872     if (isSVGW3CTest)
873         [[mainFrame webView] setFrameSize:NSMakeSize(480, 360)];
874     else
875         [[mainFrame webView] setFrameSize:NSMakeSize(LayoutTestController::maxViewWidth, LayoutTestController::maxViewHeight)];
876 }
877
878 static const char *methodNameStringForFailedTest()
879 {
880     const char *errorMessage;
881     if (gLayoutTestController->dumpAsText())
882         errorMessage = "[documentElement innerText]";
883     else if (gLayoutTestController->dumpDOMAsWebArchive())
884         errorMessage = "[[mainFrame DOMDocument] webArchive]";
885     else if (gLayoutTestController->dumpSourceAsWebArchive())
886         errorMessage = "[[mainFrame dataSource] webArchive]";
887     else
888         errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
889
890     return errorMessage;
891 }
892
893 static void dumpBackForwardListForAllWindows()
894 {
895     CFArrayRef openWindows = (CFArrayRef)[DumpRenderTreeWindow openWindows];
896     unsigned count = CFArrayGetCount(openWindows);
897     for (unsigned i = 0; i < count; i++) {
898         NSWindow *window = (NSWindow *)CFArrayGetValueAtIndex(openWindows, i);
899         WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
900         dumpBackForwardListForWebView(webView);
901     }
902 }
903
904 static void invalidateAnyPreviousWaitToDumpWatchdog()
905 {
906     if (waitToDumpWatchdog) {
907         CFRunLoopTimerInvalidate(waitToDumpWatchdog);
908         CFRelease(waitToDumpWatchdog);
909         waitToDumpWatchdog = 0;
910     }
911 }
912
913 void dump()
914 {
915     invalidateAnyPreviousWaitToDumpWatchdog();
916
917     if (dumpTree) {
918         NSString *resultString = nil;
919         NSData *resultData = nil;
920         NSString *resultMimeType = @"text/plain";
921
922         if ([[[mainFrame dataSource] _responseMIMEType] isEqualToString:@"text/plain"]) {
923             gLayoutTestController->setDumpAsText(true);
924             gLayoutTestController->setGeneratePixelResults(false);
925         }
926         if (gLayoutTestController->dumpAsText()) {
927             resultString = dumpFramesAsText(mainFrame);
928         } else if (gLayoutTestController->dumpAsPDF()) {
929             resultData = dumpFrameAsPDF(mainFrame);
930             resultMimeType = @"application/pdf";
931         } else if (gLayoutTestController->dumpDOMAsWebArchive()) {
932             WebArchive *webArchive = [[mainFrame DOMDocument] webArchive];
933             resultString = HardAutorelease(createXMLStringFromWebArchiveData((CFDataRef)[webArchive data]));
934             resultMimeType = @"application/x-webarchive";
935         } else if (gLayoutTestController->dumpSourceAsWebArchive()) {
936             WebArchive *webArchive = [[mainFrame dataSource] webArchive];
937             resultString = HardAutorelease(createXMLStringFromWebArchiveData((CFDataRef)[webArchive data]));
938             resultMimeType = @"application/x-webarchive";
939         } else {
940             sizeWebViewForCurrentTest();
941             resultString = [mainFrame renderTreeAsExternalRepresentationForPrinting:gLayoutTestController->isPrinting()];
942         }
943
944         if (resultString && !resultData)
945             resultData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
946
947         printf("Content-Type: %s\n", [resultMimeType UTF8String]);
948
949         if (resultData) {
950             fwrite([resultData bytes], 1, [resultData length], stdout);
951
952             if (!gLayoutTestController->dumpAsText() && !gLayoutTestController->dumpDOMAsWebArchive() && !gLayoutTestController->dumpSourceAsWebArchive())
953                 dumpFrameScrollPosition(mainFrame);
954
955             if (gLayoutTestController->dumpBackForwardList())
956                 dumpBackForwardListForAllWindows();
957         } else
958             printf("ERROR: nil result from %s", methodNameStringForFailedTest());
959
960         // Stop the watchdog thread before we leave this test to make sure it doesn't
961         // fire in between tests causing the next test to fail.
962         // This is a speculative fix for: https://bugs.webkit.org/show_bug.cgi?id=32339
963         invalidateAnyPreviousWaitToDumpWatchdog();
964
965         if (printSeparators) {
966             puts("#EOF");       // terminate the content block
967             fputs("#EOF\n", stderr);
968         }            
969     }
970
971     if (dumpPixels && gLayoutTestController->generatePixelResults())
972         // FIXME: when isPrinting is set, dump the image with page separators.
973         dumpWebViewAsPixelsAndCompareWithExpected(gLayoutTestController->expectedPixelHash());
974
975     puts("#EOF");   // terminate the (possibly empty) pixels block
976
977     fflush(stdout);
978     fflush(stderr);
979
980     done = YES;
981 }
982
983 static bool shouldLogFrameLoadDelegates(const char* pathOrURL)
984 {
985     return strstr(pathOrURL, "loading/");
986 }
987
988 static bool shouldLogHistoryDelegates(const char* pathOrURL)
989 {
990     return strstr(pathOrURL, "globalhistory/");
991 }
992
993 static bool shouldOpenWebInspector(const char* pathOrURL)
994 {
995     return strstr(pathOrURL, "inspector/");
996 }
997
998 static bool shouldEnableDeveloperExtras(const char* pathOrURL)
999 {
1000     return true;
1001 }
1002
1003 static void resetWebViewToConsistentStateBeforeTesting()
1004 {
1005     WebView *webView = [mainFrame webView];
1006     [webView setEditable:NO];
1007     [(EditingDelegate *)[webView editingDelegate] setAcceptsEditing:YES];
1008     [webView makeTextStandardSize:nil];
1009     [webView resetPageZoom:nil];
1010     [webView setTabKeyCyclesThroughElements:YES];
1011     [webView setPolicyDelegate:nil];
1012     [policyDelegate setPermissive:NO];
1013     [policyDelegate setControllerToNotifyDone:0];
1014     [frameLoadDelegate resetToConsistentState];
1015     [webView _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:NO];
1016     [webView _clearMainFrameName];
1017     [[webView undoManager] removeAllActions];
1018     [WebView _removeAllUserContentFromGroup:[webView groupName]];
1019     [[webView window] setAutodisplay:NO];
1020
1021     resetDefaultsToConsistentValues();
1022
1023     [[mainFrame webView] setSmartInsertDeleteEnabled:YES];
1024     [[[mainFrame webView] inspector] setJavaScriptProfilingEnabled:NO];
1025
1026     [WebView _setUsesTestModeFocusRingColor:YES];
1027     [WebView _resetOriginAccessWhitelists];
1028
1029     [[MockGeolocationProvider shared] stopTimer];
1030     
1031     // Clear the contents of the general pasteboard
1032     [[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
1033 }
1034
1035 static void runTest(const string& testPathOrURL)
1036 {
1037     ASSERT(!testPathOrURL.empty());
1038     
1039     // Look for "'" as a separator between the path or URL, and the pixel dump hash that follows.
1040     string pathOrURL(testPathOrURL);
1041     string expectedPixelHash;
1042     
1043     size_t separatorPos = pathOrURL.find("'");
1044     if (separatorPos != string::npos) {
1045         pathOrURL = string(testPathOrURL, 0, separatorPos);
1046         expectedPixelHash = string(testPathOrURL, separatorPos + 1);
1047     }
1048
1049     NSString *pathOrURLString = [NSString stringWithUTF8String:pathOrURL.c_str()];
1050     if (!pathOrURLString) {
1051         fprintf(stderr, "Failed to parse \"%s\" as UTF-8\n", pathOrURL.c_str());
1052         return;
1053     }
1054
1055     NSURL *url;
1056     if ([pathOrURLString hasPrefix:@"http://"] || [pathOrURLString hasPrefix:@"https://"])
1057         url = [NSURL URLWithString:pathOrURLString];
1058     else
1059         url = [NSURL fileURLWithPath:pathOrURLString];
1060     if (!url) {
1061         fprintf(stderr, "Failed to parse \"%s\" as a URL\n", pathOrURL.c_str());
1062         return;
1063     }
1064
1065     const string testURL([[url absoluteString] UTF8String]);
1066     
1067     resetWebViewToConsistentStateBeforeTesting();
1068
1069     gLayoutTestController = LayoutTestController::create(testURL, expectedPixelHash);
1070     topLoadingFrame = nil;
1071     ASSERT(!draggingInfo); // the previous test should have called eventSender.mouseUp to drop!
1072     releaseAndZero(&draggingInfo);
1073     done = NO;
1074
1075     gLayoutTestController->setIconDatabaseEnabled(false);
1076
1077     if (disallowedURLs)
1078         CFSetRemoveAllValues(disallowedURLs);
1079     if (shouldLogFrameLoadDelegates(pathOrURL.c_str()))
1080         gLayoutTestController->setDumpFrameLoadCallbacks(true);
1081
1082     if (shouldLogHistoryDelegates(pathOrURL.c_str()))
1083         [[mainFrame webView] setHistoryDelegate:historyDelegate];
1084     else
1085         [[mainFrame webView] setHistoryDelegate:nil];
1086
1087     if (shouldEnableDeveloperExtras(pathOrURL.c_str())) {
1088         gLayoutTestController->setDeveloperExtrasEnabled(true);
1089         if (shouldOpenWebInspector(pathOrURL.c_str()))
1090             gLayoutTestController->showWebInspector();
1091     }
1092
1093     if ([WebHistory optionalSharedHistory])
1094         [WebHistory setOptionalSharedHistory:nil];
1095     lastMousePosition = NSZeroPoint;
1096     lastClickPosition = NSZeroPoint;
1097
1098     [prevTestBFItem release];
1099     prevTestBFItem = [[[[mainFrame webView] backForwardList] currentItem] retain];
1100
1101     WorkQueue::shared()->clear();
1102     WorkQueue::shared()->setFrozen(false);
1103
1104     bool ignoreWebCoreNodeLeaks = shouldIgnoreWebCoreNodeLeaks(testURL);
1105     if (ignoreWebCoreNodeLeaks)
1106         [WebCoreStatistics startIgnoringWebCoreNodeLeaks];
1107
1108     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1109     [mainFrame loadRequest:[NSURLRequest requestWithURL:url]];
1110     [pool release];
1111
1112     while (!done) {
1113         pool = [[NSAutoreleasePool alloc] init];
1114         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]]; 
1115         [pool release];
1116     }
1117
1118     pool = [[NSAutoreleasePool alloc] init];
1119     [EventSendingController clearSavedEvents];
1120     [[mainFrame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1121
1122     WorkQueue::shared()->clear();
1123
1124     if (gLayoutTestController->closeRemainingWindowsWhenComplete()) {
1125         NSArray* array = [DumpRenderTreeWindow openWindows];
1126
1127         unsigned count = [array count];
1128         for (unsigned i = 0; i < count; i++) {
1129             NSWindow *window = [array objectAtIndex:i];
1130
1131             // Don't try to close the main window
1132             if (window == [[mainFrame webView] window])
1133                 continue;
1134             
1135             WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
1136
1137             [webView close];
1138             [window close];
1139         }
1140     }
1141
1142     // If developer extras enabled Web Inspector may have been open by the test.
1143     if (shouldEnableDeveloperExtras(pathOrURL.c_str())) {
1144         gLayoutTestController->closeWebInspector();
1145         gLayoutTestController->setDeveloperExtrasEnabled(false);
1146     }
1147
1148     resetWebViewToConsistentStateBeforeTesting();
1149
1150     [mainFrame loadHTMLString:@"<html></html>" baseURL:[NSURL URLWithString:@"about:blank"]];
1151     [mainFrame stopLoading];
1152
1153     [pool release];
1154
1155     // We should only have our main window left open when we're done
1156     ASSERT(CFArrayGetCount(openWindowsRef) == 1);
1157     ASSERT(CFArrayGetValueAtIndex(openWindowsRef, 0) == [[mainFrame webView] window]);
1158
1159     gLayoutTestController.clear();
1160
1161     if (ignoreWebCoreNodeLeaks)
1162         [WebCoreStatistics stopIgnoringWebCoreNodeLeaks];
1163 }
1164
1165 void displayWebView()
1166 {
1167     NSView *webView = [mainFrame webView];
1168     [webView display];
1169     [webView lockFocus];
1170     [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
1171     NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
1172     [webView unlockFocus];
1173 }
1174
1175 @implementation DumpRenderTreeEvent
1176
1177 + (NSPoint)mouseLocation
1178 {
1179     return [[[mainFrame webView] window] convertBaseToScreen:lastMousePosition];
1180 }
1181
1182 @end
1183
1184 @implementation DumpRenderTreeApplication
1185
1186 - (BOOL)isRunning
1187 {
1188     // <rdar://problem/7686123> Java plug-in freezes unless NSApplication is running
1189     return YES;
1190 }
1191
1192 @end