DateFieldTests.cs 12 KB

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