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/WebFramePrivate.h>
38 #import <WebKit/WebFrameView.h>
39 #import <WebKit/WebHistory.h>
40 #import <WebKit/WebPreferences.h>
41 #import <WebKit/WebView.h>
42 #import <WebKit/WebHTMLViewPrivate.h>
43 #import <WebKit/WebDocumentPrivate.h>
44 #import <WebKit/WebPluginDatabase.h>
45 #import <WebKit/WebHistoryItemPrivate.h>
47 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
48 #import <objc/objc-runtime.h> // for class_poseAs
50 #define COMMON_DIGEST_FOR_OPENSSL
51 #import <CommonCrypto/CommonDigest.h> // for MD5 functions
54 #import <malloc/malloc.h>
56 #import "AppleScriptController.h"
57 #import "DumpRenderTreeDraggingInfo.h"
58 #import "EditingDelegate.h"
59 #import "EventSendingController.h"
60 #import "GCController.h"
61 #import "NavigationController.h"
62 #import "ObjCPlugin.h"
63 #import "ObjCPluginFunction.h"
64 #import "TextInputController.h"
66 @interface DumpRenderTreeWindow : NSWindow
69 @interface DumpRenderTreePasteboard : NSPasteboard
72 @interface DumpRenderTreeEvent : NSEvent
75 @interface WaitUntilDoneDelegate : NSObject
78 @interface LayoutTestController : NSObject
81 static void runTest(const char *pathOrURL);
82 static NSString *md5HashStringForBitmap(CGImageRef bitmap);
85 DumpRenderTreeDraggingInfo *draggingInfo = 0;
87 static volatile BOOL done;
88 static NavigationController *navigationController;
90 // Deciding when it's OK to dump out the state is a bit tricky. All these must be true:
91 // - There is no load in progress
92 // - There is no work queued up (see workQueue var, below)
93 // - waitToDump==NO. This means either waitUntilDone was never called, or it was called
94 // and notifyDone was called subsequently.
95 // Note that the call to notifyDone and the end of the load can happen in either order.
97 // This is the topmost frame that is loading, during a given load, or nil when no load is
98 // in progress. Usually this is the same as the main frame, but not always. In the case
99 // where a frameset is loaded, and then new content is loaded into one of the child frames,
100 // that child frame is the "topmost frame that is loading".
101 static WebFrame *topLoadingFrame; // !nil iff a load is in progress
102 static BOOL waitToDump; // TRUE if waitUntilDone() has been called, but notifyDone() has not yet been called
104 static BOOL dumpAsText;
105 static BOOL dumpSelectionRect;
106 static BOOL dumpTitleChanges;
107 static BOOL dumpBackForwardList;
108 static BOOL dumpChildFrameScrollPositions;
109 static int dumpPixels = NO;
110 static int dumpAllPixels = NO;
111 static BOOL readFromWindow = NO;
112 static int testRepaintDefault = NO;
113 static BOOL testRepaint = NO;
114 static int repaintSweepHorizontallyDefault = NO;
115 static BOOL repaintSweepHorizontally = NO;
116 static int dumpTree = YES;
117 static BOOL printSeparators;
118 static NSString *currentTest = nil;
119 static NSPasteboard *localPasteboard;
120 static WebHistoryItem *prevTestBFItem = nil; // current b/f item at the end of the previous test
121 static BOOL windowIsKey = YES;
122 static unsigned char* screenCaptureBuffer;
123 static CGColorSpaceRef sharedColorSpace;
124 // a queue of NSInvocations, queued by callouts from the test, to be exec'ed when the load is done
125 static NSMutableArray *workQueue = nil;
126 // to prevent infinite loops, only the first page of a test can add to a work queue
127 // (since we may well come back to that same page)
128 BOOL workQueueFrozen;
130 const unsigned maxViewHeight = 600;
131 const unsigned maxViewWidth = 800;
133 BOOL doneLoading(void)
138 static CMProfileRef currentColorProfile = 0;
139 static void restoreColorSpace(int ignored)
141 if (currentColorProfile) {
142 int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
144 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);
145 currentColorProfile = 0;
149 static void crashHandler(int sig)
151 fprintf(stderr, "%s\n", strsignal(sig));
152 restoreColorSpace(0);
156 static void setDefaultColorProfileToRGB(void)
158 CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
159 CMProfileRef previousProfile;
160 int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
162 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);
165 if (previousProfile == genericProfile)
167 CFStringRef previousProfileName;
168 CFStringRef genericProfileName;
169 char previousProfileNameString[1024];
170 char genericProfileNameString[1024];
171 CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
172 CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
173 CFStringGetCString(previousProfileName, previousProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
174 CFStringGetCString(genericProfileName, genericProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
175 CFRelease(genericProfileName);
176 CFRelease(previousProfileName);
178 fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n", previousProfileNameString, genericProfileNameString);
179 fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
180 fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
182 if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
183 fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result. (Error: %i)",
184 genericProfileNameString, error);
186 currentColorProfile = previousProfile;
187 signal(SIGINT, restoreColorSpace);
188 signal(SIGHUP, restoreColorSpace);
189 signal(SIGTERM, restoreColorSpace);
193 static void* (*savedMalloc)(malloc_zone_t*, size_t);
194 static void* (*savedRealloc)(malloc_zone_t*, void*, size_t);
196 static void* checkedMalloc(malloc_zone_t* zone, size_t size)
198 if (size >= 0x10000000)
200 return savedMalloc(zone, size);
203 static void* checkedRealloc(malloc_zone_t* zone, void* ptr, size_t size)
205 if (size >= 0x10000000)
207 return savedRealloc(zone, ptr, size);
210 static void makeLargeMallocFailSilently(void)
212 malloc_zone_t* zone = malloc_default_zone();
213 savedMalloc = zone->malloc;
214 savedRealloc = zone->realloc;
215 zone->malloc = checkedMalloc;
216 zone->realloc = checkedRealloc;
219 int main(int argc, const char *argv[])
221 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
223 [NSApplication sharedApplication];
225 class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
226 class_poseAs(objc_getClass("DumpRenderTreeWindow"), objc_getClass("NSWindow"));
227 class_poseAs(objc_getClass("DumpRenderTreeEvent"), objc_getClass("NSEvent"));
229 struct option options[] = {
230 {"dump-all-pixels", no_argument, &dumpAllPixels, YES},
231 {"horizontal-sweep", no_argument, &repaintSweepHorizontallyDefault, YES},
232 {"notree", no_argument, &dumpTree, NO},
233 {"pixel-tests", no_argument, &dumpPixels, YES},
234 {"repaint", no_argument, &testRepaintDefault, YES},
235 {"tree", no_argument, &dumpTree, YES},
239 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
240 [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
241 [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
242 // 2 is the "Medium" font smoothing mode
243 [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
245 [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
246 [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
247 [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
249 [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
251 WebPreferences *preferences = [WebPreferences standardPreferences];
253 [preferences setStandardFontFamily:@"Times"];
254 [preferences setFixedFontFamily:@"Courier"];
255 [preferences setSerifFontFamily:@"Times"];
256 [preferences setSansSerifFontFamily:@"Helvetica"];
257 [preferences setCursiveFontFamily:@"Apple Chancery"];
258 [preferences setFantasyFontFamily:@"Papyrus"];
259 [preferences setDefaultFontSize:16];
260 [preferences setDefaultFixedFontSize:13];
261 [preferences setMinimumFontSize:9];
262 [preferences setJavaEnabled:NO];
263 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
266 while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
268 case '?': // unknown or ambiguous option
269 case ':': // missing argument
274 if ([[[NSFontManager sharedFontManager] availableMembersOfFontFamily:@"Ahem"] count] == 0) {
275 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");
280 setDefaultColorProfileToRGB();
281 screenCaptureBuffer = malloc(maxViewHeight * maxViewWidth * 4);
282 sharedColorSpace = CGColorSpaceCreateDeviceRGB();
285 localPasteboard = [NSPasteboard pasteboardWithUniqueName];
286 navigationController = [[NavigationController alloc] init];
288 NSRect rect = NSMakeRect(0, 0, maxViewWidth, maxViewHeight);
290 WebView *webView = [[WebView alloc] initWithFrame:rect];
291 WaitUntilDoneDelegate *delegate = [[WaitUntilDoneDelegate alloc] init];
292 EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
293 [webView setFrameLoadDelegate:delegate];
294 [webView setEditingDelegate:editingDelegate];
295 [webView setUIDelegate:delegate];
296 frame = [webView mainFrame];
298 [[webView preferences] setTabsToLinks:YES];
300 NSString *pwd = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
301 [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
302 [[WebPluginDatabase installedPlugins] refresh];
304 // The back/forward cache is causing problems due to layouts during transition from one page to another.
305 // So, turn it off for now, but we might want to turn it back on some day.
306 [[webView backForwardList] setPageCacheSize:0];
308 // 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.
309 // Put it at -10000, -10000 in "flipped coordinates", since WebCore and the DOM use flipped coordinates.
310 NSRect windowRect = NSOffsetRect(rect, -10000, [[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
311 NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
312 [[window contentView] addSubview:webView];
313 [window orderBack:nil];
314 [window setAutodisplay:NO];
316 workQueue = [[NSMutableArray alloc] init];
318 [webView setContinuousSpellCheckingEnabled:YES];
320 makeLargeMallocFailSilently();
322 signal(SIGILL, crashHandler); /* 4: illegal instruction (not reset when caught) */
323 signal(SIGTRAP, crashHandler); /* 5: trace trap (not reset when caught) */
324 signal(SIGEMT, crashHandler); /* 7: EMT instruction */
325 signal(SIGFPE, crashHandler); /* 8: floating point exception */
326 signal(SIGBUS, crashHandler); /* 10: bus error */
327 signal(SIGSEGV, crashHandler); /* 11: segmentation violation */
328 signal(SIGSYS, crashHandler); /* 12: bad argument to system call */
329 signal(SIGPIPE, crashHandler); /* 13: write on a pipe with no reader */
330 signal(SIGXCPU, crashHandler); /* 24: exceeded CPU time limit */
331 signal(SIGXFSZ, crashHandler); /* 25: exceeded file size limit */
333 [[NSURLCache sharedURLCache] removeAllCachedResponses];
335 // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
336 // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
337 NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
338 [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
340 if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
341 char filenameBuffer[2048];
342 printSeparators = YES;
343 while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
344 char *newLineCharacter = strchr(filenameBuffer, '\n');
345 if (newLineCharacter)
346 *newLineCharacter = '\0';
348 if (strlen(filenameBuffer) == 0)
351 runTest(filenameBuffer);
355 printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
356 for (int i = optind; i != argc; ++i)
362 [webView setFrameLoadDelegate:nil];
363 [webView setEditingDelegate:nil];
364 [webView setUIDelegate:nil];
367 // Work around problem where registering drag types leaves an outstanding
368 // "perform selector" on the window, which retains the window. It's a bit
369 // inelegant and perhaps dangerous to just blow them all away, but in practice
370 // it probably won't cause any trouble (and this is just a test tool, after all).
371 [NSObject cancelPreviousPerformRequestsWithTarget:window];
373 [window close]; // releases when closed
376 [editingDelegate release];
378 [localPasteboard releaseGlobally];
379 localPasteboard = nil;
381 [navigationController release];
382 navigationController = nil;
385 restoreColorSpace(0);
392 static int compareHistoryItems(id item1, id item2, void *context)
394 return [[item1 target] caseInsensitiveCompare:[item2 target]];
397 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
404 for (int i = start; i < indent; i++)
406 printf("%s", [[item URLString] UTF8String]);
407 NSString *target = [item target];
408 if (target && [target length] > 0)
409 printf(" (in frame \"%s\")", [target UTF8String]);
410 if ([item isTargetItem])
411 printf(" **nav target**");
413 NSArray *kids = [item children];
415 // must sort to eliminate arbitrary result ordering which defeats reproducible testing
416 kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
417 for (unsigned i = 0; i < [kids count]; i++)
418 dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
422 static void dumpFrameScrollPosition(WebFrame *f)
424 NSPoint scrollPosition = [[[[f frameView] documentView] superview] bounds].origin;
425 if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
426 if ([f parentFrame] != nil)
427 printf("frame '%s' ", [[f name] UTF8String]);
428 printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
431 if (dumpChildFrameScrollPositions) {
432 NSArray *kids = [f childFrames];
434 for (unsigned i = 0; i < [kids count]; i++)
435 dumpFrameScrollPosition([kids objectAtIndex:i]);
439 static void dump(void)
441 NSString *result = nil;
443 dumpAsText |= [[[[frame dataSource] response] MIMEType] isEqualToString:@"text/plain"];
445 DOMElement *documentElement = [[frame DOMDocument] documentElement];
446 if ([documentElement isKindOfClass:[DOMHTMLElement class]])
447 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
449 result = [[documentElement valueForKey:@"textContent"] stringByAppendingString:@"\n"];
451 bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
453 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
455 [[frame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
456 result = [frame renderTreeAsExternalRepresentation];
460 printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
462 fputs([result UTF8String], stdout);
464 dumpFrameScrollPosition(frame);
467 if (dumpBackForwardList) {
468 printf("\n============== Back Forward List ==============\n");
469 WebBackForwardList *bfList = [[frame webView] backForwardList];
471 // Print out all items in the list after prevTestBFItem, which was from the previous test
472 // Gather items from the end of the list, the print them out from oldest to newest
473 NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
474 for (int i = [bfList forwardListCount]; i > 0; i--) {
475 WebHistoryItem *item = [bfList itemAtIndex:i];
476 // something is wrong if the item from the last test is in the forward part of the b/f list
477 assert(item != prevTestBFItem);
478 [itemsToPrint addObject:item];
481 assert([bfList currentItem] != prevTestBFItem);
482 [itemsToPrint addObject:[bfList currentItem]];
483 int currentItemIndex = [itemsToPrint count] - 1;
485 for (int i = -1; i >= -[bfList backListCount]; i--) {
486 WebHistoryItem *item = [bfList itemAtIndex:i];
487 if (item == prevTestBFItem)
489 [itemsToPrint addObject:item];
492 for (int i = [itemsToPrint count]-1; i >= 0; i--) {
493 dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
495 [itemsToPrint release];
496 printf("===============================================\n");
505 // grab a bitmap from the view
506 WebView* view = [frame webView];
507 NSSize webViewSize = [view frame].size;
509 CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
511 NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
512 NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
513 [NSGraphicsContext setCurrentContext:nsContext];
515 if (readFromWindow) {
516 NSBitmapImageRep *imageRep;
517 [view displayIfNeeded];
519 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
523 } else if (!testRepaint)
524 [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
525 else if (!repaintSweepHorizontally) {
526 NSRect line = NSMakeRect(0, 0, webViewSize.width, 1);
527 while (line.origin.y < webViewSize.height) {
528 [view displayRectIgnoringOpacity:line inContext:nsContext];
532 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
533 while (column.origin.x < webViewSize.width) {
534 [view displayRectIgnoringOpacity:column inContext:nsContext];
538 if (dumpSelectionRect) {
539 NSView *documentView = [[frame frameView] documentView];
540 if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
541 [[NSColor redColor] set];
542 [NSBezierPath strokeRect:[documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil]];
546 [NSGraphicsContext setCurrentContext:savedContext];
548 CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
549 CGContextRelease(cgContext);
551 // compute the actual hash to compare to the expected image's hash
552 NSString *actualHash = md5HashStringForBitmap(bitmapImage);
553 printf("\nActualHash: %s\n", [actualHash UTF8String]);
559 // FIXME: It's unfortunate that we hardcode the file naming scheme here.
560 // At one time, the perl script had all the knowledge about file layout.
561 // Some day we should restore that setup by passing in more parameters to this tool.
562 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
563 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
564 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
565 NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
567 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
569 /// send the image to stdout if the hash mismatches or if there's no file in the file system
570 dumpImage = ![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0;
574 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
575 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
576 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
577 CGImageDestinationFinalize(imageDest);
578 CFRelease(imageDest);
579 printf("Content-length: %lu\n", CFDataGetLength(imageData));
580 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
581 CFRelease(imageData);
584 CGImageRelease(bitmapImage);
593 @implementation WaitUntilDoneDelegate
595 // Exec messages in the work queue until they're all done, or one of them starts a new load
596 - (void)processWork:(id)dummy
598 // quit doing work once a load is in progress
599 while ([workQueue count] > 0 && !topLoadingFrame) {
600 [[workQueue objectAtIndex:0] invoke];
601 [workQueue removeObjectAtIndex:0];
604 // if we didn't start a new load, then we finished all the commands, so we're ready to dump state
605 if (!topLoadingFrame && !waitToDump)
609 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
611 if ([dataSource webFrame] == topLoadingFrame) {
612 topLoadingFrame = nil;
613 workQueueFrozen = YES; // first complete load freezes the queue for the rest of this test
615 if ([workQueue count] > 0)
616 [self performSelector:@selector(processWork:) withObject:nil afterDelay:0];
623 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)f
625 // Make sure we only set this once per test. If it gets cleared, and then set again, we might
626 // end up doing two dumps for one test.
627 if (!topLoadingFrame && !done)
631 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
634 NSView *documentView = [[frame frameView] documentView];
635 [[[frame webView] window] makeFirstResponder:documentView];
636 if ([documentView isKindOfClass:[WebHTMLView class]])
637 [(WebHTMLView *)documentView _updateActiveState];
640 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
642 [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
645 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
647 [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
648 [navigationController webView:sender didFinishLoadForFrame:frame];
651 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
653 [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
656 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj
658 LayoutTestController *ltc = [[LayoutTestController alloc] init];
659 [obj setValue:ltc forKey:@"layoutTestController"];
662 EventSendingController *esc = [[EventSendingController alloc] init];
663 [obj setValue:esc forKey:@"eventSender"];
666 TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
667 [obj setValue:tic forKey:@"textInputController"];
670 AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
671 [obj setValue:asc forKey:@"appleScriptController"];
674 GCController *gcc = [[GCController alloc] init];
675 [obj setValue:gcc forKey:@"GCController"];
678 [obj setValue:navigationController forKey:@"navigationController"];
680 ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
681 [obj setValue:plugin forKey:@"objCPlugin"];
684 ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
685 [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
686 [pluginFunction release];
689 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
691 printf("ALERT: %s\n", [message UTF8String]);
694 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
696 if (dumpTitleChanges)
697 printf("TITLE CHANGED: %s\n", [title UTF8String]);
700 - (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
702 // A new drag was started before the old one ended. Probably shouldn't happen.
704 [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
705 [draggingInfo release];
707 draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
710 - (void)webViewFocus:(WebView *)webView
713 NSView *documentView = [[frame frameView] documentView];
714 if ([documentView isKindOfClass:[WebHTMLView class]])
715 [(WebHTMLView *)documentView _updateActiveState];
720 @implementation LayoutTestController
722 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
724 if (aSelector == @selector(waitUntilDone)
725 || aSelector == @selector(notifyDone)
726 || aSelector == @selector(dumpAsText)
727 || aSelector == @selector(dumpTitleChanges)
728 || aSelector == @selector(dumpBackForwardList)
729 || aSelector == @selector(dumpChildFrameScrollPositions)
730 || aSelector == @selector(setWindowIsKey:)
731 || aSelector == @selector(setMainFrameIsFirstResponder:)
732 || aSelector == @selector(dumpSelectionRect)
733 || aSelector == @selector(display)
734 || aSelector == @selector(testRepaint)
735 || aSelector == @selector(repaintSweepHorizontally)
736 || aSelector == @selector(queueBackNavigation:)
737 || aSelector == @selector(queueForwardNavigation:)
738 || aSelector == @selector(queueReload)
739 || aSelector == @selector(queueScript:)
740 || aSelector == @selector(queueLoad:target:)
741 || aSelector == @selector(clearBackForwardList)
742 || aSelector == @selector(keepWebHistory)
743 || aSelector == @selector(setAcceptsEditing:))
748 + (NSString *)webScriptNameForSelector:(SEL)aSelector
750 if (aSelector == @selector(setWindowIsKey:))
751 return @"setWindowIsKey";
752 if (aSelector == @selector(setMainFrameIsFirstResponder:))
753 return @"setMainFrameIsFirstResponder";
754 if (aSelector == @selector(queueBackNavigation:))
755 return @"queueBackNavigation";
756 if (aSelector == @selector(queueForwardNavigation:))
757 return @"queueForwardNavigation";
758 if (aSelector == @selector(queueScript:))
759 return @"queueScript";
760 if (aSelector == @selector(queueLoad:target:))
762 if (aSelector == @selector(setAcceptsEditing:))
763 return @"setAcceptsEditing";
767 - (void)clearBackForwardList
769 WebBackForwardList *backForwardList = [[frame webView] backForwardList];
770 WebHistoryItem *item = [[backForwardList currentItem] retain];
772 // We clear the history by setting the back/forward list's capacity to 0
773 // then restoring it back and adding back the current item.
774 int capacity = [backForwardList capacity];
775 [backForwardList setCapacity:0];
776 [backForwardList setCapacity:capacity];
777 [backForwardList addItem:item];
778 [backForwardList goToItem:item];
782 - (void)keepWebHistory
784 if (![WebHistory optionalSharedHistory]) {
785 WebHistory *history = [[WebHistory alloc] init];
786 [WebHistory setOptionalSharedHistory:history];
791 - (void)waitUntilDone
798 if (waitToDump && !topLoadingFrame && [workQueue count] == 0)
808 - (void)dumpSelectionRect
810 dumpSelectionRect = YES;
813 - (void)dumpTitleChanges
815 dumpTitleChanges = YES;
818 - (void)dumpBackForwardList
820 dumpBackForwardList = YES;
823 - (void)dumpChildFrameScrollPositions
825 dumpChildFrameScrollPositions = YES;
828 - (void)setWindowIsKey:(BOOL)flag
831 NSView *documentView = [[frame frameView] documentView];
832 if ([documentView isKindOfClass:[WebHTMLView class]])
833 [(WebHTMLView *)documentView _updateActiveState];
836 - (void)setMainFrameIsFirstResponder:(BOOL)flag
838 NSView *documentView = [[frame frameView] documentView];
840 NSResponder *firstResponder = flag ? documentView : nil;
841 [[[frame webView] window] makeFirstResponder:firstResponder];
843 if ([documentView isKindOfClass:[WebHTMLView class]])
844 [(WebHTMLView *)documentView _updateActiveState];
849 NSView *webView = [frame webView];
852 [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
853 NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
854 [webView unlockFocus];
855 readFromWindow = YES;
863 - (void)repaintSweepHorizontally
865 repaintSweepHorizontally = YES;
868 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
873 - (void)_addWorkForTarget:(id)target selector:(SEL)selector arg1:(id)arg1 arg2:(id)arg2
877 NSMethodSignature *sig = [target methodSignatureForSelector:selector];
878 NSInvocation *work = [NSInvocation invocationWithMethodSignature:sig];
879 [work retainArguments];
880 [work setTarget:target];
881 [work setSelector:selector];
883 [work setArgument:&arg1 atIndex:2];
885 [work setArgument:&arg2 atIndex:3];
887 [workQueue addObject:work];
890 - (void)_doLoad:(NSURL *)url target:(NSString *)target
892 WebFrame *targetFrame;
893 if (target && ![target isKindOfClass:[WebUndefined class]])
894 targetFrame = [frame findFrameNamed:target];
897 [targetFrame loadRequest:[NSURLRequest requestWithURL:url]];
900 - (void)_doBackOrForwardNavigation:(NSNumber *)index
902 int bfIndex = [index intValue];
904 [[frame webView] goForward];
906 [[frame webView] goBack];
908 WebBackForwardList *bfList = [[frame webView] backForwardList];
909 [[frame webView] goToBackForwardItem:[bfList itemAtIndex:bfIndex]];
913 - (void)queueBackNavigation:(int)howFarBack
915 [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:-howFarBack] arg2:nil];
918 - (void)queueForwardNavigation:(int)howFarForward
920 [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:howFarForward] arg2:nil];
925 [self _addWorkForTarget:[frame webView] selector:@selector(reload:) arg1:self arg2:nil];
928 - (void)queueScript:(NSString *)script
930 [self _addWorkForTarget:[frame webView] selector:@selector(stringByEvaluatingJavaScriptFromString:) arg1:script arg2:nil];
933 - (void)queueLoad:(NSString *)URLString target:(NSString *)target
935 NSURL *URL = [NSURL URLWithString:URLString relativeToURL:[[[frame dataSource] response] URL]];
936 [self _addWorkForTarget:self selector:@selector(_doLoad:target:) arg1:URL arg2:target];
939 - (void)setAcceptsEditing:(BOOL)newAcceptsEditing
941 [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
946 static void runTest(const char *pathOrURL)
948 CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
949 if (!pathOrURLString) {
950 fprintf(stderr, "can't parse filename as UTF-8\n");
955 if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
956 URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
958 URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
961 CFRelease(pathOrURLString);
962 fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
966 [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:YES];
968 topLoadingFrame = nil;
971 dumpSelectionRect = NO;
972 dumpTitleChanges = NO;
973 dumpBackForwardList = NO;
975 testRepaint = testRepaintDefault;
976 repaintSweepHorizontally = repaintSweepHorizontallyDefault;
977 if ([WebHistory optionalSharedHistory])
978 [WebHistory setOptionalSharedHistory:nil];
979 lastMousePosition = NSMakePoint(0, 0);
981 if (currentTest != nil)
982 CFRelease(currentTest);
983 currentTest = (NSString *)pathOrURLString;
984 [prevTestBFItem release];
985 prevTestBFItem = [[[[frame webView] backForwardList] currentItem] retain];
986 [workQueue removeAllObjects];
987 workQueueFrozen = NO;
989 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
990 [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
994 pool = [[NSAutoreleasePool alloc] init];
995 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
998 pool = [[NSAutoreleasePool alloc] init];
999 [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1001 [draggingInfo release];
1006 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
1007 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
1010 unsigned char hash[16];
1012 unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
1013 assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
1014 unsigned bytesPerPixel = bitsPerPixel / 8;
1015 unsigned pixelsHigh = CGImageGetHeight(bitmap);
1016 unsigned pixelsWide = CGImageGetWidth(bitmap);
1017 unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
1018 assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
1020 MD5_Init(&md5Context);
1021 unsigned char *bitmapData = screenCaptureBuffer;
1022 for (unsigned row = 0; row < pixelsHigh; row++) {
1023 MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
1024 bitmapData += bytesPerRow;
1026 MD5_Final(hash, &md5Context);
1029 for (int i = 0; i < 16; i++) {
1030 snprintf(hex, 33, "%s%02x", hex, hash[i]);
1033 return [NSString stringWithUTF8String:hex];
1036 @implementation DumpRenderTreePasteboard
1038 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
1039 + (NSPasteboard *)generalPasteboard
1041 return localPasteboard;
1046 @implementation DumpRenderTreeWindow
1055 @implementation DumpRenderTreeEvent
1057 + (NSPoint)mouseLocation
1059 return [[[frame webView] window] convertBaseToScreen:lastMousePosition];