TimeFieldTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System;
  2. using Xunit;
  3. namespace Terminal.Gui.ViewsTests;
  4. public class TimeFieldTests {
  5. [Fact]
  6. public void Constructors_Defaults ()
  7. {
  8. var tf = new TimeField ();
  9. Assert.False (tf.IsShortFormat);
  10. Assert.Equal (TimeSpan.MinValue, tf.Time);
  11. Assert.Equal (1, tf.CursorPosition);
  12. Assert.Equal (new Rect (0, 0, 10, 1), tf.Frame);
  13. var time = DateTime.Now.TimeOfDay;
  14. tf = new TimeField (time);
  15. Assert.False (tf.IsShortFormat);
  16. Assert.Equal (time, tf.Time);
  17. Assert.Equal (1, tf.CursorPosition);
  18. Assert.Equal (new Rect (0, 0, 10, 1), tf.Frame);
  19. tf = new TimeField (1, 2, time);
  20. Assert.False (tf.IsShortFormat);
  21. Assert.Equal (time, tf.Time);
  22. Assert.Equal (1, tf.CursorPosition);
  23. Assert.Equal (new Rect (1, 2, 10, 1), tf.Frame);
  24. tf = new TimeField (3, 4, time, true);
  25. Assert.True (tf.IsShortFormat);
  26. Assert.Equal (time, tf.Time);
  27. Assert.Equal (1, tf.CursorPosition);
  28. Assert.Equal (new Rect (3, 4, 7, 1), tf.Frame);
  29. tf.IsShortFormat = false;
  30. Assert.Equal (new Rect (3, 4, 10, 1), tf.Frame);
  31. Assert.Equal (10, tf.Width);
  32. }
  33. [Fact]
  34. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
  35. {
  36. var tf = new TimeField ();
  37. Assert.Equal (1, tf.CursorPosition);
  38. tf.CursorPosition = 0;
  39. Assert.Equal (1, tf.CursorPosition);
  40. tf.CursorPosition = 9;
  41. Assert.Equal (8, tf.CursorPosition);
  42. tf.IsShortFormat = true;
  43. tf.CursorPosition = 0;
  44. Assert.Equal (1, tf.CursorPosition);
  45. tf.CursorPosition = 6;
  46. Assert.Equal (5, tf.CursorPosition);
  47. }
  48. [Fact]
  49. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format_After_Selection ()
  50. {
  51. var tf = new TimeField ();
  52. // Start selection
  53. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorLeft | KeyCode.ShiftMask)));
  54. Assert.Equal (1, tf.SelectedStart);
  55. Assert.Equal (1, tf.SelectedLength);
  56. Assert.Equal (0, tf.CursorPosition);
  57. // Without selection
  58. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorLeft)));
  59. Assert.Equal (-1, tf.SelectedStart);
  60. Assert.Equal (0, tf.SelectedLength);
  61. Assert.Equal (1, tf.CursorPosition);
  62. tf.CursorPosition = 8;
  63. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
  64. Assert.Equal (8, tf.SelectedStart);
  65. Assert.Equal (1, tf.SelectedLength);
  66. Assert.Equal (9, tf.CursorPosition);
  67. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight)));
  68. Assert.Equal (-1, tf.SelectedStart);
  69. Assert.Equal (0, tf.SelectedLength);
  70. Assert.Equal (8, tf.CursorPosition);
  71. Assert.False (tf.IsShortFormat);
  72. tf.IsShortFormat = true;
  73. Assert.Equal (5, tf.CursorPosition);
  74. // Start selection
  75. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
  76. Assert.Equal (5, tf.SelectedStart);
  77. Assert.Equal (1, tf.SelectedLength);
  78. Assert.Equal (6, tf.CursorPosition);
  79. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight)));
  80. Assert.Equal (-1, tf.SelectedStart);
  81. Assert.Equal (0, tf.SelectedLength);
  82. Assert.Equal (5, tf.CursorPosition);
  83. }
  84. [Fact]
  85. public void KeyBindings_Command ()
  86. {
  87. TimeField tf = new TimeField (TimeSpan.Parse ("12:12:19"));
  88. tf.ReadOnly = true;
  89. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Delete)));
  90. Assert.Equal (" 12:12:19", tf.Text);
  91. tf.ReadOnly = false;
  92. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D | KeyCode.CtrlMask)));
  93. Assert.Equal (" 02:12:19", tf.Text);
  94. tf.CursorPosition = 4;
  95. tf.ReadOnly = true;
  96. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Delete)));
  97. Assert.Equal (" 02:12:19", tf.Text);
  98. tf.ReadOnly = false;
  99. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Backspace)));
  100. Assert.Equal (" 02:02:19", tf.Text);
  101. Assert.True (tf.NewKeyDownEvent (new (KeyCode.Home)));
  102. Assert.Equal (1, tf.CursorPosition);
  103. Assert.True (tf.NewKeyDownEvent (new (KeyCode.End)));
  104. Assert.Equal (8, tf.CursorPosition);
  105. Assert.True (tf.NewKeyDownEvent (new (KeyCode.A | KeyCode.CtrlMask)));
  106. Assert.Equal (1, tf.CursorPosition);
  107. Assert.True (tf.NewKeyDownEvent (new (KeyCode.E | KeyCode.CtrlMask)));
  108. Assert.Equal (8, tf.CursorPosition);
  109. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorLeft)));
  110. Assert.Equal (7, tf.CursorPosition);
  111. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight)));
  112. Assert.Equal (8, tf.CursorPosition);
  113. // Non-numerics are ignored
  114. Assert.False (tf.NewKeyDownEvent (new (KeyCode.A)));
  115. tf.ReadOnly = true;
  116. tf.CursorPosition = 1;
  117. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D1)));
  118. Assert.Equal (" 02:02:19", tf.Text);
  119. tf.ReadOnly = false;
  120. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D1)));
  121. Assert.Equal (" 12:02:19", tf.Text);
  122. Assert.Equal (2, tf.CursorPosition);
  123. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D | KeyCode.AltMask)));
  124. Assert.Equal (" 10:02:19", tf.Text);
  125. }
  126. [Fact]
  127. public void Typing_With_Selection_Normalize_Format ()
  128. {
  129. TimeField tf = new TimeField (TimeSpan.Parse ("12:12:19"));
  130. // Start selection at before the first separator :
  131. tf.CursorPosition = 2;
  132. // Now select the separator :
  133. Assert.True (tf.NewKeyDownEvent (new (KeyCode.CursorRight | KeyCode.ShiftMask)));
  134. Assert.Equal (2, tf.SelectedStart);
  135. Assert.Equal (1, tf.SelectedLength);
  136. Assert.Equal (3, tf.CursorPosition);
  137. // Type 3 over the separator
  138. Assert.True (tf.NewKeyDownEvent (new (KeyCode.D3)));
  139. // The format was normalized and replaced again with :
  140. Assert.Equal (" 12:12:19", tf.Text);
  141. Assert.Equal (4, tf.CursorPosition);
  142. }
  143. [Fact, AutoInitShutdown]
  144. public void Copy_Paste ()
  145. {
  146. TimeField tf1 = new TimeField (TimeSpan.Parse ("12:12:19"));
  147. TimeField tf2 = new TimeField (TimeSpan.Parse ("12:59:01"));
  148. // Select all text
  149. Assert.True (tf2.NewKeyDownEvent (new (KeyCode.End | KeyCode.ShiftMask)));
  150. Assert.Equal (1, tf2.SelectedStart);
  151. Assert.Equal (8, tf2.SelectedLength);
  152. Assert.Equal (9, tf2.CursorPosition);
  153. // Copy from tf2
  154. Assert.True (tf2.NewKeyDownEvent (new (KeyCode.C | KeyCode.CtrlMask)));
  155. // Paste into tf1
  156. Assert.True (tf1.NewKeyDownEvent (new (KeyCode.V | KeyCode.CtrlMask)));
  157. Assert.Equal (" 12:59:01", tf1.Text);
  158. Assert.Equal (9, tf1.CursorPosition);
  159. }
  160. }