TimePickerTests.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. renderInterface: new MockPlatformRenderInterface());
  91. private static IControlTemplate CreateTemplate()
  92. {
  93. return new FuncControlTemplate((control, scope) =>
  94. {
  95. var layoutRoot = new Grid
  96. {
  97. Name = "LayoutRoot"
  98. }.RegisterInNameScope(scope);
  99. //Skip contentpresenter
  100. var flyoutButton = new Button
  101. {
  102. Name = "PART_FlyoutButton"
  103. }.RegisterInNameScope(scope);
  104. var contentGrid = new Grid
  105. {
  106. Name = "PART_FlyoutButtonContentGrid"
  107. }.RegisterInNameScope(scope);
  108. var firstPickerHost = new Border
  109. {
  110. Name = "PART_FirstPickerHost",
  111. Child = new TextBlock
  112. {
  113. Name = "PART_HourTextBlock"
  114. }.RegisterInNameScope(scope)
  115. }.RegisterInNameScope(scope);
  116. Grid.SetColumn(firstPickerHost, 0);
  117. var secondPickerHost = new Border
  118. {
  119. Name = "PART_SecondPickerHost",
  120. Child = new TextBlock
  121. {
  122. Name = "PART_MinuteTextBlock"
  123. }.RegisterInNameScope(scope)
  124. }.RegisterInNameScope(scope);
  125. Grid.SetColumn(secondPickerHost, 2);
  126. var thirdPickerHost = new Border
  127. {
  128. Name = "PART_ThirdPickerHost",
  129. Child = new TextBlock
  130. {
  131. Name = "PART_PeriodTextBlock"
  132. }.RegisterInNameScope(scope)
  133. }.RegisterInNameScope(scope);
  134. Grid.SetColumn(thirdPickerHost, 4);
  135. var firstSpacer = new Rectangle
  136. {
  137. Name = "PART_FirstColumnDivider"
  138. }.RegisterInNameScope(scope);
  139. Grid.SetColumn(firstSpacer, 1);
  140. var secondSpacer = new Rectangle
  141. {
  142. Name = "PART_SecondColumnDivider"
  143. }.RegisterInNameScope(scope);
  144. Grid.SetColumn(secondSpacer, 3);
  145. contentGrid.Children.AddRange(new Control[] { firstPickerHost, firstSpacer, secondPickerHost, secondSpacer, thirdPickerHost });
  146. flyoutButton.Content = contentGrid;
  147. layoutRoot.Children.Add(flyoutButton);
  148. return layoutRoot;
  149. });
  150. }
  151. }
  152. }