Update the minimum font size pref to match the actual default setting in...
[WebKit-https.git] / WebKitTools / DumpRenderTree / DumpRenderTree.m
1 /*
2  * Copyright (C) 2005, 2006, 2007 Apple, Inc.  All rights reserved.
3  *           (C) 2007 Graham Dennis (graham.dennis@gmail.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29  
30 #import "DumpRenderTree.h"
31
32 #import "AppleScriptController.h"
33 #import "EditingDelegate.h"
34 #import "EventSendingController.h"
35 #import "GCController.h"
36 #import "NavigationController.h"
37 #import "ObjCPlugin.h"
38 #import "ObjCPluginFunction.h"
39 #import "ResourceLoadDelegate.h"
40 #import "TextInputController.h"
41 #import "UIDelegate.h"
42 #import <ApplicationServices/ApplicationServices.h> // for CMSetDefaultProfileBySpace
43 #import <CoreFoundation/CoreFoundation.h>
44 #import <JavaScriptCore/JavaScriptCore.h>
45 #import <WebKit/DOMElementPrivate.h>
46 #import <WebKit/DOMExtensions.h>
47 #import <WebKit/DOMRange.h>
48 #import <WebKit/WebBackForwardList.h>
49 #import <WebKit/WebCoreStatistics.h>
50 #import <WebKit/WebDataSource.h>
51 #import <WebKit/WebDocumentPrivate.h>
52 #import <WebKit/WebEditingDelegate.h>
53 #import <WebKit/WebFramePrivate.h>
54 #import <WebKit/WebFrameView.h>
55 #import <WebKit/WebHTMLViewPrivate.h>
56 #import <WebKit/WebHistory.h>
57 #import <WebKit/WebHistoryItemPrivate.h>
58 #import <WebKit/WebNSURLExtras.h>
59 #import <WebKit/WebPluginDatabase.h>
60 #import <WebKit/WebPreferences.h>
61 #import <WebKit/WebPreferencesPrivate.h>
62 #import <WebKit/WebResourceLoadDelegate.h>
63 #import <WebKit/WebViewPrivate.h>
64 #import <JavaScriptCore/Assertions.h>
65 #import <getopt.h>
66 #import <malloc/malloc.h>
67 #import <objc/objc-runtime.h>                       // for class_poseAs
68 #import <pthread.h>
69
70 #define COMMON_DIGEST_FOR_OPENSSL
71 #import <CommonCrypto/CommonDigest.h>               // for MD5 functions
72
73 @interface DumpRenderTreeWindow : NSWindow
74 @end
75
76 @interface DumpRenderTreePasteboard : NSPasteboard
77 - (int)declareType:(NSString *)type owner:(id)newOwner;
78 @end
79
80 @interface DumpRenderTreeEvent : NSEvent
81 @end
82
83 @interface WaitUntilDoneDelegate : NSObject
84 @end
85
86 @interface LayoutTestController : NSObject
87 {
88     WebScriptObject *storedWebScriptObject;
89 }
90 - (void)dealloc;
91 @end
92
93 @interface LocalPasteboard : NSPasteboard
94 {
95     NSMutableArray *typesArray;
96     NSMutableSet *typesSet;
97     NSMutableDictionary *dataByType;
98     int changeCount;
99 }
100 @end
101
102 BOOL windowIsKey = YES;
103 WebFrame *frame = 0;
104 BOOL shouldDumpEditingCallbacks;
105 BOOL shouldDumpResourceLoadCallbacks;
106
107 static void runTest(const char *pathOrURL);
108 static NSString *md5HashStringForBitmap(CGImageRef bitmap);
109 static void displayWebView();
110
111 volatile BOOL done;
112 static NavigationController *navigationController;
113
114 // Deciding when it's OK to dump out the state is a bit tricky.  All these must be true:
115 // - There is no load in progress
116 // - There is no work queued up (see workQueue var, below)
117 // - waitToDump==NO.  This means either waitUntilDone was never called, or it was called
118 //       and notifyDone was called subsequently.
119 // Note that the call to notifyDone and the end of the load can happen in either order.
120
121 // This is the topmost frame that is loading, during a given load, or nil when no load is 
122 // in progress.  Usually this is the same as the main frame, but not always.  In the case
123 // where a frameset is loaded, and then new content is loaded into one of the child frames,
124 // that child frame is the "topmost frame that is loading".
125 static WebFrame *topLoadingFrame;     // !nil iff a load is in progress
126 static BOOL waitToDump;     // TRUE if waitUntilDone() has been called, but notifyDone() has not yet been called
127
128 static BOOL dumpAsText;
129 static BOOL dumpDOMAsWebArchive;
130 static BOOL dumpSourceAsWebArchive;
131 static BOOL dumpSelectionRect;
132 static BOOL dumpTitleChanges;
133 static BOOL dumpBackForwardList;
134 static BOOL dumpChildFrameScrollPositions;
135 static int dumpPixels;
136 static int paint;
137 static int dumpAllPixels;
138 static int threaded;
139 static BOOL readFromWindow;
140 static int testRepaintDefault;
141 static BOOL testRepaint;
142 static int repaintSweepHorizontallyDefault;
143 static BOOL repaintSweepHorizontally;
144 static int dumpTree = YES;
145 static BOOL printSeparators;
146 static NSString *currentTest = nil;
147 static NSMutableDictionary *localPasteboards;
148 static WebHistoryItem *prevTestBFItem = nil;  // current b/f item at the end of the previous test
149 static unsigned char* screenCaptureBuffer;
150 static CGColorSpaceRef sharedColorSpace;
151 // a queue of NSInvocations, queued by callouts from the test, to be exec'ed when the load is done
152 static NSMutableArray *workQueue = nil;
153 // to prevent infinite loops, only the first page of a test can add to a work queue
154 // (since we may well come back to that same page)
155 static BOOL workQueueFrozen;
156
157 const unsigned maxViewHeight = 600;
158 const unsigned maxViewWidth = 800;
159
160 static pthread_mutex_t javaScriptThreadsMutex = PTHREAD_MUTEX_INITIALIZER;
161 static BOOL javaScriptThreadsShouldTerminate;
162
163 static const int javaScriptThreadsCount = 4;
164 static CFMutableDictionaryRef javaScriptThreads()
165 {
166     assert(pthread_mutex_trylock(&javaScriptThreadsMutex) == EBUSY);
167     static CFMutableDictionaryRef staticJavaScriptThreads;
168     if (!staticJavaScriptThreads)
169         staticJavaScriptThreads = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, NULL, NULL);
170     return staticJavaScriptThreads;
171 }
172
173 // Loops forever, running a script and randomly respawning, until 
174 // javaScriptThreadsShouldTerminate becomes true.
175 void* runJavaScriptThread(void* arg)
176 {
177     const char* const script =
178     " \
179     var array = []; \
180     for (var i = 0; i < 10; i++) { \
181         array.push(String(i)); \
182     } \
183     ";
184
185     while(1) {
186         JSGlobalContextRef ctx = JSGlobalContextCreate(NULL);
187         JSStringRef scriptRef = JSStringCreateWithUTF8CString(script);
188
189         JSValueRef exception = NULL;
190         JSEvaluateScript(ctx, scriptRef, NULL, NULL, 0, &exception);
191         assert(!exception);
192         
193         JSGlobalContextRelease(ctx);
194         JSStringRelease(scriptRef);
195         
196         JSGarbageCollect(ctx);
197
198         pthread_mutex_lock(&javaScriptThreadsMutex);
199
200         // Check for cancellation.
201         if (javaScriptThreadsShouldTerminate) {
202             pthread_mutex_unlock(&javaScriptThreadsMutex);
203             return 0;
204         }
205
206         // Respawn probabilistically.
207         if (random() % 5 == 0) {
208             pthread_t pthread;
209             pthread_create(&pthread, NULL, &runJavaScriptThread, NULL);
210             pthread_detach(pthread);
211
212             CFDictionaryRemoveValue(javaScriptThreads(), pthread_self());
213             CFDictionaryAddValue(javaScriptThreads(), pthread, NULL);
214
215             pthread_mutex_unlock(&javaScriptThreadsMutex);
216             return 0;
217         }
218
219         pthread_mutex_unlock(&javaScriptThreadsMutex);
220     }
221 }
222
223 static void startJavaScriptThreads(void)
224 {
225     pthread_mutex_lock(&javaScriptThreadsMutex);
226
227     for (int i = 0; i < javaScriptThreadsCount; i++) {
228         pthread_t pthread;
229         pthread_create(&pthread, NULL, &runJavaScriptThread, NULL);
230         pthread_detach(pthread);
231         CFDictionaryAddValue(javaScriptThreads(), pthread, NULL);
232     }
233
234     pthread_mutex_unlock(&javaScriptThreadsMutex);
235 }
236
237 static void stopJavaScriptThreads(void)
238 {
239     pthread_mutex_lock(&javaScriptThreadsMutex);
240
241     javaScriptThreadsShouldTerminate = YES;
242
243     const pthread_t pthreads[javaScriptThreadsCount];
244     assert(CFDictionaryGetCount(javaScriptThreads()) == javaScriptThreadsCount);
245     CFDictionaryGetKeysAndValues(javaScriptThreads(), (const void**)pthreads, NULL);
246
247     pthread_mutex_unlock(&javaScriptThreadsMutex);
248
249     for (int i = 0; i < javaScriptThreadsCount; i++) {
250         pthread_t pthread = pthreads[i];
251         pthread_join(pthread, NULL);
252     }
253 }
254
255 static BOOL shouldIgnoreWebCoreNodeLeaks(CFStringRef URLString)
256 {
257     static CFStringRef const ignoreSet[] = {
258         // Keeping this infrastructure around in case we ever need it again.
259     };
260     static const int ignoreSetCount = sizeof(ignoreSet) / sizeof(CFStringRef);
261     
262     for (int i = 0; i < ignoreSetCount; i++) {
263         CFStringRef ignoreString = ignoreSet[i];
264         CFRange range = CFRangeMake(0, CFStringGetLength(URLString));
265         CFOptionFlags flags = kCFCompareAnchored | kCFCompareBackwards | kCFCompareCaseInsensitive;
266         if (CFStringFindWithOptions(URLString, ignoreString, range, flags, NULL))
267             return YES;
268     }
269     return NO;
270 }
271
272 static CMProfileRef currentColorProfile = 0;
273 static void restoreColorSpace(int ignored)
274 {
275     if (currentColorProfile) {
276         int error = CMSetDefaultProfileByUse(cmDisplayUse, currentColorProfile);
277         if (error)
278             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);
279         currentColorProfile = 0;
280     }
281 }
282
283 static void crashHandler(int sig)
284 {
285     fprintf(stderr, "%s\n", strsignal(sig));
286     restoreColorSpace(0);
287     exit(128 + sig);
288 }
289
290 static void setDefaultColorProfileToRGB(void)
291 {
292     CMProfileRef genericProfile = [[NSColorSpace genericRGBColorSpace] colorSyncProfile];
293     CMProfileRef previousProfile;
294     int error = CMGetDefaultProfileByUse(cmDisplayUse, &previousProfile);
295     if (error) {
296         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);
297         return;
298     }
299     if (previousProfile == genericProfile)
300         return;
301     CFStringRef previousProfileName;
302     CFStringRef genericProfileName;
303     char previousProfileNameString[1024];
304     char genericProfileNameString[1024];
305     CMCopyProfileDescriptionString(previousProfile, &previousProfileName);
306     CMCopyProfileDescriptionString(genericProfile, &genericProfileName);
307     CFStringGetCString(previousProfileName, previousProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
308     CFStringGetCString(genericProfileName, genericProfileNameString, sizeof(previousProfileNameString), kCFStringEncodingUTF8);
309     CFRelease(genericProfileName);
310     CFRelease(previousProfileName);
311     
312     fprintf(stderr, "\n\nWARNING: Temporarily changing your system color profile from \"%s\" to \"%s\".\n", previousProfileNameString, genericProfileNameString);
313     fprintf(stderr, "This allows the WebKit pixel-based regression tests to have consistent color values across all machines.\n");
314     fprintf(stderr, "The colors on your screen will change for the duration of the testing.\n\n");
315     
316     if ((error = CMSetDefaultProfileByUse(cmDisplayUse, genericProfile)))
317         fprintf(stderr, "Failed to set color profile to \"%s\"! Many pixel tests will fail as a result.  (Error: %i)",
318             genericProfileNameString, error);
319     else {
320         currentColorProfile = previousProfile;
321         signal(SIGINT, restoreColorSpace);
322         signal(SIGHUP, restoreColorSpace);
323         signal(SIGTERM, restoreColorSpace);
324     }
325 }
326
327 static void* (*savedMalloc)(malloc_zone_t*, size_t);
328 static void* (*savedRealloc)(malloc_zone_t*, void*, size_t);
329
330 static void* checkedMalloc(malloc_zone_t* zone, size_t size)
331 {
332     if (size >= 0x10000000)
333         return 0;
334     return savedMalloc(zone, size);
335 }
336
337 static void* checkedRealloc(malloc_zone_t* zone, void* ptr, size_t size)
338 {
339     if (size >= 0x10000000)
340         return 0;
341     return savedRealloc(zone, ptr, size);
342 }
343
344 static void makeLargeMallocFailSilently(void)
345 {
346     malloc_zone_t* zone = malloc_default_zone();
347     savedMalloc = zone->malloc;
348     savedRealloc = zone->realloc;
349     zone->malloc = checkedMalloc;
350     zone->realloc = checkedRealloc;
351 }
352
353 void dumpRenderTree(int argc, const char *argv[])
354 {    
355     [NSApplication sharedApplication];
356
357     class_poseAs(objc_getClass("DumpRenderTreePasteboard"), objc_getClass("NSPasteboard"));
358     class_poseAs(objc_getClass("DumpRenderTreeWindow"), objc_getClass("NSWindow"));
359     class_poseAs(objc_getClass("DumpRenderTreeEvent"), objc_getClass("NSEvent"));
360
361     struct option options[] = {
362         {"dump-all-pixels", no_argument, &dumpAllPixels, YES},
363         {"horizontal-sweep", no_argument, &repaintSweepHorizontallyDefault, YES},
364         {"notree", no_argument, &dumpTree, NO},
365         {"pixel-tests", no_argument, &dumpPixels, YES},
366         {"paint", no_argument, &paint, YES},
367         {"repaint", no_argument, &testRepaintDefault, YES},
368         {"tree", no_argument, &dumpTree, YES},
369         {"threaded", no_argument, &threaded, YES},
370         {NULL, 0, NULL, 0}
371     };
372
373     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
374     [defaults setObject:@"DoubleMax" forKey:@"AppleScrollBarVariant"];
375     [defaults setInteger:4 forKey:@"AppleAntiAliasingThreshold"];
376     // 2 is the "Medium" font smoothing mode
377     [defaults setInteger:2 forKey:@"AppleFontSmoothing"];
378
379     [defaults setInteger:1 forKey:@"AppleAquaColorVariant"];
380     [defaults setObject:@"0.709800 0.835300 1.000000" forKey:@"AppleHighlightColor"];
381     [defaults setObject:@"0.500000 0.500000 0.500000" forKey:@"AppleOtherHighlightColor"];
382     
383     [defaults setObject:[NSArray arrayWithObject:@"en"] forKey:@"AppleLanguages"];
384     
385     WebPreferences *preferences = [WebPreferences standardPreferences];
386     
387     [preferences setStandardFontFamily:@"Times"];
388     [preferences setFixedFontFamily:@"Courier"];
389     [preferences setSerifFontFamily:@"Times"];
390     [preferences setSansSerifFontFamily:@"Helvetica"];
391     [preferences setCursiveFontFamily:@"Apple Chancery"];
392     [preferences setFantasyFontFamily:@"Papyrus"];
393     [preferences setDefaultFontSize:16];
394     [preferences setDefaultFixedFontSize:13];
395     [preferences setMinimumFontSize:1];
396     [preferences setJavaEnabled:NO];
397     [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
398     [preferences setEditableLinkBehavior:WebKitEditableLinkOnlyLiveWithShiftKey];
399     [preferences setTabsToLinks:NO];
400
401     int option;
402     while ((option = getopt_long(argc, (char * const *)argv, "", options, NULL)) != -1)
403         switch (option) {
404             case '?':   // unknown or ambiguous option
405             case ':':   // missing argument
406                 exit(1);
407                 break;
408         }
409     
410     if ([[[NSFontManager sharedFontManager] availableMembersOfFontFamily:@"Ahem"] count] == 0) {
411         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.org/quality/Ahem.ttf>.\n");
412         exit(1);
413     }
414     
415     if (dumpPixels) {
416         setDefaultColorProfileToRGB();
417         screenCaptureBuffer = malloc(maxViewHeight * maxViewWidth * 4);
418         sharedColorSpace = CGColorSpaceCreateDeviceRGB();
419     }
420     
421     localPasteboards = [[NSMutableDictionary alloc] init];
422
423     navigationController = [[NavigationController alloc] init];
424
425     NSRect rect = NSMakeRect(0, 0, maxViewWidth, maxViewHeight);
426     WebView *webView = [[WebView alloc] initWithFrame:rect];
427     frame = [webView mainFrame];
428     
429     WaitUntilDoneDelegate *waitUntilDoneDelegate = [[WaitUntilDoneDelegate alloc] init];
430     [webView setFrameLoadDelegate:waitUntilDoneDelegate];
431     
432     UIDelegate *uiDelegate = [[UIDelegate alloc] init];
433     [webView setUIDelegate:uiDelegate];
434
435     EditingDelegate *editingDelegate = [[EditingDelegate alloc] init];
436     [webView setEditingDelegate:editingDelegate];
437     
438     ResourceLoadDelegate *resourceLoadDelegate = [[ResourceLoadDelegate alloc] init];
439     [webView setResourceLoadDelegate:resourceLoadDelegate];
440     
441     NSString *pwd = [[NSString stringWithUTF8String:argv[0]] stringByDeletingLastPathComponent];
442     [WebPluginDatabase setAdditionalWebPlugInPaths:[NSArray arrayWithObject:pwd]];
443     [[WebPluginDatabase sharedDatabase] refresh];
444
445     // The back/forward cache is causing problems due to layouts during transition from one page to another.
446     // So, turn it off for now, but we might want to turn it back on some day.
447     [[webView backForwardList] setPageCacheSize:0];
448
449     // 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.
450     // Put it at -10000, -10000 in "flipped coordinates", since WebCore and the DOM use flipped coordinates.
451     NSRect windowRect = NSOffsetRect(rect, -10000, [[[NSScreen screens] objectAtIndex:0] frame].size.height - rect.size.height + 10000);
452     NSWindow *window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
453     [[window contentView] addSubview:webView];
454     [window orderBack:nil];
455     [window setAutodisplay:NO];
456
457     workQueue = [[NSMutableArray alloc] init];
458
459     [webView setContinuousSpellCheckingEnabled:YES];
460
461     makeLargeMallocFailSilently();
462
463     signal(SIGILL, crashHandler);    /* 4:   illegal instruction (not reset when caught) */
464     signal(SIGTRAP, crashHandler);   /* 5:   trace trap (not reset when caught) */
465     signal(SIGEMT, crashHandler);    /* 7:   EMT instruction */
466     signal(SIGFPE, crashHandler);    /* 8:   floating point exception */
467     signal(SIGBUS, crashHandler);    /* 10:  bus error */
468     signal(SIGSEGV, crashHandler);   /* 11:  segmentation violation */
469     signal(SIGSYS, crashHandler);    /* 12:  bad argument to system call */
470     signal(SIGPIPE, crashHandler);   /* 13:  write on a pipe with no reader */
471     signal(SIGXCPU, crashHandler);   /* 24:  exceeded CPU time limit */
472     signal(SIGXFSZ, crashHandler);   /* 25:  exceeded file size limit */
473     
474     [[NSURLCache sharedURLCache] removeAllCachedResponses];
475     
476     // For reasons that are not entirely clear, the following pair of calls makes WebView handle its
477     // dynamic scrollbars properly. Without it, every frame will always have scrollbars.
478     NSBitmapImageRep *imageRep = [webView bitmapImageRepForCachingDisplayInRect:[webView bounds]];
479     [webView cacheDisplayInRect:[webView bounds] toBitmapImageRep:imageRep];
480
481     if (threaded)
482         startJavaScriptThreads();
483     
484     if (argc == optind+1 && strcmp(argv[optind], "-") == 0) {
485         char filenameBuffer[2048];
486         printSeparators = YES;
487         while (fgets(filenameBuffer, sizeof(filenameBuffer), stdin)) {
488             char *newLineCharacter = strchr(filenameBuffer, '\n');
489             if (newLineCharacter)
490                 *newLineCharacter = '\0';
491             
492             if (strlen(filenameBuffer) == 0)
493                 continue;
494                 
495             runTest(filenameBuffer);
496         }
497     } else {
498         printSeparators = (optind < argc-1 || (dumpPixels && dumpTree));
499         for (int i = optind; i != argc; ++i)
500             runTest(argv[i]);
501     }
502
503     if (threaded)
504         stopJavaScriptThreads();
505     
506     [workQueue release];
507
508     [WebCoreStatistics emptyCache]; // Otherwise SVGImages trigger false positives for Frame/Node counts    
509     [webView close];
510     frame = nil;
511
512     // Work around problem where registering drag types leaves an outstanding
513     // "perform selector" on the window, which retains the window. It's a bit
514     // inelegant and perhaps dangerous to just blow them all away, but in practice
515     // it probably won't cause any trouble (and this is just a test tool, after all).
516     [NSObject cancelPreviousPerformRequestsWithTarget:window];
517     
518     [window close]; // releases when closed
519     [webView release];
520     [waitUntilDoneDelegate release];
521     [editingDelegate release];
522     [resourceLoadDelegate release];
523     [uiDelegate release];
524     
525     [localPasteboards release];
526     localPasteboards = nil;
527     
528     [navigationController release];
529     navigationController = nil;
530     
531     if (dumpPixels)
532         restoreColorSpace(0);
533 }
534
535 int main(int argc, const char *argv[])
536 {
537     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
538     dumpRenderTree(argc, argv);
539     [WebCoreStatistics garbageCollectJavaScriptObjects];
540     [pool release];
541     return 0;
542 }
543
544 static int compareHistoryItems(id item1, id item2, void *context)
545 {
546     return [[item1 target] caseInsensitiveCompare:[item2 target]];
547 }
548
549 static void dumpHistoryItem(WebHistoryItem *item, int indent, BOOL current)
550 {
551     int start = 0;
552     if (current) {
553         printf("curr->");
554         start = 6;
555     }
556     for (int i = start; i < indent; i++)
557         putchar(' ');
558     printf("%s", [[item URLString] UTF8String]);
559     NSString *target = [item target];
560     if (target && [target length] > 0)
561         printf(" (in frame \"%s\")", [target UTF8String]);
562     if ([item isTargetItem])
563         printf("  **nav target**");
564     putchar('\n');
565     NSArray *kids = [item children];
566     if (kids) {
567         // must sort to eliminate arbitrary result ordering which defeats reproducible testing
568         kids = [kids sortedArrayUsingFunction:&compareHistoryItems context:nil];
569         for (unsigned i = 0; i < [kids count]; i++)
570             dumpHistoryItem([kids objectAtIndex:i], indent+4, NO);
571     }
572 }
573
574 static void dumpFrameScrollPosition(WebFrame *f)
575 {
576     NSPoint scrollPosition = [[[[f frameView] documentView] superview] bounds].origin;
577     if (ABS(scrollPosition.x) > 0.00000001 || ABS(scrollPosition.y) > 0.00000001) {
578         if ([f parentFrame] != nil)
579             printf("frame '%s' ", [[f name] UTF8String]);
580         printf("scrolled to %.f,%.f\n", scrollPosition.x, scrollPosition.y);
581     }
582
583     if (dumpChildFrameScrollPositions) {
584         NSArray *kids = [f childFrames];
585         if (kids)
586             for (unsigned i = 0; i < [kids count]; i++)
587                 dumpFrameScrollPosition([kids objectAtIndex:i]);
588     }
589 }
590
591 static void convertWebResourceDataToString(NSMutableDictionary *resource)
592 {
593     NSString *mimeType = [resource objectForKey:@"WebResourceMIMEType"];
594     if ([mimeType hasPrefix:@"text/"] || [mimeType isEqualToString:@"application/x-javascript"]) {
595         NSData *data = [resource objectForKey:@"WebResourceData"];
596         NSString *dataAsString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
597         [resource setObject:dataAsString forKey:@"WebResourceData"];
598     }
599 }
600
601 static void normalizeWebResourceURL(NSMutableString *webResourceURL, NSString *oldURLBase)
602 {
603     [webResourceURL replaceOccurrencesOfString:oldURLBase
604                                     withString:@"file://"
605                                        options:NSLiteralSearch
606                                          range:NSMakeRange(0, [webResourceURL length])];
607 }
608
609 static void normalizeWebResourceResponse(NSMutableDictionary *propertyList, NSString *oldURLBase)
610 {
611     NSURLResponse *response = nil;
612     NSData *responseData = [propertyList objectForKey:@"WebResourceResponse"]; // WebResourceResponseKey in WebResource.m
613     if ([responseData isKindOfClass:[NSData class]]) {
614         // Decode NSURLResponse
615         NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:responseData];
616         response = [unarchiver decodeObjectForKey:@"WebResourceResponse"]; // WebResourceResponseKey in WebResource.m
617         [unarchiver finishDecoding];
618         [unarchiver release];
619
620         // Create replacement NSURLReponse
621         NSMutableString *URL = [[[NSMutableString alloc] initWithContentsOfURL:[response URL]] autorelease];
622         normalizeWebResourceURL(URL, oldURLBase);
623         NSURLResponse *newResponse = [[NSURLResponse alloc] initWithURL:[[[NSURL alloc] initWithString:URL] autorelease]
624                                                                MIMEType:[response MIMEType]
625                                                   expectedContentLength:[response expectedContentLength]
626                                                        textEncodingName:[response textEncodingName]];
627         [newResponse autorelease];
628
629         // Encode replacement NSURLResponse
630         NSMutableData *newResponseData = [[NSMutableData alloc] init];
631         NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:newResponseData];
632         [archiver encodeObject:newResponse forKey:@"WebResourceResponse"];
633         [archiver finishEncoding];
634         [archiver release];
635         [propertyList setObject:newResponseData forKey:@"WebResourceResponse"]; // WebResourceResponseKey in WebResource.m
636         [newResponseData release];
637     }
638 }
639
640 static NSString *serializeWebArchiveToXML(WebArchive *webArchive)
641 {
642     NSString *errorString;
643     NSMutableDictionary *propertyList = [NSPropertyListSerialization propertyListFromData:[webArchive data]
644                                                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
645                                                                                    format:NULL
646                                                                          errorDescription:&errorString];
647     if (!propertyList)
648         return errorString;
649
650     // Normalize WebResourceResponse and WebResourceURL values in plist for testing
651     NSString *cwdURL = [@"file://" stringByAppendingString:[[[NSFileManager defaultManager] currentDirectoryPath] stringByExpandingTildeInPath]];
652
653     NSMutableArray *resources = [NSMutableArray arrayWithCapacity:1];
654     [resources addObject:propertyList];
655
656     while ([resources count]) {
657         NSMutableDictionary *resourcePropertyList = [resources objectAtIndex:0];
658         [resources removeObjectAtIndex:0];
659
660         NSMutableDictionary *mainResource = [resourcePropertyList objectForKey:@"WebMainResource"];
661         normalizeWebResourceURL([mainResource objectForKey:@"WebResourceURL"], cwdURL);
662         convertWebResourceDataToString(mainResource);
663
664         // Add subframeArchives to list for processing
665         NSMutableArray *subframeArchives = [resourcePropertyList objectForKey:@"WebSubframeArchives"]; // WebSubframeArchivesKey in WebArchive.m
666         if (subframeArchives)
667             [resources addObjectsFromArray:subframeArchives];
668
669         NSMutableArray *subresources = [resourcePropertyList objectForKey:@"WebSubresources"]; // WebSubresourcesKey in WebArchive.m
670         NSEnumerator *enumerator = [subresources objectEnumerator];
671         NSMutableDictionary *subresourcePropertyList;
672         while ((subresourcePropertyList = [enumerator nextObject])) {
673             normalizeWebResourceURL([subresourcePropertyList objectForKey:@"WebResourceURL"], cwdURL);
674             normalizeWebResourceResponse(subresourcePropertyList, cwdURL);
675             convertWebResourceDataToString(subresourcePropertyList);
676         }
677     }
678
679     NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:propertyList
680                                                                  format:NSPropertyListXMLFormat_v1_0
681                                                        errorDescription:&errorString];
682     if (!xmlData)
683         return errorString;
684
685     return [[[NSMutableString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease];
686 }
687
688 static void dump(void)
689 {
690     if (dumpTree) {
691         NSString *result = nil;
692
693         dumpAsText |= [[[[frame dataSource] response] MIMEType] isEqualToString:@"text/plain"];
694         if (dumpAsText) {
695             DOMElement *documentElement = [[frame DOMDocument] documentElement];
696             result = [[(DOMElement *)documentElement innerText] stringByAppendingString:@"\n"];
697         } else if (dumpDOMAsWebArchive) {
698             WebArchive *webArchive = [[frame DOMDocument] webArchive];
699             result = serializeWebArchiveToXML(webArchive);
700         } else if (dumpSourceAsWebArchive) {
701             WebArchive *webArchive = [[frame dataSource] webArchive];
702             result = serializeWebArchiveToXML(webArchive);
703         } else {
704             bool isSVGW3CTest = ([currentTest rangeOfString:@"svg/W3C-SVG-1.1"].length);
705             if (isSVGW3CTest)
706                 [[frame webView] setFrameSize:NSMakeSize(480, 360)];
707             else 
708                 [[frame webView] setFrameSize:NSMakeSize(maxViewWidth, maxViewHeight)];
709             result = [frame renderTreeAsExternalRepresentation];
710         }
711
712         if (!result) {
713             const char *errorMessage;
714             if (dumpAsText)
715                 errorMessage = "[documentElement innerText]";
716             else if (dumpDOMAsWebArchive)
717                 errorMessage = "[[frame DOMDocument] webArchive]";
718             else if (dumpSourceAsWebArchive)
719                 errorMessage = "[[frame dataSource] webArchive]";
720             else
721                 errorMessage = "[frame renderTreeAsExternalRepresentation]";
722             printf("ERROR: nil result from %s", errorMessage);
723         } else {
724             fputs([result UTF8String], stdout);
725             if (!dumpAsText && !dumpDOMAsWebArchive && !dumpSourceAsWebArchive)
726                 dumpFrameScrollPosition(frame);
727         }
728
729         if (dumpBackForwardList) {
730             printf("\n============== Back Forward List ==============\n");
731             WebBackForwardList *bfList = [[frame webView] backForwardList];
732
733             // Print out all items in the list after prevTestBFItem, which was from the previous test
734             // Gather items from the end of the list, the print them out from oldest to newest
735             NSMutableArray *itemsToPrint = [[NSMutableArray alloc] init];
736             for (int i = [bfList forwardListCount]; i > 0; i--) {
737                 WebHistoryItem *item = [bfList itemAtIndex:i];
738                 // something is wrong if the item from the last test is in the forward part of the b/f list
739                 assert(item != prevTestBFItem);
740                 [itemsToPrint addObject:item];
741             }
742             
743             assert([bfList currentItem] != prevTestBFItem);
744             [itemsToPrint addObject:[bfList currentItem]];
745             int currentItemIndex = [itemsToPrint count] - 1;
746
747             for (int i = -1; i >= -[bfList backListCount]; i--) {
748                 WebHistoryItem *item = [bfList itemAtIndex:i];
749                 if (item == prevTestBFItem)
750                     break;
751                 [itemsToPrint addObject:item];
752             }
753
754             for (int i = [itemsToPrint count]-1; i >= 0; i--) {
755                 dumpHistoryItem([itemsToPrint objectAtIndex:i], 8, i == currentItemIndex);
756             }
757             [itemsToPrint release];
758             printf("===============================================\n");
759         }
760
761         if (printSeparators)
762             puts("#EOF");
763     }
764     
765     if (dumpPixels) {
766         if (!dumpAsText && !dumpDOMAsWebArchive && !dumpSourceAsWebArchive) {
767             // grab a bitmap from the view
768             WebView* view = [frame webView];
769             NSSize webViewSize = [view frame].size;
770
771             CGContextRef cgContext = CGBitmapContextCreate(screenCaptureBuffer, webViewSize.width, webViewSize.height, 8, webViewSize.width * 4, sharedColorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedLast);
772
773             NSGraphicsContext* savedContext = [[[NSGraphicsContext currentContext] retain] autorelease];
774             NSGraphicsContext* nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:cgContext flipped:NO];
775             [NSGraphicsContext setCurrentContext:nsContext];
776
777             if (readFromWindow) {
778                 NSBitmapImageRep *imageRep;
779                 [view displayIfNeeded];
780                 [view lockFocus];
781                 imageRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:[view frame]];
782                 [view unlockFocus];
783                 [imageRep draw];
784                 [imageRep release];
785             } else if (!testRepaint)
786                 [view displayRectIgnoringOpacity:NSMakeRect(0, 0, webViewSize.width, webViewSize.height) inContext:nsContext];
787             else if (!repaintSweepHorizontally) {
788                 NSRect line = NSMakeRect(0, 0, webViewSize.width, 1);
789                 while (line.origin.y < webViewSize.height) {
790                     [view displayRectIgnoringOpacity:line inContext:nsContext];
791                     line.origin.y++;
792                 }
793             } else {
794                 NSRect column = NSMakeRect(0, 0, 1, webViewSize.height);
795                 while (column.origin.x < webViewSize.width) {
796                     [view displayRectIgnoringOpacity:column inContext:nsContext];
797                     column.origin.x++;
798                 }
799             }
800             if (dumpSelectionRect) {
801                 NSView *documentView = [[frame frameView] documentView];
802                 if ([documentView conformsToProtocol:@protocol(WebDocumentSelection)]) {
803                     [[NSColor redColor] set];
804                     [NSBezierPath strokeRect:[documentView convertRect:[(id <WebDocumentSelection>)documentView selectionRect] fromView:nil]];
805                 }
806             }
807
808             [NSGraphicsContext setCurrentContext:savedContext];
809             
810             CGImageRef bitmapImage = CGBitmapContextCreateImage(cgContext);
811             CGContextRelease(cgContext);
812
813             // compute the actual hash to compare to the expected image's hash
814             NSString *actualHash = md5HashStringForBitmap(bitmapImage);
815             printf("\nActualHash: %s\n", [actualHash UTF8String]);
816
817             BOOL dumpImage;
818             if (dumpAllPixels)
819                 dumpImage = YES;
820             else {
821                 // FIXME: It's unfortunate that we hardcode the file naming scheme here.
822                 // At one time, the perl script had all the knowledge about file layout.
823                 // Some day we should restore that setup by passing in more parameters to this tool.
824                 NSString *baseTestPath = [currentTest stringByDeletingPathExtension];
825                 NSString *baselineHashPath = [baseTestPath stringByAppendingString:@"-expected.checksum"];
826                 NSString *baselineHash = [NSString stringWithContentsOfFile:baselineHashPath encoding:NSUTF8StringEncoding error:nil];
827                 NSString *baselineImagePath = [baseTestPath stringByAppendingString:@"-expected.png"];
828
829                 printf("BaselineHash: %s\n", [baselineHash UTF8String]);
830
831                 /// send the image to stdout if the hash mismatches or if there's no file in the file system
832                 dumpImage = ![baselineHash isEqualToString:actualHash] || access([baselineImagePath fileSystemRepresentation], F_OK) != 0;
833             }
834             
835             if (dumpImage) {
836                 CFMutableDataRef imageData = CFDataCreateMutable(0, 0);
837                 CGImageDestinationRef imageDest = CGImageDestinationCreateWithData(imageData, CFSTR("public.png"), 1, 0);
838                 CGImageDestinationAddImage(imageDest, bitmapImage, 0);
839                 CGImageDestinationFinalize(imageDest);
840                 CFRelease(imageDest);
841                 printf("Content-length: %lu\n", CFDataGetLength(imageData));
842                 fwrite(CFDataGetBytePtr(imageData), 1, CFDataGetLength(imageData), stdout);
843                 CFRelease(imageData);
844             }
845
846             CGImageRelease(bitmapImage);
847         }
848
849         printf("#EOF\n");
850     }
851     
852     fflush(stdout);
853
854     if (paint)
855         displayWebView();
856     
857     done = YES;
858 }
859
860 @implementation WaitUntilDoneDelegate
861
862 // Exec messages in the work queue until they're all done, or one of them starts a new load
863 - (void)processWork:(id)dummy
864 {
865     // quit doing work once a load is in progress
866     while ([workQueue count] > 0 && !topLoadingFrame) {
867         [[workQueue objectAtIndex:0] invoke];
868         [workQueue removeObjectAtIndex:0];
869     }
870     
871     // if we didn't start a new load, then we finished all the commands, so we're ready to dump state
872     if (!topLoadingFrame && !waitToDump)
873         dump();
874 }
875
876 - (void)webView:(WebView *)c locationChangeDone:(NSError *)error forDataSource:(WebDataSource *)dataSource
877 {
878     if ([dataSource webFrame] == topLoadingFrame) {
879         topLoadingFrame = nil;
880         workQueueFrozen = YES;      // first complete load freezes the queue for the rest of this test
881         if (!waitToDump) {
882             if ([workQueue count] > 0)
883                 [self performSelector:@selector(processWork:) withObject:nil afterDelay:0];
884             else
885                 dump();
886         }
887     }
888 }
889
890 - (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)f
891 {
892     ASSERT([f provisionalDataSource]);
893     // Make sure we only set this once per test.  If it gets cleared, and then set again, we might
894     // end up doing two dumps for one test.
895     if (!topLoadingFrame && !done)
896         topLoadingFrame = f;
897 }
898
899 - (void)webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)f
900 {
901     ASSERT(![f provisionalDataSource]);
902     ASSERT([f dataSource]);
903
904     windowIsKey = YES;
905     NSView *documentView = [[frame frameView] documentView];
906     [[[frame webView] window] makeFirstResponder:documentView];
907     if ([documentView isKindOfClass:[WebHTMLView class]])
908         [(WebHTMLView *)documentView _updateActiveState];
909 }
910
911 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
912 {
913     ASSERT([frame provisionalDataSource]);
914
915     [self webView:sender locationChangeDone:error forDataSource:[frame provisionalDataSource]];
916 }
917
918 - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
919 {
920     [self webView:sender locationChangeDone:nil forDataSource:[frame dataSource]];
921     [navigationController webView:sender didFinishLoadForFrame:frame];
922 }
923
924 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame
925 {
926     ASSERT(![frame provisionalDataSource]);
927     ASSERT([frame dataSource]);
928
929     [self webView:sender locationChangeDone:error forDataSource:[frame dataSource]];
930 }
931
932 - (void)webView:(WebView *)sender windowScriptObjectAvailable:(WebScriptObject *)obj 
933
934     LayoutTestController *ltc = [[LayoutTestController alloc] init];
935     [obj setValue:ltc forKey:@"layoutTestController"];
936     [ltc release];
937     
938     EventSendingController *esc = [[EventSendingController alloc] init];
939     [obj setValue:esc forKey:@"eventSender"];
940     [esc release];
941     
942     TextInputController *tic = [[TextInputController alloc] initWithWebView:sender];
943     [obj setValue:tic forKey:@"textInputController"];
944     [tic release];
945     
946     AppleScriptController *asc = [[AppleScriptController alloc] initWithWebView:sender];
947     [obj setValue:asc forKey:@"appleScriptController"];
948     [asc release];
949     
950     GCController *gcc = [[GCController alloc] init];
951     [obj setValue:gcc forKey:@"GCController"];
952     [gcc release];
953     
954     [obj setValue:navigationController forKey:@"navigationController"];
955     
956     ObjCPlugin *plugin = [[ObjCPlugin alloc] init];
957     [obj setValue:plugin forKey:@"objCPlugin"];
958     [plugin release];
959     
960     ObjCPluginFunction *pluginFunction = [[ObjCPluginFunction alloc] init];
961     [obj setValue:pluginFunction forKey:@"objCPluginFunction"];
962     [pluginFunction release];
963 }
964
965 - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame
966 {
967     if (dumpTitleChanges)
968         printf("TITLE CHANGED: %s\n", [title UTF8String]);
969 }
970
971 @end
972
973 @implementation LayoutTestController
974
975 + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
976 {
977     if (aSelector == @selector(waitUntilDone)
978             || aSelector == @selector(notifyDone)
979             || aSelector == @selector(dumpAsText)
980             || aSelector == @selector(dumpDOMAsWebArchive)
981             || aSelector == @selector(dumpSourceAsWebArchive)
982             || aSelector == @selector(dumpTitleChanges)
983             || aSelector == @selector(dumpBackForwardList)
984             || aSelector == @selector(dumpChildFrameScrollPositions)
985             || aSelector == @selector(dumpEditingCallbacks)
986             || aSelector == @selector(dumpResourceLoadCallbacks)
987             || aSelector == @selector(setWindowIsKey:)
988             || aSelector == @selector(setMainFrameIsFirstResponder:)
989             || aSelector == @selector(dumpSelectionRect)
990             || aSelector == @selector(display)
991             || aSelector == @selector(testRepaint)
992             || aSelector == @selector(repaintSweepHorizontally)
993             || aSelector == @selector(queueBackNavigation:)
994             || aSelector == @selector(queueForwardNavigation:)
995             || aSelector == @selector(queueReload)
996             || aSelector == @selector(queueScript:)
997             || aSelector == @selector(queueLoad:target:)
998             || aSelector == @selector(clearBackForwardList)
999             || aSelector == @selector(keepWebHistory)
1000             || aSelector == @selector(setAcceptsEditing:)
1001             || aSelector == @selector(setTabKeyCyclesThroughElements:)
1002             || aSelector == @selector(storeWebScriptObject:)
1003             || aSelector == @selector(accessStoredWebScriptObject)
1004             || aSelector == @selector(setUserStyleSheetLocation:)
1005             || aSelector == @selector(setUserStyleSheetEnabled:)
1006             || aSelector == @selector(objCClassNameOf:))
1007         return NO;
1008     return YES;
1009 }
1010
1011 + (NSString *)webScriptNameForSelector:(SEL)aSelector
1012 {
1013     if (aSelector == @selector(setWindowIsKey:))
1014         return @"setWindowIsKey";
1015     if (aSelector == @selector(setMainFrameIsFirstResponder:))
1016         return @"setMainFrameIsFirstResponder";
1017     if (aSelector == @selector(queueBackNavigation:))
1018         return @"queueBackNavigation";
1019     if (aSelector == @selector(queueForwardNavigation:))
1020         return @"queueForwardNavigation";
1021     if (aSelector == @selector(queueScript:))
1022         return @"queueScript";
1023     if (aSelector == @selector(queueLoad:target:))
1024         return @"queueLoad";
1025     if (aSelector == @selector(setAcceptsEditing:))
1026         return @"setAcceptsEditing";
1027     if (aSelector == @selector(setTabKeyCyclesThroughElements:))
1028         return @"setTabKeyCyclesThroughElements";
1029     if (aSelector == @selector(storeWebScriptObject:))
1030         return @"storeWebScriptObject";
1031     if (aSelector == @selector(setUserStyleSheetLocation:))
1032         return @"setUserStyleSheetLocation";
1033     if (aSelector == @selector(setUserStyleSheetEnabled:))
1034         return @"setUserStyleSheetEnabled";
1035     if (aSelector == @selector(objCClassNameOf:))
1036         return @"objCClassName";
1037     return nil;
1038 }
1039
1040 - (void)clearBackForwardList
1041 {
1042     WebBackForwardList *backForwardList = [[frame webView] backForwardList];
1043     WebHistoryItem *item = [[backForwardList currentItem] retain];
1044
1045     // We clear the history by setting the back/forward list's capacity to 0
1046     // then restoring it back and adding back the current item.
1047     int capacity = [backForwardList capacity];
1048     [backForwardList setCapacity:0];
1049     [backForwardList setCapacity:capacity];
1050     [backForwardList addItem:item];
1051     [backForwardList goToItem:item];
1052     [item release];
1053 }
1054
1055 - (void)keepWebHistory
1056 {
1057     if (![WebHistory optionalSharedHistory]) {
1058         WebHistory *history = [[WebHistory alloc] init];
1059         [WebHistory setOptionalSharedHistory:history];
1060         [history release];
1061     }
1062 }
1063
1064 - (void)waitUntilDone 
1065 {
1066     waitToDump = YES;
1067 }
1068
1069 - (void)notifyDone
1070 {
1071     if (waitToDump && !topLoadingFrame && [workQueue count] == 0)
1072         dump();
1073     waitToDump = NO;
1074 }
1075
1076 - (void)dumpAsText
1077 {
1078     dumpAsText = YES;
1079 }
1080
1081 - (void)setUserStyleSheetLocation:(NSString *)path;
1082 {
1083     NSURL *url = [NSURL URLWithString:path];
1084     [[WebPreferences standardPreferences] setUserStyleSheetLocation:url];
1085 }
1086
1087 - (void)setUserStyleSheetEnabled:(BOOL)flag;
1088 {
1089     [[WebPreferences standardPreferences] setUserStyleSheetEnabled:flag];
1090 }
1091
1092 - (void)dumpDOMAsWebArchive
1093 {
1094     dumpDOMAsWebArchive = YES;
1095 }
1096
1097 - (void)dumpSourceAsWebArchive
1098 {
1099     dumpSourceAsWebArchive = YES;
1100 }
1101
1102 - (void)dumpSelectionRect
1103 {
1104     dumpSelectionRect = YES;
1105 }
1106
1107 - (void)dumpTitleChanges
1108 {
1109     dumpTitleChanges = YES;
1110 }
1111
1112 - (void)dumpBackForwardList
1113 {
1114     dumpBackForwardList = YES;
1115 }
1116
1117 - (void)dumpChildFrameScrollPositions
1118 {
1119     dumpChildFrameScrollPositions = YES;
1120 }
1121
1122 - (void)dumpEditingCallbacks
1123 {
1124     shouldDumpEditingCallbacks = YES;
1125 }
1126
1127 - (void)dumpResourceLoadCallbacks
1128 {
1129     shouldDumpResourceLoadCallbacks = YES;
1130 }
1131
1132 - (void)setWindowIsKey:(BOOL)flag
1133 {
1134     windowIsKey = flag;
1135     NSView *documentView = [[frame frameView] documentView];
1136     if ([documentView isKindOfClass:[WebHTMLView class]])
1137         [(WebHTMLView *)documentView _updateActiveState];
1138 }
1139
1140 - (void)setMainFrameIsFirstResponder:(BOOL)flag
1141 {
1142     NSView *documentView = [[frame frameView] documentView];
1143     
1144     NSResponder *firstResponder = flag ? documentView : nil;
1145     [[[frame webView] window] makeFirstResponder:firstResponder];
1146         
1147     if ([documentView isKindOfClass:[WebHTMLView class]])
1148         [(WebHTMLView *)documentView _updateActiveState];
1149 }
1150
1151 - (void)display
1152 {
1153     displayWebView();
1154 }
1155
1156 - (void)testRepaint
1157 {
1158     testRepaint = YES;
1159 }
1160
1161 - (void)repaintSweepHorizontally
1162 {
1163     repaintSweepHorizontally = YES;
1164 }
1165
1166 - (id)invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)args
1167 {
1168     return nil;
1169 }
1170
1171 - (void)_addWorkForTarget:(id)target selector:(SEL)selector arg1:(id)arg1 arg2:(id)arg2
1172 {
1173     if (workQueueFrozen)
1174         return;
1175     NSMethodSignature *sig = [target methodSignatureForSelector:selector];
1176     NSInvocation *work = [NSInvocation invocationWithMethodSignature:sig];
1177     [work retainArguments];
1178     [work setTarget:target];
1179     [work setSelector:selector];
1180     if (arg1) {
1181         [work setArgument:&arg1 atIndex:2];
1182         if (arg2)
1183             [work setArgument:&arg2 atIndex:3];
1184     }
1185     [workQueue addObject:work];
1186 }
1187
1188 - (void)_doLoad:(NSURL *)url target:(NSString *)target
1189 {
1190     WebFrame *targetFrame;
1191     if (target && ![target isKindOfClass:[WebUndefined class]])
1192         targetFrame = [frame findFrameNamed:target];
1193     else
1194         targetFrame = frame;
1195     [targetFrame loadRequest:[NSURLRequest requestWithURL:url]];
1196 }
1197
1198 - (void)_doBackOrForwardNavigation:(NSNumber *)index
1199 {
1200     int bfIndex = [index intValue];
1201     if (bfIndex == 1)
1202         [[frame webView] goForward];
1203     if (bfIndex == -1)
1204         [[frame webView] goBack];
1205     else {        
1206         WebBackForwardList *bfList = [[frame webView] backForwardList];
1207         [[frame webView] goToBackForwardItem:[bfList itemAtIndex:bfIndex]];
1208     }
1209 }
1210
1211 - (void)queueBackNavigation:(int)howFarBack
1212 {
1213     [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:-howFarBack] arg2:nil];
1214 }
1215
1216 - (void)queueForwardNavigation:(int)howFarForward
1217 {
1218     [self _addWorkForTarget:self selector:@selector(_doBackOrForwardNavigation:) arg1:[NSNumber numberWithInt:howFarForward] arg2:nil];
1219 }
1220
1221 - (void)queueReload
1222 {
1223     [self _addWorkForTarget:[frame webView] selector:@selector(reload:) arg1:self arg2:nil];
1224 }
1225
1226 - (void)queueScript:(NSString *)script
1227 {
1228     [self _addWorkForTarget:[frame webView] selector:@selector(stringByEvaluatingJavaScriptFromString:) arg1:script arg2:nil];
1229 }
1230
1231 - (void)queueLoad:(NSString *)URLString target:(NSString *)target
1232 {
1233     NSURL *URL = [NSURL URLWithString:URLString relativeToURL:[[[frame dataSource] response] URL]];
1234     [self _addWorkForTarget:self selector:@selector(_doLoad:target:) arg1:URL arg2:target];
1235 }
1236
1237 - (void)setAcceptsEditing:(BOOL)newAcceptsEditing
1238 {
1239     [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:newAcceptsEditing];
1240 }
1241
1242 - (void)setTabKeyCyclesThroughElements:(BOOL)newTabKeyCyclesThroughElements
1243 {
1244     [[frame webView] setTabKeyCyclesThroughElements:newTabKeyCyclesThroughElements];
1245 }
1246
1247 - (void)storeWebScriptObject:(WebScriptObject *)webScriptObject
1248 {
1249     if (webScriptObject == storedWebScriptObject)
1250         return;
1251
1252     [storedWebScriptObject release];
1253     storedWebScriptObject = [webScriptObject retain];
1254 }
1255
1256 - (void)accessStoredWebScriptObject
1257 {
1258     [storedWebScriptObject callWebScriptMethod:@"" withArguments:nil];
1259     [storedWebScriptObject evaluateWebScript:@""];
1260     [storedWebScriptObject setValue:[WebUndefined undefined] forKey:@"key"];
1261     [storedWebScriptObject valueForKey:@"key"];
1262     [storedWebScriptObject removeWebScriptKey:@"key"];
1263     [storedWebScriptObject stringRepresentation];
1264     [storedWebScriptObject webScriptValueAtIndex:0];
1265     [storedWebScriptObject setWebScriptValueAtIndex:0 value:[WebUndefined undefined]];
1266     [storedWebScriptObject setException:@"exception"];
1267 }
1268
1269 - (void)dealloc
1270 {
1271     [storedWebScriptObject release];
1272     [super dealloc];
1273 }
1274
1275 - (NSString *)objCClassNameOf:(id)object
1276 {
1277     return NSStringFromClass([object class]);
1278 }
1279
1280 @end
1281
1282 static void runTest(const char *pathOrURL)
1283 {
1284     CFStringRef pathOrURLString = CFStringCreateWithCString(NULL, pathOrURL, kCFStringEncodingUTF8);
1285     if (!pathOrURLString) {
1286         fprintf(stderr, "can't parse filename as UTF-8\n");
1287         return;
1288     }
1289
1290     CFURLRef URL;
1291     if (CFStringHasPrefix(pathOrURLString, CFSTR("http://")))
1292         URL = CFURLCreateWithString(NULL, pathOrURLString, NULL);
1293     else
1294         URL = CFURLCreateWithFileSystemPath(NULL, pathOrURLString, kCFURLPOSIXPathStyle, FALSE);
1295     
1296     if (!URL) {
1297         CFRelease(pathOrURLString);
1298         fprintf(stderr, "can't turn %s into a CFURL\n", pathOrURL);
1299         return;
1300     }
1301
1302     [(EditingDelegate *)[[frame webView] editingDelegate] setAcceptsEditing:YES];
1303     [[frame webView] setTabKeyCyclesThroughElements: YES];
1304     done = NO;
1305     topLoadingFrame = nil;
1306     waitToDump = NO;
1307     dumpAsText = NO;
1308     dumpDOMAsWebArchive = NO;
1309     dumpSourceAsWebArchive = NO;
1310     dumpChildFrameScrollPositions = NO;
1311     shouldDumpEditingCallbacks = NO;
1312     shouldDumpResourceLoadCallbacks = NO;
1313     dumpSelectionRect = NO;
1314     dumpTitleChanges = NO;
1315     dumpBackForwardList = NO;
1316     readFromWindow = NO;
1317     testRepaint = testRepaintDefault;
1318     repaintSweepHorizontally = repaintSweepHorizontallyDefault;
1319     if ([WebHistory optionalSharedHistory])
1320         [WebHistory setOptionalSharedHistory:nil];
1321     lastMousePosition = NSMakePoint(0, 0);
1322
1323     if (currentTest != nil)
1324         CFRelease(currentTest);
1325     currentTest = (NSString *)pathOrURLString;
1326     [prevTestBFItem release];
1327     prevTestBFItem = [[[[frame webView] backForwardList] currentItem] retain];
1328     [workQueue removeAllObjects];
1329     workQueueFrozen = NO;
1330
1331     BOOL _shouldIgnoreWebCoreNodeLeaks = shouldIgnoreWebCoreNodeLeaks(CFURLGetString(URL));
1332     if (_shouldIgnoreWebCoreNodeLeaks)
1333         [WebCoreStatistics startIgnoringWebCoreNodeLeaks];
1334
1335     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1336     [frame loadRequest:[NSURLRequest requestWithURL:(NSURL *)URL]];
1337     CFRelease(URL);
1338     [pool release];
1339     while (!done) {
1340         pool = [[NSAutoreleasePool alloc] init];
1341         [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantPast]];
1342         [pool release];
1343     }
1344     pool = [[NSAutoreleasePool alloc] init];
1345     [[frame webView] setSelectedDOMRange:nil affinity:NSSelectionAffinityDownstream];
1346     [pool release];
1347     
1348     if (_shouldIgnoreWebCoreNodeLeaks)
1349         [WebCoreStatistics stopIgnoringWebCoreNodeLeaks];
1350 }
1351
1352 /* Hashes a bitmap and returns a text string for comparison and saving to a file */
1353 static NSString *md5HashStringForBitmap(CGImageRef bitmap)
1354 {
1355     MD5_CTX md5Context;
1356     unsigned char hash[16];
1357     
1358     unsigned bitsPerPixel = CGImageGetBitsPerPixel(bitmap);
1359     assert(bitsPerPixel == 32); // ImageDiff assumes 32 bit RGBA, we must as well.
1360     unsigned bytesPerPixel = bitsPerPixel / 8;
1361     unsigned pixelsHigh = CGImageGetHeight(bitmap);
1362     unsigned pixelsWide = CGImageGetWidth(bitmap);
1363     unsigned bytesPerRow = CGImageGetBytesPerRow(bitmap);
1364     assert(bytesPerRow >= (pixelsWide * bytesPerPixel));
1365     
1366     MD5_Init(&md5Context);
1367     unsigned char *bitmapData = screenCaptureBuffer;
1368     for (unsigned row = 0; row < pixelsHigh; row++) {
1369         MD5_Update(&md5Context, bitmapData, pixelsWide * bytesPerPixel);
1370         bitmapData += bytesPerRow;
1371     }
1372     MD5_Final(hash, &md5Context);
1373     
1374     char hex[33] = "";
1375     for (int i = 0; i < 16; i++) {
1376        snprintf(hex, 33, "%s%02x", hex, hash[i]);
1377     }
1378
1379     return [NSString stringWithUTF8String:hex];
1380 }
1381
1382 static void displayWebView()
1383 {
1384     NSView *webView = [frame webView];
1385     [webView display];
1386     [webView lockFocus];
1387     [[[NSColor blackColor] colorWithAlphaComponent:0.66] set];
1388     NSRectFillUsingOperation([webView frame], NSCompositeSourceOver);
1389     [webView unlockFocus];
1390     readFromWindow = YES;
1391 }
1392
1393 @implementation DumpRenderTreePasteboard
1394
1395 // Return a local pasteboard so we don't disturb the real pasteboards when running tests.
1396 + (NSPasteboard *)_pasteboardWithName:(NSString *)name
1397 {
1398     static int number = 0;
1399     if (!name)
1400         name = [NSString stringWithFormat:@"LocalPasteboard%d", ++number];
1401     LocalPasteboard *pasteboard = [localPasteboards objectForKey:name];
1402     if (pasteboard)
1403         return pasteboard;
1404     pasteboard = [[LocalPasteboard alloc] init];
1405     [localPasteboards setObject:pasteboard forKey:name];
1406     [pasteboard release];
1407     return pasteboard;
1408 }
1409
1410 // Convenience method for JS so that it doesn't have to try and create a NSArray on the objc side instead
1411 // of the usual WebScriptObject that is passed around
1412 - (int)declareType:(NSString *)type owner:(id)newOwner
1413 {
1414     return [self declareTypes:[NSArray arrayWithObject:type] owner:newOwner];
1415 }
1416
1417 @end
1418
1419 @implementation LocalPasteboard
1420
1421 + (id)alloc
1422 {
1423     return NSAllocateObject(self, 0, 0);
1424 }
1425
1426 - (id)init
1427 {
1428     typesArray = [[NSMutableArray alloc] init];
1429     typesSet = [[NSMutableSet alloc] init];
1430     dataByType = [[NSMutableDictionary alloc] init];
1431     return self;
1432 }
1433
1434 - (void)dealloc
1435 {
1436     [typesArray release];
1437     [typesSet release];
1438     [dataByType release];
1439     [super dealloc];
1440 }
1441
1442 - (NSString *)name
1443 {
1444     return nil;
1445 }
1446
1447 - (void)releaseGlobally
1448 {
1449 }
1450
1451 - (int)declareTypes:(NSArray *)newTypes owner:(id)newOwner
1452 {
1453     [typesArray removeAllObjects];
1454     [typesSet removeAllObjects];
1455     [dataByType removeAllObjects];
1456     return [self addTypes:newTypes owner:newOwner];
1457 }
1458
1459 - (int)addTypes:(NSArray *)newTypes owner:(id)newOwner
1460 {
1461     unsigned count = [newTypes count];
1462     unsigned i;
1463     for (i = 0; i < count; ++i) {
1464         NSString *type = [newTypes objectAtIndex:i];
1465         NSString *setType = [typesSet member:type];
1466         if (!setType) {
1467             setType = [type copy];
1468             [typesArray addObject:setType];
1469             [typesSet addObject:setType];
1470             [setType release];
1471         }
1472         if (newOwner && [newOwner respondsToSelector:@selector(pasteboard:provideDataForType:)])
1473             [newOwner pasteboard:self provideDataForType:setType];
1474     }
1475     return ++changeCount;
1476 }
1477
1478 - (int)changeCount
1479 {
1480     return changeCount;
1481 }
1482
1483 - (NSArray *)types
1484 {
1485     return typesArray;
1486 }
1487
1488 - (NSString *)availableTypeFromArray:(NSArray *)types
1489 {
1490     unsigned count = [types count];
1491     unsigned i;
1492     for (i = 0; i < count; ++i) {
1493         NSString *type = [types objectAtIndex:i];
1494         NSString *setType = [typesSet member:type];
1495         if (setType)
1496             return setType;
1497     }
1498     return nil;
1499 }
1500
1501 - (BOOL)setData:(NSData *)data forType:(NSString *)dataType
1502 {
1503     if (data == nil)
1504         data = [NSData data];
1505     if (![typesSet containsObject:dataType])
1506         return NO;
1507     [dataByType setObject:data forKey:dataType];
1508     ++changeCount;
1509     return YES;
1510 }
1511
1512 - (NSData *)dataForType:(NSString *)dataType
1513 {
1514     return [dataByType objectForKey:dataType];
1515 }
1516
1517 - (BOOL)setPropertyList:(id)propertyList forType:(NSString *)dataType;
1518 {
1519     CFDataRef data = NULL;
1520     if (propertyList)
1521         data = CFPropertyListCreateXMLData(NULL, propertyList);
1522     BOOL result = [self setData:(NSData *)data forType:dataType];
1523     if (data)
1524         CFRelease(data);
1525     return result;
1526 }
1527
1528 - (BOOL)setString:(NSString *)string forType:(NSString *)dataType
1529 {
1530     CFDataRef data = NULL;
1531     if (string) {
1532         if ([string length] == 0)
1533             data = CFDataCreate(NULL, NULL, 0);
1534         else
1535             data = CFStringCreateExternalRepresentation(NULL, (CFStringRef)string, kCFStringEncodingUTF8, 0);
1536     }
1537     BOOL result = [self setData:(NSData *)data forType:dataType];
1538     if (data)
1539         CFRelease(data);
1540     return result;
1541 }
1542
1543 @end
1544
1545 @implementation DumpRenderTreeWindow
1546
1547 - (BOOL)isKeyWindow
1548 {
1549     return windowIsKey;
1550 }
1551
1552 - (void)keyDown:(id)sender
1553 {
1554     // Do nothing, avoiding the beep we'd otherwise get from NSResponder,
1555     // once we get to the end of the responder chain.
1556 }
1557
1558 @end
1559
1560 @implementation DumpRenderTreeEvent
1561
1562 + (NSPoint)mouseLocation
1563 {
1564     return [[[frame webView] window] convertBaseToScreen:lastMousePosition];
1565 }
1566
1567 @end