1# Before `make install' is performed this script should be runnable with
2# `make test'. After `make install' it should work as `perl test.pl'
3
4#########################
5
6# change 'tests => 1' to 'tests => last_test_to_print';
7
8use Test;
9BEGIN { plan tests => 121 };
10use strict;
11use lib 'lib';
12use HTML::HTMLDoc;
13ok(1); # If we made it this far, we're ok.
14
15#########################
16
17# Insert your test code below, the Test module is use()ed here so read
18# its man page ( perldoc Test ) for help writing this test script.
19
20{
21	# test 2 - constructor
22	my $htmldoc = new HTML::HTMLDoc();
23	ok(ref($htmldoc), qr/HTML::HTMLDoc/);
24
25	# test 3 - check mode of instance
26	ok($htmldoc->_config('mode'), 'ipc');
27
28	# test 4 - test internal method for checking allowed
29	#			parameters
30	ok($htmldoc->_test_params('asdf', ['jhgjhg', 'trztr', 'asdf', 'jhkjh']), 1 );
31
32	# test 5 - same with wrong input
33	ok($htmldoc->_test_params('asdf', ['jhgjhg', 'trztr', '1asdf', 'jhkjh']), 0 );
34
35	# test 6 - internal method for storing settings
36	#		    string mode
37	$htmldoc->_set_doc_config('testkey', 'testvalue');
38	ok($htmldoc->_get_doc_config('testkey'), 'testvalue');
39
40	# test 7 - delete this config-entry
41	$htmldoc->_delete_doc_config('testkey');
42	ok($htmldoc->_get_doc_config('testkey'), undef);
43
44	# test 8 - set an array
45	$htmldoc->_set_doc_config('testkey', ['one', 'two', 'tree']);
46	ok($htmldoc->_get_doc_config('testkey')->[1], 'two' );
47
48	# test 9 - delete it again
49	$htmldoc->_delete_doc_config('testkey');
50	ok($htmldoc->_get_doc_config('testkey'), undef);
51
52	# test 10 - set page size a4
53	$htmldoc->set_page_size('a4');
54	ok($htmldoc->get_page_size(), 'a4');
55
56	# test 11 - set page size letter
57	$htmldoc->set_page_size('letter');
58	ok($htmldoc->get_page_size(), 'letter');
59
60	# test 12 - set page size 10x10cm
61	$htmldoc->set_page_size('10x10cm');
62	ok($htmldoc->get_page_size(), '10x10cm');
63
64	# test 13 - Owner password
65	$htmldoc->set_owner_password('secure');
66	ok($htmldoc->_get_doc_config('owner-password'), 'secure');
67
68	# test 14 - User password
69	$htmldoc->set_user_password('secure');
70	ok($htmldoc->_get_doc_config('user-password'), 'secure');
71
72	# test 15 - permissions - all
73	$htmldoc->set_permissions('all');
74	ok($htmldoc->_get_doc_config('permissions')->[0], 'all');
75
76	# test 16 - permissions - none
77	$htmldoc->set_permissions('none');
78	ok($htmldoc->_get_doc_config('permissions')->[0], 'none');
79
80	# clean up permissions
81	$htmldoc->_delete_doc_config('permissions');
82
83	# test  - JPEG-quality
84	$htmldoc->set_jpeg_compression();
85	ok($htmldoc->_get_doc_config('jpeg'), "75");
86	$htmldoc->set_jpeg_compression(0);
87	ok($htmldoc->_get_doc_config('jpeg'), 0);
88	$htmldoc->set_jpeg_compression(100);
89	ok($htmldoc->_get_doc_config('jpeg'), 100);
90
91	$htmldoc->_delete_doc_config('jpeg');
92	$htmldoc->best_image_quality();
93	ok($htmldoc->_get_doc_config('jpeg'), 100);
94
95	$htmldoc->_delete_doc_config('jpeg');
96	$htmldoc->low_image_quality();
97	ok($htmldoc->_get_doc_config('jpeg'), 25);
98
99	# test 17 - permissions - nos
100	my $okcounter = 0;
101	my @noperms = ('no-annotate', 'no-copy', 'no-modify',  'no-print');
102	foreach my $perm( @noperms ) {
103			$htmldoc->set_permissions($perm);
104	}
105
106	my $stored = $htmldoc->_get_doc_config('permissions');
107	for(my $i=0; $i<@$stored; $i++) {
108			$okcounter++ if ($stored->[$i] eq @noperms[$i]);
109	}
110
111	ok($okcounter, 4);
112
113	# clean up permissions
114	$htmldoc->_delete_doc_config('permissions');
115
116	# test 18 - permissions - yes
117	my $okcounter = 0;
118	my @noperms = ('annotate', 'copy', 'modify',  'print');
119	foreach my $perm( @noperms ) {
120			$htmldoc->set_permissions($perm);
121	}
122
123	my $stored = $htmldoc->_get_doc_config('permissions');
124	for(my $i=0; $i<@$stored; $i++) {
125			$okcounter++ if ($stored->[$i] eq @noperms[$i]);
126	}
127
128	ok($okcounter, 4);
129
130
131	# test 19 - permissions - none again to test if the set are deleted
132	$htmldoc->set_permissions('none');
133	ok($htmldoc->_get_doc_config('permissions')->[0], 'none');
134
135	# test 20 - permissions - corresponding flag deleted
136	$htmldoc->_delete_doc_config('permissions');
137	$htmldoc->set_permissions('copy');
138	$htmldoc->set_permissions('no-copy');
139	my $set = $htmldoc->_get_doc_config('permissions');
140	my $found = 0;
141	foreach (@$set) {
142		$found = 1 if ($_ eq 'copy');
143	}
144	ok($found, 0);
145
146	# test 21 - permissions - corresponding flag deleted
147	$htmldoc->_delete_doc_config('permissions');
148	$htmldoc->set_permissions('no-copy');
149	$htmldoc->set_permissions('copy');
150	my $set = $htmldoc->_get_doc_config('permissions');
151	my $found = 0;
152	foreach (@$set) {
153		$found = 1 if ($_ eq 'no-copy');
154	}
155	ok($found, 0);
156
157
158	# test 22,23 - landscape
159	$htmldoc->landscape();
160	my @keys = $htmldoc->_get_doc_config_keys();
161	ok(array_contains(\@keys, 'landscape'), 1 );
162	ok(array_contains(\@keys, 'portrait'), 0 );
163
164	# test 24,25 - landscape
165	$htmldoc->portrait();
166	my @keys = $htmldoc->_get_doc_config_keys();
167	ok(array_contains(\@keys, 'portrait'), 1 );
168	ok(array_contains(\@keys, 'landscape'), 0 );
169
170
171	# test 26, 27 - right margin without messure
172	my $ret = $htmldoc->set_right_margin(2);
173	ok($ret, 1);
174	ok($htmldoc->_get_doc_config('right'), '2cm' );
175
176	# test 26, 27 - right margin without messure
177	my $ret = $htmldoc->set_right_margin(2.1);
178	ok($ret, 1);
179	ok($htmldoc->_get_doc_config('right'), '2.1cm' );
180
181	# test 28,29 - right margin mm
182	my $ret = $htmldoc->set_right_margin(2, 'mm');
183	ok($ret, 1);
184	ok($htmldoc->_get_doc_config('right'), '2mm' );
185
186	# test 30,31 - right margin in
187	my $ret = $htmldoc->set_right_margin(2, 'in');
188	ok($ret, 1);
189	ok($htmldoc->_get_doc_config('right'), '2in' );
190
191	# test 32 - right margin to wrong value
192	my $ret = $htmldoc->set_right_margin(2, 'mc');
193	ok( (!$ret && $htmldoc->error()=~/wrong arguments/ && $htmldoc->error()=~/right-margin/), 1 );
194
195
196	# test 33 - left margin without messure
197	$htmldoc->set_left_margin(2);
198	ok($htmldoc->_get_doc_config('left'), '2cm' );
199
200	# test 34 - top margin without messure
201	$htmldoc->set_top_margin(2);
202	ok($htmldoc->_get_doc_config('top'), '2cm' );
203
204	# test 35 - bottom margin without messure
205	$htmldoc->set_bottom_margin(2);
206	ok($htmldoc->_get_doc_config('bottom'), '2cm' );
207
208	# test 36 - set_bodycolor
209	$htmldoc->set_bottom_margin(2);
210	ok($htmldoc->_get_doc_config('bottom'), '2cm' );
211
212	# test 37, 38 - test colors hex
213	my $c = $htmldoc->_test_color('#FF00DD');
214	ok($c, '#FF00DD');
215	my $c = $htmldoc->_test_color('#FJ00DD');
216	ok($c, undef);
217
218	# test 39,40,41,42,43 - test colors hex
219	my $c = $htmldoc->_test_color(0,0,0);
220	ok($c, '#000000');
221	my $c = $htmldoc->_test_color(255,255,255);
222	ok($c, '#ffffff');
223	my $c = $htmldoc->_test_color(256,255,255);
224	ok($c, undef);
225
226	my $c = $htmldoc->_test_color('red');
227	ok($c, 'red');
228
229	my $c = $htmldoc->_test_color('violette');
230	ok($c, undef);
231
232
233	# 44-47 setting bodycolor
234	ok($htmldoc->set_bodycolor('red'), 1);
235	ok($htmldoc->set_bodycolor('#010101'), 1);
236	ok($htmldoc->set_bodycolor('#0101011'), 0);
237	ok($htmldoc->set_bodycolor(0,0,0), 1);
238
239	# 48-52 - setting font
240	ok($htmldoc->set_bodyfont('Arial'), 1);
241	ok($htmldoc->_get_doc_config('bodyfont'), 'Arial');
242	ok($htmldoc->set_bodyfont('arial'), 1);
243	ok($htmldoc->set_bodyfont('sans-serif'), 1);
244	ok($htmldoc->set_bodyfont('arialx'), 0);
245
246	# 52 - browser-width
247	$htmldoc->set_browserwidth(100);
248	ok($htmldoc->_get_doc_config('browserwidth'), 100);
249	ok($htmldoc->set_browserwidth("sad"), 0);
250
251	# 54-56 - embed fonts
252	$htmldoc->embed_fonts();
253	my @keys = $htmldoc->_get_doc_config_keys();
254	ok(array_contains(\@keys, 'embedfonts'), 1);
255
256	$htmldoc->no_embed_fonts();
257	my @keys = $htmldoc->_get_doc_config_keys();
258	ok(array_contains(\@keys, 'embedfonts'), 0);
259
260	# 58,59 - default header and footer
261	ok($htmldoc->_get_doc_config('header'), '.t.' );
262	ok($htmldoc->_get_doc_config('footer'), '.1.' );
263
264	#  60, 61 - set special footer
265	ok($htmldoc->set_footer('/', 'a', 'A'), 1);
266	ok($htmldoc->_get_doc_config('footer'), '/aA' );
267
268	# 62 - set wrong footer
269	ok($htmldoc->set_footer('7', '&', 'x'), 0);
270
271	#  64, 65 - set special header
272	ok($htmldoc->set_header('/', 'a', 'A'), 1);
273	ok($htmldoc->_get_doc_config('header'), '/aA' );
274
275	# 62 - set wrong header
276	ok($htmldoc->set_header('7', '&', 'x'), 0);
277
278	# 63 - default outputformat
279	ok($htmldoc->_get_doc_config('format'), 'pdf');
280
281	# 66, 67 - special output-format
282	ok($htmldoc->set_output_format('html'), 1);
283	ok($htmldoc->_get_doc_config('format'), 'html');
284	$htmldoc->set_output_format('pdf');
285
286	# 68, 69 wrong output format
287	ok($htmldoc->set_output_format('asdasd'), 0);
288	ok($htmldoc->_get_doc_config('format'), 'pdf');
289
290	# 70, 71 set html
291	my $content = '<html>test</html>';
292	ok($htmldoc->set_html_content($content), 1);
293	ok($htmldoc->get_html_content(), $content);
294
295	# 70, 71 set html as ref
296	my $content = '<html>test</html>';
297	ok($htmldoc->set_html_content(\$content), 1);
298	ok($htmldoc->get_html_content(), $content);
299
300	# turn links on
301	ok($htmldoc->links(), 1);
302	ok($htmldoc->_get_doc_config('links'), '');
303	ok($htmldoc->_get_doc_config('no-links'), undef);
304
305	# turn links off
306	ok($htmldoc->no_links(), 1);
307	ok($htmldoc->_get_doc_config('no-links'), '');
308	ok($htmldoc->_get_doc_config('links'), undef);
309
310	# set path for documents
311	ok($htmldoc->path('/tmp'), 1);
312	ok($htmldoc->_get_doc_config('path'), '/tmp');
313
314	# set fontsize
315	ok($htmldoc->set_fontsize(2), 1);
316	ok($htmldoc->_get_doc_config('fontsize'), 2);
317	ok($htmldoc->set_fontsize(2.5), 1);
318	ok($htmldoc->_get_doc_config('fontsize'), 2.5);
319	ok($htmldoc->set_fontsize("x"), 0);
320
321
322	# set a logoimage
323	$htmldoc = new HTML::HTMLDoc();
324	my $setimage = $htmldoc->set_logoimage('./testdata/missingimage.gif');
325	ok($setimage, 0);
326	ok($htmldoc->error(), "Logoimage ./testdata/missingimage.gif could not be found");
327
328	my $logoimg = './testdata/testimage.gif';
329	my $setimage = $htmldoc->set_logoimage($logoimg);
330	ok($setimage, 1);
331
332	ok($htmldoc->get_logoimage(), $logoimg);
333
334}
335
336{
337	# make clean copy to test the outputs
338	my $htmldoc = new HTML::HTMLDoc();
339
340	my $t = $htmldoc->_build_parameters();
341	my $ok = 1;
342	my @cont = ('--header .t.', '--format pdf',  '--charset iso-8859-1', '--quiet', '--portrait', '--size a4', '--footer .1.');
343	foreach(@cont) {
344		if ( $t !~ /$_/ ) {
345			$ok=0;
346			last;
347		}
348	}
349	ok($ok, 1);
350
351	# Test the generation of pdf, ps and html in all possible versions
352	$htmldoc->set_html_content('test');
353	my $pdf = $htmldoc->generate_pdf();
354	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
355	ok(substr($pdf->to_string(),0,10), qr/^\Q%PDF-1.3\E/ );
356
357	my $htmldoc = new HTML::HTMLDoc();
358	$htmldoc->set_output_format('pdf11');
359	$htmldoc->set_html_content('test');
360	my $pdf = $htmldoc->generate_pdf();
361	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
362	ok(substr($pdf->to_string(),0,10), qr/^\Q%PDF-1.1\E/ );
363
364	my $htmldoc = new HTML::HTMLDoc();
365	$htmldoc->set_output_format('pdf12');
366	$htmldoc->set_html_content('test');
367	my $pdf = $htmldoc->generate_pdf();
368	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
369	ok(substr($pdf->to_string(),0,10), qr/^\Q%PDF-1.2\E/ );
370
371	my $htmldoc = new HTML::HTMLDoc();
372	$htmldoc->set_output_format('pdf13');
373	$htmldoc->set_html_content('test');
374	my $pdf = $htmldoc->generate_pdf();
375	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
376	ok(substr($pdf->to_string(),0,10), qr/^\Q%PDF-1.3\E/ );
377
378	my $htmldoc = new HTML::HTMLDoc();
379	$htmldoc->set_output_format('pdf14');
380	$htmldoc->set_html_content('test');
381	my $pdf = $htmldoc->generate_pdf();
382	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
383	ok(substr($pdf->to_string(),0,10), qr/^\Q%PDF-1.4\E/ );
384
385
386	my $htmldoc = new HTML::HTMLDoc();
387	$htmldoc->set_output_format('ps');
388	$htmldoc->set_html_content('test');
389	my $pdf = $htmldoc->generate_pdf();
390	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
391	ok(substr($pdf->to_string(),0,20), qr/^\Q%!PS-Adobe-3.0\E/ );
392
393	my $htmldoc = new HTML::HTMLDoc();
394	$htmldoc->set_output_format('ps1');
395	$htmldoc->set_html_content('test');
396	my $pdf = $htmldoc->generate_pdf();
397	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
398	ok(substr($pdf->to_string(),0,20), qr/^\Q%!PS-Adobe-3.0\E/ );
399
400	my $htmldoc = new HTML::HTMLDoc();
401	$htmldoc->set_output_format('ps2');
402	$htmldoc->set_html_content('test');
403	my $pdf = $htmldoc->generate_pdf();
404	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
405	ok(substr($pdf->to_string(),0,20), qr/^\Q%!PS-Adobe-3.0\E/ );
406
407	my $htmldoc = new HTML::HTMLDoc();
408	$htmldoc->set_output_format('ps3');
409	$htmldoc->set_html_content('test');
410	my $pdf = $htmldoc->generate_pdf();
411	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
412	ok(substr($pdf->to_string(),0,20), qr/^\Q%!PS-Adobe-3.0\E/ );
413
414	my $htmldoc = new HTML::HTMLDoc();
415	$htmldoc->set_output_format('html');
416	$htmldoc->set_html_content('<html><body>lkjlkjlkj</body></html>');
417	my $pdf = $htmldoc->generate_pdf();
418	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
419	ok(substr($pdf->to_string(),0,100), qr|\Q<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"\E| );
420	ok($pdf->to_string(), qr|\Qlkjlkjlkj\E| );
421
422
423
424	# test the generation of pdf in Apache-File-Mode
425	my $htmldoc = new HTML::HTMLDoc('mode'=>'file', 'tmpdir'=>'/tmp/hshshshd/sdasd/');
426	$htmldoc->set_html_content('<html><body>lkjlkjlkj</body></html>');
427	my $pdf = $htmldoc->generate_pdf();
428	ok(ref($pdf), 'HTML::HTMLDoc::PDF');
429	ok(substr($pdf->to_string(),0,10), qr/^\Q%PDF-1.3\E/ );
430}
431
432# look up for a key in an array
433sub array_contains {
434	my $array = shift;
435	my $key = shift;
436
437	my $contains = 0;
438	foreach(@$array) {
439		$contains = 1 if ($_ eq $key);
440	}
441	return $contains;
442}
443