Skip to content

Commit 8bffc63

Browse files
committed
Add XamlQRCode test cases
1 parent 4720e53 commit 8bffc63

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

QRCoder.sln

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29911.84
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31919.166
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoder", "QRCoder\QRCoder.csproj", "{AA6BE23A-7813-4D2A-835E-B673631AE9F1}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderConsole", "QRCoderConsole\QRCoderConsole.csproj", "{014F04C6-6099-4552-9A4F-D09C6E39D576}"
99
EndProject
10-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderDemo", "QRCoderDemo\QRCoderDemo.csproj", "{CA6DB0F5-DB6C-4DDD-8B5F-EF82FBB963E9}"
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderDemo", "QRCoderDemo\QRCoderDemo.csproj", "{CA6DB0F5-DB6C-4DDD-8B5F-EF82FBB963E9}"
1111
EndProject
1212
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QRCoderDemoUWP", "QRCoderDemoUWP\QRCoderDemoUWP.csproj", "{61933D20-DB1F-4EE8-AC8E-3A2316CDD20C}"
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoderTests", "QRCoderTests\QRCoderTests.csproj", "{1B51624B-9915-4ED6-8FC1-1B7C472246E5}"
1515
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QRCoder.Xaml", "QRCoder.Xaml\QRCoder.Xaml.csproj", "{A7A7E073-2504-4BA2-A63B-87AC34174789}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -101,6 +103,22 @@ Global
101103
{1B51624B-9915-4ED6-8FC1-1B7C472246E5}.Release|x64.Build.0 = Release|Any CPU
102104
{1B51624B-9915-4ED6-8FC1-1B7C472246E5}.Release|x86.ActiveCfg = Release|Any CPU
103105
{1B51624B-9915-4ED6-8FC1-1B7C472246E5}.Release|x86.Build.0 = Release|Any CPU
106+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|Any CPU.Build.0 = Debug|Any CPU
108+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|ARM.ActiveCfg = Debug|Any CPU
109+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|ARM.Build.0 = Debug|Any CPU
110+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|x64.ActiveCfg = Debug|Any CPU
111+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|x64.Build.0 = Debug|Any CPU
112+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|x86.ActiveCfg = Debug|Any CPU
113+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Debug|x86.Build.0 = Debug|Any CPU
114+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|Any CPU.ActiveCfg = Release|Any CPU
115+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|Any CPU.Build.0 = Release|Any CPU
116+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|ARM.ActiveCfg = Release|Any CPU
117+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|ARM.Build.0 = Release|Any CPU
118+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|x64.ActiveCfg = Release|Any CPU
119+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|x64.Build.0 = Release|Any CPU
120+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|x86.ActiveCfg = Release|Any CPU
121+
{A7A7E073-2504-4BA2-A63B-87AC34174789}.Release|x86.Build.0 = Release|Any CPU
104122
EndGlobalSection
105123
GlobalSection(SolutionProperties) = preSolution
106124
HideSolutionNode = FALSE

QRCoderTests/Helpers/HelperFunctions.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using System.Text;
53
using System.IO;
64
using System.Security.Cryptography;
75
#if !NETCOREAPP1_1
86
using System.Drawing;
97
#endif
8+
#if NETFRAMEWORK || NET5_0_WINDOWS
9+
using SW = System.Windows;
10+
using System.Windows.Media;
11+
using System.Windows.Media.Imaging;
12+
#endif
13+
1014

1115
namespace QRCoderTests.Helpers
1216
{
1317
public static class HelperFunctions
1418
{
1519

20+
#if NETFRAMEWORK || NET5_0_WINDOWS
21+
public static BitmapSource ToBitmapSource(DrawingImage source)
22+
{
23+
DrawingVisual drawingVisual = new DrawingVisual();
24+
DrawingContext drawingContext = drawingVisual.RenderOpen();
25+
drawingContext.DrawImage(source, new SW.Rect(new SW.Point(0, 0), new SW.Size(source.Width, source.Height)));
26+
drawingContext.Close();
27+
28+
RenderTargetBitmap bmp = new RenderTargetBitmap((int)source.Width, (int)source.Height, 96, 96, PixelFormats.Pbgra32);
29+
bmp.Render(drawingVisual);
30+
return bmp;
31+
}
32+
33+
public static Bitmap BitmapSourceToBitmap(DrawingImage xamlImg)
34+
{
35+
using (MemoryStream ms = new MemoryStream())
36+
{
37+
PngBitmapEncoder encoder = new PngBitmapEncoder();
38+
encoder.Frames.Add(BitmapFrame.Create(ToBitmapSource(xamlImg)));
39+
encoder.Save(ms);
40+
41+
using (Bitmap bmp = new Bitmap(ms))
42+
{
43+
return new Bitmap(bmp);
44+
}
45+
}
46+
}
47+
#endif
48+
1649
#if !NETCOREAPP1_1
1750
public static string GetAssemblyPath()
1851
{
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#if NETFRAMEWORK || NET5_0_WINDOWS || NET6_0_WINDOWS
2+
using Xunit;
3+
using QRCoder;
4+
using QRCoder.Xaml;
5+
using Shouldly;
6+
using QRCoderTests.Helpers.XUnitExtenstions;
7+
using QRCoderTests.Helpers;
8+
9+
namespace QRCoderTests
10+
{
11+
12+
public class XamlQRCodeRendererTests
13+
{
14+
15+
[Fact]
16+
[Category("QRRenderer/XamlQRCode")]
17+
public void can_create_xaml_qrcode_standard_graphic()
18+
{
19+
var gen = new QRCodeGenerator();
20+
var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.H);
21+
var xCode = new XamlQRCode(data).GetGraphic(10);
22+
23+
var bmp = HelperFunctions.BitmapSourceToBitmap(xCode);
24+
var result = HelperFunctions.BitmapToHash(bmp);
25+
result.ShouldBe("e8c61b8f0455924fe08ba68686d0d296");
26+
}
27+
28+
29+
[Fact]
30+
[Category("QRRenderer/XamlQRCode")]
31+
public void can_instantate_qrcode_parameterless()
32+
{
33+
var svgCode = new XamlQRCode();
34+
svgCode.ShouldNotBeNull();
35+
svgCode.ShouldBeOfType<XamlQRCode>();
36+
}
37+
38+
/*
39+
[Fact]
40+
[Category("QRRenderer/XamlQRCode")]
41+
public void can_render_qrcode_from_helper()
42+
{
43+
//Create QR code
44+
var bmp = QRCodeHelper.GetQRCode("This is a quick test! 123#?", 10, Color.Black, Color.White, QRCodeGenerator.ECCLevel.H);
45+
46+
var result = HelperFunctions.BitmapToHash(bmp);
47+
result.ShouldBe("e8c61b8f0455924fe08ba68686d0d296");
48+
}
49+
*/
50+
}
51+
}
52+
#endif

0 commit comments

Comments
 (0)