[iOS] Upstream changes in Tools/DumpRenderTree
[WebKit-https.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 "DefaultPolicyDelegate.h"
36 #import "DumpRenderTreeDraggingInfo.h"
37 #import "DumpRenderTreePasteboard.h"
38 #import "DumpRenderTreeWindow.h"
39 #import "EditingDelegate.h"
40 #import "EventSendingController.h"
41 #import "FrameLoadDelegate.h"
42 #import "HistoryDelegate.h"
43 #import "JavaScriptThreading.h"
44 #import "TestRunner.h"
45 #import "MockGeolocationProvider.h"
46 #import "MockWebNotificationProvider.h"
47 #import "NavigationController.h"
48 #import "ObjCPlugin.h"
49 #import "ObjCPluginFunction.h"
50 #import "PixelDumpSupport.h"
51 #import "PolicyDelegate.h"
52 #import "ResourceLoadDelegate.h"
53 #import "StorageTrackerDelegate.h"
54 #import "UIDelegate.h"
55 #import "WebArchiveDumpSupport.h"
56 #import "WebCoreTestSupport.h"
57 #import "WorkQueue.h"
58 #import "WorkQueueItem.h"
59 #import <CoreFoundation/CoreFoundation.h>
60 #import <JavaScriptCore/HeapStatistics.h>
61 #import <JavaScriptCore/Options.h>
62 #import <WebKit/DOMElement.h>
63 #import <WebKit/DOMExtensions.h>
64 #import <WebKit/DOMRange.h>
65 #import <WebKit/WebArchive.h>
66 #import <WebKit/WebBackForwardList.h>
67 #import <WebKit/WebCache.h>
68 #import <WebKit/WebCoreStatistics.h>
69 #import <WebKit/WebDataSourcePrivate.h>
70 #import <WebKit/WebDatabaseManagerPrivate.h>
71 #import <WebKit/WebDocumentPrivate.h>
72 #import <WebKit/WebDeviceOrientationProviderMock.h>
73 #import <WebKit/WebEditingDelegate.h>
74 #import <WebKit/WebFrameView.h>
75 #import <WebKit/WebHistory.h>
76 #import <WebKit/WebHistoryItemPrivate.h>
77 #import <WebKit/WebInspector.h>
78 #import <WebKit/WebKitNSStringExtras.h>
79 #import <WebKit/WebPluginDatabase.h>
80 #import <WebKit/WebPreferences.h>
81 #import <WebKit/WebPreferencesPrivate.h>
82 #import <WebKit/WebPreferenceKeysPrivate.h>
83 #import <WebKit/WebResourceLoadDelegate.h>
84 #import <WebKit/WebStorageManagerPrivate.h>
85 #import <WebKit/WebViewPrivate.h>
86 #import <getopt.h>
87 #import <wtf/Assertions.h>
88 #import <wtf/FastMalloc.h>
89 #import <wtf/RetainPtr.h>
90 #import <wtf/Threading.h>
91 #import <wtf/ObjcRuntimeExtras.h>
92 #import <wtf/OwnPtr.h>
93
94 #if !PLATFORM(IOS)
95 #import <Carbon/Carbon.h>
96 #import <WebKit/WebDynamicScrollBarsView.h>
97 #endif
98
99 #if PLATFORM(IOS)
100 #import <CoreGraphics/CGFontDB.h>
101 #import <GraphicsServices/GSFont.h>
102 #import <QuartzCore/QuartzCore.h>
103 #import <UIKit/UIApplication_Private.h>
104 #import <UIKit/UIMath.h>
105 #import <UIKit/UIWebBrowserView.h>
106 #import <UIKit/UIWebScrollView.h>
107 #import <WebKit/WAKViewPrivate.h>
108 #import <WebKit/WAKWindow.h>
109 #import <WebKit/WebCoreThread.h>
110 #import <WebKit/WebCoreThreadRun.h>
111 #import <WebKit/WebDOMOperations.h>
112 #import <fcntl.h>
113 #import "DumpRenderTreeBrowserView.h"
114 #endif
115
116 extern "C" {
117 #import <mach-o/getsect.h>
118 }
119
120 using namespace std;
121
122 #if !PLATFORM(IOS)
123 @interface DumpRenderTreeApplication : NSApplication
124 @end
125
126 @interface DumpRenderTreeEvent : NSEvent
127 @end
128 #else
129 @interface ScrollViewResizerDelegate : NSObject
130 @end
131
132 @implementation ScrollViewResizerDelegate
133 - (void)view:(UIWebDocumentView *)view didSetFrame:(CGRect)newFrame oldFrame:(CGRect)oldFrame asResultOfZoom:(BOOL)wasResultOfZoom
134 {
135     UIView *scrollView = [view superview];
136     while (![scrollView isKindOfClass:[UIWebScrollView class]])
137         scrollView = [scrollView superview];
138
139     ASSERT(scrollView && [scrollView isKindOfClass:[UIWebScrollView class]]);
140     const CGSize scrollViewSize = [scrollView bounds].size;
141     CGSize contentSize = newFrame.size;
142     contentSize.height = _ROUNDF_(MAX(CGRectGetMaxY(newFrame), scrollViewSize.height));
143     [(UIWebScrollView *)scrollView setContentSize:contentSize];
144 }
145 @end
146 #endif
147
148 @interface NSURLRequest (PrivateThingsWeShouldntReallyUse)
149 +(void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString *)host;
150 @end
151
152 #if USE(APPKIT)
153 @interface NSSound (Details)
154 + (void)_setAlertType:(NSUInteger)alertType;
155 @end
156 #endif
157
158 static void runTest(const string& testPathOrURL);
159
160 // Deciding when it's OK to dump out the state is a bit tricky.  All these must be true:
161 // - There is no load in progress
162 // - There is no work queued up (see workQueue var, below)
163 // - waitToDump==NO.  This means either waitUntilDone was never called, or it was called
164 //       and notifyDone was called subsequently.
165 // Note that the call to notifyDone and the end of the load can happen in either order.
166
167 volatile bool done;
168
169 NavigationController* gNavigationController = 0;
170 RefPtr<TestRunner> gTestRunner;
171
172 WebFrame *mainFrame = 0;
173 // This is the topmost frame that is loading, during a given load, or nil when no load is 
174 // in progress.  Usually this is the same as the main frame, but not always.  In the case
175 // where a frameset is loaded, and then new content is loaded into one of the child frames,
176 // that child frame is the "topmost frame that is loading".
177 WebFrame *topLoadingFrame = nil;     // !nil iff a load is in progress
178
179
180 CFMutableSetRef disallowedURLs = 0;
181 static CFRunLoopTimerRef waitToDumpWatchdog = 0;
182
183 // Delegates
184 static FrameLoadDelegate *frameLoadDelegate;
185 static UIDelegate *uiDelegate;
186 static EditingDelegate *editingDelegate;
187 static ResourceLoadDelegate *resourceLoadDelegate;
188 static HistoryDelegate *historyDelegate;
189 PolicyDelegate *policyDelegate;
190 DefaultPolicyDelegate *defaultPolicyDelegate;
191 StorageTrackerDelegate *storageDelegate;
192 #if PLATFORM(IOS)
193 static ScrollViewResizerDelegate *scrollViewResizerDelegate;
194 #endif
195
196 static int dumpPixelsForAllTests = NO;
197 static bool dumpPixelsForCurrentTest = false;
198 static int threaded;
199 static int dumpTree = YES;
200 static int useTimeoutWatchdog = YES;
201 static int forceComplexText;
202 static int useAcceleratedDrawing;
203 static int gcBetweenTests;
204 static BOOL printSeparators;
205 static RetainPtr<CFStringRef> persistentUserStyleSheetLocation;
206
207 static WebHistoryItem *prevTestBFItem = nil;  // current b/f item at the end of the previous test
208
209 #if PLATFORM(IOS)
210 const unsigned phoneViewHeight = 480;
211 const unsigned phoneViewWidth = 320;
212 const unsigned phoneBrowserScrollViewHeight = 416;
213 const unsigned phoneBrowserAddressBarOffset = 60;
214 const CGRect layoutTestViewportRect = { {0, 0}, {static_cast<CGFloat>(TestRunner::viewWidth), static_cast<CGFloat>(TestRunner::viewHeight)} };
215 UIWebBrowserView *gWebBrowserView = nil;
216 UIWebScrollView *gWebScrollView = nil;
217 DumpRenderTreeWindow *gDrtWindow = nil;
218 #endif
219
220 #ifdef __OBJC2__
221 static void swizzleAllMethods(Class imposter, Class original)
222 {
223     unsigned int imposterMethodCount;
224     Method* imposterMethods = class_copyMethodList(imposter, &imposterMethodCount);
225
226     unsigned int originalMethodCount;
227     Method* originalMethods = class_copyMethodList(original, &originalMethodCount);
228
229     for (unsigned int i = 0; i < imposterMethodCount; i++) {
230         SEL imposterMethodName = method_getName(imposterMethods[i]);
231
232         // Attempt to add the method to the original class.  If it fails, the method already exists and we should
233         // instead exchange the implementations.
234         if (class_addMethod(original, imposterMethodName, method_getImplementation(imposterMethods[i]), method_getTypeEncoding(imposterMethods[i])))
235             continue;
236
237         unsigned int j = 0;
238         for (; j < originalMethodCount; j++) {
239             SEL originalMethodName = method_getName(originalMethods[j]);
240             if (sel_isEqual(imposterMethodName, originalMethodName))
241                 break;
242         }
243
244         // If class_addMethod failed above then the method must exist on the original class.
245         ASSERT(j < originalMethodCount);
246         method_exchangeImplementations(imposterMethods[i], originalMethods[j]);
247     }
248
249     free(imposterMethods);
250     free(originalMethods);
251 }
252 #endif
253
254 static void poseAsClass(const char* imposter, const char* original)
255 {
256     Class imposterClass = objc_getClass(imposter);
257     Class originalClass = objc_getClass(original);
258
259 #ifndef __OBJC2__
260     class_poseAs(imposterClass, originalClass);
261 #else
262
263     // Swizzle instance methods
264     swizzleAllMethods(imposterClass, originalClass);
265     // and then class methods
266     swizzleAllMethods(object_getClass(imposterClass), object_getClass(originalClass));
267 #endif
268 }
269
270 void setPersistentUserStyleSheetLocation(CFStringRef url)
271 {
272     persistentUserStyleSheetLocation = url;
273 }
274
275 static bool shouldIgnoreWebCoreNodeLeaks(const string& URLString)
276 {
277     static char* const ignoreSet[] = {
278         // Keeping this infrastructure around in case we ever need it again.
279     };
280     static const int ignoreSetCount = sizeof(ignoreSet) / sizeof(char*);
281     
282     for (int i = 0; i < ignoreSetCount; i++) {
283         // FIXME: ignore case
284         string curIgnore(ignoreSet[i]);
285         // Match at the end of the URLString
286         if (!URLString.compare(URLString.length() - curIgnore.length(), curIgnore.length(), curIgnore))
287             return true;
288     }
289     return false;
290 }
291
292 #if !PLATFORM(IOS)
293 static NSSet *allowedFontFamilySet()
294 {
295     static NSSet *fontFamilySet = [[NSSet setWithObjects:
296         @"Ahem",
297         @"Al Bayan",
298         @"American Typewriter",
299         @"Andale Mono",
300         @"Apple Braille",
301         @"Apple Color Emoji",
302         @"Apple Chancery",
303         @"Apple Garamond BT",
304         @"Apple LiGothic",
305         @"Apple LiSung",
306         @"Apple Symbols",
307         @"AppleGothic",
308         @"AppleMyungjo",
309         @"Arial Black",
310         @"Arial Hebrew",
311         @"Arial Narrow",
312         @"Arial Rounded MT Bold",
313         @"Arial Unicode MS",
314         @"Arial",
315         @"Ayuthaya",
316         @"Baghdad",
317         @"Baskerville",
318         @"BiauKai",
319         @"Big Caslon",
320         @"Brush Script MT",
321         @"Chalkboard",
322         @"Chalkduster",
323         @"Charcoal CY",
324         @"Cochin",
325         @"Comic Sans MS",
326         @"Copperplate",
327         @"Corsiva Hebrew",
328         @"Courier New",
329         @"Courier",
330         @"DecoType Naskh",
331         @"Devanagari MT",
332         @"Didot",
333         @"Euphemia UCAS",
334         @"Futura",
335         @"GB18030 Bitmap",
336         @"Geeza Pro",
337         @"Geneva CY",
338         @"Geneva",
339         @"Georgia",
340         @"Gill Sans",
341         @"Gujarati MT",
342         @"GungSeo",
343         @"Gurmukhi MT",
344         @"HeadLineA",
345         @"Hei",
346         @"Heiti SC",
347         @"Heiti TC",
348         @"Helvetica CY",
349         @"Helvetica Neue",
350         @"Helvetica",
351         @"Herculanum",
352         @"Hiragino Kaku Gothic Pro",
353         @"Hiragino Kaku Gothic ProN",
354         @"Hiragino Kaku Gothic Std",
355         @"Hiragino Kaku Gothic StdN",
356         @"Hiragino Maru Gothic Monospaced",
357         @"Hiragino Maru Gothic Pro",
358         @"Hiragino Maru Gothic ProN",
359         @"Hiragino Mincho Pro",
360         @"Hiragino Mincho ProN",
361         @"Hiragino Sans GB",
362         @"Hoefler Text",
363         @"Impact",
364         @"InaiMathi",
365         @"Kai",
366         @"Kailasa",
367         @"Kokonor",
368         @"Krungthep",
369         @"KufiStandardGK",
370         @"LiHei Pro",
371         @"LiSong Pro",
372         @"Lucida Grande",
373         @"Marker Felt",
374         @"Menlo",
375         @"Microsoft Sans Serif",
376         @"Monaco",
377         @"Mshtakan",
378         @"Nadeem",
379         @"New Peninim MT",
380         @"Optima",
381         @"Osaka",
382         @"Papyrus",
383         @"PCMyungjo",
384         @"PilGi",
385         @"Plantagenet Cherokee",
386         @"Raanana",
387         @"Sathu",
388         @"Silom",
389         @"Skia",
390         @"Songti SC",
391         @"Songti TC",
392         @"STFangsong",
393         @"STHeiti",
394         @"STIXGeneral",
395         @"STIXSizeOneSym",
396         @"STKaiti",
397         @"STSong",
398         @"Symbol",
399         @"Tahoma",
400         @"Thonburi",
401         @"Times New Roman",
402         @"Times",
403         @"Trebuchet MS",
404         @"Verdana",
405         @"Webdings",
406         @"WebKit WeightWatcher",
407         @"Wingdings 2",
408         @"Wingdings 3",
409         @"Wingdings",
410         @"Zapf Dingbats",
411         @"Zapfino",
412         nil] retain];
413     
414     return fontFamilySet;
415 }
416
417 static NSSet *systemHiddenFontFamilySet()
418 {
419     static NSSet *fontFamilySet = [[NSSet setWithObjects:
420         @".LucidaGrandeUI",
421         nil] retain];
422
423     return fontFamilySet;
424 }
425
426 static IMP appKitAvailableFontFamiliesIMP;
427 static IMP appKitAvailableFontsIMP;
428
429 static NSArray *drt_NSFontManager_availableFontFamilies(id self, SEL _cmd)
430 {
431     static NSArray *availableFontFamilies;
432     if (availableFontFamilies)
433         return availableFontFamilies;
434     
435     NSArray *availableFamilies = wtfCallIMP<id>(appKitAvailableFontFamiliesIMP, self, _cmd);
436
437     NSMutableSet *prunedFamiliesSet = [NSMutableSet setWithArray:availableFamilies];
438     [prunedFamiliesSet intersectSet:allowedFontFamilySet()];
439
440     availableFontFamilies = [[prunedFamiliesSet allObjects] retain];
441     return availableFontFamilies;
442 }
443
444 static NSArray *drt_NSFontManager_availableFonts(id self, SEL _cmd)
445 {
446     static NSArray *availableFonts;
447     if (availableFonts)
448         return availableFonts;
449     
450     NSSet *allowedFamilies = allowedFontFamilySet();
451     NSMutableArray *availableFontList = [[NSMutableArray alloc] initWithCapacity:[allowedFamilies count] * 2];
452     for (NSString *fontFamily in allowedFontFamilySet()) {
453         NSArray* fontsForFamily = [[NSFontManager sharedFontManager] availableMembersOfFontFamily:fontFamily];
454         for (NSArray* fontInfo in fontsForFamily) {
455             // Font name is the first entry in the array.
456             [availableFontList addObject:[fontInfo objectAtIndex:0]];
457         }
458     }
459
460     for (NSString *hiddenFontFamily in systemHiddenFontFamilySet()) {
461         [availableFontList addObject:hiddenFontFamily];
462     }
463
464     availableFonts = availableFontList;
465     return availableFonts;
466 }
467
468 static void swizzleNSFontManagerMethods()
469 {
470     Method availableFontFamiliesMethod = class_getInstanceMethod(objc_getClass("NSFontManager"), @selector(availableFontFamilies));
471     ASSERT(availableFontFamiliesMethod);
472     if (!availableFontFamiliesMethod) {
473         NSLog(@"Failed to swizzle the \"availableFontFamilies\" method on NSFontManager");
474         return;
475     }
476     
477     appKitAvailableFontFamiliesIMP = method_setImplementation(availableFontFamiliesMethod, (IMP)drt_NSFontManager_availableFontFamilies);
478
479     Method availableFontsMethod = class_getInstanceMethod(objc_getClass("NSFontManager"), @selector(availableFonts));
480     ASSERT(availableFontsMethod);
481     if (!availableFontsMethod) {
482         NSLog(@"Failed to swizzle the \"availableFonts\" method on NSFontManager");
483         return;
484     }
485     
486     appKitAvailableFontsIMP = method_setImplementation(availableFontsMethod, (IMP)drt_NSFontManager_availableFonts);
487 }
488
489 static void activateTestingFonts()
490 {
491     static const char* fontFileNames[] = {
492         "AHEM____.TTF",
493         "WebKitWeightWatcher100.ttf",
494         "WebKitWeightWatcher200.ttf",
495         "WebKitWeightWatcher300.ttf",
496         "WebKitWeightWatcher400.ttf",
497         "WebKitWeightWatcher500.ttf",
498         "WebKitWeightWatcher600.ttf",
499         "WebKitWeightWatcher700.ttf",
500         "WebKitWeightWatcher800.ttf",
501         "WebKitWeightWatcher900.ttf",
502 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
503         "SampleFont.sfont",
504 #endif
505         0
506     };
507
508     NSMutableArray *fontURLs = [NSMutableArray array];
509     NSURL *resourcesDirectory = [NSURL URLWithString:@"DumpRenderTree.resources" relativeToURL:[[NSBundle mainBundle] executableURL]];
510     for (unsigned i = 0; fontFileNames[i]; ++i) {
511         NSURL *fontURL = [resourcesDirectory URLByAppendingPathComponent:[NSString stringWithUTF8String:fontFileNames[i]]];
512         [fontURLs addObject:[fontURL absoluteURL]];
513     }
514
515     CFArrayRef errors = 0;
516     if (!CTFontManagerRegisterFontsForURLs((CFArrayRef)fontURLs, kCTFontManagerScopeProcess, &errors)) {
517         NSLog(@"Failed to activate fonts: %@", errors);
518         CFRelease(errors);
519         exit(1);
520     }
521 }
522
523 static void adjustFonts()
524 {
525     swizzleNSFontManagerMethods();
526     activateTestingFonts();
527 }
528 #else
529 static void activateFontsIOS()
530 {
531     static const char* fontSectionNames[] = {
532         "Ahem",
533         "ltcher100",
534         "WeightWatcher200",
535         "WeightWatcher300",
536         "WeightWatcher400",
537         "WeightWatcher500",
538         "WeightWatcher600",
539         "WeightWatcher700",
540         "WeightWatcher800",
541         "WeightWatcher900",
542         0
543     };
544
545     for (unsigned i = 0; fontSectionNames[i]; ++i) {
546         unsigned long fontDataLength;
547         char* fontData = getsectdata("__DATA", fontSectionNames[i], &fontDataLength);
548         if (!fontData) {
549             fprintf(stderr, "Failed to locate the %s font.\n", fontSectionNames[i]);
550             exit(1);
551         }
552
553         CGDataProviderRef data = CGDataProviderCreateWithData(NULL, fontData, fontDataLength, NULL);
554         if (!data) {
555             fprintf(stderr, "Failed to create CGDataProviderRef for the %s font.\n", fontSectionNames[i]);
556             exit(1);
557         }
558
559         CGFontRef cgFont = CGFontCreateWithDataProvider(data);
560         CGDataProviderRelease(data);
561         if (!cgFont) {
562             fprintf(stderr, "Failed to create CGFontRef for the %s font.\n", fontSectionNames[i]);
563             exit(1);
564         }
565
566         if (!GSFontAddCGFont(cgFont)) {
567             fprintf(stderr, "Failed to add CGFont to GraphicsServices for the %s font.\n", fontSectionNames[i]);
568             exit(1);
569         }
570         CGFontRelease(cgFont);
571     }
572 }
573 #endif // !PLATFORM(IOS)
574
575
576 #if PLATFORM(IOS)
577 void adjustWebDocumentForFlexibleViewport(UIWebBrowserView *webBrowserView, UIWebScrollView *scrollView)
578 {
579     // These values match MobileSafari's, see -[TabDocument _createDocumentView].
580     [webBrowserView setMinimumScale:0.25f forDocumentTypes:UIEveryDocumentMask];
581     [webBrowserView setMaximumScale:5.0f forDocumentTypes:UIEveryDocumentMask];
582     [webBrowserView setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:UIEveryDocumentMask];
583     [webBrowserView setViewportSize:CGSizeMake(UIWebViewStandardViewportWidth, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:UIEveryDocumentMask];
584
585     // Adjust the viewport view and viewport to have similar behavior
586     // as the browser.
587     [(DumpRenderTreeBrowserView *)webBrowserView setScrollingUsesUIWebScrollView:YES];
588     [webBrowserView setDelegate:scrollViewResizerDelegate];
589
590     CGRect viewportRect = CGRectMake(0, 0, phoneViewWidth, phoneBrowserScrollViewHeight);
591     [scrollView setBounds:viewportRect];
592     [scrollView setFrame:viewportRect];
593
594     [webBrowserView setMinimumSize:viewportRect.size];
595     [webBrowserView setAutoresizes:YES];
596     CGRect browserViewFrame = [webBrowserView frame];
597     browserViewFrame.origin = CGPointMake(0, phoneBrowserAddressBarOffset);
598     [webBrowserView setFrame:browserViewFrame];
599 }
600
601 void adjustWebDocumentForStandardViewport(UIWebBrowserView *webBrowserView, UIWebScrollView *scrollView)
602 {
603     [webBrowserView setMinimumScale:1.0f forDocumentTypes:UIEveryDocumentMask];
604     [webBrowserView setMaximumScale:5.0f forDocumentTypes:UIEveryDocumentMask];
605     [webBrowserView setInitialScale:1.0f forDocumentTypes:UIEveryDocumentMask];
606
607     [(DumpRenderTreeBrowserView *)webBrowserView setScrollingUsesUIWebScrollView:NO];
608     [webBrowserView setDelegate: nil];
609
610     [scrollView setBounds:layoutTestViewportRect];
611     [scrollView setFrame:layoutTestViewportRect];
612
613     [webBrowserView setMinimumSize:layoutTestViewportRect.size];
614     [webBrowserView setAutoresizes:NO];
615     CGRect browserViewFrame = [webBrowserView frame];
616     browserViewFrame.origin = CGPointZero;
617     [webBrowserView setFrame:browserViewFrame];
618 }
619 #endif
620
621 #if !PLATFORM(IOS)
622 @interface DRTMockScroller : NSScroller
623 @end
624
625 @implementation DRTMockScroller
626
627 - (NSRect)rectForPart:(NSScrollerPart)partCode
628 {
629     if (partCode != NSScrollerKnob)
630         return [super rectForPart:partCode];
631
632     NSRect frameRect = [self frame];
633     NSRect bounds = [self bounds];
634     BOOL isHorizontal = frameRect.size.width > frameRect.size.height;
635     CGFloat trackLength = isHorizontal ? bounds.size.width : bounds.size.height;
636     CGFloat minKnobSize = isHorizontal ? bounds.size.height : bounds.size.width;
637     CGFloat knobLength = max(minKnobSize, static_cast<CGFloat>(round(trackLength * [self knobProportion])));
638     CGFloat knobPosition = static_cast<CGFloat>((round([self doubleValue] * (trackLength - knobLength))));
639     
640     if (isHorizontal)
641         return NSMakeRect(bounds.origin.x + knobPosition, bounds.origin.y, knobLength, bounds.size.height);
642
643     return NSMakeRect(bounds.origin.x, bounds.origin.y +  + knobPosition, bounds.size.width, knobLength);
644 }
645
646 - (void)drawKnob
647 {
648     if (![self isEnabled])
649         return;
650
651     NSRect knobRect = [self rectForPart:NSScrollerKnob];
652     
653     static NSColor *knobColor = [[NSColor colorWithDeviceRed:0x80 / 255.0 green:0x80 / 255.0 blue:0x80 / 255.0 alpha:1] retain];
654     [knobColor set];
655
656     NSRectFill(knobRect);
657 }
658
659 - (void)drawRect:(NSRect)dirtyRect
660 {
661     static NSColor *trackColor = [[NSColor colorWithDeviceRed:0xC0 / 255.0 green:0xC0 / 255.0 blue:0xC0 / 255.0 alpha:1] retain];
662     static NSColor *disabledTrackColor = [[NSColor colorWithDeviceRed:0xE0 / 255.0 green:0xE0 / 255.0 blue:0xE0 / 255.0 alpha:1] retain];
663
664     if ([self isEnabled])
665         [trackColor set];
666     else
667         [disabledTrackColor set];
668
669     NSRectFill(dirtyRect);
670     
671     [self drawKnob];
672 }
673
674 @end
675
676 static void registerMockScrollbars()
677 {
678     [WebDynamicScrollBarsView setCustomScrollerClass:[DRTMockScroller class]];
679 }
680 #endif
681
682 WebView *createWebViewAndOffscreenWindow()
683 {
684 #if !PLATFORM(IOS)
685     NSRect rect = NSMakeRect(0, 0, TestRunner::viewWidth, TestRunner::viewHeight);
686     WebView *webView = [[WebView alloc] initWithFrame:rect frameName:nil groupName:@"org.webkit.DumpRenderTree"];
687 #else
688     UIWebBrowserView *webBrowserView = [[[DumpRenderTreeBrowserView alloc] initWithFrame:layoutTestViewportRect] autorelease];
689
690     WebView *webView = [[webBrowserView webView] retain];
691     [webView setGroupName:@"org.webkit.DumpRenderTree"];
692 #endif
693
694     [webView setUIDelegate:uiDelegate];
695     [webView setFrameLoadDelegate:frameLoadDelegate];
696     [webView setEditingDelegate:editingDelegate];
697     [webView setResourceLoadDelegate:resourceLoadDelegate];
698     [webView _setGeolocationProvider:[MockGeolocationProvider shared]];
699     [webView _setDeviceOrientationProvider:[WebDeviceOrientationProviderMock shared]];
700     [webView _setNotificationProvider:[MockWebNotificationProvider shared]];
701
702     // Register the same schemes that Safari does
703     [WebView registerURLSchemeAsLocal:@"feed"];
704     [WebView registerURLSchemeAsLocal:@"feeds"];
705     [WebView registerURLSchemeAsLocal:@"feedsearch"];
706     
707 #if !PLATFORM(IOS)
708     [webView setContinuousSpellCheckingEnabled:YES];
709     [webView setAutomaticQuoteSubstitutionEnabled:NO];
710     [webView setAutomaticLinkDetectionEnabled:NO];
711     [webView setAutomaticDashSubstitutionEnabled:NO];
712     [webView setAutomaticTextReplacementEnabled:NO];
713     [webView setAutomaticSpellingCorrectionEnabled:YES];
714     [webView setGrammarCheckingEnabled:YES];
715
716     [webView setDefersCallbacks:NO];
717     [webView setInteractiveFormValidationEnabled:YES];
718     [webView setValidationMessageTimerMagnification:-1];
719     
720     // 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.
721     // Put it at -10000, -10000 in "flipped coordinates", since WebCore and the DOM use flipped coordinates.
722     NSRect windowRect = NSOffsetRect(rect, -10000, [(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
723     DumpRenderTreeWindow *window = [[DumpRenderTreeWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
724
725     [window setColorSpace:[[NSScreen mainScreen] colorSpace]];
726     [window setCollectionBehavior:NSWindowCollectionBehaviorStationary];
727     [[window contentView] addSubview:webView];
728     [window orderBack:nil];
729     [window setAutodisplay:NO];
730
731     [window startListeningForAcceleratedCompositingChanges];
732 #else
733     DumpRenderTreeWindow *drtWindow = [[DumpRenderTreeWindow alloc] initWithLayer:[webBrowserView layer]];
734     [drtWindow setContentView:webView];
735     [webBrowserView setWAKWindow:drtWindow];
736
737     [[webView window] makeFirstResponder:[[[webView mainFrame] frameView] documentView]];
738
739     CGRect uiWindowRect = layoutTestViewportRect;
740     uiWindowRect.origin.y += [UIApp statusBarHeight];
741     UIWindow *uiWindow = [[[UIWindow alloc] initWithFrame:uiWindowRect] autorelease];
742
743     // The UIWindow and UIWebBrowserView are released when the DumpRenderTreeWindow is closed.
744     drtWindow.uiWindow = uiWindow;
745     drtWindow.browserView = webBrowserView;
746
747     UIWebScrollView *scrollView = [[UIWebScrollView alloc] initWithFrame:layoutTestViewportRect];
748     [scrollView addSubview:webBrowserView];
749
750     [uiWindow addSubview:scrollView];
751     [scrollView release];
752
753     adjustWebDocumentForStandardViewport(webBrowserView, scrollView);
754 #endif
755     
756 #if !PLATFORM(IOS)
757     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
758     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
759     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
760     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
761 #else
762     [[webView mainFrame] _setVisibleSize:CGSizeMake(phoneViewWidth, phoneViewHeight)];
763     [[webView preferences] _setTelephoneNumberParsingEnabled:NO];
764
765     // Initialize the global UIViews, and set the key UIWindow to be painted.
766     if (!gWebBrowserView) {
767         gWebBrowserView = [webBrowserView retain];
768         gWebScrollView = [scrollView retain];
769         gDrtWindow = [drtWindow retain];
770         [uiWindow makeKeyAndVisible];
771         [uiWindow retain];
772     }
773 #endif
774
775     return webView;
776 }
777
778 static NSString *libraryPathForDumpRenderTree()
779 {
780     //FIXME: This may not be sufficient to prevent interactions/crashes
781     //when running more than one copy of DumpRenderTree.
782     //See https://bugs.webkit.org/show_bug.cgi?id=10906
783     char* dumpRenderTreeTemp = getenv("DUMPRENDERTREE_TEMP");
784     if (dumpRenderTreeTemp)
785         return [[NSFileManager defaultManager] stringWithFileSystemRepresentation:dumpRenderTreeTemp length:strlen(dumpRenderTreeTemp)];
786     else
787         return [@"~/Library/Application Support/DumpRenderTree" stringByExpandingTildeInPath];
788 }
789
790 // Called before each test.
791 static void resetWebPreferencesToConsistentValues()
792 {
793     WebPreferences *preferences = [WebPreferences standardPreferences];
794
795     [preferences setAllowUniversalAccessFromFileURLs:YES];
796     [preferences setAllowFileAccessFromFileURLs:YES];
797     [preferences setStandardFontFamily:@"Times"];
798     [preferences setFixedFontFamily:@"Courier"];
799     [preferences setSerifFontFamily:@"Times"];
800     [preferences setSansSerifFontFamily:@"Helvetica"];
801     [preferences setCursiveFontFamily:@"Apple Chancery"];
802     [preferences setFantasyFontFamily:@"Papyrus"];
803     [preferences setPictographFontFamily:@"Apple Color Emoji"];
804     [preferences setDefaultFontSize:16];
805     [preferences setDefaultFixedFontSize:13];
806     [preferences setMinimumFontSize:0];
807     [preferences setDefaultTextEncodingName:@"ISO-8859-1"];
808     [preferences setJavaEnabled:NO];
809     [preferences setJavaScriptEnabled:YES];
810     [preferences setEditableLinkBehavior:WebKitEditableLinkOnlyLiveWithShiftKey];
811 #if !PLATFORM(IOS)
812     [preferences setTabsToLinks:NO];
813 #endif
814     [preferences setDOMPasteAllowed:YES];
815 #if !PLATFORM(IOS)
816     [preferences setShouldPrintBackgrounds:YES];
817 #endif
818     [preferences setCacheModel:WebCacheModelDocumentBrowser];
819     [preferences setXSSAuditorEnabled:NO];
820     [preferences setExperimentalNotificationsEnabled:NO];
821     [preferences setPlugInsEnabled:YES];
822 #if !PLATFORM(IOS)
823     [preferences setTextAreasAreResizable:YES];
824 #endif
825
826     [preferences setPrivateBrowsingEnabled:NO];
827     [preferences setAuthorAndUserStylesEnabled:YES];
828     [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
829     [preferences setJavaScriptCanAccessClipboard:YES];
830     [preferences setOfflineWebApplicationCacheEnabled:YES];
831     [preferences setDeveloperExtrasEnabled:NO];
832     [preferences setJavaScriptExperimentsEnabled:YES];
833     [preferences setLoadsImagesAutomatically:YES];
834     [preferences setLoadsSiteIconsIgnoringImageLoadingPreference:NO];
835     [preferences setFrameFlatteningEnabled:NO];
836     [preferences setSpatialNavigationEnabled:NO];
837     if (persistentUserStyleSheetLocation) {
838         [preferences setUserStyleSheetLocation:[NSURL URLWithString:(NSString *)(persistentUserStyleSheetLocation.get())]];
839         [preferences setUserStyleSheetEnabled:YES];
840     } else
841         [preferences setUserStyleSheetEnabled:NO];
842 #if PLATFORM(IOS)
843     [preferences setMediaPlaybackAllowsInline:YES];
844     [preferences setMediaPlaybackRequiresUserGesture:NO];
845
846     // Enable the tracker before creating the first WebView will
847     // cause initialization to use the correct database paths.
848     [preferences setStorageTrackerEnabled:YES];
849 #endif
850
851 #if ENABLE(IOS_TEXT_AUTOSIZING)
852     // Disable text autosizing by default.
853     [preferences _setMinimumZoomFontSize:0];
854 #endif
855
856     // The back/forward cache is causing problems due to layouts during transition from one page to another.
857     // So, turn it off for now, but we might want to turn it back on some day.
858     [preferences setUsesPageCache:NO];
859     [preferences setAcceleratedCompositingEnabled:YES];
860 #if USE(CA)
861     [preferences setCanvasUsesAcceleratedDrawing:YES];
862     [preferences setAcceleratedDrawingEnabled:useAcceleratedDrawing];
863 #endif
864     [preferences setWebGLEnabled:NO];
865     [preferences setCSSRegionsEnabled:YES];
866     [preferences setCSSGridLayoutEnabled:NO];
867     [preferences setUsePreHTML5ParserQuirks:NO];
868     [preferences setAsynchronousSpellCheckingEnabled:NO];
869     [preferences setMockScrollbarsEnabled:YES];
870     [preferences setSeamlessIFramesEnabled:YES];
871
872 #if ENABLE(WEB_AUDIO)
873     [preferences setWebAudioEnabled:YES];
874 #endif
875
876 #if ENABLE(IOS_TEXT_AUTOSIZING)
877     // Disable text autosizing by default.
878     [preferences _setMinimumZoomFontSize:0];
879 #endif
880
881     [preferences setScreenFontSubstitutionEnabled:YES];
882
883 #if ENABLE(MEDIA_SOURCE)
884     [preferences setMediaSourceEnabled:YES];
885 #endif
886
887     [WebPreferences _setCurrentNetworkLoaderSessionCookieAcceptPolicy:NSHTTPCookieAcceptPolicyOnlyFromMainDocumentDomain];
888 }
889
890 // Called once on DumpRenderTree startup.
891 static void setDefaultsToConsistentValuesForTesting()
892 {
893 #if PLATFORM(IOS)
894     WebThreadLock();
895 #endif
896
897     static const int NoFontSmoothing = 0;
898     static const int BlueTintedAppearance = 1;
899
900     NSString *libraryPath = libraryPathForDumpRenderTree();
901
902     NSDictionary *dict = @{
903         @"AppleKeyboardUIMode": @1,
904         @"AppleMagnifiedMode": @YES,
905         @"AppleAntiAliasingThreshold": @4,
906         @"AppleFontSmoothing": @(NoFontSmoothing),
907         @"AppleAquaColorVariant": @(BlueTintedAppearance),
908         @"AppleHighlightColor": @"0.709800 0.835300 1.000000",
909         @"AppleOtherHighlightColor":@"0.500000 0.500000 0.500000",
910         @"AppleLanguages": @[ @"en" ],
911         WebKitEnableFullDocumentTeardownPreferenceKey: @YES,
912         WebKitFullScreenEnabledPreferenceKey: @YES,
913         @"UseWebKitWebInspector": @YES,
914 #if !PLATFORM(IOS)
915         @"NSTestCorrectionDictionary": @{
916             @"notationl": @"notational",
917             @"mesage": @"message",
918             @"wouldn": @"would",
919             @"wellcome": @"welcome",
920             @"hellolfworld": @"hello\nworld"
921         },
922 #endif
923         @"WebKitKerningAndLigaturesEnabledByDefault": @NO,
924         @"AppleScrollBarVariant": @"DoubleMax",
925 #if !PLATFORM(IOS)
926         @"NSScrollAnimationEnabled": @NO,
927 #endif
928         @"NSOverlayScrollersEnabled": @NO,
929         @"AppleShowScrollBars": @"Always",
930         WebDatabaseDirectoryDefaultsKey: [libraryPath stringByAppendingPathComponent:@"Databases"],
931         WebStorageDirectoryDefaultsKey: [libraryPath stringByAppendingPathComponent:@"LocalStorage"],
932         WebKitLocalCacheDefaultsKey: [libraryPath stringByAppendingPathComponent:@"LocalCache"]
933     };
934
935     [[NSUserDefaults standardUserDefaults] setValuesForKeysWithDictionary:dict];
936 }
937
938 static void runThread(void* arg)
939 {
940     static ThreadIdentifier previousId = 0;
941     ThreadIdentifier currentId = currentThread();
942     // Verify 2 successive threads do not get the same Id.
943     ASSERT(previousId != currentId);
944     previousId = currentId;
945 }
946
947 static void* runPthread(void* arg)
948 {
949     runThread(arg);
950     return 0;
951 }
952
953 static void testThreadIdentifierMap()
954 {
955     // Imitate 'foreign' threads that are not created by WTF.
956     pthread_t pthread;
957     pthread_create(&pthread, 0, &runPthread, 0);
958     pthread_join(pthread, 0);
959
960     pthread_create(&pthread, 0, &runPthread, 0);
961     pthread_join(pthread, 0);
962
963     // Now create another thread using WTF. On OSX, it will have the same pthread handle
964     // but should get a different ThreadIdentifier.
965     createThread(runThread, 0, "DumpRenderTree: test");
966 }
967
968 static void allocateGlobalControllers()
969 {
970     // FIXME: We should remove these and move to the ObjC standard [Foo sharedInstance] model
971     gNavigationController = [[NavigationController alloc] init];
972     frameLoadDelegate = [[FrameLoadDelegate alloc] init];
973     uiDelegate = [[UIDelegate alloc] init];
974     editingDelegate = [[EditingDelegate alloc] init];
975     resourceLoadDelegate = [[ResourceLoadDelegate alloc] init];
976     policyDelegate = [[PolicyDelegate alloc] init];
977     historyDelegate = [[HistoryDelegate alloc] init];
978     storageDelegate = [[StorageTrackerDelegate alloc] init];
979     defaultPolicyDelegate = [[DefaultPolicyDelegate alloc] init];
980 #if PLATFORM(IOS)
981     scrollViewResizerDelegate = [[ScrollViewResizerDelegate alloc] init];
982 #endif
983 }
984
985 // ObjC++ doens't seem to let me pass NSObject*& sadly.
986 static inline void releaseAndZero(NSObject** object)
987 {
988     [*object release];
989     *object = nil;
990 }
991
992 static void releaseGlobalControllers()
993 {
994     releaseAndZero(&gNavigationController);
995     releaseAndZero(&frameLoadDelegate);
996     releaseAndZero(&editingDelegate);
997     releaseAndZero(&resourceLoadDelegate);
998     releaseAndZero(&uiDelegate);
999     releaseAndZero(&policyDelegate);
1000     releaseAndZero(&storageDelegate);
1001 #if PLATFORM(IOS)
1002     releaseAndZero(&scrollViewResizerDelegate);
1003 #endif
1004 }
1005
1006 static void initializeGlobalsFromCommandLineOptions(int argc, const char *argv[])
1007 {
1008     struct option options[] = {
1009         {"notree", no_argument, &dumpTree, NO},
1010         {"pixel-tests", no_argument, &dumpPixelsForAllTests, YES},
1011         {"tree", no_argument, &dumpTree, YES},
1012         {"threaded", no_argument, &threaded, YES},
1013         {"complex-text", no_argument, &forceComplexText, YES},
1014         {"accelerated-drawing", no_argument, &useAcceleratedDrawing, YES},
1015         {"gc-between-tests", no_argument, &gcBetweenTests, YES},
1016         {"no-timeout", no_argument, &useTimeoutWatchdog, NO},
1017         {NULL, 0, NULL, 0}
1018     };
1019     
1020     int option;
1021     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1) {
1022         switch (option) {
1023             case '?':   // unknown or ambiguous option
1024             case ':':   // missing argument
1025                 exit(1);
1026                 break;
1027         }
1028     }
1029 }
1030
1031 static void addTestPluginsToPluginSearchPath(const char* executablePath)
1032 {
1033 #if !PLATFORM(IOS)
1034     NSString *pwd = [[NSString stringWithUTF8String:executablePath] stringByDeletingLastPathComponent];
1035     [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
1036     [[WebPluginDatabase sharedDatabase] refresh];
1037 #endif
1038 }
1039
1040 static bool useLongRunningServerMode(int argc, const char *argv[])
1041 {
1042     // This assumes you've already called getopt_long
1043     return (argc == optind+1 && strcmp(argv[optind], "-") == 0);
1044 }
1045
1046 static void runTestingServerLoop()
1047 {
1048     // When DumpRenderTree run in server mode, we just wait around for file names
1049     // to be passed to us and read each in turn, passing the results back to the client
1050     char filenameBuffer[2048];
1051     while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
1052         char *newLineCharacter = strchr(filenameBuffer, '\n');
1053         if (newLineCharacter)
1054             *newLineCharacter = '\0';
1055
1056         if (strlen(filenameBuffer) == 0)
1057             continue;
1058
1059         runTest(filenameBuffer);
1060     }
1061 }
1062
1063 static void prepareConsistentTestingEnvironment()
1064 {
1065 #if !PLATFORM(IOS)
1066     poseAsClass("DumpRenderTreePasteboard", "NSPasteboard");
1067     poseAsClass("DumpRenderTreeEvent", "NSEvent");
1068 #else
1069     poseAsClass("DumpRenderTreeEvent", "GSEvent");
1070 #endif
1071
1072     [[WebPreferences standardPreferences] setAutosaves:NO];
1073
1074 #if !PLATFORM(IOS)
1075
1076     // FIXME: We'd like to start with a clean state for every test, but this function can't be used more than once yet.
1077     [WebPreferences _switchNetworkLoaderToNewTestingSession];
1078
1079     NSURLCache *sharedCache =
1080         [[NSURLCache alloc] initWithMemoryCapacity:1024 * 1024
1081                                       diskCapacity:0
1082                                           diskPath:[libraryPathForDumpRenderTree() stringByAppendingPathComponent:@"URLCache"]];
1083     [NSURLCache setSharedURLCache:sharedCache];
1084     [sharedCache release];
1085
1086     adjustFonts();
1087     registerMockScrollbars();
1088 #else
1089     activateFontsIOS();
1090 #endif
1091     
1092     allocateGlobalControllers();
1093     
1094     makeLargeMallocFailSilently();
1095
1096 #if !PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
1097     NSActivityOptions options = (NSActivityUserInitiatedAllowingIdleSystemSleep | NSActivityLatencyCritical) & ~(NSActivitySuddenTerminationDisabled | NSActivityAutomaticTerminationDisabled);
1098     static id assertion = [[[NSProcessInfo processInfo] beginActivityWithOptions:options reason:@"DumpRenderTree should not be subject to process suppression"] retain];
1099     ASSERT_UNUSED(assertion, assertion);
1100 #endif
1101 }
1102
1103 void dumpRenderTree(int argc, const char *argv[])
1104 {
1105 #if PLATFORM(IOS)
1106     int infd = open("/tmp/DumpRenderTree_IN", O_RDWR);
1107     dup2(infd, STDIN_FILENO);
1108     int outfd = open("/tmp/DumpRenderTree_OUT", O_RDWR);
1109     dup2(outfd, STDOUT_FILENO);
1110     int errfd = open("/tmp/DumpRenderTree_ERROR", O_RDWR | O_NONBLOCK);
1111     dup2(errfd, STDERR_FILENO);
1112 #endif
1113
1114     initializeGlobalsFromCommandLineOptions(argc, argv);
1115     prepareConsistentTestingEnvironment();
1116     addTestPluginsToPluginSearchPath(argv[0]);
1117
1118     if (forceComplexText)
1119         [WebView _setAlwaysUsesComplexTextCodePath:YES];
1120
1121 #if USE(APPKIT)
1122     [NSSound _setAlertType:0];
1123 #endif
1124
1125     WebView *webView = createWebViewAndOffscreenWindow();
1126     mainFrame = [webView mainFrame];
1127
1128     [[NSURLCache sharedURLCache] removeAllCachedResponses];
1129     [WebCache empty];
1130
1131     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"localhost"];
1132     [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:@"127.0.0.1"];
1133
1134     // http://webkit.org/b/32689
1135     testThreadIdentifierMap();
1136
1137     if (threaded)
1138         startJavaScriptThreads();
1139
1140     if (useLongRunningServerMode(argc, argv)) {
1141         printSeparators = YES;
1142         runTestingServerLoop();
1143     } else {
1144         printSeparators = optind < argc - 1;
1145         for (int i = optind; i != argc; ++i)
1146             runTest(argv[i]);
1147     }
1148
1149     if (threaded)
1150         stopJavaScriptThreads();
1151
1152 #if !PLATFORM(IOS)
1153     NSWindow *window = [webView window];
1154 #endif
1155     [webView close];
1156     mainFrame = nil;
1157
1158 #if !PLATFORM(IOS)
1159     // Work around problem where registering drag types leaves an outstanding
1160     // "perform selector" on the window, which retains the window. It's a bit
1161     // inelegant and perhaps dangerous to just blow them all away, but in practice
1162     // it probably won't cause any trouble (and this is just a test tool, after all).
1163     [NSObject cancelPreviousPerformRequestsWithTarget:window];
1164     
1165     [window close]; // releases when closed
1166 #else
1167     UIWindow *uiWindow = [gWebBrowserView window];
1168     [uiWindow removeFromSuperview];
1169     [uiWindow release];
1170 #endif
1171
1172     [webView release];
1173     
1174     releaseGlobalControllers();
1175     
1176 #if !PLATFORM(IOS)
1177     [DumpRenderTreePasteboard releaseLocalPasteboards];
1178 #endif
1179
1180     // FIXME: This should be moved onto TestRunner and made into a HashSet
1181     if (disallowedURLs) {
1182         CFRelease(disallowedURLs);
1183         disallowedURLs = 0;
1184     }
1185
1186 #if PLATFORM(IOS)
1187     close(infd);
1188     close(outfd);
1189     close(errfd);
1190 #endif
1191 }
1192
1193 #if PLATFORM(IOS)
1194 static int _argc;
1195 static const char **_argv;
1196
1197 @implementation DumpRenderTree
1198
1199 - (void)_runDumpRenderTree
1200 {
1201     dumpRenderTree(_argc, _argv);
1202 }
1203
1204 - (void)applicationDidFinishLaunching:(NSNotification *)notification
1205 {
1206     [self performSelectorOnMainThread:@selector(_runDumpRenderTree) withObject:nil waitUntilDone:NO];
1207 }
1208
1209 - (void)_deferDumpToMainThread
1210 {
1211     ASSERT(WebThreadIsCurrent());
1212     
1213     dispatch_async(dispatch_get_main_queue(), ^{
1214         dump();
1215     });
1216 }
1217
1218 - (void)_webThreadEventLoopHasRun
1219 {
1220     ASSERT(!WebThreadIsCurrent());
1221     _hasFlushedWebThreadRunQueue = YES;
1222 }
1223
1224 - (void)_webThreadInvoked
1225 {
1226     ASSERT(WebThreadIsCurrent());
1227     dispatch_async(dispatch_get_main_queue(), ^{
1228         [self _webThreadEventLoopHasRun];
1229     });
1230 }
1231
1232 // The test can end in response to a delegate callback while there are still methods queued on the Web Thread.
1233 // If we do not ensure the Web Thread has been run, the callback can be done on a WebView that no longer exists.
1234 // To avoid this, _waitForWebThread dispatches a call to the WebThread event loop, actively processing the delegate
1235 // callbacks in the main thread while waiting for the call to be invoked on the Web Thread.
1236 - (void)_waitForWebThread
1237 {
1238     ASSERT(!WebThreadIsCurrent());
1239     _hasFlushedWebThreadRunQueue = NO;
1240     WebThreadRun(^{
1241         [self _webThreadInvoked];
1242     });
1243     while (!_hasFlushedWebThreadRunQueue) {
1244         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1245         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1246         [pool release];
1247     }
1248 }
1249
1250 @end
1251 #endif
1252
1253 int DumpRenderTreeMain(int argc, const char *argv[])
1254 {
1255 #if PLATFORM(IOS)
1256     _UIApplicationLoadWebKit();
1257 #endif
1258     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1259
1260     setDefaultsToConsistentValuesForTesting(); // Must be called before NSApplication initialization.
1261
1262 #if !PLATFORM(IOS)
1263     [DumpRenderTreeApplication sharedApplication]; // Force AppKit to init itself
1264
1265     dumpRenderTree(argc, argv);
1266 #else
1267     _argc = argc;
1268     _argv = argv;
1269     UIApplicationMain(argc, (char**)argv, @"DumpRenderTree", nil);
1270 #endif
1271     [WebCoreStatistics garbageCollectJavaScriptObjects];
1272     [WebCoreStatistics emptyCache]; // Otherwise SVGImages trigger false positives for Frame/Node counts
1273     if (JSC::Options::logHeapStatisticsAtExit())
1274         JSC::HeapStatistics::reportSuccess();
1275     [pool release];
1276     return 0;
1277 }
1278
1279 static NSInteger compareHistoryItems(id item1, id item2, void *context)
1280 {
1281     return [[item1 target] caseInsensitiveCompare:[item2 target]];
1282 }
1283
1284 static NSData *dumpAudio()
1285 {
1286     const vector<char>& dataVector = gTestRunner->audioResult();
1287     
1288     NSData *data = [NSData dataWithBytes:dataVector.data() length:dataVector.size()];
1289     return data;
1290 }
1291
1292 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
1293 {
1294     int start = 0;
1295     if (current) {
1296         printf("curr->");
1297         start = 6;
1298     }
1299     for (int i = start; i < indent; i++)
1300         putchar(' ');
1301     
1302     NSString *urlString = [item URLString];
1303     if ([[NSURL URLWithString:urlString] isFileURL]) {
1304         NSRange range = [urlString rangeOfString:@"/LayoutTests/"];
1305         urlString = [@"(file test):" stringByAppendingString:[urlString substringFromIndex:(range.length + range.location)]];
1306     }
1307     
1308     printf("%s", [urlString UTF8String]);
1309     NSString *target = [item target];
1310     if (target && [target length] > 0)
1311         printf(" (in frame \"%s\")", [target UTF8String]);
1312     if ([item isTargetItem])
1313         printf("  **nav target**");
1314     putchar('\n');
1315     NSArray *kids = [item children];
1316     if (kids) {
1317         // must sort to eliminate arbitrary result ordering which defeats reproducible testing
1318         kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
1319         for (unsigned i = 0; i < [kids count]; i++)
1320             dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
1321     }
1322 }
1323
1324 static void dumpFrameScrollPosition(WebFrame *f)
1325 {
1326     WebScriptObject* scriptObject = [f windowObject];
1327     NSPoint scrollPosition = NSMakePoint(
1328         [[scriptObject valueForKey:@"pageXOffset"] floatValue],
1329         [[scriptObject valueForKey:@"pageYOffset"] floatValue]);
1330     if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
1331         if ([f parentFrame] != nil)
1332             printf("frame '%s' ", [[f name] UTF8String]);
1333         printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
1334     }
1335
1336     if (gTestRunner->dumpChildFrameScrollPositions()) {
1337         NSArray *kids = [f childFrames];
1338         if (kids)
1339             for (unsigned i = 0; i < [kids count]; i++)
1340                 dumpFrameScrollPosition([kids objectAtIndex:i]);
1341     }
1342 }
1343
1344 static NSString *dumpFramesAsText(WebFrame *frame)
1345 {
1346     DOMDocument *document = [frame DOMDocument];
1347     DOMElement *documentElement = [document documentElement];
1348
1349     if (!documentElement)
1350         return @"";
1351
1352     NSMutableString *result = [[[NSMutableString alloc] init] autorelease];
1353
1354     // Add header for all but the main frame.
1355     if ([frame parentFrame])
1356         result = [NSMutableString stringWithFormat:@"\n--------\nFrame: '%@'\n--------\n", [frame name]];
1357
1358     [result appendFormat:@"%@\n", [documentElement innerText]];
1359
1360     if (gTestRunner->dumpChildFramesAsText()) {
1361         NSArray *kids = [frame childFrames];
1362         if (kids) {
1363             for (unsigned i = 0; i < [kids count]; i++)
1364                 [result appendString:dumpFramesAsText([kids objectAtIndex:i])];
1365         }
1366     }
1367
1368     return result;
1369 }
1370
1371 static NSData *dumpFrameAsPDF(WebFrame *frame)
1372 {
1373 #if !PLATFORM(IOS)
1374     if (!frame)
1375         return nil;
1376
1377     // Sadly we have to dump to a file and then read from that file again
1378     // +[NSPrintOperation PDFOperationWithView:insideRect:] requires a rect and prints to a single page
1379     // likewise +[NSView dataWithPDFInsideRect:] also prints to a single continuous page
1380     // The goal of this function is to test "real" printing across multiple pages.
1381     // FIXME: It's possible there might be printing SPI to let us print a multi-page PDF to an NSData object
1382     NSString *path = [libraryPathForDumpRenderTree() stringByAppendingPathComponent:@"test.pdf"];
1383
1384     NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]];
1385     [printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
1386     [printInfoDict setObject:path forKey:NSPrintSavePath];
1387
1388     NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
1389     [printInfo setHorizontalPagination:NSAutoPagination];
1390     [printInfo setVerticalPagination:NSAutoPagination];
1391     [printInfo setVerticallyCentered:NO];
1392
1393     NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:[frame frameView] printInfo:printInfo];
1394     [printOperation setShowPanels:NO];
1395     [printOperation runOperation];
1396
1397     [printInfo release];
1398
1399     NSData *pdfData = [NSData dataWithContentsOfFile:path];
1400     [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
1401
1402     return pdfData;
1403 #else
1404     return nil;
1405 #endif
1406 }
1407
1408 static void dumpBackForwardListForWebView(WebView *view)
1409 {
1410     printf("\n============== Back Forward List ==============\n");
1411     WebBackForwardList *bfList = [view backForwardList];
1412
1413     // Print out all items in the list after prevTestBFItem, which was from the previous test
1414     // Gather items from the end of the list, the print them out from oldest to newest
1415     NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
1416     for (int i = [bfList forwardListCount]; i > 0; i--) {
1417         WebHistoryItem *item = [bfList itemAtIndex:i];
1418         // something is wrong if the item from the last test is in the forward part of the b/f list
1419         assert(item != prevTestBFItem);
1420         [itemsToPrint addObject:item];
1421     }
1422             
1423     assert([bfList currentItem] != prevTestBFItem);
1424     [itemsToPrint addObject:[bfList currentItem]];
1425     int currentItemIndex = [itemsToPrint count] - 1;
1426
1427     for (int i = -1; i >= -[bfList backListCount]; i--) {
1428         WebHistoryItem *item = [bfList itemAtIndex:i];
1429         if (item == prevTestBFItem)
1430             break;
1431         [itemsToPrint addObject:item];
1432     }
1433
1434     for (int i = [itemsToPrint count]-1; i >= 0; i--)
1435         dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
1436
1437     [itemsToPrint release];
1438     printf("===============================================\n");
1439 }
1440
1441 static void sizeWebViewForCurrentTest()
1442 {
1443     // W3C SVG tests expect to be 480x360
1444     bool isSVGW3CTest = (gTestRunner->testPathOrURL().find("svg/W3C-SVG-1.1") != string::npos);
1445     if (isSVGW3CTest)
1446         [[mainFrame webView] setFrameSize:NSMakeSize(TestRunner::w3cSVGViewWidth, TestRunner::w3cSVGViewHeight)];
1447     else
1448         [[mainFrame webView] setFrameSize:NSMakeSize(TestRunner::viewWidth, TestRunner::viewHeight)];
1449 }
1450
1451 static const char *methodNameStringForFailedTest()
1452 {
1453     const char *errorMessage;
1454     if (gTestRunner->dumpAsText())
1455         errorMessage = "[documentElement innerText]";
1456     else if (gTestRunner->dumpDOMAsWebArchive())
1457         errorMessage = "[[mainFrame DOMDocument] webArchive]";
1458     else if (gTestRunner->dumpSourceAsWebArchive())
1459         errorMessage = "[[mainFrame dataSource] webArchive]";
1460     else
1461         errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
1462
1463     return errorMessage;
1464 }
1465
1466 static void dumpBackForwardListForAllWindows()
1467 {
1468     CFArrayRef openWindows = (CFArrayRef)[DumpRenderTreeWindow openWindows];
1469     unsigned count = CFArrayGetCount(openWindows);
1470     for (unsigned i = 0; i < count; i++) {
1471         NSWindow *window = (NSWindow *)CFArrayGetValueAtIndex(openWindows, i);
1472 #if !PLATFORM(IOS)
1473         WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
1474 #else
1475         ASSERT([[window contentView] isKindOfClass:[WebView class]]);
1476         WebView *webView = (WebView *)[window contentView];
1477 #endif
1478         dumpBackForwardListForWebView(webView);
1479     }
1480 }
1481
1482 static void invalidateAnyPreviousWaitToDumpWatchdog()
1483 {
1484     if (waitToDumpWatchdog) {
1485         CFRunLoopTimerInvalidate(waitToDumpWatchdog);
1486         CFRelease(waitToDumpWatchdog);
1487         waitToDumpWatchdog = 0;
1488     }
1489 }
1490
1491 void setWaitToDumpWatchdog(CFRunLoopTimerRef timer)
1492 {
1493     ASSERT(timer);
1494     ASSERT(shouldSetWaitToDumpWatchdog());
1495     waitToDumpWatchdog = timer;
1496     CFRunLoopAddTimer(CFRunLoopGetCurrent(), waitToDumpWatchdog, kCFRunLoopCommonModes);
1497 }
1498
1499 bool shouldSetWaitToDumpWatchdog()
1500 {
1501     return !waitToDumpWatchdog && useTimeoutWatchdog;
1502 }
1503
1504 void dump()
1505 {
1506 #if PLATFORM(IOS)
1507     // This can get called on the web thread if from a JavaScript notifyDone().
1508     if (WebThreadIsCurrent()) {
1509         [(DumpRenderTree *)UIApp _deferDumpToMainThread];
1510         return;
1511     }
1512     WebThreadLock();
1513 #endif
1514     
1515     invalidateAnyPreviousWaitToDumpWatchdog();
1516     ASSERT(!gTestRunner->hasPendingWebNotificationClick());
1517
1518     if (dumpTree) {
1519         NSString *resultString = nil;
1520         NSData *resultData = nil;
1521         NSString *resultMimeType = @"text/plain";
1522
1523         if ([[[mainFrame dataSource] _responseMIMEType] isEqualToString:@"text/plain"]) {
1524             gTestRunner->setDumpAsText(true);
1525             gTestRunner->setGeneratePixelResults(false);
1526         }
1527         if (gTestRunner->dumpAsAudio()) {
1528             resultData = dumpAudio();
1529             resultMimeType = @"audio/wav";
1530         } else if (gTestRunner->dumpAsText()) {
1531             resultString = dumpFramesAsText(mainFrame);
1532         } else if (gTestRunner->dumpAsPDF()) {
1533             resultData = dumpFrameAsPDF(mainFrame);
1534             resultMimeType = @"application/pdf";
1535         } else if (gTestRunner->dumpDOMAsWebArchive()) {
1536             WebArchive *webArchive = [[mainFrame DOMDocument] webArchive];
1537             resultString = CFBridgingRelease(createXMLStringFromWebArchiveData((CFDataRef)[webArchive data]));
1538             resultMimeType = @"application/x-webarchive";
1539         } else if (gTestRunner->dumpSourceAsWebArchive()) {
1540             WebArchive *webArchive = [[mainFrame dataSource] webArchive];
1541             resultString = CFBridgingRelease(createXMLStringFromWebArchiveData((CFDataRef)[webArchive data]));
1542             resultMimeType = @"application/x-webarchive";
1543         } else
1544             resultString = [mainFrame renderTreeAsExternalRepresentationForPrinting:gTestRunner->isPrinting()];
1545
1546         if (resultString && !resultData)
1547             resultData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
1548
1549         printf("Content-Type: %s\n", [resultMimeType UTF8String]);
1550
1551         WTF::FastMallocStatistics mallocStats = WTF::fastMallocStatistics();
1552         printf("DumpMalloc: %li\n", mallocStats.committedVMBytes);
1553
1554         if (gTestRunner->dumpAsAudio())
1555             printf("Content-Length: %lu\n", static_cast<unsigned long>([resultData length]));
1556
1557         if (resultData) {
1558             fwrite([resultData bytes], 1, [resultData length], stdout);
1559
1560             if (!gTestRunner->dumpAsText() && !gTestRunner->dumpDOMAsWebArchive() && !gTestRunner->dumpSourceAsWebArchive() && !gTestRunner->dumpAsAudio())
1561                 dumpFrameScrollPosition(mainFrame);
1562
1563             if (gTestRunner->dumpBackForwardList())
1564                 dumpBackForwardListForAllWindows();
1565         } else
1566             printf("ERROR: nil result from %s", methodNameStringForFailedTest());
1567
1568         // Stop the watchdog thread before we leave this test to make sure it doesn't
1569         // fire in between tests causing the next test to fail.
1570         // This is a speculative fix for: https://bugs.webkit.org/show_bug.cgi?id=32339
1571         invalidateAnyPreviousWaitToDumpWatchdog();
1572
1573         if (printSeparators) {
1574             puts("#EOF");       // terminate the content block
1575             fputs("#EOF\n", stderr);
1576         }            
1577     }
1578
1579     if (dumpPixelsForCurrentTest && gTestRunner->generatePixelResults())
1580         // FIXME: when isPrinting is set, dump the image with page separators.
1581         dumpWebViewAsPixelsAndCompareWithExpected(gTestRunner->expectedPixelHash());
1582
1583     puts("#EOF");   // terminate the (possibly empty) pixels block
1584
1585     fflush(stdout);
1586     fflush(stderr);
1587
1588     done = YES;
1589     CFRunLoopStop(CFRunLoopGetMain());
1590 }
1591
1592 static bool shouldLogFrameLoadDelegates(const char* pathOrURL)
1593 {
1594     return strstr(pathOrURL, "loading/");
1595 }
1596
1597 static bool shouldLogHistoryDelegates(const char* pathOrURL)
1598 {
1599     return strstr(pathOrURL, "globalhistory/");
1600 }
1601
1602 static bool shouldOpenWebInspector(const char* pathOrURL)
1603 {
1604     return strstr(pathOrURL, "inspector/");
1605 }
1606
1607 static bool shouldDumpAsText(const char* pathOrURL)
1608 {
1609     return strstr(pathOrURL, "dumpAsText/");
1610 }
1611
1612 static bool shouldEnableDeveloperExtras(const char* pathOrURL)
1613 {
1614     return true;
1615 }
1616
1617 #if PLATFORM(IOS)
1618 static bool shouldMakeViewportFlexible(const char* pathOrURL)
1619 {
1620     return strstr(pathOrURL, "viewport/");
1621 }
1622 #endif
1623
1624 static void resetWebViewToConsistentStateBeforeTesting()
1625 {
1626     WebView *webView = [mainFrame webView];
1627 #if PLATFORM(IOS)
1628     adjustWebDocumentForStandardViewport(gWebBrowserView, gWebScrollView);
1629     [webView _setAllowsMessaging:YES];
1630     [mainFrame setMediaDataLoadsAutomatically:YES];
1631 #endif
1632     [webView setEditable:NO];
1633     [(EditingDelegate *)[webView editingDelegate] setAcceptsEditing:YES];
1634     [webView makeTextStandardSize:nil];
1635     [webView resetPageZoom:nil];
1636     [webView _scaleWebView:1.0 atOrigin:NSZeroPoint];
1637 #if !PLATFORM(IOS)
1638     [webView _setCustomBackingScaleFactor:0];
1639 #endif
1640     [webView setTabKeyCyclesThroughElements:YES];
1641     [webView setPolicyDelegate:defaultPolicyDelegate];
1642     [policyDelegate setPermissive:NO];
1643     [policyDelegate setControllerToNotifyDone:0];
1644     [frameLoadDelegate resetToConsistentState];
1645 #if !PLATFORM(IOS)
1646     [webView _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:NO];
1647 #endif
1648     [webView _clearMainFrameName];
1649     [[webView undoManager] removeAllActions];
1650     [WebView _removeAllUserContentFromGroup:[webView groupName]];
1651 #if !PLATFORM(IOS)
1652     [[webView window] setAutodisplay:NO];
1653 #endif
1654     [webView setTracksRepaints:NO];
1655     
1656     resetWebPreferencesToConsistentValues();
1657
1658     TestRunner::setSerializeHTTPLoads(false);
1659
1660     setlocale(LC_ALL, "");
1661
1662     if (gTestRunner) {
1663         WebCoreTestSupport::resetInternalsObject([mainFrame globalContext]);
1664         // in the case that a test using the chrome input field failed, be sure to clean up for the next test
1665         gTestRunner->removeChromeInputField();
1666     }
1667
1668 #if !PLATFORM(IOS)
1669     [webView setContinuousSpellCheckingEnabled:YES];
1670     [webView setAutomaticQuoteSubstitutionEnabled:NO];
1671     [webView setAutomaticLinkDetectionEnabled:NO];
1672     [webView setAutomaticDashSubstitutionEnabled:NO];
1673     [webView setAutomaticTextReplacementEnabled:NO];
1674     [webView setAutomaticSpellingCorrectionEnabled:YES];
1675     [webView setGrammarCheckingEnabled:YES];
1676
1677     [WebView _setUsesTestModeFocusRingColor:YES];
1678 #endif
1679     [WebView _resetOriginAccessWhitelists];
1680     [WebView _setAllowsRoundingHacks:NO];
1681
1682     [[MockGeolocationProvider shared] stopTimer];
1683     [[MockWebNotificationProvider shared] reset];
1684     
1685 #if !PLATFORM(IOS)
1686     // Clear the contents of the general pasteboard
1687     [[NSPasteboard generalPasteboard] declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
1688 #endif
1689
1690     [mainFrame _clearOpener];
1691 }
1692
1693 #if PLATFORM(IOS)
1694 // Work around <rdar://problem/9909073> WebKit's method of calling delegates on
1695 // the main thread is not thread safe. If the web thread is attempting to call
1696 // out to a delegate method on the main thread, we want to spin the main thread
1697 // run loop until the delegate method completes before taking the web thread
1698 // lock to prevent potentially re-entering WebCore.
1699 static void WebThreadLockAfterDelegateCallbacksHaveCompleted()
1700 {
1701     dispatch_semaphore_t delegateSemaphore = dispatch_semaphore_create(0);
1702     WebThreadRun(^{
1703         dispatch_semaphore_signal(delegateSemaphore);
1704     });
1705
1706     while (dispatch_semaphore_wait(delegateSemaphore, DISPATCH_TIME_NOW)) {
1707         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1708         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1709         [pool release];
1710     }
1711
1712     WebThreadLock();
1713     
1714     dispatch_release(delegateSemaphore);
1715 }
1716 #endif
1717
1718 static void runTest(const string& inputLine)
1719 {
1720     ASSERT(!inputLine.empty());
1721
1722     TestCommand command = parseInputLine(inputLine);
1723     const string& pathOrURL = command.pathOrURL;
1724     dumpPixelsForCurrentTest = command.shouldDumpPixels || dumpPixelsForAllTests;
1725
1726     NSString *pathOrURLString = [NSString stringWithUTF8String:pathOrURL.c_str()];
1727     if (!pathOrURLString) {
1728         fprintf(stderr, "Failed to parse \"%s\" as UTF-8\n", pathOrURL.c_str());
1729         return;
1730     }
1731
1732     NSURL *url;
1733     if ([pathOrURLString hasPrefix:@"http://"] || [pathOrURLString hasPrefix:@"https://"] || [pathOrURLString hasPrefix:@"file://"])
1734         url = [NSURL URLWithString:pathOrURLString];
1735     else
1736         url = [NSURL fileURLWithPath:pathOrURLString];
1737     if (!url) {
1738         fprintf(stderr, "Failed to parse \"%s\" as a URL\n", pathOrURL.c_str());
1739         return;
1740     }
1741
1742     const string testURL([[url absoluteString] UTF8String]);
1743     
1744     resetWebViewToConsistentStateBeforeTesting();
1745
1746     gTestRunner = TestRunner::create(testURL, command.expectedPixelHash);
1747     topLoadingFrame = nil;
1748 #if !PLATFORM(IOS)
1749     ASSERT(!draggingInfo); // the previous test should have called eventSender.mouseUp to drop!
1750     releaseAndZero(&draggingInfo);
1751 #endif
1752     done = NO;
1753
1754     sizeWebViewForCurrentTest();
1755     gTestRunner->setIconDatabaseEnabled(false);
1756
1757     if (disallowedURLs)
1758         CFSetRemoveAllValues(disallowedURLs);
1759     if (shouldLogFrameLoadDelegates(pathOrURL.c_str()))
1760         gTestRunner->setDumpFrameLoadCallbacks(true);
1761
1762     if (shouldLogHistoryDelegates(pathOrURL.c_str()))
1763         [[mainFrame webView] setHistoryDelegate:historyDelegate];
1764     else
1765         [[mainFrame webView] setHistoryDelegate:nil];
1766
1767     if (shouldEnableDeveloperExtras(pathOrURL.c_str())) {
1768         gTestRunner->setDeveloperExtrasEnabled(true);
1769         if (shouldOpenWebInspector(pathOrURL.c_str()))
1770             gTestRunner->showWebInspector();
1771         if (shouldDumpAsText(pathOrURL.c_str())) {
1772             gTestRunner->setDumpAsText(true);
1773             gTestRunner->setGeneratePixelResults(false);
1774         }
1775     }
1776
1777 #if PLATFORM(IOS)
1778     if (shouldMakeViewportFlexible(pathOrURL.c_str()))
1779         adjustWebDocumentForFlexibleViewport(gWebBrowserView, gWebScrollView);
1780 #endif
1781
1782     if ([WebHistory optionalSharedHistory])
1783         [WebHistory setOptionalSharedHistory:nil];
1784     lastMousePosition = NSZeroPoint;
1785     lastClickPosition = NSZeroPoint;
1786
1787     [prevTestBFItem release];
1788     prevTestBFItem = [[[[mainFrame webView] backForwardList] currentItem] retain];
1789
1790     WorkQueue::shared()->clear();
1791     WorkQueue::shared()->setFrozen(false);
1792
1793     bool ignoreWebCoreNodeLeaks = shouldIgnoreWebCoreNodeLeaks(testURL);
1794     if (ignoreWebCoreNodeLeaks)
1795         [WebCoreStatistics startIgnoringWebCoreNodeLeaks];
1796
1797     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1798     [mainFrame loadRequest:[NSURLRequest requestWithURL:url]];
1799     [pool release];
1800
1801     while (!done) {
1802         pool = [[NSAutoreleasePool alloc] init];
1803         CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10, false);
1804         [pool release];
1805     }
1806
1807 #if PLATFORM(IOS)
1808     [(DumpRenderTree *)UIApp _waitForWebThread];
1809     WebThreadLockAfterDelegateCallbacksHaveCompleted();
1810 #endif
1811     pool = [[NSAutoreleasePool alloc] init];
1812     [EventSendingController clearSavedEvents];
1813     [[mainFrame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1814
1815     WorkQueue::shared()->clear();
1816
1817     if (gTestRunner->closeRemainingWindowsWhenComplete()) {
1818         NSArray* array = [DumpRenderTreeWindow openWindows];
1819
1820         unsigned count = [array count];
1821         for (unsigned i = 0; i < count; i++) {
1822             NSWindow *window = [array objectAtIndex:i];
1823
1824             // Don't try to close the main window
1825             if (window == [[mainFrame webView] window])
1826                 continue;
1827             
1828 #if !PLATFORM(IOS)
1829             WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
1830 #else
1831             ASSERT([[window contentView] isKindOfClass:[WebView class]]);
1832             WebView *webView = (WebView *)[window contentView];
1833 #endif
1834
1835             [webView close];
1836             [window close];
1837         }
1838     }
1839
1840     // If developer extras enabled Web Inspector may have been open by the test.
1841     if (shouldEnableDeveloperExtras(pathOrURL.c_str())) {
1842         gTestRunner->closeWebInspector();
1843         gTestRunner->setDeveloperExtrasEnabled(false);
1844     }
1845
1846     resetWebViewToConsistentStateBeforeTesting();
1847
1848     // Loading an empty request synchronously replaces the document with a blank one, which is necessary
1849     // to stop timers, WebSockets and other activity that could otherwise spill output into next test's results.
1850     [mainFrame loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]]];
1851
1852     [pool release];
1853
1854     // We should only have our main window left open when we're done
1855     ASSERT(CFArrayGetCount(openWindowsRef) == 1);
1856     ASSERT(CFArrayGetValueAtIndex(openWindowsRef, 0) == [[mainFrame webView] window]);
1857
1858     gTestRunner.clear();
1859
1860     if (ignoreWebCoreNodeLeaks)
1861         [WebCoreStatistics stopIgnoringWebCoreNodeLeaks];
1862
1863     if (gcBetweenTests)
1864         [WebCoreStatistics garbageCollectJavaScriptObjects];
1865 }
1866
1867 void displayWebView()
1868 {
1869 #if !PLATFORM(IOS)
1870     WebView *webView = [mainFrame webView];
1871     [webView display];
1872     
1873     [webView setTracksRepaints:YES];
1874     [webView resetTrackedRepaints];
1875 #else
1876     // FIXME: <rdar://problem/5106253> DumpRenderTree: fix DRT and ImageDiff to re-enable pixel tests
1877     [gDrtWindow layoutTilesNow];
1878     [gDrtWindow setNeedsDisplayInRect:[gDrtWindow frame]];
1879     [CATransaction flush];
1880 #endif
1881 }
1882
1883 #if !PLATFORM(IOS)
1884 @implementation DumpRenderTreeEvent
1885
1886 + (NSPoint)mouseLocation
1887 {
1888     return [[[mainFrame webView] window] convertBaseToScreen:lastMousePosition];
1889 }
1890
1891 @end
1892
1893 @implementation DumpRenderTreeApplication
1894
1895 - (BOOL)isRunning
1896 {
1897     // <rdar://problem/7686123> Java plug-in freezes unless NSApplication is running
1898     return YES;
1899 }
1900
1901 @end
1902 #endif