2 * Copyright (C) 2005 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #import "TestController.h"
28 #import "TestViewerSplitView.h"
29 #import "ScalingImageView.h"
31 #import <WebCore/DrawView.h>
33 @interface NSArray (TestControllerAdditions)
37 @implementation NSArray (TestControllerAdditions)
41 return [self objectAtIndex:0];
46 static TestController *__sharedInstance = nil;
48 @implementation TestController
52 if (self = [super init]) {
53 NSString *path = [[NSUserDefaults standardUserDefaults] objectForKey:@"TestDirectory"];
54 BOOL isDirectory = NO;
55 if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory] || !isDirectory) {
56 path = [@"~" stringByStandardizingPath];
58 [self setCurrentPath:path];
65 [self setKeys:[NSArray arrayWithObject:@"currentPath"] triggerChangeNotificationsForDependentKey:@"directoryHierarchy"];
66 [self setKeys:[NSArray arrayWithObject:@"currentPath"] triggerChangeNotificationsForDependentKey:@"tests"];
69 + (id)sharedController
71 if (!__sharedInstance) {
72 __sharedInstance = [[self alloc] init];
74 return __sharedInstance;
77 - (void)loadNibIfNecessary
80 [NSBundle loadNibNamed:@"TestViewer" owner:self];
86 [_testsTableView setTarget:self];
87 [_testsTableView setDoubleAction:@selector(openTestViewerForSelection:)];
88 _drawView = [[DrawView alloc] initWithFrame:NSZeroRect];
89 _imageView = [[ScalingImageView alloc] initWithFrame:NSZeroRect];
90 [_splitView addSubview:_drawView];
91 [_splitView addSubview:_imageView];
94 - (IBAction)showTestsPanel:(id)sender
96 [self loadNibIfNecessary];
97 [_testPanel makeKeyAndOrderFront:sender];
100 - (IBAction)showTestWindow:(id)sender
102 [self loadNibIfNecessary];
103 [_testWindow makeKeyAndOrderFront:sender];
106 - (IBAction)showCompositeWindow:(id)sender
108 [self loadNibIfNecessary];
109 NSLog(@"showCompositeWindow: %@", _compositeWindow);
110 [_compositeWindow makeKeyAndOrderFront:sender];
113 - (IBAction)browse:(id)sender
115 NSOpenPanel *openPanel = [NSOpenPanel openPanel];
116 [openPanel setCanChooseDirectories:YES];
117 [openPanel setCanChooseFiles:NO];
118 [openPanel beginSheetForDirectory:nil file:nil modalForWindow:_testPanel modalDelegate:self didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
121 - (void)openPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
123 if (returnCode == NSOKButton) {
124 NSArray *folders = [openPanel filenames];
125 NSString *selectedFolder = [folders firstObject];
126 [self setCurrentPath:selectedFolder];
130 - (IBAction)jumpToParentDirectory:(id)sender
132 int index = [_parentDirectoryPopup indexOfSelectedItem];
133 NSArray *components = [_currentPath pathComponents];
134 NSArray *newComponents = [components subarrayWithRange:NSMakeRange(0, [components count] - index)];
135 NSString *newPath = [NSString pathWithComponents:newComponents];
136 [self setCurrentPath:newPath];
139 - (void)setSelectedTest:(SVGTest *)selectedTest
141 id oldTest = _selectedTest;
142 _selectedTest = [selectedTest retain];
145 if ([_testWindow isVisible]) {
146 [_testWindow setTitle:[NSString stringWithFormat:@"Test Viewer - %@", [_selectedTest name]]];
147 [_drawView setDocument:[_selectedTest svgDocument]];
148 [_imageView setImage:[_selectedTest image]];
149 if ([_compositeWindow isVisible])
150 [_compositeImageView setImage:[_selectedTest compositeImage]];
154 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
156 [self setSelectedTest:[[_testsArrayController selectedObjects] firstObject]];
159 - (IBAction)openTestViewerForSelection:(id)sender
161 [self showTestWindow:sender];
162 [_drawView setDocument:[_selectedTest svgDocument]];
163 [_imageView setImage:[_selectedTest image]];
166 - (IBAction)openSourceForSelection:(id)sender
168 [[NSWorkspace sharedWorkspace] openFile:[_selectedTest svgPath] withApplication:@"TextEdit"];
171 - (IBAction)openSelectionInViewer:(id)sender
173 [[NSWorkspace sharedWorkspace] openFile:[_selectedTest svgPath]];
176 - (NSString *)imagePathForSVGPath:(NSString *)svgPath
178 // eventually this code will build an array instead...
180 NSString *currentDirectory = [self currentPath];
181 NSString *parentDirectory = [currentDirectory stringByDeletingLastPathComponent];
183 NSString *testName = [[svgPath lastPathComponent] stringByDeletingPathExtension];
184 NSString *imageName, *imageDirectory, *imagePath;
186 // first look in ../png/test.png -- SVG 1.1 baselines
187 // The SVG 1.1 spec has various different pngs, we should allow the
188 // tester to choose...
189 imageName = [[@"full-" stringByAppendingString:testName] stringByAppendingPathExtension:@"png"];
190 imageDirectory = [parentDirectory stringByAppendingPathComponent:@"png"];
191 imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
192 if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
194 // then look for ../name.png -- openclipart.org
195 imageName = [testName stringByAppendingPathExtension:@"png"];
196 imageDirectory = parentDirectory;
197 imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
198 if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
200 // then look for ./name-w3c.png -- WebCore tests
201 imageName = [[testName stringByAppendingString:@"-w3c"] stringByAppendingPathExtension:@"png"];
202 imageDirectory = currentDirectory;
203 imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
204 if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
206 // finally try name-baseline.png -- ksvg regression baselines
207 imageName = [[testName stringByAppendingString:@"-baseline"] stringByAppendingPathExtension:@"png"];
208 imageDirectory = currentDirectory;
209 imagePath = [imageDirectory stringByAppendingPathComponent:imageName];
210 if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
218 NSMutableArray *newTests = [[NSMutableArray alloc] init];
219 NSArray *files = [[NSFileManager defaultManager] directoryContentsAtPath:[self currentPath]];
220 NSString *file = nil;
221 foreacharray(file, files) {
222 if ([[file pathExtension] isEqualToString:@"svg"]) {
223 NSString *svgPath = [[self currentPath] stringByAppendingPathComponent:file];
224 NSString *imagePath = [self imagePathForSVGPath:svgPath];
225 [newTests addObject:[SVGTest testWithSVGPath:svgPath imagePath:imagePath]];
228 [self setValue:newTests forKey:@"tests"];
233 - (NSArray *)directoryHierarchy
235 // A hackish way to reverse an array.
236 return [[[_currentPath pathComponents] reverseObjectEnumerator] allObjects];
239 - (NSString *)currentPath
244 - (void)setCurrentPath:(NSString *)newPath
246 if (![newPath isEqualToString:_currentPath]) {
247 [_currentPath release];
248 _currentPath = [newPath copy];
249 [self setValue:nil forKey:@"tests"];
252 [[NSUserDefaults standardUserDefaults] setObject:_currentPath forKey:@"TestDirectory"];
255 - (IBAction)toggleViewersScaleRule:(id)sender
257 if ([_drawView imageScaling] == NSScaleProportionally) {
258 [_drawView setImageScaling:NSScaleNone];
259 [_imageView setImageScaling:NSScaleNone];
261 [_drawView setImageScaling:NSScaleProportionally];
262 [_imageView setImageScaling:NSScaleProportionally];