Skip to content

Commit

Permalink
Merge pull request #11 from mikeclayton/feature/#10-fix-wrong-jump-lo…
Browse files Browse the repository at this point in the history
…cation

Feature/#10 fix wrong jump location
  • Loading branch information
mikeclayton authored Mar 10, 2023
2 parents 35793dc + 8cc323b commit 180e35b
Show file tree
Hide file tree
Showing 14 changed files with 656 additions and 783 deletions.
137 changes: 137 additions & 0 deletions src/FancyMouse.Tests/Drawing/RectangleInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
using System.Drawing;
using FancyMouse.Drawing.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.Helpers.Tests;

[TestClass]
public static class RectangleInfoTests
{
[TestClass]
public class CenterTests
{
public class TestCase
{
public TestCase(RectangleInfo rectangle, PointInfo point, RectangleInfo expectedResult)
{
this.Rectangle = rectangle;
this.Point = point;
this.ExpectedResult = expectedResult;
}

public RectangleInfo Rectangle { get; set; }

public PointInfo Point { get; set; }

public RectangleInfo ExpectedResult { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
// zero-sized
yield return new[] { new TestCase(new(0, 0, 0, 0), new(0, 0), new(0, 0, 0, 0)), };

// zero-origin
yield return new[] { new TestCase(new(0, 0, 200, 200), new(100, 100), new(0, 0, 200, 200)), };
yield return new[] { new TestCase(new(0, 0, 200, 200), new(500, 500), new(400, 400, 200, 200)), };
yield return new[] { new TestCase(new(0, 0, 800, 600), new(1000, 1000), new(600, 700, 800, 600)), };

// non-zero origin
yield return new[] { new TestCase(new(1000, 2000, 200, 200), new(100, 100), new(0, 0, 200, 200)), };
yield return new[] { new TestCase(new(1000, 2000, 200, 200), new(500, 500), new(400, 400, 200, 200)), };
yield return new[] { new TestCase(new(1000, 2000, 800, 600), new(1000, 1000), new(600, 700, 800, 600)), };

// negative result
yield return new[] { new TestCase(new(0, 0, 1000, 1200), new(300, 300), new(-200, -300, 1000, 1200)), };
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
var actual = data.Rectangle.Center(data.Point);
var expected = data.ExpectedResult;
Assert.AreEqual(expected.X, actual.X);
Assert.AreEqual(expected.Y, actual.Y);
Assert.AreEqual(expected.Width, actual.Width);
Assert.AreEqual(expected.Height, actual.Height);
}
}

[TestClass]
public class ClampTests
{
public class TestCase
{
public TestCase(RectangleInfo inner, RectangleInfo outer, RectangleInfo expectedResult)
{
this.Inner = inner;
this.Outer = outer;
this.ExpectedResult = expectedResult;
}

public RectangleInfo Inner { get; set; }

public RectangleInfo Outer { get; set; }

public RectangleInfo ExpectedResult { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
// already inside - obj fills bounds exactly
yield return new[]
{
new TestCase(new(0, 0, 100, 100), new(0, 0, 100, 100), new(0, 0, 100, 100)),
};

// already inside - obj exactly in each corner
yield return new[]
{
new TestCase(new(0, 0, 100, 100), new(0, 0, 200, 200), new(0, 0, 100, 100)),
};
yield return new[]
{
new TestCase(new(100, 0, 100, 100), new(0, 0, 200, 200), new(100, 0, 100, 100)),
};
yield return new[]
{
new TestCase(new(0, 100, 100, 100), new(0, 0, 200, 200), new(0, 100, 100, 100)),
};
yield return new[]
{
new TestCase(new(100, 100, 100, 100), new(0, 0, 200, 200), new(100, 100, 100, 100)),
};

// move inside - obj outside each corner
yield return new[]
{
new TestCase(new(-50, -50, 100, 100), new(0, 0, 200, 200), new(0, 0, 100, 100)),
};
yield return new[]
{
new TestCase(new(250, -50, 100, 100), new(0, 0, 200, 200), new(100, 0, 100, 100)),
};
yield return new[]
{
new TestCase(new(-50, 250, 100, 100), new(0, 0, 200, 200), new(0, 100, 100, 100)),
};
yield return new[]
{
new TestCase(new(150, 150, 100, 100), new(0, 0, 200, 200), new(100, 100, 100, 100)),
};
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
var actual = data.Inner.Clamp(data.Outer);
var expected = data.ExpectedResult;
Assert.AreEqual(expected.X, actual.X);
Assert.AreEqual(expected.Y, actual.Y);
Assert.AreEqual(expected.Width, actual.Width);
Assert.AreEqual(expected.Height, actual.Height);
}
}
}
53 changes: 53 additions & 0 deletions src/FancyMouse.Tests/Drawing/SizeInfoTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using FancyMouse.Drawing.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.Tests.Drawing;

public sealed class SizeInfoTests
{
[TestClass]
public class ScaleToFitRatioTests
{
public class TestCase
{
public TestCase(SizeInfo obj, SizeInfo bounds, decimal expectedResult)
{
this.Obj = obj;
this.Bounds = bounds;
this.ExpectedResult = expectedResult;
}

public SizeInfo Obj { get; set; }

public SizeInfo Bounds { get; set; }

public decimal ExpectedResult { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
// identity tests
yield return new[] { new TestCase(new(512, 384), new(512, 384), 1), };
yield return new[] { new TestCase(new(1024, 768), new(1024, 768), 1), };

// general tests
yield return new[] { new TestCase(new(512, 384), new(2048, 1536), 4), };
yield return new[] { new TestCase(new(2048, 1536), new(1024, 768), 0.5M), };

// scale to fit width
yield return new[] { new TestCase(new(512, 384), new(2048, 3072), 4), };

// scale to fit height
yield return new[] { new TestCase(new(512, 384), new(4096, 1536), 4), };
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
var actual = data.Obj.ScaleToFitRatio(data.Bounds);
var expected = data.ExpectedResult;
Assert.AreEqual(expected, actual);
}
}
}
70 changes: 70 additions & 0 deletions src/FancyMouse.Tests/Helpers/DrawingHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Drawing;
using FancyMouse.Drawing;
using FancyMouse.Drawing.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.Helpers.Tests;

[TestClass]
public static class DrawingHelperTests
{
[TestClass]
public class GetJumpLocationTests
{
public class TestCase
{
public TestCase(PointInfo previewLocation, SizeInfo previewSize, RectangleInfo desktopBounds, PointInfo expectedResult)
{
this.PreviewLocation = previewLocation;
this.PreviewSize = previewSize;
this.DesktopBounds = desktopBounds;
this.ExpectedResult = expectedResult;
}

public PointInfo PreviewLocation { get; set; }

public SizeInfo PreviewSize { get; set; }

public RectangleInfo DesktopBounds { get; set; }

public PointInfo ExpectedResult { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
// corners and midpoint with a zero origin
yield return new[] { new TestCase(new(0, 0), new(160, 120), new(0, 0, 1600, 1200), new(0, 0)) };
yield return new[] { new TestCase(new(160, 0), new(160, 120), new(0, 0, 1600, 1200), new(1600, 0)) };
yield return new[] { new TestCase(new(0, 120), new(160, 120), new(0, 0, 1600, 1200), new(0, 1200)) };
yield return new[] { new TestCase(new(160, 120), new(160, 120), new(0, 0, 1600, 1200), new(1600, 1200)) };
yield return new[] { new TestCase(new(80, 60), new(160, 120), new(0, 0, 1600, 1200), new(800, 600)) };

// corners and midpoint with a positive origin
yield return new[] { new TestCase(new(0, 0), new(160, 120), new(1000, 1000, 1600, 1200), new(1000, 1000)) };
yield return new[] { new TestCase(new(160, 0), new(160, 120), new(1000, 1000, 1600, 1200), new(2600, 1000)) };
yield return new[] { new TestCase(new(0, 120), new(160, 120), new(1000, 1000, 1600, 1200), new(1000, 2200)) };
yield return new[] { new TestCase(new(160, 120), new(160, 120), new(1000, 1000, 1600, 1200), new(2600, 2200)) };
yield return new[] { new TestCase(new(80, 60), new(160, 120), new(1000, 1000, 1600, 1200), new(1800, 1600)) };

// corners and midpoint with a negative origin
yield return new[] { new TestCase(new(0, 0), new(160, 120), new(-1000, -1000, 1600, 1200), new(-1000, -1000)) };
yield return new[] { new TestCase(new(160, 0), new(160, 120), new(-1000, -1000, 1600, 1200), new(600, -1000)) };
yield return new[] { new TestCase(new(0, 120), new(160, 120), new(-1000, -1000, 1600, 1200), new(-1000, 200)) };
yield return new[] { new TestCase(new(160, 120), new(160, 120), new(-1000, -1000, 1600, 1200), new(600, 200)) };
yield return new[] { new TestCase(new(80, 60), new(160, 120), new(-1000, -1000, 1600, 1200), new(-200, -400)) };
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
var actual = DrawingHelper.GetJumpLocation(
data.PreviewLocation,
data.PreviewSize,
data.DesktopBounds);
var expected = data.ExpectedResult;
Assert.AreEqual(expected.X, actual.X);
Assert.AreEqual(expected.Y, actual.Y);
}
}
}
Loading

0 comments on commit 180e35b

Please sign in to comment.