Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebKitTools / DrawTest / TestController.m
blob506920bc6ee2dc911f2fa1d8e20d116ec69027e8
1 /*
2  * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2006 Nefaur Khandker <nefaurk@gmail.com>  All rights reserved.
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  * 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  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
25  */
27 #import "TestController.h"
28 #import "SVGTest.h"
29 #import "TestViewerSplitView.h"
30 #import "ScalingImageView.h"
31 #import "DrawTestView.h"
33 #import <WebKit/WebView.h>
35 @interface NSArray (TestControllerAdditions)
36 - (id)firstObject;
37 @end
39 @implementation NSArray (TestControllerAdditions)
40 - (id)firstObject
42     if ([self count])
43         return [self objectAtIndex:0];
44     return nil;
46 @end
48 static TestController *__sharedInstance = nil;
50 @implementation TestController
52 - (id)init
54     if (self = [super init]) {
55         NSString *path = [[NSUserDefaults standardUserDefaults] objectForKey:@"TestDirectory"];
56         BOOL isDirectory = NO;
57         if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] || !isDirectory) {
58             path = [@"~" stringByStandardizingPath];
59         }
60         [self setCurrentPath:path];
61     }
62     return self;
65 + (void)initialize
67     [self setKeys:[NSArray arrayWithObject:@"currentPath"] triggerChangeNotificationsForDependentKey:@"directoryHierarchy"];
68     [self setKeys:[NSArray arrayWithObject:@"currentPath"] triggerChangeNotificationsForDependentKey:@"tests"];
71 + (id)sharedController
73     if (!__sharedInstance) {
74         __sharedInstance = [[self alloc] init];
75     }
76     return __sharedInstance;
79 - (void)loadNibIfNecessary
81     if (!_testPanel) {
82         [NSBundle loadNibNamed:@"TestViewer" owner:self];
83     }
86 - (void)awakeFromNib
88     [_testsTableView setTarget:self];
89     [_testsTableView setDoubleAction:@selector(openTestViewerForSelection:)];
90     _drawView = [[DrawTestView alloc] initWithFrame:NSZeroRect];
91     _imageView = [[ScalingImageView alloc] initWithFrame:NSZeroRect];
92     [_splitView addSubview:_drawView];
93     [_splitView addSubview:_imageView];
96 - (IBAction)showTestsPanel:(id)sender
98     [self loadNibIfNecessary];
99     [_testPanel makeKeyAndOrderFront:sender];
102 - (IBAction)showTestWindow:(id)sender
104     [self loadNibIfNecessary];
105     [_testWindow makeKeyAndOrderFront:sender];
108 - (IBAction)showCompositeWindow:(id)sender
110     [self loadNibIfNecessary];
111     NSLog(@"showCompositeWindow: %@", _compositeWindow);
112     [_compositeWindow makeKeyAndOrderFront:sender];
115 - (IBAction)browse:(id)sender
117     NSOpenPanel *openPanel = [NSOpenPanel openPanel];
118     [openPanel setCanChooseDirectories:YES];
119     [openPanel setCanChooseFiles:NO];
120     [openPanel beginSheetForDirectory:nil file:nil modalForWindow:_testPanel modalDelegate:self didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
123 - (void)openPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
125     if (returnCode == NSOKButton) {
126         NSArray *folders = [openPanel filenames];
127         NSString *selectedFolder = [folders firstObject];
128         [self setCurrentPath:selectedFolder];
129     }
132 - (IBAction)jumpToParentDirectory:(id)sender
134     int index = [_parentDirectoryPopup indexOfSelectedItem];
135     NSArray *components = [_currentPath pathComponents];
136     NSArray *newComponents = [components subarrayWithRange:NSMakeRange(0, [components count] - index)];
137     NSString *newPath = [NSString pathWithComponents:newComponents];
138     [self setCurrentPath:newPath];
141 - (void)setSelectedTest:(SVGTest *)selectedTest
143     id oldTest = _selectedTest;
144     _selectedTest = [selectedTest retain];
145     [oldTest release];
146     
147     if ([_testWindow isVisible]) {
148         [_testWindow setTitle:[NSString stringWithFormat:@"Test Viewer - %@", [_selectedTest name]]]; 
149         [_drawView setDocument:[NSURL fileURLWithPath:[_selectedTest svgPath]]];
150         [_imageView setImage:[_selectedTest image]];
151         if ([_compositeWindow isVisible])
152             [_compositeImageView setImage:[_selectedTest compositeImage]];
153     }
156 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
158     [self setSelectedTest:[[_testsArrayController selectedObjects] firstObject]];
161 - (IBAction)openTestViewerForSelection:(id)sender
163     [self showTestWindow:sender];
164     [_drawView setDocument:[NSURL fileURLWithPath:[_selectedTest svgPath]]];
165     [_imageView setImage:[_selectedTest image]];
168 - (IBAction)openSourceForSelection:(id)sender
170     [[NSWorkspace sharedWorkspace] openFile:[_selectedTest svgPath] withApplication:@"TextEdit"];
173 - (IBAction)openSelectionInViewer:(id)sender
175     [[NSWorkspace sharedWorkspace] openFile:[_selectedTest svgPath]];
178 - (NSString *)imagePathForSVGPath:(NSString *)svgPath
180     // eventually this code will build an array instead...
181     
182     NSString *currentDirectory = [self currentPath];
183     NSString *parentDirectory = [currentDirectory stringByDeletingLastPathComponent];
184     
185     NSString *testName = [[svgPath lastPathComponent] stringByDeletingPathExtension];
186     NSString *imageName, *imageDirectory, *imagePath;
187     
188     // first look in ../png/test.png -- SVG 1.1 baselines
189     // The SVG 1.1 spec has various different pngs, we should allow the
190     // tester to choose...
191     imageName = [[@"full-" stringByAppendingString:testName] stringByAppendingPathExtension:@"png"];
192     imageDirectory = [parentDirectory stringByAppendingPathComponent:@"png"];
193     imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
194     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
195     
196     // then look for ../name.png -- openclipart.org
197     imageName = [testName stringByAppendingPathExtension:@"png"];
198     imageDirectory = parentDirectory;
199     imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
200     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
201     
202     // then look for ./name-w3c.png -- WebCore tests
203     imageName = [[testName stringByAppendingString:@"-w3c"] stringByAppendingPathExtension:@"png"];
204     imageDirectory = currentDirectory;
205     imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
206     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
207     
208     // finally try name-baseline.png -- ksvg regression baselines
209     imageName = [[testName stringByAppendingString:@"-baseline"] stringByAppendingPathExtension:@"png"];
210     imageDirectory = currentDirectory;
211     imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
212     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
213     
214     return nil;
217 - (NSArray *)tests
219     if (!_tests) {
220         NSMutableArray *newTests = [[NSMutableArray alloc] init];
221         NSArray *files = [[NSFileManager defaultManager] directoryContentsAtPath:[self currentPath]];
222         NSString *file = nil;
223         foreacharray(file, files) {
224             if ([[file pathExtension] isEqualToString:@"svg"]) {
225                 NSString *svgPath = [[self currentPath] stringByAppendingPathComponent:file];
226                 NSString *imagePath = [self imagePathForSVGPath:svgPath];
227                 [newTests addObject:[SVGTest testWithSVGPath:svgPath imagePath:imagePath]];
228             }
229         }
230         [self setValue:newTests forKey:@"tests"];
231     }
232     return _tests;
235 - (NSArray *)directoryHierarchy
237     // A hackish way to reverse an array.
238     return [[[_currentPath pathComponents] reverseObjectEnumerator] allObjects];
241 - (NSString *)currentPath
243     return _currentPath;
246 - (void)setCurrentPath:(NSString *)newPath
248     if (![newPath isEqualToString:_currentPath]) {
249         [_currentPath release];
250         _currentPath = [newPath copy];
251         [self setValue:nil forKey:@"tests"];
252     }
253     
254     [[NSUserDefaults standardUserDefaults] setObject:_currentPath forKey:@"TestDirectory"];
257 - (IBAction)toggleViewersScaleRule:(id)sender
259 #if 0
260     if ([_drawView imageScaling] == NSScaleProportionally) {
261         [_drawView setImageScaling:NSScaleNone];
262         [_imageView setImageScaling:NSScaleNone];
263     } else {
264         [_drawView setImageScaling:NSScaleProportionally];
265         [_imageView setImageScaling:NSScaleProportionally];
266     }
267 #endif
270 @end