forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPTYTextViewTest.m
More file actions
2636 lines (2309 loc) · 108 KB
/
PTYTextViewTest.m
File metadata and controls
2636 lines (2309 loc) · 108 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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "iTermColorMap.h"
#import "iTermSelection.h"
#import "iTermTextDrawingHelper.h"
#import "NSColor+iTerm.h"
#import "NSFont+iTerm.h"
#import "NSImage+iTerm.h"
#import "NSView+iTerm.h"
#import "PTYSession.h"
#import "PTYTextView.h"
#import "SessionView.h"
#import "VT100LineInfo.h"
#import "iTermApplication.h"
#import "iTermApplicationDelegate.h"
#import "iTermAdvancedSettingsModel.h"
#import "iTermFakeUserDefaults.h"
#import "iTermPreferences.h"
#import "iTermSelectorSwizzler.h"
#import <objc/runtime.h>
#import <XCTest/XCTest.h>
#define NUM_DIFF_BUCKETS 10
#define STRINGIFY(s) #s
#define STRINGIFY_MACRO(m) STRINGIFY(m)
typedef struct {
CGFloat variance;
CGFloat maxDiff;
int buckets[NUM_DIFF_BUCKETS];
} iTermDiffStats;
static NSString *const kDiffScriptPath = @"/tmp/diffs";
@interface PTYTextViewTest : XCTestCase
@end
@interface iTermFakeSessionForPTYTextViewTest : PTYSession
@end
@implementation iTermFakeSessionForPTYTextViewTest
- (BOOL)textViewWindowUsesTransparency {
return YES;
}
@end
@interface PTYTextViewTest ()<PTYTextViewDelegate, PTYTextViewDataSource>
@end
@interface PTYTextView (Internal)
- (void)paste:(id)sender;
- (void)pasteOptions:(id)sender;
- (void)pasteSelection:(id)sender;
- (void)pasteBase64Encoded:(id)sender;
@end
@implementation PTYTextViewTest {
PTYTextView *_textView;
iTermColorMap *_colorMap;
NSString *_pasteboardString;
NSMutableDictionary *_methodsCalled;
screen_char_t _buffer[5];
NSMutableString *_script;
}
- (void)setUp {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[@"" writeToFile:kDiffScriptPath atomically:NO encoding:NSUTF8StringEncoding error:nil];
});
_script = [NSMutableString string];
_colorMap = [[iTermColorMap alloc] init];
_textView = [[PTYTextView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100) colorMap:_colorMap];
[_textView.colorMap setColor:[NSColor redColor] forKey:kColorMapBackground];
[_textView.colorMap setColor:[NSColor redColor] forKey:kColorMapForeground];
_textView.delegate = self;
_textView.dataSource = self;
_methodsCalled = [[NSMutableDictionary alloc] init];
}
- (void)tearDown {
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:kDiffScriptPath];
[fileHandle seekToEndOfFile];
[fileHandle writeData:[_script dataUsingEncoding:NSUTF8StringEncoding]];
[fileHandle closeFile];
[_textView release];
[_colorMap release];
[_methodsCalled release];
}
- (void)setLineDirtyAtY:(int)y {
}
- (NSIndexSet *)dirtyIndexesOnLine:(int)line {
return nil;
}
- (BOOL)isRestartable {
return NO;
}
- (void)setRangeOfCharsAnimated:(NSRange)range onLine:(int)line {
}
- (NSIndexSet *)animatedLines {
return nil;
}
- (void)resetAnimatedLines {
}
- (void)resetDirty {
}
- (void)textViewSelectPreviousTab {
}
- (void)selectPaneRightInCurrentTerminal {
}
- (void)textViewSelectPreviousPane {
}
- (VT100GridRange)dirtyRangeForLine:(int)y {
return VT100GridRangeMake(0, 0);
}
- (PTYScroller *)textViewVerticalScroller {
return nil;
}
- (BOOL)textViewHasCoprocess {
return NO;
}
- (void)textViewWillNeedUpdateForBlink {
}
- (BOOL)textViewWindowUsesTransparency {
return NO;
}
- (BOOL)textViewShouldShowMarkIndicators {
return YES;
}
- (int)optionKey {
return 2;
}
- (void)textViewSwapPane {
}
- (BOOL)textViewIsMaximized {
return NO;
}
- (void)textViewMovePane {
}
- (NSStringEncoding)textViewEncoding {
return NSUTF8StringEncoding;
}
- (void)selectPaneLeftInCurrentTerminal {
}
- (NSDictionary *)textViewVariables {
return nil;
}
- (void)setCharDirtyAtCursorX:(int)x Y:(int)y {
}
- (SCPPath *)scpPathForFile:(NSString *)filename onLine:(int)line {
return nil;
}
- (void)launchProfileInCurrentTerminal:(NSDictionary *)profile withURL:(NSString *)url {
}
- (void)textViewSplitVertically:(BOOL)vertically withProfileGuid:(NSString *)guid {
}
- (BOOL)isPasting {
return NO;
}
- (void)launchCoprocessWithCommand:(NSString *)command {
}
- (BOOL)textViewDelegateHandlesAllKeystrokes {
return NO;
}
- (screen_char_t *)getLineAtScreenIndex:(int)theIndex {
return nil;
}
- (BOOL)setUseSavedGridIfAvailable:(BOOL)use {
return NO;
}
- (void)textViewInvalidateRestorableState {
}
- (BOOL)textViewIsZoomedIn {
return NO;
}
- (VT100GridCoordRange)textViewRangeOfOutputForCommandMark:(VT100ScreenMark *)mark {
return VT100GridCoordRangeMake(0, 0, 0, 0);
}
- (void)textViewToggleBroadcastingInput {
}
- (void)textViewDrawBackgroundImageInView:(NSView *)view viewRect:(NSRect)rect blendDefaultBackground:(BOOL)blendDefaultBackground {
}
- (BOOL)textViewAmbiguousWidthCharsAreDoubleWidth {
return NO;
}
- (void)insertText:(NSString *)string {
}
- (BOOL)hasActionableKeyMappingForEvent:(NSEvent *)event {
return NO;
}
- (void)sendEscapeSequence:(NSString *)text {
}
- (VT100Terminal *)terminal {
return nil;
}
- (void)textViewSelectNextPane {
}
- (void)textViewPostTabContentsChangedNotification {
}
- (NSString *)textViewCurrentWorkingDirectory {
return nil;
}
- (BOOL)textViewCanSelectOutputOfLastCommand {
return NO;
}
- (BOOL)textViewCanSelectCurrentCommand {
return NO;
}
- (void)textViewCloseWithConfirmation {
}
- (BOOL)textViewTabHasMaximizedPanel {
return NO;
}
- (BOOL)textViewSessionIsBroadcastingInput {
return NO;
}
- (VT100GridCoordRange)coordRangeOfNote:(PTYNoteViewController *)note {
return VT100GridCoordRangeMake(0, 0, 0, 0);
}
- (BOOL)showingAlternateScreen {
return NO;
}
- (BOOL)textViewShouldPlaceCursorAt:(VT100GridCoord)coord verticalOk:(BOOL *)verticalOk {
return YES;
}
- (NSColor *)textViewBadgeColor {
return nil;
}
- (BOOL)textViewReportMouseEvent:(NSEventType)eventType
modifiers:(NSUInteger)modifiers
button:(MouseButtonNumber)button
coordinate:(VT100GridCoord)coord
deltaY:(CGFloat)deltaY {
return NO;
}
- (BOOL)textViewShouldDrawFilledInCursor {
return YES;
}
- (void)pasteString:(NSString *)aString {
}
- (int)scrollbackOverflow {
return 0;
}
- (void)textViewEditSession {
}
- (screen_char_t *)getLineAtIndex:(int)theIndex withBuffer:(screen_char_t*)buffer {
return nil;
}
- (void)clearBuffer {
}
- (void)textViewDidBecomeFirstResponder {
}
- (NSString *)debugString {
return nil;
}
- (BOOL)shouldSendContentsChangedNotification {
return NO;
}
- (void)sendHexCode:(NSString *)codes {
}
- (BOOL)textViewIsActiveSession {
return YES;
}
- (NSArray *)charactersWithNotesOnLine:(int)line {
return nil;
}
- (void)textViewFontDidChange {
}
- (VT100RemoteHost *)remoteHostOnLine:(int)line {
return nil;
}
- (int)rightOptionKey {
return 1;
}
- (NSArray *)notesInRange:(VT100GridCoordRange)range {
return nil;
}
- (void)saveFindContextAbsPos {
}
- (void)textViewSelectPreviousWindow {
}
- (void)resetAllDirty {
}
- (void)textViewCreateTabWithProfileGuid:(NSString *)guid {
}
- (int)lineNumberOfMarkAfterLine:(int)line {
return line + 1;
}
- (int)lineNumberOfMarkBeforeLine:(int)line {
return line - 1;
}
- (VT100ScreenMark *)markOnLine:(int)line {
return nil;
}
- (PTYScrollView *)scrollview {
return nil;
}
- (void)addNote:(PTYNoteViewController *)note inRange:(VT100GridCoordRange)range {
}
- (void)startDownloadOverSCP:(SCPPath *)path {
}
- (void)selectPaneAboveInCurrentTerminal {
}
- (void)saveToDvr {
}
- (void)removeInaccessibleNotes {
}
- (VT100GridAbsCoordRange)textViewRangeOfLastCommandOutput {
return VT100GridAbsCoordRangeMake(0, 0, 0, 0);
}
- (VT100GridAbsCoordRange)textViewRangeOfCurrentCommand {
return VT100GridAbsCoordRangeMake(0, 0, 0, 0);
}
- (void)textViewCreateWindowWithProfileGuid:(NSString *)guid {
}
- (BOOL)textViewSuppressingAllOutput {
return NO;
}
- (void)textViewRestartWithConfirmation {
}
- (void)setFindString:(NSString *)aString forwardDirection:(BOOL)direction mode:(iTermFindMode)mode startingAtX:(int)x startingAtY:(int)y withOffset:(int)offsetof inContext:(FindContext *)context multipleResults:(BOOL)multipleResults {
}
- (PTYTask *)shell {
return nil;
}
- (void)menuForEvent:(NSEvent *)theEvent menu:(NSMenu *)theMenu {
}
- (void)resetScrollbackOverflow {
}
- (void)selectPaneBelowInCurrentTerminal {
}
- (void)writeTask:(NSString *)string {
}
- (void)writeStringWithLatin1Encoding:(NSString *)string {
}
- (void)textViewSelectNextWindow {
}
- (void)keyDown:(NSEvent *)event {
}
- (long long)totalScrollbackOverflow {
return 0;
}
- (int)cursorX {
return 1;
}
- (int)cursorY {
return 1;
}
- (FindContext *)findContext {
return nil;
}
- (long long)absoluteLineNumberOfCursor {
return 0;
}
- (BOOL)textViewHasBackgroundImage {
return NO;
}
- (void)textViewSelectNextTab {
}
- (iTermUnicodeNormalization)textViewUnicodeNormalizationForm {
return iTermUnicodeNormalizationNone;
}
- (BOOL)xtermMouseReporting {
return NO;
}
- (BOOL)xtermMouseReportingAllowMouseWheel {
return YES;
}
- (void)textViewBeginDrag {
}
- (NSMenu *)menuForEvent:event {
return nil;
}
- (NSString *)workingDirectoryOnLine:(int)line {
return nil;
}
- (void)queueKeyDown:(NSEvent *)event {
}
- (void)uploadFiles:(NSArray *)localFilenames toPath:(SCPPath *)destinationPath {
}
- (void)sendText:(NSString *)text {
}
- (BOOL)alertOnNextMark {
return NO;
}
- (NSDate *)timestampForLine:(int)y {
return nil;
}
- (int)numberOfScrollbackLines {
return 0;
}
- (NSColor *)textViewCursorGuideColor {
return nil;
}
- (void)textViewToggleAnnotations {
}
- (BOOL)textViewShouldAcceptKeyDownEvent:(NSEvent *)event {
return YES;
}
- (void)textViewThinksUserIsTryingToSendArrowKeysWithScrollWheel:(BOOL)trying {
}
- (void)textViewResizeFrameIfNeeded {
}
- (void)textViewDidRefresh {
}
- (void)textViewBackgroundColorDidChange {
}
- (NSInteger)textViewUnicodeVersion {
return 9;
}
- (NSURL *)textViewCurrentLocation {
return nil;
}
- (void)textViewBurySession {
}
- (BOOL)continueFindAllResults:(NSMutableArray *)results inContext:(FindContext *)context {
return NO;
}
- (BOOL)isAllDirty {
return NO;
}
- (BOOL)isDirtyAtX:(int)x Y:(int)y {
return NO;
}
- (void)invokeMenuItemWithSelector:(SEL)selector {
[self invokeMenuItemWithSelector:selector tag:0];
}
- (void)invokeMenuItemWithSelector:(SEL)selector tag:(NSInteger)tag {
NSMenuItem *fakeMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Fake Menu Item"
action:selector
keyEquivalent:@""] autorelease];
[fakeMenuItem setTag:tag];
XCTAssert([_textView validateMenuItem:fakeMenuItem]);
[_textView performSelector:selector withObject:fakeMenuItem];
}
- (void)registerCall:(SEL)selector {
[self registerCall:selector argument:nil];
}
- (void)registerCall:(SEL)selector argument:(NSObject *)argument {
NSString *name = NSStringFromSelector(selector);
if (argument) {
name = [name stringByAppendingString:[argument description]];
}
NSNumber *number = _methodsCalled[name];
if (!number) {
number = @0;
}
_methodsCalled[name] = @(number.intValue + 1);
}
- (void)testPaste {
[[NSPasteboard generalPasteboard] clearContents];
[[NSPasteboard generalPasteboard] setString:@"test" forType:NSPasteboardTypeString];
[self invokeMenuItemWithSelector:@selector(paste:)];
XCTAssert([_methodsCalled[@"paste:"] intValue] == 1);
}
- (void)testPasteOptions {
[self invokeMenuItemWithSelector:@selector(pasteOptions:)];
XCTAssert([_methodsCalled[@"pasteOptions:"] intValue] == 1);
}
- (void)testPasteSelection {
[_textView selectAll:nil];
[self invokeMenuItemWithSelector:@selector(pasteSelection:) tag:1];
XCTAssert([_methodsCalled[@"textViewPasteFromSessionWithMostRecentSelection:1"] intValue] == 1);
}
- (PTYSession *)sessionWithProfileOverrides:(NSDictionary *)profileOverrides
size:(VT100GridSize)size {
PTYSession *session = [[[PTYSession alloc] initSynthetic:NO] autorelease];
NSString* plistFile = [[NSBundle bundleForClass:[self class]]
pathForResource:@"DefaultBookmark"
ofType:@"plist"];
NSMutableDictionary* profile = [NSMutableDictionary dictionaryWithContentsOfFile:plistFile];
for (NSString *key in profileOverrides) {
profile[key] = profileOverrides[key];
}
[session setProfile:profile];
XCTAssert([session setScreenSize:NSMakeRect(0, 0, 200, 200) parent:nil]);
[session setPreferencesFromAddressBookEntry:profile];
[session setSize:size];
NSRect theFrame = NSMakeRect(0,
0,
size.width * session.textview.charWidth + [iTermAdvancedSettingsModel terminalMargin] * 2,
size.height * session.textview.lineHeight + [iTermAdvancedSettingsModel terminalVMargin] * 2);
session.view.frame = theFrame;
[session loadInitialColorTable];
[session setBookmarkName:profile[KEY_NAME]];
[session setName:profile[KEY_NAME]];
[session setDefaultName:profile[KEY_NAME]];
return session;
}
- (NSImage *)imageForInput:(NSString *)input
hook:(void (^)(PTYTextView *))hook
profileOverrides:(NSDictionary *)profileOverrides
size:(VT100GridSize)size {
PTYSession *session = [self sessionWithProfileOverrides:profileOverrides size:size];
[session synchronousReadTask:input];
if (hook) {
hook(session.textview);
}
return [session.view snapshot];
}
- (NSString *)pathForGoldenWithName:(NSString *)name {
return [self pathForTestResourceNamed:[self shortNameForGolden:name]];
}
- (NSString *)shortNameForGolden:(NSString *)name {
NSString *domain = @"";
if ([[[iTermApplication sharedApplication] delegate] isRunningOnTravis]) {
// Travis runs in a VM that renders text a little differently than a retina device running
// the app in low-res mode.
domain = @"travis-";
} else if ([[NSScreen mainScreen] backingScaleFactor] == 1.0) {
domain = @"nonretina-";
}
return [NSString stringWithFormat:@"PTYTextViewTest-golden-%@%@.png", domain, name];
}
- (NSString *)pathForTestResourceNamed:(NSString *)name {
NSString *resourcePath = [[NSBundle bundleForClass:[self class]] resourcePath];
return [resourcePath stringByAppendingPathComponent:name];
}
// Minor differences in anti-aliasing on different machines (even running the same version of the
// OS) cause false failures with golden images, so we'll ignore tiny differences in brightness.
- (BOOL)image:(NSData *)image1 approximatelyEqualToImage:(NSData *)image2 size:(NSSize)size stats:(iTermDiffStats *)stats diffPath:(NSString *)diffPath {
if (image1.length != image2.length) {
return NO;
}
NSMutableData *diffData = [NSMutableData data];
unsigned char *bytes1 = (unsigned char *)image1.bytes;
unsigned char *bytes2 = (unsigned char *)image2.bytes;
const CGFloat threshold = 0.1;
CGFloat sumOfSquares = 0;
CGFloat maxDiff = 0;
CGFloat sum = 0;
for (int j = 0; j < NUM_DIFF_BUCKETS; j++) {
stats->buckets[j] = 0;
}
for (int i = 0; i < image1.length; i+= 4) {
CGFloat brightness1 = PerceivedBrightness(bytes1[i + 0] / 255.0, bytes1[i + 1] / 255.0, bytes1[i + 2] / 255.0);
CGFloat brightness2 = PerceivedBrightness(bytes2[i + 0] / 255.0, bytes2[i + 1] / 255.0, bytes2[i + 2] / 255.0);
CGFloat diff = fabs(brightness1 - brightness2);
unsigned char diffbytes[3] = { 0, 0, 0 };
if (diff > 0) {
diffbytes[0] = diff * 255;
diffbytes[1] = (1.0 - diff) * 255;
diffbytes[2] = 0;
} else {
diffbytes[0] = (brightness1 + brightness2) * 128;
diffbytes[1] = (brightness1 + brightness2) * 128;
diffbytes[2] = (brightness1 + brightness2) * 128;
}
[diffData appendBytes:diffbytes length:sizeof(diffbytes)];
sumOfSquares += diff*diff;
sum += diff;
maxDiff = MAX(maxDiff, diff);
if (diff > 0) {
int bucket = MIN((NUM_DIFF_BUCKETS - 1), MAX(0, diff * NUM_DIFF_BUCKETS));
stats->buckets[bucket]++;
}
}
CGFloat N = image1.length / 4;
stats->variance = sumOfSquares/N - (sum/N)*(sum/N);
stats->maxDiff = maxDiff;
NSImage *diffImage = [NSImage imageWithRawData:diffData
size:size
bitsPerSample:8
samplesPerPixel:3
hasAlpha:NO
colorSpaceName:NSCalibratedRGBColorSpace];
NSData *encodedDiffImage = [diffImage dataForFileOfType:NSPNGFileType];
[encodedDiffImage writeToFile:diffPath atomically:NO];
return maxDiff < threshold;
}
- (NSString *)decilesInStats:(iTermDiffStats)stats {
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < NUM_DIFF_BUCKETS; i++) {
[array addObject:[@(stats.buckets[i]) description]];
}
return [array componentsJoinedByString:@", "];
}
- (void)doGoldenTestForInput:(NSString *)input
name:(NSString *)name
hook:(void (^)(PTYTextView *))hook
profileOverrides:(NSDictionary *)profileOverrides
createGolden:(BOOL)createGolden
size:(VT100GridSize)size {
NSImage *actual = [self imageForInput:input
hook:^(PTYTextView *textView) {
textView.thinStrokes = iTermThinStrokesSettingNever;
if (hook) {
hook(textView);
}
}
profileOverrides:profileOverrides
size:size];
NSString *goldenName = [self pathForGoldenWithName:name];
if (createGolden) {
NSData *pngData = [actual dataForFileOfType:NSPNGFileType];
[pngData writeToFile:goldenName atomically:NO];
NSLog(@"Wrote to golden file at %@", goldenName);
} else {
NSImage *golden = [[NSImage alloc] initWithContentsOfFile:goldenName];
if (!golden) {
golden = [NSImage imageNamed:[self shortNameForGolden:name]];
}
XCTAssertNotNil(golden, @"Failed to load golden image with name %@, short name %@", goldenName, [self shortNameForGolden:name]);
NSData *goldenData = [golden rawPixelsInRGBColorSpace];
XCTAssertNotNil(goldenData, @"Failed to extract pixels from golden image");
NSData *actualData = [actual rawPixelsInRGBColorSpace];
XCTAssertEqual(goldenData.length, actualData.length, @"Different number of pixels between %@ and %@", golden, actual);
iTermDiffStats stats = { 0 };
NSString *diffPath = [NSString stringWithFormat:@"/tmp/diff-%@.png", name];
BOOL ok = [self image:goldenData approximatelyEqualToImage:actualData size:actual.size stats:&stats diffPath:diffPath];
if (ok) {
NSLog(@"Tests “%@” ok with variance: %f. Max diff: %f", name, stats.variance, stats.maxDiff);
} else {
char *projectDir = STRINGIFY_MACRO(PROJECT_DIR);
NSString *sourceFolder = [NSString stringWithUTF8String:projectDir];
if (sourceFolder && ![[[iTermApplication sharedApplication] delegate] isRunningOnTravis]) {
NSData *pngData = [actual dataForFileOfType:NSPNGFileType];
NSString *sourceName = [[[[sourceFolder stringByAppendingPathComponent:@"tests/Goldens"] stringByAppendingPathComponent:@"PTYTextViewTest-golden-"] stringByAppendingString:name] stringByAppendingString:@".png"];
[pngData writeToFile:sourceName atomically:NO];
NSLog(@"Wrote to golden file at %@", sourceName);
}
NSString *failPath = [NSString stringWithFormat:@"/tmp/failed-%@.png", name];
[[actual dataForFileOfType:NSPNGFileType] writeToFile:failPath atomically:NO];
NSLog(@"nTest “%@” about to fail.\nActual output in %@.\nExpected output in %@",
name, failPath, goldenName);
[_script appendFormat:@"echo ----- %@ -----\n", name];
[_script appendFormat:@"echo Actual:\n"];
[_script appendFormat:@"imgcat %@\n", failPath];
[_script appendFormat:@"echo Expected:\n"];
[_script appendFormat:@"imgcat %@\n", goldenName];
[_script appendFormat:@"echo Diffs:\n"];
[_script appendFormat:@"imgcat %@\n", diffPath];
}
XCTAssert(ok, @"variance=%f maxdiff=%f deciles=%@", stats.variance, stats.maxDiff, [self decilesInStats:stats]);
}
}
- (NSString *)sgrSequence:(int)n {
return [NSString stringWithFormat:@"%c[%dm", VT100CC_ESC, n];
}
- (NSString *)sgrSequenceWithSubparams:(NSArray *)values {
return [NSString stringWithFormat:@"%c[%@m",
VT100CC_ESC, [values componentsJoinedByString:@":"]];
}
#pragma mark - Drawing Tests
// Background color should be selected but grayed because window is not focused.
- (void)testCharacterSelection {
[self doGoldenTestForInput:@"abcd"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(1, 0, 3, 0),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeCharacter];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(5, 2)];
}
// A 2x2 box should be selected. The selection color is grayed because the window is unfocused.
- (void)testBoxSelection {
[self doGoldenTestForInput:@"abcd\r\nefgh\r\nijkl\r\nmnop"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(1, 1, 3, 2),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeBox];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(5, 5)];
}
// ab, fg, and jk should be selected. The selection color is grayed because the window is
// unfocused.
- (void)testMultipleDiscontinuousSelection {
[self doGoldenTestForInput:@"abcd\r\nefgh\r\nijkl\r\nmnop"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(1, 1, 3, 2),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeBox];
[textView.selection addSubSelection:subSelection];
range = VT100GridWindowedRangeMake(VT100GridCoordRangeMake(0, 0, 2, 0),
0, 0);
subSelection = [iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeCharacter];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(5, 5)];
}
// The middle two letters should be selected. The selection color is grayed because the window is
// unfocused.
- (void)testWindowedCharacterSelection {
[self doGoldenTestForInput:@"abcd\r\nefgh\r\nijkl\r\nmnop"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(1, 0, 3, 3),
1, 2);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeBox];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(5, 5)];
}
// A cell cells after the a are selected. The selection color is grayed because the window is
// unfocused.
- (void)testSelectedTabOrphan {
[self doGoldenTestForInput:@"a\t\x08q"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(1, 0, 3, 0),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeCharacter];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(9, 2)];
}
// The area between a and b is selected, and so is b. The selection color is grayed because the window is
// unfocused.
- (void)testSelectedTab {
[self doGoldenTestForInput:@"a\tb"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(7, 0, 9, 0),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeCharacter];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(9, 2)];
}
// Although one of the tab fillers after a is selected, only a should appear selected.
// The selection color is grayed because the window is unfocused.
- (void)testSelectedTabFillerWithoutTab {
[self doGoldenTestForInput:@"a\tb"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(0, 0, 3, 0),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeCharacter];
[textView.selection addSubSelection:subSelection];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(9, 2)];
}
// By default, the text view is not the first responder. Ensure the color is correct when it is FR.
- (void)testCharacterSelectionTextviewIsFirstResponder {
[self doGoldenTestForInput:@"abcd"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
VT100GridWindowedRange range =
VT100GridWindowedRangeMake(VT100GridCoordRangeMake(1, 0, 3, 0),
0, 0);
iTermSubSelection *subSelection =
[iTermSubSelection subSelectionWithRange:range
mode:kiTermSelectionModeCharacter];
[textView.selection addSubSelection:subSelection];
textView.drawingHook = ^(iTermTextDrawingHelper *helper) {
helper.isFrontTextView = YES;
};
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(5, 2)];
}
// Draws a cursor guide on the line with b.
- (void)testCursorGuide {
[self doGoldenTestForInput:@"a\r\nb\r\nc\r\nd\x1b[2A"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
textView.drawingHook = ^(iTermTextDrawingHelper *helper) {
helper.shouldDrawFilledInCursor = YES;
helper.highlightCursorLine = YES;
};
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(5, 5)];
}
// Draws a badge which blends with nondefault background colors.
- (void)testBadge {
[self doGoldenTestForInput:@"\n\n\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\x1b[42mabc"
name:NSStringFromSelector(_cmd)
hook:^(PTYTextView *textView) {
[textView setBadgeLabel:@"Badge"];
}
profileOverrides:nil
createGolden:NO
size:VT100GridSizeMake(80, 25)];
}
// Test various combinations of the basic 256 colors
- (void)test256Colors {
// Tests a few combos of fg and bg colors in the set of 256 indexed colors.
NSMutableString *input = [NSMutableString string];
// 16 + r*36 + g*6 + b
// RGB values are in 0 to 5 incl.
NSNumber *black = @(16 + 0*36 + 0*6 + 0);
NSNumber *gray = @(16 + 3*36 + 3*6 + 3);
NSNumber *red = @(16 + 5*36 + 0*6 + 0);