forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppleScriptTest.m
More file actions
436 lines (387 loc) · 20.1 KB
/
AppleScriptTest.m
File metadata and controls
436 lines (387 loc) · 20.1 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//
// AppleScriptTest.m
// iTerm
//
// Created by Alberto Miguel Pose on 28/12/13.
//
//
#import "AppleScriptTest.h"
#import <AppKit/NSWorkspace.h>
#import "iTermTests.h"
#import "NSStringITerm.h"
static NSString *const kTestAppName = @"iTerm2ForApplescriptTesting.app";
static NSString *const kTestBundleId = @"com.googlecode.iterm2.applescript";
@implementation AppleScriptTest
- (NSURL *)appUrl {
return [NSURL fileURLWithPath:[@"./" stringByAppendingString:kTestAppName]];
}
- (void)setup {
// ------ Arrange ------
NSURL *appURL = [self appUrl];
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
[self killTestApp];
// Nuke its prefs
NSString *defaultsDelete = [NSString stringWithFormat:@"defaults delete %@", kTestBundleId];
system([defaultsDelete UTF8String]);
// Start it up fresh
BOOL isRunning = [sharedWorkspace launchApplication:[appURL path]];
assert(isRunning);
}
- (void)teardown {
[self killTestApp];
}
- (void)killTestApp {
// Kill all running instances of iTerm2ForApplescriptTesting
pid_t thePid = 0;
for (NSRunningApplication *app in [[NSWorkspace sharedWorkspace] runningApplications]) {
if ([app.bundleIdentifier isEqualToString:kTestBundleId]) {
thePid = app.processIdentifier;
kill(app.processIdentifier, SIGKILL);
}
}
// Wait for it to die
if (thePid) {
BOOL running = NO;
do {
running = NO;
int rc = kill(thePid, 0);
if (rc && errno == ESRCH) {
running = NO;
} else {
running = YES;
usleep(100000);
}
} while (running);
}
}
- (NSString *)scriptWithCommands:(NSArray *)commands outputs:(NSArray *)outputs {
NSURL *appURL = [self appUrl];
return [NSString stringWithFormat:
@"tell application \"%@\"\n"
@" activate\n"
@" %@\n"
@"end tell\n"
@"{%@}\n",
[appURL path],
[commands componentsJoinedByString:@"\n"],
outputs ? [outputs componentsJoinedByString:@", "] : 0];
}
- (NSAppleEventDescriptor *)runScript:(NSString *)script {
NSAppleScript *appleScript = [[[NSAppleScript alloc] initWithSource:script] autorelease];
NSDictionary *errorInfo = NULL;
NSAppleEventDescriptor *eventDescriptor = [appleScript executeAndReturnError:&errorInfo];
if (errorInfo) {
NSLog(@"Script:\n%@\n\nFailed with error:\n%@",
script, errorInfo);
assert(false);
}
return eventDescriptor;
}
- (void)testCreateWindowWithDefaultProfile {
NSArray *commands = @[ @"set oldWindowCount to (count of terminal windows)",
@"create window with default profile",
@"set newWindowCount to (count of terminal windows)" ];
NSArray *outputs = @[ @"oldWindowCount", @"newWindowCount" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 0);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 1);
}
- (void)testCreateWindowWithNamedProfile {
NSArray *commands = @[ @"set oldWindowCount to (count of terminal windows)",
@"create window with profile \"Default\"",
@"set newWindowCount to (count of terminal windows)" ];
NSArray *outputs = @[ @"oldWindowCount", @"newWindowCount" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 0);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 1);
}
- (void)testCreateWindowWithDefaultProfileAndCommand {
NSArray *commands = @[ @"create window with default profile command \"touch /tmp/rancommand\"" ];
unlink("/tmp/rancommand");
NSString *script = [self scriptWithCommands:commands
outputs:nil];
[self runScript:script];
// Wait for the command to finish running. It gets half a second.
BOOL ok = NO;
for (int i = 0; i < 5; i++) {
ok = [[NSFileManager defaultManager] fileExistsAtPath:@"/tmp/rancommand"];
if (!ok) {
usleep(100000);
}
}
assert(ok);
}
- (void)testSelectWindow {
NSArray *commands = @[ @"create window with default profile",
@"tell current session of current window",
@" write text \"echo NUMBER ONE\"",
@"end tell",
@"create window with default profile",
@"tell current session of current window",
@" write text \"echo NUMBER TWO\"",
@"end tell",
@"delay 0.2", // Give write text time to echo result back
@"set secondWindowContents to (contents of current session of current window)",
@"select first terminal window",
@"set firstWindowContents to (contents of current session of current window)" ];
NSArray *outputs = @[ @"firstWindowContents", @"secondWindowContents" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
NSString *firstWindowContents = [[eventDescriptor descriptorAtIndex:1] stringValue];
NSString *secondWindowContents = [[eventDescriptor descriptorAtIndex:2] stringValue];
assert([firstWindowContents containsString:@"NUMBER ONE"]);
assert([secondWindowContents containsString:@"NUMBER TWO"]);
}
- (void)testSelectTab {
NSArray *commands = @[ @"create window with default profile",
@"tell current session of current window",
@" write text \"echo NUMBER ONE\"",
@"end tell",
@"tell current window",
@" create tab with default profile",
@"end tell",
@"tell current session of current window",
@" write text \"echo NUMBER TWO\"",
@"end tell",
@"delay 0.2", // Give write text time to echo result back
@"set secondTabContents to (contents of current session of current window)",
@"tell first tab of current window",
@" select",
@"end tell",
@"set firstTabContents to (contents of current session of current window)" ];
NSArray *outputs = @[ @"firstTabContents", @"secondTabContents" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
NSString *firstTabContents = [[eventDescriptor descriptorAtIndex:1] stringValue];
NSString *secondTabContents = [[eventDescriptor descriptorAtIndex:2] stringValue];
assert([firstTabContents containsString:@"NUMBER ONE"]);
assert([secondTabContents containsString:@"NUMBER TWO"]);
}
- (void)testSelectSession {
NSArray *commands = @[ @"create window with default profile",
@"tell current session of current window",
@" write text \"echo NUMBER ONE\"",
@"end tell",
@"tell current session of current window",
@" split horizontally with default profile",
@"end tell",
@"tell current session of current window",
@" write text \"echo NUMBER TWO\"",
@"end tell",
@"delay 0.2", // Give write text time to echo result back
@"set secondSessionContents to (contents of current session of current window)",
@"tell first session of current tab of current window",
@" select",
@"end tell",
@"set firstSessionContents to (contents of current session of current window)" ];
NSArray *outputs = @[ @"firstSessionContents", @"secondSessionContents" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
NSString *firstSessionContents = [[eventDescriptor descriptorAtIndex:1] stringValue];
NSString *secondSessionContents = [[eventDescriptor descriptorAtIndex:2] stringValue];
assert([firstSessionContents containsString:@"NUMBER ONE"]);
assert([secondSessionContents containsString:@"NUMBER TWO"]);
}
- (void)testSplitHorizontallyWithDefaultProfile {
NSArray *commands = @[ @"create window with profile \"Default\"",
@"set oldSessionCount to (count of sessions in first tab in first terminal window)",
@"tell current session of current window",
@" split horizontally with default profile",
@"end tell",
@"set newSessionCount to (count of sessions in first tab in first terminal window)" ];
NSArray *outputs = @[ @"oldSessionCount", @"newSessionCount" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 1);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 2);
}
- (void)testSplitVerticallyWithDefaultProfile {
NSArray *commands = @[ @"create window with profile \"Default\"",
@"set oldSessionCount to (count of sessions in first tab in first terminal window)",
@"tell current session of current window",
@" split vertically with default profile",
@"end tell",
@"set newSessionCount to (count of sessions in first tab in first terminal window)" ];
NSArray *outputs = @[ @"oldSessionCount", @"newSessionCount" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 1);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 2);
}
- (void)testCreateTabWithDefaultProfile {
NSArray *commands = @[ @"create window with default profile",
@"set oldTabCount to (count of tabs in first terminal window)",
@"tell current window",
@" create tab with default profile",
@"end tell",
@"set newTabCount to (count of tabs in first terminal window)" ];
NSArray *outputs = @[ @"oldTabCount", @"newTabCount" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 1);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 2);
}
- (void)testCreateTabWithNamedProfile {
NSArray *commands = @[ @"create window with default profile",
@"set oldTabCount to (count of tabs in first terminal window)",
@"tell current window",
@" create tab with profile \"Default\"",
@"end tell",
@"set newTabCount to (count of tabs in first terminal window)" ];
NSArray *outputs = @[ @"oldTabCount", @"newTabCount" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 1);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 2);
}
- (void)testResizeSession {
NSArray *commands = @[ @"create window with default profile",
@"set oldRows to (rows in current session of current window)",
@"set oldColumns to (columns in current session of current window)",
@"tell current session of current window",
@" set rows to 20",
@" set columns to 30",
@"end tell",
@"set newRows to (rows in current session of current window)",
@"set newColumns to (columns in current session of current window)" ];
NSArray *outputs = @[ @"oldRows", @"oldColumns", @"newRows", @"newColumns" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
assert(eventDescriptor);
assert([[eventDescriptor descriptorAtIndex:1] int32Value] == 25);
assert([[eventDescriptor descriptorAtIndex:2] int32Value] == 80);
assert([[eventDescriptor descriptorAtIndex:3] int32Value] == 20);
assert([[eventDescriptor descriptorAtIndex:4] int32Value] == 30);
}
- (void)testWriteContentsOfFile {
NSString *helloWorld = @"Hello world";
[helloWorld writeToFile:@"/tmp/testFile"
atomically:NO
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *commands = @[ @"create window with default profile",
@"tell current session of current window",
@" write text \"cat > /dev/null\"",
@" write contents of file \"/tmp/testFile\"",
@"end tell",
@"delay 0.2", // Give write text time to echo result back
@"set sessionContents to (contents of current session of current window)" ];
NSArray *outputs = @[ @"sessionContents" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
NSString *contents = [[eventDescriptor descriptorAtIndex:1] stringValue];
assert([contents containsString:helloWorld]);
}
- (void)testTty {
NSArray *commands = @[ @"create window with default profile",
@"set ttyName to (tty of current session of current window)" ];
NSArray *outputs = @[ @"ttyName" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
NSString *contents = [[eventDescriptor descriptorAtIndex:1] stringValue];
assert([contents hasPrefix:@"/dev/ttys"]);
}
- (void)testUniqueId {
NSArray *commands = @[ @"create window with default profile",
@"create window with default profile",
@"set firstUniqueId to (unique ID of current session of first terminal window)",
@"set secondUniqueId to (unique ID of current session of second terminal window)" ];
NSArray *outputs = @[ @"firstUniqueId", @"secondUniqueId" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
int uid1 = [[eventDescriptor descriptorAtIndex:1] int32Value];
int uid2 = [[eventDescriptor descriptorAtIndex:2] int32Value];
assert(uid1 != uid2);
}
- (void)testSetGetColors {
NSArray *colors = @[ @"foreground color",
@"background color",
@"bold color",
@"cursor color",
@"cursor text color",
@"selected text color",
@"selection color",
@"ANSI black color",
@"ANSI red color",
@"ANSI green color",
@"ANSI yellow color",
@"ANSI blue color",
@"ANSI magenta color",
@"ANSI cyan color",
@"ANSI white color",
@"ANSI bright black color",
@"ANSI bright red color",
@"ANSI bright green color",
@"ANSI bright yellow color",
@"ANSI bright blue color",
@"ANSI bright magenta color",
@"ANSI bright cyan color",
@"ANSI bright white color" ];
NSMutableArray *commands = [NSMutableArray arrayWithArray:@[ @"create window with default profile",
@"tell current session of current window" ]];
NSMutableArray *outputs = [NSMutableArray array];
for (NSString *color in colors) {
NSString *name = [color stringByReplacingOccurrencesOfString:@" " withString:@""];
[commands addObject:[NSString stringWithFormat:@"set old%@ to %@", name, color]];
[commands addObject:[NSString stringWithFormat:@"set %@ to {65535, 0, 0, 0}", color]];
[commands addObject:[NSString stringWithFormat:@"set new%@ to %@", name, color]];
[outputs addObject:[NSString stringWithFormat:@"old%@", name]];
[outputs addObject:[NSString stringWithFormat:@"new%@", name]];
}
[commands addObject:@"end tell"];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
int i = 1;
for (NSString *name in outputs) {
NSString *value = [NSString stringWithFormat:@"{%d, %d, %d, %d}",
[[[eventDescriptor descriptorAtIndex:i] descriptorAtIndex:1] int32Value],
[[[eventDescriptor descriptorAtIndex:i] descriptorAtIndex:2] int32Value],
[[[eventDescriptor descriptorAtIndex:i] descriptorAtIndex:3] int32Value],
[[[eventDescriptor descriptorAtIndex:i] descriptorAtIndex:4] int32Value]];
if ([name hasPrefix:@"old"]) {
assert(![value isEqualToString:@"{65535, 0, 0, 0}"]);
} else {
assert([value isEqualToString:@"{65535, 0, 0, 0}"]);
}
i++;
}
}
- (void)testSetGetName {
NSArray *commands = @[ @"create window with default profile",
@"set oldName to name of current session of current window",
@"tell current session of current window",
@" set name to \"Testing\"",
@"end tell",
@"set newName to name of current session of current window" ];
NSArray *outputs = @[ @"oldName", @"newName" ];
NSString *script = [self scriptWithCommands:commands
outputs:outputs];
NSAppleEventDescriptor *eventDescriptor = [self runScript:script];
NSString *oldName = [[eventDescriptor descriptorAtIndex:1] stringValue];
NSString *newName = [[eventDescriptor descriptorAtIndex:2] stringValue];
assert(![oldName isEqualToString:newName]);
assert([newName isEqualToString:@"Testing"]);
}
@end