1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package convwatch;
29 
30 import java.util.ArrayList;
31 import convwatch.EnhancedComplexTestCase;
32 import convwatch.PRNCompare;
33 import convwatch.GraphicalTestArguments;
34 import helper.URLHelper;
35 import convwatch.OfficePrint;
36 import java.io.File;
37 
38 public class GfxCompare extends EnhancedComplexTestCase
39 {
40     // The first of the mandatory functions:
41     /**
42      * Return the name of the test.
43      * In this case it is the actual name of the service.
44      * @return The tested service.
45      */
46     // public String getTestObjectName() {
47     //     return "ConvWatch runner";
48     // }
49 
50     // The second of the mandatory functions: return all test methods as an
51     // array. There is only one test function in this example.
52     /**
53      * Return all test methods.
54      * @return The test methods.
55      */
56 
57     public String[] getTestMethodNames() {
58         return new String[]{"gfxcompare"};
59     }
60 
61     /**
62      *
63      * @return a List of software which must accessable as an external executable
64      */
65     protected Object[] mustInstalledSoftware()
66         {
67             ArrayList aList = new ArrayList();
68             // Tools from ImageMagick
69             aList.add( "composite -version" );
70             aList.add( "identify -version" );
71 
72             // Ghostscript
73             aList.add( "gs -version" );
74             return aList.toArray();
75         }
76 
77 
78     GraphicalTestArguments m_aArguments = null;
79     /**
80      * The test method itself.
81      * Don't try to call it from outside, it is started only from qadevOOo runner
82      */
83 
84     /* protected */
85     public void gfxcompare()
86         {
87             GlobalLogWriter.set(log);
88 
89             // check if all need software is installed and accessable
90             checkEnvironment(mustInstalledSoftware());
91 
92             m_aArguments = getGraphicalTestArguments();
93 
94             String sFile1 = (String)param.get("FILE1");
95             String sFile2 = (String)param.get("FILE2");
96             compare(sFile1, sFile2);
97         }
98 
99     // -----------------------------------------------------------------------------
100 
101     String createJPEG(String _sFile, String _sAdditional)
102         {
103             String sJPEGFile = "";
104             if (_sFile.startsWith("file:///"))
105             {
106                 _sFile = FileHelper.getSystemPathFromFileURL(_sFile);
107             }
108             File aFile = new File(_sFile);
109             if (aFile.exists())
110             {
111                 String sAbsFile = aFile.getAbsolutePath();
112                 if (!sAbsFile.equals(_sFile))
113                 {
114                     _sFile = sAbsFile;
115                 }
116             }
117             else
118             {
119                 GlobalLogWriter.get().println("File: '" + _sFile + "' doesn't exist.");
120                 return "";
121             }
122             String sFileDir = FileHelper.getPath(_sFile);
123             String sBasename = FileHelper.getBasename(_sFile);
124             String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
125 
126             String fs = System.getProperty("file.separator");
127             String sTmpDir = util.utils.getUsersTempDir();
128             if (m_aArguments.getOutputPath() != null)
129             {
130                 sTmpDir = m_aArguments.getOutputPath();
131             }
132 
133             if (_sFile.toLowerCase().endsWith("ps") ||
134                 _sFile.toLowerCase().endsWith("prn") ||
135                 _sFile.toLowerCase().endsWith("pdf"))
136             {
137                 // seems to be a Postscript of PDF file
138 
139                 String[] aList = PRNCompare.createJPEGFromPostscript(sTmpDir, sFileDir, sBasename, m_aArguments.getResolutionInDPI());
140                 sJPEGFile = aList[0];
141             }
142             else if (_sFile.toLowerCase().endsWith("jpg") ||
143                      _sFile.toLowerCase().endsWith("jpeg"))
144             {
145                 // do nothing, it's already a picture.
146                 return _sFile;
147             }
148             else
149             {
150                 // we assume it's an office document.
151                 String sInputURL;
152                 String sOutputURL;
153                 String sPrintFileURL;
154 
155                 String sInputFile = sFileDir + fs + sBasename;
156                 sInputURL = URLHelper.getFileURLFromSystemPath(sInputFile);
157 
158                 String sOutputFile = sTmpDir + fs + sBasename;
159                 sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile);
160 
161                 String sPrintFile = sTmpDir + fs + sNameNoSuffix + _sAdditional + ".ps";
162                 sPrintFileURL = URLHelper.getFileURLFromSystemPath(sPrintFile);
163 
164                 try
165                 {
166                     OfficePrint.printToFile(m_aArguments, sInputURL, sOutputURL, sPrintFileURL);
167                     sJPEGFile = createJPEG(sPrintFile, _sAdditional);
168                 }
169                 catch (ConvWatchCancelException e)
170                 {
171                     GlobalLogWriter.get().println("Exception caught, can't create:" + sPrintFileURL);
172                 }
173             }
174             return sJPEGFile;
175         }
176 
177 
178     public String compare(String _sFile1, String _sFile2)
179         {
180             String sJPEGFile1 = createJPEG(_sFile1, "-1");
181             String sJPEGFile2 = createJPEG(_sFile2, "-2");
182 
183             if (sJPEGFile1.length() > 0 && sJPEGFile2.length() > 0)
184             {
185                 String sDiffFile = PRNCompare.compareJPEGs(sJPEGFile1, sJPEGFile2);
186 
187                 if (sDiffFile.length() > 0)
188                 {
189                     GlobalLogWriter.get().println("Difference created: " + sDiffFile);
190                 }
191                 return sDiffFile;
192             }
193             return "";
194         }
195 
196 }
197