Really messed stuff up

This commit is contained in:
Tig
2024-11-13 13:19:18 -07:00
parent 7fae0c36b6
commit f922cbaed0
5 changed files with 354 additions and 121 deletions

View File

@@ -10,18 +10,18 @@ public class OrientationHelperTests
// Arrange
Mock<IOrientation> mockIOrientation = new Mock<IOrientation> ();
var orientationHelper = new OrientationHelper (mockIOrientation.Object);
var changingEventInvoked = false;
var changedEventInvoked = false;
var changingEventInvoked = 0;
var changedEventInvoked = 0;
orientationHelper.OrientationChanging += (sender, e) => { changingEventInvoked = true; };
orientationHelper.OrientationChanged += (sender, e) => { changedEventInvoked = true; };
orientationHelper.OrientationChanging += (sender, e) => { changingEventInvoked++; };
orientationHelper.OrientationChanged += (sender, e) => { changedEventInvoked++; };
// Act
orientationHelper.Orientation = Orientation.Vertical;
// Assert
Assert.True (changingEventInvoked, "OrientationChanging event was not invoked.");
Assert.True (changedEventInvoked, "OrientationChanged event was not invoked.");
Assert.Equal (1, changingEventInvoked);
Assert.Equal(1, changedEventInvoked);
}
[Fact]
@@ -29,15 +29,15 @@ public class OrientationHelperTests
{
// Arrange
Mock<IOrientation> mockIOrientation = new Mock<IOrientation> ();
var onChangingOverrideCalled = false;
var onChangedOverrideCalled = false;
var onChangingOverrideCalled = 0;
var onChangedOverrideCalled = 0;
mockIOrientation.Setup (x => x.OnOrientationChanging (It.IsAny<Orientation> (), It.IsAny<Orientation> ()))
.Callback (() => onChangingOverrideCalled = true)
.Callback (() => onChangingOverrideCalled++)
.Returns (false); // Ensure it doesn't cancel the change
mockIOrientation.Setup (x => x.OnOrientationChanged (It.IsAny<Orientation> ()))
.Callback (() => onChangedOverrideCalled = true);
.Callback (() => onChangedOverrideCalled++);
var orientationHelper = new OrientationHelper (mockIOrientation.Object);
@@ -45,8 +45,8 @@ public class OrientationHelperTests
orientationHelper.Orientation = Orientation.Vertical;
// Assert
Assert.True (onChangingOverrideCalled, "OnOrientationChanging override was not called.");
Assert.True (onChangedOverrideCalled, "OnOrientationChanged override was not called.");
Assert.Equal (1, onChangingOverrideCalled);
Assert.Equal (1, onChangedOverrideCalled);
}
[Fact]
@@ -56,18 +56,18 @@ public class OrientationHelperTests
Mock<IOrientation> mockIOrientation = new Mock<IOrientation> ();
var orientationHelper = new OrientationHelper (mockIOrientation.Object);
orientationHelper.Orientation = Orientation.Horizontal; // Set initial orientation
var changingEventInvoked = false;
var changedEventInvoked = false;
var changingEventInvoked = 0;
var changedEventInvoked = 0;
orientationHelper.OrientationChanging += (sender, e) => { changingEventInvoked = true; };
orientationHelper.OrientationChanged += (sender, e) => { changedEventInvoked = true; };
orientationHelper.OrientationChanging += (sender, e) => { changingEventInvoked++; };
orientationHelper.OrientationChanged += (sender, e) => { changedEventInvoked++; };
// Act
orientationHelper.Orientation = Orientation.Horizontal; // Set to the same value
// Assert
Assert.False (changingEventInvoked, "OrientationChanging event was invoked.");
Assert.False (changedEventInvoked, "OrientationChanged event was invoked.");
Assert.Equal (0, changingEventInvoked);
Assert.Equal (0, changedEventInvoked);
}
[Fact]

View File

@@ -52,7 +52,9 @@ public class ScrollBarTests
Assert.False (scrollBar.CanFocus);
Assert.Equal (Orientation.Vertical, scrollBar.Orientation);
Assert.Equal (0, scrollBar.Size);
Assert.Equal (0, scrollBar.ViewportDimension);
Assert.Equal (0, scrollBar.GetSliderPosition ());
Assert.Equal (0, scrollBar.ContentPosition);
Assert.True (scrollBar.AutoHide);
}
@@ -95,9 +97,8 @@ public class ScrollBarTests
var changingCount = 0;
var changedCount = 0;
var scrollBar = new ScrollBar { };
scrollBar.Layout ();
scrollBar.Size = scrollBar.Viewport.Height * 2;
scrollBar.Layout ();
scrollBar.Size = 4;
scrollBar.Frame = new Rectangle (0, 0, 1, 4); // Needs to be at least 4 for slider to move
scrollBar.ContentPositionChanging += (s, e) =>
{
@@ -686,7 +687,6 @@ public class ScrollBarTests
super.Add (scrollBar);
scrollBar.Size = contentSize;
scrollBar.Layout ();
scrollBar.ContentPosition = contentPosition;
super.BeginInit ();
@@ -794,52 +794,130 @@ public class ScrollBarTests
}
[Theory]
[InlineData (1, 10, 0, 0)]
[InlineData (1, 10, 5, 0)]
[InlineData (1, 10, 10, 1)]
[InlineData (1, 20, 0, 0)]
[InlineData (1, 20, 10, 0)]
[InlineData (1, 20, 20, 1)]
[InlineData (2, 10, 0, 0)]
[InlineData (2, 10, 5, 1)]
[InlineData (2, 10, 10, 2)]
[InlineData (2, 20, 0, 0)]
[InlineData (2, 20, 10, 1)]
[InlineData (2, 20, 20, 2)]
[InlineData (3, 10, 0, 0)]
[InlineData (3, 10, 5, 1)]
[InlineData (3, 10, 10, 3)]
[InlineData (3, 20, 0, 0)]
[InlineData (3, 20, 10, 1)]
[InlineData (3, 20, 20, 3)]
[InlineData (4, 10, 0, 0)]
[InlineData (4, 10, 5, 2)]
[InlineData (4, 10, 10, 4)]
[InlineData (4, 20, 0, 0)]
[InlineData (4, 20, 10, 2)]
[InlineData (4, 20, 20, 4)]
[InlineData (5, 10, 0, 0)]
[InlineData (5, 10, 5, 2)]
[InlineData (5, 10, 10, 5)]
[InlineData (5, 20, 0, 0)]
[InlineData (5, 20, 10, 2)]
[InlineData (5, 20, 20, 5)]
public void ScrollBar_CombinatorialTests (int width, int size, int contentPosition, int expectedSliderPosition)
[InlineData (-1, 10, 1)]
[InlineData (0, 10, 1)]
[InlineData (10, 15, 5)]
[InlineData (10, 5, 10)]
[InlineData (10, 3, 10)]
[InlineData (10, 2, 10)]
[InlineData (10, 1, 10)]
[InlineData (10, 0, 1)]
[InlineData (10, 10, 8)]
[InlineData (10, 20, 4)]
[InlineData (10, 100, 1)]
[InlineData (15, 10, 15)]
[InlineData (15, 0, 1)]
[InlineData (15, 1, 15)]
[InlineData (15, 2, 15)]
[InlineData (15, 3, 15)]
[InlineData (15, 5, 15)]
[InlineData (15, 14, 13)]
[InlineData (15, 15, 13)]
[InlineData (15, 16, 12)]
[InlineData (20, 10, 20)]
[InlineData (100, 10, 100)]
public void CalculateSliderSize_Width_Matches_ViewportDimension (int viewportDimension, int size, int expectedSliderSize)
{
// Arrange
var scrollBar = new ScrollBar
{
Width = width,
ViewportDimension = viewportDimension,
Size = size,
ContentPosition = contentPosition
Orientation = Orientation.Horizontal // Assuming horizontal for simplicity
};
scrollBar.Width = viewportDimension; // Changing orientation changes Width
scrollBar.BeginInit ();
scrollBar.EndInit ();
scrollBar.Layout ();
// Act
var sliderPosition = scrollBar.GetSliderPosition ();
var sliderSize = scrollBar.CalculateSliderSize ();
// Assert
Assert.Equal (expectedSliderPosition, sliderPosition);
Assert.Equal (expectedSliderSize, sliderSize);
}
// 012345678901
// ◄█░░░░░░░░░►
[Theory]
// ◄█►
[InlineData (3, 3, -1, 0)]
[InlineData (3, 3, 0, 0)]
[InlineData (3, 3, 1, 0)]
[InlineData (3, 3, 2, 0)]
// ◄██►
[InlineData (4, 2, 1, 0)]
[InlineData (4, 2, 2, 0)]
// 0123
// ---
// ◄█░►
[InlineData (4, 3, 0, 0)]
// ◄░█►
[InlineData (4, 3, 1, 1)]
// ◄░█►
[InlineData (4, 3, 2, 1)]
// 01234
// ----
// ◄█░►
[InlineData (4, 4, 0, 0)]
// ◄░█►
[InlineData (4, 4, 1, 1)]
// ◄░█►
[InlineData (4, 4, 2, 1)]
// 012345
// ◄███►
// -----
[InlineData (5, 5, 3, 0)]
[InlineData (5, 5, 4, 0)]
// 0123456
// ◄██░►
// ------
[InlineData (5, 6, 0, 0)]
[InlineData (5, 6, 1, 1)]
[InlineData (5, 6, 2, 1)]
// 01234567890
// ◄█░░░►
// ----------
[InlineData (5, 10, -1, 0)]
[InlineData (5, 10, 0, 0)]
// 01234567890
// ◄░█░░►
// --^-------
[InlineData (5, 10, 1, 2)]
[InlineData (5, 10, 2, 3)]
[InlineData (5, 10, 3, 3)]
[InlineData (5, 10, 4, 3)]
[InlineData (5, 10, 5, 3)]
[InlineData (5, 10, 6, 3)]
[InlineData (5, 10, 7, 3)]
[InlineData (5, 10, 8, 3)]
[InlineData (5, 10, 9, 3)]
[InlineData (5, 10, 10, 3)]
public void CalculateContentPosition_ComprehensiveTests (int viewportDimension, int size, int sliderPosition, int expectedContentPosition)
{
// Arrange
var scrollBar = new ScrollBar
{
ViewportDimension = viewportDimension,
Size = size,
Orientation = Orientation.Horizontal // Assuming horizontal for simplicity
};
scrollBar.Width = viewportDimension; // Changing orientation changes Width
scrollBar.Layout ();
// Act
var contentPosition = scrollBar.CalculateContentPosition (sliderPosition);
// Assert
Assert.Equal (expectedContentPosition, contentPosition);
}
}

View File

@@ -87,6 +87,90 @@ public class ScrollSliderTests (ITestOutputHelper output)
Assert.True (scrollSlider.Size <= 5);
}
[Theory]
[CombinatorialData]
public void Size_Clamps_To_ViewportDimensions ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (-1, 12, 1)] int sliderSize, Orientation orientation)
{
var scrollSlider = new ScrollSlider
{
Orientation = orientation,
ViewportDimension = dimension,
Size = sliderSize,
};
scrollSlider.Layout ();
Assert.True (scrollSlider.Size > 0);
Assert.True (scrollSlider.Size <= dimension);
}
[Theory]
[CombinatorialData]
public void ViewportDimensions_Clamps_0_To_Dimension ([CombinatorialRange (0, 10, 1)] int dimension, Orientation orientation)
{
var scrollSlider = new ScrollSlider
{
Orientation = orientation,
ViewportDimension = dimension,
};
Assert.InRange (scrollSlider.ViewportDimension, 1, 10);
View super = new ()
{
Id = "super",
Height = dimension,
Width = dimension,
};
scrollSlider = new ScrollSlider
{
Orientation = orientation,
};
super.Add (scrollSlider);
super.Layout ();
Assert.InRange (scrollSlider.ViewportDimension, 1, 10);
scrollSlider.ViewportDimension = dimension;
Assert.InRange (scrollSlider.ViewportDimension, 1, 10);
}
[Theory]
[CombinatorialData]
public void ClampPosition_Clamps_To_Viewport_Minus_Size ([CombinatorialRange (10, 10, 1)] int dimension, [CombinatorialRange (1, 5, 1)] int sliderSize, [CombinatorialRange (-2, 6, 2)] int sliderPosition, Orientation orientation)
{
var scrollSlider = new ScrollSlider
{
Orientation = orientation,
ViewportDimension = dimension,
Size = sliderSize,
};
int clampedPosition = scrollSlider.ClampPosition (sliderPosition);
Assert.InRange (clampedPosition, 0, dimension - sliderSize);
View super = new ()
{
Id = "super",
Height = dimension,
Width = dimension,
};
scrollSlider = new ScrollSlider
{
Orientation = orientation,
Size = sliderSize,
};
super.Add (scrollSlider);
super.Layout ();
clampedPosition = scrollSlider.ClampPosition (sliderPosition);
Assert.InRange (clampedPosition, 0, dimension - sliderSize);
}
[Theory]
[CombinatorialData]
public void Position_Clamps_To_SuperView_Viewport ([CombinatorialRange (0, 5, 1)] int sliderSize, [CombinatorialRange (-2, 6, 2)] int sliderPosition, Orientation orientation)
@@ -431,15 +515,12 @@ public class ScrollSliderTests (ITestOutputHelper output)
var scrollSlider = new ScrollSlider
{
Orientation = orientation,
Size = sliderSize,
Position = position,
};
Assert.Equal (sliderSize, scrollSlider.Size);
super.Add (scrollSlider);
scrollSlider.Size = sliderSize;
scrollSlider.Layout ();
scrollSlider.Position = position;
super.BeginInit ();
super.EndInit ();
super.Layout ();
super.Draw ();