TimePickerTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 TimePickerTests
  13. {
  14. [Fact]
  15. public void SelectedTimeChanged_Should_Fire_When_SelectedTime_Set()
  16. {
  17. using (UnitTestApplication.Start(Services))
  18. {
  19. bool handled = false;
  20. TimePicker timePicker = new TimePicker();
  21. timePicker.SelectedTimeChanged += (s, e) =>
  22. {
  23. handled = true;
  24. };
  25. TimeSpan value = TimeSpan.FromHours(10);
  26. timePicker.SelectedTime = value;
  27. Threading.Dispatcher.UIThread.RunJobs();
  28. Assert.True(handled);
  29. }
  30. }
  31. [Fact]
  32. public void Using_24HourClock_Should_Hide_Period()
  33. {
  34. using (UnitTestApplication.Start(Services))
  35. {
  36. TimePicker timePicker = new TimePicker()
  37. {
  38. ClockIdentifier = "12HourClock",
  39. Template = CreateTemplate()
  40. };
  41. timePicker.ApplyTemplate();
  42. var desc = timePicker.GetVisualDescendants();
  43. Assert.True(desc.Count() > 1);//Should be layoutroot grid & button
  44. Grid container = null;
  45. Assert.True(desc.ElementAt(1) is Button);
  46. container = (desc.ElementAt(1) as Button).Content as Grid;
  47. Assert.True(container != null);
  48. var periodTextHost = container.Children[4] as Border;
  49. Assert.True(periodTextHost != null);
  50. Assert.True(periodTextHost.IsVisible);
  51. timePicker.ClockIdentifier = "24HourClock";
  52. Assert.False(periodTextHost.IsVisible);
  53. }
  54. }
  55. [Fact]
  56. public void SelectedTime_null_Should_Use_Placeholders()
  57. {
  58. using (UnitTestApplication.Start(Services))
  59. {
  60. TimePicker timePicker = new TimePicker()
  61. {
  62. Template = CreateTemplate()
  63. };
  64. timePicker.ApplyTemplate();
  65. var desc = timePicker.GetVisualDescendants();
  66. Assert.True(desc.Count() > 1);//Should be layoutroot grid & button
  67. Grid container = null;
  68. Assert.True(desc.ElementAt(1) is Button);
  69. container = (desc.ElementAt(1) as Button).Content as Grid;
  70. Assert.True(container != null);
  71. var hourTextHost = container.Children[0] as Border;
  72. Assert.True(hourTextHost != null);
  73. var hourText = hourTextHost.Child as TextBlock;
  74. var minuteTextHost = container.Children[2] as Border;
  75. Assert.True(minuteTextHost != null);
  76. var minuteText = minuteTextHost.Child as TextBlock;
  77. TimeSpan ts = TimeSpan.FromHours(10);
  78. timePicker.SelectedTime = ts;
  79. Assert.False(hourText.Text == "hour");
  80. Assert.False(minuteText.Text == "minute");
  81. timePicker.SelectedTime = null;
  82. Assert.True(hourText.Text == "hour");
  83. Assert.True(minuteText.Text == "minute");
  84. }
  85. }
  86. private static TestServices Services => TestServices.MockThreadingInterface.With(
  87. standardCursorFactory: Mock.Of<IStandardCursorFactory>());
  88. private IControlTemplate CreateTemplate()
  89. {
  90. return new FuncControlTemplate((control, scope) =>
  91. {
  92. var layoutRoot = new Grid
  93. {
  94. Name = "LayoutRoot"
  95. }.RegisterInNameScope(scope);
  96. //Skip contentpresenter
  97. var flyoutButton = new Button
  98. {
  99. Name = "FlyoutButton"
  100. }.RegisterInNameScope(scope);
  101. var contentGrid = new Grid
  102. {
  103. Name = "FlyoutButtonContentGrid"
  104. }.RegisterInNameScope(scope);
  105. var firstPickerHost = new Border
  106. {
  107. Name = "FirstPickerHost",
  108. Child = new TextBlock
  109. {
  110. Name = "HourTextBlock"
  111. }.RegisterInNameScope(scope)
  112. }.RegisterInNameScope(scope);
  113. Grid.SetColumn(firstPickerHost, 0);
  114. var secondPickerHost = new Border
  115. {
  116. Name = "SecondPickerHost",
  117. Child = new TextBlock
  118. {
  119. Name = "MinuteTextBlock"
  120. }.RegisterInNameScope(scope)
  121. }.RegisterInNameScope(scope);
  122. Grid.SetColumn(secondPickerHost, 2);
  123. var thirdPickerHost = new Border
  124. {
  125. Name = "ThirdPickerHost",
  126. Child = new TextBlock
  127. {
  128. Name = "PeriodTextBlock"
  129. }.RegisterInNameScope(scope)
  130. }.RegisterInNameScope(scope);
  131. Grid.SetColumn(thirdPickerHost, 4);
  132. var firstSpacer = new Rectangle
  133. {
  134. Name = "FirstColumnDivider"
  135. }.RegisterInNameScope(scope);
  136. Grid.SetColumn(firstSpacer, 1);
  137. var secondSpacer = new Rectangle
  138. {
  139. Name = "SecondColumnDivider"
  140. }.RegisterInNameScope(scope);
  141. Grid.SetColumn(secondSpacer, 3);
  142. contentGrid.Children.AddRange(new IControl[] { firstPickerHost, firstSpacer, secondPickerHost, secondSpacer, thirdPickerHost });
  143. flyoutButton.Content = contentGrid;
  144. layoutRoot.Children.Add(flyoutButton);
  145. return layoutRoot;
  146. });
  147. }
  148. }
  149. }