TimeFieldTests.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. namespace 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. public void Copy_Paste ()
  39. {
  40. IApplication app = Application.Create();
  41. app.Init("fake");
  42. try
  43. {
  44. var tf1 = new TimeField { Time = TimeSpan.Parse ("12:12:19"), App = app };
  45. var tf2 = new TimeField { Time = TimeSpan.Parse ("12:59:01"), App = app };
  46. // Select all text
  47. Assert.True (tf2.NewKeyDownEvent (Key.End.WithShift));
  48. Assert.Equal (1, tf2.SelectedStart);
  49. Assert.Equal (8, tf2.SelectedLength);
  50. Assert.Equal (9, tf2.CursorPosition);
  51. // Copy from tf2
  52. Assert.True (tf2.NewKeyDownEvent (Key.C.WithCtrl));
  53. // Paste into tf1
  54. Assert.True (tf1.NewKeyDownEvent (Key.V.WithCtrl));
  55. Assert.Equal (" 12:59:01", tf1.Text);
  56. Assert.Equal (9, tf1.CursorPosition);
  57. }
  58. finally
  59. {
  60. app.Dispose ();
  61. }
  62. }
  63. [Fact]
  64. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
  65. {
  66. var tf = new TimeField ();
  67. Assert.Equal (1, tf.CursorPosition);
  68. tf.CursorPosition = 0;
  69. Assert.Equal (1, tf.CursorPosition);
  70. tf.CursorPosition = 9;
  71. Assert.Equal (8, tf.CursorPosition);
  72. tf.IsShortFormat = true;
  73. tf.CursorPosition = 0;
  74. Assert.Equal (1, tf.CursorPosition);
  75. tf.CursorPosition = 6;
  76. Assert.Equal (5, tf.CursorPosition);
  77. }
  78. [Fact]
  79. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format_After_Selection ()
  80. {
  81. var tf = new TimeField ();
  82. // Start selection
  83. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft.WithShift));
  84. Assert.Equal (1, tf.SelectedStart);
  85. Assert.Equal (1, tf.SelectedLength);
  86. Assert.Equal (0, tf.CursorPosition);
  87. // Without selection
  88. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  89. Assert.Equal (-1, tf.SelectedStart);
  90. Assert.Equal (0, tf.SelectedLength);
  91. Assert.Equal (1, tf.CursorPosition);
  92. tf.CursorPosition = 8;
  93. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  94. Assert.Equal (8, tf.SelectedStart);
  95. Assert.Equal (1, tf.SelectedLength);
  96. Assert.Equal (9, tf.CursorPosition);
  97. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  98. Assert.Equal (-1, tf.SelectedStart);
  99. Assert.Equal (0, tf.SelectedLength);
  100. Assert.Equal (8, tf.CursorPosition);
  101. Assert.False (tf.IsShortFormat);
  102. Assert.False (tf.IsInitialized);
  103. tf.BeginInit ();
  104. tf.EndInit ();
  105. tf.IsShortFormat = true;
  106. Assert.Equal (5, tf.CursorPosition);
  107. // Start selection
  108. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  109. Assert.Equal (5, tf.SelectedStart);
  110. Assert.Equal (1, tf.SelectedLength);
  111. Assert.Equal (6, tf.CursorPosition);
  112. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  113. Assert.Equal (-1, tf.SelectedStart);
  114. Assert.Equal (0, tf.SelectedLength);
  115. Assert.Equal (5, tf.CursorPosition);
  116. }
  117. [Fact]
  118. public void KeyBindings_Command ()
  119. {
  120. var tf = new TimeField { Time = TimeSpan.Parse ("12:12:19") };
  121. tf.BeginInit ();
  122. tf.EndInit ();
  123. Assert.Equal (9, tf.CursorPosition);
  124. tf.CursorPosition = 1;
  125. tf.ReadOnly = true;
  126. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  127. Assert.Equal (" 12:12:19", tf.Text);
  128. tf.ReadOnly = false;
  129. Assert.True (tf.NewKeyDownEvent (Key.D.WithCtrl));
  130. Assert.Equal (" 02:12:19", tf.Text);
  131. tf.CursorPosition = 4;
  132. tf.ReadOnly = true;
  133. Assert.True (tf.NewKeyDownEvent (Key.Delete));
  134. Assert.Equal (" 02:12:19", tf.Text);
  135. tf.ReadOnly = false;
  136. Assert.True (tf.NewKeyDownEvent (Key.Backspace));
  137. Assert.Equal (" 02:02:19", tf.Text);
  138. Assert.True (tf.NewKeyDownEvent (Key.Home));
  139. Assert.Equal (1, tf.CursorPosition);
  140. Assert.True (tf.NewKeyDownEvent (Key.End));
  141. Assert.Equal (8, tf.CursorPosition);
  142. Assert.True (tf.NewKeyDownEvent (Key.A.WithCtrl));
  143. Assert.Equal (1, tf.CursorPosition);
  144. Assert.Equal (9, tf.Text.Length);
  145. Assert.True (tf.NewKeyDownEvent (Key.E.WithCtrl));
  146. Assert.Equal (8, tf.CursorPosition);
  147. Assert.True (tf.NewKeyDownEvent (Key.CursorLeft));
  148. Assert.Equal (7, tf.CursorPosition);
  149. Assert.True (tf.NewKeyDownEvent (Key.CursorRight));
  150. Assert.Equal (8, tf.CursorPosition);
  151. // Non-numerics are ignored
  152. Assert.False (tf.NewKeyDownEvent (Key.A));
  153. tf.ReadOnly = true;
  154. tf.CursorPosition = 1;
  155. Assert.True (tf.NewKeyDownEvent (Key.D1));
  156. Assert.Equal (" 02:02:19", tf.Text);
  157. tf.ReadOnly = false;
  158. Assert.True (tf.NewKeyDownEvent (Key.D1));
  159. Assert.Equal (" 12:02:19", tf.Text);
  160. Assert.Equal (2, tf.CursorPosition);
  161. #if UNIX_KEY_BINDINGS
  162. Assert.True (tf.NewKeyDownEvent (Key.D.WithAlt));
  163. Assert.Equal (" 10:02:19", tf.Text);
  164. #endif
  165. }
  166. [Fact]
  167. public void Typing_With_Selection_Normalize_Format ()
  168. {
  169. var tf = new TimeField { Time = TimeSpan.Parse ("12:12:19") };
  170. // Start selection at before the first separator :
  171. tf.CursorPosition = 2;
  172. // Now select the separator :
  173. Assert.True (tf.NewKeyDownEvent (Key.CursorRight.WithShift));
  174. Assert.Equal (2, tf.SelectedStart);
  175. Assert.Equal (1, tf.SelectedLength);
  176. Assert.Equal (3, tf.CursorPosition);
  177. // Type 3 over the separator
  178. Assert.True (tf.NewKeyDownEvent (Key.D3));
  179. // The format was normalized and replaced again with :
  180. Assert.Equal (" 12:12:19", tf.Text);
  181. Assert.Equal (4, tf.CursorPosition);
  182. }
  183. }