1
1
using Codeuctivity . HtmlRenderer ;
2
2
using Codeuctivity . HtmlRendererTests . Infrastructure ;
3
3
using Codeuctivity . PdfjsSharp ;
4
+ using Jering . Javascript . NodeJS ;
4
5
using PuppeteerSharp ;
5
6
using System ;
6
7
using System . IO ;
11
12
12
13
namespace Codeuctivity . HtmlRendererTests
13
14
{
14
- public class RendererTests
15
+ public class RendererTests : IDisposable
15
16
{
17
+ private bool disposedValue ;
18
+
19
+ public RendererTests ( )
20
+ {
21
+ Rasterize = new Rasterizer ( ) ;
22
+ }
23
+
24
+ public Rasterizer Rasterize { get ; private set ; }
25
+
16
26
[ Theory ]
17
27
[ InlineData ( "BasicTextFormated.html" ) ]
18
28
public async Task ShouldConvertHtmlToPdf ( string testFileName )
@@ -32,11 +42,9 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
32
42
33
43
var actualImagePathDirectory = Path . Combine ( Path . GetTempPath ( ) , testFileName ) ;
34
44
35
- using var rasterize = new Rasterizer ( ) ;
36
-
37
45
if ( ! IsRunningOnWslOrAzureOrMacos ( ) )
38
46
{
39
- var actualImages = await rasterize . ConvertToPngAsync ( actualFilePath , actualImagePathDirectory ) ;
47
+ var actualImages = await Rasterize . ConvertToPngAsync ( actualFilePath , actualImagePathDirectory ) ;
40
48
Assert . Single ( actualImages ) ;
41
49
DocumentAsserter . AssertImageIsEqual ( actualImages . Single ( ) , expectReferenceFilePath , 2000 ) ;
42
50
}
@@ -45,6 +53,46 @@ public async Task ShouldConvertHtmlToPdf(string testFileName)
45
53
await ChromiumProcessDisposedAsserter . AssertNoChromiumProcessIsRunning ( ) ;
46
54
}
47
55
56
+ [ Theory ]
57
+ [ InlineData ( "BasicTextFormatedInlineBackground.html" , false , 6000 ) ]
58
+ [ InlineData ( "BasicTextFormatedInlineBackground.html" , true , 6000 ) ]
59
+ public async Task ShouldConvertHtmlToPdfWithOptions ( string testFileName , bool printBackground , int allowedPixelDiff )
60
+ {
61
+ var sourceHtmlFilePath = $ "../../../TestInput/{ testFileName } ";
62
+ var actualFilePath = Path . Combine ( Path . GetTempPath ( ) , $ "ActualConvertHtmlToPdf{ testFileName } .{ printBackground } .pdf") ;
63
+ var expectReferenceFilePath = $ "../../../ExpectedTestOutcome/ExpectedFromHtmlConvertHtmlToPdf{ testFileName } .{ printBackground } .png";
64
+
65
+ if ( File . Exists ( actualFilePath ) )
66
+ {
67
+ File . Delete ( actualFilePath ) ;
68
+ }
69
+
70
+ await using ( var chromiumRenderer = await Renderer . CreateAsync ( ) )
71
+ {
72
+ await chromiumRenderer . ConvertHtmlToPdf ( sourceHtmlFilePath , actualFilePath , new PdfOptions ( ) { PrintBackground = printBackground } ) ;
73
+
74
+ var actualImagePathDirectory = Path . Combine ( Path . GetTempPath ( ) , testFileName ) ;
75
+
76
+ if ( ! IsRunningOnWslOrAzureOrMacos ( ) )
77
+ {
78
+ try
79
+ {
80
+ var actualImages = await Rasterize . ConvertToPngAsync ( actualFilePath , actualImagePathDirectory ) ;
81
+ Assert . Single ( actualImages ) ;
82
+ // File.Copy(actualImages.Single(), expectReferenceFilePath, true);
83
+ DocumentAsserter . AssertImageIsEqual ( actualImages . Single ( ) , expectReferenceFilePath , allowedPixelDiff ) ;
84
+ }
85
+ catch ( InvocationException ex )
86
+ {
87
+ // Working around issue in Jering.Javascript.NodeJS, silencing false positiv failing
88
+ Assert . True ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) , ex . Message ) ;
89
+ }
90
+ }
91
+ File . Delete ( actualFilePath ) ;
92
+ }
93
+ await ChromiumProcessDisposedAsserter . AssertNoChromiumProcessIsRunning ( ) ;
94
+ }
95
+
48
96
private static bool IsRunningOnWslOrAzureOrMacos ( )
49
97
{
50
98
if ( RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) || RuntimeInformation . IsOSPlatform ( OSPlatform . FreeBSD ) )
@@ -88,6 +136,36 @@ public async Task ShouldConvertHtmlToPng(string testFileName)
88
136
await ChromiumProcessDisposedAsserter . AssertNoChromiumProcessIsRunning ( ) ;
89
137
}
90
138
139
+ [ Theory ]
140
+ [ InlineData ( "BasicTextFormatedInlineBackground.html" , false , 11000 ) ]
141
+ [ InlineData ( "BasicTextFormatedInlineBackground.html" , true , 9500 ) ]
142
+ public async Task ShouldConvertHtmlToPngScreenshotOptions ( string testFileName , bool omitBackground , int allowedPixelDiff )
143
+ {
144
+ var sourceHtmlFilePath = $ "../../../TestInput/{ testFileName } ";
145
+ var actualFilePath = Path . Combine ( Path . GetTempPath ( ) , $ "ActualConvertHtmlToPng{ testFileName } .{ omitBackground } .png") ;
146
+ var expectReferenceFilePath = $ "../../../ExpectedTestOutcome/ExpectedConvertHtmlToPng{ testFileName } .{ omitBackground } .png";
147
+
148
+ if ( File . Exists ( actualFilePath ) )
149
+ {
150
+ File . Delete ( actualFilePath ) ;
151
+ }
152
+
153
+ await using ( var chromiumRenderer = await Renderer . CreateAsync ( ) )
154
+ {
155
+ ScreenshotOptions screenshotOptions = new ScreenshotOptions
156
+ {
157
+ OmitBackground = omitBackground
158
+ } ;
159
+
160
+ await chromiumRenderer . ConvertHtmlToPng ( sourceHtmlFilePath , actualFilePath , screenshotOptions ) ;
161
+ // File.Copy(actualFilePath, expectReferenceFilePath);
162
+ DocumentAsserter . AssertImageIsEqual ( actualFilePath , expectReferenceFilePath , allowedPixelDiff ) ;
163
+ }
164
+
165
+ File . Delete ( actualFilePath ) ;
166
+ await ChromiumProcessDisposedAsserter . AssertNoChromiumProcessIsRunning ( ) ;
167
+ }
168
+
91
169
[ Fact ]
92
170
public async Task ShouldDisposeGracefull ( )
93
171
{
@@ -124,5 +202,24 @@ public async Task ShouldConvertHtmlToPngNoSandbox(string testFileName)
124
202
File . Delete ( actualFilePath ) ;
125
203
await ChromiumProcessDisposedAsserter . AssertNoChromiumProcessIsRunning ( ) ;
126
204
}
205
+
206
+ protected virtual void Dispose ( bool disposing )
207
+ {
208
+ if ( ! disposedValue )
209
+ {
210
+ if ( disposing )
211
+ {
212
+ Rasterize ? . Dispose ( ) ;
213
+ }
214
+
215
+ disposedValue = true ;
216
+ }
217
+ }
218
+
219
+ public void Dispose ( )
220
+ {
221
+ Dispose ( disposing : true ) ;
222
+ GC . SuppressFinalize ( this ) ;
223
+ }
127
224
}
128
225
}
0 commit comments