DatePickerTests.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Linq;
  3. using Avalonia.Controls.Shapes;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Platform;
  6. using Avalonia.UnitTests;
  7. using Avalonia.VisualTree;
  8. using Moq;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class DatePickerTests
  13. {
  14. [Fact]
  15. public void SelectedDateChanged_Should_Fire_When_SelectedDate_Set()
  16. {
  17. using (UnitTestApplication.Start(Services))
  18. {
  19. bool handled = false;
  20. DatePicker datePicker = new DatePicker();
  21. datePicker.SelectedDateChanged += (s, e) =>
  22. {
  23. handled = true;
  24. };
  25. DateTimeOffset value = new DateTimeOffset(2000, 10, 10, 0, 0, 0, TimeSpan.Zero);
  26. datePicker.SelectedDate = value;
  27. Threading.Dispatcher.UIThread.RunJobs();
  28. Assert.True(handled);
  29. }
  30. }
  31. [Fact]
  32. public void DayVisible_False_Should_Hide_Day()
  33. {
  34. using (UnitTestApplication.Start(Services))
  35. {
  36. DatePicker datePicker = new DatePicker
  37. {
  38. Template = CreateTemplate(),
  39. DayVisible = false
  40. };
  41. datePicker.ApplyTemplate();
  42. Threading.Dispatcher.UIThread.RunJobs();
  43. var desc = datePicker.GetVisualDescendants();
  44. Assert.True(desc.Count() > 1);//Should be layoutroot grid & button
  45. TextBlock dayText = null;
  46. Grid container = null;
  47. Assert.True(desc.ElementAt(1) is Button);
  48. container = (desc.ElementAt(1) as Button).Content as Grid;
  49. Assert.True(container != null);
  50. for(int i = 0; i < container.Children.Count; i++)
  51. {
  52. if(container.Children[i] is TextBlock tb && tb.Name == "DayText")
  53. {
  54. dayText = tb;
  55. break;
  56. }
  57. }
  58. Assert.True(dayText != null);
  59. Assert.True(!dayText.IsVisible);
  60. Assert.True(container.ColumnDefinitions.Count == 3);
  61. }
  62. }
  63. [Fact]
  64. public void MonthVisible_False_Should_Hide_Month()
  65. {
  66. using (UnitTestApplication.Start(Services))
  67. {
  68. DatePicker datePicker = new DatePicker
  69. {
  70. Template = CreateTemplate(),
  71. MonthVisible = false
  72. };
  73. datePicker.ApplyTemplate();
  74. Threading.Dispatcher.UIThread.RunJobs();
  75. var desc = datePicker.GetVisualDescendants();
  76. Assert.True(desc.Count() > 1);//Should be layoutroot grid & button
  77. TextBlock monthText = null;
  78. Grid container = null;
  79. Assert.True(desc.ElementAt(1) is Button);
  80. container = (desc.ElementAt(1) as Button).Content as Grid;
  81. Assert.True(container != null);
  82. for (int i = 0; i < container.Children.Count; i++)
  83. {
  84. if (container.Children[i] is TextBlock tb && tb.Name == "MonthText")
  85. {
  86. monthText = tb;
  87. break;
  88. }
  89. }
  90. Assert.True(monthText != null);
  91. Assert.True(!monthText.IsVisible);
  92. Assert.True(container.ColumnDefinitions.Count == 3);
  93. }
  94. }
  95. [Fact]
  96. public void YearVisible_False_Should_Hide_Year()
  97. {
  98. using (UnitTestApplication.Start(Services))
  99. {
  100. DatePicker datePicker = new DatePicker
  101. {
  102. Template = CreateTemplate(),
  103. YearVisible = false
  104. };
  105. datePicker.ApplyTemplate();
  106. Threading.Dispatcher.UIThread.RunJobs();
  107. var desc = datePicker.GetVisualDescendants();
  108. Assert.True(desc.Count() > 1);//Should be layoutroot grid & button
  109. TextBlock yearText = null;
  110. Grid container = null;
  111. Assert.True(desc.ElementAt(1) is Button);
  112. container = (desc.ElementAt(1) as Button).Content as Grid;
  113. Assert.True(container != null);
  114. for (int i = 0; i < container.Children.Count; i++)
  115. {
  116. if (container.Children[i] is TextBlock tb && tb.Name == "YearText")
  117. {
  118. yearText = tb;
  119. break;
  120. }
  121. }
  122. Assert.True(yearText != null);
  123. Assert.True(!yearText.IsVisible);
  124. Assert.True(container.ColumnDefinitions.Count == 3);
  125. }
  126. }
  127. [Fact]
  128. public void SelectedDate_null_Should_Use_Placeholders()
  129. {
  130. using (UnitTestApplication.Start(Services))
  131. {
  132. DatePicker datePicker = new DatePicker
  133. {
  134. Template = CreateTemplate(),
  135. YearVisible = false
  136. };
  137. datePicker.ApplyTemplate();
  138. Threading.Dispatcher.UIThread.RunJobs();
  139. var desc = datePicker.GetVisualDescendants();
  140. Assert.True(desc.Count() > 1);//Should be layoutroot grid & button
  141. TextBlock yearText = null;
  142. TextBlock monthText = null;
  143. TextBlock dayText = null;
  144. Grid container = null;
  145. Assert.True(desc.ElementAt(1) is Button);
  146. container = (desc.ElementAt(1) as Button).Content as Grid;
  147. Assert.True(container != null);
  148. for (int i = 0; i < container.Children.Count; i++)
  149. {
  150. if (container.Children[i] is TextBlock tb && tb.Name == "YearText")
  151. {
  152. yearText = tb;
  153. }
  154. else if (container.Children[i] is TextBlock tb1 && tb1.Name == "MonthText")
  155. {
  156. monthText = tb1;
  157. }
  158. else if (container.Children[i] is TextBlock tb2 && tb2.Name == "DayText")
  159. {
  160. dayText = tb2;
  161. }
  162. }
  163. DateTimeOffset value = new DateTimeOffset(2000, 10, 10, 0, 0, 0, TimeSpan.Zero);
  164. datePicker.SelectedDate = value;
  165. Assert.False(dayText.Text == "day");
  166. Assert.False(monthText.Text == "month");
  167. Assert.False(yearText.Text == "year");
  168. Assert.False(datePicker.Classes.Contains(":hasnodate"));
  169. datePicker.SelectedDate = null;
  170. Assert.True(dayText.Text == "day");
  171. Assert.True(monthText.Text == "month");
  172. Assert.True(yearText.Text == "year");
  173. Assert.True(datePicker.Classes.Contains(":hasnodate"));
  174. }
  175. }
  176. private static TestServices Services => TestServices.MockThreadingInterface.With(
  177. standardCursorFactory: Mock.Of<IStandardCursorFactory>());
  178. private IControlTemplate CreateTemplate()
  179. {
  180. return new FuncControlTemplate((control, scope) =>
  181. {
  182. var layoutRoot = new Grid
  183. {
  184. Name = "LayoutRoot"
  185. }.RegisterInNameScope(scope);
  186. //Skip contentpresenter
  187. var flyoutButton = new Button
  188. {
  189. Name = "FlyoutButton"
  190. }.RegisterInNameScope(scope);
  191. var contentGrid = new Grid
  192. {
  193. Name = "ButtonContentGrid"
  194. }.RegisterInNameScope(scope);
  195. var dayText = new TextBlock
  196. {
  197. Name = "DayText"
  198. }.RegisterInNameScope(scope);
  199. var monthText = new TextBlock
  200. {
  201. Name = "MonthText"
  202. }.RegisterInNameScope(scope);
  203. var yearText = new TextBlock
  204. {
  205. Name = "YearText"
  206. }.RegisterInNameScope(scope);
  207. var firstSpacer = new Rectangle
  208. {
  209. Name = "FirstSpacer"
  210. }.RegisterInNameScope(scope);
  211. var secondSpacer = new Rectangle
  212. {
  213. Name = "SecondSpacer"
  214. }.RegisterInNameScope(scope);
  215. contentGrid.Children.AddRange(new IControl[] { dayText, monthText, yearText, firstSpacer, secondSpacer });
  216. flyoutButton.Content = contentGrid;
  217. layoutRoot.Children.Add(flyoutButton);
  218. return layoutRoot;
  219. });
  220. }
  221. }
  222. }