Reviewed by Darin.
[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/WebFrameView.h>
38 #import <WebKit/WebPreferences.h>
39 #import <WebKit/WebView.h>
40 #import <WebKit/WebHTMLViewPrivate.h>
41 #import <WebKit/WebDocumentPrivate.h>
42 #import <WebKit/WebPluginDatabase.h>
43
44 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
45 #import <objc/objc-runtime.h>                       // for class_poseAs
46
47 #define COMMON_DIGEST_FOR_OPENSSL
48 #import <CommonCrypto/CommonDigest.h>               // for MD5 functions
49
50 #import <getopt.h>
51 #import <malloc/malloc.h>
52
53 #import "AppleScriptController.h"
54 #import "DumpRenderTreeDraggingInfo.h"
55 #import "EditingDelegate.h"
56 #import "EventSendingController.h"
57 #import "NavigationController.h"
58 #import "ObjCPlugin.h"
59 #import "ObjCPluginFunction.h"
60 #import "TextInputController.h"
61
62 @interface DumpRenderTreeWindow : NSWindow
63 @end
64
65 @interface DumpRenderTreePasteboard : NSPasteboard
66 @end
67
68 @interface WaitUntilDoneDelegate : NSObject
69 @end
70
71 @interface LayoutTestController : NSObject
72 @end
73
74 static void dumpRenderTree(const char *pathOrURL);
75 static NSString *md5HashStringForBitmap(CGImageRef bitmap);
76
77 WebFrame *frame = 0;
78 DumpRenderTreeDraggingInfo *draggingInfo = 0;
79
80 static volatile BOOL done;
81 static NavigationController *navigationController;
82 static BOOL readyToDump;
83 static BOOL waitToDump;
84 static BOOL dumpAsText;
85 static BOOL dumpSelectionRect;
86 static BOOL dumpTitleChanges;
87 static int dumpPixels = NO;
88 static int dumpAllPixels = NO;
89 static BOOL readFromWindow = NO;
90 static int testRepaintDefault = NO;
91 static BOOL testRepaint = NO;
92 static int repaintSweepHorizontallyDefault = NO;
93 static BOOL repaintSweepHorizontally = NO;
94 static int dumpTree = YES;
95 static BOOL printSeparators;
96 static NSString *currentTest = nil;
97 static NSPasteboard *localPasteboard;
98 static BOOL windowIsKey = YES;
99 static unsigned char* screenCaptureBuffer;
100 static CGColorSpaceRef sharedColorSpace;
101
102 const unsigned maxViewHeight = 600;
103 const unsigned maxViewWidth = 800;
104
105 BOOL doneLoading(void)
106 {
107     return done;
108 }
109
110 static CMProfileRef currentColorProfile = 0;
111 static void restoreColorSpace(int ignored)
112 {
113     if (currentColorProfile) {
114         int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
115         if (error)
116             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);
117         currentColorProfile = 0;
118     }
119 }
120
121 static void crashHandler(int sig)
122 {
123     fprintf(stderr, "%s\n", strsignal(sig));
124     restoreColorSpace(0);
125     exit(128 + sig);
126 }
127
128 static void setDefaultColorProfileToRGB(void)
129 {
130     CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
131     CMProfileRef previousProfile;
132     int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
133     if (error) {
134         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);
135         return;
136     }
137     if (previousProfile == genericProfile)
138         return;
139     CFStringRef previousProfileName;
140     CFStringRef genericProfileName;
141     char previousProfileNameString[1024];
142     char genericProfileNameString[1024];
143     CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
144     CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
145     CFStringGetCString(previousProfileName, previousProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
146     CFStringGetCString(genericProfileName, genericProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
147     CFRelease(genericProfileName);
148     CFRelease(previousProfileName);
149     
150     fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n", previousProfileNameString, genericProfileNameString);
151     fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
152     fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
153     
154     if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
155         fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result.  (Error: %i)",
156             genericProfileNameString, error);
157     else {
158         currentColorProfile = previousProfile;
159         signal(SIGINT, restoreColorSpace);
160         signal(SIGHUP, restoreColorSpace);
161         signal(SIGTERM, restoreColorSpace);
162     }
163 }
164
165 static void* (*savedMalloc)(malloc_zone_t*, size_t);
166 static void* (*savedRealloc)(malloc_zone_t*, void*, size_t);
167
168 static void* checkedMalloc(malloc_zone_t* zone, size_t size)
169 {
170     if (size >= 0x10000000)
171         return 0;
172     return savedMalloc(zone, size);
173 }
174
175 static void* checkedRealloc(malloc_zone_t* zone, void* ptr, size_t size)
176 {
177     if (size >= 0x10000000)
178         return 0;
179     return savedRealloc(zone, ptr, size);
180 }
181
182 static void makeLargeMallocFailSilently(void)
183 {
184     malloc_zone_t* zone = malloc_default_zone();
185     savedMalloc = zone->malloc;
186     savedRealloc = zone->realloc;
187     zone->malloc = checkedMalloc;
188     zone->realloc = checkedRealloc;
189 }
190
191 int main(int argc, const char *argv[])
192 {
193     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
194     
195     [NSApplication sharedApplication];
196
197     class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
198     class_poseAs(objc_getClass("DumpRenderTreeWindow"), objc_getClass("NSWindow"));
199     
200     struct option options[] = {
201         {"dump-all-pixels", no_argument, &dumpAllPixels, YES},
202         {"horizontal-sweep", no_argument, &repaintSweepHorizontallyDefault, YES},
203         {"notree", no_argument, &dumpTree, NO},
204         {"pixel-tests", no_argument, &dumpPixels, YES},
205         {"repaint", no_argument, &testRepaintDefault, YES},
206         {"tree", no_argument, &dumpTree, YES},
207         {NULL, 0, NULL, 0}
208     };
209
210     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
211     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
212     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
213     // 2 is the "Medium" font smoothing mode
214     [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
215
216     [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
217     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
218     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
219     
220     [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
221     
222     WebPreferences *preferences = [WebPreferences standardPreferences];
223     
224     [preferences setStandardFontFamily:@"Times"];
225     [preferences setFixedFontFamily:@"Courier"];
226     [preferences setSerifFontFamily:@"Times"];
227     [preferences setSansSerifFontFamily:@"Helvetica"];
228     [preferences setCursiveFontFamily:@"Apple Chancery"];
229     [preferences setFantasyFontFamily:@"Papyrus"];
230     [preferences setDefaultFontSize:16];
231     [preferences setDefaultFixedFontSize:13];
232     [preferences setMinimumFontSize:9];
233     [preferences setJavaEnabled:NO];
234     [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
235
236     int option;
237     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
238         switch (option) {
239             case '?':   // unknown or ambiguous option
240             case ':':   // missing argument
241                 exit(1);
242                 break;
243         }
244     
245     if ([[[NSFontManager sharedFontManager] availableMembersOfFontFamily:@"Ahem"] count] == 0) {
246         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");
247         exit(1);
248     }
249     
250     if (dumpPixels) {
251         setDefaultColorProfileToRGB();
252         screenCaptureBuffer = malloc(maxViewHeight * maxViewWidth * 4);
253         sharedColorSpace = CGColorSpaceCreateDeviceRGB();
254     }
255     
256     localPasteboard = [NSPasteboard pasteboardWithUniqueName];
257     navigationController = [[NavigationController alloc] init];
258
259     NSRect rect = NSMakeRect(0, 0, maxViewWidth, maxViewHeight);
260     
261     WebView *webView = [[WebView alloc] initWithFrame:rect];
262     WaitUntilDoneDelegate *delegate = [[WaitUntilDoneDelegate alloc] init];
263     EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
264     [webView setFrameLoadDelegate:delegate];
265     [webView setEditingDelegate:editingDelegate];
266     [webView setUIDelegate:delegate];
267     frame = [webView mainFrame];
268     
269     [[webView preferences] setTabsToLinks:YES];
270     
271     NSString *pwd = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
272     [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
273     [[WebPluginDatabase installedPlugins] refresh];
274
275     // The back/forward cache is causing problems due to layouts during transition from one page to another.
276     // So, turn it off for now, but we might want to turn it back on some day.
277     [[webView backForwardList] setPageCacheSize:0];
278
279     // 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.
280     NSRect windowRect = NSOffsetRect(rect, -10000, -10000);
281     NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
282     [[window contentView] addSubview:webView];
283     [window orderBack:nil];
284     [window setAutodisplay:NO];
285
286     [webView setContinuousSpellCheckingEnabled:YES];
287
288     makeLargeMallocFailSilently();
289
290     signal(SIGILL, crashHandler);    /* 4:   illegal instruction (not reset when caught) */
291     signal(SIGTRAP, crashHandler);   /* 5:   trace trap (not reset when caught) */
292     signal(SIGEMT, crashHandler);    /* 7:   EMT instruction */
293     signal(SIGFPE, crashHandler);    /* 8:   floating point exception */
294     signal(SIGBUS, crashHandler);    /* 10:  bus error */
295     signal(SIGSEGV, crashHandler);   /* 11:  segmentation violation */
296     signal(SIGSYS, crashHandler);    /* 12:  bad argument to system call */
297     signal(SIGPIPE, crashHandler);   /* 13:  write on a pipe with no reader */
298     signal(SIGXCPU, crashHandler);   /* 24:  exceeded CPU time limit */
299     signal(SIGXFSZ, crashHandler);   /* 25:  exceeded file size limit */
300     
301     [[NSURLCache sharedURLCache] removeAllCachedResponses];
302     
303     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
304     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
305     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
306     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
307     
308     if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
309         char filenameBuffer[2048];
310         printSeparators = YES;
311         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
312             char *newLineCharacter = strchr(filenameBuffer, '\n');
313             if (newLineCharacter)
314                 *newLineCharacter = '\0';
315             
316             if (strlen(filenameBuffer) == 0)
317                 continue;
318                 
319             dumpRenderTree(filenameBuffer);
320             fflush(stdout);
321         }
322     } else {
323         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
324         for (int i = optind; i != argc; ++i) {
325             dumpRenderTree(argv[i]);
326         }
327     }
328     
329     [webView setFrameLoadDelegate:nil];
330     [webView setEditingDelegate:nil];
331     [webView setUIDelegate:nil];
332     frame = nil;
333
334     // Work around problem where registering drag types leaves an outstanding
335     // "perform selector" on the window, which retains the window. It's a bit
336     // inelegant and perhaps dangerous to just blow them all away, but in practice
337     // it probably won't cause any trouble (and this is just a test tool, after all).
338     [NSObject cancelPreviousPerformRequestsWithTarget:window];
339     
340     [window release];
341     [webView release];
342     [delegate release];
343     [editingDelegate release];
344
345     [localPasteboard releaseGlobally];
346     localPasteboard = nil;
347     
348     [navigationController release];
349     navigationController = nil;
350     
351     if (dumpPixels)
352         restoreColorSpace(0);
353     
354     [pool release];
355
356     return 0;
357 }
358
359 static void dump(void)
360 {
361     NSString *result = nil;
362     if (dumpTree) {
363         if (dumpAsText) {
364             DOMElement *documentElement = [[frame DOMDocument] documentElement];
365             if ([documentElement isKindOfClass:[DOMHTMLElement class]])
366                 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
367         } else {
368             bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
369             if (isSVGW3CTest)
370                 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
371             else 
372                 [[frame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
373             result = [frame renderTreeAsExternalRepresentation];
374         }
375         
376         if (!result)
377             printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
378         else {
379             fputs([result UTF8String], stdout);
380             if (!dumpAsText) {
381                 NSPoint scrollPosition = [[[[frame frameView] documentView] superview] bounds].origin;
382                 if (scrollPosition.x != 0 || scrollPosition.y != 0)
383                     printf("scrolled to %0.f,%0.f\n", scrollPosition.x, scrollPosition.y);
384             }
385         }
386
387         if (printSeparators)
388             puts("#EOF");
389     }
390     
391     if (dumpPixels) {
392         if (!dumpAsText) {
393             // grab a bitmap from the view
394             WebView* view = [frame webView];
395             NSSize webViewSize = [view frame].size;
396
397             CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
398
399             NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
400             NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
401             [NSGraphicsContext setCurrentContext:nsContext];
402
403             if (readFromWindow) {
404                 NSBitmapImageRep *imageRep;
405                 [view displayIfNeeded];
406                 [view lockFocus];
407                 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
408                 [view unlockFocus];
409                 [imageRep draw];
410                 [imageRep release];
411             } else if (!testRepaint)
412                 [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
413             else if (!repaintSweepHorizontally) {
414                 NSRect line = NSMakeRect(0, 0, webViewSize.width, 1);
415                 while (line.origin.y < webViewSize.height) {
416                     [view displayRectIgnoringOpacity:line inContext:nsContext];
417                     line.origin.y++;
418                 }
419             } else {
420                 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
421                 while (column.origin.x < webViewSize.width) {
422                     [view displayRectIgnoringOpacity:column inContext:nsContext];
423                     column.origin.x++;
424                 }
425             }
426             if (dumpSelectionRect) {
427                 NSView *documentView = [[frame frameView] documentView];
428                 if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
429                     [[NSColor redColor] set];
430                     [NSBezierPath strokeRect:[documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil]];
431                 }
432             }
433
434             [NSGraphicsContext setCurrentContext:savedContext];
435             
436             CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
437             CGContextRelease(cgContext);
438
439             // compute the actual hash to compare to the expected image's hash
440             NSString *actualHash = md5HashStringForBitmap(bitmapImage);
441             printf("\nActualHash: %s\n", [actualHash UTF8String]);
442
443             BOOL dumpImage;
444             if (dumpAllPixels)
445                 dumpImage = YES;
446             else {
447                 // FIXME: It's unfortunate that we hardcode the file naming scheme here.
448                 // At one time, the perl script had all the knowledge about file layout.
449                 // Some day we should restore that setup by passing in more parameters to this tool.
450                 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
451                 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
452                 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
453                 NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
454
455                 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
456
457                 /// send the image to stdout if the hash mismatches or if there's no file in the file system
458                 dumpImage = ![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0;
459             }
460             
461             if (dumpImage) {
462                 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
463                 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
464                 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
465                 CGImageDestinationFinalize(imageDest);
466                 CFRelease(imageDest);
467                 printf("Content-length: %lu\n", CFDataGetLength(imageData));
468                 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
469                 CFRelease(imageData);
470             }
471
472             CGImageRelease(bitmapImage);
473         }
474
475         printf("#EOF\n");
476     }
477
478     done = YES;
479 }
480
481 @implementation WaitUntilDoneDelegate
482
483 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
484 {
485     if ([dataSource webFrame] == frame) {
486         if (waitToDump)
487             readyToDump = YES;
488         else
489             dump();
490     }
491 }
492
493 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
494 {
495     if (frame == f)
496         readyToDump = NO;
497
498     windowIsKey = YES;
499     NSView *documentView = [[frame frameView] documentView];
500     [[[frame webView] window] makeFirstResponder:documentView];
501     if ([documentView isKindOfClass:[WebHTMLView class]])
502         [(WebHTMLView *)documentView _updateFocusState];
503 }
504
505 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
506 {
507     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
508 }
509
510 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
511 {
512     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
513     [navigationController webView:sender didFinishLoadForFrame:frame];
514 }
515
516 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
517 {
518     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
519 }
520
521 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
522
523     LayoutTestController *ltc = [[LayoutTestController alloc] init];
524     [obj setValue:ltc forKey:@"layoutTestController"];
525     [ltc release];
526     
527     EventSendingController *esc = [[EventSendingController alloc] init];
528     [obj setValue:esc forKey:@"eventSender"];
529     [esc release];
530     
531     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
532     [obj setValue:tic forKey:@"textInputController"];
533     [tic release];
534     
535     AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
536     [obj setValue:asc forKey:@"appleScriptController"];
537     [asc release];
538     
539     [obj setValue:navigationController forKey:@"navigationController"];
540     
541     ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
542     [obj setValue:plugin forKey:@"objCPlugin"];
543     [plugin release];
544     
545     ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
546     [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
547     [pluginFunction release];
548 }
549
550 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
551 {
552     printf("ALERT: %s\n", [message UTF8String]);
553 }
554
555 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
556 {
557     if (dumpTitleChanges)
558         printf("TITLE CHANGED: %s\n", [title UTF8String]);
559 }
560
561 - (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
562 {
563     // A new drag was started before the old one ended.  Probably shouldn't happen.
564     if (draggingInfo) {
565         [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
566         [draggingInfo release];
567     }
568     draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
569 }
570
571 - (void)webViewFocus:(WebView *)webView
572 {
573     windowIsKey = YES;
574     NSView *documentView = [[frame frameView] documentView];
575     if ([documentView isKindOfClass:[WebHTMLView class]])
576         [(WebHTMLView *)documentView _updateFocusState];
577 }
578
579 @end
580
581 @implementation LayoutTestController
582
583 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
584 {
585     if (aSelector == @selector(waitUntilDone)
586             || aSelector == @selector(notifyDone)
587             || aSelector == @selector(dumpAsText)
588             || aSelector == @selector(dumpTitleChanges)
589             || aSelector == @selector(setWindowIsKey:)
590             || aSelector == @selector(setMainFrameIsFirstResponder:)
591             || aSelector == @selector(dumpSelectionRect)
592             || aSelector == @selector(display)
593             || aSelector == @selector(testRepaint)
594             || aSelector == @selector(repaintSweepHorizontally))
595         return NO;
596     return YES;
597 }
598
599 + (NSString *)webScriptNameForSelector:(SEL)aSelector
600 {
601     if (aSelector == @selector(setWindowIsKey:))
602         return @"setWindowIsKey";
603     if (aSelector == @selector(setMainFrameIsFirstResponder:))
604         return @"setMainFrameIsFirstResponder";
605     return nil;
606 }
607
608 - (void)waitUntilDone 
609 {
610     waitToDump = YES;
611 }
612
613 - (void)notifyDone
614 {
615     if (waitToDump && readyToDump)
616         dump();
617     waitToDump = NO;
618 }
619
620 - (void)dumpAsText
621 {
622     dumpAsText = YES;
623 }
624
625 - (void)dumpSelectionRect
626 {
627     dumpSelectionRect = YES;
628 }
629
630 - (void)dumpTitleChanges
631 {
632     dumpTitleChanges = YES;
633 }
634
635 - (void)setWindowIsKey:(BOOL)flag
636 {
637     windowIsKey = flag;
638     NSView *documentView = [[frame frameView] documentView];
639     if ([documentView isKindOfClass:[WebHTMLView class]])
640         [(WebHTMLView *)documentView _updateFocusState];
641 }
642
643 - (void)setMainFrameIsFirstResponder:(BOOL)flag
644 {
645     NSView *documentView = [[frame frameView] documentView];
646     
647     NSResponder *firstResponder = flag ? documentView : nil;
648     [[[frame webView] window] makeFirstResponder:firstResponder];
649         
650     if ([documentView isKindOfClass:[WebHTMLView class]])
651         [(WebHTMLView *)documentView _updateFocusState];
652 }
653
654 - (void)display
655 {
656     NSView *webView = [frame webView];
657     [webView display];
658     [webView lockFocus];
659     [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
660     NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
661     [webView unlockFocus];
662     readFromWindow = YES;
663 }
664
665 - (void)testRepaint
666 {
667     testRepaint = YES;
668 }
669
670 - (void)repaintSweepHorizontally
671 {
672     repaintSweepHorizontally = YES;
673 }
674
675 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
676 {
677     return nil;
678 }
679
680 @end
681
682 static void dumpRenderTree(const char *pathOrURL)
683 {
684     CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
685     if (!pathOrURLString) {
686         fprintf(stderr, "can't parse filename as UTF-8\n");
687         return;
688     }
689
690     CFURLRef URL;
691     if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
692         URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
693     else
694         URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
695     
696     if (!URL) {
697         CFRelease(pathOrURLString);
698         fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
699         return;
700     }
701
702     done = NO;
703     readyToDump = NO;
704     waitToDump = NO;
705     dumpAsText = NO;
706     dumpSelectionRect = NO;
707     dumpTitleChanges = NO;
708     readFromWindow = NO;
709     testRepaint = testRepaintDefault;
710     repaintSweepHorizontally = repaintSweepHorizontallyDefault;
711     if (currentTest != nil)
712         CFRelease(currentTest);
713     currentTest = (NSString *)pathOrURLString;
714
715     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
716     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
717     CFRelease(URL);
718     [pool release];
719     while (!done) {
720         pool = [[NSAutoreleasePool alloc] init];
721         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
722         [pool release];
723     }
724     pool = [[NSAutoreleasePool alloc] init];
725     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
726     if (draggingInfo)
727         [draggingInfo release];
728     draggingInfo = nil;
729     [pool release];
730 }
731
732 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
733 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
734 {
735     MD5_CTX md5Context;
736     unsigned char hash[16];
737     
738     unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
739     assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
740     unsigned bytesPerPixel = bitsPerPixel / 8;
741     unsigned pixelsHigh = CGImageGetHeight(bitmap);
742     unsigned pixelsWide = CGImageGetWidth(bitmap);
743     unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
744     assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
745     
746     MD5_Init(&md5Context);
747     unsigned char *bitmapData = screenCaptureBuffer;
748     for (unsigned row = 0; row < pixelsHigh; row++) {
749         MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
750         bitmapData += bytesPerRow;
751     }
752     MD5_Final(hash, &md5Context);
753     
754     char hex[33] = "";
755     for (int i = 0; i < 16; i++) {
756        snprintf(hex, 33, "%s%02x", hex, hash[i]);
757     }
758
759     return [NSString stringWithUTF8String:hex];
760 }
761
762 @implementation DumpRenderTreePasteboard
763
764 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
765 + (NSPasteboard *)generalPasteboard
766 {
767     return localPasteboard;
768 }
769
770 @end
771
772 @implementation DumpRenderTreeWindow
773
774 - (BOOL)isKeyWindow
775 {
776     return windowIsKey;
777 }
778
779 @end