2 * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
29 #import "DumpRenderTree.h"
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>
45 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
46 #import <objc/objc-runtime.h> // for class_poseAs
48 #define COMMON_DIGEST_FOR_OPENSSL
49 #import <CommonCrypto/CommonDigest.h> // for MD5 functions
52 #import <malloc/malloc.h>
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"
64 @interface DumpRenderTreeWindow : NSWindow
67 @interface DumpRenderTreePasteboard : NSPasteboard
70 @interface DumpRenderTreeEvent : NSEvent
73 @interface WaitUntilDoneDelegate : NSObject
76 @interface LayoutTestController : NSObject
79 static void dumpRenderTree(const char *pathOrURL);
80 static NSString *md5HashStringForBitmap(CGImageRef bitmap);
83 DumpRenderTreeDraggingInfo *draggingInfo = 0;
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;
107 const unsigned maxViewHeight = 600;
108 const unsigned maxViewWidth = 800;
110 BOOL doneLoading(void)
115 static CMProfileRef currentColorProfile = 0;
116 static void restoreColorSpace(int ignored)
118 if (currentColorProfile) {
119 int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
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;
126 static void crashHandler(int sig)
128 fprintf(stderr, "%s\n", strsignal(sig));
129 restoreColorSpace(0);
133 static void setDefaultColorProfileToRGB(void)
135 CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
136 CMProfileRef previousProfile;
137 int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
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);
142 if (previousProfile == genericProfile)
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);
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");
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);
163 currentColorProfile = previousProfile;
164 signal(SIGINT, restoreColorSpace);
165 signal(SIGHUP, restoreColorSpace);
166 signal(SIGTERM, restoreColorSpace);
170 static void* (*savedMalloc)(malloc_zone_t*, size_t);
171 static void* (*savedRealloc)(malloc_zone_t*, void*, size_t);
173 static void* checkedMalloc(malloc_zone_t* zone, size_t size)
175 if (size >= 0x10000000)
177 return savedMalloc(zone, size);
180 static void* checkedRealloc(malloc_zone_t* zone, void* ptr, size_t size)
182 if (size >= 0x10000000)
184 return savedRealloc(zone, ptr, size);
187 static void makeLargeMallocFailSilently(void)
189 malloc_zone_t* zone = malloc_default_zone();
190 savedMalloc = zone->malloc;
191 savedRealloc = zone->realloc;
192 zone->malloc = checkedMalloc;
193 zone->realloc = checkedRealloc;
196 int main(int argc, const char *argv[])
198 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
200 [NSApplication sharedApplication];
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"));
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},
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"];
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"];
226 [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
228 WebPreferences *preferences = [WebPreferences standardPreferences];
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];
243 while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
245 case '?': // unknown or ambiguous option
246 case ':': // missing argument
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");
257 setDefaultColorProfileToRGB();
258 screenCaptureBuffer = malloc(maxViewHeight * maxViewWidth * 4);
259 sharedColorSpace = CGColorSpaceCreateDeviceRGB();
262 localPasteboard = [NSPasteboard pasteboardWithUniqueName];
263 navigationController = [[NavigationController alloc] init];
265 NSRect rect = NSMakeRect(0, 0, maxViewWidth, maxViewHeight);
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];
275 [[webView preferences] setTabsToLinks:YES];
277 NSString *pwd = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
278 [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
279 [[WebPluginDatabase installedPlugins] refresh];
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];
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];
293 [webView setContinuousSpellCheckingEnabled:YES];
295 makeLargeMallocFailSilently();
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 */
308 [[NSURLCache sharedURLCache] removeAllCachedResponses];
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];
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';
323 if (strlen(filenameBuffer) == 0)
326 dumpRenderTree(filenameBuffer);
330 printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
331 for (int i = optind; i != argc; ++i)
332 dumpRenderTree(argv[i]);
335 [webView setFrameLoadDelegate:nil];
336 [webView setEditingDelegate:nil];
337 [webView setUIDelegate:nil];
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];
346 [window close]; // releases when closed
349 [editingDelegate release];
351 [localPasteboard releaseGlobally];
352 localPasteboard = nil;
354 [navigationController release];
355 navigationController = nil;
358 restoreColorSpace(0);
365 static void dump(void)
367 NSString *result = nil;
369 dumpAsText |= [[[[frame dataSource] response] MIMEType] isEqualToString:@"text/plain"];
371 DOMElement *documentElement = [[frame DOMDocument] documentElement];
372 if ([documentElement isKindOfClass:[DOMHTMLElement class]])
373 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
375 result = [[documentElement valueForKey:@"textContent"] stringByAppendingString:@"\n"];
377 bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
379 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
381 [[frame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
382 result = [frame renderTreeAsExternalRepresentation];
386 printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
388 fputs([result UTF8String], stdout);
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);
402 // grab a bitmap from the view
403 WebView* view = [frame webView];
404 NSSize webViewSize = [view frame].size;
406 CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
408 NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
409 NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
410 [NSGraphicsContext setCurrentContext:nsContext];
412 if (readFromWindow) {
413 NSBitmapImageRep *imageRep;
414 [view displayIfNeeded];
416 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
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];
429 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
430 while (column.origin.x < webViewSize.width) {
431 [view displayRectIgnoringOpacity:column inContext:nsContext];
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]];
443 [NSGraphicsContext setCurrentContext:savedContext];
445 CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
446 CGContextRelease(cgContext);
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]);
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"];
464 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
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;
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);
481 CGImageRelease(bitmapImage);
490 @implementation WaitUntilDoneDelegate
492 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
494 if ([dataSource webFrame] == frame) {
502 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
508 NSView *documentView = [[frame frameView] documentView];
509 [[[frame webView] window] makeFirstResponder:documentView];
510 if ([documentView isKindOfClass:[WebHTMLView class]])
511 [(WebHTMLView *)documentView _updateActiveState];
514 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
516 [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
519 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
521 [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
522 [navigationController webView:sender didFinishLoadForFrame:frame];
525 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
527 [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
530 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj
532 LayoutTestController *ltc = [[LayoutTestController alloc] init];
533 [obj setValue:ltc forKey:@"layoutTestController"];
536 EventSendingController *esc = [[EventSendingController alloc] init];
537 [obj setValue:esc forKey:@"eventSender"];
540 TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
541 [obj setValue:tic forKey:@"textInputController"];
544 AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
545 [obj setValue:asc forKey:@"appleScriptController"];
548 GCController *gcc = [[GCController alloc] init];
549 [obj setValue:gcc forKey:@"GCController"];
552 [obj setValue:navigationController forKey:@"navigationController"];
554 ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
555 [obj setValue:plugin forKey:@"objCPlugin"];
558 ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
559 [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
560 [pluginFunction release];
563 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
565 printf("ALERT: %s\n", [message UTF8String]);
568 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
570 if (dumpTitleChanges)
571 printf("TITLE CHANGED: %s\n", [title UTF8String]);
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
576 // A new drag was started before the old one ended. Probably shouldn't happen.
578 [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
579 [draggingInfo release];
581 draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
584 - (void)webViewFocus:(WebView *)webView
587 NSView *documentView = [[frame frameView] documentView];
588 if ([documentView isKindOfClass:[WebHTMLView class]])
589 [(WebHTMLView *)documentView _updateActiveState];
594 @implementation LayoutTestController
596 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
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:))
615 + (NSString *)webScriptNameForSelector:(SEL)aSelector
617 if (aSelector == @selector(setWindowIsKey:))
618 return @"setWindowIsKey";
619 if (aSelector == @selector(setMainFrameIsFirstResponder:))
620 return @"setMainFrameIsFirstResponder";
621 if (aSelector == @selector(setAcceptsEditing:))
622 return @"setAcceptsEditing";
626 - (void)clearBackForwardList
628 WebBackForwardList *backForwardList = [[frame webView] backForwardList];
629 WebHistoryItem *item = [[backForwardList currentItem] retain];
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];
641 - (void)keepWebHistory
643 if (![WebHistory optionalSharedHistory]) {
644 WebHistory *history = [[WebHistory alloc] init];
645 [WebHistory setOptionalSharedHistory:history];
650 - (void)waitUntilDone
657 if (waitToDump && readyToDump)
667 - (void)dumpSelectionRect
669 dumpSelectionRect = YES;
672 - (void)dumpTitleChanges
674 dumpTitleChanges = YES;
677 - (void)setWindowIsKey:(BOOL)flag
680 NSView *documentView = [[frame frameView] documentView];
681 if ([documentView isKindOfClass:[WebHTMLView class]])
682 [(WebHTMLView *)documentView _updateActiveState];
685 - (void)setMainFrameIsFirstResponder:(BOOL)flag
687 NSView *documentView = [[frame frameView] documentView];
689 NSResponder *firstResponder = flag ? documentView : nil;
690 [[[frame webView] window] makeFirstResponder:firstResponder];
692 if ([documentView isKindOfClass:[WebHTMLView class]])
693 [(WebHTMLView *)documentView _updateActiveState];
698 NSView *webView = [frame webView];
701 [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
702 NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
703 [webView unlockFocus];
704 readFromWindow = YES;
712 - (void)repaintSweepHorizontally
714 repaintSweepHorizontally = YES;
717 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
722 - (void)setAcceptsEditing:(BOOL)newAcceptsEditing
724 [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
729 static void dumpRenderTree(const char *pathOrURL)
731 CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
732 if (!pathOrURLString) {
733 fprintf(stderr, "can't parse filename as UTF-8\n");
738 if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
739 URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
741 URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
744 CFRelease(pathOrURLString);
745 fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
749 [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:YES];
754 dumpSelectionRect = NO;
755 dumpTitleChanges = NO;
757 testRepaint = testRepaintDefault;
758 repaintSweepHorizontally = repaintSweepHorizontallyDefault;
759 if ([WebHistory optionalSharedHistory])
760 [WebHistory setOptionalSharedHistory:nil];
761 lastMousePosition = NSMakePoint(0, 0);
763 if (currentTest != nil)
764 CFRelease(currentTest);
765 currentTest = (NSString *)pathOrURLString;
767 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
768 [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
772 pool = [[NSAutoreleasePool alloc] init];
773 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
776 pool = [[NSAutoreleasePool alloc] init];
777 [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
779 [draggingInfo release];
784 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
785 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
788 unsigned char hash[16];
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));
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;
804 MD5_Final(hash, &md5Context);
807 for (int i = 0; i < 16; i++) {
808 snprintf(hex, 33, "%s%02x", hex, hash[i]);
811 return [NSString stringWithUTF8String:hex];
814 @implementation DumpRenderTreePasteboard
816 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
817 + (NSPasteboard *)generalPasteboard
819 return localPasteboard;
824 @implementation DumpRenderTreeWindow
833 @implementation DumpRenderTreeEvent
835 + (NSPoint)mouseLocation
837 return [[[frame webView] window] convertBaseToScreen:lastMousePosition];