TimeFieldTests.cs 6.8 KB

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