TimePickerTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. fontManagerImpl: new MockFontManagerImpl(),
  88. standardCursorFactory: Mock.Of<ICursorFactory>(),
  89. textShaperImpl: new MockTextShaperImpl());
  90. private IControlTemplate CreateTemplate()
  91. {
  92. return new FuncControlTemplate((control, scope) =>
  93. {
  94. var layoutRoot = new Grid
  95. {
  96. Name = "LayoutRoot"
  97. }.RegisterInNameScope(scope);
  98. //Skip contentpresenter
  99. var flyoutButton = new Button
  100. {
  101. Name = "FlyoutButton"
  102. }.RegisterInNameScope(scope);
  103. var contentGrid = new Grid
  104. {
  105. Name = "FlyoutButtonContentGrid"
  106. }.RegisterInNameScope(scope);
  107. var firstPickerHost = new Border
  108. {
  109. Name = "FirstPickerHost",
  110. Child = new TextBlock
  111. {
  112. Name = "HourTextBlock"
  113. }.RegisterInNameScope(scope)
  114. }.RegisterInNameScope(scope);
  115. Grid.SetColumn(firstPickerHost, 0);
  116. var secondPickerHost = new Border
  117. {
  118. Name = "SecondPickerHost",
  119. Child = new TextBlock
  120. {
  121. Name = "MinuteTextBlock"
  122. }.RegisterInNameScope(scope)
  123. }.RegisterInNameScope(scope);
  124. Grid.SetColumn(secondPickerHost, 2);
  125. var thirdPickerHost = new Border
  126. {
  127. Name = "ThirdPickerHost",
  128. Child = new TextBlock
  129. {
  130. Name = "PeriodTextBlock"
  131. }.RegisterInNameScope(scope)
  132. }.RegisterInNameScope(scope);
  133. Grid.SetColumn(thirdPickerHost, 4);
  134. var firstSpacer = new Rectangle
  135. {
  136. Name = "FirstColumnDivider"
  137. }.RegisterInNameScope(scope);
  138. Grid.SetColumn(firstSpacer, 1);
  139. var secondSpacer = new Rectangle
  140. {
  141. Name = "SecondColumnDivider"
  142. }.RegisterInNameScope(scope);
  143. Grid.SetColumn(secondSpacer, 3);
  144. contentGrid.Children.AddRange(new IControl[] { firstPickerHost, firstSpacer, secondPickerHost, secondSpacer, thirdPickerHost });
  145. flyoutButton.Content = contentGrid;
  146. layoutRoot.Children.Add(flyoutButton);
  147. return layoutRoot;
  148. });
  149. }
  150. }
  151. }