8f3864ec42d9865570f3d0d952c2e915165a4ed0
[WebKit-https.git] / WebKitTools / DumpRenderTree / mac / DumpRenderTree.mm
1 /*
2  * Copyright (C) 2005, 2006, 2007 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 "DumpRenderTree.h"
31
32 #import "AccessibilityController.h"
33 #import "CheckedMalloc.h"
34 #import "DumpRenderTreePasteboard.h"
35 #import "DumpRenderTreeWindow.h"
36 #import "EditingDelegate.h"
37 #import "EventSendingController.h"
38 #import "FrameLoadDelegate.h"
39 #import "JavaScriptThreading.h"
40 #import "LayoutTestController.h"
41 #import "NavigationController.h"
42 #import "ObjCPlugin.h"
43 #import "ObjCPluginFunction.h"
44 #import "PixelDumpSupport.h"
45 #import "PolicyDelegate.h"
46 #import "ResourceLoadDelegate.h"
47 #import "UIDelegate.h"
48 #import "WorkQueue.h"
49 #import "WorkQueueItem.h"
50 #import <CoreFoundation/CoreFoundation.h>
51 #import <WebKit/DOMElementPrivate.h>
52 #import <WebKit/DOMExtensions.h>
53 #import <WebKit/DOMRange.h>
54 #import <WebKit/WebBackForwardList.h>
55 #import <WebKit/WebCache.h>
56 #import <WebKit/WebCoreStatistics.h>
57 #import <WebKit/WebDataSourcePrivate.h>
58 #import <WebKit/WebDatabaseManagerPrivate.h>
59 #import <WebKit/WebDocumentPrivate.h>
60 #import <WebKit/WebEditingDelegate.h>
61 #import <WebKit/WebFrameView.h>
62 #import <WebKit/WebHTMLRepresentationInternal.h>
63 #import <WebKit/WebHistory.h>
64 #import <WebKit/WebHistoryItemPrivate.h>
65 #import <WebKit/WebPluginDatabase.h>
66 #import <WebKit/WebPreferences.h>
67 #import <WebKit/WebPreferencesPrivate.h>
68 #import <WebKit/WebResourceLoadDelegate.h>
69 #import <WebKit/WebTypesInternal.h>
70 #import <WebKit/WebViewPrivate.h>
71 #import <getopt.h>
72 #import <mach-o/getsect.h>
73 #import <objc/objc-runtime.h>
74 #import <wtf/Assertions.h>
75 #import <wtf/RetainPtr.h>
76 #import <wtf/OwnPtr.h>
77
78 @interface DumpRenderTreeEvent : NSEvent
79 @end
80
81 static void runTest(const char *pathOrURL);
82
83 // Deciding when it's OK to dump out the state is a bit tricky.  All these must be true:
84 // - There is no load in progress
85 // - There is no work queued up (see workQueue var, below)
86 // - waitToDump==NO.  This means either waitUntilDone was never called, or it was called
87 //       and notifyDone was called subsequently.
88 // Note that the call to notifyDone and the end of the load can happen in either order.
89
90 volatile bool done;
91
92 NavigationController* gNavigationController = 0;
93 LayoutTestController* gLayoutTestController = 0;
94
95 WebFrame *mainFrame = 0;
96 // This is the topmost frame that is loading, during a given load, or nil when no load is 
97 // in progress.  Usually this is the same as the main frame, but not always.  In the case
98 // where a frameset is loaded, and then new content is loaded into one of the child frames,
99 // that child frame is the "topmost frame that is loading".
100 WebFrame *topLoadingFrame = nil;     // !nil iff a load is in progress
101
102
103 CFMutableSetRef disallowedURLs = 0;
104 CFRunLoopTimerRef waitToDumpWatchdog = 0;
105
106 // Delegates
107 static FrameLoadDelegate *frameLoadDelegate;
108 static UIDelegate *uiDelegate;
109 static EditingDelegate *editingDelegate;
110 static ResourceLoadDelegate *resourceLoadDelegate;
111 PolicyDelegate *policyDelegate;
112
113 static int dumpPixels;
114 static int dumpAllPixels;
115 static int threaded;
116 static int testRepaintDefault;
117 static int repaintSweepHorizontallyDefault;
118 static int dumpTree = YES;
119 static int forceComplexText;
120 static BOOL printSeparators;
121 static NSString *currentTest = nil;
122 static RetainPtr<CFStringRef> persistentUserStyleSheetLocation;
123
124 static WebHistoryItem *prevTestBFItem = nil;  // current b/f item at the end of the previous test
125
126 const unsigned maxViewHeight = 600;
127 const unsigned maxViewWidth = 800;
128
129 #if __OBJC2__
130 static void swizzleAllMethods(Class imposter, Class original)
131 {
132     unsigned int imposterMethodCount;
133     Method* imposterMethods = class_copyMethodList(imposter, &imposterMethodCount);
134
135     unsigned int originalMethodCount;
136     Method* originalMethods = class_copyMethodList(original, &originalMethodCount);
137
138     for (unsigned int i = 0; i < imposterMethodCount; i++) {
139         SEL imposterMethodName = method_getName(imposterMethods[i]);
140
141         // Attempt to add the method to the original class.  If it fails, the method already exists and we should
142         // instead exchange the implementations.
143         if (class_addMethod(original, imposterMethodName, method_getImplementation(originalMethods[i]), method_getTypeEncoding(originalMethods[i])))
144             continue;
145
146         unsigned int j = 0;
147         for (; j < originalMethodCount; j++) {
148             SEL originalMethodName = method_getName(originalMethods[j]);
149             if (sel_isEqual(imposterMethodName, originalMethodName))
150                 break;
151         }
152
153         // If class_addMethod failed above then the method must exist on the original class.
154         ASSERT(j < originalMethodCount);
155         method_exchangeImplementations(imposterMethods[i], originalMethods[j]);
156     }
157
158     free(imposterMethods);
159     free(originalMethods);
160 }
161 #endif
162
163 static void poseAsClass(const char* imposter, const char* original)
164 {
165     Class imposterClass = objc_getClass(imposter);
166     Class originalClass = objc_getClass(original);
167
168 #if !__OBJC2__
169     class_poseAs(imposterClass, originalClass);
170 #else
171
172     // Swizzle instance methods
173     swizzleAllMethods(imposterClass, originalClass);
174     // and then class methods
175     swizzleAllMethods(object_getClass(imposterClass), object_getClass(originalClass));
176 #endif
177 }
178
179 void setPersistentUserStyleSheetLocation(CFStringRef url)
180 {
181     persistentUserStyleSheetLocation = url;
182 }
183
184 static BOOL shouldIgnoreWebCoreNodeLeaks(CFStringRef URLString)
185 {
186     static CFStringRef const ignoreSet[] = {
187         // Keeping this infrastructure around in case we ever need it again.
188     };
189     static const int ignoreSetCount = sizeof(ignoreSet) / sizeof(CFStringRef);
190     
191     for (int i = 0; i < ignoreSetCount; i++) {
192         CFStringRef ignoreString = ignoreSet[i];
193         CFRange range = CFRangeMake(0, CFStringGetLength(URLString));
194         CFOptionFlags flags = kCFCompareAnchored | kCFCompareBackwards | kCFCompareCaseInsensitive;
195         if (CFStringFindWithOptions(URLString, ignoreString, range, flags, NULL))
196             return YES;
197     }
198     return NO;
199 }
200
201 static void activateFonts()
202 {
203     static const char* fontSectionNames[] = {
204         "Ahem",
205         "WeightWatcher100",
206         "WeightWatcher200",
207         "WeightWatcher300",
208         "WeightWatcher400",
209         "WeightWatcher500",
210         "WeightWatcher600",
211         "WeightWatcher700",
212         "WeightWatcher800",
213         "WeightWatcher900",
214         0
215     };
216
217     for (unsigned i = 0; fontSectionNames[i]; ++i) {
218         unsigned long fontDataLength;
219         char* fontData = getsectdata("__DATA", fontSectionNames[i], &fontDataLength);
220         if (!fontData) {
221             fprintf(stderr, "Failed to locate the %s font.\n", fontSectionNames[i]);
222             exit(1);
223         }
224
225         ATSFontContainerRef fontContainer;
226         OSStatus status = ATSFontActivateFromMemory(fontData, fontDataLength, kATSFontContextLocal, kATSFontFormatUnspecified, NULL, kATSOptionFlagsDefault, &fontContainer);
227
228         if (status != noErr) {
229             fprintf(stderr, "Failed to activate the %s font.\n", fontSectionNames[i]);
230             exit(1);
231         }
232     }
233 }
234
235 WebView *createWebViewAndOffscreenWindow()
236 {
237     NSRect rect = NSMakeRect(0, 0, maxViewWidth, maxViewHeight);
238     WebView *webView = [[WebView alloc] initWithFrame:rect frameName:nil groupName:@"org.webkit.DumpRenderTree"];
239         
240     [webView setUIDelegate:uiDelegate];
241     [webView setFrameLoadDelegate:frameLoadDelegate];
242     [webView setEditingDelegate:editingDelegate];
243     [webView setResourceLoadDelegate:resourceLoadDelegate];
244
245     // Register the same schemes that Safari does
246     [WebView registerURLSchemeAsLocal:@"feed"];
247     [WebView registerURLSchemeAsLocal:@"feeds"];
248     [WebView registerURLSchemeAsLocal:@"feedsearch"];
249     
250     [webView setContinuousSpellCheckingEnabled:YES];
251     
252     // 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.
253     // Put it at -10000, -10000 in "flipped coordinates", since WebCore and the DOM use flipped coordinates.
254     NSRect windowRect = NSOffsetRect(rect, -10000, [[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
255     DumpRenderTreeWindow *window = [[DumpRenderTreeWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
256     [[window contentView] addSubview:webView];
257     [window orderBack:nil];
258     [window setAutodisplay:NO];
259     
260     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
261     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
262     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
263     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
264         
265     return webView;
266 }
267
268 void testStringByEvaluatingJavaScriptFromString()
269 {
270     // maps expected result <= JavaScript expression
271     NSDictionary *expressions = [NSDictionary dictionaryWithObjectsAndKeys:
272         @"0", @"0", 
273         @"0", @"'0'", 
274         @"", @"",
275         @"", @"''", 
276         @"", @"new String()", 
277         @"", @"new String('0')", 
278         @"", @"throw 1", 
279         @"", @"{ }", 
280         @"", @"[ ]", 
281         @"", @"//", 
282         @"", @"a.b.c", 
283         @"", @"(function() { throw 'error'; })()", 
284         @"", @"null",
285         @"", @"undefined",
286         @"true", @"true",
287         @"false", @"false",
288         nil
289     ];
290
291     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
292     WebView *webView = [[WebView alloc] initWithFrame:NSZeroRect frameName:@"" groupName:@""];
293
294     NSEnumerator *enumerator = [expressions keyEnumerator];
295     id expression;
296     while ((expression = [enumerator nextObject])) {
297         NSString *expectedResult = [expressions objectForKey:expression];
298         NSString *result = [webView stringByEvaluatingJavaScriptFromString:expression];
299         assert([result isEqualToString:expectedResult]);
300     }
301
302     [webView close];
303     [webView release];
304     [pool release];
305 }
306
307 static void setDefaultsToConsistentValuesForTesting()
308 {
309     // Give some clear to undocumented defaults values
310     static const int MediumFontSmoothing = 2;
311     static const int BlueTintedAppearance = 1;
312
313     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
314     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
315     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"]; // smallest font size to CG should perform antialiasing on
316     [defaults setInteger:MediumFontSmoothing forKey:@"AppleFontSmoothing"];
317     [defaults setInteger:BlueTintedAppearance forKey:@"AppleAquaColorVariant"];
318     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
319     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
320     [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
321
322     NSString *libraryPath = [@"~/Library/Application Support/DumpRenderTree" stringByExpandingTildeInPath];
323     [defaults setObject:[libraryPath stringByAppendingPathComponent:@"Databases"] forKey:WebDatabaseDirectoryDefaultsKey];
324     
325     WebPreferences *preferences = [WebPreferences standardPreferences];
326
327     [preferences setStandardFontFamily:@"Times"];
328     [preferences setFixedFontFamily:@"Courier"];
329     [preferences setSerifFontFamily:@"Times"];
330     [preferences setSansSerifFontFamily:@"Helvetica"];
331     [preferences setCursiveFontFamily:@"Apple Chancery"];
332     [preferences setFantasyFontFamily:@"Papyrus"];
333     [preferences setDefaultFontSize:16];
334     [preferences setDefaultFixedFontSize:13];
335     [preferences setMinimumFontSize:1];
336     [preferences setJavaEnabled:NO];
337     [preferences setEditableLinkBehavior:WebKitEditableLinkOnlyLiveWithShiftKey];
338     [preferences setTabsToLinks:NO];
339     [preferences setDOMPasteAllowed:YES];
340     [preferences setFullDocumentTeardownEnabled:YES];
341     [preferences setShouldPrintBackgrounds:YES];
342
343     // The back/forward cache is causing problems due to layouts during transition from one page to another.
344     // So, turn it off for now, but we might want to turn it back on some day.
345     [preferences setUsesPageCache:NO];
346 }
347
348 static void crashHandler(int sig)
349 {
350     char *signalName = strsignal(sig);
351     write(STDERR_FILENO, signalName, strlen(signalName));
352     write(STDERR_FILENO, "\n", 1);
353     restoreColorSpace(0);
354     exit(128 + sig);
355 }
356
357 static void installSignalHandlers()
358 {
359     signal(SIGILL, crashHandler);    /* 4:   illegal instruction (not reset when caught) */
360     signal(SIGTRAP, crashHandler);   /* 5:   trace trap (not reset when caught) */
361     signal(SIGEMT, crashHandler);    /* 7:   EMT instruction */
362     signal(SIGFPE, crashHandler);    /* 8:   floating point exception */
363     signal(SIGBUS, crashHandler);    /* 10:  bus error */
364     signal(SIGSEGV, crashHandler);   /* 11:  segmentation violation */
365     signal(SIGSYS, crashHandler);    /* 12:  bad argument to system call */
366     signal(SIGPIPE, crashHandler);   /* 13:  write on a pipe with no reader */
367     signal(SIGXCPU, crashHandler);   /* 24:  exceeded CPU time limit */
368     signal(SIGXFSZ, crashHandler);   /* 25:  exceeded file size limit */
369 }
370
371 static void allocateGlobalControllers()
372 {
373     // FIXME: We should remove these and move to the ObjC standard [Foo sharedInstance] model
374     gNavigationController = [[NavigationController alloc] init];
375     frameLoadDelegate = [[FrameLoadDelegate alloc] init];
376     uiDelegate = [[UIDelegate alloc] init];
377     editingDelegate = [[EditingDelegate alloc] init];
378     resourceLoadDelegate = [[ResourceLoadDelegate alloc] init];
379     policyDelegate = [[PolicyDelegate alloc] init];
380 }
381
382 // ObjC++ doens't seem to let me pass NSObject*& sadly.
383 static inline void releaseAndZero(NSObject** object)
384 {
385     [*object release];
386     *object = nil;
387 }
388
389 static void releaseGlobalControllers()
390 {
391     releaseAndZero(&gNavigationController);
392     releaseAndZero(&frameLoadDelegate);
393     releaseAndZero(&editingDelegate);
394     releaseAndZero(&resourceLoadDelegate);
395     releaseAndZero(&uiDelegate);
396     releaseAndZero(&policyDelegate);
397 }
398
399 static void initializeGlobalsFromCommandLineOptions(int argc, const char *argv[])
400 {
401     struct option options[] = {
402         {"dump-all-pixels", no_argument, &dumpAllPixels, YES},
403         {"horizontal-sweep", no_argument, &repaintSweepHorizontallyDefault, YES},
404         {"notree", no_argument, &dumpTree, NO},
405         {"pixel-tests", no_argument, &dumpPixels, YES},
406         {"repaint", no_argument, &testRepaintDefault, YES},
407         {"tree", no_argument, &dumpTree, YES},
408         {"threaded", no_argument, &threaded, YES},
409         {"complex-text", no_argument, &forceComplexText, YES},
410         {NULL, 0, NULL, 0}
411     };
412     
413     int option;
414     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1) {
415         switch (option) {
416             case '?':   // unknown or ambiguous option
417             case ':':   // missing argument
418                 exit(1);
419                 break;
420         }
421     }
422 }
423
424 static void addTestPluginsToPluginSearchPath(const char* executablePath)
425 {
426     NSString *pwd = [[NSString stringWithUTF8String:executablePath] stringByDeletingLastPathComponent];
427     [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
428     [[WebPluginDatabase sharedDatabase] refresh];
429 }
430
431 static bool useLongRunningServerMode(int argc, const char *argv[])
432 {
433     // This assumes you've already called getopt_long
434     return (argc == optind+1 && strcmp(argv[optind], "-") == 0);
435 }
436
437 static void runTestingServerLoop()
438 {
439     // When DumpRenderTree run in server mode, we just wait around for file names
440     // to be passed to us and read each in turn, passing the results back to the client
441     char filenameBuffer[2048];
442     while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
443         char *newLineCharacter = strchr(filenameBuffer, '\n');
444         if (newLineCharacter)
445             *newLineCharacter = '\0';
446
447         if (strlen(filenameBuffer) == 0)
448             continue;
449
450         runTest(filenameBuffer);
451     }
452 }
453
454 static void prepareConsistentTestingEnvironment()
455 {
456     poseAsClass("DumpRenderTreePasteboard", "NSPasteboard");
457     poseAsClass("DumpRenderTreeEvent", "NSEvent");
458
459     setDefaultsToConsistentValuesForTesting();
460     activateFonts();
461     
462     if (dumpPixels)
463         initializeColorSpaceAndScreeBufferForPixelTests();
464     allocateGlobalControllers();
465     
466     makeLargeMallocFailSilently();
467 }
468
469 void dumpRenderTree(int argc, const char *argv[])
470 {
471     initializeGlobalsFromCommandLineOptions(argc, argv);
472     prepareConsistentTestingEnvironment();
473     addTestPluginsToPluginSearchPath(argv[0]);
474     if (dumpPixels)
475         installSignalHandlers();
476
477     if (forceComplexText)
478         [WebView _setAlwaysUseATSU:YES];
479
480     WebView *webView = createWebViewAndOffscreenWindow();
481     mainFrame = [webView mainFrame];
482
483     [[NSURLCache sharedURLCache] removeAllCachedResponses];
484
485     [WebCache empty];
486      
487     // <rdar://problem/5222911>
488     testStringByEvaluatingJavaScriptFromString();
489
490     if (threaded)
491         startJavaScriptThreads();
492
493     if (useLongRunningServerMode(argc, argv)) {
494         printSeparators = YES;
495         runTestingServerLoop();
496     } else {
497         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
498         for (int i = optind; i != argc; ++i)
499             runTest(argv[i]);
500     }
501
502     if (threaded)
503         stopJavaScriptThreads();
504
505     [WebCoreStatistics emptyCache]; // Otherwise SVGImages trigger false positives for Frame/Node counts    
506     [webView close];
507     mainFrame = nil;
508
509     // Work around problem where registering drag types leaves an outstanding
510     // "perform selector" on the window, which retains the window. It's a bit
511     // inelegant and perhaps dangerous to just blow them all away, but in practice
512     // it probably won't cause any trouble (and this is just a test tool, after all).
513     NSWindow *window = [webView window];
514     [NSObject cancelPreviousPerformRequestsWithTarget:window];
515     
516     [window close]; // releases when closed
517     [webView release];
518     
519     releaseGlobalControllers();
520     
521     [DumpRenderTreePasteboard releaseLocalPasteboards];
522
523     // FIXME: This should be moved onto LayoutTestController and made into a HashSet
524     if (disallowedURLs) {
525         CFRelease(disallowedURLs);
526         disallowedURLs = 0;
527     }
528
529     if (dumpPixels)
530         restoreColorSpace(0);
531 }
532
533 int main(int argc, const char *argv[])
534 {
535     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
536     [NSApplication sharedApplication]; // Force AppKit to init itself
537     dumpRenderTree(argc, argv);
538     [WebCoreStatistics garbageCollectJavaScriptObjects];
539     [pool release];
540     return 0;
541 }
542
543 static NSInteger compareHistoryItems(id item1, id item2, void *context)
544 {
545     return [[item1 target] caseInsensitiveCompare:[item2 target]];
546 }
547
548 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
549 {
550     int start = 0;
551     if (current) {
552         printf("curr->");
553         start = 6;
554     }
555     for (int i = start; i < indent; i++)
556         putchar(' ');
557     printf("%s", [[item URLString] UTF8String]);
558     NSString *target = [item target];
559     if (target && [target length] > 0)
560         printf(" (in frame \"%s\")", [target UTF8String]);
561     if ([item isTargetItem])
562         printf("  **nav target**");
563     putchar('\n');
564     NSArray *kids = [item children];
565     if (kids) {
566         // must sort to eliminate arbitrary result ordering which defeats reproducible testing
567         kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
568         for (unsigned i = 0; i < [kids count]; i++)
569             dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
570     }
571 }
572
573 static void dumpFrameScrollPosition(WebFrame *f)
574 {
575     NSPoint scrollPosition = [[[[f frameView] documentView] superview] bounds].origin;
576     if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
577         if ([f parentFrame] != nil)
578             printf("frame '%s' ", [[f name] UTF8String]);
579         printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
580     }
581
582     if (gLayoutTestController->dumpChildFrameScrollPositions()) {
583         NSArray *kids = [f childFrames];
584         if (kids)
585             for (unsigned i = 0; i < [kids count]; i++)
586                 dumpFrameScrollPosition([kids objectAtIndex:i]);
587     }
588 }
589
590 static NSString *dumpFramesAsText(WebFrame *frame)
591 {
592     DOMDocument *document = [frame DOMDocument];
593     DOMElement *documentElement = [document documentElement];
594
595     if (!documentElement)
596         return @"";
597
598     NSMutableString *result = [[[NSMutableString alloc] init] autorelease];
599
600     // Add header for all but the main frame.
601     if ([frame parentFrame])
602         result = [NSMutableString stringWithFormat:@"\n--------\nFrame: '%@'\n--------\n", [frame name]];
603
604     [result appendFormat:@"%@\n", [documentElement innerText]];
605
606     if (gLayoutTestController->dumpChildFramesAsText()) {
607         NSArray *kids = [frame childFrames];
608         if (kids) {
609             for (unsigned i = 0; i < [kids count]; i++)
610                 [result appendString:dumpFramesAsText([kids objectAtIndex:i])];
611         }
612     }
613
614     return result;
615 }
616
617 static NSData *dumpFrameAsPDF(WebFrame *frame)
618 {
619     if (!frame)
620         return nil;
621
622     // Sadly we have to dump to a file and then read from that file again
623     // +[NSPrintOperation PDFOperationWithView:insideRect:] requires a rect and prints to a single page
624     // likewise +[NSView dataWithPDFInsideRect:] also prints to a single continuous page
625     // The goal of this function is to test "real" printing across multiple pages.
626     // FIXME: It's possible there might be printing SPI to let us print a multi-page PDF to an NSData object
627     NSString *path = @"/tmp/test.pdf";
628
629     NSMutableDictionary *printInfoDict = [NSMutableDictionary dictionaryWithDictionary:[[NSPrintInfo sharedPrintInfo] dictionary]];
630     [printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
631     [printInfoDict setObject:path forKey:NSPrintSavePath];
632
633     NSPrintInfo *printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
634     [printInfo setHorizontalPagination:NSAutoPagination];
635     [printInfo setVerticalPagination:NSAutoPagination];
636     [printInfo setVerticallyCentered:NO];
637
638     NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:[frame frameView] printInfo:printInfo];
639     [printOperation setShowPanels:NO];
640     [printOperation runOperation];
641
642     [printInfo release];
643
644     NSData *pdfData = [NSData dataWithContentsOfFile:path];
645     [[NSFileManager defaultManager] removeFileAtPath:path handler:nil];
646
647     return pdfData;
648 }
649
650 static void convertMIMEType(NSMutableString *mimeType)
651 {
652 #ifdef BUILDING_ON_LEOPARD
653     // Workaround for <rdar://problem/5539824> on Leopard
654     if ([mimeType isEqualToString:@"text/xml"])
655         [mimeType setString:@"application/xml"];
656 #endif
657 }
658
659 static void convertWebResourceDataToString(NSMutableDictionary *resource)
660 {
661     NSMutableString *mimeType = [resource objectForKey:@"WebResourceMIMEType"];
662     convertMIMEType(mimeType);
663     
664     if ([mimeType hasPrefix:@"text/"] || [[WebHTMLRepresentation supportedNonImageMIMETypes] containsObject:mimeType]) {
665         NSData *data = [resource objectForKey:@"WebResourceData"];
666         NSString *dataAsString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
667         [resource setObject:dataAsString forKey:@"WebResourceData"];
668     }
669 }
670
671 static void normalizeWebResourceURL(NSMutableString *webResourceURL)
672 {
673     static int fileUrlLength = [(NSString *)@"file://" length];
674     NSRange layoutTestsWebArchivePathRange = [webResourceURL rangeOfString:@"/LayoutTests/" options:NSBackwardsSearch];
675     if (layoutTestsWebArchivePathRange.location == NSNotFound)
676         return;
677     NSRange currentWorkingDirectoryRange = NSMakeRange(fileUrlLength, layoutTestsWebArchivePathRange.location - fileUrlLength);
678     [webResourceURL replaceCharactersInRange:currentWorkingDirectoryRange withString:@""];
679 }
680
681 static void convertWebResourceResponseToDictionary(NSMutableDictionary *propertyList)
682 {
683     NSURLResponse *response = nil;
684     NSData *responseData = [propertyList objectForKey:@"WebResourceResponse"]; // WebResourceResponseKey in WebResource.m
685     if ([responseData isKindOfClass:[NSData class]]) {
686         // Decode NSURLResponse
687         NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:responseData];
688         response = [unarchiver decodeObjectForKey:@"WebResourceResponse"]; // WebResourceResponseKey in WebResource.m
689         [unarchiver finishDecoding];
690         [unarchiver release];
691     }        
692     
693     NSMutableDictionary *responseDictionary = [[NSMutableDictionary alloc] init];
694     
695     NSMutableString *urlString = [[[response URL] description] mutableCopy];
696     normalizeWebResourceURL(urlString);
697     [responseDictionary setObject:urlString forKey:@"URL"];
698     [urlString release];
699     
700     NSMutableString *mimeTypeString = [[response MIMEType] mutableCopy];
701     convertMIMEType(mimeTypeString);
702     [responseDictionary setObject:mimeTypeString forKey:@"MIMEType"];
703     [mimeTypeString release];
704
705     NSString *textEncodingName = [response textEncodingName];
706     if (textEncodingName)
707         [responseDictionary setObject:textEncodingName forKey:@"textEncodingName"];
708     [responseDictionary setObject:[NSNumber numberWithLongLong:[response expectedContentLength]] forKey:@"expectedContentLength"];
709     
710     if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
711         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
712         
713         [responseDictionary setObject:[httpResponse allHeaderFields] forKey:@"allHeaderFields"];
714         [responseDictionary setObject:[NSNumber numberWithInt:[httpResponse statusCode]] forKey:@"statusCode"];
715     }
716     
717     [propertyList setObject:responseDictionary forKey:@"WebResourceResponse"];
718     [responseDictionary release];
719 }
720
721 static NSInteger compareResourceURLs(id resource1, id resource2, void *context)
722 {
723     NSString *url1 = [resource1 objectForKey:@"WebResourceURL"];
724     NSString *url2 = [resource2 objectForKey:@"WebResourceURL"];
725  
726     return [url1 compare:url2];
727 }
728
729 static NSString *serializeWebArchiveToXML(WebArchive *webArchive)
730 {
731     NSString *errorString;
732     NSMutableDictionary *propertyList = [NSPropertyListSerialization propertyListFromData:[webArchive data]
733                                                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
734                                                                                    format:NULL
735                                                                          errorDescription:&errorString];
736     if (!propertyList)
737         return errorString;
738
739     NSMutableArray *resources = [NSMutableArray arrayWithCapacity:1];
740     [resources addObject:propertyList];
741
742     while ([resources count]) {
743         NSMutableDictionary *resourcePropertyList = [resources objectAtIndex:0];
744         [resources removeObjectAtIndex:0];
745
746         NSMutableDictionary *mainResource = [resourcePropertyList objectForKey:@"WebMainResource"];
747         normalizeWebResourceURL([mainResource objectForKey:@"WebResourceURL"]);
748         convertWebResourceDataToString(mainResource);
749
750         // Add subframeArchives to list for processing
751         NSMutableArray *subframeArchives = [resourcePropertyList objectForKey:@"WebSubframeArchives"]; // WebSubframeArchivesKey in WebArchive.m
752         if (subframeArchives)
753             [resources addObjectsFromArray:subframeArchives];
754
755         NSMutableArray *subresources = [resourcePropertyList objectForKey:@"WebSubresources"]; // WebSubresourcesKey in WebArchive.m
756         NSEnumerator *enumerator = [subresources objectEnumerator];
757         NSMutableDictionary *subresourcePropertyList;
758         while ((subresourcePropertyList = [enumerator nextObject])) {
759             normalizeWebResourceURL([subresourcePropertyList objectForKey:@"WebResourceURL"]);
760             convertWebResourceResponseToDictionary(subresourcePropertyList);
761             convertWebResourceDataToString(subresourcePropertyList);
762         }
763         
764         // Sort the subresources so they're always in a predictable order for the dump
765         if (NSArray *sortedSubresources = [subresources sortedArrayUsingFunction:compareResourceURLs context:nil])
766             [resourcePropertyList setObject:sortedSubresources forKey:@"WebSubresources"];
767     }
768
769     NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:propertyList
770                                                                  format:NSPropertyListXMLFormat_v1_0
771                                                        errorDescription:&errorString];
772     if (!xmlData)
773         return errorString;
774
775     NSMutableString *string = [[[NSMutableString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
776
777     // Replace "Apple Computer" with "Apple" in the DTD declaration.
778     NSRange range = [string rangeOfString:@"-//Apple Computer//"];
779     if (range.location != NSNotFound)
780         [string replaceCharactersInRange:range withString:@"-//Apple//"];
781     
782     return string;
783 }
784
785 static void dumpBackForwardListForWebView(WebView *view)
786 {
787     printf("\n============== Back Forward List ==============\n");
788     WebBackForwardList *bfList = [view backForwardList];
789
790     // Print out all items in the list after prevTestBFItem, which was from the previous test
791     // Gather items from the end of the list, the print them out from oldest to newest
792     NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
793     for (int i = [bfList forwardListCount]; i > 0; i--) {
794         WebHistoryItem *item = [bfList itemAtIndex:i];
795         // something is wrong if the item from the last test is in the forward part of the b/f list
796         assert(item != prevTestBFItem);
797         [itemsToPrint addObject:item];
798     }
799             
800     assert([bfList currentItem] != prevTestBFItem);
801     [itemsToPrint addObject:[bfList currentItem]];
802     int currentItemIndex = [itemsToPrint count] - 1;
803
804     for (int i = -1; i >= -[bfList backListCount]; i--) {
805         WebHistoryItem *item = [bfList itemAtIndex:i];
806         if (item == prevTestBFItem)
807             break;
808         [itemsToPrint addObject:item];
809     }
810
811     for (int i = [itemsToPrint count]-1; i >= 0; i--)
812         dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
813
814     [itemsToPrint release];
815     printf("===============================================\n");
816 }
817
818 static void sizeWebViewForCurrentTest()
819 {
820     // W3C SVG tests expect to be 480x360
821     bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
822     if (isSVGW3CTest)
823         [[mainFrame webView] setFrameSize:NSMakeSize(480, 360)];
824     else
825         [[mainFrame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
826 }
827
828 static const char *methodNameStringForFailedTest()
829 {
830     const char *errorMessage;
831     if (gLayoutTestController->dumpAsText())
832         errorMessage = "[documentElement innerText]";
833     else if (gLayoutTestController->dumpDOMAsWebArchive())
834         errorMessage = "[[mainFrame DOMDocument] webArchive]";
835     else if (gLayoutTestController->dumpSourceAsWebArchive())
836         errorMessage = "[[mainFrame dataSource] webArchive]";
837     else
838         errorMessage = "[mainFrame renderTreeAsExternalRepresentation]";
839
840     return errorMessage;
841 }
842
843 static void dumpBackForwardListForAllWindows()
844 {
845     CFArrayRef openWindows = (CFArrayRef)[DumpRenderTreeWindow openWindows];
846     unsigned count = CFArrayGetCount(openWindows);
847     for (unsigned i = 0; i < count; i++) {
848         NSWindow *window = (NSWindow *)CFArrayGetValueAtIndex(openWindows, i);
849         WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
850         dumpBackForwardListForWebView(webView);
851     }
852 }
853
854 static void invalidateAnyPreviousWaitToDumpWatchdog()
855 {
856     if (waitToDumpWatchdog) {
857         CFRunLoopTimerInvalidate(waitToDumpWatchdog);
858         CFRelease(waitToDumpWatchdog);
859         waitToDumpWatchdog = 0;
860     }
861 }
862
863 void dump()
864 {
865     invalidateAnyPreviousWaitToDumpWatchdog();
866
867     if (dumpTree) {
868         NSString *resultString = nil;
869         NSData *resultData = nil;
870         NSString *resultMimeType = @"text/plain";
871
872         bool dumpAsText = gLayoutTestController->dumpAsText();
873         dumpAsText |= [[[mainFrame dataSource] _responseMIMEType] isEqualToString:@"text/plain"];
874         gLayoutTestController->setDumpAsText(dumpAsText);
875         if (gLayoutTestController->dumpAsText()) {
876             resultString = dumpFramesAsText(mainFrame);
877         } else if (gLayoutTestController->dumpAsPDF()) {
878             resultData = dumpFrameAsPDF(mainFrame);
879             resultMimeType = @"application/pdf";
880         } else if (gLayoutTestController->dumpDOMAsWebArchive()) {
881             WebArchive *webArchive = [[mainFrame DOMDocument] webArchive];
882             resultString = serializeWebArchiveToXML(webArchive);
883             resultMimeType = @"application/x-webarchive";
884         } else if (gLayoutTestController->dumpSourceAsWebArchive()) {
885             WebArchive *webArchive = [[mainFrame dataSource] webArchive];
886             resultString = serializeWebArchiveToXML(webArchive);
887             resultMimeType = @"application/x-webarchive";
888         } else {
889             sizeWebViewForCurrentTest();
890             resultString = [mainFrame renderTreeAsExternalRepresentation];
891         }
892
893         if (resultString && !resultData)
894             resultData = [resultString dataUsingEncoding:NSUTF8StringEncoding];
895
896         printf("Content-Type: %s\n", [resultMimeType UTF8String]);
897
898         if (resultData) {
899             fwrite([resultData bytes], 1, [resultData length], stdout);
900
901             if (!gLayoutTestController->dumpAsText() && !gLayoutTestController->dumpDOMAsWebArchive() && !gLayoutTestController->dumpSourceAsWebArchive())
902                 dumpFrameScrollPosition(mainFrame);
903
904             if (gLayoutTestController->dumpBackForwardList())
905                 dumpBackForwardListForAllWindows();
906         } else
907             printf("ERROR: nil result from %s", methodNameStringForFailedTest());
908
909         if (printSeparators) {
910             puts("#EOF");
911             fputs("#EOF\n", stderr);
912         }            
913     }
914     
915     if (dumpPixels)
916         dumpWebViewAsPixelsAndCompareWithExpected([currentTest UTF8String], dumpAllPixels);
917
918     fflush(stdout);
919     fflush(stderr);
920
921     done = YES;
922 }
923
924 static bool shouldLogFrameLoadDelegates(const char *pathOrURL)
925 {
926     return strstr(pathOrURL, "loading/");
927 }
928
929 static CFURLRef createCFURLFromPathOrURL(CFStringRef pathOrURLString)
930 {
931     CFURLRef URL;
932     if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")) || CFStringHasPrefix(pathOrURLString, CFSTR("https://")))
933         URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
934     else
935         URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
936     return URL;
937 }
938
939 static void resetWebViewToConsistentStateBeforeTesting()
940 {
941     WebView *webView = [mainFrame webView];
942     [(EditingDelegate *)[webView editingDelegate] setAcceptsEditing:YES];
943     [webView makeTextStandardSize:nil];
944     [webView resetPageZoom:nil];
945     [webView setTabKeyCyclesThroughElements:YES];
946     [webView setPolicyDelegate:nil];
947     [webView _setDashboardBehavior:WebDashboardBehaviorUseBackwardCompatibilityMode to:NO];
948
949     WebPreferences *preferences = [webView preferences];
950     [preferences setPrivateBrowsingEnabled:NO];
951     [preferences setAuthorAndUserStylesEnabled:YES];
952     [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
953     [preferences setOfflineWebApplicationCacheEnabled:YES];
954     [preferences setFullDocumentTeardownEnabled:YES];
955     
956     if (persistentUserStyleSheetLocation) {
957         [preferences setUserStyleSheetLocation:[NSURL URLWithString:(NSString *)(persistentUserStyleSheetLocation.get())]];
958         [preferences setUserStyleSheetEnabled:YES];
959     } else
960         [preferences setUserStyleSheetEnabled:NO];
961
962     [WebView _setUsesTestModeFocusRingColor:YES];
963 }
964
965 static void runTest(const char *pathOrURL)
966 {
967     CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
968     if (!pathOrURLString) {
969         fprintf(stderr, "Failed to parse filename as UTF-8: %s\n", pathOrURL);
970         return;
971     }
972
973     CFURLRef URL = createCFURLFromPathOrURL(pathOrURLString);
974     if (!URL) {
975         CFRelease(pathOrURLString);
976         fprintf(stderr, "Can't turn %s into a CFURL\n", pathOrURL);
977         return;
978     }
979
980     resetWebViewToConsistentStateBeforeTesting();
981
982     gLayoutTestController = new LayoutTestController(testRepaintDefault, repaintSweepHorizontallyDefault);
983     topLoadingFrame = nil;
984     done = NO;
985
986     if (disallowedURLs)
987         CFSetRemoveAllValues(disallowedURLs);
988     if (shouldLogFrameLoadDelegates(pathOrURL))
989         gLayoutTestController->setDumpFrameLoadCallbacks(true);
990
991     if ([WebHistory optionalSharedHistory])
992         [WebHistory setOptionalSharedHistory:nil];
993     lastMousePosition = NSZeroPoint;
994     lastClickPosition = NSZeroPoint;
995
996     if (currentTest != nil)
997         CFRelease(currentTest);
998     currentTest = (NSString *)pathOrURLString;
999     [prevTestBFItem release];
1000     prevTestBFItem = [[[[mainFrame webView] backForwardList] currentItem] retain];
1001
1002     WorkQueue::shared()->clear();
1003     WorkQueue::shared()->setFrozen(false);
1004
1005     BOOL _shouldIgnoreWebCoreNodeLeaks = shouldIgnoreWebCoreNodeLeaks(CFURLGetString(URL));
1006     if (_shouldIgnoreWebCoreNodeLeaks)
1007         [WebCoreStatistics startIgnoringWebCoreNodeLeaks];
1008
1009     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1010     [mainFrame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
1011     CFRelease(URL);
1012     [pool release];
1013     while (!done) {
1014         pool = [[NSAutoreleasePool alloc] init];
1015         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1016         [pool release];
1017     }
1018     pool = [[NSAutoreleasePool alloc] init];
1019     [EventSendingController clearSavedEvents];
1020     [[mainFrame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1021
1022     WorkQueue::shared()->clear();
1023
1024     if (gLayoutTestController->closeRemainingWindowsWhenComplete()) {
1025         NSArray* array = [DumpRenderTreeWindow openWindows];
1026
1027         unsigned count = [array count];
1028         for (unsigned i = 0; i < count; i++) {
1029             NSWindow *window = [array objectAtIndex:i];
1030
1031             // Don't try to close the main window
1032             if (window == [[mainFrame webView] window])
1033                 continue;
1034             
1035             WebView *webView = [[[window contentView] subviews] objectAtIndex:0];
1036
1037             [webView close];
1038             [window close];
1039         }
1040     }
1041     
1042     [mainFrame loadHTMLString:@"<html></html>" baseURL:[NSURL URLWithString:@"about:blank"]];
1043     [mainFrame stopLoading];
1044     
1045     [pool release];
1046
1047     // We should only have our main window left open when we're done
1048     ASSERT(CFArrayGetCount(openWindowsRef) == 1);
1049     ASSERT(CFArrayGetValueAtIndex(openWindowsRef, 0) == [[mainFrame webView] window]);
1050
1051     delete gLayoutTestController;
1052     gLayoutTestController = 0;
1053
1054     if (_shouldIgnoreWebCoreNodeLeaks)
1055         [WebCoreStatistics stopIgnoringWebCoreNodeLeaks];
1056 }
1057
1058 void displayWebView()
1059 {
1060     NSView *webView = [mainFrame webView];
1061     [webView display];
1062     [webView lockFocus];
1063     [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
1064     NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
1065     [webView unlockFocus];
1066 }
1067
1068 @implementation DumpRenderTreeEvent
1069
1070 + (NSPoint)mouseLocation
1071 {
1072     return [[[mainFrame webView] window] convertBaseToScreen:lastMousePosition];
1073 }
1074
1075 @end