ScrollBarTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(track.Value, 50);
  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(target.Value, 50);
  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(target.Value, 50);
  49. }
  50. [Fact]
  51. public void ScrollBar_Can_AutoHide()
  52. {
  53. var target = new ScrollBar();
  54. target.Visibility = ScrollBarVisibility.Auto;
  55. target.Minimum = 0;
  56. target.Maximum = 100;
  57. target.ViewportSize = 100;
  58. Assert.False(target.IsVisible);
  59. }
  60. [Fact]
  61. public void ScrollBar_Should_Not_AutoHide_When_ViewportSize_Is_NaN()
  62. {
  63. var target = new ScrollBar();
  64. target.Visibility = ScrollBarVisibility.Auto;
  65. target.Minimum = 0;
  66. target.Maximum = 100;
  67. target.ViewportSize = double.NaN;
  68. Assert.True(target.IsVisible);
  69. }
  70. [Fact]
  71. public void ScrollBar_Should_Not_AutoHide_When_Visibility_Set_To_Visible()
  72. {
  73. var target = new ScrollBar();
  74. target.Visibility = ScrollBarVisibility.Visible;
  75. target.Minimum = 0;
  76. target.Maximum = 100;
  77. target.ViewportSize = 100;
  78. Assert.True(target.IsVisible);
  79. }
  80. [Fact]
  81. public void ScrollBar_Should_Hide_When_Visibility_Set_To_Hidden()
  82. {
  83. var target = new ScrollBar();
  84. target.Visibility = ScrollBarVisibility.Hidden;
  85. target.Minimum = 0;
  86. target.Maximum = 100;
  87. target.ViewportSize = 10;
  88. Assert.False(target.IsVisible);
  89. }
  90. private static Control Template(ScrollBar control)
  91. {
  92. return new Border
  93. {
  94. Child = new Track
  95. {
  96. Name = "track",
  97. [!Track.MinimumProperty] = control[!RangeBase.MinimumProperty],
  98. [!Track.MaximumProperty] = control[!RangeBase.MaximumProperty],
  99. [!!Track.ValueProperty] = control[!!RangeBase.ValueProperty],
  100. [!Track.ViewportSizeProperty] = control[!ScrollBar.ViewportSizeProperty],
  101. [!Track.OrientationProperty] = control[!ScrollBar.OrientationProperty],
  102. Thumb = new Thumb
  103. {
  104. Template = new FuncControlTemplate<Thumb>(ThumbTemplate),
  105. },
  106. },
  107. };
  108. }
  109. private static Control ThumbTemplate(Thumb control)
  110. {
  111. return new Border
  112. {
  113. Background = Brushes.Gray,
  114. };
  115. }
  116. }
  117. }