DatePickerTests.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Globalization;
  2. using UnitTests;
  3. namespace UnitTests.ViewsTests;
  4. public class DatePickerTests
  5. {
  6. [Fact]
  7. [AutoInitShutdown]
  8. public void DatePicker_ShouldNot_SetDateOutOfRange_UsingNextMonthButton ()
  9. {
  10. var date = new DateTime (9999, 11, 15);
  11. var datePicker = new DatePicker (date);
  12. var top = new Runnable ();
  13. top.Add (datePicker);
  14. Application.Begin (top);
  15. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_dateField"), datePicker.Focused);
  16. // Set focus to next month button
  17. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  18. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_calendar"), datePicker.Focused);
  19. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  20. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_previousMonthButton"), datePicker.Focused);
  21. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  22. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_nextMonthButton"), datePicker.Focused);
  23. // Change month to December
  24. Assert.False (Application.RaiseKeyDownEvent (Key.Enter));
  25. Assert.Equal (12, datePicker.Date.Month);
  26. // Next month button is disabled, so focus advanced to edit field
  27. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_previousMonthButton"), datePicker.Focused);
  28. top.Dispose ();
  29. }
  30. [Fact]
  31. [AutoInitShutdown]
  32. public void DatePicker_ShouldNot_SetDateOutOfRange_UsingPreviousMonthButton ()
  33. {
  34. var date = new DateTime (1, 2, 15);
  35. var datePicker = new DatePicker (date);
  36. var top = new Runnable ();
  37. // Move focus to previous month button
  38. top.Add (datePicker);
  39. Application.Begin (top);
  40. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_dateField"), datePicker.Focused);
  41. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  42. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_calendar"), datePicker.Focused);
  43. datePicker.AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  44. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_previousMonthButton"), datePicker.Focused);
  45. // Change month to January
  46. Assert.False (datePicker.NewKeyDownEvent (Key.Enter));
  47. Assert.Equal (1, datePicker.Date.Month);
  48. // Next prev button is disabled, so focus advanced to edit button
  49. Assert.Equal (datePicker.SubViews.First (v => v.Id == "_calendar"), datePicker.Focused);
  50. top.Dispose ();
  51. }
  52. }