ScrollBarTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Linq;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Input;
  8. using Avalonia.Media;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests.Primitives
  11. {
  12. public class ScrollBarTests
  13. {
  14. [Fact]
  15. public void Setting_Value_Should_Update_Track_Value()
  16. {
  17. var target = new ScrollBar
  18. {
  19. Template = new FuncControlTemplate<ScrollBar>(Template),
  20. };
  21. target.ApplyTemplate();
  22. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  23. target.Value = 50;
  24. Assert.Equal(50, track.Value);
  25. }
  26. [Fact]
  27. public void Setting_Track_Value_Should_Update_Value()
  28. {
  29. var target = new ScrollBar
  30. {
  31. Template = new FuncControlTemplate<ScrollBar>(Template),
  32. };
  33. target.ApplyTemplate();
  34. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  35. track.Value = 50;
  36. Assert.Equal(50, target.Value);
  37. }
  38. [Fact]
  39. public void Setting_Track_Value_After_Setting_Value_Should_Update_Value()
  40. {
  41. var target = new ScrollBar
  42. {
  43. Template = new FuncControlTemplate<ScrollBar>(Template),
  44. };
  45. target.ApplyTemplate();
  46. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  47. target.Value = 25;
  48. track.Value = 50;
  49. Assert.Equal(50, target.Value);
  50. }
  51. [Fact]
  52. public void Thumb_DragDelta_Event_Should_Raise_Scroll_Event()
  53. {
  54. var target = new ScrollBar
  55. {
  56. Template = new FuncControlTemplate<ScrollBar>(Template),
  57. };
  58. target.ApplyTemplate();
  59. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  60. var raisedEvent = Assert.Raises<ScrollEventArgs>(
  61. handler => target.Scroll += handler,
  62. handler => target.Scroll -= handler,
  63. () =>
  64. {
  65. var ev = new VectorEventArgs
  66. {
  67. RoutedEvent = Thumb.DragDeltaEvent,
  68. Vector = new Vector(0, 0)
  69. };
  70. track.Thumb.RaiseEvent(ev);
  71. });
  72. Assert.Equal(ScrollEventType.ThumbTrack, raisedEvent.Arguments.ScrollEventType);
  73. }
  74. [Fact]
  75. public void Thumb_DragComplete_Event_Should_Raise_Scroll_Event()
  76. {
  77. var target = new ScrollBar
  78. {
  79. Template = new FuncControlTemplate<ScrollBar>(Template),
  80. };
  81. target.ApplyTemplate();
  82. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  83. var raisedEvent = Assert.Raises<ScrollEventArgs>(
  84. handler => target.Scroll += handler,
  85. handler => target.Scroll -= handler,
  86. () =>
  87. {
  88. var ev = new VectorEventArgs
  89. {
  90. RoutedEvent = Thumb.DragCompletedEvent,
  91. Vector = new Vector(0, 0)
  92. };
  93. track.Thumb.RaiseEvent(ev);
  94. });
  95. Assert.Equal(ScrollEventType.EndScroll, raisedEvent.Arguments.ScrollEventType);
  96. }
  97. [Fact]
  98. public void ScrollBar_Can_AutoHide()
  99. {
  100. var target = new ScrollBar();
  101. target.Visibility = ScrollBarVisibility.Auto;
  102. target.ViewportSize = 1;
  103. target.Maximum = 0;
  104. Assert.False(target.IsVisible);
  105. }
  106. [Fact]
  107. public void ScrollBar_Should_Not_AutoHide_When_ViewportSize_Is_NaN()
  108. {
  109. var target = new ScrollBar();
  110. target.Visibility = ScrollBarVisibility.Auto;
  111. target.Minimum = 0;
  112. target.Maximum = 100;
  113. target.ViewportSize = double.NaN;
  114. Assert.True(target.IsVisible);
  115. }
  116. [Fact]
  117. public void ScrollBar_Should_Not_AutoHide_When_Visibility_Set_To_Visible()
  118. {
  119. var target = new ScrollBar();
  120. target.Visibility = ScrollBarVisibility.Visible;
  121. target.Minimum = 0;
  122. target.Maximum = 100;
  123. target.ViewportSize = 100;
  124. Assert.True(target.IsVisible);
  125. }
  126. [Fact]
  127. public void ScrollBar_Should_Hide_When_Visibility_Set_To_Hidden()
  128. {
  129. var target = new ScrollBar();
  130. target.Visibility = ScrollBarVisibility.Hidden;
  131. target.Minimum = 0;
  132. target.Maximum = 100;
  133. target.ViewportSize = 10;
  134. Assert.False(target.IsVisible);
  135. }
  136. private static Control Template(ScrollBar control)
  137. {
  138. return new Border
  139. {
  140. Child = new Track
  141. {
  142. Name = "track",
  143. [!Track.MinimumProperty] = control[!RangeBase.MinimumProperty],
  144. [!Track.MaximumProperty] = control[!RangeBase.MaximumProperty],
  145. [!!Track.ValueProperty] = control[!!RangeBase.ValueProperty],
  146. [!Track.ViewportSizeProperty] = control[!ScrollBar.ViewportSizeProperty],
  147. [!Track.OrientationProperty] = control[!ScrollBar.OrientationProperty],
  148. Thumb = new Thumb
  149. {
  150. Template = new FuncControlTemplate<Thumb>(ThumbTemplate),
  151. },
  152. },
  153. };
  154. }
  155. private static Control ThumbTemplate(Thumb control)
  156. {
  157. return new Border
  158. {
  159. Background = Brushes.Gray,
  160. };
  161. }
  162. }
  163. }