Reviewed by Darin Adler.
[WebKit-https.git] / WebKitTools / DumpRenderTree / DumpRenderTree.m
1 /*
2  * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer. 
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution. 
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission. 
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import <Foundation/NSURLRequest.h>
30 #import <Foundation/NSError.h>
31
32 #import <WebKit/DOMExtensions.h>
33 #import <WebKit/DOMRange.h>
34 #import <WebKit/WebCoreStatistics.h>
35 #import <WebKit/WebDataSource.h>
36 #import <WebKit/WebFrame.h>
37 #import <WebKit/WebFrameLoadDelegate.h>
38 #import <WebKit/WebEditingDelegate.h>
39 #import <WebKit/WebFrameView.h>
40 #import <WebKit/WebPreferences.h>
41 #import <WebKit/WebView.h>
42
43 #import <Carbon/Carbon.h> // for GetCurrentEventTime()
44
45 #define COMMON_DIGEST_FOR_OPENSSL
46 #import <CommonCrypto/CommonDigest.h>
47 #import <getopt.h>
48
49 #import "TextInputController.h"
50
51 @interface WaitUntilDoneDelegate : NSObject
52 @end
53
54 @interface EditingDelegate : NSObject
55 @end
56
57 @interface LayoutTestController : NSObject
58 @end
59
60 @interface EventSendingController : NSObject
61 {
62     NSPoint last;
63     BOOL down;
64     int clickCount;
65     NSTimeInterval lastClick;
66 }
67
68 @end
69
70 static void dumpRenderTree(const char *filename);
71 static NSString *md5HashStringForBitmap(NSBitmapImageRep *bitmap);
72
73 static volatile BOOL done;
74 static WebFrame *frame;
75 static BOOL readyToDump;
76 static BOOL waitToDump;
77 static BOOL dumpAsText;
78 static BOOL dumpTitleChanges;
79 static int dumpPixels = NO;
80 static int dumpTree = YES;
81 static BOOL printSeparators;
82 static NSString *currentTest = nil;
83
84 int main(int argc, const char *argv[])
85 {
86     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
87
88     int width = 800;
89     int height = 600;
90     
91     struct option options[] = {
92         {"width", required_argument, NULL, 'w'},
93         {"height", required_argument, NULL, 'h'},
94         {"pixel-tests", no_argument, &dumpPixels, YES},
95         {"tree", no_argument, &dumpTree, YES},
96         {"notree", no_argument, &dumpTree, NO},
97         {NULL, 0, NULL, 0}
98     };
99
100     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
101     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
102     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
103     // 2 is the "Medium" font smoothing mode
104     [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
105
106     [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
107     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
108     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
109     
110     WebPreferences *preferences = [WebPreferences standardPreferences];
111     
112     [preferences setStandardFontFamily:@"Times"];
113     [preferences setFixedFontFamily:@"Courier"];
114     [preferences setSerifFontFamily:@"Times"];
115     [preferences setSansSerifFontFamily:@"Helvetica"];
116     [preferences setCursiveFontFamily:@"Apple Chancery"];
117     [preferences setFantasyFontFamily:@"Papyrus"];
118     [preferences setDefaultFontSize:16];
119     [preferences setDefaultFixedFontSize:13];
120     [preferences setMinimumFontSize:9];
121     [preferences setJavaEnabled:NO];
122
123     int option;
124     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
125         switch (option) {
126             case 'w':
127                 width = strtol(optarg, NULL, 0);
128                 if (width <= 0) {
129                     fprintf(stderr, "%s: invalid width\n", argv[0]);
130                     exit(1);
131                 }
132                 break;
133             case 'h':
134                 height = strtol(optarg, NULL, 0);
135                 if (height <= 0) {
136                     fprintf(stderr, "%s: invalid height\n", argv[0]);
137                     exit(1);
138                 }
139                 break;
140             case '?':   // unknown or ambiguous option
141             case ':':   // missing argument
142                 exit(1);
143                 break;
144         }
145     
146     WebView *webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
147     WaitUntilDoneDelegate *delegate = [[WaitUntilDoneDelegate alloc] init];
148     EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
149     [webView setFrameLoadDelegate:delegate];
150     [webView setEditingDelegate:editingDelegate];
151     [webView setUIDelegate:delegate];
152     frame = [webView mainFrame];
153     
154     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
155     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
156     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
157     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
158
159     if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
160         char filenameBuffer[2048];
161         printSeparators = YES;
162         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
163             char *newLineCharacter = strchr(filenameBuffer, '\n');
164             if (newLineCharacter)
165                 *newLineCharacter = '\0';
166             
167             if (strlen(filenameBuffer) == 0)
168                 continue;
169                 
170             dumpRenderTree(filenameBuffer);
171             fflush(stdout);
172         }
173     } else {
174         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
175         for (int i = optind; i != argc; ++i) {
176             dumpRenderTree(argv[i]);
177         }
178     }
179     
180     [webView setFrameLoadDelegate:nil];
181     [webView setEditingDelegate:nil];
182     [webView setUIDelegate:nil];
183
184     [webView release];
185     [delegate release];
186     [editingDelegate release];
187
188     [pool release];
189     return 0;
190 }
191
192 static void dump(void)
193 {
194     NSString *result = nil;
195     if (dumpTree) {
196         if (dumpAsText) {
197             DOMElement *documentElement = [[frame DOMDocument] documentElement];
198             if ([documentElement isKindOfClass:[DOMHTMLElement class]])
199                 result = [[(DOMHTMLElement *)documentElement innerText] stringByAppendingString:@"\n"];
200         } else
201             result = [frame renderTreeAsExternalRepresentation];
202         
203         if (!result)
204             printf("ERROR: nil result from %s", dumpAsText ? "[documentElement innerText]" : "[frame renderTreeAsExternalRepresentation]");
205         else
206             fputs([result UTF8String], stdout);
207         
208         if (printSeparators)
209             puts("#EOF");
210     }
211     
212     if (dumpPixels) {
213         if (!dumpAsText) {
214             NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
215             NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
216             NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
217             
218             // grab a bitmap from the view
219             WebView *view = [frame webView];
220             NSBitmapImageRep *imageRep = [view bitmapImageRepForCachingDisplayInRect:[view frame]];
221             [view cacheDisplayInRect:[view frame] toBitmapImageRep:imageRep];
222             
223             // has the actual hash to compare to the expected image's hash
224             NSString *actualHash = md5HashStringForBitmap(imageRep);
225             printf("\nActualHash: %s\n", [actualHash UTF8String]);
226             printf("BaselineHash: %s\n", [baselineHash UTF8String]);
227             
228             // if the hashes don't match, send image back to stdout for diff comparision
229             if ([baselineHash isEqualToString:actualHash] == NO) {            
230                 NSData *imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
231                 printf("Content-length: %d\n", [imageData length]);
232                 fwrite([imageData bytes], 1, [imageData length], stdout);
233             }
234         }
235
236         printf("#EOF\n");
237     }
238
239     done = YES;
240 }
241
242 @implementation WaitUntilDoneDelegate
243
244 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
245 {
246     if ([dataSource webFrame] == frame) {
247         if (waitToDump)
248             readyToDump = YES;
249         else
250             dump();
251     }
252 }
253
254 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
255 {
256     if (frame == f)
257         readyToDump = NO;
258 }
259
260 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
261 {
262     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
263 }
264
265 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
266 {
267     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
268 }
269
270 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
271 {
272     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
273 }
274
275 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
276
277     LayoutTestController *ltc = [[LayoutTestController alloc] init];
278     [obj setValue:ltc forKey:@"layoutTestController"];
279     [ltc release];
280     EventSendingController *esc = [[EventSendingController alloc] init];
281     [obj setValue:esc forKey:@"eventSender"];
282     [esc release];
283     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
284     [obj setValue:tic forKey:@"textInputController"];
285     [tic release];
286 }
287
288 - (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
289 {
290     printf("ALERT: %s\n", [message UTF8String]);
291 }
292
293 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
294 {
295     if (dumpTitleChanges)
296         printf("TITLE CHANGED: %s\n", [title UTF8String]);
297 }
298
299 @end
300
301 @interface DOMNode (dumpPath)
302 - (NSString *)dumpPath;
303 @end
304
305 @implementation DOMNode (dumpPath)
306 - (NSString *)dumpPath
307 {
308     DOMNode *parent = [self parentNode];
309     NSString *str = [NSString stringWithFormat:@"%@", [self nodeName]];
310     if (parent != nil) {
311         str = [str stringByAppendingString:@" > "];
312         str = [str stringByAppendingString:[parent dumpPath]];
313     }
314     return str;
315 }
316 @end
317
318 @interface DOMRange (dump)
319 - (NSString *)dump;
320 @end
321
322 @implementation DOMRange (dump)
323 - (NSString *)dump
324 {
325     return [NSString stringWithFormat:@"range from %ld of %@ to %ld of %@", [self startOffset], [[self startContainer] dumpPath], [self endOffset], [[self endContainer] dumpPath]];
326 }
327 @end
328
329
330 @implementation EditingDelegate
331
332 - (BOOL)webView:(WebView *)webView shouldBeginEditingInDOMRange:(DOMRange *)range
333 {
334     printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n", [[range dump] UTF8String]);
335     return YES;
336 }
337
338 - (BOOL)webView:(WebView *)webView shouldEndEditingInDOMRange:(DOMRange *)range
339 {
340     printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n", [[range dump] UTF8String]);
341     return YES;
342 }
343
344 - (BOOL)webView:(WebView *)webView shouldInsertNode:(DOMNode *)node replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
345 {
346     static const char *insertactionstring[] = {
347         "WebViewInsertActionTyped",
348         "WebViewInsertActionPasted",
349         "WebViewInsertActionDropped",
350     };
351
352     printf("EDITING DELEGATE: shouldInsertNode:%s replacingDOMRange:%s givenAction:%s\n", [[node dumpPath] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
353     return YES;
354 }
355
356 - (BOOL)webView:(WebView *)webView shouldInsertText:(NSString *)text replacingDOMRange:(DOMRange *)range givenAction:(WebViewInsertAction)action
357 {
358     static const char *insertactionstring[] = {
359         "WebViewInsertActionTyped",
360         "WebViewInsertActionPasted",
361         "WebViewInsertActionDropped",
362     };
363
364     printf("EDITING DELEGATE: shouldInsertText:%s replacingDOMRange:%s givenAction:%s\n", [[text description] UTF8String], [[range dump] UTF8String], insertactionstring[action]);
365     return YES;
366 }
367
368 - (BOOL)webView:(WebView *)webView shouldDeleteDOMRange:(DOMRange *)range
369 {
370     printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", [[range dump] UTF8String]);
371     return YES;
372 }
373
374 - (BOOL)webView:(WebView *)webView shouldChangeSelectedDOMRange:(DOMRange *)currentRange toDOMRange:(DOMRange *)proposedRange affinity:(NSSelectionAffinity)selectionAffinity stillSelecting:(BOOL)flag
375 {
376     static const char *affinitystring[] = {
377         "NSSelectionAffinityUpstream",
378         "NSSelectionAffinityDownstream"
379     };
380     static const char *boolstring[] = {
381         "FALSE",
382         "TRUE"
383     };
384
385     printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s toDOMRange:%s affinity:%s stillSelecting:%s\n", [[currentRange dump] UTF8String], [[proposedRange dump] UTF8String], affinitystring[selectionAffinity], boolstring[flag]);
386     return YES;
387 }
388
389 - (BOOL)webView:(WebView *)webView shouldApplyStyle:(DOMCSSStyleDeclaration *)style toElementsInDOMRange:(DOMRange *)range
390 {
391     printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n", [[style description] UTF8String], [[range dump] UTF8String]);
392     return YES;
393 }
394
395 - (BOOL)webView:(WebView *)webView shouldChangeTypingStyle:(DOMCSSStyleDeclaration *)currentStyle toStyle:(DOMCSSStyleDeclaration *)proposedStyle
396 {
397     printf("EDITING DELEGATE: shouldChangeTypingStyle:%s toStyle:%s\n", [[currentStyle description] UTF8String], [[proposedStyle description] UTF8String]);
398     return YES;
399 }
400
401 - (void)webViewDidBeginEditing:(NSNotification *)notification
402 {
403     printf("EDITING DELEGATE: webViewDidBeginEditing:%s\n", [[notification name] UTF8String]);
404 }
405
406 - (void)webViewDidChange:(NSNotification *)notification
407 {
408     printf("EDITING DELEGATE: webViewDidChange:%s\n", [[notification name] UTF8String]);
409 }
410
411 - (void)webViewDidEndEditing:(NSNotification *)notification
412 {
413     printf("EDITING DELEGATE: webViewDidEndEditing:%s\n", [[notification name] UTF8String]);
414 }
415
416 - (void)webViewDidChangeTypingStyle:(NSNotification *)notification
417 {
418     printf("EDITING DELEGATE: webViewDidChangeTypingStyle:%s\n", [[notification name] UTF8String]);
419 }
420
421 - (void)webViewDidChangeSelection:(NSNotification *)notification
422 {
423     if (!done)
424         printf("EDITING DELEGATE: webViewDidChangeSelection:%s\n", [[notification name] UTF8String]);
425 }
426
427 @end
428
429 @implementation LayoutTestController
430
431 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
432 {
433     if (aSelector == @selector(waitUntilDone)
434             || aSelector == @selector(notifyDone)
435             || aSelector == @selector(dumpAsText)
436             || aSelector == @selector(dumpTitleChanges))
437         return NO;
438     return YES;
439 }
440
441 - (void)waitUntilDone 
442 {
443     waitToDump = YES;
444 }
445
446 - (void)notifyDone
447 {
448     if (waitToDump && readyToDump)
449         dump();
450     waitToDump = NO;
451 }
452
453 - (void)dumpAsText
454 {
455     dumpAsText = YES;
456 }
457
458 - (void)dumpTitleChanges
459 {
460     dumpTitleChanges = YES;
461 }
462
463 @end
464
465 @implementation EventSendingController
466
467 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
468 {
469     if (aSelector == @selector(mouseDown)
470             || aSelector == @selector(mouseUp)
471             || aSelector == @selector(mouseMoveToX:Y:))
472         return NO;
473     return YES;
474 }
475
476 + (NSString *)webScriptNameForSelector:(SEL)aSelector
477 {
478     if(aSelector == @selector(mouseMoveToX:Y:))
479         return @"mouseMoveTo";
480     return nil;
481 }
482
483 - (id)init
484 {
485     last = NSMakePoint(0, 0);
486     down = NO;
487     clickCount = 0;
488     lastClick = 0;
489     return self;
490 }
491
492 - (void)mouseDown
493 {
494     [[[frame frameView] documentView] layout];
495     if(GetCurrentEventTime() - lastClick >= 1)
496         clickCount = 1;
497     else
498         clickCount++;
499     NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseDown location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:clickCount pressure:nil];
500
501     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
502     if (subView) {
503         [subView mouseDown:event];
504         down = YES;
505     }
506 }
507
508 - (void)mouseUp
509 {
510     [[[frame frameView] documentView] layout];
511     NSEvent *event = [NSEvent mouseEventWithType:NSLeftMouseUp location:last modifierFlags:nil timestamp:GetCurrentEventTime() windowNumber:0 context:[NSGraphicsContext currentContext] eventNumber:nil clickCount:clickCount pressure:nil];
512
513     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
514     if (subView) {
515         [subView mouseUp:event];
516         down = NO;
517         lastClick = [event timestamp];
518     }
519 }
520
521 - (void)mouseMoveToX:(int)x Y:(int)y
522 {
523     last = NSMakePoint(x, [[frame webView] frame].size.height - y);
524     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];
525
526     NSView *subView = [[frame webView] hitTest:[event locationInWindow]];
527     if (subView) {
528         if (down)
529             [subView mouseDragged:event];
530         else
531             [subView mouseMoved:event];
532     }
533 }
534
535 @end
536
537 static void dumpRenderTree(const char *filename)
538 {
539     CFStringRef filenameString = CFStringCreateWithCString(NULL, filename, kCFStringEncodingUTF8);
540     if (filenameString == NULL) {
541         fprintf(stderr, "can't parse filename as UTF-8\n");
542         return;
543     }
544
545     CFURLRef URL = CFURLCreateWithFileSystemPath(NULL, filenameString, kCFURLPOSIXPathStyle, FALSE);
546     if (URL == NULL) {
547         fprintf(stderr, "can't turn %s into a CFURL\n", filename);
548         return;
549     }
550
551     done = NO;
552     readyToDump = NO;
553     waitToDump = NO;
554     dumpAsText = NO;
555     dumpTitleChanges = NO;
556     if (currentTest != nil)
557         CFRelease(currentTest);
558     currentTest = (NSString *)filenameString;
559
560     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
561     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
562     CFRelease(URL);
563     [pool release];
564     while (!done) {
565         pool = [[NSAutoreleasePool alloc] init];
566         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
567         [pool release];
568     }
569     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
570 }
571
572 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
573 NSString *md5HashStringForBitmap(NSBitmapImageRep *bitmap)
574 {
575     MD5_CTX md5Context;
576     unsigned char hash[16];
577
578     MD5_Init(&md5Context);
579     MD5_Update(&md5Context, [bitmap bitmapData], [bitmap bytesPerPlane]);
580     MD5_Final(hash, &md5Context);
581     
582     char hex[33] = "";
583     for (int i = 0; i < 16; i++) {
584        snprintf(hex, 33, "%s%02x", hex, hash[i]);
585     }
586
587     return [NSString stringWithUTF8String:hex];
588 }