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);
463 // FIXME: Buildbots seem to scroll to (-2,0) on a number of tests, but we can't reproduce locally.
464 // Once we fix that issue, we can turn this back on.
467 dumpFrameScrollPosition(frame);
471 if (dumpBackForwardList) {
472 printf("\n============== Back Forward List ==============\n");
473 WebBackForwardList *bfList = [[frame webView] backForwardList];
475 // Print out all items in the list after prevTestBFItem, which was from the previous test
476 // Gather items from the end of the list, the print them out from oldest to newest
477 NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
478 for (int i = [bfList forwardListCount]; i > 0; i--) {
479 WebHistoryItem *item = [bfList itemAtIndex:i];
480 // something is wrong if the item from the last test is in the forward part of the b/f list
481 assert(item != prevTestBFItem);
482 [itemsToPrint addObject:item];
485 assert([bfList currentItem] != prevTestBFItem);
486 [itemsToPrint addObject:[bfList currentItem]];
487 int currentItemIndex = [itemsToPrint count] - 1;
489 for (int i = -1; i >= -[bfList backListCount]; i--) {
490 WebHistoryItem *item = [bfList itemAtIndex:i];
491 if (item == prevTestBFItem)
493 [itemsToPrint addObject:item];
496 for (int i = [itemsToPrint count]-1; i >= 0; i--) {
497 dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
499 [itemsToPrint release];
500 printf("===============================================\n");
509 // grab a bitmap from the view
510 WebView* view = [frame webView];
511 NSSize webViewSize = [view frame].size;
513 CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
515 NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
516 NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
517 [NSGraphicsContext setCurrentContext:nsContext];
519 if (readFromWindow) {
520 NSBitmapImageRep *imageRep;
521 [view displayIfNeeded];
523 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
527 } else if (!testRepaint)
528 [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
529 else if (!repaintSweepHorizontally) {
530 NSRect line = NSMakeRect(0, 0, webViewSize.width, 1);
531 while (line.origin.y < webViewSize.height) {
532 [view displayRectIgnoringOpacity:line inContext:nsContext];
536 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
537 while (column.origin.x < webViewSize.width) {
538 [view displayRectIgnoringOpacity:column inContext:nsContext];
542 if (dumpSelectionRect) {
543 NSView *documentView = [[frame frameView] documentView];
544 if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
545 [[NSColor redColor] set];
546 [NSBezierPath strokeRect:[documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil]];
550 [NSGraphicsContext setCurrentContext:savedContext];
552 CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
553 CGContextRelease(cgContext);
555 // compute the actual hash to compare to the expected image's hash
556 NSString *actualHash = md5HashStringForBitmap(bitmapImage);
557 printf("\nActualHash: %s\n", [actualHash UTF8String]);
563 // FIXME: It's unfortunate that we hardcode the file naming scheme here.
564 // At one time, the perl script had all the knowledge about file layout.
565 // Some day we should restore that setup by passing in more parameters to this tool.
566 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
567 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
568 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
569 NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
571 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
573 /// send the image to stdout if the hash mismatches or if there's no file in the file system
574 dumpImage = ![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0;
578 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
579 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
580 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
581 CGImageDestinationFinalize(imageDest);
582 CFRelease(imageDest);
583 printf("Content-length: %lu\n", CFDataGetLength(imageData));
584 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
585 CFRelease(imageData);
588 CGImageRelease(bitmapImage);
597 @implementation WaitUntilDoneDelegate
599 // Exec messages in the work queue until they're all done, or one of them starts a new load
600 - (void)processWork:(id)dummy
602 // quit doing work once a load is in progress
603 while ([workQueue count] > 0 && !topLoadingFrame) {
604 [[workQueue objectAtIndex:0] invoke];
605 [workQueue removeObjectAtIndex:0];
608 // if we didn't start a new load, then we finished all the commands, so we're ready to dump state
609 if (!topLoadingFrame && !waitToDump)
613 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
615 if ([dataSource webFrame] == topLoadingFrame) {
616 topLoadingFrame = nil;
617 workQueueFrozen = YES; // first complete load freezes the queue for the rest of this test
619 if ([workQueue count] > 0)
620 [self performSelector:@selector(processWork:) withObject:nil afterDelay:0];
627 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)f
629 // Make sure we only set this once per test. If it gets cleared, and then set again, we might
630 // end up doing two dumps for one test.
631 if (!topLoadingFrame && !done)
635 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
638 NSView *documentView = [[frame frameView] documentView];
639 [[[frame webView] window] makeFirstResponder:documentView];
640 if ([documentView isKindOfClass:[WebHTMLView class]])
641 [(WebHTMLView *)documentView _updateActiveState];
644 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
646 [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
649 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
651 [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
652 [navigationController webView:sender didFinishLoadForFrame:frame];
655 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
657 [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
660 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj
662 LayoutTestController *ltc = [[LayoutTestController alloc] init];
663 [obj setValue:ltc forKey:@"layoutTestController"];
666 EventSendingController *esc = [[EventSendingController alloc] init];
667 [obj setValue:esc forKey:@"eventSender"];
670 TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
671 [obj setValue:tic forKey:@"textInputController"];
674 AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
675 [obj setValue:asc forKey:@"appleScriptController"];
678 GCController *gcc = [[GCController alloc] init];
679 [obj setValue:gcc forKey:@"GCController"];
682 [obj setValue:navigationController forKey:@"navigationController"];
684 ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
685 [obj setValue:plugin forKey:@"objCPlugin"];
688 ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
689 [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
690 [pluginFunction release];
693 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
695 printf("ALERT: %s\n", [message UTF8String]);
698 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
700 if (dumpTitleChanges)
701 printf("TITLE CHANGED: %s\n", [title UTF8String]);
704 - (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
706 // A new drag was started before the old one ended. Probably shouldn't happen.
708 [[draggingInfo draggingSource] draggedImage:[draggingInfo draggedImage] endedAt:lastMousePosition operation:NSDragOperationNone];
709 [draggingInfo release];
711 draggingInfo = [[DumpRenderTreeDraggingInfo alloc] initWithImage:anImage offset:initialOffset pasteboard:pboard source:sourceObj];
714 - (void)webViewFocus:(WebView *)webView
717 NSView *documentView = [[frame frameView] documentView];
718 if ([documentView isKindOfClass:[WebHTMLView class]])
719 [(WebHTMLView *)documentView _updateActiveState];
724 @implementation LayoutTestController
726 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
728 if (aSelector == @selector(waitUntilDone)
729 || aSelector == @selector(notifyDone)
730 || aSelector == @selector(dumpAsText)
731 || aSelector == @selector(dumpTitleChanges)
732 || aSelector == @selector(dumpBackForwardList)
733 || aSelector == @selector(dumpChildFrameScrollPositions)
734 || aSelector == @selector(setWindowIsKey:)
735 || aSelector == @selector(setMainFrameIsFirstResponder:)
736 || aSelector == @selector(dumpSelectionRect)
737 || aSelector == @selector(display)
738 || aSelector == @selector(testRepaint)
739 || aSelector == @selector(repaintSweepHorizontally)
740 || aSelector == @selector(queueBackNavigation:)
741 || aSelector == @selector(queueForwardNavigation:)
742 || aSelector == @selector(queueReload)
743 || aSelector == @selector(queueScript:)
744 || aSelector == @selector(queueLoad:target:)
745 || aSelector == @selector(clearBackForwardList)
746 || aSelector == @selector(keepWebHistory)
747 || aSelector == @selector(setAcceptsEditing:))
752 + (NSString *)webScriptNameForSelector:(SEL)aSelector
754 if (aSelector == @selector(setWindowIsKey:))
755 return @"setWindowIsKey";
756 if (aSelector == @selector(setMainFrameIsFirstResponder:))
757 return @"setMainFrameIsFirstResponder";
758 if (aSelector == @selector(queueBackNavigation:))
759 return @"queueBackNavigation";
760 if (aSelector == @selector(queueForwardNavigation:))
761 return @"queueForwardNavigation";
762 if (aSelector == @selector(queueScript:))
763 return @"queueScript";
764 if (aSelector == @selector(queueLoad:target:))
766 if (aSelector == @selector(setAcceptsEditing:))
767 return @"setAcceptsEditing";
771 - (void)clearBackForwardList
773 WebBackForwardList *backForwardList = [[frame webView] backForwardList];
774 WebHistoryItem *item = [[backForwardList currentItem] retain];
776 // We clear the history by setting the back/forward list's capacity to 0
777 // then restoring it back and adding back the current item.
778 int capacity = [backForwardList capacity];
779 [backForwardList setCapacity:0];
780 [backForwardList setCapacity:capacity];
781 [backForwardList addItem:item];
782 [backForwardList goToItem:item];
786 - (void)keepWebHistory
788 if (![WebHistory optionalSharedHistory]) {
789 WebHistory *history = [[WebHistory alloc] init];
790 [WebHistory setOptionalSharedHistory:history];
795 - (void)waitUntilDone
802 if (waitToDump && !topLoadingFrame && [workQueue count] == 0)
812 - (void)dumpSelectionRect
814 dumpSelectionRect = YES;
817 - (void)dumpTitleChanges
819 dumpTitleChanges = YES;
822 - (void)dumpBackForwardList
824 dumpBackForwardList = YES;
827 - (void)dumpChildFrameScrollPositions
829 dumpChildFrameScrollPositions = YES;
832 - (void)setWindowIsKey:(BOOL)flag
835 NSView *documentView = [[frame frameView] documentView];
836 if ([documentView isKindOfClass:[WebHTMLView class]])
837 [(WebHTMLView *)documentView _updateActiveState];
840 - (void)setMainFrameIsFirstResponder:(BOOL)flag
842 NSView *documentView = [[frame frameView] documentView];
844 NSResponder *firstResponder = flag ? documentView : nil;
845 [[[frame webView] window] makeFirstResponder:firstResponder];
847 if ([documentView isKindOfClass:[WebHTMLView class]])
848 [(WebHTMLView *)documentView _updateActiveState];
853 NSView *webView = [frame webView];
856 [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
857 NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
858 [webView unlockFocus];
859 readFromWindow = YES;
867 - (void)repaintSweepHorizontally
869 repaintSweepHorizontally = YES;
872 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
877 - (void)_addWorkForTarget:(id)target selector:(SEL)selector arg1:(id)arg1 arg2:(id)arg2
881 NSMethodSignature *sig = [target methodSignatureForSelector:selector];
882 NSInvocation *work = [NSInvocation invocationWithMethodSignature:sig];
883 [work retainArguments];
884 [work setTarget:target];
885 [work setSelector:selector];
887 [work setArgument:&arg1 atIndex:2];
889 [work setArgument:&arg2 atIndex:3];
891 [workQueue addObject:work];
894 - (void)_doLoad:(NSURL *)url target:(NSString *)target
896 WebFrame *targetFrame;
897 if (target && ![target isKindOfClass:[WebUndefined class]])
898 targetFrame = [frame findFrameNamed:target];
901 [targetFrame loadRequest:[NSURLRequest requestWithURL:url]];
904 - (void)_doBackOrForwardNavigation:(NSNumber *)index
906 int bfIndex = [index intValue];
908 [[frame webView] goForward];
910 [[frame webView] goBack];
912 WebBackForwardList *bfList = [[frame webView] backForwardList];
913 [[frame webView] goToBackForwardItem:[bfList itemAtIndex:bfIndex]];
917 - (void)queueBackNavigation:(int)howFarBack
919 [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:-howFarBack] arg2:nil];
922 - (void)queueForwardNavigation:(int)howFarForward
924 [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:howFarForward] arg2:nil];
929 [self _addWorkForTarget:[frame webView] selector:@selector(reload:) arg1:self arg2:nil];
932 - (void)queueScript:(NSString *)script
934 [self _addWorkForTarget:[frame webView] selector:@selector(stringByEvaluatingJavaScriptFromString:) arg1:script arg2:nil];
937 - (void)queueLoad:(NSString *)URLString target:(NSString *)target
939 NSURL *URL = [NSURL URLWithString:URLString relativeToURL:[[[frame dataSource] response] URL]];
940 [self _addWorkForTarget:self selector:@selector(_doLoad:target:) arg1:URL arg2:target];
943 - (void)setAcceptsEditing:(BOOL)newAcceptsEditing
945 [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
950 static void runTest(const char *pathOrURL)
952 CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
953 if (!pathOrURLString) {
954 fprintf(stderr, "can't parse filename as UTF-8\n");
959 if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
960 URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
962 URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
965 CFRelease(pathOrURLString);
966 fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
970 [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:YES];
972 topLoadingFrame = nil;
975 dumpChildFrameScrollPositions = NO;
976 dumpSelectionRect = NO;
977 dumpTitleChanges = NO;
978 dumpBackForwardList = NO;
980 testRepaint = testRepaintDefault;
981 repaintSweepHorizontally = repaintSweepHorizontallyDefault;
982 if ([WebHistory optionalSharedHistory])
983 [WebHistory setOptionalSharedHistory:nil];
984 lastMousePosition = NSMakePoint(0, 0);
986 if (currentTest != nil)
987 CFRelease(currentTest);
988 currentTest = (NSString *)pathOrURLString;
989 [prevTestBFItem release];
990 prevTestBFItem = [[[[frame webView] backForwardList] currentItem] retain];
991 [workQueue removeAllObjects];
992 workQueueFrozen = NO;
994 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
995 [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
999 pool = [[NSAutoreleasePool alloc] init];
1000 [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1003 pool = [[NSAutoreleasePool alloc] init];
1004 [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1006 [draggingInfo release];
1011 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
1012 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
1015 unsigned char hash[16];
1017 unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
1018 assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
1019 unsigned bytesPerPixel = bitsPerPixel / 8;
1020 unsigned pixelsHigh = CGImageGetHeight(bitmap);
1021 unsigned pixelsWide = CGImageGetWidth(bitmap);
1022 unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
1023 assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
1025 MD5_Init(&md5Context);
1026 unsigned char *bitmapData = screenCaptureBuffer;
1027 for (unsigned row = 0; row < pixelsHigh; row++) {
1028 MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
1029 bitmapData += bytesPerRow;
1031 MD5_Final(hash, &md5Context);
1034 for (int i = 0; i < 16; i++) {
1035 snprintf(hex, 33, "%s%02x", hex, hash[i]);
1038 return [NSString stringWithUTF8String:hex];
1041 @implementation DumpRenderTreePasteboard
1043 // Return a local pasteboard so we don't disturb the real pasteboard when running tests.
1044 + (NSPasteboard *)generalPasteboard
1046 return localPasteboard;
1051 @implementation DumpRenderTreeWindow
1060 @implementation DumpRenderTreeEvent
1062 + (NSPoint)mouseLocation
1064 return [[[frame webView] window] convertBaseToScreen:lastMousePosition];