ScrollSliderTests.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. namespace Terminal.Gui.ViewsTests;
  2. public class ScrollSliderTests
  3. {
  4. // Test for GetPositionFromSliderLocation to GetSliderLocationDimensionFromPosition
  5. [Theory]
  6. [InlineData (Orientation.Vertical, 26, 236, -1, 0)]
  7. [InlineData (Orientation.Vertical, 26, 236, 0, 0)]
  8. [InlineData (Orientation.Vertical, 26, 236, 5, 46)]
  9. [InlineData (Orientation.Vertical, 26, 236, 10, 91)]
  10. [InlineData (Orientation.Vertical, 26, 236, 15, 137)]
  11. [InlineData (Orientation.Vertical, 26, 236, 20, 182)]
  12. [InlineData (Orientation.Vertical, 26, 236, 26, 210)]
  13. [InlineData (Orientation.Vertical, 26, 236, 27, 210)]
  14. [InlineData (Orientation.Vertical, 37, 236, 2, 13)]
  15. [InlineData (Orientation.Vertical, 42, 236, 29, 164)]
  16. public void Test_Position_Location_Consistency (Orientation orientation, int scrollLength, int size, int location, int expectedPosition)
  17. {
  18. // Arrange
  19. Scroll host = new ()
  20. {
  21. Orientation = orientation,
  22. Width = orientation == Orientation.Vertical ? 1 : scrollLength,
  23. Height = orientation == Orientation.Vertical ? scrollLength : 1,
  24. Size = size
  25. };
  26. host.BeginInit ();
  27. host.EndInit ();
  28. // Act
  29. host.Position = host._slider.GetPositionFromSliderLocation (location);
  30. (int calculatedLocation, int calculatedDimension) = host._slider.GetSliderLocationDimensionFromPosition ();
  31. int calculatedPosition = host._slider.GetPositionFromSliderLocation (calculatedLocation);
  32. // Assert
  33. AssertLocation (scrollLength, location, calculatedLocation, calculatedDimension);
  34. Assert.Equal (host.Position, expectedPosition);
  35. Assert.Equal (calculatedPosition, expectedPosition);
  36. }
  37. // Randomized Test for more extensive testing
  38. [Theory]
  39. [InlineData (Orientation.Vertical, 26, 236, 5)]
  40. public void Test_Position_Location_Consistency_Random (Orientation orientation, int scrollLength, int size, int testCount)
  41. {
  42. var random = new Random ();
  43. Scroll host = new ()
  44. {
  45. Orientation = orientation,
  46. Width = orientation == Orientation.Vertical ? 1 : scrollLength,
  47. Height = orientation == Orientation.Vertical ? scrollLength : 1,
  48. Size = size
  49. };
  50. host.BeginInit ();
  51. host.EndInit ();
  52. // Number of random tests to run
  53. for (var i = 0; i < testCount; i++)
  54. {
  55. // Arrange
  56. int randomScrollLength = random.Next (0, 60); // Random content size length
  57. int randomLocation = random.Next (0, randomScrollLength); // Random location
  58. host.Width = host.Orientation == Orientation.Vertical ? 1 : randomScrollLength;
  59. host.Height = host.Orientation == Orientation.Vertical ? randomScrollLength : 1;
  60. // Slider may have changed content size
  61. host.LayoutSubviews ();
  62. // Act
  63. host.Position = host._slider.GetPositionFromSliderLocation (randomLocation);
  64. (int calculatedLocation, int calculatedDimension) = host._slider.GetSliderLocationDimensionFromPosition ();
  65. int calculatedPosition = host._slider.GetPositionFromSliderLocation (calculatedLocation);
  66. // Assert
  67. AssertLocation (randomScrollLength, randomLocation, calculatedLocation, calculatedDimension);
  68. Assert.Equal (host.Position, calculatedPosition);
  69. }
  70. }
  71. private static void AssertLocation (int scrollLength, int location, int calculatedLocation, int calculatedDimension)
  72. {
  73. if (location < 0)
  74. {
  75. Assert.Equal (0, calculatedLocation);
  76. }
  77. else if (location + calculatedDimension >= scrollLength)
  78. {
  79. Assert.Equal (scrollLength - calculatedDimension, calculatedLocation);
  80. }
  81. else
  82. {
  83. Assert.Equal (location, calculatedLocation);
  84. }
  85. }
  86. }