Skip to content

Commit

Permalink
#19 - refactoring to make testing easier
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeclayton committed Jul 13, 2023
1 parent c741987 commit 7f6fbab
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 60 deletions.
127 changes: 127 additions & 0 deletions src/FancyMouse.UnitTests/Helpers/DrawingHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Cryptography.X509Certificates;
using FancyMouse.Helpers;
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Settings;
using FancyMouse.Models.Styles;
using FancyMouse.NativeMethods;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FancyMouse.UnitTests.Helpers;

[TestClass]
public static class DrawingHelperTests
{
[TestClass]
public sealed class GetPreviewLayoutTests
{
private static Bitmap DrawDesktopImage(List<(RectangleInfo Bounds, Color Color)> screens)
{
// created a "desktop" bitmap with the given screen areas drawn the specified colors.
var desktopBounds = LayoutHelper.GetCombinedScreenBounds(
screens.Select(s => s.Bounds).ToList());

// we can only create bitmaps with non-negative coordinates
if (desktopBounds.X < 0 || desktopBounds.Y < 0)
{
throw new InvalidOperationException();
}

// create the bitmap
var bitmap = new Bitmap((int)desktopBounds.Width, (int)desktopBounds.Height, PixelFormat.Format32bppArgb);

// draw the rectangles
using var graphics = Graphics.FromImage(bitmap);
foreach (var screen in screens)
{
var screenBounds = screen.Bounds.ToRectangle();
/*
using var pen = new Pen(screen.Color);
graphics.DrawRectangle(
pen, screenBounds.X, screenBounds.Y, screenBounds.Width - 1, screenBounds.Height - 1);
*/
using var brush = new SolidBrush(screen.Color);
graphics.FillRectangle(brush, screenBounds);
}

return bitmap;
}

public sealed class TestCase
{
public TestCase(PreviewStyle previewStyle, List<(RectangleInfo Bounds, Color Color)> screens, PointInfo activatedLocation)
{
this.PreviewStyle = previewStyle;
this.Screens = screens;
this.ActivatedLocation = activatedLocation;
}

public PreviewStyle PreviewStyle { get; set; }

public List<(RectangleInfo Bounds, Color Color)> Screens { get; set; }

public PointInfo ActivatedLocation { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
yield return new object[]
{
new TestCase(
previewStyle: AppSettings.DefaultSettings.PreviewStyle,
screens: new()
{
new()
{
Bounds = new RectangleInfo(0, 0, 500, 500),
Color = Color.Red,
},
new()
{
Bounds = new RectangleInfo(500, 0, 500, 500),
Color = Color.Orange,
},
new()
{
Bounds = new RectangleInfo(500, 500, 500, 500),
Color = Color.Yellow,
},
new()
{
Bounds = new RectangleInfo(0, 500, 500, 500),
Color = Color.Green,
},
},
new(x: 50, y: 50)),
};
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
// generate the fake desktop image
using var desktop = GetPreviewLayoutTests.DrawDesktopImage(data.Screens);

// var desktop = Bitmap.FromFile(".\\desktop.png");
using var graphics = Graphics.FromImage(desktop);
var desktopHdc = new Core.HDC(graphics.GetHdc());

// draw the preview image
var previewLayout = LayoutHelper.GetPreviewLayout(
previewStyle: data.PreviewStyle,
screens: data.Screens.Select(s => s.Bounds).ToList(),
activatedLocation: data.ActivatedLocation);
using var actual = DrawingHelper.RenderPreview(previewLayout, desktopHdc);
actual.Save(".\\actual.png", ImageFormat.Png);
/*
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);
*/
}
}
}
117 changes: 112 additions & 5 deletions src/FancyMouse.UnitTests/Helpers/LayoutHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Text.Json;
using FancyMouse.Helpers;
using FancyMouse.Models.Drawing;
using FancyMouse.Models.Layout;
Expand Down Expand Up @@ -230,7 +231,7 @@ public sealed class GetPreviewLayoutTests
{
public sealed class TestCase
{
public TestCase(PreviewStyle previewStyle, List<ScreenInfo> screens, PointInfo activatedLocation, PreviewLayout expectedResult)
public TestCase(PreviewStyle previewStyle, List<RectangleInfo> screens, PointInfo activatedLocation, PreviewLayout expectedResult)
{
this.PreviewStyle = previewStyle;
this.Screens = screens;
Expand All @@ -240,7 +241,7 @@ public TestCase(PreviewStyle previewStyle, List<ScreenInfo> screens, PointInfo a

public PreviewStyle PreviewStyle { get; set; }

public List<ScreenInfo> Screens { get; set; }
public List<RectangleInfo> Screens { get; set; }

public PointInfo ActivatedLocation { get; set; }

Expand Down Expand Up @@ -279,15 +280,15 @@ public static IEnumerable<object[]> GetTestCases()
paddingStyle: PaddingStyle.Empty,
backgroundStyle: new(Color.Transparent, Color.Transparent)
));
var screens = new List<ScreenInfo>
var screens = new List<RectangleInfo>
{
new(HANDLE.Null, true, new(0, 0, 1024, 768), new(0, 0, 1024, 768)),
new(0, 0, 1024, 768),
};
var activatedLocation = new PointInfo(512, 384);
var previewLayout = new PreviewLayout(
virtualScreen: new(0, 0, 1024, 768),
screens: screens,
activatedScreen: screens[0],
activatedScreenIndex: 0,
formBounds: new(0, 0, 0, 0),
previewStyle: new BoxStyle(
marginStyle: MarginStyle.Empty,
Expand Down Expand Up @@ -316,14 +317,18 @@ 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;
*/
/* form bounds */
/*
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");
Assert.AreEqual(expected.FormBounds.Height, actual.FormBounds.Height, 0.00001M, "FormBounds.Height");
Assert.AreEqual(expected.FormBounds.ToRectangle(), actual.FormBounds.ToRectangle(), "FormBounds.ToRectangle");
*/
/* preview bounds */
/*
Assert.AreEqual(expected.PreviewBounds.X, actual.PreviewBounds.X, 0.00001M, "PreviewBounds.X");
Expand Down Expand Up @@ -351,4 +356,106 @@ public void RunTestCases(TestCase data)
*/
}
}

[TestClass]
public sealed class GetBoxBoundsFromContentBoundsTests
{
public sealed class TestCase
{
public TestCase(RectangleInfo contentBounds, BoxStyle boxStyle, BoxBounds expectedResult)
{
this.ContentBounds = contentBounds;
this.BoxStyle = boxStyle;
this.ExpectedResult = expectedResult;
}

public RectangleInfo ContentBounds { get; set; }

public BoxStyle BoxStyle { get; set; }

public BoxBounds ExpectedResult { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
yield return new[]
{
new TestCase(
contentBounds: new(100, 100, 800, 600),
boxStyle: new(
marginStyle: new(3),
borderStyle: new(Color.Red, 5, 0),
paddingStyle: new(7),
backgroundStyle: BackgroundStyle.Empty),
expectedResult: new(
outerBounds: new(85, 85, 830, 630),
marginBounds: new(85, 85, 830, 630),
borderBounds: new(88, 88, 824, 624),
paddingBounds: new(93, 93, 814, 614),
contentBounds: new(100, 100, 800, 600))),
};
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
var actual = LayoutHelper.GetBoxBoundsFromContentBounds(data.ContentBounds, data.BoxStyle);
var expected = data.ExpectedResult;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}
}

[TestClass]
public sealed class GetBoxBoundsFromOuterBoundsTests
{
public sealed class TestCase
{
public TestCase(RectangleInfo outerBounds, BoxStyle boxStyle, BoxBounds expectedResult)
{
this.OuterBounds = outerBounds;
this.BoxStyle = boxStyle;
this.ExpectedResult = expectedResult;
}

public RectangleInfo OuterBounds { get; set; }

public BoxStyle BoxStyle { get; set; }

public BoxBounds ExpectedResult { get; set; }
}

public static IEnumerable<object[]> GetTestCases()
{
yield return new[]
{
new TestCase(
outerBounds: new(85, 85, 830, 630),
boxStyle: new(
marginStyle: new(3),
borderStyle: new(Color.Red, 5, 0),
paddingStyle: new(7),
backgroundStyle: BackgroundStyle.Empty),
expectedResult: new(
outerBounds: new(85, 85, 830, 630),
marginBounds: new(85, 85, 830, 630),
borderBounds: new(88, 88, 824, 624),
paddingBounds: new(93, 93, 814, 614),
contentBounds: new(100, 100, 800, 600))),
};
}

[TestMethod]
[DynamicData(nameof(GetTestCases), DynamicDataSourceType.Method)]
public void RunTestCases(TestCase data)
{
var actual = LayoutHelper.GetBoxBoundsFromOuterBounds(data.OuterBounds, data.BoxStyle);
var expected = data.ExpectedResult;
Assert.AreEqual(
JsonSerializer.Serialize(expected),
JsonSerializer.Serialize(actual));
}
}
}
2 changes: 1 addition & 1 deletion src/FancyMouse.WindowsHotKeys/HotKeyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private LRESULT WindowProc(HWND hWnd, MESSAGE_TYPE msg, WPARAM wParam, LPARAM lP
public void Start()
{
// see https://learn.microsoft.com/en-us/windows/win32/winmsg/using-messages-and-message-queues
var hInstance = Process.GetCurrentProcess().Handle;
var hInstance = (HINSTANCE)Process.GetCurrentProcess().Handle;

// see https://stackoverflow.com/a/30992796/3156906
var wndClass = new WNDCLASSEXW(
Expand Down
Loading

0 comments on commit 7f6fbab

Please sign in to comment.