-
Notifications
You must be signed in to change notification settings - Fork 11
/
TSServiceTestWindowController.m
242 lines (195 loc) · 8.5 KB
/
TSServiceTestWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// TSServiceTestWindowController.m
// ThisService
//
// Created by Jesper on 2012-07-05.
// Copyright 2012 waffle software. All rights reserved.
// BSD licensed - see license.txt for more information.
//
//
#import "TSServiceTestWindowController.h"
#import "TSService.h"
#import "WFMachTime.h"
@interface TSServiceTestWindowController ()
@end
@implementation TSServiceTestWindowController
@synthesize logTextView = _logTextView;
@synthesize durationLabel = _durationLabel;
@synthesize noInputBox = _noInputBox;
@synthesize noOutputBox = _noOutputBox;
@synthesize inputLabel = _inputLabel;
@synthesize inputInstructionsLabel = _inputInstructionsLabel;
@synthesize outputLabel = _outputLabel;
@synthesize statusLabel = _statusLabel;
@synthesize statusIndicator = _statusIndicator;
@synthesize outputDirtyLabel = _outputDirtyLabel;
@synthesize testServiceHeadlineLabel = _testServiceHeadlineLabel;
@synthesize inputTextView = _inputTextView;
@synthesize outputTextView = _outputTextView;
@synthesize inputTextScrollView = _inputTextScrollView;
@synthesize outputTextScrollView = _outputTextScrollView;
@synthesize createServiceButton = _createServiceButton;
@synthesize testServiceButton = _testServiceButton;
@synthesize doomed = doomed;
@synthesize delegate = _delegate;
@synthesize previousRunTime = _previousRunTime;
@synthesize ongoingTimer = _ongoingTimer;
-(TSServiceTestWindowController *)initWithTester:(TSServiceTester *)tester
{
self = [super initWithWindowNibName:@"TSServiceTestWindowController"];
if (self) {
_tester = [tester retain];
[_tester setDelegate:self];
}
return self;
}
- (void)testerLaunchedService:(TSServiceTester *)tester {
self.statusLabel.stringValue = @"Connecting to service…";
}
- (void)updateCreateServiceButton:(BOOL)e {
[self.createServiceButton setEnabled:_pairingCompleted && e];
}
-(void)testerPairedWithService:(TSServiceTester *)tester {
self.statusLabel.stringValue = @"";
[self.statusIndicator stopAnimation:nil];
[self setTestingEnabled:YES];
_pairingCompleted = YES;
[self updateCreateServiceButton:YES];
}
- (void)textDidChange:(NSNotification *)notification {
[self.outputDirtyLabel setHidden:(self.previousRunTime == nil)];
}
-(void)testerWillInvokeService:(TSServiceTester *)tester withPasteboard:(NSPasteboard *)pasteboard {
if (supportsInput) {
[pasteboard setString:self.inputTextView.string forType:NSStringPboardType];
}
}
- (void)tester:(TSServiceTester *)tester failedWithError:(NSString *)error {
[_tester cancel];
}
- (void)testerBeganRunningService:(TSServiceTester *)tester {
[self performSelectorOnMainThread:@selector(serializedTesterBeganRunningService:) withObject:tester waitUntilDone:NO];
}
- (void)serializedTesterBeganRunningService:(TSServiceTester *)tester {
self.statusLabel.stringValue = @"Running service…";
[self setTestingEnabled:NO];
[self.statusIndicator startAnimation:nil];
}
static NSDateFormatter *durationFormatter = nil;
- (void)serializedTesterFinishedRunningServiceWithPasteboard:(NSArray *)objects {
TSServiceTester *tester = [objects objectAtIndex:0];
NSPasteboard *pasteboard = [objects objectAtIndex:1];
self.statusLabel.stringValue = @"";
[self.statusIndicator stopAnimation:nil];
if (supportsOutput) {
NSString *outputString = [pasteboard stringForType:NSStringPboardType];
if (!outputString) {
outputString = @"– No string returned from AppleScript service! Use the \"return\" statement to produce output. –";
}
[self.outputTextView setString:outputString];
[self.outputDirtyLabel setHidden:YES];
}
NSTimeInterval duration = [self.ongoingTimer intervalSinceStart];
self.previousRunTime = [NSNumber numberWithDouble:duration];
NSTimeInterval effectiveTimeout = [tester.service effectiveTimeout];
NSString *durationReading = [durationFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:duration]];
NSString *durationText = nil;
NSColor *textColor = [NSColor disabledControlTextColor];
if (duration >= effectiveTimeout) {
NSString *timeoutReading = [durationFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:effectiveTimeout]];
durationText = [NSString stringWithFormat:@"Service ran for longer than %@ timeout! %@.", timeoutReading, durationReading];
textColor = [NSColor colorWithDeviceRed:0.8 green:0 blue:0 alpha:1];
} else if (duration >= (effectiveTimeout * 0.9)) {
NSString *timeoutReading = [durationFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:effectiveTimeout]];
durationText = [NSString stringWithFormat:@"Service ran close to the %@ timeout! %@.", timeoutReading, durationReading];
textColor = [NSColor colorWithDeviceRed:0.5 green:0.3 blue:0.3 alpha:1];
} else {
durationText = [NSString stringWithFormat:@"Service ran in %@.", durationReading];
}
self.durationLabel.textColor = textColor;
self.durationLabel.stringValue = durationText;
[self setTestingEnabled:YES];
[self updateCreateServiceButton:YES];
}
- (void)testerFinishedRunningService:(TSServiceTester *)tester withPasteboard:(NSPasteboard *)pasteboard {
[self performSelectorOnMainThread:@selector(serializedTesterFinishedRunningServiceWithPasteboard:) withObject:[NSArray arrayWithObjects:tester, pasteboard, nil] waitUntilDone:NO];
}
- (void)setTestingEnabled:(BOOL)enabled {
[self.inputTextView setEditable:enabled];
[self.testServiceButton setEnabled:enabled];
}
- (void)windowDidLoad
{
[super windowDidLoad];
if (!durationFormatter) {
durationFormatter = [[NSDateFormatter alloc] init];
[durationFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[durationFormatter setDateFormat:@"HH:mm:ss.SSS"];
[durationFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
}
if (![_tester startConnection]) {
self.doomed = YES;
[_delegate testing:self failedFatally:nil];
return;
}
TSService *service = _tester.service;
supportsInput = service.supportsInput;
supportsOutput = service.supportsOutput;
if (!supportsInput) {
[self.inputLabel setHidden:YES];
[self.inputTextScrollView setHidden:YES];
[self.inputInstructionsLabel setHidden:YES];
} else {
[self.noInputBox setHidden:YES];
}
if (!supportsOutput) {
[self.outputLabel setHidden:YES];
[self.outputDirtyLabel setHidden:YES];
[self.outputTextScrollView setHidden:YES];
} else {
[self.noOutputBox setHidden:YES];
}
[self setTestingEnabled:NO];
self.durationLabel.stringValue = @"";
self.testServiceHeadlineLabel.stringValue = [NSString stringWithFormat:@"Test %@", _tester.service.serviceName];
[self.statusIndicator startAnimation:nil];
self.statusLabel.stringValue = @"Launching service…";
}
- (IBAction)doTestService:(id)sender {
[self.outputDirtyLabel setHidden:YES];
self.ongoingTimer = [WFMachTime currentTime];
[self updateCreateServiceButton:NO];
BOOL retry = NO;
do {
if (![_tester startTesting]) {
NSAlert *alert = [[NSAlert alertWithMessageText:@"The service script could not be updated." defaultButton:@"Try Again" alternateButton:@"Stop Testing" otherButton:nil informativeTextWithFormat:@"Before testing the service, ThisService updates the service script from its original location at %@. No such script exists at this location so the service can't be updated with the latest version.\n\nTo continue testing the service, restore the service script to this location and click Try Again.", [[_tester service] pathToScriptOutsideService]] retain];
retry = NO;
NSInteger returnCode = [alert runModal];
[alert release];
if (returnCode == NSAlertDefaultReturn) {
retry = YES;
} else {
[self cancel:sender];
}
}
} while (retry);
}
- (IBAction)help:(id)sender {
[[NSHelpManager sharedHelpManager] openHelpAnchor:@"testhelp" inBook:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleHelpBookName"]];
}
- (IBAction)cancel:(id)sender {
[_tester cancel];
[_delegate testingCancelled:self];
}
- (IBAction)createService:(id)sender {
[_tester cancel];
[_delegate testingDone:self];
}
- (void)dealloc
{
[_previousRunTime release];
[_ongoingTimer release];
[_tester release];
[super dealloc];
}
@end