Skip to content

Commit

Permalink
#19 - fixed happy-path texting
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Jul 16, 2023
1 parent d95f19a commit 144a037
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ obj

releases

*.csproj.user
*.user

wiki/anim
162 changes: 92 additions & 70 deletions src/FancyMouse.UnitTests/Helpers/LayoutHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,54 +13,11 @@ public static class LayoutHelperTests
{
/*
[TestClass]
public sealed class CalculateLayoutInfoTests
public sealed class OldLayoutTests
{
public sealed class TestCase
{
public TestCase(LayoutConfig layoutConfig, LayoutInfo expectedResult)
{
this.LayoutConfig = layoutConfig;
this.ExpectedResult = expectedResult;
}
public LayoutConfig LayoutConfig { get; }
public LayoutInfo ExpectedResult { get; }
}
public static IEnumerable<object[]> GetTestCases()
{
// happy path - check the preview form is shown
// at the correct size and position on a single screen
//
// +----------------+
// | |
// | 0 |
// | |
// +----------------+
var layoutConfig = new LayoutConfig(
virtualScreenBounds: new(0, 0, 5120, 1440),
screens: new List<ScreenInfo>
{
new(HMONITOR.Null, false, new(0, 0, 5120, 1440), new(0, 0, 5120, 1440)),
},
activatedLocation: new(5120M / 2, 1440M / 2),
activatedScreenIndex: 0,
activatedScreenNumber: 1,
maximumFormSize: new(1600, 1200),
formPadding: new(5, 5, 5, 5),
previewPadding: new(0, 0, 0, 0));
var layoutInfo = new LayoutInfo(
layoutConfig: layoutConfig,
formBounds: new(1760, 491.40625M, 1600, 457.1875M),
previewBounds: new(0, 0, 1590, 447.1875M),
screenBounds: new List<RectangleInfo>
{
new(0, 0, 1590, 447.1875M),
},
activatedScreenBounds: new(0, 0, 5120, 1440));
yield return new object[] { new TestCase(layoutConfig, layoutInfo) };
// primary monitor not topmost / leftmost - if there are screens
// that are further left or higher than the primary monitor
// they'll have negative coordinates which has caused some
Expand Down Expand Up @@ -194,8 +151,11 @@ public void RunTestCases(TestCase data)
// (int)1280.000000000000 -> 1280
// so we'll compare the raw values, *and* convert to an int-based
// Rectangle to compare rounded values
var actual = LayoutHelper.CalculateLayoutInfo(data.LayoutConfig);
var actual = LayoutHelper.GetPreviewLayout(data.PreviewStyle, data.Screens, data.ActivatedLocation);
var expected = data.ExpectedResult;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
Assert.AreEqual(expected.FormBounds.X, actual.FormBounds.X, 0.00001M, "FormBounds.X");
Assert.AreEqual(expected.FormBounds.Y, actual.FormBounds.Y, 0.00001M, "FormBounds.Y");
Assert.AreEqual(expected.FormBounds.Width, actual.FormBounds.Width, 0.00001M, "FormBounds.Width");
Expand Down Expand Up @@ -249,17 +209,18 @@ public TestCase(PreviewStyle previewStyle, List<RectangleInfo> screens, PointInf

public static IEnumerable<object[]> GetTestCases()
{
// happy path - 50% scaling
// happy path - 50% scaling, *has* preview borders but *no* screenshot borders
//
// +----------------+
// | |
// | 0 |
// | |
// +----------------+
var previewConfig = new PreviewStyle(
var previewStyle = new PreviewStyle(
canvasSize: new(
width: 7 + 2 + 5 + 512 + 2 + 7,
height: 7 + 2 + 384 + 2 + 7),
width: 524,
height: 396
),
canvasStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(
Expand All @@ -273,12 +234,7 @@ public static IEnumerable<object[]> GetTestCases()
color2: Color.FromArgb(3, 68, 192) // darker blue
)
),
screenshotStyle: new(
marginStyle: MarginStyle.Empty,
borderStyle: new(Color.Black, 5, 3),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(Color.Transparent, Color.Transparent)
));
screenshotStyle: BoxStyle.Empty);
var screens = new List<RectangleInfo>
{
new(0, 0, 1024, 768),
Expand All @@ -288,21 +244,82 @@ public static IEnumerable<object[]> GetTestCases()
virtualScreen: new(0, 0, 1024, 768),
screens: screens,
activatedScreenIndex: 0,
formBounds: new(0, 0, 0, 0),
previewStyle: new BoxStyle(
marginStyle: MarginStyle.Empty,
borderStyle: BorderStyle.Empty,
formBounds: new(250, 186, 524, 396),
previewStyle: previewStyle,
previewBounds: new(
outerBounds: new(0, 0, 524, 396),
marginBounds: new(0, 0, 524, 396),
borderBounds: new(0, 0, 524, 396),
paddingBounds: new(5, 5, 514, 386),
contentBounds: new(6, 6, 512, 384)
),
screenshotBounds: new()
{
new(
outerBounds: new(6, 6, 512, 384),
marginBounds: new(6, 6, 512, 384),
borderBounds: new(6, 6, 512, 384),
paddingBounds: new(6, 6, 512, 384),
contentBounds: new(6, 6, 512, 384)
),
});
yield return new object[] { new TestCase(previewStyle, screens, activatedLocation, previewLayout) };

// happy path - 50% scaling, *no* preview borders but *has* screenshot borders
//
// +----------------+
// | |
// | 0 |
// | |
// +----------------+
previewStyle = new PreviewStyle(
canvasSize: new(
width: 512,
height: 384
),
canvasStyle: BoxStyle.Empty,
screenshotStyle: new(
marginStyle: new(
all: 1),
borderStyle: new(
color: SystemColors.Highlight,
all: 5,
depth: 3),
paddingStyle: PaddingStyle.Empty,
backgroundStyle: BackgroundStyle.Empty),
backgroundStyle: new(
color1: Color.FromArgb(13, 87, 210), // light blue
color2: Color.FromArgb(3, 68, 192) // darker blue
)
));
screens = new List<RectangleInfo>
{
new(0, 0, 1024, 768),
};
activatedLocation = new PointInfo(512, 384);
previewLayout = new PreviewLayout(
virtualScreen: new(0, 0, 1024, 768),
screens: screens,
activatedScreenIndex: 0,
formBounds: new(256, 192, 512, 384),
previewStyle: previewStyle,
previewBounds: new(
outerBounds: RectangleInfo.Empty,
marginBounds: RectangleInfo.Empty,
borderBounds: RectangleInfo.Empty,
paddingBounds: RectangleInfo.Empty,
contentBounds: RectangleInfo.Empty),
screenshotStyle: BoxStyle.Empty,
screenshotBounds: Enumerable.Empty<BoxBounds>());
yield return new object[] { new TestCase(previewConfig, screens, activatedLocation, previewLayout) };
outerBounds: new(0, 0, 512, 384),
marginBounds: new(0, 0, 512, 384),
borderBounds: new(0, 0, 512, 384),
paddingBounds: new(0, 0, 512, 384),
contentBounds: new(0, 0, 512, 384)
),
screenshotBounds: new()
{
new(
outerBounds: new(0, 0, 512, 384),
marginBounds: new(0, 0, 512, 384),
borderBounds: new(1, 1, 510, 382),
paddingBounds: new(6, 6, 500, 372),
contentBounds: new(6, 6, 500, 372)
),
});
yield return new object[] { new TestCase(previewStyle, screens, activatedLocation, previewLayout) };
}

[TestMethod]
Expand All @@ -316,10 +333,15 @@ public void RunTestCases(TestCase data)
// (int)1280.000000000000 -> 1280
// so we'll compare the raw values, *and* convert to an int-based
// Rectangle to compare rounded values
/*
var actual = LayoutHelper.GetPreviewLayout(data.PreviewStyle, data.Screens, data.ActivatedLocation);
var expected = data.ExpectedResult;
*/
var options = new JsonSerializerOptions
{
WriteIndented = true,
};
Assert.AreEqual(
JsonSerializer.Serialize(expected, options),
JsonSerializer.Serialize(actual, options));
/* form bounds */
/*
Assert.AreEqual(expected.FormBounds.X, actual.FormBounds.X, 0.00001M, "FormBounds.X");
Expand Down
26 changes: 9 additions & 17 deletions src/FancyMouse.UnitTests/Models/Settings/AppSettingsReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text.Json;
using FancyMouse.Models.Settings;
using FancyMouse.Models.Styles;
using FancyMouse.UnitTests.TestUtils;
using FancyMouse.WindowsHotKeys;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -51,7 +52,7 @@ public void MissingVersionShouldBeTreatedAsVersion1()
[TestMethod]
public void EmptyVersion1ShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 1,
Expand All @@ -66,7 +67,7 @@ public void EmptyVersion1ShouldParse()
[TestMethod]
public void Version1WithNullRootKeysShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 1,
Expand All @@ -82,7 +83,7 @@ public void Version1WithNullRootKeysShouldParse()
[TestMethod]
public void Version1WithNullChildKeysShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 1,
Expand All @@ -101,7 +102,7 @@ public void Version1WithNullChildKeysShouldParse()
[TestMethod]
public void Version1WithAllValuesShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 1,
Expand Down Expand Up @@ -133,7 +134,7 @@ public void Version1WithAllValuesShouldParse()
[TestMethod]
public void Version2WithNullRootKeysShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 2,
Expand All @@ -150,7 +151,7 @@ public void Version2WithNullRootKeysShouldParse()
[TestMethod]
public void Version2WithAllValuesShouldParse()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 2,
Expand Down Expand Up @@ -254,7 +255,7 @@ public void Version2WithAllValuesShouldParse()
[TestMethod]
public void PerformanceTest()
{
var json = ParseJsonTests.SerializeAnonymousType(
var json = SerializationUtils.SerializeAnonymousType(
new
{
version = 2,
Expand Down Expand Up @@ -321,16 +322,7 @@ public void PerformanceTest()
var averageMs = (decimal)times.Sum() / times.Count / ticksPerMs;
Console.WriteLine($"{averageMs} ms");

Assert.IsTrue(averageMs < 1);
}

private static string SerializeAnonymousType<T>(T value)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
};
return JsonSerializer.Serialize(value, options);
Assert.IsTrue(averageMs < 1.5M);
}
}
}
14 changes: 14 additions & 0 deletions src/FancyMouse.UnitTests/TestUtils/SerializationUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json;

namespace FancyMouse.UnitTests.TestUtils;
internal static class SerializationUtils
{
public static string SerializeAnonymousType<T>(T value)
{
var options = new JsonSerializerOptions
{
WriteIndented = true,
};
return JsonSerializer.Serialize(value, options);
}
}
8 changes: 4 additions & 4 deletions src/FancyMouse/Helpers/DrawingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ internal static Bitmap RenderPreview(
var previewGraphics = Graphics.FromImage(previewImage);
previewImageCreatedCallback?.Invoke(previewImage);

DrawingHelper.DrawBoxBorder(previewGraphics, previewLayout.PreviewStyle, previewLayout.PreviewBounds);
DrawingHelper.DrawBoxBorder(previewGraphics, previewLayout.PreviewStyle.CanvasStyle, previewLayout.PreviewBounds);
DrawingHelper.DrawBoxBackground(
previewGraphics,
previewLayout.PreviewStyle,
previewLayout.PreviewStyle.CanvasStyle,
previewLayout.PreviewBounds,
Enumerable.Empty<RectangleInfo>());

Expand All @@ -57,7 +57,7 @@ internal static Bitmap RenderPreview(
foreach (var screenshotBounds in previewLayout.ScreenshotBounds)
{
DrawingHelper.DrawBoxBorder(
previewGraphics, previewLayout.ScreenshotStyle, screenshotBounds);
previewGraphics, previewLayout.PreviewStyle.ScreenshotStyle, screenshotBounds);
}

var placeholdersDrawn = false;
Expand Down Expand Up @@ -85,7 +85,7 @@ internal static Bitmap RenderPreview(
// draw placeholders for any undrawn screens
DrawingHelper.DrawPlaceholders(
previewGraphics,
previewLayout.ScreenshotStyle,
previewLayout.PreviewStyle.ScreenshotStyle,
targetScreens.Where((_, idx) => idx > i).ToList());
placeholdersDrawn = true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/FancyMouse/Helpers/LayoutHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static PreviewLayout GetPreviewLayout(
.Offset(previewStyle.CanvasStyle.PaddingStyle.Left, previewStyle.CanvasStyle.PaddingStyle.Top);

// now we know the size of the content area we can work out the background bounds
builder.PreviewStyle = previewStyle.CanvasStyle;
builder.PreviewStyle = previewStyle;
builder.PreviewBounds = LayoutHelper.GetBoxBoundsFromContentBounds(
contentBounds,
previewStyle.CanvasStyle);
Expand All @@ -81,7 +81,6 @@ public static PreviewLayout GetPreviewLayout(
.ScaleToFitRatio(contentBounds.Size);

// now calculate the positions of each of the screenshot images on the preview
builder.ScreenshotStyle = previewStyle.ScreenshotStyle;
builder.ScreenshotBounds = allScreens
.Select(
screen => LayoutHelper.GetBoxBoundsFromOuterBounds(
Expand Down
Loading

0 comments on commit 144a037

Please sign in to comment.