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