TimePickerTests.cs 6.3 KB

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