Skip to content

Commit cb85957

Browse files
committed
Improved unit test by using the native ImageMagick method instead.
1 parent ba35a6b commit cb85957

File tree

3 files changed

+8
-42
lines changed

3 files changed

+8
-42
lines changed

src/Magick.NET/MagickNET.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,9 @@ void IMagickNET.SetTempDirectory(string path)
484484
void IMagickNET.SetRandomSeed(ulong seed)
485485
=> SetRandomSeed(seed);
486486

487+
internal static string? GetWindowsResource(string id)
488+
=> NativeMagickNET.GetWindowsResource(id);
489+
487490
private static void CheckImageMagickFiles(string path)
488491
{
489492
foreach (var configurationFile in ConfigurationFiles.Default.All)

src/Magick.NET/Native/Magick.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ private partial class NativeMagickNET
3333

3434
public static partial string? GetFontName(IntPtr instance, nuint index);
3535

36+
[Cleanup]
37+
public static partial string? GetWindowsResource(string id);
38+
3639
public static partial void ResetRandomSeed();
3740

3841
[Throws]
Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using System;
5-
using System.Runtime.InteropServices;
6-
using System.Text;
74
using ImageMagick;
85
using ImageMagick.Configuration;
96
using Xunit;
@@ -20,50 +17,13 @@ public void ShouldBeEmbeddedInTheNativeLibrary()
2017
if (!Runtime.IsWindows)
2118
Assert.Skip("The embedded resources are only available on Windows.");
2219

23-
#if PLATFORM_x64 || PLATFORM_AnyCPU
24-
var module = GetModuleHandle(ImageMagick.NativeLibrary.X64Name);
25-
#elif PLATFORM_arm64
26-
var module = GetModuleHandle(ImageMagick.NativeLibrary.Arm64Name);
27-
#else
28-
var module = GetModuleHandle(ImageMagick.NativeLibrary.X86Name);
29-
#endif
30-
Assert.NotEqual(IntPtr.Zero, module);
31-
3220
foreach (var configurationFile in ConfigurationFiles.Default.All)
3321
{
34-
var resource = FindResource(module, configurationFile.FileName, "IMAGEMAGICK");
35-
Assert.NotEqual(IntPtr.Zero, resource);
36-
37-
var resourceData = LoadResource(module, resource);
38-
Assert.NotEqual(IntPtr.Zero, resourceData);
39-
40-
var resourcePointer = LockResource(resourceData);
41-
Assert.NotEqual(IntPtr.Zero, resourcePointer);
42-
43-
var size = SizeofResource(module, resource);
44-
Assert.NotEqual(0U, size);
45-
46-
var bytes = new byte[size];
47-
Marshal.Copy(resourcePointer, bytes, 0, (int)size);
22+
var data = MagickNET.GetWindowsResource(configurationFile.FileName);
4823

49-
var data = Encoding.UTF8.GetString(bytes);
24+
Assert.NotNull(data);
5025
Assert.Equal(configurationFile.Data, data);
5126
}
5227
}
53-
54-
[DllImport("kernel32.dll", SetLastError = true)]
55-
private static extern IntPtr FindResource(IntPtr hModule, string lpName, string lpType);
56-
57-
[DllImport("kernel32.dll", SetLastError = true)]
58-
private static extern IntPtr GetModuleHandle(string lpModuleName);
59-
60-
[DllImport("kernel32.dll", SetLastError = true)]
61-
private static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo);
62-
63-
[DllImport("kernel32.dll", SetLastError = true)]
64-
private static extern IntPtr LockResource(IntPtr hResData);
65-
66-
[DllImport("kernel32.dll", SetLastError = true)]
67-
private static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);
6828
}
6929
}

0 commit comments

Comments
 (0)