TimeFieldTests.cs 6.9 KB

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