/
iTermToolbeltTest.m
516 lines (410 loc) · 19.9 KB
/
iTermToolbeltTest.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
//
// iTermToolbeltTest.m
// iTerm2
//
// Created by George Nachman on 7/4/15.
//
//
#import <Cocoa/Cocoa.h>
#import "iTermApplication.h"
#import "iTermController.h"
#import "iTermRootTerminalView.h"
#import "iTermShellHistoryController.h"
#import "PseudoTerminal.h"
#import "PTYTab.h"
#import "ToolCapturedOutputView.h"
#import "ToolCommandHistoryView.h"
#import "ToolDirectoriesView.h"
#import "Trigger.h"
#import "VT100RemoteHost.h"
#import <XCTest/XCTest.h>
@interface iTermToolbeltTest : XCTestCase<iTermToolbeltViewDelegate>
@property(nonatomic, retain) NSString *currentDir;
@end
@implementation iTermToolbeltTest {
PseudoTerminal *_windowController; // weak
PTYSession *_session; // weak
iTermRootTerminalView *_view; // weak
NSMutableString *_insertedText;
NSString *_currentDir;
}
- (void)setUp {
[super setUp];
_insertedText = [[NSMutableString alloc] init];
_currentDir = [@"/dir" retain];
// Erase command history for the remotehost we test with.
VT100RemoteHost *host = [[[VT100RemoteHost alloc] init] autorelease];
host.hostname = @"hostname";
host.username = @"user";
[[iTermShellHistoryController sharedInstance] eraseCommandHistoryForHost:host];
// Erase directory history for the remotehost we test with.
[[iTermShellHistoryController sharedInstance] eraseDirectoriesForHost:host];
// Create a window and save convenience pointers to its various bits.
_session = [[iTermController sharedInstance] launchBookmark:nil inTerminal:nil];
_windowController = (PseudoTerminal *)_session.delegate.realParentWindow;
_view = (iTermRootTerminalView *)_windowController.window.contentView;
// Make it big so all the tools fit.
[_windowController.window setFrame:_windowController.window.screen.visibleFrame display:YES];
// Show the toolbelt
if (!_view.shouldShowToolbelt) {
[_windowController toggleToolbeltVisibility:self];
}
// Show all the tools
for (NSString *toolName in [iTermToolbeltView allTools]) {
if (![iTermToolbeltView shouldShowTool:toolName]) {
[iTermToolbeltView toggleShouldShowTool:toolName];
}
}
// Define a capture output trigger.
NSDictionary *trigger = @{ kTriggerRegexKey: @"error:",
kTriggerActionKey: @"CaptureTrigger",
kTriggerParameterKey: @"sleep 99999" };
[_session setSessionSpecificProfileValues:@{ KEY_TRIGGERS: @[ trigger ] }];
}
- (void)tearDown {
iTermApplication *app = iTermApplication.sharedApplication;
app.fakeCurrentEvent = nil;
[_currentDir release];
[_insertedText release];
[[_windowController retain] autorelease];
[_session terminate];
[_windowController close];
[super tearDown];
}
#pragma mark - Utilities
- (void)sendData:(NSData *)data toTerminal:(VT100Terminal *)terminal {
[terminal.parser putStreamData:data.bytes length:data.length];
CVector vector;
CVectorCreate(&vector, 1);
[terminal.parser addParsedTokensToVector:&vector];
for (int i = 0; i < CVectorCount(&vector); i++) {
[terminal executeToken:CVectorGetObject(&vector, i)];
}
CVectorDestroy(&vector);
}
- (void)sendPrompt {
NSString *promptLine = [NSString stringWithFormat:
@"%c]1337;RemoteHost=user@hostname%c"
@"%c]1337;CurrentDir=%@%c"
@"%c]133;A%c"
@"> "
@"%c]133;B%c",
VT100CC_ESC, VT100CC_BEL, // RemoteHost
VT100CC_ESC, _currentDir, VT100CC_BEL, // CurrentDir
VT100CC_ESC, VT100CC_BEL, // FinalTerm A
VT100CC_ESC, VT100CC_BEL]; // FinalTerm B
[self sendData:[promptLine dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
}
- (void)sendPromptAndStartCommand:(NSString *)command toSession:(PTYSession *)session {
NSString *promptLine = [NSString stringWithFormat:
@"%c]1337;RemoteHost=user@hostname%c"
@"%c]1337;CurrentDir=%@%c"
@"%c]133;A%c"
@"> "
@"%c]133;B%c"
@"%@"
@"%c]133;C%c",
VT100CC_ESC, VT100CC_BEL, // RemoteHost
VT100CC_ESC, _currentDir, VT100CC_BEL, // CurrentDir
VT100CC_ESC, VT100CC_BEL, // FinalTerm A
VT100CC_ESC, VT100CC_BEL, // FinalTerm B
command,
VT100CC_ESC, VT100CC_BEL]; // FinalTerm C
[self sendData:[promptLine dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:session.terminal];
}
- (void)endCommand {
NSString *promptLine = [NSString stringWithFormat:@"%c]133;D;1%c",
VT100CC_ESC, VT100CC_BEL];
[self sendData:[promptLine dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
}
- (void)writeLongCommandOutput {
for (int i = 0; i < _session.screen.height * 2; i++) {
[self sendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
}
}
#pragma mark - General Testse
- (void)testToolbeltIsVisible {
// Ensure the toolbelt is visible.
XCTAssert(_view.shouldShowToolbelt);
XCTAssert(_view.toolbelt);
XCTAssert(_view.toolbelt.window);
}
#pragma mark Captured Output
- (void)testToolbeltHasCapturedOutputTool {
ToolCapturedOutputView *tool = (ToolCapturedOutputView *)[_view.toolbelt
toolWithName:kCapturedOutputToolName];
XCTAssert(tool);
}
- (void)testCapturedOutputUpdatesOnMatch {
ToolCapturedOutputView *tool = (ToolCapturedOutputView *)[_view.toolbelt
toolWithName:kCapturedOutputToolName];
XCTAssertEqual(tool.tableView.numberOfRows, 0);
// Gotta have a command mark for captured output to work
[self sendPromptAndStartCommand:@"make" toSession:_session];
[self sendData:[@"Hello\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
XCTAssertEqual(tool.tableView.numberOfRows, 0);
[self sendData:[@"error: blah\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
XCTAssertEqual(tool.tableView.numberOfRows, 1);
}
- (void)testCapturedOutputShowsLineOnClick {
ToolCapturedOutputView *tool = (ToolCapturedOutputView *)[_view.toolbelt
toolWithName:kCapturedOutputToolName];
XCTAssertEqual(tool.tableView.numberOfRows, 0);
// Gotta have a command mark for captured output to work
[self sendPromptAndStartCommand:@"make" toSession:_session];
NSRect rectForFirstCellOfCapturedLine = _session.textview.cursorFrame;
[self sendData:[@"error: blah\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
[self writeLongCommandOutput];
// Update scroll position for new text
[_session.textview refresh];
XCTAssert(!NSIntersectsRect(_session.textview.enclosingScrollView.documentVisibleRect,
rectForFirstCellOfCapturedLine));
[tool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
XCTAssert(NSIntersectsRect(_session.textview.enclosingScrollView.documentVisibleRect,
rectForFirstCellOfCapturedLine));
}
- (void)testCapturedOutputActivatesTriggerOnDoubleClick {
ToolCapturedOutputView *tool = (ToolCapturedOutputView *)[_view.toolbelt
toolWithName:kCapturedOutputToolName];
XCTAssertEqual(tool.tableView.numberOfRows, 0);
// Gotta have a command mark for captured output to work
[self sendPromptAndStartCommand:@"make" toSession:_session];
[self sendData:[@"error: blah\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
// Select the row
[tool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
XCTAssert(!_session.hasCoprocess);
// Fake a double click on it
[tool.tableView.delegate performSelector:tool.tableView.doubleAction withObject:nil];
XCTAssert(_session.hasCoprocess);
}
#pragma mark Command History
- (void)testCommandHistoryBoldsCommandsForCurrentSession {
PTYSession *otherSession = [[iTermController sharedInstance] launchBookmark:nil
inTerminal:_windowController];
// Set the hostname for both sessions
[self sendPromptAndStartCommand:@"command 1" toSession:_session];
[self sendPromptAndStartCommand:@"command 2" toSession:otherSession];
// Send the first command to tab 0
[self sendData:[@"Output 1" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
[self endCommand];
// Send the second command to tab 1
[self sendData:[@"Output 2" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
[self endCommand];
ToolCommandHistoryView *tool =
(ToolCommandHistoryView *)[_view.toolbelt toolWithName:kCommandHistoryToolName];
// Select tab 0 and get its two commands from the table view.
[_windowController.tabView selectTabViewItemAtIndex:0];
NSArray *values = @[ [tool.tableView.dataSource tableView:tool.tableView
objectValueForTableColumn:tool.tableView.tableColumns[0]
row:0],
[tool.tableView.dataSource tableView:tool.tableView
objectValueForTableColumn:tool.tableView.tableColumns[0]
row:1] ];
// TODO(georgen): Test that the first one should be bold.
XCTAssert([values[0] isKindOfClass:[NSAttributedString class]]);
XCTAssert([values[1] isKindOfClass:[NSAttributedString class]]);
// Select tab 1 and get its two commands from the table view.
[_windowController.tabView selectTabViewItemAtIndex:1];
values = @[ [tool.tableView.dataSource tableView:tool.tableView
objectValueForTableColumn:tool.tableView.tableColumns[0]
row:0],
[tool.tableView.dataSource tableView:tool.tableView
objectValueForTableColumn:tool.tableView.tableColumns[0]
row:1] ];
// TODO(georgen): Test that the second one should be bold.
XCTAssert([values[0] isKindOfClass:[NSAttributedString class]]);
XCTAssert([values[1] isKindOfClass:[NSAttributedString class]]);
}
- (void)testCommandHistoryScrollsToClickedCommand {
NSRect firstCommandRect = _session.textview.enclosingScrollView.documentVisibleRect;
[self sendPromptAndStartCommand:@"command 1" toSession:_session];
[self writeLongCommandOutput];
[self endCommand];
[self sendPromptAndStartCommand:@"command 2" toSession:_session];
[self writeLongCommandOutput];
[self endCommand];
ToolCommandHistoryView *tool =
(ToolCommandHistoryView *)[_view.toolbelt toolWithName:kCommandHistoryToolName];
XCTAssert(!NSIntersectsRect(_session.textview.enclosingScrollView.documentVisibleRect,
firstCommandRect));
[tool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
XCTAssert(NSIntersectsRect(_session.textview.enclosingScrollView.documentVisibleRect,
firstCommandRect));
}
- (void)testCommandHistoryUpdatesWhenNewCommandIsEntered {
ToolCommandHistoryView *tool =
(ToolCommandHistoryView *)[_view.toolbelt toolWithName:kCommandHistoryToolName];
// Send a prompt first so we know the hostname.
[self sendPromptAndStartCommand:@"command 1" toSession:_session];
[self writeLongCommandOutput];
[self endCommand];
int n = tool.tableView.numberOfRows;
[self sendPromptAndStartCommand:@"command 2" toSession:_session];
[self writeLongCommandOutput];
[self endCommand];
XCTAssertEqual(tool.tableView.numberOfRows, n + 1);
}
- (void)testCommandHistoryEntersCommandOnDoubleClick {
[self sendPromptAndStartCommand:@"command 1" toSession:_session];
[self endCommand];
[self sendPrompt];
ToolCommandHistoryView *tool =
(ToolCommandHistoryView *)[_view.toolbelt toolWithName:kCommandHistoryToolName];
[tool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:tool.tableView.numberOfRows - 1]
byExtendingSelection:NO];
tool.toolWrapper.delegate.delegate = self;
[tool.tableView.delegate performSelector:tool.tableView.doubleAction withObject:tool.tableView];
XCTAssertEqualObjects(_insertedText, @"command 1");
}
- (void)testCommandHistoryWritesCdOnOptionDoubleClick {
[self sendPromptAndStartCommand:@"command 1" toSession:_session];
[self endCommand];
[self sendPrompt];
ToolCommandHistoryView *tool =
(ToolCommandHistoryView *)[_view.toolbelt toolWithName:kCommandHistoryToolName];
[tool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:tool.tableView.numberOfRows - 1]
byExtendingSelection:NO];
tool.toolWrapper.delegate.delegate = self;
iTermApplication *app = iTermApplication.sharedApplication;
CGEventRef fakeEvent = CGEventCreateKeyboardEvent(NULL, 0, true);
CGEventSetFlags(fakeEvent, kCGEventFlagMaskAlternate);
app.fakeCurrentEvent = [NSEvent eventWithCGEvent:fakeEvent];
CFRelease(fakeEvent);
[tool.tableView.delegate performSelector:tool.tableView.doubleAction withObject:tool.tableView.target];
XCTAssertEqualObjects(_insertedText, [@"cd " stringByAppendingString:_currentDir]);
}
- (void)testCommandHistoryLinkedToCapturedOutput {
[self sendPromptAndStartCommand:@"command 1" toSession:_session];
[self sendData:[@"error: 1\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
[self endCommand];
[self sendPromptAndStartCommand:@"command 2" toSession:_session];
[self sendData:[@"error: 2\r\n" dataUsingEncoding:NSUTF8StringEncoding]
toTerminal:_session.terminal];
[self endCommand];
ToolCapturedOutputView *capturedOutputTool =
(ToolCapturedOutputView *)[_view.toolbelt toolWithName:kCapturedOutputToolName];
ToolCommandHistoryView *commandHistoryTool =
(ToolCommandHistoryView *)[_view.toolbelt toolWithName:kCommandHistoryToolName];
// Select first command
[commandHistoryTool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
byExtendingSelection:NO];
NSString *object;
object = [capturedOutputTool.tableView.dataSource tableView:capturedOutputTool.tableView
objectValueForTableColumn:capturedOutputTool.tableView.tableColumns[0]
row:0];
XCTAssert([object containsString:@"error: 1"]);
// Select second command
[commandHistoryTool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:1]
byExtendingSelection:NO];
object = [capturedOutputTool.tableView.dataSource tableView:capturedOutputTool.tableView
objectValueForTableColumn:capturedOutputTool.tableView.tableColumns[0]
row:0];
XCTAssert([object containsString:@"error: 2"]);
// Select nothing
[commandHistoryTool.tableView selectRowIndexes:[NSIndexSet indexSet]
byExtendingSelection:NO];
object = [capturedOutputTool.tableView.dataSource tableView:capturedOutputTool.tableView
objectValueForTableColumn:capturedOutputTool.tableView.tableColumns[0]
row:0];
XCTAssert([object containsString:@"error: 2"]);
}
#pragma mark Directories
- (void)testDirectoriesUpdatesOnCd {
[self sendPromptAndStartCommand:@"cd /tmp" toSession:_session];
self.currentDir = @"/tmp";
[self endCommand];
[self sendPrompt];
ToolDirectoriesView *tool =
(ToolDirectoriesView *)[_view.toolbelt toolWithName:kRecentDirectoriesToolName];
XCTAssertEqual(tool.tableView.numberOfRows, 2);
NSAttributedString *object = [tool.tableView.dataSource tableView:tool.tableView
objectValueForTableColumn:tool.tableView.tableColumns[0]
row:0];
XCTAssertEqualObjects([object string], @"/dir");
object = [tool.tableView.dataSource tableView:tool.tableView
objectValueForTableColumn:tool.tableView.tableColumns[0]
row:1];
XCTAssertEqualObjects([object string], @"/tmp");
}
- (void)testDirectoriesInsertsDirectoryNameOnDoubleClick {
[self sendPromptAndStartCommand:@"make" toSession:_session];
ToolDirectoriesView *tool =
(ToolDirectoriesView *)[_view.toolbelt toolWithName:kRecentDirectoriesToolName];
[tool.tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:0] byExtendingSelection:NO];
tool.toolWrapper.delegate.delegate = self;
[tool.tableView.delegate performSelector:tool.tableView.doubleAction withObject:tool.tableView];
XCTAssertEqualObjects(_insertedText, _currentDir);
}
#pragma mark Window
- (void)testToolbeltImpingesOnWindowWhenNearRightEdge {
// Hide toolbelt
[_windowController toggleToolbeltVisibility:nil];
XCTAssert(!_view.shouldShowToolbelt);
// Ensure the window fills the visible frame
[_windowController.window setFrame:_windowController.window.screen.visibleFrame display:YES];
NSRect originalWindowFrame = _windowController.window.frame;
NSRect originalTabViewFrame = _view.tabView.frame;
// Show toolbelt
[_windowController toggleToolbeltVisibility:nil];
// Window frame should not change
XCTAssertEqual(_windowController.window.frame.size.width, originalWindowFrame.size.width);
// TabView's frame should change
XCTAssertNotEqual(_view.tabView.frame.size.width, originalTabViewFrame.size.width);
}
- (void)testToolbeltGrowsWhenSpaceIsAvailableOnRight {
// Hide toolbelt
[_windowController toggleToolbeltVisibility:nil];
XCTAssert(!_view.shouldShowToolbelt);
// Ensure the window has space on right
NSRect newWindowFrame = _windowController.window.screen.visibleFrame;
newWindowFrame.size.width -= 300;
[_windowController.window setFrame:newWindowFrame display:YES];
NSRect originalWindowFrame = _windowController.window.frame;
NSRect originalTabViewFrame = _view.tabView.frame;
// Show toolbelt
[_windowController toggleToolbeltVisibility:nil];
// Window frame should change
XCTAssertNotEqual(_windowController.window.frame.size.width, originalWindowFrame.size.width);
// TabView's frame should not
XCTAssertEqual(_view.tabView.frame.size.width, originalTabViewFrame.size.width);
}
#pragma mark - iTermToolbeltViewDelegate
- (CGFloat)growToolbeltBy:(CGFloat)amount {
return amount;
}
- (void)toolbeltUpdateMouseCursor {
}
- (void)toolbeltInsertText:(NSString *)text {
[_insertedText appendString:text];
}
- (VT100RemoteHost *)toolbeltCurrentHost {
return nil;
}
- (pid_t)toolbeltCurrentShellProcessId {
return 0;
}
- (VT100ScreenMark *)toolbeltLastCommandMark {
return nil;
}
- (void)toolbeltDidSelectMark:(iTermMark *)mark {
}
- (void)toolbeltActivateTriggerForCapturedOutputInCurrentSession:(CapturedOutput *)capturedOutput {
}
- (BOOL)toolbeltCurrentSessionHasGuid:(NSString *)guid {
return NO;
}
- (NSArray<iTermCommandHistoryCommandUseMO *> *)toolbeltCommandUsesForCurrentSession {
return @[];
}
@end