CalendarDatePickerTests.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Input;
  6. using Avalonia.Platform;
  7. using Avalonia.UnitTests;
  8. using Moq;
  9. using Xunit;
  10. using System.Globalization;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class CalendarDatePickerTests : ScopedTestBase
  14. {
  15. private static bool CompareDates(DateTime first, DateTime second)
  16. {
  17. return first.Year == second.Year &&
  18. first.Month == second.Month &&
  19. first.Day == second.Day;
  20. }
  21. [Fact(Skip = "FIX ME ASAP")]
  22. public void SelectedDateChanged_Should_Fire_When_SelectedDate_Set()
  23. {
  24. using (UnitTestApplication.Start(Services))
  25. {
  26. bool handled = false;
  27. CalendarDatePicker datePicker = CreateControl();
  28. datePicker.SelectedDateChanged += (s,e) =>
  29. {
  30. handled = true;
  31. };
  32. DateTime value = new DateTime(2000, 10, 10);
  33. datePicker.SelectedDate = value;
  34. Threading.Dispatcher.UIThread.RunJobs();
  35. Assert.True(handled);
  36. }
  37. }
  38. [Fact]
  39. public void Setting_Selected_Date_To_Blackout_Date_Should_Throw()
  40. {
  41. using (UnitTestApplication.Start(Services))
  42. {
  43. CalendarDatePicker datePicker = CreateControl();
  44. datePicker.BlackoutDates.AddDatesInPast();
  45. DateTime goodValue = DateTime.Today.AddDays(1);
  46. datePicker.SelectedDate = goodValue;
  47. Assert.True(CompareDates(datePicker.SelectedDate.Value, goodValue));
  48. DateTime badValue = DateTime.Today.AddDays(-1);
  49. Assert.ThrowsAny<ArgumentOutOfRangeException>(
  50. () => datePicker.SelectedDate = badValue);
  51. }
  52. }
  53. [Fact]
  54. public void Adding_Blackout_Dates_Containing_Selected_Date_Should_Throw()
  55. {
  56. using (UnitTestApplication.Start(Services))
  57. {
  58. CalendarDatePicker datePicker = CreateControl();
  59. datePicker.SelectedDate = DateTime.Today.AddDays(5);
  60. Assert.ThrowsAny<ArgumentOutOfRangeException>(
  61. () => datePicker.BlackoutDates.Add(new CalendarDateRange(DateTime.Today, DateTime.Today.AddDays(10))));
  62. }
  63. }
  64. [Fact]
  65. public void Setting_Date_Manually_With_CustomDateFormatString_Should_Be_Accepted()
  66. {
  67. CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");
  68. using (UnitTestApplication.Start(Services))
  69. {
  70. CalendarDatePicker datePicker = CreateControl();
  71. datePicker.SelectedDateFormat = CalendarDatePickerFormat.Custom;
  72. datePicker.CustomDateFormatString = "dd.MM.yyyy";
  73. var tb = GetTextBox(datePicker);
  74. tb.Clear();
  75. RaiseTextEvent(tb, "17.10.2024");
  76. RaiseKeyEvent(tb, Key.Enter, KeyModifiers.None);
  77. Assert.Equal("17.10.2024", datePicker.Text);
  78. Assert.True(CompareDates(datePicker.SelectedDate.Value, new DateTime(2024, 10, 17)));
  79. tb.Clear();
  80. RaiseTextEvent(tb, "12.10.2024");
  81. RaiseKeyEvent(tb, Key.Enter, KeyModifiers.None);
  82. Assert.Equal("12.10.2024", datePicker.Text);
  83. Assert.True(CompareDates(datePicker.SelectedDate.Value, new DateTime(2024, 10, 12)));
  84. }
  85. }
  86. private static TestServices Services => TestServices.MockThreadingInterface.With(
  87. standardCursorFactory: Mock.Of<ICursorFactory>());
  88. private static CalendarDatePicker CreateControl()
  89. {
  90. var datePicker =
  91. new CalendarDatePicker
  92. {
  93. Template = CreateTemplate()
  94. };
  95. datePicker.ApplyTemplate();
  96. return datePicker;
  97. }
  98. private static IControlTemplate CreateTemplate()
  99. {
  100. return new FuncControlTemplate<CalendarDatePicker>((control, scope) =>
  101. {
  102. var textBox =
  103. new TextBox
  104. {
  105. Name = "PART_TextBox"
  106. }.RegisterInNameScope(scope);
  107. var button =
  108. new Button
  109. {
  110. Name = "PART_Button"
  111. }.RegisterInNameScope(scope);
  112. var calendar =
  113. new Calendar
  114. {
  115. Name = "PART_Calendar",
  116. [!Calendar.SelectedDateProperty] = control[!CalendarDatePicker.SelectedDateProperty],
  117. [!Calendar.DisplayDateProperty] = control[!CalendarDatePicker.DisplayDateProperty],
  118. [!Calendar.DisplayDateStartProperty] = control[!CalendarDatePicker.DisplayDateStartProperty],
  119. [!Calendar.DisplayDateEndProperty] = control[!CalendarDatePicker.DisplayDateEndProperty]
  120. }.RegisterInNameScope(scope);
  121. var popup =
  122. new Popup
  123. {
  124. Name = "PART_Popup"
  125. }.RegisterInNameScope(scope);
  126. var panel = new Panel();
  127. panel.Children.Add(textBox);
  128. panel.Children.Add(button);
  129. panel.Children.Add(popup);
  130. panel.Children.Add(calendar);
  131. return panel;
  132. });
  133. }
  134. private TextBox GetTextBox(CalendarDatePicker control)
  135. {
  136. return control.GetTemplateChildren()
  137. .OfType<TextBox>()
  138. .First();
  139. }
  140. private static void RaiseKeyEvent(TextBox textBox, Key key, KeyModifiers inputModifiers)
  141. {
  142. textBox.RaiseEvent(new KeyEventArgs
  143. {
  144. RoutedEvent = InputElement.KeyDownEvent,
  145. KeyModifiers = inputModifiers,
  146. Key = key
  147. });
  148. }
  149. private static void RaiseTextEvent(TextBox textBox, string text)
  150. {
  151. textBox.RaiseEvent(new TextInputEventArgs
  152. {
  153. RoutedEvent = InputElement.TextInputEvent,
  154. Text = text
  155. });
  156. }
  157. }
  158. }