2006-03-13 Eric Seidel <eseidel@apple.com>
[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 <WebKit/DOMExtensions.h>
30 #import <WebKit/DOMRange.h>
31 #import <WebKit/WebBackForwardList.h>
32 #import <WebKit/WebCoreStatistics.h>
33 #import <WebKit/WebDataSource.h>
34 #import <WebKit/WebEditingDelegate.h>
35 #import <WebKit/WebFrameView.h>
36 #import <WebKit/WebPreferences.h>
37 #import <WebKit/WebView.h>
38 #import <WebKit/WebHTMLViewPrivate.h>
39 #import <WebKit/WebPluginDatabase.h>
40
41 #import <Carbon/Carbon.h>                           // for GetCurrentEventTime()
42 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
43 #import <objc/objc-runtime.h>                       // for class_poseAs
44
45 #define COMMON_DIGEST_FOR_OPENSSL
46 #import <CommonCrypto/CommonDigest.h>               // for MD5 functions
47
48 #import <getopt.h>
49 #import <malloc/malloc.h>
50
51 #import "TextInputController.h"
52 #import "NavigationController.h"
53 #import "AppleScriptController.h"
54
55 @interface DumpRenderTreeWindow : NSWindow
56 @end
57
58 @interface DumpRenderTreeDraggingInfo : NSObject <NSDraggingInfo>
59 {
60 @private
61     NSSize offset;
62     NSImage *draggedImage;
63     NSPasteboard *draggingPasteboard;
64     id draggingSource;
65 }
66 - (id)initWithImage:(NSImage *)image offset:(NSSize)offset pasteboard:(NSPasteboard *)pasteboard source:(id)source; 
67 - (NSWindow *)draggingDestinationWindow;
68 - (NSDragOperation)draggingSourceOperationMask;
69 - (NSPoint)draggingLocation;
70 - (NSPoint)draggedImageLocation;
71 - (NSImage *)draggedImage;
72 - (NSPasteboard *)draggingPasteboard;
73 - (id)draggingSource;
74 - (int)draggingSequenceNumber;
75 - (void)slideDraggedImageTo:(NSPoint)screenPoint;
76 - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination;
77 @end
78
79 @interface DumpRenderTreePasteboard : NSPasteboard
80 @end
81
82 @interface WaitUntilDoneDelegate : NSObject
83 @end
84
85 @interface EditingDelegate : NSObject
86 @end
87
88 @interface LayoutTestController : NSObject
89 @end
90
91 @interface EventSendingController : NSObject
92 {
93     BOOL down;
94     int clickCount;
95     NSTimeInterval lastClick;
96     int eventNumber;
97     double timeOffset;
98 }
99 @end
100
101 static void dumpRenderTree(const char *pathOrURL);
102 static NSString *md5HashStringForBitmap(CGImageRef bitmap);
103
104 static volatile BOOL done;
105 static WebFrame *frame;
106 static NavigationController *navigationController;
107 static BOOL readyToDump;
108 static BOOL waitToDump;
109 static BOOL dumpAsText;
110 static BOOL dumpTitleChanges;
111 static int dumpPixels = NO;
112 static int dumpTree = YES;
113 static BOOL printSeparators;
114 static NSString *currentTest = nil;
115 static NSPasteboard *localPasteboard;
116 static BOOL windowIsKey = YES;
117 static NSPoint lastMousePosition;
118 static DumpRenderTreeDraggingInfo *draggingInfo;
119 static unsigned char* screenCaptureBuffer;
120 static CGColorSpaceRef sharedColorSpace;
121
122 const unsigned maxViewHeight = 600;
123 const unsigned maxViewWidth = 800;
124
125 static CMProfileRef currentColorProfile = 0;
126 static void restoreColorSpace(int ignored)
127 {
128     if (currentColorProfile) {
129         int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
130         if (error)
131             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);
132         currentColorProfile = 0;
133     }
134 }
135
136 static void setDefaultColorProfileToRGB(void)
137 {
138     CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
139     CMProfileRef previousProfile;
140     int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
141     if (error) {
142         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);
143         return;
144     }
145     if (previousProfile == genericProfile)
146         return;
147     CFStringRef previousProfileName;
148     CFStringRef genericProfileName;
149     CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
150     CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
151     
152     fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n",
153         CFStringGetCStringPtr(previousProfileName, kCFStringEncodingMacRoman),
154         CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman));
155     fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
156     fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
157     
158     if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
159         fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result.  (Error: %i)",
160             CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman), error);
161     else {
162         currentColorProfile = previousProfile;
163         signal(SIGINT, restoreColorSpace);
164         signal(SIGHUP, restoreColorSpace);
165         signal(SIGTERM, restoreColorSpace);
166     }
167     CFRelease(genericProfileName);
168     CFRelease(previousProfileName);
169 }
170
171 static void* (*savedMalloc)(malloc_zone_t*, size_t);
172 static void* (*savedRealloc)(malloc_zone_t*, void*, size_t);
173
174 static void* checkedMalloc(malloc_zone_t* zone, size_t size)
175 {
176     if (size >= 0x10000000)
177         return 0;
178     return savedMalloc(zone, size);
179 }
180
181 static void* checkedRealloc(malloc_zone_t* zone, void* ptr, size_t size)
182 {
183     if (size >= 0x10000000)
184         return 0;
185     return savedRealloc(zone, ptr, size);
186 }
187
188 static void makeLargeMallocFailSilently(void)
189 {
190     malloc_zone_t* zone = malloc_default_zone();
191     savedMalloc = zone->malloc;
192     savedRealloc = zone->realloc;
193     zone->malloc = checkedMalloc;
194     zone->realloc = checkedRealloc;
195 }
196
197 int main(int argc, const char *argv[])
198 {
199     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
200     
201     [NSApplication sharedApplication];
202
203     class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
204     class_poseAs(objc_getClass("DumpRenderTreeWindow"), objc_getClass("NSWindow"));
205     
206     struct option options[] = {
207         {"pixel-tests", no_argument, &dumpPixels, YES},
208         {"tree", no_argument, &dumpTree, YES},
209         {"notree", no_argument, &dumpTree, NO},
210         {NULL, 0, NULL, 0}
211     };
212
213     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
214     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
215     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
216     // 2 is the "Medium" font smoothing mode
217     [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
218
219     [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
220     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
221     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
222     
223     [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
224     
225     WebPreferences *preferences = [WebPreferences standardPreferences];
226     
227     [preferences setStandardFontFamily:@"Times"];
228     [preferences setFixedFontFamily:@"Courier"];
229     [preferences setSerifFontFamily:@"Times"];
230     [preferences setSansSerifFontFamily:@"Helvetica"];
231     [preferences setCursiveFontFamily:@"Apple Chancery"];
232     [preferences setFantasyFontFamily:@"Papyrus"];
233     [preferences setDefaultFontSize:16];
234     [preferences setDefaultFixedFontSize:13];
235     [preferences setMinimumFontSize:9];
236     [preferences setJavaEnabled: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     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     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
291     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
292     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
293     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
294     
295     if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
296         char filenameBuffer[2048];
297         printSeparators = YES;
298         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
299             char *newLineCharacter = strchr(filenameBuffer, '\n');
300             if (newLineCharacter)
301                 *newLineCharacter = '\0';
302             
303             if (strlen(filenameBuffer) == 0)
304                 continue;
305                 
306             dumpRenderTree(filenameBuffer);
307             fflush(stdout);
308         }
309     } else {
310         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
311         for (int i = optind; i != argc; ++i) {
312             dumpRenderTree(argv[i]);
313         }
314     }
315     
316     [webView setFrameLoadDelegate:nil];
317     [webView setEditingDelegate:nil];
318     [webView setUIDelegate:nil];
319     frame = nil;
320
321     // Work around problem where registering drag types leaves an outstanding
322     // "perform selector" on the window, which retains the window. It's a bit
323     // inelegant and perhaps dangerous to just blow them all away, but in practice
324     // it probably won't cause any trouble (and this is just a test tool, after all).
325     [NSObject cancelPreviousPerformRequestsWithTarget:window];
326     
327     [window release];
328     [webView release];
329     [delegate release];
330     [editingDelegate release];
331
332     [localPasteboard releaseGlobally];
333     localPasteboard = nil;
334     
335     [navigationController release];
336     navigationController = nil;
337     
338     if (dumpPixels)
339         restoreColorSpace(0);
340     
341     [pool release];
342
343     return 0;
344 }
345
346 static void dump(void)
347 {
348     NSString *result = nil;
349     if (dumpTree) {
350         if (dumpAsText) {
351             DOMElement *documentElement = [[frame DOMDocument] documentElement];
352             if ([documentElement isKindOfClass:[DOMHTMLElement class]])
353                 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
354         } else {
355             bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
356             if (isSVGW3CTest)
357                 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
358             else 
359                 [[frame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
360             result = [frame renderTreeAsExternalRepresentation];
361         }
362         
363         if (!result)
364             printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
365         else
366             fputs([result UTF8String], stdout);
367         
368         if (printSeparators)
369             puts("#EOF");
370     }
371     
372     if (dumpPixels) {
373         if (!dumpAsText) {
374             // FIXME: It's unfortunate that we hardcode the file naming scheme here.
375             // At one time, the perl script had all the knowledge about file layout.
376             // Some day we should restore that setup by passing in more parameters to this tool.
377
378             NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
379             NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
380             NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
381             NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
382             
383             // grab a bitmap from the view
384             WebView *view = [frame webView];
385             NSSize webViewSize = [[frame webView] frame].size;
386             CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
387             NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
388             [NSGraphicsContext saveGraphicsState];
389             [NSGraphicsContext setCurrentContext:nsContext];
390             [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
391             [NSGraphicsContext restoreGraphicsState];
392             
393             // has the actual hash to compare to the expected image's hash
394             CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
395             NSString *actualHash = md5HashStringForBitmap(bitmapImage);
396             printf("\nActualHash: %s\n", [actualHash UTF8String]);
397             printf("BaselineHash: %s\n", [baselineHash UTF8String]);
398             
399             // if the hashes don't match, send image back to stdout for diff comparision
400             if (![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0) {
401                 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
402                 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
403                 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
404                 CGImageDestinationFinalize(imageDest);
405                 CFRelease(imageDest);
406                 printf("Content-length: %lu\n", CFDataGetLength(imageData));
407                 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
408                 CFRelease(imageData);
409             }
410             CGImageRelease(bitmapImage);
411             CGContextRelease(cgContext);
412         }
413
414         printf("#EOF\n");
415     }
416
417     done = YES;
418 }
419
420 @implementation WaitUntilDoneDelegate
421
422 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
423 {
424     if ([dataSource webFrame] == frame) {
425         if (waitToDump)
426             readyToDump = YES;
427         else
428             dump();
429     }
430 }
431
432 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
433 {
434     if (frame == f)
435         readyToDump = NO;
436
437     windowIsKey = YES;
438     NSView *documentView = [[frame frameView] documentView];
439     [[[frame webView] window] makeFirstResponder:documentView];
440     if ([documentView isKindOfClass:[WebHTMLView class]])
441         [(WebHTMLView *)documentView _updateFocusState];
442 }
443
444 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
445 {
446     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
447 }
448
449 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
450 {
451     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
452     [navigationController webView:sender didFinishLoadForFrame:frame];
453 }
454
455 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
456 {
457     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
458 }
459
460 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
461
462     LayoutTestController *ltc = [[LayoutTestController alloc] init];
463     [obj setValue:ltc forKey:@"layoutTestController"];
464     [ltc release];
465     EventSendingController *esc = [[EventSendingController alloc] init];
466     [obj setValue:esc forKey:@"eventSender"];
467     [esc release];
468     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
469     [obj setValue:tic forKey:@"textInputController"];
470     [tic release];
471     AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
472     [obj setValue:asc forKey:@"appleScriptController"];
473     [asc release];
474     [obj setValue:navigationController forKey:@"navigationController"];
475 }
476
477 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
478 {
479     printf("ALERT: %s\n", [message UTF8String]);
480 }
481
482 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
483 {
484     if (dumpTitleChanges)
485         printf("TITLE CHANGED: %s\n", [title UTF8String]);
486 }
487
488 - (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
489 {
490     // A new drag was started before the old one ended.  Probably shouldn't happen.
491     if (draggingInfo) {
492         [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
493         [draggingInfo release];
494     }
495     draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
496 }
497
498 - (void)webViewFocus:(WebView *)webView
499 {
500     windowIsKey = YES;
501     NSView *documentView = [[frame frameView] documentView];
502     if ([documentView isKindOfClass:[WebHTMLView class]])
503         [(WebHTMLView *)documentView _updateFocusState];
504 }
505
506 @end
507
508 @interface DOMNode (dumpPath)
509 - (NSString *)dumpPath;
510 @end
511
512 @implementation DOMNode (dumpPath)
513 - (NSString *)dumpPath
514 {
515     DOMNode *parent = [self parentNode];
516     NSString *str = [NSString stringWithFormat:@"%@", [self nodeName]];
517     if (parent != nil) {
518         str = [str stringByAppendingString:@" > "];
519         str = [str stringByAppendingString:[parent dumpPath]];
520     }
521     return str;
522 }
523 @end
524
525 @interface DOMRange (dump)
526 - (NSString *)dump;
527 @end
528
529 @implementation DOMRange (dump)
530 - (NSString *)dump
531 {
532     return [NSString stringWithFormat:@"range from %ld of %@ to %ld of %@", [self startOffset], [[self startContainer] dumpPath], [self endOffset], [[self endContainer] dumpPath]];
533 }
534 @end
535
536
537 @implementation EditingDelegate
538
539 - (BOOL)webView:(WebView *)webView shouldBeginEditingInDOMRange:(DOMRange *)range
540 {
541     printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", [[range dump] UTF8String]);
542     return YES;
543 }
544
545 - (BOOL)webView:(WebView *)webView shouldEndEditingInDOMRange:(DOMRange *)range
546 {
547     printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", [[range dump] UTF8String]);
548     return YES;
549 }
550
551 - (BOOL)webView:(WebView *)webView shouldInsertNode:(DOMNode *)node replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
552 {
553     static const char *insertactionstring[] = {
554         "WebViewInsertActionTyped",
555         "WebViewInsertActionPasted",
556         "WebViewInsertActionDropped",
557     };
558
559     printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", [[node dumpPath] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
560     return YES;
561 }
562
563 - (BOOL)webView:(WebView *)webView shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
564 {
565     static const char *insertactionstring[] = {
566         "WebViewInsertActionTyped",
567         "WebViewInsertActionPasted",
568         "WebViewInsertActionDropped",
569     };
570
571     printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n", [[text description] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
572     return YES;
573 }
574
575 - (BOOL)webView:(WebView *)webView shouldDeleteDOMRange:(DOMRange *)range
576 {
577     printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", [[range dump] UTF8String]);
578     return YES;
579 }
580
581 - (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag
582 {
583     static const char *affinitystring[] = {
584         "NSSelectionAffinityUpstream",
585         "NSSelectionAffinityDownstream"
586     };
587     static const char *boolstring[] = {
588         "FALSE",
589         "TRUE"
590     };
591
592     printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n", [[currentRange dump] UTF8String], [[proposedRange dump] UTF8String], affinitystring[selectionAffinity], boolstring[flag]);
593     return YES;
594 }
595
596 - (BOOL)webView:(WebView *)webView shouldApplyStyle:(DOMCSSStyleDeclaration *)style toElementsInDOMRange:(DOMRange *)range
597 {
598     printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n", [[style description] UTF8String], [[range dump] UTF8String]);
599     return YES;
600 }
601
602 - (BOOL)webView:(WebView *)webView shouldChangeTypingStyle:(DOMCSSStyleDeclaration *)currentStyle toStyle:(DOMCSSStyleDeclaration *)proposedStyle
603 {
604     printf("EDITING DELEGATE: shouldChangeTypingStyle:%s toStyle:%s\n", [[currentStyle description] UTF8String], [[proposedStyle description] UTF8String]);
605     return YES;
606 }
607
608 - (void)webViewDidBeginEditing:(NSNotification *)notification
609 {
610     printf("EDITING DELEGATE: webViewDidBeginEditing:%s\n", [[notification name] UTF8String]);
611 }
612
613 - (void)webViewDidChange:(NSNotification *)notification
614 {
615     printf("EDITING DELEGATE: webViewDidChange:%s\n", [[notification name] UTF8String]);
616 }
617
618 - (void)webViewDidEndEditing:(NSNotification *)notification
619 {
620     printf("EDITING DELEGATE: webViewDidEndEditing:%s\n", [[notification name] UTF8String]);
621 }
622
623 - (void)webViewDidChangeTypingStyle:(NSNotification *)notification
624 {
625     printf("EDITING DELEGATE: webViewDidChangeTypingStyle:%s\n", [[notification name] UTF8String]);
626 }
627
628 - (void)webViewDidChangeSelection:(NSNotification *)notification
629 {
630     if (!done)
631         printf("EDITING DELEGATE: webViewDidChangeSelection:%s\n", [[notification name] UTF8String]);
632 }
633
634 @end
635
636 @implementation LayoutTestController
637
638 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
639 {
640     if (aSelector == @selector(waitUntilDone)
641             || aSelector == @selector(notifyDone)
642             || aSelector == @selector(dumpAsText)
643             || aSelector == @selector(dumpTitleChanges)
644             || aSelector == @selector(setWindowIsKey:)
645             || aSelector == @selector(setMainFrameIsFirstResponder:))
646         return NO;
647     return YES;
648 }
649
650 + (NSString *)webScriptNameForSelector:(SEL)aSelector
651 {
652     if (aSelector == @selector(setWindowIsKey:))
653         return @"setWindowIsKey";
654     if (aSelector == @selector(setMainFrameIsFirstResponder:))
655         return @"setMainFrameIsFirstResponder";
656     return nil;
657 }
658
659 - (void)waitUntilDone 
660 {
661     waitToDump = YES;
662 }
663
664 - (void)notifyDone
665 {
666     if (waitToDump && readyToDump)
667         dump();
668     waitToDump = NO;
669 }
670
671 - (void)dumpAsText
672 {
673     dumpAsText = YES;
674 }
675
676 - (void)dumpTitleChanges
677 {
678     dumpTitleChanges = YES;
679 }
680
681 - (void)setWindowIsKey:(BOOL)flag
682 {
683     windowIsKey = flag;
684     NSView *documentView = [[frame frameView] documentView];
685     if ([documentView isKindOfClass:[WebHTMLView class]])
686         [(WebHTMLView *)documentView _updateFocusState];
687 }
688
689 - (void)setMainFrameIsFirstResponder:(BOOL)flag
690 {
691     NSView *documentView = [[frame frameView] documentView];
692     
693     NSResponder *firstResponder = flag ? documentView : nil;
694     [[[frame webView] window] makeFirstResponder:firstResponder];
695         
696     if ([documentView isKindOfClass:[WebHTMLView class]])
697         [(WebHTMLView *)documentView _updateFocusState];
698 }
699
700 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
701 {
702     return nil;
703 }
704
705 @end
706
707 @implementation EventSendingController
708
709 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
710 {
711     if (aSelector == @selector(mouseDown)
712             || aSelector == @selector(mouseUp)
713             || aSelector == @selector(mouseClick)
714             || aSelector == @selector(mouseMoveToX:Y:)
715             || aSelector == @selector(leapForward:))
716         return NO;
717     return YES;
718 }
719
720 + (NSString *)webScriptNameForSelector:(SEL)aSelector
721 {
722     if (aSelector == @selector(mouseMoveToX:Y:))
723         return @"mouseMoveTo";
724     if (aSelector == @selector(leapForward:))
725         return @"leapForward";
726     return nil;
727 }
728
729 - (id)init
730 {
731     lastMousePosition = NSMakePoint(0, 0);
732     down = NO;
733     clickCount = 0;
734     lastClick = 0;
735     return self;
736 }
737
738 - (double)currentEventTime
739 {
740     return GetCurrentEventTime() + timeOffset;
741 }
742
743 - (void)leapForward:(int)milliseconds
744 {
745     timeOffset += milliseconds / 1000.0;
746 }
747
748 - (void)mouseDown
749 {
750     [[[frame frameView] documentView] layout];
751     if ([self currentEventTime] - lastClick >= 1)
752         clickCount = 1;
753     else
754         clickCount++;
755     NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown 
756                                         location:lastMousePosition 
757                                    modifierFlags:nil 
758                                        timestamp:[self currentEventTime]
759                                     windowNumber:[[[frame webView] window] windowNumber] 
760                                          context:[NSGraphicsContext currentContext] 
761                                      eventNumber:++eventNumber 
762                                       clickCount:clickCount 
763                                         pressure:nil];
764
765     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
766     if (subView) {
767         [subView mouseDown:event];
768         down = YES;
769     }
770 }
771
772 - (void)mouseUp
773 {
774     [[[frame frameView] documentView] layout];
775     NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp 
776                                         location:lastMousePosition 
777                                    modifierFlags:nil 
778                                        timestamp:[self currentEventTime]
779                                     windowNumber:[[[frame webView] window] windowNumber] 
780                                          context:[NSGraphicsContext currentContext] 
781                                      eventNumber:++eventNumber 
782                                       clickCount:clickCount 
783                                         pressure:nil];
784
785     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
786     if (subView) {
787         [subView mouseUp:event];
788         down = NO;
789         lastClick = [event timestamp];
790         if (draggingInfo) {
791             WebView *webView = [frame webView];
792             
793             NSDragOperation dragOperation = [webView draggingUpdated:draggingInfo];
794             
795             [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:dragOperation];
796             if (dragOperation != NSDragOperationNone)
797                 [webView performDragOperation:draggingInfo];
798             [draggingInfo release];
799             draggingInfo = nil;
800         }
801     }
802 }
803
804 - (void)mouseMoveToX:(int)x Y:(int)y
805 {
806     lastMousePosition = NSMakePoint(x, [[frame webView] frame].size.height - y);
807     NSEvent *event = [NSEvent mouseEventWithType:(down ? NSLeftMouseDragged : NSMouseMoved) 
808                                         location:lastMousePosition 
809                                    modifierFlags:nil 
810                                        timestamp:[self currentEventTime]
811                                     windowNumber:[[[frame webView] window] windowNumber] 
812                                          context:[NSGraphicsContext currentContext] 
813                                      eventNumber:++eventNumber 
814                                       clickCount:(down ? clickCount : 0) 
815                                         pressure:nil];
816
817     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
818     if (subView) {
819         if (down) {
820             [subView mouseDragged:event];
821             [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] movedTo:lastMousePosition];
822             [[frame webView] draggingUpdated:draggingInfo];
823         }
824         else
825             [subView mouseMoved:event];
826     }
827 }
828
829 - (void)mouseClick
830 {
831     [[[frame frameView] documentView] layout];
832     if ([self currentEventTime] - lastClick >= 1)
833         clickCount = 1;
834     else
835         clickCount++;
836     NSEvent *mouseDownEvent = [NSEvent mouseEventWithType:NSLeftMouseDown 
837                                         location:lastMousePosition 
838                                    modifierFlags:nil 
839                                        timestamp:[self currentEventTime]
840                                     windowNumber:[[[frame webView] window] windowNumber] 
841                                          context:[NSGraphicsContext currentContext] 
842                                      eventNumber:++eventNumber 
843                                       clickCount:clickCount 
844                                         pressure:nil];
845
846     NSView *subView = [[frame webView] hitTest:[mouseDownEvent locationInWindow]];
847     if (subView) {
848         [self leapForward:1];
849         NSEvent *mouseUpEvent = [NSEvent mouseEventWithType:NSLeftMouseUp
850                                                    location:lastMousePosition
851                                               modifierFlags:nil
852                                                   timestamp:[self currentEventTime]
853                                                windowNumber:[[[frame webView] window] windowNumber]
854                                                     context:[NSGraphicsContext currentContext]
855                                                 eventNumber:++eventNumber
856                                                  clickCount:clickCount
857                                                    pressure:nil];
858         [NSApp postEvent:mouseUpEvent atStart:NO];
859         [subView mouseDown:mouseDownEvent];
860     }
861 }
862
863 @end
864
865 static void dumpRenderTree(const char *pathOrURL)
866 {
867     CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
868     if (!pathOrURLString) {
869         fprintf(stderr, "can't parse filename as UTF-8\n");
870         return;
871     }
872
873     CFURLRef URL;
874     if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
875         URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
876     else
877         URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
878     
879     if (!URL) {
880         CFRelease(pathOrURLString);
881         fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
882         return;
883     }
884
885     done = NO;
886     readyToDump = NO;
887     waitToDump = NO;
888     dumpAsText = NO;
889     dumpTitleChanges = NO;
890     if (currentTest != nil)
891         CFRelease(currentTest);
892     currentTest = (NSString *)pathOrURLString;
893
894     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
895     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
896     CFRelease(URL);
897     [pool release];
898     while (!done) {
899         pool = [[NSAutoreleasePool alloc] init];
900         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
901         [pool release];
902     }
903     pool = [[NSAutoreleasePool alloc] init];
904     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
905     if (draggingInfo)
906         [draggingInfo release];
907     draggingInfo = nil;
908     [pool release];
909 }
910
911 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
912 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
913 {
914     MD5_CTX md5Context;
915     unsigned char hash[16];
916     
917     unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
918     assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
919     unsigned bytesPerPixel = bitsPerPixel / 8;
920     unsigned pixelsHigh = CGImageGetHeight(bitmap);
921     unsigned pixelsWide = CGImageGetWidth(bitmap);
922     unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
923     assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
924     
925     MD5_Init(&md5Context);
926     unsigned char *bitmapData = screenCaptureBuffer;
927     for (unsigned row = 0; row < pixelsHigh; row++) {
928         MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
929         bitmapData += bytesPerRow;
930     }
931     MD5_Final(hash, &md5Context);
932     
933     char hex[33] = "";
934     for (int i = 0; i < 16; i++) {
935        snprintf(hex, 33, "%s%02x", hex, hash[i]);
936     }
937
938     return [NSString stringWithUTF8String:hex];
939 }
940
941 @implementation DumpRenderTreePasteboard
942
943 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
944 + (NSPasteboard *)generalPasteboard
945 {
946     return localPasteboard;
947 }
948
949 @end
950
951 @implementation DumpRenderTreeWindow
952
953 - (BOOL)isKeyWindow
954 {
955     return windowIsKey;
956 }
957
958 @end
959
960 @implementation DumpRenderTreeDraggingInfo
961
962 - (id)initWithImage:(NSImage *)anImage offset:(NSSize)o pasteboard:(NSPasteboard *)pboard source:(id)source
963 {
964     draggedImage = [anImage retain];
965     draggingPasteboard = [pboard retain];
966     draggingSource = [source retain];
967     offset = o;
968     
969     return [super init];
970 }
971
972 - (void)dealloc
973 {
974     [draggedImage release];
975     [draggingPasteboard release];
976     [draggingSource release];
977     [super dealloc];
978 }
979
980 - (NSWindow *)draggingDestinationWindow 
981 {
982     return [[frame webView] window];
983 }
984
985 - (NSDragOperation)draggingSourceOperationMask 
986 {
987     return [draggingSource draggingSourceOperationMaskForLocal:YES];
988 }
989
990 - (NSPoint)draggingLocation
991
992     return lastMousePosition; 
993 }
994
995 - (NSPoint)draggedImageLocation 
996 {
997     return NSMakePoint(lastMousePosition.x + offset.width, lastMousePosition.y + offset.height);
998 }
999
1000 - (NSImage *)draggedImage
1001 {
1002     return draggedImage;
1003 }
1004
1005 - (NSPasteboard *)draggingPasteboard
1006 {
1007     return draggingPasteboard;
1008 }
1009
1010 - (id)draggingSource
1011 {
1012     return draggingSource;
1013 }
1014
1015 - (int)draggingSequenceNumber
1016 {
1017     NSLog(@"DumpRenderTree doesn't support draggingSequenceNumber");
1018     return 0;
1019 }
1020
1021 - (void)slideDraggedImageTo:(NSPoint)screenPoint
1022 {
1023     NSLog(@"DumpRenderTree doesn't support slideDraggedImageTo:");
1024 }
1025
1026 - (NSArray *)namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
1027 {
1028     NSLog(@"DumpRenderTree doesn't support namesOfPromisedFilesDroppedAtDestination:");
1029     return nil;
1030 }
1031
1032 @end
1033