CalendarDatePickerTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Templates;
  9. using Avalonia.Data;
  10. using Avalonia.Markup.Data;
  11. using Avalonia.Platform;
  12. using Avalonia.UnitTests;
  13. using Moq;
  14. using Xunit;
  15. namespace Avalonia.Controls.UnitTests
  16. {
  17. public class CalendarDatePickerTests
  18. {
  19. private static bool CompareDates(DateTime first, DateTime second)
  20. {
  21. return first.Year == second.Year &&
  22. first.Month == second.Month &&
  23. first.Day == second.Day;
  24. }
  25. [Fact(Skip = "FIX ME ASAP")]
  26. public void SelectedDateChanged_Should_Fire_When_SelectedDate_Set()
  27. {
  28. using (UnitTestApplication.Start(Services))
  29. {
  30. bool handled = false;
  31. CalendarDatePicker datePicker = CreateControl();
  32. datePicker.SelectedDateChanged += (s,e) =>
  33. {
  34. handled = true;
  35. };
  36. DateTime value = new DateTime(2000, 10, 10);
  37. datePicker.SelectedDate = value;
  38. Threading.Dispatcher.UIThread.RunJobs();
  39. Assert.True(handled);
  40. }
  41. }
  42. [Fact]
  43. public void Setting_Selected_Date_To_Blackout_Date_Should_Throw()
  44. {
  45. using (UnitTestApplication.Start(Services))
  46. {
  47. CalendarDatePicker datePicker = CreateControl();
  48. datePicker.BlackoutDates.AddDatesInPast();
  49. DateTime goodValue = DateTime.Today.AddDays(1);
  50. datePicker.SelectedDate = goodValue;
  51. Assert.True(CompareDates(datePicker.SelectedDate.Value, goodValue));
  52. DateTime badValue = DateTime.Today.AddDays(-1);
  53. Assert.ThrowsAny<ArgumentOutOfRangeException>(
  54. () => datePicker.SelectedDate = badValue);
  55. }
  56. }
  57. [Fact]
  58. public void Adding_Blackout_Dates_Containing_Selected_Date_Should_Throw()
  59. {
  60. using (UnitTestApplication.Start(Services))
  61. {
  62. CalendarDatePicker datePicker = CreateControl();
  63. datePicker.SelectedDate = DateTime.Today.AddDays(5);
  64. Assert.ThrowsAny<ArgumentOutOfRangeException>(
  65. () => datePicker.BlackoutDates.Add(new CalendarDateRange(DateTime.Today, DateTime.Today.AddDays(10))));
  66. }
  67. }
  68. private static TestServices Services => TestServices.MockThreadingInterface.With(
  69. standardCursorFactory: Mock.Of<ICursorFactory>());
  70. private CalendarDatePicker CreateControl()
  71. {
  72. var datePicker =
  73. new CalendarDatePicker
  74. {
  75. Template = CreateTemplate()
  76. };
  77. datePicker.ApplyTemplate();
  78. return datePicker;
  79. }
  80. private IControlTemplate CreateTemplate()
  81. {
  82. return new FuncControlTemplate<CalendarDatePicker>((control, scope) =>
  83. {
  84. var textBox =
  85. new TextBox
  86. {
  87. Name = "PART_TextBox"
  88. }.RegisterInNameScope(scope);
  89. var button =
  90. new Button
  91. {
  92. Name = "PART_Button"
  93. }.RegisterInNameScope(scope);
  94. var calendar =
  95. new Calendar
  96. {
  97. Name = "PART_Calendar",
  98. [!Calendar.SelectedDateProperty] = control[!CalendarDatePicker.SelectedDateProperty],
  99. [!Calendar.DisplayDateProperty] = control[!CalendarDatePicker.DisplayDateProperty],
  100. [!Calendar.DisplayDateStartProperty] = control[!CalendarDatePicker.DisplayDateStartProperty],
  101. [!Calendar.DisplayDateEndProperty] = control[!CalendarDatePicker.DisplayDateEndProperty]
  102. }.RegisterInNameScope(scope);
  103. var popup =
  104. new Popup
  105. {
  106. Name = "PART_Popup"
  107. }.RegisterInNameScope(scope);
  108. var panel = new Panel();
  109. panel.Children.Add(textBox);
  110. panel.Children.Add(button);
  111. panel.Children.Add(popup);
  112. panel.Children.Add(calendar);
  113. return panel;
  114. });
  115. }
  116. }
  117. }