Skip to content

Commit

Permalink
Added shared library for test application: Svg.Tests.Common (#1136)
Browse files Browse the repository at this point in the history
- Refactored the shared test codes
- Removed Svg.Benchmark dependency on Svg.UnitTests
  • Loading branch information
paulushub committed Jan 25, 2024
1 parent 3b41c8c commit 030ac29
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 352 deletions.
24 changes: 16 additions & 8 deletions Source/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
Expand All @@ -24,16 +25,23 @@

[assembly: CLSCompliant(true)]

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Svg.UnitTests,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
[assembly:InternalsVisibleTo("Svg.UnitTests,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" +
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" +
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" +
"5a7f0cb4")]

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Svg.Benchmark,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" +
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" +
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" +
"5a7f0cb4")]
[assembly: InternalsVisibleTo("Svg.Benchmark,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" +
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" +
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" +
"5a7f0cb4")]

[assembly: InternalsVisibleTo("Svg.Tests.Common,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" +
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" +
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" +
"5a7f0cb4")]
11 changes: 9 additions & 2 deletions Source/Svg.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29215.179
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Svg", "Svg.csproj", "{886A98C5-37C0-4E8B-885E-30C1D2F98B47}"
EndProject
Expand Down Expand Up @@ -40,6 +40,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Generators", "Generators",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Svg.Custom", "..\Svg.Custom\Svg.Custom.csproj", "{6FAA2B79-FE5C-456B-A743-E9290665B223}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Svg.Tests.Common", "..\Tests\Svg.Tests.Common\Svg.Tests.Common.csproj", "{D7C625E8-79EA-4859-A457-2C4A90786790}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -90,6 +92,10 @@ Global
{6FAA2B79-FE5C-456B-A743-E9290665B223}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FAA2B79-FE5C-456B-A743-E9290665B223}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6FAA2B79-FE5C-456B-A743-E9290665B223}.Release|Any CPU.Build.0 = Release|Any CPU
{D7C625E8-79EA-4859-A457-2C4A90786790}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7C625E8-79EA-4859-A457-2C4A90786790}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7C625E8-79EA-4859-A457-2C4A90786790}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7C625E8-79EA-4859-A457-2C4A90786790}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -104,6 +110,7 @@ Global
{9379360C-CB0B-4035-AE8F-8863A420FDCF} = {038E2E7B-BC03-4DA7-AA5F-CA8BD95BD0F2}
{BACDD1F4-B97D-4E27-BD09-6957633FF484} = {FAFD6DC7-5203-4CED-A151-66379DD43695}
{8DEB3EA7-5915-4EB9-8052-85A7AC4379EC} = {2EC3F3A0-F097-43EF-A603-9B82E3061469}
{D7C625E8-79EA-4859-A457-2C4A90786790} = {2EC3F3A0-F097-43EF-A603-9B82E3061469}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5096EEB3-8F41-44B5-BCF9-58743A59AB21}
Expand Down
7 changes: 1 addition & 6 deletions Tests/Svg.Benchmark/Svg.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,12 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\Svg.csproj" />
<ProjectReference Include="..\Svg.UnitTests\Svg.UnitTests.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net462'">
<Reference Include="WindowsBase" />
<ProjectReference Include="..\Svg.Tests.Common\Svg.Tests.Common.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,24 @@ public static int GetSpecificity(this ISelector selector)
specificity |= (1 << 4) * selector.Specificity.Tags;
return specificity;
}


public static IEnumerable<SvgElement> QuerySelectorExCssAll(NonSvgElement elem,
string selector, SvgElementFactory elementFactory)
{
var stylesheetParser = new StylesheetParser(true, true);
var stylesheet = stylesheetParser.Parse(selector + " {color:black}");
var exCssSelector = stylesheet.StyleRules.First().Selector;
return elem.QuerySelectorAll(exCssSelector, elementFactory);
}

public static IEnumerable<SvgElement> QuerySelectorFizzlerAll(NonSvgElement elem,
string selector, SvgElementFactory elementFactory)
{
var generator = new SelectorGenerator<SvgElement>(new SvgElementOps(elementFactory));
Fizzler.Parser.Parse(selector, generator);
return generator.Selector(Enumerable.Repeat(elem, 1));
}

}
}
File renamed without changes.
123 changes: 123 additions & 0 deletions Tests/Svg.Tests.Common/ExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Drawing.Imaging;
using System.Drawing;
using System.Drawing.Drawing2D;


namespace Svg.Tests.Common
{
/// <summary>
/// Taken from https://web.archive.org/web/20130111215043/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale
/// and slightly modified.
/// Image width and height, default threshold and handling of alpha values have been adapted.
/// </summary>
public static class ExtensionMethods
{
private static readonly int ImageWidth = 64;
private static readonly int ImageHeight = 64;

public static float PercentageDifference(this Image img1, Image img2, byte threshold = 10)
{
byte[,] differences = img1.GetDifferences(img2);

int diffPixels = 0;

foreach (byte b in differences)
{
if (b > threshold) { diffPixels++; }
}

return diffPixels / (float)(differences.GetLength(0) * differences.GetLength(1));
}

public static Bitmap Resize(this Image originalImage, int newWidth, int newHeight)
{
if (originalImage.Width > originalImage.Height)
newWidth = originalImage.Width * newHeight / originalImage.Height;
else
newHeight = originalImage.Height * newWidth / originalImage.Width;

var smallVersion = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(smallVersion))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(originalImage, 0, 0, smallVersion.Width, smallVersion.Height);
}

return smallVersion;
}

public static byte[,] GetGrayScaleValues(this Bitmap img)
{
byte[,] grayScale = new byte[img.Width, img.Height];

for (int y = 0; y < grayScale.GetLength(1); y++)
{
for (int x = 0; x < grayScale.GetLength(0); x++)
{
var alpha = img.GetPixel(x, y).A;
var gray = img.GetPixel(x, y).R;
grayScale[x, y] = (byte)Math.Abs(gray * alpha / 255);
}
}
return grayScale;
}

// the colormatrix needed to grayscale an image
static readonly ColorMatrix ColorMatrix = new ColorMatrix(new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});

public static Bitmap GetGrayScaleVersion(this Bitmap original)
{
// create a blank bitmap the same size as original
// https://web.archive.org/web/20130111215043/http://www.switchonthecode.com/tutorials/csharp-tutorial-convert-a-color-image-to-grayscale
var newBitmap = new Bitmap(original.Width, original.Height);

// get a graphics object from the new image
using (var g = Graphics.FromImage(newBitmap))
// create some image attributes
using (var attributes = new ImageAttributes())
{
// set the color matrix attribute
attributes.SetColorMatrix(ColorMatrix);

// draw the original image on the new image
// using the grayscale color matrix
g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);
}

return newBitmap;
}

public static byte[,] GetDifferences(this Image img1, Image img2)
{
using (var resizedThisOne = img1.Resize(ImageWidth, ImageHeight))
using (var thisOne = resizedThisOne.GetGrayScaleVersion())
using (var resizedTheOtherOne = img2.Resize(ImageWidth, ImageHeight))
using (var theOtherOne = resizedTheOtherOne.GetGrayScaleVersion())
{
byte[,] differences = new byte[thisOne.Width, thisOne.Height];
byte[,] firstGray = thisOne.GetGrayScaleValues();
byte[,] secondGray = theOtherOne.GetGrayScaleValues();

for (int y = 0; y < differences.GetLength(1); y++)
{
for (int x = 0; x < differences.GetLength(0); x++)
{
differences[x, y] = (byte)Math.Abs(firstGray[x, y] - secondGray[x, y]);
}
}
return differences;
}
}
}
}
49 changes: 49 additions & 0 deletions Tests/Svg.Tests.Common/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Svg.Tests.Common")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Svg.Tests.Common")]
[assembly: AssemblyCopyright("Copyright © 2024")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6ab1d266-f201-46c0-9d14-523768eb18db")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

[assembly: InternalsVisibleTo("Svg.Benchmark,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" +
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" +
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" +
"5a7f0cb4")]

[assembly: InternalsVisibleTo("Svg.UnitTests,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001008d4e723a8c76be" +
"e667607d1fca2c0f0cdcc1c1b926ae46669128282ecad43e6d0776497cd8289dca11e4479773d5" +
"45fc4c557686de548aadbb8652fa550e21d4c402885fec4c1deebfa79e861adb966fc8f4e78235" +
"79a535280ddd3a0168cb4d19522c7591b6693377058675da70e50c7bd6fdceae055cef085f02a0" +
"5a7f0cb4")]
35 changes: 35 additions & 0 deletions Tests/Svg.Tests.Common/Svg.Tests.Common.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netcoreapp3.1;net462</TargetFrameworks>
<SignAssembly>False</SignAssembly>
<DelaySign>False</DelaySign>
<IsPackable>False</IsPackable>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>svgkey.snk</AssemblyOriginatorKeyFile>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net462'">
<Reference Include="System.Net.Http" />
<Reference Include="System.IO.Compression" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1'">
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="System.Drawing.Common" Version="5.0.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\Svg.csproj" />
<PackageReference Include="Fizzler" Version="1.3.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.IO;
using System.IO.Compression;

namespace Svg.UnitTests
namespace Svg.Tests.Common
{
internal static class TestsUtils
public static class TestsUtils
{
private const string FixImage = "smiley.png";

Expand Down
Binary file added Tests/Svg.Tests.Common/svgkey.snk
Binary file not shown.
Loading

0 comments on commit 030ac29

Please sign in to comment.