ScrollBarTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.Media;
  8. using Xunit;
  9. namespace Avalonia.Controls.UnitTests.Primitives
  10. {
  11. public class ScrollBarTests
  12. {
  13. [Fact]
  14. public void Setting_Value_Should_Update_Track_Value()
  15. {
  16. var target = new ScrollBar
  17. {
  18. Template = new FuncControlTemplate<ScrollBar>(Template),
  19. };
  20. target.ApplyTemplate();
  21. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  22. target.Value = 50;
  23. Assert.Equal(50, track.Value);
  24. }
  25. [Fact]
  26. public void Setting_Track_Value_Should_Update_Value()
  27. {
  28. var target = new ScrollBar
  29. {
  30. Template = new FuncControlTemplate<ScrollBar>(Template),
  31. };
  32. target.ApplyTemplate();
  33. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  34. track.Value = 50;
  35. Assert.Equal(50, target.Value);
  36. }
  37. [Fact]
  38. public void Setting_Track_Value_After_Setting_Value_Should_Update_Value()
  39. {
  40. var target = new ScrollBar
  41. {
  42. Template = new FuncControlTemplate<ScrollBar>(Template),
  43. };
  44. target.ApplyTemplate();
  45. var track = (Track)target.GetTemplateChildren().First(x => x.Name == "track");
  46. target.Value = 25;
  47. track.Value = 50;
  48. Assert.Equal(50, target.Value);
  49. }
  50. [Fact]
  51. public void ScrollBar_Can_AutoHide()
  52. {
  53. var target = new ScrollBar();
  54. target.Visibility = ScrollBarVisibility.Auto;
  55. target.ViewportSize = 1;
  56. target.Maximum = 0;
  57. Assert.False(target.IsVisible);
  58. }
  59. [Fact]
  60. public void ScrollBar_Should_Not_AutoHide_When_ViewportSize_Is_NaN()
  61. {
  62. var target = new ScrollBar();
  63. target.Visibility = ScrollBarVisibility.Auto;
  64. target.Minimum = 0;
  65. target.Maximum = 100;
  66. target.ViewportSize = double.NaN;
  67. Assert.True(target.IsVisible);
  68. }
  69. [Fact]
  70. public void ScrollBar_Should_Not_AutoHide_When_Visibility_Set_To_Visible()
  71. {
  72. var target = new ScrollBar();
  73. target.Visibility = ScrollBarVisibility.Visible;
  74. target.Minimum = 0;
  75. target.Maximum = 100;
  76. target.ViewportSize = 100;
  77. Assert.True(target.IsVisible);
  78. }
  79. [Fact]
  80. public void ScrollBar_Should_Hide_When_Visibility_Set_To_Hidden()
  81. {
  82. var target = new ScrollBar();
  83. target.Visibility = ScrollBarVisibility.Hidden;
  84. target.Minimum = 0;
  85. target.Maximum = 100;
  86. target.ViewportSize = 10;
  87. Assert.False(target.IsVisible);
  88. }
  89. private static Control Template(ScrollBar control)
  90. {
  91. return new Border
  92. {
  93. Child = new Track
  94. {
  95. Name = "track",
  96. [!Track.MinimumProperty] = control[!RangeBase.MinimumProperty],
  97. [!Track.MaximumProperty] = control[!RangeBase.MaximumProperty],
  98. [!!Track.ValueProperty] = control[!!RangeBase.ValueProperty],
  99. [!Track.ViewportSizeProperty] = control[!ScrollBar.ViewportSizeProperty],
  100. [!Track.OrientationProperty] = control[!ScrollBar.OrientationProperty],
  101. Thumb = new Thumb
  102. {
  103. Template = new FuncControlTemplate<Thumb>(ThumbTemplate),
  104. },
  105. },
  106. };
  107. }
  108. private static Control ThumbTemplate(Thumb control)
  109. {
  110. return new Border
  111. {
  112. Background = Brushes.Gray,
  113. };
  114. }
  115. }
  116. }