2 * Copyright (C) 2005 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 <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>
38 #import <Carbon/Carbon.h> // for GetCurrentEventTime()
39 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
40 #import <objc/objc-runtime.h> // for class_poseAs
42 #define COMMON_DIGEST_FOR_OPENSSL
43 #import <CommonCrypto/CommonDigest.h> // for MD5 functions
47 #import "TextInputController.h"
48 #import "NavigationController.h"
50 @interface DumpRenderTreePasteboard : NSPasteboard
53 @interface WaitUntilDoneDelegate : NSObject
56 @interface EditingDelegate : NSObject
59 @interface LayoutTestController : NSObject
62 @interface EventSendingController : NSObject
67 NSTimeInterval lastClick;
72 static void dumpRenderTree(const char *filename);
73 static NSString *md5HashStringForBitmap(NSBitmapImageRep *bitmap);
75 static volatile BOOL done;
76 static WebFrame *frame;
77 static NavigationController *navigationController;
78 static BOOL readyToDump;
79 static BOOL waitToDump;
80 static BOOL dumpAsText;
81 static BOOL dumpTitleChanges;
82 static int dumpPixels = NO;
83 static int dumpTree = YES;
84 static BOOL printSeparators;
85 static NSString *currentTest = nil;
86 static NSPasteboard *localPasteboard;
88 static CMProfileRef currentColorProfile = 0;
89 static void restoreColorSpace(int ignored)
91 if (currentColorProfile) {
92 int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
94 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);
95 currentColorProfile = 0;
99 static void setDefaultColorProfileToRGB(void)
101 CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
102 CMProfileRef previousProfile;
103 int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
105 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 if (previousProfile == genericProfile)
110 CFStringRef previousProfileName;
111 CFStringRef genericProfileName;
112 CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
113 CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
115 fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n",
116 CFStringGetCStringPtr(previousProfileName, kCFStringEncodingMacRoman),
117 CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman));
118 fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
119 fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
121 if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
122 fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result. (Error: %i)",
123 CFStringGetCStringPtr(genericProfileName, kCFStringEncodingMacRoman), error);
125 currentColorProfile = previousProfile;
126 signal(SIGINT, restoreColorSpace);
127 signal(SIGHUP, restoreColorSpace);
128 signal(SIGTERM, restoreColorSpace);
130 CFRelease(genericProfileName);
131 CFRelease(previousProfileName);
134 int main(int argc, const char *argv[])
136 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
138 class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
140 struct option options[] = {
141 {"pixel-tests", no_argument, &dumpPixels, YES},
142 {"tree", no_argument, &dumpTree, YES},
143 {"notree", no_argument, &dumpTree, NO},
147 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
148 [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
149 [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
150 // 2 is the "Medium" font smoothing mode
151 [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
153 [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
154 [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
155 [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
157 [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
159 WebPreferences *preferences = [WebPreferences standardPreferences];
161 [preferences setStandardFontFamily:@"Times"];
162 [preferences setFixedFontFamily:@"Courier"];
163 [preferences setSerifFontFamily:@"Times"];
164 [preferences setSansSerifFontFamily:@"Helvetica"];
165 [preferences setCursiveFontFamily:@"Apple Chancery"];
166 [preferences setFantasyFontFamily:@"Papyrus"];
167 [preferences setDefaultFontSize:16];
168 [preferences setDefaultFixedFontSize:13];
169 [preferences setMinimumFontSize:9];
170 [preferences setJavaEnabled:NO];
173 while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
175 case '?': // unknown or ambiguous option
176 case ':': // missing argument
181 if ([[[NSFontManager sharedFontManager] availableMembersOfFontFamily:@"Ahem"] count] == 0) {
182 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 setDefaultColorProfileToRGB();
189 localPasteboard = [NSPasteboard pasteboardWithUniqueName];
190 navigationController = [[NavigationController alloc] init];
192 WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600)];
193 WaitUntilDoneDelegate *delegate = [[WaitUntilDoneDelegate alloc] init];
194 EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
195 [webView setFrameLoadDelegate:delegate];
196 [webView setEditingDelegate:editingDelegate];
197 [webView setUIDelegate:delegate];
198 frame = [webView mainFrame];
200 // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
201 // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
202 NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
203 [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
205 if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
206 char filenameBuffer[2048];
207 printSeparators = YES;
208 while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
209 char *newLineCharacter = strchr(filenameBuffer, '\n');
210 if (newLineCharacter)
211 *newLineCharacter = '\0';
213 if (strlen(filenameBuffer) == 0)
216 dumpRenderTree(filenameBuffer);
220 printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
221 for (int i = optind; i != argc; ++i) {
222 dumpRenderTree(argv[i]);
226 [webView setFrameLoadDelegate:nil];
227 [webView setEditingDelegate:nil];
228 [webView setUIDelegate:nil];
233 [editingDelegate release];
235 [localPasteboard releaseGlobally];
236 localPasteboard = nil;
238 [navigationController release];
239 navigationController = nil;
242 restoreColorSpace(0);
249 static void dump(void)
251 NSString *result = nil;
254 DOMElement *documentElement = [[frame DOMDocument] documentElement];
255 if ([documentElement isKindOfClass:[DOMHTMLElement class]])
256 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
258 bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
260 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
262 [[frame webView] setFrameSize:NSMakeSize(800, 600)];
263 result = [frame renderTreeAsExternalRepresentation];
267 printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
269 fputs([result UTF8String], stdout);
277 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
278 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
279 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
281 // grab a bitmap from the view
282 WebView *view = [frame webView];
283 NSBitmapImageRep *imageRep = [view bitmapImageRepForCachingDisplayInRect:[view frame]];
284 [view cacheDisplayInRect:[view frame] toBitmapImageRep:imageRep];
286 // has the actual hash to compare to the expected image's hash
287 NSString *actualHash = md5HashStringForBitmap(imageRep);
288 printf("\nActualHash: %s\n", [actualHash UTF8String]);
289 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
291 // if the hashes don't match, send image back to stdout for diff comparision
292 if ([baselineHash isEqualToString:actualHash] == NO) {
293 NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
294 printf("Content-length: %d\n", [imageData length]);
295 fwrite([imageData bytes], 1, [imageData length], stdout);
305 @implementation WaitUntilDoneDelegate
307 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
309 if ([dataSource webFrame] == frame) {
317 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
323 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
325 [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
328 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
330 [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
331 [navigationController webView:sender didFinishLoadForFrame:frame];
334 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
336 [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
339 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj
341 LayoutTestController *ltc = [[LayoutTestController alloc] init];
342 [obj setValue:ltc forKey:@"layoutTestController"];
344 EventSendingController *esc = [[EventSendingController alloc] init];
345 [obj setValue:esc forKey:@"eventSender"];
347 TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
348 [obj setValue:tic forKey:@"textInputController"];
350 [obj setValue:navigationController forKey:@"navigationController"];
353 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
355 printf("ALERT: %s\n", [message UTF8String]);
358 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
360 if (dumpTitleChanges)
361 printf("TITLE CHANGED: %s\n", [title UTF8String]);
366 @interface DOMNode (dumpPath)
367 - (NSString *)dumpPath;
370 @implementation DOMNode (dumpPath)
371 - (NSString *)dumpPath
373 DOMNode *parent = [self parentNode];
374 NSString *str = [NSString stringWithFormat:@"%@", [self nodeName]];
376 str = [str stringByAppendingString:@" > "];
377 str = [str stringByAppendingString:[parent dumpPath]];
383 @interface DOMRange (dump)
387 @implementation DOMRange (dump)
390 return [NSString stringWithFormat:@"range from %ld of %@ to %ld of %@", [self startOffset], [[self startContainer] dumpPath], [self endOffset], [[self endContainer] dumpPath]];
395 @implementation EditingDelegate
397 - (BOOL)webView:(WebView *)webView shouldBeginEditingInDOMRange:(DOMRange *)range
399 printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", [[range dump] UTF8String]);
403 - (BOOL)webView:(WebView *)webView shouldEndEditingInDOMRange:(DOMRange *)range
405 printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", [[range dump] UTF8String]);
409 - (BOOL)webView:(WebView *)webView shouldInsertNode:(DOMNode *)node replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
411 static const char *insertactionstring[] = {
412 "WebViewInsertActionTyped",
413 "WebViewInsertActionPasted",
414 "WebViewInsertActionDropped",
417 printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", [[node dumpPath] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
421 - (BOOL)webView:(WebView *)webView shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
423 static const char *insertactionstring[] = {
424 "WebViewInsertActionTyped",
425 "WebViewInsertActionPasted",
426 "WebViewInsertActionDropped",
429 printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n", [[text description] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
433 - (BOOL)webView:(WebView *)webView shouldDeleteDOMRange:(DOMRange *)range
435 printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", [[range dump] UTF8String]);
439 - (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag
441 static const char *affinitystring[] = {
442 "NSSelectionAffinityUpstream",
443 "NSSelectionAffinityDownstream"
445 static const char *boolstring[] = {
450 printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n", [[currentRange dump] UTF8String], [[proposedRange dump] UTF8String], affinitystring[selectionAffinity], boolstring[flag]);
454 - (BOOL)webView:(WebView *)webView shouldApplyStyle:(DOMCSSStyleDeclaration *)style toElementsInDOMRange:(DOMRange *)range
456 printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n", [[style description] UTF8String], [[range dump] UTF8String]);
460 - (BOOL)webView:(WebView *)webView shouldChangeTypingStyle:(DOMCSSStyleDeclaration *)currentStyle toStyle:(DOMCSSStyleDeclaration *)proposedStyle
462 printf("EDITING DELEGATE: shouldChangeTypingStyle:%s toStyle:%s\n", [[currentStyle description] UTF8String], [[proposedStyle description] UTF8String]);
466 - (void)webViewDidBeginEditing:(NSNotification *)notification
468 printf("EDITING DELEGATE: webViewDidBeginEditing:%s\n", [[notification name] UTF8String]);
471 - (void)webViewDidChange:(NSNotification *)notification
473 printf("EDITING DELEGATE: webViewDidChange:%s\n", [[notification name] UTF8String]);
476 - (void)webViewDidEndEditing:(NSNotification *)notification
478 printf("EDITING DELEGATE: webViewDidEndEditing:%s\n", [[notification name] UTF8String]);
481 - (void)webViewDidChangeTypingStyle:(NSNotification *)notification
483 printf("EDITING DELEGATE: webViewDidChangeTypingStyle:%s\n", [[notification name] UTF8String]);
486 - (void)webViewDidChangeSelection:(NSNotification *)notification
489 printf("EDITING DELEGATE: webViewDidChangeSelection:%s\n", [[notification name] UTF8String]);
494 @implementation LayoutTestController
496 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
498 if (aSelector == @selector(waitUntilDone)
499 || aSelector == @selector(notifyDone)
500 || aSelector == @selector(dumpAsText)
501 || aSelector == @selector(dumpTitleChanges))
506 - (void)waitUntilDone
513 if (waitToDump && readyToDump)
523 - (void)dumpTitleChanges
525 dumpTitleChanges = YES;
530 @implementation EventSendingController
532 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
534 if (aSelector == @selector(mouseDown)
535 || aSelector == @selector(mouseUp)
536 || aSelector == @selector(mouseMoveToX:Y:))
541 + (NSString *)webScriptNameForSelector:(SEL)aSelector
543 if(aSelector == @selector(mouseMoveToX:Y:))
544 return @"mouseMoveTo";
550 last = NSMakePoint(0, 0);
559 [[[frame frameView] documentView] layout];
560 if (GetCurrentEventTime() - lastClick >= 1)
564 NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:clickCount pressure:nil];
566 NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
568 [subView mouseDown:event];
575 [[[frame frameView] documentView] layout];
576 NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:clickCount pressure:nil];
578 NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
580 [subView mouseUp:event];
582 lastClick = [event timestamp];
586 - (void)mouseMoveToX:(int)x Y:(int)y
588 last = NSMakePoint(x, [[frame webView] frame].size.height - y);
589 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];
591 NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
594 [subView mouseDragged:event];
596 [subView mouseMoved:event];
602 static void dumpRenderTree(const char *filename)
604 CFStringRef filenameString = CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
605 if (!filenameString) {
606 fprintf(stderr, "can't parse filename as UTF-8\n");
610 CFURLRef URL = CFURLCreateWithFileSystemPath(NULL, filenameString, kCFURLPOSIXPathStyle, FALSE);
612 CFRelease(filenameString);
613 fprintf(stderr, "can't turn %s into a CFURL\n", filename);
621 dumpTitleChanges = NO;
622 if (currentTest != nil)
623 CFRelease(currentTest);
624 currentTest = (NSString *)filenameString;
626 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
627 [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
631 pool = [[NSAutoreleasePool alloc] init];
632 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
635 pool = [[NSAutoreleasePool alloc] init];
636 [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
640 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
641 static NSString *md5HashStringForBitmap(NSBitmapImageRep *bitmap)
644 unsigned char hash[16];
646 MD5_Init(&md5Context);
647 MD5_Update(&md5Context, [bitmap bitmapData], [bitmap bytesPerPlane]);
648 MD5_Final(hash, &md5Context);
651 for (int i = 0; i < 16; i++) {
652 snprintf(hex, 33, "%s%02x", hex, hash[i]);
655 return [NSString stringWithUTF8String:hex];
658 @implementation DumpRenderTreePasteboard
660 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
661 + (NSPasteboard *)generalPasteboard
663 return localPasteboard;