DateFieldTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #nullable enable
  2. using System.Globalization;
  3. using System.Runtime.InteropServices;
  4. namespace UnitTests.ViewsTests;
  5. public class DateFieldTests
  6. {
  7. [Fact]
  8. [TestDate]
  9. public void Constructors_Defaults ()
  10. {
  11. var df = new DateField ();
  12. df.Layout ();
  13. Assert.Equal (DateTime.MinValue, df.Date);
  14. Assert.Equal (1, df.CursorPosition);
  15. Assert.Equal (new (0, 0, 12, 1), df.Frame);
  16. Assert.Equal (" 01/01/0001", df.Text);
  17. DateTime date = DateTime.Now;
  18. df = new (date);
  19. df.Layout ();
  20. Assert.Equal (date, df.Date);
  21. Assert.Equal (1, df.CursorPosition);
  22. Assert.Equal (new (0, 0, 12, 1), df.Frame);
  23. Assert.Equal ($" {date.ToString (CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)}", df.Text);
  24. df = new (date) { X = 1, Y = 2 };
  25. df.Layout ();
  26. Assert.Equal (date, df.Date);
  27. Assert.Equal (1, df.CursorPosition);
  28. Assert.Equal (new (1, 2, 12, 1), df.Frame);
  29. Assert.Equal ($" {date.ToString (CultureInfo.InvariantCulture.DateTimeFormat.ShortDatePattern)}", df.Text);
  30. }
  31. [Fact]
  32. [TestDate]
  33. [SetupFakeApplication]
  34. public void Copy_Paste ()
  35. {
  36. var df1 = new DateField (DateTime.Parse ("12/12/1971"));
  37. var df2 = new DateField (DateTime.Parse ("12/31/2023"));
  38. // Select all text
  39. Assert.True (df2.NewKeyDownEvent (Key.End.WithShift));
  40. Assert.Equal (1, df2.SelectedStart);
  41. Assert.Equal (10, df2.SelectedLength);
  42. Assert.Equal (11, df2.CursorPosition);
  43. // Copy from df2
  44. Assert.True (df2.NewKeyDownEvent (Key.C.WithCtrl));
  45. // Paste into df1
  46. Assert.True (df1.NewKeyDownEvent (Key.V.WithCtrl));
  47. Assert.Equal (" 12/31/2023", df1.Text);
  48. Assert.Equal (11, df1.CursorPosition);
  49. }
  50. [Fact]
  51. [TestDate]
  52. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format ()
  53. {
  54. var df = new DateField ();
  55. Assert.Equal (1, df.CursorPosition);
  56. df.CursorPosition = 0;
  57. Assert.Equal (1, df.CursorPosition);
  58. df.CursorPosition = 11;
  59. Assert.Equal (10, df.CursorPosition);
  60. }
  61. [Fact]
  62. [TestDate]
  63. public void CursorPosition_Min_Is_Always_One_Max_Is_Always_Max_Format_After_Selection ()
  64. {
  65. var df = new DateField ();
  66. // Start selection
  67. Assert.True (df.NewKeyDownEvent (Key.CursorLeft.WithShift));
  68. Assert.Equal (1, df.SelectedStart);
  69. Assert.Equal (1, df.SelectedLength);
  70. Assert.Equal (0, df.CursorPosition);
  71. // Without selection
  72. Assert.True (df.NewKeyDownEvent (Key.CursorLeft));
  73. Assert.Equal (-1, df.SelectedStart);
  74. Assert.Equal (0, df.SelectedLength);
  75. Assert.Equal (1, df.CursorPosition);
  76. df.CursorPosition = 10;
  77. Assert.True (df.NewKeyDownEvent (Key.CursorRight.WithShift));
  78. Assert.Equal (10, df.SelectedStart);
  79. Assert.Equal (1, df.SelectedLength);
  80. Assert.Equal (11, df.CursorPosition);
  81. Assert.True (df.NewKeyDownEvent (Key.CursorRight));
  82. Assert.Equal (-1, df.SelectedStart);
  83. Assert.Equal (0, df.SelectedLength);
  84. Assert.Equal (10, df.CursorPosition);
  85. }
  86. [Fact]
  87. [TestDate]
  88. public void Date_Start_From_01_01_0001_And_End_At_12_31_9999 ()
  89. {
  90. var df = new DateField (DateTime.Parse ("01/01/0001"));
  91. Assert.Equal (" 01/01/0001", df.Text);
  92. df.Date = DateTime.Parse ("12/31/9999");
  93. Assert.Equal (" 12/31/9999", df.Text);
  94. }
  95. [Fact]
  96. [TestDate]
  97. public void KeyBindings_Command ()
  98. {
  99. var df = new DateField (DateTime.Parse ("12/12/1971")) { ReadOnly = true };
  100. Assert.True (df.NewKeyDownEvent (Key.Delete));
  101. Assert.Equal (" 12/12/1971", df.Text);
  102. df.ReadOnly = false;
  103. Assert.True (df.NewKeyDownEvent (Key.D.WithCtrl));
  104. Assert.Equal (" 02/12/1971", df.Text);
  105. df.CursorPosition = 4;
  106. df.ReadOnly = true;
  107. Assert.True (df.NewKeyDownEvent (Key.Delete));
  108. Assert.Equal (" 02/12/1971", df.Text);
  109. df.ReadOnly = false;
  110. Assert.True (df.NewKeyDownEvent (Key.Backspace));
  111. Assert.Equal (" 02/02/1971", df.Text);
  112. Assert.True (df.NewKeyDownEvent (Key.Home));
  113. Assert.Equal (1, df.CursorPosition);
  114. Assert.True (df.NewKeyDownEvent (Key.End));
  115. Assert.Equal (10, df.CursorPosition);
  116. Assert.True (df.NewKeyDownEvent (Key.E.WithCtrl));
  117. Assert.Equal (10, df.CursorPosition);
  118. Assert.True (df.NewKeyDownEvent (Key.CursorLeft));
  119. Assert.Equal (9, df.CursorPosition);
  120. Assert.True (df.NewKeyDownEvent (Key.CursorRight));
  121. Assert.Equal (10, df.CursorPosition);
  122. // Non-numerics are ignored
  123. Assert.False (df.NewKeyDownEvent (Key.A));
  124. df.ReadOnly = true;
  125. df.CursorPosition = 1;
  126. Assert.True (df.NewKeyDownEvent (Key.D1));
  127. Assert.Equal (" 02/02/1971", df.Text);
  128. df.ReadOnly = false;
  129. Assert.True (df.NewKeyDownEvent (Key.D1));
  130. Assert.Equal (" 12/02/1971", df.Text);
  131. Assert.Equal (2, df.CursorPosition);
  132. #if UNIX_KEY_BINDINGS
  133. Assert.True (df.NewKeyDownEvent (Key.D.WithAlt));
  134. Assert.Equal (" 10/02/1971", df.Text);
  135. #endif
  136. }
  137. [Fact]
  138. [TestDate]
  139. public void Typing_With_Selection_Normalize_Format ()
  140. {
  141. var df = new DateField (DateTime.Parse ("12/12/1971"))
  142. {
  143. // Start selection at before the first separator /
  144. CursorPosition = 2
  145. };
  146. // Now select the separator /
  147. Assert.True (df.NewKeyDownEvent (Key.CursorRight.WithShift));
  148. Assert.Equal (2, df.SelectedStart);
  149. Assert.Equal (1, df.SelectedLength);
  150. Assert.Equal (3, df.CursorPosition);
  151. // Type 3 over the separator
  152. Assert.True (df.NewKeyDownEvent (Key.D3));
  153. // The format was normalized and replaced again with /
  154. Assert.Equal (" 12/12/1971", df.Text);
  155. Assert.Equal (4, df.CursorPosition);
  156. }
  157. [Fact]
  158. [TestDate]
  159. public void Culture_Pt_Portuguese ()
  160. {
  161. CultureInfo cultureBackup = CultureInfo.CurrentCulture;
  162. try
  163. {
  164. CultureInfo.CurrentCulture = new ("pt-PT");
  165. var df = new DateField (DateTime.Parse ("12/12/1971"))
  166. {
  167. // Move to the first 2
  168. CursorPosition = 2
  169. };
  170. // Type 3 over the separator
  171. Assert.True (df.NewKeyDownEvent (Key.D3));
  172. // If InvariantCulture was used this will fail but not with PT culture
  173. Assert.Equal (" 13/12/1971", df.Text);
  174. Assert.Equal ("13/12/1971", df.Date!.Value.ToString (CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern));
  175. Assert.Equal (4, df.CursorPosition);
  176. }
  177. finally
  178. {
  179. CultureInfo.CurrentCulture = cultureBackup;
  180. }
  181. }
  182. /// <summary>
  183. /// Tests specific culture date formatting edge cases.
  184. /// Split from the monolithic culture test for better isolation and maintainability.
  185. /// </summary>
  186. [Theory]
  187. [TestDate]
  188. [InlineData ("en-US", "01/01/1971", '/')]
  189. [InlineData ("en-GB", "01/01/1971", '/')]
  190. [InlineData ("de-DE", "01.01.1971", '.')]
  191. [InlineData ("fr-FR", "01/01/1971", '/')]
  192. [InlineData ("es-ES", "01/01/1971", '/')]
  193. [InlineData ("it-IT", "01/01/1971", '/')]
  194. [InlineData ("ja-JP", "1971/01/01", '/')]
  195. [InlineData ("zh-CN", "1971/01/01", '/')]
  196. [InlineData ("ko-KR", "1971.01.01", '.')]
  197. [InlineData ("pt-PT", "01/01/1971", '/')]
  198. [InlineData ("pt-BR", "01/01/1971", '/')]
  199. [InlineData ("ru-RU", "01.01.1971", '.')]
  200. [InlineData ("nl-NL", "01-01-1971", '-')]
  201. [InlineData ("sv-SE", "1971-01-01", '-')]
  202. [InlineData ("pl-PL", "01.01.1971", '.')]
  203. [InlineData ("tr-TR", "01.01.1971", '.')]
  204. public void Culture_SpecificCultures_ProducesExpectedFormat (string cultureName, string expectedDate, char expectedSeparator)
  205. {
  206. // Skip cultures that may have platform-specific issues
  207. if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  208. {
  209. // macOS has known issues with certain cultures - see #3592
  210. string [] problematicOnMac = { "ar-SA", "en-SA", "en-TH", "th", "th-TH" };
  211. if (problematicOnMac.Contains (cultureName))
  212. {
  213. return;
  214. }
  215. }
  216. CultureInfo cultureBackup = CultureInfo.CurrentCulture;
  217. try
  218. {
  219. var culture = new CultureInfo (cultureName);
  220. // Parse date using InvariantCulture BEFORE changing CurrentCulture
  221. DateTime date = DateTime.Parse ("1/1/1971", CultureInfo.InvariantCulture);
  222. CultureInfo.CurrentCulture = culture;
  223. var df = new DateField (date);
  224. // Verify the text contains the expected separator
  225. Assert.Contains (expectedSeparator, df.Text);
  226. // Verify the date is formatted correctly (accounting for leading space)
  227. Assert.Equal ($" {expectedDate}", df.Text);
  228. }
  229. catch (CultureNotFoundException)
  230. {
  231. // Skip cultures not available on this system
  232. }
  233. finally
  234. {
  235. CultureInfo.CurrentCulture = cultureBackup;
  236. }
  237. }
  238. /// <summary>
  239. /// Tests right-to-left cultures separately due to their complexity.
  240. /// </summary>
  241. [Theory]
  242. [TestDate]
  243. [InlineData ("ar-SA")] // Arabic (Saudi Arabia)
  244. [InlineData ("he-IL")] // Hebrew (Israel)
  245. [InlineData ("fa-IR")] // Persian (Iran)
  246. public void Culture_RightToLeft_HandlesFormatting (string cultureName)
  247. {
  248. if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  249. {
  250. // macOS has known issues with RTL cultures - see #3592
  251. return;
  252. }
  253. CultureInfo cultureBackup = CultureInfo.CurrentCulture;
  254. try
  255. {
  256. var culture = new CultureInfo (cultureName);
  257. // Parse date using InvariantCulture BEFORE changing CurrentCulture
  258. // This is critical because RTL cultures may use different calendars
  259. DateTime date = DateTime.Parse ("1/1/1971", CultureInfo.InvariantCulture);
  260. CultureInfo.CurrentCulture = culture;
  261. var df = new DateField (date);
  262. // Just verify DateField doesn't crash with RTL cultures
  263. // and produces some text
  264. Assert.NotEmpty (df.Text);
  265. Assert.NotNull (df.Date);
  266. }
  267. catch (CultureNotFoundException)
  268. {
  269. // Skip cultures not available on this system
  270. }
  271. finally
  272. {
  273. CultureInfo.CurrentCulture = cultureBackup;
  274. }
  275. }
  276. /// <summary>
  277. /// Tests that DateField handles calendar systems that differ from Gregorian.
  278. /// </summary>
  279. [Theory]
  280. [TestDate]
  281. [InlineData ("th-TH")] // Thai Buddhist calendar
  282. public void Culture_NonGregorianCalendar_HandlesFormatting (string cultureName)
  283. {
  284. if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  285. {
  286. // macOS has known issues with certain calendars - see #3592
  287. return;
  288. }
  289. CultureInfo cultureBackup = CultureInfo.CurrentCulture;
  290. try
  291. {
  292. var culture = new CultureInfo (cultureName);
  293. // Parse date using InvariantCulture BEFORE changing CurrentCulture
  294. DateTime date = DateTime.Parse ("1/1/1971", CultureInfo.InvariantCulture);
  295. CultureInfo.CurrentCulture = culture;
  296. var df = new DateField (date);
  297. // Buddhist calendar is 543 years ahead (1971 + 543 = 2514)
  298. // Just verify it doesn't crash and produces valid output
  299. Assert.NotEmpty (df.Text);
  300. Assert.NotNull (df.Date);
  301. }
  302. catch (CultureNotFoundException)
  303. {
  304. // Skip cultures not available on this system
  305. }
  306. finally
  307. {
  308. CultureInfo.CurrentCulture = cultureBackup;
  309. }
  310. }
  311. }