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