|
| 1 | +using Xunit; |
| 2 | +using QRCoder; |
| 3 | +using Shouldly; |
| 4 | +using QRCoderTests.Helpers.XUnitExtenstions; |
| 5 | +using QRCoderTests.Helpers; |
| 6 | +#if !NETCOREAPP1_1 |
| 7 | +using System.Drawing; |
| 8 | +using System.IO; |
| 9 | +#endif |
| 10 | + |
| 11 | +namespace QRCoderTests |
| 12 | +{ |
| 13 | + /**************************************************************************************************** |
| 14 | + * Note: Test cases compare the outcome visually even if it's slower than a byte-wise compare. |
| 15 | + * This is necessary, because the Deflate implementation differs on the different target |
| 16 | + * platforms and thus the outcome, even if visually identical, differs. Thus only a visual |
| 17 | + * test method makes sense. In addition bytewise differences shouldn't be important, if the |
| 18 | + * visual outcome is identical and thus the qr code is identical/scannable. |
| 19 | + ****************************************************************************************************/ |
| 20 | + public class PngByteQRCodeRendererTests |
| 21 | + { |
| 22 | + |
| 23 | + |
| 24 | + [Fact] |
| 25 | + [Category("QRRenderer/PngByteQRCode")] |
| 26 | + public void can_render_pngbyte_qrcode_blackwhite() |
| 27 | + { |
| 28 | + //Create QR code |
| 29 | + var gen = new QRCodeGenerator(); |
| 30 | + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); |
| 31 | + var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5); |
| 32 | + |
| 33 | +#if NETCOREAPP1_1 |
| 34 | + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); |
| 35 | + result.ShouldBe("1fc35c3bea6fad47427143ce716c83b8"); |
| 36 | +#else |
| 37 | + using (var mStream = new MemoryStream(pngCodeGfx)) |
| 38 | + { |
| 39 | + var bmp = (Bitmap)Image.FromStream(mStream); |
| 40 | + var result = HelperFunctions.BitmapToHash(bmp); |
| 41 | + result.ShouldBe("18b19e6037cff06ae995d8d487b0e46e"); |
| 42 | + } |
| 43 | +#endif |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + [Category("QRRenderer/PngByteQRCode")] |
| 48 | + public void can_render_pngbyte_qrcode_color() |
| 49 | + { |
| 50 | + //Create QR code |
| 51 | + var gen = new QRCodeGenerator(); |
| 52 | + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); |
| 53 | + var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 0, 0 }, new byte[] { 0, 0, 255 }); |
| 54 | + |
| 55 | +#if NETCOREAPP1_1 |
| 56 | + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); |
| 57 | + result.ShouldBe("0144b1d40aa6eeb6cb07df42822ea0a7"); |
| 58 | +#else |
| 59 | + using (var mStream = new MemoryStream(pngCodeGfx)) |
| 60 | + { |
| 61 | + var bmp = (Bitmap)Image.FromStream(mStream); |
| 62 | + var result = HelperFunctions.BitmapToHash(bmp); |
| 63 | + result.ShouldBe("37ae73e90b66beac317b790be3db24cc"); |
| 64 | + } |
| 65 | +#endif |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + [Fact] |
| 70 | + [Category("QRRenderer/PngByteQRCode")] |
| 71 | + public void can_render_pngbyte_qrcode_color_with_alpha() |
| 72 | + { |
| 73 | + //Create QR code |
| 74 | + var gen = new QRCodeGenerator(); |
| 75 | + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); |
| 76 | + var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 }); |
| 77 | + |
| 78 | +#if NETCOREAPP1_1 |
| 79 | + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); |
| 80 | + result.ShouldBe("627ce564fb5e17be42e4a85e907a17b5"); |
| 81 | +#else |
| 82 | + using (var mStream = new MemoryStream(pngCodeGfx)) |
| 83 | + { |
| 84 | + var bmp = (Bitmap)Image.FromStream(mStream); |
| 85 | + var result = HelperFunctions.BitmapToHash(bmp); |
| 86 | + result.ShouldBe("c56c2a9535fd8e9a92a6ac9709d21e67"); |
| 87 | + } |
| 88 | +#endif |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + [Category("QRRenderer/PngByteQRCode")] |
| 93 | + public void can_render_pngbyte_qrcode_color_without_quietzones() |
| 94 | + { |
| 95 | + //Create QR code |
| 96 | + var gen = new QRCodeGenerator(); |
| 97 | + var data = gen.CreateQrCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L); |
| 98 | + var pngCodeGfx = new PngByteQRCode(data).GetGraphic(5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 }, false); |
| 99 | + |
| 100 | +#if NETCOREAPP1_1 |
| 101 | + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); |
| 102 | + result.ShouldBe("07f760b3eb54901840b094d31e299713"); |
| 103 | +#else |
| 104 | + File.WriteAllBytes(@"C:\Temp\pngbyte_35.png", pngCodeGfx); |
| 105 | + using (var mStream = new MemoryStream(pngCodeGfx)) |
| 106 | + { |
| 107 | + var bmp = (Bitmap)Image.FromStream(mStream); |
| 108 | + bmp.MakeTransparent(Color.Transparent); |
| 109 | + var result = HelperFunctions.BitmapToHash(bmp); |
| 110 | +#if NET35_OR_GREATER || NET40_OR_GREATER |
| 111 | + result.ShouldBe("75be11d582575617d2490c54b69e844e"); |
| 112 | +#else |
| 113 | + result.ShouldBe("fbbc8255ebf3e4f4a1d21f0dd15f76f8"); |
| 114 | +#endif |
| 115 | + } |
| 116 | +#endif |
| 117 | + } |
| 118 | + |
| 119 | + [Fact] |
| 120 | + [Category("QRRenderer/PngByteQRCode")] |
| 121 | + public void can_instantate_pngbyte_qrcode_parameterless() |
| 122 | + { |
| 123 | + var pngCode = new PngByteQRCode(); |
| 124 | + pngCode.ShouldNotBeNull(); |
| 125 | + pngCode.ShouldBeOfType<PngByteQRCode>(); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + [Category("QRRenderer/PngByteQRCode")] |
| 130 | + public void can_render_pngbyte_qrcode_from_helper() |
| 131 | + { |
| 132 | + //Create QR code |
| 133 | + var pngCodeGfx = PngByteQRCodeHelper.GetQRCode("This is a quick test! 123#?", QRCodeGenerator.ECCLevel.L, 10); |
| 134 | + |
| 135 | +#if NETCOREAPP1_1 |
| 136 | + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); |
| 137 | + result.ShouldBe("c562388f4f3cf13a299b469a3e3b852f"); |
| 138 | +#else |
| 139 | + using (var mStream = new MemoryStream(pngCodeGfx)) |
| 140 | + { |
| 141 | + var bmp = (Bitmap)Image.FromStream(mStream); |
| 142 | + var result = HelperFunctions.BitmapToHash(bmp); |
| 143 | + result.ShouldBe("1978fb11ce26acf9b6cb7490b4c44ef2"); |
| 144 | + } |
| 145 | +#endif |
| 146 | + } |
| 147 | + |
| 148 | + [Fact] |
| 149 | + [Category("QRRenderer/PngByteQRCode")] |
| 150 | + public void can_render_pngbyte_qrcode_from_helper_2() |
| 151 | + { |
| 152 | + //Create QR code |
| 153 | + var pngCodeGfx = PngByteQRCodeHelper.GetQRCode("This is a quick test! 123#?", 5, new byte[] { 255, 255, 255, 127 }, new byte[] { 0, 0, 255 }, QRCodeGenerator.ECCLevel.L); |
| 154 | + |
| 155 | +#if NETCOREAPP1_1 |
| 156 | + var result = HelperFunctions.ByteArrayToHash(pngCodeGfx); |
| 157 | + result.ShouldBe("627ce564fb5e17be42e4a85e907a17b5"); |
| 158 | +#else |
| 159 | + using (var mStream = new MemoryStream(pngCodeGfx)) |
| 160 | + { |
| 161 | + var bmp = (Bitmap)Image.FromStream(mStream); |
| 162 | + var result = HelperFunctions.BitmapToHash(bmp); |
| 163 | + result.ShouldBe("c56c2a9535fd8e9a92a6ac9709d21e67"); |
| 164 | + } |
| 165 | +#endif |
| 166 | + } |
| 167 | + |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | + |
0 commit comments