DatePickerTests.cs 4.1 KB

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