cd682e6deef27df35d1bd4f369611a3fa681b095
[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/WebCoreStatistics.h>
32 #import <WebKit/WebDataSource.h>
33 #import <WebKit/WebEditingDelegate.h>
34 #import <WebKit/WebFrameView.h>
35 #import <WebKit/WebPreferences.h>
36 #import <WebKit/WebView.h>
37 #import <WebKit/WebHTMLViewPrivate.h>
38
39 #import <Carbon/Carbon.h>                           // for GetCurrentEventTime()
40 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
41 #import <objc/objc-runtime.h>                       // for class_poseAs
42
43 #define COMMON_DIGEST_FOR_OPENSSL
44 #import <CommonCrypto/CommonDigest.h>               // for MD5 functions
45
46 #import <getopt.h>
47
48 #import "TextInputController.h"
49 #import "NavigationController.h"
50 #import "AppleScriptController.h"
51
52 @interface DumpRenderTreePasteboard : NSPasteboard
53 @end
54
55 @interface WaitUntilDoneDelegate : NSObject
56 @end
57
58 @interface EditingDelegate : NSObject
59 @end
60
61 @interface LayoutTestController : NSObject
62 @end
63
64 @interface EventSendingController : NSObject
65 {
66     NSPoint last;
67     BOOL down;
68     int clickCount;
69     NSTimeInterval lastClick;
70 }
71
72 @end
73
74 static void dumpRenderTree(const char *filename);
75 static NSString *md5HashStringForBitmap(NSBitmapImageRep *bitmap);
76
77 static volatile BOOL done;
78 static WebFrame *frame;
79 static NavigationController *navigationController;
80 static BOOL readyToDump;
81 static BOOL waitToDump;
82 static BOOL dumpAsText;
83 static BOOL dumpTitleChanges;
84 static int dumpPixels = NO;
85 static int dumpTree = YES;
86 static BOOL printSeparators;
87 static NSString *currentTest = nil;
88 static NSPasteboard *localPasteboard;
89
90 static CMProfileRef currentColorProfile = 0;
91 static void restoreColorSpace(int ignored)
92 {
93     if (currentColorProfile) {
94         int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
95         if (error)
96             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);
97         currentColorProfile = 0;
98     }
99 }
100
101 static void setDefaultColorProfileToRGB(void)
102 {
103     CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
104     CMProfileRef previousProfile;
105     int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
106     if (error) {
107         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);
108         return;
109     }
110     if (previousProfile == genericProfile)
111         return;
112     CFStringRef previousProfileName;
113     CFStringRef genericProfileName;
114     CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
115     CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
116     
117     fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n",
118         CFStringGetCStringPtr(previousProfileName, kCFStringEncodingMacRoman),
119         CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman));
120     fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
121     fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
122     
123     if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
124         fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result.  (Error: %i)",
125             CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman), error);
126     else {
127         currentColorProfile = previousProfile;
128         signal(SIGINT, restoreColorSpace);
129         signal(SIGHUP, restoreColorSpace);
130         signal(SIGTERM, restoreColorSpace);
131     }
132     CFRelease(genericProfileName);
133     CFRelease(previousProfileName);
134 }
135
136 int main(int argc, const char *argv[])
137 {
138     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
139     
140     [NSApplication sharedApplication];
141
142     class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
143     
144     struct option options[] = {
145         {"pixel-tests", no_argument, &dumpPixels, YES},
146         {"tree", no_argument, &dumpTree, YES},
147         {"notree", no_argument, &dumpTree, NO},
148         {NULL, 0, NULL, 0}
149     };
150
151     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
152     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
153     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
154     // 2 is the "Medium" font smoothing mode
155     [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
156
157     [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
158     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
159     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
160     
161     [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
162     
163     WebPreferences *preferences = [WebPreferences standardPreferences];
164     
165     [preferences setStandardFontFamily:@"Times"];
166     [preferences setFixedFontFamily:@"Courier"];
167     [preferences setSerifFontFamily:@"Times"];
168     [preferences setSansSerifFontFamily:@"Helvetica"];
169     [preferences setCursiveFontFamily:@"Apple Chancery"];
170     [preferences setFantasyFontFamily:@"Papyrus"];
171     [preferences setDefaultFontSize:16];
172     [preferences setDefaultFixedFontSize:13];
173     [preferences setMinimumFontSize:9];
174     [preferences setJavaEnabled:NO];
175
176     int option;
177     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
178         switch (option) {
179             case '?':   // unknown or ambiguous option
180             case ':':   // missing argument
181                 exit(1);
182                 break;
183         }
184     
185     if ([[[NSFontManager sharedFontManager] availableMembersOfFontFamily:@"Ahem"] count] == 0) {
186         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");
187         exit(1);
188     }
189     
190     if (dumpPixels)
191         setDefaultColorProfileToRGB();
192     
193     localPasteboard = [NSPasteboard pasteboardWithUniqueName];
194     navigationController = [[NavigationController alloc] init];
195
196     WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)];
197     WaitUntilDoneDelegate *delegate = [[WaitUntilDoneDelegate alloc] init];
198     EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
199     [webView setFrameLoadDelegate:delegate];
200     [webView setEditingDelegate:editingDelegate];
201     [webView setUIDelegate:delegate];
202     frame = [webView mainFrame];
203     
204     [webView setContinuousSpellCheckingEnabled:YES];
205     
206     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
207     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
208     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
209     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
210     
211     if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
212         char filenameBuffer[2048];
213         printSeparators = YES;
214         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
215             char *newLineCharacter = strchr(filenameBuffer, '\n');
216             if (newLineCharacter)
217                 *newLineCharacter = '\0';
218             
219             if (strlen(filenameBuffer) == 0)
220                 continue;
221                 
222             dumpRenderTree(filenameBuffer);
223             fflush(stdout);
224         }
225     } else {
226         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
227         for (int i = optind; i != argc; ++i) {
228             dumpRenderTree(argv[i]);
229         }
230     }
231     
232     [webView setFrameLoadDelegate:nil];
233     [webView setEditingDelegate:nil];
234     [webView setUIDelegate:nil];
235     frame = nil;
236
237     [webView release];
238     [delegate release];
239     [editingDelegate release];
240
241     [localPasteboard releaseGlobally];
242     localPasteboard = nil;
243     
244     [navigationController release];
245     navigationController = nil;
246     
247     if (dumpPixels)
248         restoreColorSpace(0);
249     
250     [pool release];
251
252     return 0;
253 }
254
255 static void dump(void)
256 {
257     NSString *result = nil;
258     if (dumpTree) {
259         if (dumpAsText) {
260             DOMElement *documentElement = [[frame DOMDocument] documentElement];
261             if ([documentElement isKindOfClass:[DOMHTMLElement class]])
262                 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
263         } else {
264             bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
265             if (isSVGW3CTest)
266                 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
267             else 
268                 [[frame webView] setFrameSize:NSMakeSize(800, 600)];
269             result = [frame renderTreeAsExternalRepresentation];
270         }
271         
272         if (!result)
273             printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
274         else
275             fputs([result UTF8String], stdout);
276         
277         if (printSeparators)
278             puts("#EOF");
279     }
280     
281     if (dumpPixels) {
282         if (!dumpAsText) {
283             // FIXME: It's unfortunate that we hardcode the file naming scheme here.
284             // At one time, the perl script had all the knowledge about file layout.
285             // Some day we should restore that setup by passing in more parameters to this tool.
286
287             NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
288             NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
289             NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
290             NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
291             
292             // grab a bitmap from the view
293             WebView *view = [frame webView];
294             NSBitmapImageRep *imageRep = [view bitmapImageRepForCachingDisplayInRect:[view frame]];
295             [view cacheDisplayInRect:[view frame] toBitmapImageRep:imageRep];
296             
297             // has the actual hash to compare to the expected image's hash
298             NSString *actualHash = md5HashStringForBitmap(imageRep);
299             printf("\nActualHash: %s\n", [actualHash UTF8String]);
300             printf("BaselineHash: %s\n", [baselineHash UTF8String]);
301             
302             // if the hashes don't match, send image back to stdout for diff comparision
303             if (![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0) {            
304                 NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
305                 printf("Content-length: %d\n", [imageData length]);
306                 fwrite([imageData bytes], 1, [imageData length], stdout);
307             }
308         }
309
310         printf("#EOF\n");
311     }
312
313     done = YES;
314 }
315
316 @implementation WaitUntilDoneDelegate
317
318 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
319 {
320     if ([dataSource webFrame] == frame) {
321         if (waitToDump)
322             readyToDump = YES;
323         else
324             dump();
325     }
326 }
327
328 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
329 {
330     if (frame == f)
331         readyToDump = NO;
332         
333     if ([[[frame frameView] documentView] isKindOfClass:[WebHTMLView class]]) {
334         [(WebHTMLView *)[[frame frameView] documentView] _setWindowHasFocus:YES];
335         [(WebHTMLView *)[[frame frameView] documentView] _setDisplaysWithFocusAttributes:YES];
336     }
337 }
338
339 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
340 {
341     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
342 }
343
344 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
345 {
346     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
347     [navigationController webView:sender didFinishLoadForFrame:frame];
348 }
349
350 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
351 {
352     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
353 }
354
355 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
356
357     LayoutTestController *ltc = [[LayoutTestController alloc] init];
358     [obj setValue:ltc forKey:@"layoutTestController"];
359     [ltc release];
360     EventSendingController *esc = [[EventSendingController alloc] init];
361     [obj setValue:esc forKey:@"eventSender"];
362     [esc release];
363     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
364     [obj setValue:tic forKey:@"textInputController"];
365     [tic release];
366     AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
367     [obj setValue:asc forKey:@"appleScriptController"];
368     [asc release];
369     [obj setValue:navigationController forKey:@"navigationController"];
370 }
371
372 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
373 {
374     printf("ALERT: %s\n", [message UTF8String]);
375 }
376
377 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
378 {
379     if (dumpTitleChanges)
380         printf("TITLE CHANGED: %s\n", [title UTF8String]);
381 }
382
383 @end
384
385 @interface DOMNode (dumpPath)
386 - (NSString *)dumpPath;
387 @end
388
389 @implementation DOMNode (dumpPath)
390 - (NSString *)dumpPath
391 {
392     DOMNode *parent = [self parentNode];
393     NSString *str = [NSString stringWithFormat:@"%@", [self nodeName]];
394     if (parent != nil) {
395         str = [str stringByAppendingString:@" > "];
396         str = [str stringByAppendingString:[parent dumpPath]];
397     }
398     return str;
399 }
400 @end
401
402 @interface DOMRange (dump)
403 - (NSString *)dump;
404 @end
405
406 @implementation DOMRange (dump)
407 - (NSString *)dump
408 {
409     return [NSString stringWithFormat:@"range from %ld of %@ to %ld of %@", [self startOffset], [[self startContainer] dumpPath], [self endOffset], [[self endContainer] dumpPath]];
410 }
411 @end
412
413
414 @implementation EditingDelegate
415
416 - (BOOL)webView:(WebView *)webView shouldBeginEditingInDOMRange:(DOMRange *)range
417 {
418     printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", [[range dump] UTF8String]);
419     return YES;
420 }
421
422 - (BOOL)webView:(WebView *)webView shouldEndEditingInDOMRange:(DOMRange *)range
423 {
424     printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", [[range dump] UTF8String]);
425     return YES;
426 }
427
428 - (BOOL)webView:(WebView *)webView shouldInsertNode:(DOMNode *)node replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
429 {
430     static const char *insertactionstring[] = {
431         "WebViewInsertActionTyped",
432         "WebViewInsertActionPasted",
433         "WebViewInsertActionDropped",
434     };
435
436     printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", [[node dumpPath] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
437     return YES;
438 }
439
440 - (BOOL)webView:(WebView *)webView shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
441 {
442     static const char *insertactionstring[] = {
443         "WebViewInsertActionTyped",
444         "WebViewInsertActionPasted",
445         "WebViewInsertActionDropped",
446     };
447
448     printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n", [[text description] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
449     return YES;
450 }
451
452 - (BOOL)webView:(WebView *)webView shouldDeleteDOMRange:(DOMRange *)range
453 {
454     printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", [[range dump] UTF8String]);
455     return YES;
456 }
457
458 - (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag
459 {
460     static const char *affinitystring[] = {
461         "NSSelectionAffinityUpstream",
462         "NSSelectionAffinityDownstream"
463     };
464     static const char *boolstring[] = {
465         "FALSE",
466         "TRUE"
467     };
468
469     printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n", [[currentRange dump] UTF8String], [[proposedRange dump] UTF8String], affinitystring[selectionAffinity], boolstring[flag]);
470     return YES;
471 }
472
473 - (BOOL)webView:(WebView *)webView shouldApplyStyle:(DOMCSSStyleDeclaration *)style toElementsInDOMRange:(DOMRange *)range
474 {
475     printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n", [[style description] UTF8String], [[range dump] UTF8String]);
476     return YES;
477 }
478
479 - (BOOL)webView:(WebView *)webView shouldChangeTypingStyle:(DOMCSSStyleDeclaration *)currentStyle toStyle:(DOMCSSStyleDeclaration *)proposedStyle
480 {
481     printf("EDITING DELEGATE: shouldChangeTypingStyle:%s toStyle:%s\n", [[currentStyle description] UTF8String], [[proposedStyle description] UTF8String]);
482     return YES;
483 }
484
485 - (void)webViewDidBeginEditing:(NSNotification *)notification
486 {
487     printf("EDITING DELEGATE: webViewDidBeginEditing:%s\n", [[notification name] UTF8String]);
488 }
489
490 - (void)webViewDidChange:(NSNotification *)notification
491 {
492     printf("EDITING DELEGATE: webViewDidChange:%s\n", [[notification name] UTF8String]);
493 }
494
495 - (void)webViewDidEndEditing:(NSNotification *)notification
496 {
497     printf("EDITING DELEGATE: webViewDidEndEditing:%s\n", [[notification name] UTF8String]);
498 }
499
500 - (void)webViewDidChangeTypingStyle:(NSNotification *)notification
501 {
502     printf("EDITING DELEGATE: webViewDidChangeTypingStyle:%s\n", [[notification name] UTF8String]);
503 }
504
505 - (void)webViewDidChangeSelection:(NSNotification *)notification
506 {
507     if (!done)
508         printf("EDITING DELEGATE: webViewDidChangeSelection:%s\n", [[notification name] UTF8String]);
509 }
510
511 @end
512
513 @implementation LayoutTestController
514
515 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
516 {
517     if (aSelector == @selector(waitUntilDone)
518             || aSelector == @selector(notifyDone)
519             || aSelector == @selector(dumpAsText)
520             || aSelector == @selector(dumpTitleChanges)
521             || aSelector == @selector(setWindowHasFocus:)
522             || aSelector == @selector(setDisplaysWithFocusAttributes:))
523         return NO;
524     return YES;
525 }
526
527 + (NSString *)webScriptNameForSelector:(SEL)aSelector
528 {
529     if (aSelector == @selector(setWindowHasFocus:))
530         return @"setWindowHasFocus";
531     if (aSelector == @selector(setDisplaysWithFocusAttributes:))
532         return @"setDisplaysWithFocusAttributes";
533     return nil;
534 }
535
536 - (void)waitUntilDone 
537 {
538     waitToDump = YES;
539 }
540
541 - (void)notifyDone
542 {
543     if (waitToDump && readyToDump)
544         dump();
545     waitToDump = NO;
546 }
547
548 - (void)dumpAsText
549 {
550     dumpAsText = YES;
551 }
552
553 - (void)dumpTitleChanges
554 {
555     dumpTitleChanges = YES;
556 }
557
558 - (void)setWindowHasFocus:(BOOL)flag
559 {
560     if ([[[frame frameView] documentView] isKindOfClass:[WebHTMLView class]])
561         [(WebHTMLView *)[[frame frameView] documentView] _setWindowHasFocus:flag];
562 }
563
564 - (void)setDisplaysWithFocusAttributes:(BOOL)flag
565 {
566     if ([[[frame frameView] documentView] isKindOfClass:[WebHTMLView class]])
567         [(WebHTMLView *)[[frame frameView] documentView] _setDisplaysWithFocusAttributes:flag];
568 }
569
570 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
571 {
572     return nil;
573 }
574
575 @end
576
577 @implementation EventSendingController
578
579 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
580 {
581     if (aSelector == @selector(mouseDown)
582             || aSelector == @selector(mouseUp)
583             || aSelector == @selector(mouseMoveToX:Y:))
584         return NO;
585     return YES;
586 }
587
588 + (NSString *)webScriptNameForSelector:(SEL)aSelector
589 {
590     if(aSelector == @selector(mouseMoveToX:Y:))
591         return @"mouseMoveTo";
592     return nil;
593 }
594
595 - (id)init
596 {
597     last = NSMakePoint(0, 0);
598     down = NO;
599     clickCount = 0;
600     lastClick = 0;
601     return self;
602 }
603
604 - (void)mouseDown
605 {
606     [[[frame frameView] documentView] layout];
607     if (GetCurrentEventTime() - lastClick >= 1)
608         clickCount = 1;
609     else
610         clickCount++;
611     NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:clickCount pressure:nil];
612
613     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
614     if (subView) {
615         [subView mouseDown:event];
616         down = YES;
617     }
618 }
619
620 - (void)mouseUp
621 {
622     [[[frame frameView] documentView] layout];
623     NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:clickCount pressure:nil];
624
625     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
626     if (subView) {
627         [subView mouseUp:event];
628         down = NO;
629         lastClick = [event timestamp];
630     }
631 }
632
633 - (void)mouseMoveToX:(int)x Y:(int)y
634 {
635     last = NSMakePoint(x, [[frame webView] frame].size.height - y);
636     NSEvent *event = [NSEvent mouseEventWithType:(down ? NSLeftMouseDragged : NSMouseMoved) location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:(down ? clickCount : 0) pressure:nil];
637
638     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
639     if (subView) {
640         if (down)
641             [subView mouseDragged:event];
642         else
643             [subView mouseMoved:event];
644     }
645 }
646
647 @end
648
649 static void dumpRenderTree(const char *filename)
650 {
651     CFStringRef filenameString = CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
652     if (!filenameString) {
653         fprintf(stderr, "can't parse filename as UTF-8\n");
654         return;
655     }
656
657     CFURLRef URL = CFURLCreateWithFileSystemPath(NULL, filenameString, kCFURLPOSIXPathStyle, FALSE);
658     if (!URL) {
659         CFRelease(filenameString);
660         fprintf(stderr, "can't turn %s into a CFURL\n", filename);
661         return;
662     }
663
664     done = NO;
665     readyToDump = NO;
666     waitToDump = NO;
667     dumpAsText = NO;
668     dumpTitleChanges = NO;
669     if (currentTest != nil)
670         CFRelease(currentTest);
671     currentTest = (NSString *)filenameString;
672
673     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
674     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
675     CFRelease(URL);
676     [pool release];
677     while (!done) {
678         pool = [[NSAutoreleasePool alloc] init];
679         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
680         [pool release];
681     }
682     pool = [[NSAutoreleasePool alloc] init];
683     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
684     [pool release];
685 }
686
687 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
688 static NSString *md5HashStringForBitmap(NSBitmapImageRep *bitmap)
689 {
690     MD5_CTX md5Context;
691     unsigned char hash[16];
692
693     MD5_Init(&md5Context);
694     MD5_Update(&md5Context, [bitmap bitmapData], [bitmap bytesPerPlane]);
695     MD5_Final(hash, &md5Context);
696     
697     char hex[33] = "";
698     for (int i = 0; i < 16; i++) {
699        snprintf(hex, 33, "%s%02x", hex, hash[i]);
700     }
701
702     return [NSString stringWithUTF8String:hex];
703 }
704
705 @implementation DumpRenderTreePasteboard
706
707 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
708 + (NSPasteboard *)generalPasteboard
709 {
710     return localPasteboard;
711 }
712
713 @end