2006-10-02 Nikolas Zimmermann <zimmermann@kde.org>
[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             // FIXME: Buildbots seem to scroll to (-2,0) on a number of tests, but we can't reproduce locally.
464             // Once we fix that issue, we can turn this back on.
465             /*
466             if (!dumpAsText)
467                 dumpFrameScrollPosition(frame);
468              */
469         }
470
471         if (dumpBackForwardList) {
472             printf("\n============== Back Forward List ==============\n");
473             WebBackForwardList *bfList = [[frame webView] backForwardList];
474
475             // Print out all items in the list after prevTestBFItem, which was from the previous test
476             // Gather items from the end of the list, the print them out from oldest to newest
477             NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
478             for (int i = [bfList forwardListCount]; i > 0; i--) {
479                 WebHistoryItem *item = [bfList itemAtIndex:i];
480                 // something is wrong if the item from the last test is in the forward part of the b/f list
481                 assert(item != prevTestBFItem);
482                 [itemsToPrint addObject:item];
483             }
484             
485             assert([bfList currentItem] != prevTestBFItem);
486             [itemsToPrint addObject:[bfList currentItem]];
487             int currentItemIndex = [itemsToPrint count] - 1;
488
489             for (int i = -1; i >= -[bfList backListCount]; i--) {
490                 WebHistoryItem *item = [bfList itemAtIndex:i];
491                 if (item == prevTestBFItem)
492                     break;
493                 [itemsToPrint addObject:item];
494             }
495
496             for (int i = [itemsToPrint count]-1; i >= 0; i--) {
497                 dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
498             }
499             [itemsToPrint release];
500             printf("===============================================\n");
501         }
502
503         if (printSeparators)
504             puts("#EOF");
505     }
506     
507     if (dumpPixels) {
508         if (!dumpAsText) {
509             // grab a bitmap from the view
510             WebView* view = [frame webView];
511             NSSize webViewSize = [view frame].size;
512
513             CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
514
515             NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
516             NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
517             [NSGraphicsContext setCurrentContext:nsContext];
518
519             if (readFromWindow) {
520                 NSBitmapImageRep *imageRep;
521                 [view displayIfNeeded];
522                 [view lockFocus];
523                 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
524                 [view unlockFocus];
525                 [imageRep draw];
526                 [imageRep release];
527             } else if (!testRepaint)
528                 [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
529             else if (!repaintSweepHorizontally) {
530                 NSRect line = NSMakeRect(0, 0, webViewSize.width, 1);
531                 while (line.origin.y < webViewSize.height) {
532                     [view displayRectIgnoringOpacity:line inContext:nsContext];
533                     line.origin.y++;
534                 }
535             } else {
536                 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
537                 while (column.origin.x < webViewSize.width) {
538                     [view displayRectIgnoringOpacity:column inContext:nsContext];
539                     column.origin.x++;
540                 }
541             }
542             if (dumpSelectionRect) {
543                 NSView *documentView = [[frame frameView] documentView];
544                 if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
545                     [[NSColor redColor] set];
546                     [NSBezierPath strokeRect:[documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil]];
547                 }
548             }
549
550             [NSGraphicsContext setCurrentContext:savedContext];
551             
552             CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
553             CGContextRelease(cgContext);
554
555             // compute the actual hash to compare to the expected image's hash
556             NSString *actualHash = md5HashStringForBitmap(bitmapImage);
557             printf("\nActualHash: %s\n", [actualHash UTF8String]);
558
559             BOOL dumpImage;
560             if (dumpAllPixels)
561                 dumpImage = YES;
562             else {
563                 // FIXME: It's unfortunate that we hardcode the file naming scheme here.
564                 // At one time, the perl script had all the knowledge about file layout.
565                 // Some day we should restore that setup by passing in more parameters to this tool.
566                 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
567                 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
568                 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
569                 NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
570
571                 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
572
573                 /// send the image to stdout if the hash mismatches or if there's no file in the file system
574                 dumpImage = ![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0;
575             }
576             
577             if (dumpImage) {
578                 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
579                 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
580                 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
581                 CGImageDestinationFinalize(imageDest);
582                 CFRelease(imageDest);
583                 printf("Content-length: %lu\n", CFDataGetLength(imageData));
584                 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
585                 CFRelease(imageData);
586             }
587
588             CGImageRelease(bitmapImage);
589         }
590
591         printf("#EOF\n");
592     }
593
594     done = YES;
595 }
596
597 @implementation WaitUntilDoneDelegate
598
599 // Exec messages in the work queue until they're all done, or one of them starts a new load
600 - (void)processWork:(id)dummy
601 {
602     // quit doing work once a load is in progress
603     while ([workQueue count] > 0 && !topLoadingFrame) {
604         [[workQueue objectAtIndex:0] invoke];
605         [workQueue removeObjectAtIndex:0];
606     }
607     
608     // if we didn't start a new load, then we finished all the commands, so we're ready to dump state
609     if (!topLoadingFrame && !waitToDump)
610         dump();
611 }
612
613 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
614 {
615     if ([dataSource webFrame] == topLoadingFrame) {
616         topLoadingFrame = nil;
617         workQueueFrozen = YES;      // first complete load freezes the queue for the rest of this test
618         if (!waitToDump) {
619             if ([workQueue count] > 0)
620                 [self performSelector:@selector(processWork:) withObject:nil afterDelay:0];
621             else
622                 dump();
623         }
624     }
625 }
626
627 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)f
628 {
629     // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
630     // end up doing two dumps for one test.
631     if (!topLoadingFrame && !done)
632         topLoadingFrame = f;
633 }
634
635 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
636 {
637     windowIsKey = YES;
638     NSView *documentView = [[frame frameView] documentView];
639     [[[frame webView] window] makeFirstResponder:documentView];
640     if ([documentView isKindOfClass:[WebHTMLView class]])
641         [(WebHTMLView *)documentView _updateActiveState];
642 }
643
644 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
645 {
646     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
647 }
648
649 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
650 {
651     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
652     [navigationController webView:sender didFinishLoadForFrame:frame];
653 }
654
655 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
656 {
657     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
658 }
659
660 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
661
662     LayoutTestController *ltc = [[LayoutTestController alloc] init];
663     [obj setValue:ltc forKey:@"layoutTestController"];
664     [ltc release];
665     
666     EventSendingController *esc = [[EventSendingController alloc] init];
667     [obj setValue:esc forKey:@"eventSender"];
668     [esc release];
669     
670     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
671     [obj setValue:tic forKey:@"textInputController"];
672     [tic release];
673     
674     AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
675     [obj setValue:asc forKey:@"appleScriptController"];
676     [asc release];
677     
678     GCController *gcc = [[GCController alloc] init];
679     [obj setValue:gcc forKey:@"GCController"];
680     [gcc release];
681     
682     [obj setValue:navigationController forKey:@"navigationController"];
683     
684     ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
685     [obj setValue:plugin forKey:@"objCPlugin"];
686     [plugin release];
687     
688     ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
689     [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
690     [pluginFunction release];
691 }
692
693 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
694 {
695     printf("ALERT: %s\n", [message UTF8String]);
696 }
697
698 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
699 {
700     if (dumpTitleChanges)
701         printf("TITLE CHANGED: %s\n", [title UTF8String]);
702 }
703
704 - (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
705 {
706     // A new drag was started before the old one ended.  Probably shouldn't happen.
707     if (draggingInfo) {
708         [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
709         [draggingInfo release];
710     }
711     draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
712 }
713
714 - (void)webViewFocus:(WebView *)webView
715 {
716     windowIsKey = YES;
717     NSView *documentView = [[frame frameView] documentView];
718     if ([documentView isKindOfClass:[WebHTMLView class]])
719         [(WebHTMLView *)documentView _updateActiveState];
720 }
721
722 @end
723
724 @implementation LayoutTestController
725
726 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
727 {
728     if (aSelector == @selector(waitUntilDone)
729             || aSelector == @selector(notifyDone)
730             || aSelector == @selector(dumpAsText)
731             || aSelector == @selector(dumpTitleChanges)
732             || aSelector == @selector(dumpBackForwardList)
733             || aSelector == @selector(dumpChildFrameScrollPositions)
734             || aSelector == @selector(setWindowIsKey:)
735             || aSelector == @selector(setMainFrameIsFirstResponder:)
736             || aSelector == @selector(dumpSelectionRect)
737             || aSelector == @selector(display)
738             || aSelector == @selector(testRepaint)
739             || aSelector == @selector(repaintSweepHorizontally)
740             || aSelector == @selector(queueBackNavigation:)
741             || aSelector == @selector(queueForwardNavigation:)
742             || aSelector == @selector(queueReload)
743             || aSelector == @selector(queueScript:)
744             || aSelector == @selector(queueLoad:target:)
745             || aSelector == @selector(clearBackForwardList)
746             || aSelector == @selector(keepWebHistory)
747             || aSelector == @selector(setAcceptsEditing:))
748         return NO;
749     return YES;
750 }
751
752 + (NSString *)webScriptNameForSelector:(SEL)aSelector
753 {
754     if (aSelector == @selector(setWindowIsKey:))
755         return @"setWindowIsKey";
756     if (aSelector == @selector(setMainFrameIsFirstResponder:))
757         return @"setMainFrameIsFirstResponder";
758     if (aSelector == @selector(queueBackNavigation:))
759         return @"queueBackNavigation";
760     if (aSelector == @selector(queueForwardNavigation:))
761         return @"queueForwardNavigation";
762     if (aSelector == @selector(queueScript:))
763         return @"queueScript";
764     if (aSelector == @selector(queueLoad:target:))
765         return @"queueLoad";
766     if (aSelector == @selector(setAcceptsEditing:))
767         return @"setAcceptsEditing";
768     return nil;
769 }
770
771 - (void)clearBackForwardList
772 {
773     WebBackForwardList *backForwardList = [[frame webView] backForwardList];
774     WebHistoryItem *item = [[backForwardList currentItem] retain];
775
776     // We clear the history by setting the back/forward list's capacity to 0
777     // then restoring it back and adding back the current item.
778     int capacity = [backForwardList capacity];
779     [backForwardList setCapacity:0];
780     [backForwardList setCapacity:capacity];
781     [backForwardList addItem:item];
782     [backForwardList goToItem:item];
783     [item release];
784 }
785
786 - (void)keepWebHistory
787 {
788     if (![WebHistory optionalSharedHistory]) {
789         WebHistory *history = [[WebHistory alloc] init];
790         [WebHistory setOptionalSharedHistory:history];
791         [history release];
792     }
793 }
794
795 - (void)waitUntilDone 
796 {
797     waitToDump = YES;
798 }
799
800 - (void)notifyDone
801 {
802     if (waitToDump && !topLoadingFrame && [workQueue count] == 0)
803         dump();
804     waitToDump = NO;
805 }
806
807 - (void)dumpAsText
808 {
809     dumpAsText = YES;
810 }
811
812 - (void)dumpSelectionRect
813 {
814     dumpSelectionRect = YES;
815 }
816
817 - (void)dumpTitleChanges
818 {
819     dumpTitleChanges = YES;
820 }
821
822 - (void)dumpBackForwardList
823 {
824     dumpBackForwardList = YES;
825 }
826
827 - (void)dumpChildFrameScrollPositions
828 {
829     dumpChildFrameScrollPositions = YES;
830 }
831
832 - (void)setWindowIsKey:(BOOL)flag
833 {
834     windowIsKey = flag;
835     NSView *documentView = [[frame frameView] documentView];
836     if ([documentView isKindOfClass:[WebHTMLView class]])
837         [(WebHTMLView *)documentView _updateActiveState];
838 }
839
840 - (void)setMainFrameIsFirstResponder:(BOOL)flag
841 {
842     NSView *documentView = [[frame frameView] documentView];
843     
844     NSResponder *firstResponder = flag ? documentView : nil;
845     [[[frame webView] window] makeFirstResponder:firstResponder];
846         
847     if ([documentView isKindOfClass:[WebHTMLView class]])
848         [(WebHTMLView *)documentView _updateActiveState];
849 }
850
851 - (void)display
852 {
853     NSView *webView = [frame webView];
854     [webView display];
855     [webView lockFocus];
856     [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
857     NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
858     [webView unlockFocus];
859     readFromWindow = YES;
860 }
861
862 - (void)testRepaint
863 {
864     testRepaint = YES;
865 }
866
867 - (void)repaintSweepHorizontally
868 {
869     repaintSweepHorizontally = YES;
870 }
871
872 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
873 {
874     return nil;
875 }
876
877 - (void)_addWorkForTarget:(id)target selector:(SEL)selector arg1:(id)arg1 arg2:(id)arg2
878 {
879     if (workQueueFrozen)
880         return;
881     NSMethodSignature *sig = [target methodSignatureForSelector:selector];
882     NSInvocation *work = [NSInvocation invocationWithMethodSignature:sig];
883     [work retainArguments];
884     [work setTarget:target];
885     [work setSelector:selector];
886     if (arg1) {
887         [work setArgument:&arg1 atIndex:2];
888         if (arg2)
889             [work setArgument:&arg2 atIndex:3];
890     }
891     [workQueue addObject:work];
892 }
893
894 - (void)_doLoad:(NSURL *)url target:(NSString *)target
895 {
896     WebFrame *targetFrame;
897     if (target && ![target isKindOfClass:[WebUndefined class]])
898         targetFrame = [frame findFrameNamed:target];
899     else
900         targetFrame = frame;
901     [targetFrame loadRequest:[NSURLRequest requestWithURL:url]];
902 }
903
904 - (void)_doBackOrForwardNavigation:(NSNumber *)index
905 {
906     int bfIndex = [index intValue];
907     if (bfIndex == 1)
908         [[frame webView] goForward];
909     if (bfIndex == -1)
910         [[frame webView] goBack];
911     else {        
912         WebBackForwardList *bfList = [[frame webView] backForwardList];
913         [[frame webView] goToBackForwardItem:[bfList itemAtIndex:bfIndex]];
914     }
915 }
916
917 - (void)queueBackNavigation:(int)howFarBack
918 {
919     [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:-howFarBack] arg2:nil];
920 }
921
922 - (void)queueForwardNavigation:(int)howFarForward
923 {
924     [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:howFarForward] arg2:nil];
925 }
926
927 - (void)queueReload
928 {
929     [self _addWorkForTarget:[frame webView] selector:@selector(reload:) arg1:self arg2:nil];
930 }
931
932 - (void)queueScript:(NSString *)script
933 {
934     [self _addWorkForTarget:[frame webView] selector:@selector(stringByEvaluatingJavaScriptFromString:) arg1:script arg2:nil];
935 }
936
937 - (void)queueLoad:(NSString *)URLString target:(NSString *)target
938 {
939     NSURL *URL = [NSURL URLWithString:URLString relativeToURL:[[[frame dataSource] response] URL]];
940     [self _addWorkForTarget:self selector:@selector(_doLoad:target:) arg1:URL arg2:target];
941 }
942
943 - (void)setAcceptsEditing:(BOOL)newAcceptsEditing
944 {
945     [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
946 }
947
948 @end
949
950 static void runTest(const char *pathOrURL)
951 {
952     CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
953     if (!pathOrURLString) {
954         fprintf(stderr, "can't parse filename as UTF-8\n");
955         return;
956     }
957
958     CFURLRef URL;
959     if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
960         URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
961     else
962         URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
963     
964     if (!URL) {
965         CFRelease(pathOrURLString);
966         fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
967         return;
968     }
969
970     [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:YES];
971     done = NO;
972     topLoadingFrame = nil;
973     waitToDump = NO;
974     dumpAsText = NO;
975     dumpChildFrameScrollPositions = NO;
976     dumpSelectionRect = NO;
977     dumpTitleChanges = NO;
978     dumpBackForwardList = NO;
979     readFromWindow = NO;
980     testRepaint = testRepaintDefault;
981     repaintSweepHorizontally = repaintSweepHorizontallyDefault;
982     if ([WebHistory optionalSharedHistory])
983         [WebHistory setOptionalSharedHistory:nil];
984     lastMousePosition = NSMakePoint(0, 0);
985
986     if (currentTest != nil)
987         CFRelease(currentTest);
988     currentTest = (NSString *)pathOrURLString;
989     [prevTestBFItem release];
990     prevTestBFItem = [[[[frame webView] backForwardList] currentItem] retain];
991     [workQueue removeAllObjects];
992     workQueueFrozen = NO;
993
994     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
995     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
996     CFRelease(URL);
997     [pool release];
998     while (!done) {
999         pool = [[NSAutoreleasePool alloc] init];
1000         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1001         [pool release];
1002     }
1003     pool = [[NSAutoreleasePool alloc] init];
1004     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1005     if (draggingInfo)
1006         [draggingInfo release];
1007     draggingInfo = nil;
1008     [pool release];
1009 }
1010
1011 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
1012 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
1013 {
1014     MD5_CTX md5Context;
1015     unsigned char hash[16];
1016     
1017     unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
1018     assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
1019     unsigned bytesPerPixel = bitsPerPixel / 8;
1020     unsigned pixelsHigh = CGImageGetHeight(bitmap);
1021     unsigned pixelsWide = CGImageGetWidth(bitmap);
1022     unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
1023     assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
1024     
1025     MD5_Init(&md5Context);
1026     unsigned char *bitmapData = screenCaptureBuffer;
1027     for (unsigned row = 0; row < pixelsHigh; row++) {
1028         MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
1029         bitmapData += bytesPerRow;
1030     }
1031     MD5_Final(hash, &md5Context);
1032     
1033     char hex[33] = "";
1034     for (int i = 0; i < 16; i++) {
1035        snprintf(hex, 33, "%s%02x", hex, hash[i]);
1036     }
1037
1038     return [NSString stringWithUTF8String:hex];
1039 }
1040
1041 @implementation DumpRenderTreePasteboard
1042
1043 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
1044 + (NSPasteboard *)generalPasteboard
1045 {
1046     return localPasteboard;
1047 }
1048
1049 @end
1050
1051 @implementation DumpRenderTreeWindow
1052
1053 - (BOOL)isKeyWindow
1054 {
1055     return windowIsKey;
1056 }
1057
1058 @end
1059
1060 @implementation DumpRenderTreeEvent
1061
1062 + (NSPoint)mouseLocation
1063 {
1064     return [[[frame webView] window] convertBaseToScreen:lastMousePosition];
1065 }
1066
1067 @end