forked from gui-cs/Terminal.Gui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableViewTests.cs
More file actions
1342 lines (1027 loc) · 36.1 KB
/
TableViewTests.cs
File metadata and controls
1342 lines (1027 loc) · 36.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
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Terminal.Gui;
using Xunit;
using System.Globalization;
using Xunit.Abstractions;
using System.Reflection;
namespace Terminal.Gui.Views {
public class TableViewTests {
readonly ITestOutputHelper output;
public TableViewTests (ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void EnsureValidScrollOffsets_WithNoCells ()
{
var tableView = new TableView ();
Assert.Equal (0, tableView.RowOffset);
Assert.Equal (0, tableView.ColumnOffset);
// Set empty table
tableView.Table = new DataTable ();
// Since table has no rows or columns scroll offset should default to 0
tableView.EnsureValidScrollOffsets ();
Assert.Equal (0, tableView.RowOffset);
Assert.Equal (0, tableView.ColumnOffset);
}
[Fact]
public void EnsureValidScrollOffsets_LoadSmallerTable ()
{
var tableView = new TableView ();
tableView.Bounds = new Rect (0, 0, 25, 10);
Assert.Equal (0, tableView.RowOffset);
Assert.Equal (0, tableView.ColumnOffset);
// Set big table
tableView.Table = BuildTable (25, 50);
// Scroll down and along
tableView.RowOffset = 20;
tableView.ColumnOffset = 10;
tableView.EnsureValidScrollOffsets ();
// The scroll should be valid at the moment
Assert.Equal (20, tableView.RowOffset);
Assert.Equal (10, tableView.ColumnOffset);
// Set small table
tableView.Table = BuildTable (2, 2);
// Setting a small table should automatically trigger fixing the scroll offsets to ensure valid cells
Assert.Equal (0, tableView.RowOffset);
Assert.Equal (0, tableView.ColumnOffset);
// Trying to set invalid indexes should not be possible
tableView.RowOffset = 20;
tableView.ColumnOffset = 10;
Assert.Equal (1, tableView.RowOffset);
Assert.Equal (1, tableView.ColumnOffset);
}
[Fact]
[AutoInitShutdown]
public void Redraw_EmptyTable ()
{
var tableView = new TableView ();
tableView.ColorScheme = new ColorScheme();
tableView.Bounds = new Rect (0, 0, 25, 10);
// Set a table with 1 column
tableView.Table = BuildTable (1, 50);
tableView.Redraw(tableView.Bounds);
tableView.Table.Columns.Remove(tableView.Table.Columns[0]);
tableView.Redraw(tableView.Bounds);
}
[Fact]
public void SelectedCellChanged_NotFiredForSameValue ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50)
};
bool called = false;
tableView.SelectedCellChanged += (e) => { called = true; };
Assert.Equal (0, tableView.SelectedColumn);
Assert.False (called);
// Changing value to same as it already was should not raise an event
tableView.SelectedColumn = 0;
Assert.False (called);
tableView.SelectedColumn = 10;
Assert.True (called);
}
[Fact]
public void SelectedCellChanged_SelectedColumnIndexesCorrect ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50)
};
bool called = false;
tableView.SelectedCellChanged += (e) => {
called = true;
Assert.Equal (0, e.OldCol);
Assert.Equal (10, e.NewCol);
};
tableView.SelectedColumn = 10;
Assert.True (called);
}
[Fact]
public void SelectedCellChanged_SelectedRowIndexesCorrect ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50)
};
bool called = false;
tableView.SelectedCellChanged += (e) => {
called = true;
Assert.Equal (0, e.OldRow);
Assert.Equal (10, e.NewRow);
};
tableView.SelectedRow = 10;
Assert.True (called);
}
[Fact]
public void Test_SumColumnWidth_UnicodeLength ()
{
Assert.Equal (11, "hello there".Sum (c => Rune.ColumnWidth (c)));
// Creates a string with the peculiar (french?) r symbol
String surrogate = "Les Mise" + Char.ConvertFromUtf32 (Int32.Parse ("0301", NumberStyles.HexNumber)) + "rables";
// The unicode width of this string is shorter than the string length!
Assert.Equal (14, surrogate.Sum (c => Rune.ColumnWidth (c)));
Assert.Equal (15, surrogate.Length);
}
[Fact]
public void IsSelected_MultiSelectionOn_Vertical ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50),
MultiSelect = true
};
// 3 cell vertical selection
tableView.SetSelection (1, 1, false);
tableView.SetSelection (1, 3, true);
Assert.False (tableView.IsSelected (0, 0));
Assert.False (tableView.IsSelected (1, 0));
Assert.False (tableView.IsSelected (2, 0));
Assert.False (tableView.IsSelected (0, 1));
Assert.True (tableView.IsSelected (1, 1));
Assert.False (tableView.IsSelected (2, 1));
Assert.False (tableView.IsSelected (0, 2));
Assert.True (tableView.IsSelected (1, 2));
Assert.False (tableView.IsSelected (2, 2));
Assert.False (tableView.IsSelected (0, 3));
Assert.True (tableView.IsSelected (1, 3));
Assert.False (tableView.IsSelected (2, 3));
Assert.False (tableView.IsSelected (0, 4));
Assert.False (tableView.IsSelected (1, 4));
Assert.False (tableView.IsSelected (2, 4));
}
[Fact]
public void IsSelected_MultiSelectionOn_Horizontal ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50),
MultiSelect = true
};
// 2 cell horizontal selection
tableView.SetSelection (1, 0, false);
tableView.SetSelection (2, 0, true);
Assert.False (tableView.IsSelected (0, 0));
Assert.True (tableView.IsSelected (1, 0));
Assert.True (tableView.IsSelected (2, 0));
Assert.False (tableView.IsSelected (3, 0));
Assert.False (tableView.IsSelected (0, 1));
Assert.False (tableView.IsSelected (1, 1));
Assert.False (tableView.IsSelected (2, 1));
Assert.False (tableView.IsSelected (3, 1));
}
[Fact]
public void IsSelected_MultiSelectionOn_BoxSelection ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50),
MultiSelect = true
};
// 4 cell horizontal in box 2x2
tableView.SetSelection (0, 0, false);
tableView.SetSelection (1, 1, true);
Assert.True (tableView.IsSelected (0, 0));
Assert.True (tableView.IsSelected (1, 0));
Assert.False (tableView.IsSelected (2, 0));
Assert.True (tableView.IsSelected (0, 1));
Assert.True (tableView.IsSelected (1, 1));
Assert.False (tableView.IsSelected (2, 1));
Assert.False (tableView.IsSelected (0, 2));
Assert.False (tableView.IsSelected (1, 2));
Assert.False (tableView.IsSelected (2, 2));
}
[AutoInitShutdown]
[Fact]
public void PageDown_ExcludesHeaders ()
{
var tableView = new TableView () {
Table = BuildTable (25, 50),
MultiSelect = true,
Bounds = new Rect (0, 0, 10, 5)
};
// Header should take up 2 lines
tableView.Style.ShowHorizontalHeaderOverline = false;
tableView.Style.ShowHorizontalHeaderUnderline = true;
tableView.Style.AlwaysShowHeaders = false;
// ensure that TableView has the input focus
Application.Top.Add (tableView);
Application.Top.FocusFirst ();
Assert.True (tableView.HasFocus);
Assert.Equal (0, tableView.RowOffset);
tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
// window height is 5 rows 2 are header so page down should give 3 new rows
Assert.Equal (3, tableView.SelectedRow);
Assert.Equal (1, tableView.RowOffset);
// header is no longer visible so page down should give 5 new rows
tableView.ProcessKey (new KeyEvent (Key.PageDown, new KeyModifiers ()));
Assert.Equal (8, tableView.SelectedRow);
Assert.Equal (4, tableView.RowOffset);
}
[Fact]
public void DeleteRow_SelectAll_AdjustsSelectionToPreventOverrun ()
{
// create a 4 by 4 table
var tableView = new TableView () {
Table = BuildTable (4, 4),
MultiSelect = true,
Bounds = new Rect (0, 0, 10, 5)
};
tableView.SelectAll ();
Assert.Equal (16, tableView.GetAllSelectedCells ().Count ());
// delete one of the columns
tableView.Table.Columns.RemoveAt (2);
// table should now be 3x4
Assert.Equal (12, tableView.GetAllSelectedCells ().Count ());
// remove a row
tableView.Table.Rows.RemoveAt (1);
// table should now be 3x3
Assert.Equal (9, tableView.GetAllSelectedCells ().Count ());
}
[Fact]
public void DeleteRow_SelectLastRow_AdjustsSelectionToPreventOverrun ()
{
// create a 4 by 4 table
var tableView = new TableView () {
Table = BuildTable (4, 4),
MultiSelect = true,
Bounds = new Rect (0, 0, 10, 5)
};
// select the last row
tableView.MultiSelectedRegions.Clear ();
tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (0, 3), new Rect (0, 3, 4, 1)));
Assert.Equal (4, tableView.GetAllSelectedCells ().Count ());
// remove a row
tableView.Table.Rows.RemoveAt (0);
tableView.EnsureValidSelection ();
// since the selection no longer exists it should be removed
Assert.Empty (tableView.MultiSelectedRegions);
}
[Theory]
[InlineData (true)]
[InlineData (false)]
public void GetAllSelectedCells_SingleCellSelected_ReturnsOne (bool multiSelect)
{
var tableView = new TableView () {
Table = BuildTable (3, 3),
MultiSelect = multiSelect,
Bounds = new Rect (0, 0, 10, 5)
};
tableView.SetSelection (1, 1, false);
Assert.Single (tableView.GetAllSelectedCells ());
Assert.Equal (new Point (1, 1), tableView.GetAllSelectedCells ().Single ());
}
[Fact]
public void GetAllSelectedCells_SquareSelection_ReturnsFour ()
{
var tableView = new TableView () {
Table = BuildTable (3, 3),
MultiSelect = true,
Bounds = new Rect (0, 0, 10, 5)
};
// move cursor to 1,1
tableView.SetSelection (1, 1, false);
// spread selection across to 2,2 (e.g. shift+right then shift+down)
tableView.SetSelection (2, 2, true);
var selected = tableView.GetAllSelectedCells ().ToArray ();
Assert.Equal (4, selected.Length);
Assert.Equal (new Point (1, 1), selected [0]);
Assert.Equal (new Point (2, 1), selected [1]);
Assert.Equal (new Point (1, 2), selected [2]);
Assert.Equal (new Point (2, 2), selected [3]);
}
[Fact]
public void GetAllSelectedCells_SquareSelection_FullRowSelect ()
{
var tableView = new TableView () {
Table = BuildTable (3, 3),
MultiSelect = true,
FullRowSelect = true,
Bounds = new Rect (0, 0, 10, 5)
};
// move cursor to 1,1
tableView.SetSelection (1, 1, false);
// spread selection across to 2,2 (e.g. shift+right then shift+down)
tableView.SetSelection (2, 2, true);
var selected = tableView.GetAllSelectedCells ().ToArray ();
Assert.Equal (6, selected.Length);
Assert.Equal (new Point (0, 1), selected [0]);
Assert.Equal (new Point (1, 1), selected [1]);
Assert.Equal (new Point (2, 1), selected [2]);
Assert.Equal (new Point (0, 2), selected [3]);
Assert.Equal (new Point (1, 2), selected [4]);
Assert.Equal (new Point (2, 2), selected [5]);
}
[Fact]
public void GetAllSelectedCells_TwoIsolatedSelections_ReturnsSix ()
{
var tableView = new TableView () {
Table = BuildTable (20, 20),
MultiSelect = true,
Bounds = new Rect (0, 0, 10, 5)
};
/*
Sets up disconnected selections like:
00000000000
01100000000
01100000000
00000001100
00000000000
*/
tableView.MultiSelectedRegions.Clear ();
tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (1, 1), new Rect (1, 1, 2, 2)));
tableView.MultiSelectedRegions.Push (new TableView.TableSelection (new Point (7, 3), new Rect (7, 3, 2, 1)));
tableView.SelectedColumn = 8;
tableView.SelectedRow = 3;
var selected = tableView.GetAllSelectedCells ().ToArray ();
Assert.Equal (6, selected.Length);
Assert.Equal (new Point (1, 1), selected [0]);
Assert.Equal (new Point (2, 1), selected [1]);
Assert.Equal (new Point (1, 2), selected [2]);
Assert.Equal (new Point (2, 2), selected [3]);
Assert.Equal (new Point (7, 3), selected [4]);
Assert.Equal (new Point (8, 3), selected [5]);
}
[Fact]
public void TableView_ExpandLastColumn_True ()
{
var tv = SetUpMiniTable ();
// the thing we are testing
tv.Style.ExpandLastColumn = true;
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬──────┐
│A│B │
├─┼──────┤
│1│2 │
";
TestHelpers.AssertDriverContentsAre (expected, output);
// Shutdown must be called to safely clean up Application if Init has been called
Application.Shutdown ();
}
[Fact]
public void TableView_ExpandLastColumn_False ()
{
var tv = SetUpMiniTable ();
// the thing we are testing
tv.Style.ExpandLastColumn = false;
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬─┬────┐
│A│B│ │
├─┼─┼────┤
│1│2│ │
";
TestHelpers.AssertDriverContentsAre (expected, output);
// Shutdown must be called to safely clean up Application if Init has been called
Application.Shutdown ();
}
[Fact]
public void TableView_ExpandLastColumn_False_ExactBounds ()
{
var tv = SetUpMiniTable ();
// the thing we are testing
tv.Style.ExpandLastColumn = false;
// width exactly matches the max col widths
tv.Bounds = new Rect (0, 0, 5, 4);
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│2│
";
TestHelpers.AssertDriverContentsAre (expected, output);
// Shutdown must be called to safely clean up Application if Init has been called
Application.Shutdown ();
}
[Fact]
[AutoInitShutdown]
public void TableView_Activate()
{
string activatedValue = null;
var tv = new TableView (BuildTable(1,1));
tv.CellActivated += (c) => activatedValue = c.Table.Rows[c.Row][c.Col].ToString();
Application.Top.Add (tv);
Application.Begin (Application.Top);
// pressing enter should activate the first cell (selected cell)
tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
Assert.Equal ("R0C0",activatedValue);
// reset the test
activatedValue = null;
// clear keybindings and ensure that Enter does not trigger the event anymore
tv.ClearKeybindings ();
tv.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
Assert.Null(activatedValue);
// New method for changing the activation key
tv.AddKeyBinding (Key.z, Command.Accept);
tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
Assert.Equal ("R0C0", activatedValue);
// reset the test
activatedValue = null;
tv.ClearKeybindings ();
// Old method for changing the activation key
tv.CellActivationKey = Key.z;
tv.ProcessKey (new KeyEvent (Key.z, new KeyModifiers ()));
Assert.Equal ("R0C0", activatedValue);
}
[Fact]
public void TableViewMultiSelect_CannotFallOffLeft()
{
var tv = SetUpMiniTable ();
tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
tv.MultiSelect = true;
tv.SelectedColumn = 1;
tv.SelectedRow = 1;
tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single().Rect);
// this next shift left should be ignored because we are already at the bounds
tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
Assert.Equal (0, tv.SelectedColumn);
Assert.Equal (1, tv.SelectedRow);
Application.Shutdown ();
}
[Fact]
public void TableViewMultiSelect_CannotFallOffRight()
{
var tv = SetUpMiniTable ();
tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
tv.MultiSelect = true;
tv.SelectedColumn = 0;
tv.SelectedRow = 1;
tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
// this next shift right should be ignored because we are already at the right bounds
tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 1, 2, 1), tv.MultiSelectedRegions.Single ().Rect);
Assert.Equal (1, tv.SelectedColumn);
Assert.Equal (1, tv.SelectedRow);
Application.Shutdown ();
}
[Fact]
public void TableViewMultiSelect_CannotFallOffBottom ()
{
var tv = SetUpMiniTable ();
tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
tv.MultiSelect = true;
tv.SelectedColumn = 0;
tv.SelectedRow = 0;
tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
// this next moves should be ignored because we already selected the whole table
tv.ProcessKey (new KeyEvent (Key.CursorRight | Key.ShiftMask, new KeyModifiers { Shift = true }));
tv.ProcessKey (new KeyEvent (Key.CursorDown | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
Assert.Equal (1, tv.SelectedColumn);
Assert.Equal (1, tv.SelectedRow);
Application.Shutdown ();
}
[Fact]
public void TableViewMultiSelect_CannotFallOffTop()
{
var tv = SetUpMiniTable ();
tv.Table.Rows.Add (1, 2); // add another row (brings us to 2 rows)
tv.MultiSelect = true;
tv.SelectedColumn = 1;
tv.SelectedRow = 1;
tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
// this next moves should be ignored because we already selected the whole table
tv.ProcessKey (new KeyEvent (Key.CursorLeft | Key.ShiftMask, new KeyModifiers { Shift = true }));
tv.ProcessKey (new KeyEvent (Key.CursorUp | Key.ShiftMask, new KeyModifiers { Shift = true }));
Assert.Equal (new Rect (0, 0, 2, 2), tv.MultiSelectedRegions.Single ().Rect);
Assert.Equal (0, tv.SelectedColumn);
Assert.Equal (0, tv.SelectedRow);
Application.Shutdown ();
}
[Theory]
[InlineData (false)]
[InlineData (true)]
public void TableView_ColorTests_FocusedOrNot (bool focused)
{
var tv = SetUpMiniTable ();
// width exactly matches the max col widths
tv.Bounds = new Rect (0, 0, 5, 4);
// private method for forcing the view to be focused/not focused
var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
// when the view is/isn't focused
setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│2│
";
TestHelpers.AssertDriverContentsAre (expected, output);
string expectedColors = @"
00000
00000
00000
01000
";
TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
// 0
tv.ColorScheme.Normal,
// 1
focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal});
Application.Shutdown();
}
[Theory]
[InlineData (false)]
[InlineData (true)]
public void TableView_ColorTests_InvertSelectedCellFirstCharacter (bool focused)
{
var tv = SetUpMiniTable ();
tv.Style.InvertSelectedCellFirstCharacter = true;
// width exactly matches the max col widths
tv.Bounds = new Rect (0, 0, 5, 4);
// private method for forcing the view to be focused/not focused
var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
// when the view is/isn't focused
setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│2│
";
TestHelpers.AssertDriverContentsAre (expected, output);
string expectedColors = @"
00000
00000
00000
01000
";
var invertHotFocus = new Attribute(tv.ColorScheme.HotFocus.Background,tv.ColorScheme.HotFocus.Foreground);
var invertHotNormal = new Attribute(tv.ColorScheme.HotNormal.Background,tv.ColorScheme.HotNormal.Foreground);
TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
// 0
tv.ColorScheme.Normal,
// 1
focused ? invertHotFocus : invertHotNormal});
Application.Shutdown();
}
[Theory]
[InlineData (false)]
[InlineData (true)]
public void TableView_ColorsTest_RowColorGetter (bool focused)
{
var tv = SetUpMiniTable ();
// width exactly matches the max col widths
tv.Bounds = new Rect (0, 0, 5, 4);
var rowHighlight = new ColorScheme () {
Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
HotNormal = Attribute.Make (Color.Green, Color.Blue),
HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
Focus = Attribute.Make (Color.Cyan, Color.Magenta),
};
// when B is 2 use the custom highlight colour for the row
tv.Style.RowColorGetter += (e)=>Convert.ToInt32(e.Table.Rows[e.RowIndex][1]) == 2 ? rowHighlight : null;
// private method for forcing the view to be focused/not focused
var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
// when the view is/isn't focused
setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│2│
";
TestHelpers.AssertDriverContentsAre (expected, output);
string expectedColors = @"
00000
00000
00000
21222
";
TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
// 0
tv.ColorScheme.Normal,
// 1
focused ? rowHighlight.HotFocus : rowHighlight.HotNormal,
// 2
rowHighlight.Normal});
// change the value in the table so that
// it no longer matches the RowColorGetter
// delegate conditional ( which checks for
// the value 2)
tv.Table.Rows[0][1] = 5;
tv.Redraw (tv.Bounds);
expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│5│
";
TestHelpers.AssertDriverContentsAre (expected, output);
expectedColors = @"
00000
00000
00000
01000
";
// now we only see 2 colors used (the selected cell color and Normal
// rowHighlight should no longer be used because the delegate returned null
// (now that the cell value is 5 - which does not match the conditional)
TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
// 0
tv.ColorScheme.Normal,
// 1
focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
// Shutdown must be called to safely clean up Application if Init has been called
Application.Shutdown ();
}
[Theory]
[InlineData (false)]
[InlineData (true)]
public void TableView_ColorsTest_ColorGetter (bool focused)
{
var tv = SetUpMiniTable ();
// width exactly matches the max col widths
tv.Bounds = new Rect (0, 0, 5, 4);
// Create a style for column B
var bStyle = tv.Style.GetOrCreateColumnStyle (tv.Table.Columns ["B"]);
// when B is 2 use the custom highlight colour
var cellHighlight = new ColorScheme () {
Normal = Attribute.Make (Color.BrightCyan, Color.DarkGray),
HotNormal = Attribute.Make (Color.Green, Color.Blue),
HotFocus = Attribute.Make (Color.BrightYellow, Color.White),
Focus = Attribute.Make (Color.Cyan, Color.Magenta),
};
bStyle.ColorGetter = (a) => Convert.ToInt32(a.CellValue) == 2 ? cellHighlight : null;
// private method for forcing the view to be focused/not focused
var setFocusMethod = typeof (View).GetMethod ("SetHasFocus", BindingFlags.Instance | BindingFlags.NonPublic);
// when the view is/isn't focused
setFocusMethod.Invoke (tv, new object [] { focused, tv, true });
tv.Redraw (tv.Bounds);
string expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│2│
";
TestHelpers.AssertDriverContentsAre (expected, output);
string expectedColors = @"
00000
00000
00000
01020
";
TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
// 0
tv.ColorScheme.Normal,
// 1
focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal,
// 2
cellHighlight.Normal});
// change the value in the table so that
// it no longer matches the ColorGetter
// delegate conditional ( which checks for
// the value 2)
tv.Table.Rows[0][1] = 5;
tv.Redraw (tv.Bounds);
expected = @"
┌─┬─┐
│A│B│
├─┼─┤
│1│5│
";
TestHelpers.AssertDriverContentsAre (expected, output);
expectedColors = @"
00000
00000
00000
01000
";
// now we only see 2 colors used (the selected cell color and Normal
// cellHighlight should no longer be used because the delegate returned null
// (now that the cell value is 5 - which does not match the conditional)
TestHelpers.AssertDriverColorsAre (expectedColors, new Attribute [] {
// 0
tv.ColorScheme.Normal,
// 1
focused ? tv.ColorScheme.HotFocus : tv.ColorScheme.HotNormal });
// Shutdown must be called to safely clean up Application if Init has been called
Application.Shutdown ();
}
private TableView SetUpMiniTable ()
{
var tv = new TableView ();
tv.Bounds = new Rect (0, 0, 10, 4);
var dt = new DataTable ();
var colA = dt.Columns.Add ("A");
var colB = dt.Columns.Add ("B");
dt.Rows.Add (1, 2);
tv.Table = dt;
tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
tv.Style.GetOrCreateColumnStyle (colA).MinWidth = 1;
tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
tv.Style.GetOrCreateColumnStyle (colB).MaxWidth = 1;
GraphViewTests.InitFakeDriver ();
tv.ColorScheme = Colors.Base;
return tv;
}
[Fact]
[AutoInitShutdown]
public void ScrollDown_OneLineAtATime ()
{
var tableView = new TableView ();
// Set big table
tableView.Table = BuildTable (25, 50);
// 1 header + 4 rows visible
tableView.Bounds = new Rect (0, 0, 25, 5);
tableView.Style.ShowHorizontalHeaderUnderline = false;
tableView.Style.ShowHorizontalHeaderOverline = false;
tableView.Style.AlwaysShowHeaders = true;
// select last row
tableView.SelectedRow = 3; // row is 0 indexed so this is the 4th visible row
// Scroll down
tableView.ProcessKey (new KeyEvent () { Key = Key.CursorDown });
// Scrolled off the page by 1 row so it should only have moved down 1 line of RowOffset
Assert.Equal(4,tableView.SelectedRow);
Assert.Equal (1, tableView.RowOffset);
}
[Fact]
public void ScrollRight_SmoothScrolling ()
{
GraphViewTests.InitFakeDriver ();
var tableView = new TableView ();
tableView.ColorScheme = Colors.TopLevel;
// 3 columns are visibile
tableView.Bounds = new Rect (0, 0, 7, 5);
tableView.Style.ShowHorizontalHeaderUnderline = false;
tableView.Style.ShowHorizontalHeaderOverline = false;
tableView.Style.AlwaysShowHeaders = true;
tableView.Style.SmoothHorizontalScrolling = true;
var dt = new DataTable ();
dt.Columns.Add ("A");
dt.Columns.Add ("B");
dt.Columns.Add ("C");
dt.Columns.Add ("D");
dt.Columns.Add ("E");
dt.Columns.Add ("F");
dt.Rows.Add (1, 2, 3, 4, 5, 6);
tableView.Table = dt;
// select last visible column
tableView.SelectedColumn = 2; // column C