TimeFieldTests.cs 6.9 KB

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