1package graphical_compare;
2
3#*************************************************************************
4#
5# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6#
7# Copyright 2000, 2010 Oracle and/or its affiliates.
8#
9# OpenOffice.org - a multi-platform office productivity suite
10#
11# This file is part of OpenOffice.org.
12#
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.
16#
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).
22#
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.
27#
28#*************************************************************************
29
30use CallExternals;
31use stringhelper;
32use timehelper;
33use filehelper;
34use loghelper;
35use oshelper;
36use cwstestresulthelper;
37use solarenvhelper;
38use ConvwatchHelper;
39
40use strict;
41use Cwd;
42# use File::Basename;
43use Getopt::Long;
44use English;                  # $OSNAME, ...
45use File::Path;
46use Cwd 'chdir';
47use Sys::Hostname;
48use Time::localtime;
49
50# my $cwd = getcwd();
51
52BEGIN {
53    use Exporter   ();
54    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
55
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);
65}
66
67
68our $nTimeOut = 300 * 1000;
69our $viewable = 1;
70our $port;
71our $resolution;
72our $overwritereference;
73our $fixreference;
74our $sConncectionString;
75
76sub setConnectionString($)
77{
78    $sConncectionString=shift;
79}
80
81sub getOOoRunnerClasspath()
82{
83    my $sSourceRoot;
84    my $sUPDExtensions = "";
85    if (defined ($ENV{SOL_TMP}) && defined ($ENV{SOLARVERSION}))
86    {
87        $sSourceRoot = $ENV{SOLARVERSION};
88    }
89    elsif (defined $ENV{SOURCE_ROOT})
90    {
91        $sSourceRoot = $ENV{SOURCE_ROOT};
92        $sSourceRoot = appendPath($sSourceRoot, $ENV{WORK_STAMP});
93    }
94    else
95    {
96        $sSourceRoot = $ENV{SOLARVERSION};
97        $sUPDExtensions = ".$ENV{UPDMINOR}";
98    }
99    $sSourceRoot = appendPath($sSourceRoot, $ENV{INPATH});
100    my $sSourceRootBin = appendPath($sSourceRoot, "bin" . $sUPDExtensions);
101    my $sSourceRootLib = appendPath($sSourceRoot, "lib" . $sUPDExtensions);
102
103    if (! -d $sSourceRoot )
104    {
105        log_print( "SourceRoot not found, search it in '$sSourceRoot'\n");
106        return "";
107    }
108
109    my $sOOoRunnerPath = $sSourceRootBin;
110    my $sUnoilPath   = $sSourceRootBin;
111    my $sRidlPath    = $sSourceRootBin;
112    my $sJurtPath    = $sSourceRootBin;
113    my $sJuhPath     = $sSourceRootBin;
114    my $sJavaUnoPath = $sSourceRootBin;
115
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())
124    {
125        $sOOoRunnerClasspath .= getJavaPathSeparator() . $sSourceRootBin;
126    }
127    else
128    {
129        $sOOoRunnerClasspath .= getJavaPathSeparator() . $sSourceRootLib;
130    }
131    return $sOOoRunnerClasspath;
132}
133
134# ------------------------------------------------------------------------------
135sub getTempPath()
136{
137    my $sTempPath;
138    if (isWindowsEnvironment())
139    {
140        $sTempPath = "C:/temp";
141    }
142    elsif (isUnixEnvironment())
143    {
144        $sTempPath = "/tmp";
145    }
146    else
147    {
148        die "getTempPath() Failed, due to unsupported environment.\n";
149    }
150    return $sTempPath;
151}
152# ------------------------------------------------------------------------------
153
154sub getProjectOutput()
155{
156    my $sOutput = appendPath(getProjectRoot(), $ENV{INPATH});
157    $sOutput = appendPath($sOutput, "misc");
158    return $sOutput;
159}
160
161# ------------------------------------------------------------------------------
162sub getProjectOutputReference()
163{
164    my $sOutput = appendPath(getProjectRoot(), $ENV{INPATH});
165    $sOutput = appendPath($sOutput, "reference");
166    return $sOutput;
167}
168
169
170sub searchForReference($)
171{
172    my $sFile = shift;
173    if ( -e $sFile )
174    {
175        return 0;
176    }
177    if ( -e $sFile . ".ps")
178    {
179        return 0;
180    }
181    if ( -e $sFile . ".pdf")
182    {
183        return 0;
184    }
185    return 1;
186}
187# ------------------------------------------------------------------------------
188
189# my $sOfficeName = $officeprefixname . $officename;
190sub SingleDocumentCompare($$$$$$)
191{
192    # get all about the document to compare
193    my $sDocumentPoolPath = shift;
194    my $sDocumentPool = shift;
195    my $sDocumentName = shift;
196    my $sDebug = "";
197
198    # get all about the destination office
199    my $sCreatorType = shift;
200    if (! $sCreatorType)
201    {
202        # log_print( "parameter -creatortype not given. Use 'OOo'\n");
203        $sCreatorType = "ps";
204    }
205    my $prepareonly = shift;
206    my $show = shift;
207
208    # my $nSimpleCompareTime = getTime();
209
210    my $nConvwatchFailed = 0;
211    set_logfile( appendPath(getProjectOutput(), $sDocumentName . ".txt" ));
212
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    # ------------------------------------------------------------------------------
219
220    my $sOOoRunnerClasspath = quoteIfNeed(getOOoRunnerClasspath());
221    if ($OSNAME eq "cygwin")
222    {
223        if (!startswith($sOOoRunnerClasspath, "\""))
224        {
225            $sOOoRunnerClasspath = quote($sOOoRunnerClasspath);
226        }
227    }
228    if (length($sOOoRunnerClasspath) == 0)
229    {
230        $nConvwatchFailed == 1;
231    }
232    # ------------------------------------------------------------------------------
233    # create postscript or pdf from second installed office
234    # ------------------------------------------------------------------------------
235
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);
242
243    log_print("----- CREATE POSTSCRIPT OR PDF WITH RUNNING OFFICE -----\n");
244    # my $nPrepareSecondPostscriptTime = getTime();
245    if ($nConvwatchFailed == 0)
246    {
247        my $sInputPath = $sDocumentPoolPath;
248        $sInputPath = appendPath($sInputPath, $sDocumentPool);
249        $sInputPath = appendPath($sInputPath, $sDocumentName);
250
251        if (! -f $sInputPath )
252        {
253            $nConvwatchFailed = 1;
254            log_print("ERROR: File '$sInputPath' doesn't exists.\n");
255        }
256        else
257        {
258            my $sOutputPath = getProjectOutput();
259            my $sPropertyFile = appendPath(getProjectOutput() , $sDocumentName . ".build.props");
260
261            local *PROPERTYFILE;
262            if (open(PROPERTYFILE, ">$sPropertyFile"))
263            {
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)
270                {
271                    print PROPERTYFILE "ConnectionString=$sConncectionString\n";
272                }
273                else
274                {
275                    print PROPERTYFILE "ConnectionString=pipe,name=" . getUsername() . "\n";
276                }
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())
282                {
283                    print PROPERTYFILE "DOC_COMPARATOR_PRINTER_NAME=CrossOffice Generic Printer\n";
284                }
285                print PROPERTYFILE "NoOffice=true\n";
286
287                close(PROPERTYFILE);
288            }
289            else
290            {
291                print "Can't open '$sPropertyFile' for write.\n";
292            }
293            if ( -e "$sPropertyFile")
294            {
295                # start OOoRunner
296                # sleep 10;
297                # $sOOoRunnerClasspathFromDestinationName = quoteIfNeed(getOOoRunnerClasspath());
298                my $sParams;
299                if ( $ENV{PERL} )
300                {
301                    $sParams = "-Dperl.exe=" . convertCygwinPath($ENV{PERL});
302                }
303
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";
311
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)
317                {
318                    my $sFailure = "Failed after try to create Postscript/pdf document for " . $sDocumentName;
319                    log_print("ERROR: $sFailure\n");
320                    $nConvwatchFailed = 1;
321                }
322            }
323            else
324            {
325                my $sFailure = "There is no propertyfile: $sPropertyFile";
326                log_print( "ERROR: $sFailure\n");
327                $nConvwatchFailed=1;
328            }
329        }
330
331        # set prepareonly and it is possible to only create ps or pdf files
332        if ($prepareonly)
333        {
334            print(" [only create ");
335            if ($sCreatorType eq "ps" || $sCreatorType eq "pdf")
336            {
337                print(" $sCreatorType");
338            }
339            else
340            {
341                print(" (${sCreatorType}?)");
342            }
343            if ($nConvwatchFailed == 0)
344            {
345                print(" ok");
346            }
347            else
348            {
349                print(" failed")
350            }
351            print("]\n");
352            return $nConvwatchFailed;
353        }
354
355
356        # ------------------------------------------------------------------------------
357        # create jpeg from postscript or pdf from second installed office
358        # ------------------------------------------------------------------------------
359
360        if ($nConvwatchFailed == 0)
361        {
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);
366
367            my $sOutputPath = getProjectOutput();
368
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)
380            {
381                $sParams .= " -gs.path " . quoteIfNeed($gspath);
382            }
383            if ($gsexe)
384            {
385                $sParams .= " -gs.exe $gsexe";
386            }
387
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)
393            {
394                my $sFailure = "Failed after try to create JPEG from Postscript/pdf document for " . $sDocumentName;
395                log_print("ERROR: $sFailure\n");
396                $nConvwatchFailed = 1;
397            }
398        }
399    }
400
401    # ------------------------------------------------------------------------------
402    # create jpeg from postscript or pdf from references
403    # ------------------------------------------------------------------------------
404
405    if ($nConvwatchFailed == 0)
406    {
407        log_print("----- CREATE JPEG FROM POSTSCRIPT OR PDF FROM REFERENCE -----\n");
408
409        # start OOoRunner
410        my $sInputPath = appendPath(getProjectRoot(), "references");
411        $sInputPath = appendPath($sInputPath, getEnvironment());
412        $sInputPath = appendPath($sInputPath, $sDocumentPool);
413        $sInputPath = appendPath($sInputPath, $sDocumentName);
414
415        my $err = searchForReference($sInputPath);
416        if ($err != 0)
417        {
418            log_print("ERROR: Can't find Postscript or PDF reference for '$sInputPath'\n");
419            $nConvwatchFailed = 1;
420        }
421        else
422        {
423            my $sOutputPath = getProjectOutputReference();
424            rmkdir $sOutputPath;
425
426            my $sIndexFile = appendPath($sOutputPath, "index.ini");
427            # we need the index.ini for better run through
428            local *INDEXINI;
429            if ( ! -e $sIndexFile)
430            {
431                if (open(INDEXINI, ">$sIndexFile"))
432                {
433                    # print INDEXINI "[$sDocumentName]\n";
434                    close(INDEXINI);
435                }
436            }
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)
448            {
449                $sParams .= " -gs.path " . quoteIfNeed($gspath);
450            }
451            if ($gsexe)
452            {
453                $sParams .= " -gs.exe $gsexe";
454            }
455
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)
461            {
462                my $sFailure = "Failed after try to create JPEG from Postscript/pdf document for references.";
463                log_print("ERROR: $sFailure\n");
464                $nConvwatchFailed = 1;
465            }
466        }
467    }
468    # ------------------------------------------------------------------------------
469    # compare JPEGs
470    # ------------------------------------------------------------------------------
471
472    if ($nConvwatchFailed == 0)
473    {
474        log_print("----- COMPARE JPEGS -----\n");
475        my $sInputPath = appendPath(getProjectOutputReference(), $sDocumentName);
476
477        my $sOutputPath = getProjectOutput();
478
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)
490        {
491            $sParams .= " -imagemagick.path " . quoteIfNeed($impath);
492        }
493
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)
500        {
501            my $sFailure = "Failed after compare JPEGs $sDocumentName\n";
502            log_print("ERROR: $sFailure\n");
503            $nConvwatchFailed = 1;
504
505            if ($show)
506            {
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")
512                {
513                    my $sInputPath = appendPath(getProjectOutput(), $sDocumentName . ".ps.ini");
514                    if (! -e $sInputPath)
515                    {
516                        $sInputPath = appendPath(getProjectOutput(), $sDocumentName . ".pdf.ini");
517                        if (! -e $sInputPath)
518                        {
519                            $sInputPath = 0;
520                        }
521                    }
522                    if ($sInputPath)
523                    {
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)
533                        # {
534                        #     my $sFailure = "Failed after try to create Postscript/pdf document for " . $sDocumentName;
535                        #     log_print("ERROR: $sFailure\n");
536                        #     $nConvwatchFailed = 1;
537                        # }
538                    }
539                }
540                else
541                {
542                    print "WARNING: The show program '$sJavaProgram' doesn't exists.\n";
543                }
544            }
545        }
546    }
547
548    log_print( "\n\n");
549    close_logfile();
550
551    if ($nConvwatchFailed == 0)
552    {
553        print(" [ok]\n");
554    }
555    else
556    {
557        print(" [FAILED]\n");
558        print("\nPrint output of test: $sDocumentName\n");
559        my $sLogFile = appendPath(getProjectOutput(), $sDocumentName . ".txt");
560        showFile($sLogFile);
561    }
562    # printTime(endTime($nSimpleCompareTime));
563
564    return $nConvwatchFailed;
565}
566
567# ------------------------------------------------------------------------------
568# cat $file
569sub showFile($)
570{
571    my $logfile = shift;
572    local *LOGFILE;
573    if (open(LOGFILE, "$logfile"))
574    {
575        my $line;
576        while ($line = <LOGFILE>)
577        {
578            chomp($line);
579            print $line ."\n";
580        }
581        close(LOGFILE);
582    }
583}
584
585
5861;
587