Skip to content

Commit f8b2aa8

Browse files
committed
Minor improvements.
GDI unit test, fix typos, extension method to get image, etc.
1 parent 43c66e7 commit f8b2aa8

32 files changed

+107
-19
lines changed

src/OpenMacroBoard.SDK/ButtonPressEffectAdapter.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ private KeyBitmap ResizeBitmap(KeyBitmap keyBitmap)
9999
var targetWidth = (int)Math.Round(Config.Scale * keyBitmap.Width);
100100
var targetHeight = (int)Math.Round(Config.Scale * keyBitmap.Height);
101101

102-
var smallerImage = Image.LoadPixelData<Bgr24>(
103-
bitmapDataAccess.GetData(),
104-
keyBitmap.Width,
105-
keyBitmap.Height
106-
);
107-
102+
var smallerImage = bitmapDataAccess.ToImage();
108103
smallerImage.Mutate(x => x.Resize(targetWidth, targetHeight));
109104

110105
var offsetLeft = (int)Math.Round((keyBitmap.Width - targetWidth) * Config.OriginX);

src/OpenMacroBoard.SDK/IKeyBitmapDataAccess.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ namespace OpenMacroBoard.SDK
77
/// </summary>
88
public interface IKeyBitmapDataAccess
99
{
10+
/// <summary>
11+
/// Gets the width of the bitmap.
12+
/// </summary>
13+
int Width { get; }
14+
15+
/// <summary>
16+
/// Gets the height of the bitmap.
17+
/// </summary>
18+
int Height { get; }
19+
1020
/// <summary>
1121
/// Gets a value indicating whether the underlying byte array is null.
1222
/// </summary>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using SixLabors.ImageSharp;
2+
using SixLabors.ImageSharp.PixelFormats;
3+
4+
namespace OpenMacroBoard.SDK
5+
{
6+
/// <summary>
7+
/// Extension methods for <see cref="IKeyBitmapDataAccess"/>.
8+
/// </summary>
9+
public static class KeyBitmapDataAccessExtensions
10+
{
11+
/// <summary>
12+
/// Creates a new <see cref="Image{Bgr24}"/> for this <see cref="IKeyBitmapDataAccess"/>.
13+
/// </summary>
14+
/// <remarks>
15+
/// <para>
16+
/// Keep in mind that this operation allocates and creates a copy under the hood.
17+
/// </para>
18+
/// </remarks>
19+
public static Image<Bgr24> ToImage(this IKeyBitmapDataAccess dataAccess)
20+
{
21+
return Image.LoadPixelData<Bgr24>(dataAccess.GetData(), dataAccess.Width, dataAccess.Height);
22+
}
23+
}
24+
}

src/OpenMacroBoard.Tests/DeviceContextTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using FluentAssertions;
1+
using FluentAssertions;
22
using OpenMacroBoard.SDK;
33
using System;
44
using System.Collections.Generic;
@@ -186,7 +186,7 @@ public IDisposable Subscribe(IObserver<DeviceStateReport> observer)
186186
}
187187
}
188188

189-
private class DoNothingDisposable : IDisposable
189+
private sealed class DoNothingDisposable : IDisposable
190190
{
191191
public static IDisposable Instance { get; } = new DoNothingDisposable();
192192

src/OpenMacroBoard.Tests/ExtendedVerifierImageExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using OpenMacroBoard.Meta.TestUtils;
2+
using OpenMacroBoard.SDK;
3+
using SixLabors.ImageSharp;
4+
using SixLabors.ImageSharp.PixelFormats;
25
using System.Threading.Tasks;
36

47
namespace OpenMacroBoard.Tests
@@ -17,5 +20,17 @@ await settings
1720
.WithExtension("png")
1821
.VerifyAsync(target.BoardImage);
1922
}
23+
24+
public static Task VerifyAsync(this ExtendedVerifySettings settings, KeyBitmap keyBitmap)
25+
{
26+
var dataAccess = (IKeyBitmapDataAccess)keyBitmap;
27+
28+
using var img = dataAccess.ToImage();
29+
30+
return settings
31+
.WithFileNameSuffix("_KeyBitmapImg")
32+
.WithExtension("png")
33+
.VerifyAsync(img);
34+
}
2035
}
2136
}

src/OpenMacroBoard.Tests/FakeMacroBoard.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ public void SetKeyBitmap(int keyId, KeyBitmap bitmapData)
125125
else
126126
{
127127
// load given image
128-
using var keyImage = Image.LoadPixelData<Bgr24>(
129-
dataAccess.GetData(),
130-
bitmapData.Width,
131-
bitmapData.Height
132-
);
133-
128+
using var keyImage = dataAccess.ToImage();
134129
ResizeAndApplyImage(keyId, keyImage);
135130
}
136131
}

src/OpenMacroBoard.Tests/KeyBitmapFactoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using SixLabors.ImageSharp.PixelFormats;
55
using Xunit;
66

7-
namespace StreamDeckSharp.Tests
7+
namespace OpenMacroBoard.Tests
88
{
99
public class KeyBitmapFactoryTests
1010
{
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using OpenMacroBoard.Meta.TestUtils;
2+
using OpenMacroBoard.SDK;
3+
using System.Drawing;
4+
using System.Runtime.Versioning;
5+
using System.Threading.Tasks;
6+
using VerifyXunit;
7+
using Xunit;
8+
9+
namespace OpenMacroBoard.Tests
10+
{
11+
[UsesVerify]
12+
public class KeyBitmapFactoryVisualTests
13+
{
14+
public ExtendedVerifySettings Verifier { get; } = DefaultVerifySettings.Build();
15+
16+
[Fact]
17+
[SupportedOSPlatform("windows")]
18+
public async Task GDI_arc_is_drawn_correctly()
19+
{
20+
Verifier.Initialize();
21+
Verifier.UseFileNameAsDirectory();
22+
23+
var key = KeyBitmap.Create
24+
.FromGraphics(
25+
width: 64,
26+
height: 64,
27+
g =>
28+
{
29+
g.Clear(Color.Red);
30+
g.DrawArc(Pens.Blue, new Rectangle(10, 10, 40, 40), 10, 180);
31+
}
32+
);
33+
34+
await Verifier.VerifyAsync(key);
35+
}
36+
}
37+
}

src/OpenMacroBoard.Tests/KeyBitmapTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void NegativeOrZeroHeightCausesConstructorToThrow()
3939
}
4040

4141
[Fact]
42-
public void ImageDataArrayLengthMissmatchThrowsException()
42+
public void ImageDataArrayLengthMismatchThrowsException()
4343
{
4444
Action act_correctParams = () => _ = KeyBitmap.Create.FromBgr24Array(2, 2, new byte[12]);
4545
Action act_incorrectParams = () => _ = KeyBitmap.Create.FromBgr24Array(3, 3, new byte[5]);
@@ -122,11 +122,14 @@ public void AllEqualityMethodsBehaveTheSameWay()
122122
var key2 = KeyBitmap.Create.FromBgr24Array(1, 1, key2Data);
123123
var key3 = KeyBitmap.Create.FromBgr24Array(1, 1, new byte[3]);
124124

125+
125126
var equalityMethods = new List<Func<KeyBitmap, KeyBitmap, bool>>()
126127
{
127128
KeyBitmap.Equals,
128129
(a,b) => a == b,
130+
#pragma warning disable S1940 // Boolean checks should not be inverted
129131
(a,b) => !(a != b),
132+
#pragma warning restore S1940
130133
(a,b) => a.Equals(b),
131134
};
132135

@@ -140,7 +143,7 @@ public void AllEqualityMethodsBehaveTheSameWay()
140143
}
141144

142145
[Fact]
143-
public void HashCodesDontMatchEasilyForDifferentObjects()
146+
public void HashCodesDonNotMatchEasilyForDifferentObjects()
144147
{
145148
var key2Data = new byte[3] { 1, 2, 3 };
146149

src/OpenMacroBoard.Tests/KeyBitmapVisualTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class KeyBitmapVisualTests
1818

1919
[Theory]
2020
[ClassData(typeof(DeviceGridKeyPositionDataProvider))]
21-
public async Task FakeMacroboardSelfCheck(string deviceName, GridKeyLayout keys)
21+
public async Task FakeMacroBoardSelfCheck(string deviceName, GridKeyLayout keys)
2222
{
2323
Verifier.Initialize();
2424

@@ -185,7 +185,7 @@ async Task TestKey(KeyBitmap key, string state)
185185
}
186186

187187
[Fact]
188-
public async Task BitmapKeyChannelsWorkAsExpectedFor32bitExtentsions()
188+
public async Task BitmapKeyChannelsWorkAsExpectedFor32bitExtensions()
189189
{
190190
Verifier.Initialize();
191191
Verifier.UseFileNameAsDirectory();

0 commit comments

Comments
 (0)