jl165 merging heads
[LibreOffice.git] / testgraphical / source / graphical_compare.pm
blob5cde8d64ea01c2b1bc4b55803c7f3a491ce23db1
1 package graphical_compare;
3 #*************************************************************************
5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 # Copyright 2000, 2010 Oracle and/or its affiliates.
9 # OpenOffice.org - a multi-platform office productivity suite
11 # This file is part of OpenOffice.org.
13 # OpenOffice.org is free software: you can redistribute it and/or modify
14 # it under the terms of the GNU Lesser General Public License version 3
15 # only, as published by the Free Software Foundation.
17 # OpenOffice.org is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU Lesser General Public License version 3 for more details
21 # (a copy is included in the LICENSE file that accompanied this code).
23 # You should have received a copy of the GNU Lesser General Public License
24 # version 3 along with OpenOffice.org. If not, see
25 # <http://www.openoffice.org/license.html>
26 # for a copy of the LGPLv3 License.
28 #*************************************************************************
30 use CallExternals;
31 use stringhelper;
32 use timehelper;
33 use filehelper;
34 use loghelper;
35 use oshelper;
36 use cwstestresulthelper;
37 use solarenvhelper;
38 use ConvwatchHelper;
40 use strict;
41 use Cwd;
42 # use File::Basename;
43 use Getopt::Long;
44 use English; # $OSNAME, ...
45 use File::Path;
46 use Cwd 'chdir';
47 use Sys::Hostname;
48 use Time::localtime;
50 # my $cwd = getcwd();
52 BEGIN {
53 use Exporter ();
54 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
56 $VERSION = 1.00;
57 # if using RCS/CVS, this may be preferred
58 $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
59 @ISA = qw(Exporter);
60 @EXPORT = qw(&SingleDocumentCompare &setPrefix &setConnectionString);
61 %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
62 # your exported package globals go here,
63 # as well as any optionally exported functions
64 @EXPORT_OK = ( ); # qw($Var1 %Hashit &func3);
68 our $nTimeOut = 300 * 1000;
69 our $viewable = 1;
70 our $port;
71 our $resolution;
72 our $overwritereference;
73 our $fixreference;
74 our $sConncectionString;
76 sub setConnectionString($)
78 $sConncectionString=shift;
81 sub getOOoRunnerClasspath()
83 my $sSourceRoot;
84 my $sUPDExtensions = "";
85 if (defined ($ENV{SOL_TMP}) && defined ($ENV{SOLARVERSION}))
87 $sSourceRoot = $ENV{SOLARVERSION};
89 elsif (defined $ENV{SOURCE_ROOT})
91 $sSourceRoot = $ENV{SOURCE_ROOT};
92 $sSourceRoot = appendPath($sSourceRoot, $ENV{WORK_STAMP});
94 else
96 $sSourceRoot = $ENV{SOLARVERSION};
97 $sUPDExtensions = ".$ENV{UPDMINOR}";
99 $sSourceRoot = appendPath($sSourceRoot, $ENV{INPATH});
100 my $sSourceRootBin = appendPath($sSourceRoot, "bin" . $sUPDExtensions);
101 my $sSourceRootLib = appendPath($sSourceRoot, "lib" . $sUPDExtensions);
103 if (! -d $sSourceRoot )
105 log_print( "SourceRoot not found, search it in '$sSourceRoot'\n");
106 return "";
109 my $sOOoRunnerPath = $sSourceRootBin;
110 my $sUnoilPath = $sSourceRootBin;
111 my $sRidlPath = $sSourceRootBin;
112 my $sJurtPath = $sSourceRootBin;
113 my $sJuhPath = $sSourceRootBin;
114 my $sJavaUnoPath = $sSourceRootBin;
116 my $sOOoRunnerClasspath =
117 appendPath( $sRidlPath, "ridl.jar") . getJavaPathSeparator() .
118 appendPath( $sUnoilPath, "unoil.jar") . getJavaPathSeparator() .
119 appendPath( $sJurtPath, "jurt.jar") . getJavaPathSeparator() .
120 appendPath( $sJuhPath, "juh.jar") . getJavaPathSeparator() .
121 appendPath( $sJavaUnoPath, "java_uno.jar") . getJavaPathSeparator() .
122 appendPath( $sOOoRunnerPath, "OOoRunnerLight.jar");
123 if (isWindowsEnvironment())
125 $sOOoRunnerClasspath .= getJavaPathSeparator() . $sSourceRootBin;
127 else
129 $sOOoRunnerClasspath .= getJavaPathSeparator() . $sSourceRootLib;
131 return $sOOoRunnerClasspath;
134 # ------------------------------------------------------------------------------
135 sub getTempPath()
137 my $sTempPath;
138 if (isWindowsEnvironment())
140 $sTempPath = "C:/temp";
142 elsif (isUnixEnvironment())
144 $sTempPath = "/tmp";
146 else
148 die "getTempPath() Failed, due to unsupported environment.\n";
150 return $sTempPath;
152 # ------------------------------------------------------------------------------
154 sub getProjectOutput()
156 my $sOutput = appendPath(getProjectRoot(), $ENV{INPATH});
157 $sOutput = appendPath($sOutput, "misc");
158 return $sOutput;
161 # ------------------------------------------------------------------------------
162 sub getProjectOutputReference()
164 my $sOutput = appendPath(getProjectRoot(), $ENV{INPATH});
165 $sOutput = appendPath($sOutput, "reference");
166 return $sOutput;
170 sub searchForReference($)
172 my $sFile = shift;
173 if ( -e $sFile )
175 return 0;
177 if ( -e $sFile . ".ps")
179 return 0;
181 if ( -e $sFile . ".pdf")
183 return 0;
185 return 1;
187 # ------------------------------------------------------------------------------
189 # my $sOfficeName = $officeprefixname . $officename;
190 sub SingleDocumentCompare($$$$$$)
192 # get all about the document to compare
193 my $sDocumentPoolPath = shift;
194 my $sDocumentPool = shift;
195 my $sDocumentName = shift;
196 my $sDebug = "";
198 # get all about the destination office
199 my $sCreatorType = shift;
200 if (! $sCreatorType)
202 # log_print( "parameter -creatortype not given. Use 'OOo'\n");
203 $sCreatorType = "ps";
205 my $prepareonly = shift;
206 my $show = shift;
208 # my $nSimpleCompareTime = getTime();
210 my $nConvwatchFailed = 0;
211 set_logfile( appendPath(getProjectOutput(), $sDocumentName . ".txt" ));
213 print("$sDocumentName");
214 log_print("\n");
215 log_print("Graphical compare on document: '$sDocumentName'\n");
216 # ------------------------------------------------------------------------------
217 # create postscript or pdf from first installed office
218 # ------------------------------------------------------------------------------
220 my $sOOoRunnerClasspath = quoteIfNeed(getOOoRunnerClasspath());
221 if ($OSNAME eq "cygwin")
223 if (!startswith($sOOoRunnerClasspath, "\""))
225 $sOOoRunnerClasspath = quote($sOOoRunnerClasspath);
228 if (length($sOOoRunnerClasspath) == 0)
230 $nConvwatchFailed == 1;
232 # ------------------------------------------------------------------------------
233 # create postscript or pdf from second installed office
234 # ------------------------------------------------------------------------------
236 my $sPathesIni = appendPath(getProjectOutput(), "pathes.ini");
237 my $gspath = getFromPathes($sPathesIni, "gs.path");
238 my $gsexe = getFromPathes($sPathesIni, "gs.exe");
239 my $impath = getFromPathes($sPathesIni, "imagemagick.path");
240 my $javaexe = getFromPathes($sPathesIni, "java.exe");
241 setJavaExecutable($javaexe);
243 log_print("----- CREATE POSTSCRIPT OR PDF WITH RUNNING OFFICE -----\n");
244 # my $nPrepareSecondPostscriptTime = getTime();
245 if ($nConvwatchFailed == 0)
247 my $sInputPath = $sDocumentPoolPath;
248 $sInputPath = appendPath($sInputPath, $sDocumentPool);
249 $sInputPath = appendPath($sInputPath, $sDocumentName);
251 if (! -f $sInputPath )
253 $nConvwatchFailed = 1;
254 log_print("ERROR: File '$sInputPath' doesn't exists.\n");
256 else
258 my $sOutputPath = getProjectOutput();
259 my $sPropertyFile = appendPath(getProjectOutput() , $sDocumentName . ".build.props");
261 local *PROPERTYFILE;
262 if (open(PROPERTYFILE, ">$sPropertyFile"))
264 print PROPERTYFILE "# This file is automatically created by graphical_compare.pl\n";
265 print PROPERTYFILE "DOC_COMPARATOR_PRINT_MAX_PAGE=9999\n";
266 print PROPERTYFILE "DOC_COMPARATOR_GFX_OUTPUT_DPI_RESOLUTION=180\n";
267 print PROPERTYFILE "DOC_COMPARATOR_REFERENCE_CREATOR_TYPE=$sCreatorType\n";
268 print PROPERTYFILE "TEMPPATH=" . getTempPath() . "\n";
269 if ($sConncectionString)
271 print PROPERTYFILE "ConnectionString=$sConncectionString\n";
273 else
275 print PROPERTYFILE "ConnectionString=pipe,name=" . getUsername() . "\n";
277 print PROPERTYFILE "OFFICE_VIEWABLE=true\n";
278 print PROPERTYFILE "CREATE_DEFAULT_REFERENCE=true\n";
279 print PROPERTYFILE "DOC_COMPARATOR_INPUT_PATH=$sInputPath\n";
280 print PROPERTYFILE "DOC_COMPARATOR_OUTPUT_PATH=$sOutputPath\n";
281 if (isWindowsEnvironment())
283 print PROPERTYFILE "DOC_COMPARATOR_PRINTER_NAME=CrossOffice Generic Printer\n";
285 print PROPERTYFILE "NoOffice=true\n";
287 close(PROPERTYFILE);
289 else
291 print "Can't open '$sPropertyFile' for write.\n";
293 if ( -e "$sPropertyFile")
295 # start OOoRunner
296 # sleep 10;
297 # $sOOoRunnerClasspathFromDestinationName = quoteIfNeed(getOOoRunnerClasspath());
298 my $sParams;
299 if ( $ENV{PERL} )
301 $sParams = "-Dperl.exe=" . convertCygwinPath($ENV{PERL});
304 $sParams .= " -cp " . $sOOoRunnerClasspath .
305 " org.openoffice.Runner" .
306 " -TimeOut $nTimeOut" .
307 " -tb java_complex" .
308 " -ini $sPropertyFile" .
309 " -o graphical.PostscriptCreator";
310 # $sParams .= " -cs pipe,name=$USER";
312 # $sDebug = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
313 my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
314 $sDebug = "";
315 log_print( "\n\n");
316 if ($err != 0)
318 my $sFailure = "Failed after try to create Postscript/pdf document for " . $sDocumentName;
319 log_print("ERROR: $sFailure\n");
320 $nConvwatchFailed = 1;
323 else
325 my $sFailure = "There is no propertyfile: $sPropertyFile";
326 log_print( "ERROR: $sFailure\n");
327 $nConvwatchFailed=1;
331 # set prepareonly and it is possible to only create ps or pdf files
332 if ($prepareonly)
334 print(" [only create ");
335 if ($sCreatorType eq "ps" || $sCreatorType eq "pdf")
337 print(" $sCreatorType");
339 else
341 print(" (${sCreatorType}?)");
343 if ($nConvwatchFailed == 0)
345 print(" ok");
347 else
349 print(" failed")
351 print("]\n");
352 return $nConvwatchFailed;
356 # ------------------------------------------------------------------------------
357 # create jpeg from postscript or pdf from second installed office
358 # ------------------------------------------------------------------------------
360 if ($nConvwatchFailed == 0)
362 log_print("----- CREATE JPEG FROM POSTSCRIPT OR PDF FROM RUNNING OFFICE -----\n");
363 # start OOoRunner
364 my $sInputPath = getProjectOutput();
365 $sInputPath = appendPath($sInputPath, $sDocumentName);
367 my $sOutputPath = getProjectOutput();
369 my $sParams = "-cp " . $sOOoRunnerClasspath .
370 " org.openoffice.Runner" .
371 " -TimeOut $nTimeOut" .
372 " -tb java_complex" .
373 " -DOC_COMPARATOR_INPUT_PATH " . quoteIfNeed($sInputPath) .
374 " -DOC_COMPARATOR_OUTPUT_PATH " . quoteIfNeed($sOutputPath) .
375 " -DOC_COMPARATOR_REFERENCE_CREATOR_TYPE $sCreatorType" .
376 " -NoOffice" .
377 " -NoSmallPictures" .
378 " -o graphical.JPEGCreator";
379 if ($gspath)
381 $sParams .= " -gs.path " . quoteIfNeed($gspath);
383 if ($gsexe)
385 $sParams .= " -gs.exe $gsexe";
388 # $sDebug = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
389 my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
390 $sDebug = "";
391 # log_print( "\n\n");
392 if ($err != 0)
394 my $sFailure = "Failed after try to create JPEG from Postscript/pdf document for " . $sDocumentName;
395 log_print("ERROR: $sFailure\n");
396 $nConvwatchFailed = 1;
401 # ------------------------------------------------------------------------------
402 # create jpeg from postscript or pdf from references
403 # ------------------------------------------------------------------------------
405 if ($nConvwatchFailed == 0)
407 log_print("----- CREATE JPEG FROM POSTSCRIPT OR PDF FROM REFERENCE -----\n");
409 # start OOoRunner
410 my $sInputPath = appendPath(getProjectRoot(), "references");
411 $sInputPath = appendPath($sInputPath, getEnvironment());
412 $sInputPath = appendPath($sInputPath, $sDocumentPool);
413 $sInputPath = appendPath($sInputPath, $sDocumentName);
415 my $err = searchForReference($sInputPath);
416 if ($err != 0)
418 log_print("ERROR: Can't find Postscript or PDF reference for '$sInputPath'\n");
419 $nConvwatchFailed = 1;
421 else
423 my $sOutputPath = getProjectOutputReference();
424 rmkdir $sOutputPath;
426 my $sIndexFile = appendPath($sOutputPath, "index.ini");
427 # we need the index.ini for better run through
428 local *INDEXINI;
429 if ( ! -e $sIndexFile)
431 if (open(INDEXINI, ">$sIndexFile"))
433 # print INDEXINI "[$sDocumentName]\n";
434 close(INDEXINI);
437 my $sParams = "-cp " . $sOOoRunnerClasspath .
438 " org.openoffice.Runner" .
439 " -TimeOut $nTimeOut" .
440 " -tb java_complex" .
441 " -DOC_COMPARATOR_INPUT_PATH " . quoteIfNeed($sInputPath) .
442 " -DOC_COMPARATOR_OUTPUT_PATH " . quoteIfNeed($sOutputPath) .
443 " -DOC_COMPARATOR_REFERENCE_CREATOR_TYPE $sCreatorType" .
444 " -NoOffice" .
445 " -NoSmallPictures" .
446 " -o graphical.JPEGCreator";
447 if ($gspath)
449 $sParams .= " -gs.path " . quoteIfNeed($gspath);
451 if ($gsexe)
453 $sParams .= " -gs.exe $gsexe";
456 # $sDebug = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
457 my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
458 $sDebug = "";
459 # log_print( "\n\n");
460 if ($err != 0)
462 my $sFailure = "Failed after try to create JPEG from Postscript/pdf document for references.";
463 log_print("ERROR: $sFailure\n");
464 $nConvwatchFailed = 1;
468 # ------------------------------------------------------------------------------
469 # compare JPEGs
470 # ------------------------------------------------------------------------------
472 if ($nConvwatchFailed == 0)
474 log_print("----- COMPARE JPEGS -----\n");
475 my $sInputPath = appendPath(getProjectOutputReference(), $sDocumentName);
477 my $sOutputPath = getProjectOutput();
479 my $sParams = "-Xmx512m" .
480 " -cp " . $sOOoRunnerClasspath .
481 " org.openoffice.Runner" .
482 " -TimeOut $nTimeOut" .
483 " -tb java_complex" .
484 " -DOC_COMPARATOR_INPUT_PATH " . quoteIfNeed($sInputPath) .
485 " -DOC_COMPARATOR_OUTPUT_PATH " . quoteIfNeed($sOutputPath) .
486 " -NoOffice" .
487 " -NoSmallPictures" .
488 " -o graphical.JPEGComparator";
489 if ($impath)
491 $sParams .= " -imagemagick.path " . quoteIfNeed($impath);
494 # start OOoRunner
495 # $sDebug = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
496 my $err = calljava(getJavaExecutable(), $sParams, $sDebug);
497 $sDebug = "";
498 log_print( "\n\n");
499 if ($err != 0)
501 my $sFailure = "Failed after compare JPEGs $sDocumentName\n";
502 log_print("ERROR: $sFailure\n");
503 $nConvwatchFailed = 1;
505 if ($show)
507 # try to execute new java tool to show graphical compare
508 my $sJavaProgram = appendPath(getProjectRoot(), $ENV{INPATH});
509 $sJavaProgram = appendPath($sJavaProgram, "class");
510 $sJavaProgram = appendPath($sJavaProgram, "ConvwatchGUIProject.jar");
511 if ( -e "$sJavaProgram")
513 my $sInputPath = appendPath(getProjectOutput(), $sDocumentName . ".ps.ini");
514 if (! -e $sInputPath)
516 $sInputPath = appendPath(getProjectOutput(), $sDocumentName . ".pdf.ini");
517 if (! -e $sInputPath)
519 $sInputPath = 0;
522 if ($sInputPath)
524 my $sParams = "-Xms128m -Xmx512m -jar $sJavaProgram $sInputPath";
525 # $sParams .= " -cs pipe,name=$USER";
526 # my $sJavaExe = "C:/Program Files/Java/jdk1.6.0_16/bin/java.exe"; # getJavaExecutable()
527 my $sJavaExe = getJavaExecutable();
528 # $sDebug = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=9001,suspend=y";
529 my $err = calljava($sJavaExe, $sParams, $sDebug);
530 # $sDebug = "";
531 # log_print( "\n\n");
532 # if ($err != 0)
534 # my $sFailure = "Failed after try to create Postscript/pdf document for " . $sDocumentName;
535 # log_print("ERROR: $sFailure\n");
536 # $nConvwatchFailed = 1;
540 else
542 print "WARNING: The show program '$sJavaProgram' doesn't exists.\n";
548 log_print( "\n\n");
549 close_logfile();
551 if ($nConvwatchFailed == 0)
553 print(" [ok]\n");
555 else
557 print(" [FAILED]\n");
558 print("\nPrint output of test: $sDocumentName\n");
559 my $sLogFile = appendPath(getProjectOutput(), $sDocumentName . ".txt");
560 showFile($sLogFile);
562 # printTime(endTime($nSimpleCompareTime));
564 return $nConvwatchFailed;
567 # ------------------------------------------------------------------------------
568 # cat $file
569 sub showFile($)
571 my $logfile = shift;
572 local *LOGFILE;
573 if (open(LOGFILE, "$logfile"))
575 my $line;
576 while ($line = <LOGFILE>)
578 chomp($line);
579 print $line ."\n";
581 close(LOGFILE);