-
Notifications
You must be signed in to change notification settings - Fork 84
/
PdfConverterTests.cs
234 lines (196 loc) · 6.54 KB
/
PdfConverterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using NUnit.Framework;
using WkHtmlToXSharp.Tests.Properties;
namespace WkHtmlToXSharp.Tests
{
[TestFixture]
public class PdfConverterTest
{
private static readonly global::Common.Logging.ILog _Log = global::Common.Logging.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static string SimplePageFile = null;
public static int count = 0;
private void TryRegisterLibraryBundles()
{
var ignore = Environment.GetEnvironmentVariable("WKHTMLTOXSHARP_NOBUNDLES");
if (ignore == null || ignore.ToLower() != "true")
{
// Register all available bundles..
WkHtmlToXLibrariesManager.Register(new Linux32NativeBundle());
WkHtmlToXLibrariesManager.Register(new Linux64NativeBundle());
WkHtmlToXLibrariesManager.Register(new Win32NativeBundle());
WkHtmlToXLibrariesManager.Register(new Win64NativeBundle());
}
}
[TestFixtureSetUp]
public void FixtureSetup()
{
TryRegisterLibraryBundles();
// Avoid using Temp folder as WebKit doesnt like it.
SimplePageFile = Path.GetTempFileName();
File.Delete(SimplePageFile);
SimplePageFile = Path.Combine(
Path.GetDirectoryName(SimplePageFile),
Path.GetFileNameWithoutExtension(SimplePageFile)+ ".xhtml");
File.WriteAllBytes(SimplePageFile, Resources.SimplePage_xhtml);
}
//[TestFixtureTearDown]
public void FixtureTearDown()
{
if (File.Exists(SimplePageFile))
File.Delete(SimplePageFile);
}
private MultiplexingConverter _GetConverter()
{
var obj = new MultiplexingConverter();
obj.Begin += (s,e) => _Log.DebugFormat("Conversion begin, phase count: {0}", e.Value);
//obj.Error += (s, e) => _Log.Error(e.Value);
obj.Warning += (s, e) => _Log.Warn(e.Value);
//obj.PhaseChanged += (s, e) => _Log.InfoFormat("PhaseChanged: {0} - {1}", e.Value, e.Value2);
//obj.ProgressChanged += (s, e) => _Log.InfoFormat("ProgressChanged: {0} - {1}", e.Value, e.Value2);
obj.Finished += (s, e) => _Log.InfoFormat("Finished: {0}", e.Value ? "success" : "failed!");
return obj;
}
private void _SimpleConversion()
{
using (var wk = _GetConverter())
{
_Log.DebugFormat("Performing conversion..");
wk.GlobalSettings.Margin.Top = "0cm";
wk.GlobalSettings.Margin.Bottom = "0cm";
wk.GlobalSettings.Margin.Left = "0cm";
wk.GlobalSettings.Margin.Right = "0cm";
//wk.GlobalSettings.Out = @"c:\temp\tmp.pdf";
wk.ObjectSettings.Web.EnablePlugins = false;
wk.ObjectSettings.Web.EnableJavascript = false;
wk.ObjectSettings.Page = SimplePageFile;
//wk.ObjectSettings.Page = "http://doc.trolltech.com/4.6/qstring.html";
wk.ObjectSettings.Load.Proxy = "none";
var tmp = wk.Convert();
Assert.IsNotEmpty(tmp);
var number = 0;
lock (this) number = count++;
var tempfname = Path.GetTempFileName();
var tempPath = Path.Combine(
Path.GetDirectoryName(tempfname),
Path.GetFileNameWithoutExtension(tempfname) + "." + (number) + ".pdf"
);
File.WriteAllBytes(tempPath, tmp);
}
}
[Test]
public void CanConvertFromFile()
{
_SimpleConversion();
}
#region Testing Threads handling
class ThreadData
{
public Thread Thread;
public Exception Exception;
public ManualResetEvent WaitHandle;
}
void ThreadStart(object arg)
{
_Log.DebugFormat("New thread {0}", arg);
var tmp = arg as ThreadData;
try
{
_SimpleConversion();
}
catch (Exception ex)
{
tmp.Exception = ex;
}
finally
{
tmp.WaitHandle.Set();
}
}
#endregion
private const int ConcurrentTimeout = 95000;
[Test]
[Timeout(ConcurrentTimeout)]
[RequiresThread(ApartmentState.MTA)]
public void CanConvertConcurrently()
{
var error = false;
var threads = new List<ThreadData>();
for (int i = 0; i < 8; i++)
{
var tmp = new ThreadData()
{
Thread = new Thread(ThreadStart),
WaitHandle = new ManualResetEvent(false)
};
threads.Add(tmp);
tmp.Thread.Start(tmp);
}
var handles = threads.Select(x => x.WaitHandle).ToArray();
Assert.IsTrue(WaitHandle.WaitAll(handles, ConcurrentTimeout), "At least one thread timeout");
//WaitAll(handles);
threads.ForEach(x => x.Thread.Abort());
var exceptions = threads.Select(x => x.Exception).Where(x => x != null);
foreach (var tmp in threads)
{
if (tmp.Exception != null)
{
error = true;
var tid = tmp.Thread.ManagedThreadId;
_Log.Error("Thread-" + tid + " failed!", tmp.Exception);
}
}
Assert.IsFalse(error, "At least one thread failed!");
}
[Test]
public void ConvertFromString()
{
using (var wk = _GetConverter())
{
_Log.DebugFormat("Performing conversion..");
using (var stream = new MemoryStream(Resources.SimplePage_xhtml))
using (var sr = new StreamReader(stream))
{
var str = sr.ReadToEnd();
var tmp = wk.Convert(str);
Assert.IsNotEmpty(tmp);
}
}
}
[Test]
// [Ignore("This test requires (still to be released) wkhtmltopdf v0.12+")]
public void CanHandleAuthFailure()
{
using (var wk = new MultiplexingConverter())
{
var failed = false;
wk.GlobalSettings.Margin.Top = "0cm";
wk.GlobalSettings.Margin.Bottom = "0cm";
wk.GlobalSettings.Margin.Left = "0cm";
wk.GlobalSettings.Margin.Right = "0cm";
wk.ObjectSettings.Load.Proxy = "none";
wk.ObjectSettings.Load.LoadErrorHandling = LoadErrorHandlingType.abort;
wk.ObjectSettings.Load.StopSlowScripts = true;
wk.ObjectSettings.Web.EnablePlugins = false;
wk.ObjectSettings.Web.EnableJavascript = false;
wk.ObjectSettings.Page = @"https://httpbin.org/basic-auth/user/passwd"; // Some misg site requiring HTTP Basic auth.
wk.Begin += (s, e) => { Console.WriteLine("==>> Begin: {0}", e.Value); };
wk.PhaseChanged += (s, e) => { Console.WriteLine("==>> New Phase: {0} ({1})", e.Value, e.Value2); };
wk.ProgressChanged += (s, e) => { Console.WriteLine("==>> Progress: {0} ({1})", e.Value, e.Value2); };
wk.Error += (s, e) => {
failed = true;
Console.WriteLine("==>> ERROR: {0}", e.Value);
};
wk.Finished += (s, e) => { Console.WriteLine("==>> WARN: {0}", e.Value); };
var tmp = wk.Convert();
Assert.IsNotNull(tmp);
Assert.IsTrue(failed);
}
}
}
}