DatePickerTests.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.Globalization;
  2. namespace Terminal.Gui.ViewsTests;
  3. public class DatePickerTests
  4. {
  5. [Fact]
  6. public void DatePicker_ChangingCultureChangesFormat ()
  7. {
  8. var date = new DateTime (2000, 7, 23);
  9. var datePicker = new DatePicker (date);
  10. datePicker.Culture = CultureInfo.GetCultureInfo ("en-GB");
  11. Assert.Equal ("23/07/2000", datePicker.Text);
  12. datePicker.Culture = CultureInfo.GetCultureInfo ("pl-PL");
  13. Assert.Equal ("23.07.2000", datePicker.Text);
  14. // Deafult date format for en-US is M/d/yyyy but we are using StandardizeDateFormat method
  15. // to convert it to the format that has 2 digits for month and day.
  16. datePicker.Culture = CultureInfo.GetCultureInfo ("en-US");
  17. Assert.Equal ("07/23/2000", datePicker.Text);
  18. }
  19. [Fact]
  20. public void DatePicker_Initialize_ShouldSetCurrentDate ()
  21. {
  22. var datePicker = new DatePicker ();
  23. Assert.Equal (DateTime.Now.Date.Day, datePicker.Date.Day);
  24. Assert.Equal (DateTime.Now.Date.Month, datePicker.Date.Month);
  25. Assert.Equal (DateTime.Now.Date.Year, datePicker.Date.Year);
  26. }
  27. [Fact]
  28. public void DatePicker_SetDate_ShouldChangeText ()
  29. {
  30. var datePicker = new DatePicker { Culture = CultureInfo.GetCultureInfo ("en-GB") };
  31. var newDate = new DateTime (2024, 1, 15);
  32. string format = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
  33. datePicker.Date = newDate;
  34. Assert.Equal (newDate.ToString (format), datePicker.Text);
  35. }
  36. [Fact]
  37. [AutoInitShutdown]
  38. public void DatePicker_ShouldNot_SetDateOutOfRange_UsingNextMonthButton ()
  39. {
  40. var date = new DateTime (9999, 11, 15);
  41. var datePicker = new DatePicker (date);
  42. var top = new Toplevel ();
  43. top.Add (datePicker);
  44. Application.Begin (top);
  45. // Set focus to next month button
  46. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  47. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  48. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  49. // Change month to December
  50. Assert.True (datePicker.NewKeyDownEvent (Key.Enter));
  51. Assert.Equal (12, datePicker.Date.Month);
  52. // Date should not change as next month button is disabled
  53. Assert.False (datePicker.NewKeyDownEvent (Key.Enter));
  54. Assert.Equal (12, datePicker.Date.Month);
  55. top.Dispose ();
  56. }
  57. [Fact]
  58. [AutoInitShutdown]
  59. public void DatePicker_ShouldNot_SetDateOutOfRange_UsingPreviousMonthButton ()
  60. {
  61. var date = new DateTime (1, 2, 15);
  62. var datePicker = new DatePicker (date);
  63. var top = new Toplevel ();
  64. // Move focus to previous month button
  65. top.Add (datePicker);
  66. Application.Begin (top);
  67. // set focus to the previous month button
  68. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  69. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  70. // Change month to January
  71. Assert.True (datePicker.NewKeyDownEvent (Key.Enter));
  72. Assert.Equal (1, datePicker.Date.Month);
  73. // Date should not change as previous month button is disabled
  74. Assert.False (datePicker.NewKeyDownEvent (Key.Enter));
  75. Assert.Equal (1, datePicker.Date.Month);
  76. top.Dispose ();
  77. }
  78. }