197528f88ce681376ffc2966edc2dcf374505e3f
[WebKit-https.git] / WebKitTools / DrawTest / TestController.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  * 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.
12  *
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. 
24  */
25
26 #import "TestController.h"
27 #import "SVGTest.h"
28 #import "TestViewerSplitView.h"
29 #import "ScalingImageView.h"
30
31 #import <WebCore+SVG/DrawView.h>
32
33 @interface NSArray (TestControllerAdditions)
34 - (id)firstObject;
35 @end
36
37 @implementation NSArray (TestControllerAdditions)
38 - (id)firstObject
39 {
40     if ([self count])
41         return [self objectAtIndex:0];
42     return nil;
43 }
44 @end
45
46 static TestController *__sharedInstance = nil;
47
48 @implementation TestController
49
50 - (id)init
51 {
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];
57         }
58         [self setCurrentPath:path];
59     }
60     return self;
61 }
62
63 + (void)initialize
64 {
65     [self setKeys:[NSArray arrayWithObject:@"currentPath"] triggerChangeNotificationsForDependentKey:@"directoryHierarchy"];
66     [self setKeys:[NSArray arrayWithObject:@"currentPath"] triggerChangeNotificationsForDependentKey:@"tests"];
67 }
68
69 + (id)sharedController
70 {
71     if (!__sharedInstance) {
72         __sharedInstance = [[self alloc] init];
73     }
74     return __sharedInstance;
75 }
76
77 - (void)loadNibIfNecessary
78 {
79     if (!_testPanel) {
80         [NSBundle loadNibNamed:@"TestViewer" owner:self];
81     }
82 }
83
84 - (void)awakeFromNib
85 {
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];
92 }
93
94 - (IBAction)showTestsPanel:(id)sender
95 {
96     [self loadNibIfNecessary];
97     [_testPanel makeKeyAndOrderFront:sender];
98 }
99
100 - (IBAction)showTestWindow:(id)sender
101 {
102     [self loadNibIfNecessary];
103     [_testWindow makeKeyAndOrderFront:sender];
104 }
105
106 - (IBAction)showCompositeWindow:(id)sender
107 {
108     [self loadNibIfNecessary];
109     NSLog(@"showCompositeWindow: %@", _compositeWindow);
110     [_compositeWindow makeKeyAndOrderFront:sender];
111 }
112
113 - (IBAction)browse:(id)sender
114 {
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];
119 }
120
121 - (void)openPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
122 {
123     if (returnCode == NSOKButton) {
124         NSArray *folders = [openPanel filenames];
125         NSString *selectedFolder = [folders firstObject];
126         [self setCurrentPath:selectedFolder];
127     }
128 }
129
130 - (IBAction)jumpToParentDirectory:(id)sender
131 {
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];
137 }
138
139 - (void)setSelectedTest:(SVGTest *)selectedTest
140 {
141     id oldTest = _selectedTest;
142     _selectedTest = [selectedTest retain];
143     [oldTest release];
144     
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]];
151     }
152 }
153
154 - (void)tableViewSelectionDidChange:(NSNotification *)aNotification
155 {
156     [self setSelectedTest:[[_testsArrayController selectedObjects] firstObject]];
157 }
158
159 - (IBAction)openTestViewerForSelection:(id)sender
160 {
161     [self showTestWindow:sender];
162     [_drawView setDocument:[_selectedTest svgDocument]];
163     [_imageView setImage:[_selectedTest image]];
164 }
165
166 - (IBAction)openSourceForSelection:(id)sender
167 {
168     [[NSWorkspace sharedWorkspace] openFile:[_selectedTest svgPath] withApplication:@"TextEdit"];
169 }
170
171 - (IBAction)openSelectionInViewer:(id)sender
172 {
173     [[NSWorkspace sharedWorkspace] openFile:[_selectedTest svgPath]];
174 }
175      
176 - (NSString *)imagePathForSVGPath:(NSString *)svgPath
177 {
178     // eventually this code will build an array instead...
179     
180     NSString *svgName = [svgPath lastPathComponent];
181     NSString *testName = [svgName stringByDeletingPathExtension];
182     NSString *imageName = [testName stringByAppendingPathExtension:@"png"];
183     NSString *currentDirectory = [self currentPath];
184     NSString *parentDirectory = [currentDirectory stringByDeletingLastPathComponent];
185     
186     // first look for ../png/name.png -- SVG 1.1 baselines
187     NSString *imageDirectory = [parentDirectory stringByAppendingPathComponent:@"png"];
188     NSString *fullImageName = [@"full-" stringByAppendingString:imageName];
189     // The SVG 1.1 spec has various differnet pngs, we should allow the
190     // tester to choose...
191     NSString *imagePath = [imageDirectory stringByAppendingPathComponent:fullImageName];
192     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
193     
194     // then look for ../name.png -- openclipart.org
195     imagePath = [currentDirectory stringByAppendingPathComponent:imageName];
196     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
197     
198     // finally try name-baseline.png -- ksvg regression baselines
199     testName = [testName stringByAppendingString:@"-baseline"];
200     imageName = [testName stringByAppendingPathExtension:@"png"];
201     imagePath = [currentDirectory stringByAppendingPathComponent:imageName];
202     if ([[NSFileManager defaultManager] fileExistsAtPath:imagePath]) return imagePath;
203     
204     return nil;
205 }
206
207 - (NSArray *)tests
208 {
209     if (!_tests) {
210         NSMutableArray *newTests = [[NSMutableArray alloc] init];
211         NSArray *files = [[NSFileManager defaultManager] directoryContentsAtPath:[self currentPath]];
212         NSString *file = nil;
213         foreacharray(file, files) {
214             if ([[file pathExtension] isEqualToString:@"svg"]) {
215                 NSString *svgPath = [[self currentPath] stringByAppendingPathComponent:file];
216                 NSString *imagePath = [self imagePathForSVGPath:svgPath];
217                 [newTests addObject:[SVGTest testWithSVGPath:svgPath imagePath:imagePath]];
218             }
219         }
220         [self setValue:newTests forKey:@"tests"];
221     }
222     return _tests;
223 }
224
225 - (NSArray *)directoryHierarchy
226 {
227     // A hackish way to reverse an array.
228     return [[[_currentPath pathComponents] reverseObjectEnumerator] allObjects];
229 }
230
231 - (NSString *)currentPath
232 {
233     return _currentPath;
234 }
235
236 - (void)setCurrentPath:(NSString *)newPath
237 {
238     if (![newPath isEqualToString:_currentPath]) {
239         [_currentPath release];
240         _currentPath = [newPath copy];
241         [self setValue:nil forKey:@"tests"];
242     }
243     
244     [[NSUserDefaults standardUserDefaults] setObject:_currentPath forKey:@"TestDirectory"];
245 }
246
247 - (IBAction)toggleViewersScaleRule:(id)sender
248 {
249     if ([_drawView imageScaling] == NSScaleProportionally) {
250         [_drawView setImageScaling:NSScaleNone];
251         [_imageView setImageScaling:NSScaleNone];
252     } else {
253         [_drawView setImageScaling:NSScaleProportionally];
254         [_imageView setImageScaling:NSScaleProportionally];
255     }
256 }
257
258 @end