0802047746b9e5191be31b79baae26f07ffe9cb2
[WebKit-https.git] / WebKitTools / DumpRenderTree / DumpRenderTree.m
1 /*
2  * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28  
29 #import "DumpRenderTree.h"
30
31 #import <WebKit/DOMExtensions.h>
32 #import <WebKit/DOMRange.h>
33 #import <WebKit/WebBackForwardList.h>
34 #import <WebKit/WebCoreStatistics.h>
35 #import <WebKit/WebDataSource.h>
36 #import <WebKit/WebEditingDelegate.h>
37 #import <WebKit/WebFramePrivate.h>
38 #import <WebKit/WebFrameView.h>
39 #import <WebKit/WebHistory.h>
40 #import <WebKit/WebPreferences.h>
41 #import <WebKit/WebView.h>
42 #import <WebKit/WebHTMLViewPrivate.h>
43 #import <WebKit/WebDocumentPrivate.h>
44 #import <WebKit/WebPluginDatabase.h>
45 #import <WebKit/WebHistoryItemPrivate.h>
46
47 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
48 #import <objc/objc-runtime.h>                       // for class_poseAs
49
50 #define COMMON_DIGEST_FOR_OPENSSL
51 #import <CommonCrypto/CommonDigest.h>               // for MD5 functions
52
53 #import <getopt.h>
54 #import <malloc/malloc.h>
55
56 #import "AppleScriptController.h"
57 #import "DumpRenderTreeDraggingInfo.h"
58 #import "EditingDelegate.h"
59 #import "EventSendingController.h"
60 #import "GCController.h"
61 #import "NavigationController.h"
62 #import "ObjCPlugin.h"
63 #import "ObjCPluginFunction.h"
64 #import "TextInputController.h"
65
66 @interface DumpRenderTreeWindow : NSWindow
67 @end
68
69 @interface DumpRenderTreePasteboard : NSPasteboard
70 @end
71
72 @interface DumpRenderTreeEvent : NSEvent
73 @end
74
75 @interface WaitUntilDoneDelegate : NSObject
76 @end
77
78 @interface LayoutTestController : NSObject
79 @end
80
81 static void runTest(const char *pathOrURL);
82 static NSString *md5HashStringForBitmap(CGImageRef bitmap);
83
84 WebFrame *frame = 0;
85 DumpRenderTreeDraggingInfo *draggingInfo = 0;
86
87 static volatile BOOL done;
88 static NavigationController *navigationController;
89
90 // Deciding when it's OK to dump out the state is a bit tricky.  All these must be true:
91 // - There is no load in progress
92 // - There is no work queued up (see workQueue var, below)
93 // - waitToDump==NO.  This means either waitUntilDone was never called, or it was called
94 //       and notifyDone was called subsequently.
95 // Note that the call to notifyDone and the end of the load can happen in either order.
96
97 // This is the topmost frame that is loading, during a given load, or nil when no load is 
98 // in progress.  Usually this is the same as the main frame, but not always.  In the case
99 // where a frameset is loaded, and then new content is loaded into one of the child frames,
100 // that child frame is the "topmost frame that is loading".
101 static WebFrame *topLoadingFrame;     // !nil iff a load is in progress
102 static BOOL waitToDump;     // TRUE if waitUntilDone() has been called, but notifyDone() has not yet been called
103
104 static BOOL dumpAsText;
105 static BOOL dumpSelectionRect;
106 static BOOL dumpTitleChanges;
107 static BOOL dumpBackForwardList;
108 static BOOL dumpChildFrameScrollPositions;
109 static int dumpPixels = NO;
110 static int dumpAllPixels = NO;
111 static BOOL readFromWindow = NO;
112 static int testRepaintDefault = NO;
113 static BOOL testRepaint = NO;
114 static int repaintSweepHorizontallyDefault = NO;
115 static BOOL repaintSweepHorizontally = NO;
116 static int dumpTree = YES;
117 static BOOL printSeparators;
118 static NSString *currentTest = nil;
119 static NSPasteboard *localPasteboard;
120 static WebHistoryItem *prevTestBFItem = nil;  // current b/f item at the end of the previous test
121 static BOOL windowIsKey = YES;
122 static unsigned char* screenCaptureBuffer;
123 static CGColorSpaceRef sharedColorSpace;
124 // a queue of NSInvocations, queued by callouts from the test, to be exec'ed when the load is done
125 static NSMutableArray *workQueue = nil;
126 // to prevent infinite loops, only the first page of a test can add to a work queue
127 // (since we may well come back to that same page)
128 BOOL workQueueFrozen;
129
130 const unsigned maxViewHeight = 600;
131 const unsigned maxViewWidth = 800;
132
133 BOOL doneLoading(void)
134 {
135     return done;
136 }
137
138 static CMProfileRef currentColorProfile = 0;
139 static void restoreColorSpace(int ignored)
140 {
141     if (currentColorProfile) {
142         int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
143         if (error)
144             fprintf(stderr, "Failed to retore previous color profile!  You may need to open System Preferences : Displays : Color and manually restore your color settings.  (Error: %i)", error);
145         currentColorProfile = 0;
146     }
147 }
148
149 static void crashHandler(int sig)
150 {
151     fprintf(stderr, "%s\n", strsignal(sig));
152     restoreColorSpace(0);
153     exit(128 + sig);
154 }
155
156 static void setDefaultColorProfileToRGB(void)
157 {
158     CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
159     CMProfileRef previousProfile;
160     int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
161     if (error) {
162         fprintf(stderr, "Failed to get current color profile.  I will not be able to restore your current profile, thus I'm not changing it.  Many pixel tests may fail as a result.  (Error: %i)\n", error);
163         return;
164     }
165     if (previousProfile == genericProfile)
166         return;
167     CFStringRef previousProfileName;
168     CFStringRef genericProfileName;
169     char previousProfileNameString[1024];
170     char genericProfileNameString[1024];
171     CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
172     CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
173     CFStringGetCString(previousProfileName, previousProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
174     CFStringGetCString(genericProfileName, genericProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
175     CFRelease(genericProfileName);
176     CFRelease(previousProfileName);
177     
178     fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n", previousProfileNameString, genericProfileNameString);
179     fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
180     fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
181     
182     if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
183         fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result.  (Error: %i)",
184             genericProfileNameString, error);
185     else {
186         currentColorProfile = previousProfile;
187         signal(SIGINT, restoreColorSpace);
188         signal(SIGHUP, restoreColorSpace);
189         signal(SIGTERM, restoreColorSpace);
190     }
191 }
192
193 static void* (*savedMalloc)(malloc_zone_t*, size_t);
194 static void* (*savedRealloc)(malloc_zone_t*, void*, size_t);
195
196 static void* checkedMalloc(malloc_zone_t* zone, size_t size)
197 {
198     if (size >= 0x10000000)
199         return 0;
200     return savedMalloc(zone, size);
201 }
202
203 static void* checkedRealloc(malloc_zone_t* zone, void* ptr, size_t size)
204 {
205     if (size >= 0x10000000)
206         return 0;
207     return savedRealloc(zone, ptr, size);
208 }
209
210 static void makeLargeMallocFailSilently(void)
211 {
212     malloc_zone_t* zone = malloc_default_zone();
213     savedMalloc = zone->malloc;
214     savedRealloc = zone->realloc;
215     zone->malloc = checkedMalloc;
216     zone->realloc = checkedRealloc;
217 }
218
219 int main(int argc, const char *argv[])
220 {
221     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
222     
223     [NSApplication sharedApplication];
224
225     class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
226     class_poseAs(objc_getClass("DumpRenderTreeWindow"), objc_getClass("NSWindow"));
227     class_poseAs(objc_getClass("DumpRenderTreeEvent"), objc_getClass("NSEvent"));
228
229     struct option options[] = {
230         {"dump-all-pixels", no_argument, &dumpAllPixels, YES},
231         {"horizontal-sweep", no_argument, &repaintSweepHorizontallyDefault, YES},
232         {"notree", no_argument, &dumpTree, NO},
233         {"pixel-tests", no_argument, &dumpPixels, YES},
234         {"repaint", no_argument, &testRepaintDefault, YES},
235         {"tree", no_argument, &dumpTree, YES},
236         {NULL, 0, NULL, 0}
237     };
238
239     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
240     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
241     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
242     // 2 is the "Medium" font smoothing mode
243     [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
244
245     [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
246     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
247     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
248     
249     [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
250     
251     WebPreferences *preferences = [WebPreferences standardPreferences];
252     
253     [preferences setStandardFontFamily:@"Times"];
254     [preferences setFixedFontFamily:@"Courier"];
255     [preferences setSerifFontFamily:@"Times"];
256     [preferences setSansSerifFontFamily:@"Helvetica"];
257     [preferences setCursiveFontFamily:@"Apple Chancery"];
258     [preferences setFantasyFontFamily:@"Papyrus"];
259     [preferences setDefaultFontSize:16];
260     [preferences setDefaultFixedFontSize:13];
261     [preferences setMinimumFontSize:9];
262     [preferences setJavaEnabled:NO];
263     [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
264
265     int option;
266     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
267         switch (option) {
268             case '?':   // unknown or ambiguous option
269             case ':':   // missing argument
270                 exit(1);
271                 break;
272         }
273     
274     if ([[[NSFontManager sharedFontManager] availableMembersOfFontFamily:@"Ahem"] count] == 0) {
275         fprintf(stderr, "\nAhem font is not available. This special simple font is used to construct certain types of predictable tests.\n\nTo run regression tests, please get it from <http://webkit.opendarwin.org/quality/Ahem.ttf>.\n");
276         exit(1);
277     }
278     
279     if (dumpPixels) {
280         setDefaultColorProfileToRGB();
281         screenCaptureBuffer = malloc(maxViewHeight * maxViewWidth * 4);
282         sharedColorSpace = CGColorSpaceCreateDeviceRGB();
283     }
284     
285     localPasteboard = [NSPasteboard pasteboardWithUniqueName];
286     navigationController = [[NavigationController alloc] init];
287
288     NSRect rect = NSMakeRect(0, 0, maxViewWidth, maxViewHeight);
289     
290     WebView *webView = [[WebView alloc] initWithFrame:rect];
291     WaitUntilDoneDelegate *delegate = [[WaitUntilDoneDelegate alloc] init];
292     EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
293     [webView setFrameLoadDelegate:delegate];
294     [webView setEditingDelegate:editingDelegate];
295     [webView setUIDelegate:delegate];
296     frame = [webView mainFrame];
297     
298     [[webView preferences] setTabsToLinks:YES];
299     
300     NSString *pwd = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
301     [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
302     [[WebPluginDatabase installedPlugins] refresh];
303
304     // The back/forward cache is causing problems due to layouts during transition from one page to another.
305     // So, turn it off for now, but we might want to turn it back on some day.
306     [[webView backForwardList] setPageCacheSize:0];
307
308     // 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.
309     // Put it at -10000, -10000 in "flipped coordinates", since WebCore and the DOM use flipped coordinates.
310     NSRect windowRect = NSOffsetRect(rect, -10000, [[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
311     NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
312     [[window contentView] addSubview:webView];
313     [window orderBack:nil];
314     [window setAutodisplay:NO];
315
316     workQueue = [[NSMutableArray alloc] init];
317
318     [webView setContinuousSpellCheckingEnabled:YES];
319
320     makeLargeMallocFailSilently();
321
322     signal(SIGILL, crashHandler);    /* 4:   illegal instruction (not reset when caught) */
323     signal(SIGTRAP, crashHandler);   /* 5:   trace trap (not reset when caught) */
324     signal(SIGEMT, crashHandler);    /* 7:   EMT instruction */
325     signal(SIGFPE, crashHandler);    /* 8:   floating point exception */
326     signal(SIGBUS, crashHandler);    /* 10:  bus error */
327     signal(SIGSEGV, crashHandler);   /* 11:  segmentation violation */
328     signal(SIGSYS, crashHandler);    /* 12:  bad argument to system call */
329     signal(SIGPIPE, crashHandler);   /* 13:  write on a pipe with no reader */
330     signal(SIGXCPU, crashHandler);   /* 24:  exceeded CPU time limit */
331     signal(SIGXFSZ, crashHandler);   /* 25:  exceeded file size limit */
332     
333     [[NSURLCache sharedURLCache] removeAllCachedResponses];
334     
335     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
336     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
337     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
338     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
339     
340     if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
341         char filenameBuffer[2048];
342         printSeparators = YES;
343         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
344             char *newLineCharacter = strchr(filenameBuffer, '\n');
345             if (newLineCharacter)
346                 *newLineCharacter = '\0';
347             
348             if (strlen(filenameBuffer) == 0)
349                 continue;
350                 
351             runTest(filenameBuffer);
352             fflush(stdout);
353         }
354     } else {
355         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
356         for (int i = optind; i != argc; ++i)
357             runTest(argv[i]);
358     }
359     
360     [workQueue release];
361
362     [webView setFrameLoadDelegate:nil];
363     [webView setEditingDelegate:nil];
364     [webView setUIDelegate:nil];
365     frame = nil;
366
367     // Work around problem where registering drag types leaves an outstanding
368     // "perform selector" on the window, which retains the window. It's a bit
369     // inelegant and perhaps dangerous to just blow them all away, but in practice
370     // it probably won't cause any trouble (and this is just a test tool, after all).
371     [NSObject cancelPreviousPerformRequestsWithTarget:window];
372     
373     [window close]; // releases when closed
374     [webView release];
375     [delegate release];
376     [editingDelegate release];
377
378     [localPasteboard releaseGlobally];
379     localPasteboard = nil;
380     
381     [navigationController release];
382     navigationController = nil;
383     
384     if (dumpPixels)
385         restoreColorSpace(0);
386     
387     [pool release];
388
389     return 0;
390 }
391
392 static int compareHistoryItems(id item1, id item2, void *context)
393 {
394     return [[item1 target] caseInsensitiveCompare:[item2 target]];
395 }
396
397 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
398 {
399     int start = 0;
400     if (current) {
401         printf("curr->");
402         start = 6;
403     }
404     for (int i = start; i < indent; i++)
405         putchar(' ');
406     printf("%s", [[item URLString] UTF8String]);
407     NSString *target = [item target];
408     if (target && [target length] > 0)
409         printf(" (in frame \"%s\")", [target UTF8String]);
410     if ([item isTargetItem])
411         printf("  **nav target**");
412     putchar('\n');
413     NSArray *kids = [item children];
414     if (kids) {
415         // must sort to eliminate arbitrary result ordering which defeats reproducible testing
416         kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
417         for (unsigned i = 0; i < [kids count]; i++)
418             dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
419     }
420 }
421
422 static void dumpFrameScrollPosition(WebFrame *f)
423 {
424     NSPoint scrollPosition = [[[[f frameView] documentView] superview] bounds].origin;
425     if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
426         if ([f parentFrame] != nil)
427             printf("frame '%s' ", [[f name] UTF8String]);
428         printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
429     }
430
431     if (dumpChildFrameScrollPositions) {
432         NSArray *kids = [f childFrames];
433         if (kids)
434             for (unsigned i = 0; i < [kids count]; i++)
435                 dumpFrameScrollPosition([kids objectAtIndex:i]);
436     }
437 }
438
439 static void dump(void)
440 {
441     NSString *result = nil;
442     if (dumpTree) {
443         dumpAsText |= [[[[frame dataSource] response] MIMEType] isEqualToString:@"text/plain"];
444         if (dumpAsText) {
445             DOMElement *documentElement = [[frame DOMDocument] documentElement];
446             if ([documentElement isKindOfClass:[DOMHTMLElement class]])
447                 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
448             else
449                 result = [[documentElement valueForKey:@"textContent"] stringByAppendingString:@"\n"];
450         } else {
451             bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
452             if (isSVGW3CTest)
453                 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
454             else 
455                 [[frame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
456             result = [frame renderTreeAsExternalRepresentation];
457         }
458         
459         if (!result)
460             printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
461         else {
462             fputs([result UTF8String], stdout);
463             if (!dumpAsText)
464                 dumpFrameScrollPosition(frame);
465         }
466
467         if (dumpBackForwardList) {
468             printf("\n============== Back Forward List ==============\n");
469             WebBackForwardList *bfList = [[frame webView] backForwardList];
470
471             // Print out all items in the list after prevTestBFItem, which was from the previous test
472             // Gather items from the end of the list, the print them out from oldest to newest
473             NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
474             for (int i = [bfList forwardListCount]; i > 0; i--) {
475                 WebHistoryItem *item = [bfList itemAtIndex:i];
476                 // something is wrong if the item from the last test is in the forward part of the b/f list
477                 assert(item != prevTestBFItem);
478                 [itemsToPrint addObject:item];
479             }
480             
481             assert([bfList currentItem] != prevTestBFItem);
482             [itemsToPrint addObject:[bfList currentItem]];
483             int currentItemIndex = [itemsToPrint count] - 1;
484
485             for (int i = -1; i >= -[bfList backListCount]; i--) {
486                 WebHistoryItem *item = [bfList itemAtIndex:i];
487                 if (item == prevTestBFItem)
488                     break;
489                 [itemsToPrint addObject:item];
490             }
491
492             for (int i = [itemsToPrint count]-1; i >= 0; i--) {
493                 dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
494             }
495             [itemsToPrint release];
496             printf("===============================================\n");
497         }
498
499         if (printSeparators)
500             puts("#EOF");
501     }
502     
503     if (dumpPixels) {
504         if (!dumpAsText) {
505             // grab a bitmap from the view
506             WebView* view = [frame webView];
507             NSSize webViewSize = [view frame].size;
508
509             CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
510
511             NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
512             NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
513             [NSGraphicsContext setCurrentContext:nsContext];
514
515             if (readFromWindow) {
516                 NSBitmapImageRep *imageRep;
517                 [view displayIfNeeded];
518                 [view lockFocus];
519                 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
520                 [view unlockFocus];
521                 [imageRep draw];
522                 [imageRep release];
523             } else if (!testRepaint)
524                 [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
525             else if (!repaintSweepHorizontally) {
526                 NSRect line = NSMakeRect(0, 0, webViewSize.width, 1);
527                 while (line.origin.y < webViewSize.height) {
528                     [view displayRectIgnoringOpacity:line inContext:nsContext];
529                     line.origin.y++;
530                 }
531             } else {
532                 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
533                 while (column.origin.x < webViewSize.width) {
534                     [view displayRectIgnoringOpacity:column inContext:nsContext];
535                     column.origin.x++;
536                 }
537             }
538             if (dumpSelectionRect) {
539                 NSView *documentView = [[frame frameView] documentView];
540                 if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
541                     [[NSColor redColor] set];
542                     [NSBezierPath strokeRect:[documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil]];
543                 }
544             }
545
546             [NSGraphicsContext setCurrentContext:savedContext];
547             
548             CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
549             CGContextRelease(cgContext);
550
551             // compute the actual hash to compare to the expected image's hash
552             NSString *actualHash = md5HashStringForBitmap(bitmapImage);
553             printf("\nActualHash: %s\n", [actualHash UTF8String]);
554
555             BOOL dumpImage;
556             if (dumpAllPixels)
557                 dumpImage = YES;
558             else {
559                 // FIXME: It's unfortunate that we hardcode the file naming scheme here.
560                 // At one time, the perl script had all the knowledge about file layout.
561                 // Some day we should restore that setup by passing in more parameters to this tool.
562                 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
563                 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
564                 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
565                 NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
566
567                 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
568
569                 /// send the image to stdout if the hash mismatches or if there's no file in the file system
570                 dumpImage = ![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0;
571             }
572             
573             if (dumpImage) {
574                 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
575                 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
576                 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
577                 CGImageDestinationFinalize(imageDest);
578                 CFRelease(imageDest);
579                 printf("Content-length: %lu\n", CFDataGetLength(imageData));
580                 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
581                 CFRelease(imageData);
582             }
583
584             CGImageRelease(bitmapImage);
585         }
586
587         printf("#EOF\n");
588     }
589
590     done = YES;
591 }
592
593 @implementation WaitUntilDoneDelegate
594
595 // Exec messages in the work queue until they're all done, or one of them starts a new load
596 - (void)processWork:(id)dummy
597 {
598     // quit doing work once a load is in progress
599     while ([workQueue count] > 0 && !topLoadingFrame) {
600         [[workQueue objectAtIndex:0] invoke];
601         [workQueue removeObjectAtIndex:0];
602     }
603     
604     // if we didn't start a new load, then we finished all the commands, so we're ready to dump state
605     if (!topLoadingFrame && !waitToDump)
606         dump();
607 }
608
609 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
610 {
611     if ([dataSource webFrame] == topLoadingFrame) {
612         topLoadingFrame = nil;
613         workQueueFrozen = YES;      // first complete load freezes the queue for the rest of this test
614         if (!waitToDump) {
615             if ([workQueue count] > 0)
616                 [self performSelector:@selector(processWork:) withObject:nil afterDelay:0];
617             else
618                 dump();
619         }
620     }
621 }
622
623 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)f
624 {
625     // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
626     // end up doing two dumps for one test.
627     if (!topLoadingFrame && !done)
628         topLoadingFrame = f;
629 }
630
631 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
632 {
633     windowIsKey = YES;
634     NSView *documentView = [[frame frameView] documentView];
635     [[[frame webView] window] makeFirstResponder:documentView];
636     if ([documentView isKindOfClass:[WebHTMLView class]])
637         [(WebHTMLView *)documentView _updateActiveState];
638 }
639
640 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
641 {
642     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
643 }
644
645 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
646 {
647     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
648     [navigationController webView:sender didFinishLoadForFrame:frame];
649 }
650
651 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
652 {
653     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
654 }
655
656 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
657
658     LayoutTestController *ltc = [[LayoutTestController alloc] init];
659     [obj setValue:ltc forKey:@"layoutTestController"];
660     [ltc release];
661     
662     EventSendingController *esc = [[EventSendingController alloc] init];
663     [obj setValue:esc forKey:@"eventSender"];
664     [esc release];
665     
666     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
667     [obj setValue:tic forKey:@"textInputController"];
668     [tic release];
669     
670     AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
671     [obj setValue:asc forKey:@"appleScriptController"];
672     [asc release];
673     
674     GCController *gcc = [[GCController alloc] init];
675     [obj setValue:gcc forKey:@"GCController"];
676     [gcc release];
677     
678     [obj setValue:navigationController forKey:@"navigationController"];
679     
680     ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
681     [obj setValue:plugin forKey:@"objCPlugin"];
682     [plugin release];
683     
684     ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
685     [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
686     [pluginFunction release];
687 }
688
689 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
690 {
691     printf("ALERT: %s\n", [message UTF8String]);
692 }
693
694 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
695 {
696     if (dumpTitleChanges)
697         printf("TITLE CHANGED: %s\n", [title UTF8String]);
698 }
699
700 - (void)webView:(WebView *)sender dragImage:(NSImage *)anImage at:(NSPoint)viewLocation offset:(NSSize)initialOffset event:(NSEvent *)event pasteboard:(NSPasteboard *)pboard source:(id)sourceObj slideBack:(BOOL)slideFlag forView:(NSView *)view
701 {
702     // A new drag was started before the old one ended.  Probably shouldn't happen.
703     if (draggingInfo) {
704         [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
705         [draggingInfo release];
706     }
707     draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
708 }
709
710 - (void)webViewFocus:(WebView *)webView
711 {
712     windowIsKey = YES;
713     NSView *documentView = [[frame frameView] documentView];
714     if ([documentView isKindOfClass:[WebHTMLView class]])
715         [(WebHTMLView *)documentView _updateActiveState];
716 }
717
718 @end
719
720 @implementation LayoutTestController
721
722 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
723 {
724     if (aSelector == @selector(waitUntilDone)
725             || aSelector == @selector(notifyDone)
726             || aSelector == @selector(dumpAsText)
727             || aSelector == @selector(dumpTitleChanges)
728             || aSelector == @selector(dumpBackForwardList)
729             || aSelector == @selector(dumpChildFrameScrollPositions)
730             || aSelector == @selector(setWindowIsKey:)
731             || aSelector == @selector(setMainFrameIsFirstResponder:)
732             || aSelector == @selector(dumpSelectionRect)
733             || aSelector == @selector(display)
734             || aSelector == @selector(testRepaint)
735             || aSelector == @selector(repaintSweepHorizontally)
736             || aSelector == @selector(queueBackNavigation:)
737             || aSelector == @selector(queueForwardNavigation:)
738             || aSelector == @selector(queueReload)
739             || aSelector == @selector(queueScript:)
740             || aSelector == @selector(queueLoad:target:)
741             || aSelector == @selector(clearBackForwardList)
742             || aSelector == @selector(keepWebHistory)
743             || aSelector == @selector(setAcceptsEditing:))
744         return NO;
745     return YES;
746 }
747
748 + (NSString *)webScriptNameForSelector:(SEL)aSelector
749 {
750     if (aSelector == @selector(setWindowIsKey:))
751         return @"setWindowIsKey";
752     if (aSelector == @selector(setMainFrameIsFirstResponder:))
753         return @"setMainFrameIsFirstResponder";
754     if (aSelector == @selector(queueBackNavigation:))
755         return @"queueBackNavigation";
756     if (aSelector == @selector(queueForwardNavigation:))
757         return @"queueForwardNavigation";
758     if (aSelector == @selector(queueScript:))
759         return @"queueScript";
760     if (aSelector == @selector(queueLoad:target:))
761         return @"queueLoad";
762     if (aSelector == @selector(setAcceptsEditing:))
763         return @"setAcceptsEditing";
764     return nil;
765 }
766
767 - (void)clearBackForwardList
768 {
769     WebBackForwardList *backForwardList = [[frame webView] backForwardList];
770     WebHistoryItem *item = [[backForwardList currentItem] retain];
771
772     // We clear the history by setting the back/forward list's capacity to 0
773     // then restoring it back and adding back the current item.
774     int capacity = [backForwardList capacity];
775     [backForwardList setCapacity:0];
776     [backForwardList setCapacity:capacity];
777     [backForwardList addItem:item];
778     [backForwardList goToItem:item];
779     [item release];
780 }
781
782 - (void)keepWebHistory
783 {
784     if (![WebHistory optionalSharedHistory]) {
785         WebHistory *history = [[WebHistory alloc] init];
786         [WebHistory setOptionalSharedHistory:history];
787         [history release];
788     }
789 }
790
791 - (void)waitUntilDone 
792 {
793     waitToDump = YES;
794 }
795
796 - (void)notifyDone
797 {
798     if (waitToDump && !topLoadingFrame && [workQueue count] == 0)
799         dump();
800     waitToDump = NO;
801 }
802
803 - (void)dumpAsText
804 {
805     dumpAsText = YES;
806 }
807
808 - (void)dumpSelectionRect
809 {
810     dumpSelectionRect = YES;
811 }
812
813 - (void)dumpTitleChanges
814 {
815     dumpTitleChanges = YES;
816 }
817
818 - (void)dumpBackForwardList
819 {
820     dumpBackForwardList = YES;
821 }
822
823 - (void)dumpChildFrameScrollPositions
824 {
825     dumpChildFrameScrollPositions = YES;
826 }
827
828 - (void)setWindowIsKey:(BOOL)flag
829 {
830     windowIsKey = flag;
831     NSView *documentView = [[frame frameView] documentView];
832     if ([documentView isKindOfClass:[WebHTMLView class]])
833         [(WebHTMLView *)documentView _updateActiveState];
834 }
835
836 - (void)setMainFrameIsFirstResponder:(BOOL)flag
837 {
838     NSView *documentView = [[frame frameView] documentView];
839     
840     NSResponder *firstResponder = flag ? documentView : nil;
841     [[[frame webView] window] makeFirstResponder:firstResponder];
842         
843     if ([documentView isKindOfClass:[WebHTMLView class]])
844         [(WebHTMLView *)documentView _updateActiveState];
845 }
846
847 - (void)display
848 {
849     NSView *webView = [frame webView];
850     [webView display];
851     [webView lockFocus];
852     [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
853     NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
854     [webView unlockFocus];
855     readFromWindow = YES;
856 }
857
858 - (void)testRepaint
859 {
860     testRepaint = YES;
861 }
862
863 - (void)repaintSweepHorizontally
864 {
865     repaintSweepHorizontally = YES;
866 }
867
868 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
869 {
870     return nil;
871 }
872
873 - (void)_addWorkForTarget:(id)target selector:(SEL)selector arg1:(id)arg1 arg2:(id)arg2
874 {
875     if (workQueueFrozen)
876         return;
877     NSMethodSignature *sig = [target methodSignatureForSelector:selector];
878     NSInvocation *work = [NSInvocation invocationWithMethodSignature:sig];
879     [work retainArguments];
880     [work setTarget:target];
881     [work setSelector:selector];
882     if (arg1) {
883         [work setArgument:&arg1 atIndex:2];
884         if (arg2)
885             [work setArgument:&arg2 atIndex:3];
886     }
887     [workQueue addObject:work];
888 }
889
890 - (void)_doLoad:(NSURL *)url target:(NSString *)target
891 {
892     WebFrame *targetFrame;
893     if (target && ![target isKindOfClass:[WebUndefined class]])
894         targetFrame = [frame findFrameNamed:target];
895     else
896         targetFrame = frame;
897     [targetFrame loadRequest:[NSURLRequest requestWithURL:url]];
898 }
899
900 - (void)_doBackOrForwardNavigation:(NSNumber *)index
901 {
902     int bfIndex = [index intValue];
903     if (bfIndex == 1)
904         [[frame webView] goForward];
905     if (bfIndex == -1)
906         [[frame webView] goBack];
907     else {        
908         WebBackForwardList *bfList = [[frame webView] backForwardList];
909         [[frame webView] goToBackForwardItem:[bfList itemAtIndex:bfIndex]];
910     }
911 }
912
913 - (void)queueBackNavigation:(int)howFarBack
914 {
915     [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:-howFarBack] arg2:nil];
916 }
917
918 - (void)queueForwardNavigation:(int)howFarForward
919 {
920     [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:howFarForward] arg2:nil];
921 }
922
923 - (void)queueReload
924 {
925     [self _addWorkForTarget:[frame webView] selector:@selector(reload:) arg1:self arg2:nil];
926 }
927
928 - (void)queueScript:(NSString *)script
929 {
930     [self _addWorkForTarget:[frame webView] selector:@selector(stringByEvaluatingJavaScriptFromString:) arg1:script arg2:nil];
931 }
932
933 - (void)queueLoad:(NSString *)URLString target:(NSString *)target
934 {
935     NSURL *URL = [NSURL URLWithString:URLString relativeToURL:[[[frame dataSource] response] URL]];
936     [self _addWorkForTarget:self selector:@selector(_doLoad:target:) arg1:URL arg2:target];
937 }
938
939 - (void)setAcceptsEditing:(BOOL)newAcceptsEditing
940 {
941     [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
942 }
943
944 @end
945
946 static void runTest(const char *pathOrURL)
947 {
948     CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
949     if (!pathOrURLString) {
950         fprintf(stderr, "can't parse filename as UTF-8\n");
951         return;
952     }
953
954     CFURLRef URL;
955     if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
956         URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
957     else
958         URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
959     
960     if (!URL) {
961         CFRelease(pathOrURLString);
962         fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
963         return;
964     }
965
966     [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:YES];
967     done = NO;
968     topLoadingFrame = nil;
969     waitToDump = NO;
970     dumpAsText = NO;
971     dumpChildFrameScrollPositions = NO;
972     dumpSelectionRect = NO;
973     dumpTitleChanges = NO;
974     dumpBackForwardList = NO;
975     readFromWindow = NO;
976     testRepaint = testRepaintDefault;
977     repaintSweepHorizontally = repaintSweepHorizontallyDefault;
978     if ([WebHistory optionalSharedHistory])
979         [WebHistory setOptionalSharedHistory:nil];
980     lastMousePosition = NSMakePoint(0, 0);
981
982     if (currentTest != nil)
983         CFRelease(currentTest);
984     currentTest = (NSString *)pathOrURLString;
985     [prevTestBFItem release];
986     prevTestBFItem = [[[[frame webView] backForwardList] currentItem] retain];
987     [workQueue removeAllObjects];
988     workQueueFrozen = NO;
989
990     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
991     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
992     CFRelease(URL);
993     [pool release];
994     while (!done) {
995         pool = [[NSAutoreleasePool alloc] init];
996         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
997         [pool release];
998     }
999     pool = [[NSAutoreleasePool alloc] init];
1000     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1001     if (draggingInfo)
1002         [draggingInfo release];
1003     draggingInfo = nil;
1004     [pool release];
1005 }
1006
1007 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
1008 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
1009 {
1010     MD5_CTX md5Context;
1011     unsigned char hash[16];
1012     
1013     unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
1014     assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
1015     unsigned bytesPerPixel = bitsPerPixel / 8;
1016     unsigned pixelsHigh = CGImageGetHeight(bitmap);
1017     unsigned pixelsWide = CGImageGetWidth(bitmap);
1018     unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
1019     assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
1020     
1021     MD5_Init(&md5Context);
1022     unsigned char *bitmapData = screenCaptureBuffer;
1023     for (unsigned row = 0; row < pixelsHigh; row++) {
1024         MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
1025         bitmapData += bytesPerRow;
1026     }
1027     MD5_Final(hash, &md5Context);
1028     
1029     char hex[33] = "";
1030     for (int i = 0; i < 16; i++) {
1031        snprintf(hex, 33, "%s%02x", hex, hash[i]);
1032     }
1033
1034     return [NSString stringWithUTF8String:hex];
1035 }
1036
1037 @implementation DumpRenderTreePasteboard
1038
1039 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
1040 + (NSPasteboard *)generalPasteboard
1041 {
1042     return localPasteboard;
1043 }
1044
1045 @end
1046
1047 @implementation DumpRenderTreeWindow
1048
1049 - (BOOL)isKeyWindow
1050 {
1051     return windowIsKey;
1052 }
1053
1054 @end
1055
1056 @implementation DumpRenderTreeEvent
1057
1058 + (NSPoint)mouseLocation
1059 {
1060     return [[[frame webView] window] convertBaseToScreen:lastMousePosition];
1061 }
1062
1063 @end