1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package convwatch;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.util.ArrayList;
27cdf0e10cSrcweir import convwatch.EnhancedComplexTestCase;
28cdf0e10cSrcweir import convwatch.PRNCompare;
29cdf0e10cSrcweir import convwatch.GraphicalTestArguments;
30cdf0e10cSrcweir import helper.URLHelper;
31cdf0e10cSrcweir import convwatch.OfficePrint;
32cdf0e10cSrcweir import java.io.File;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir public class GfxCompare extends EnhancedComplexTestCase
35cdf0e10cSrcweir {
36cdf0e10cSrcweir     // The first of the mandatory functions:
37cdf0e10cSrcweir     /**
38cdf0e10cSrcweir      * Return the name of the test.
39cdf0e10cSrcweir      * In this case it is the actual name of the service.
40cdf0e10cSrcweir      * @return The tested service.
41cdf0e10cSrcweir      */
42cdf0e10cSrcweir     // public String getTestObjectName() {
43cdf0e10cSrcweir     //     return "ConvWatch runner";
44cdf0e10cSrcweir     // }
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     // The second of the mandatory functions: return all test methods as an
47cdf0e10cSrcweir     // array. There is only one test function in this example.
48cdf0e10cSrcweir     /**
49cdf0e10cSrcweir      * Return all test methods.
50cdf0e10cSrcweir      * @return The test methods.
51cdf0e10cSrcweir      */
52cdf0e10cSrcweir 
getTestMethodNames()53cdf0e10cSrcweir     public String[] getTestMethodNames() {
54cdf0e10cSrcweir         return new String[]{"gfxcompare"};
55cdf0e10cSrcweir     }
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     /**
58cdf0e10cSrcweir      *
59*bb6af6bcSPedro Giffuni      * @return a List of software which must accessible as an external executable
60cdf0e10cSrcweir      */
mustInstalledSoftware()61cdf0e10cSrcweir     protected Object[] mustInstalledSoftware()
62cdf0e10cSrcweir         {
63cdf0e10cSrcweir             ArrayList aList = new ArrayList();
64cdf0e10cSrcweir             // Tools from ImageMagick
65cdf0e10cSrcweir             aList.add( "composite -version" );
66cdf0e10cSrcweir             aList.add( "identify -version" );
67cdf0e10cSrcweir 
68cdf0e10cSrcweir             // Ghostscript
69cdf0e10cSrcweir             aList.add( "gs -version" );
70cdf0e10cSrcweir             return aList.toArray();
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     GraphicalTestArguments m_aArguments = null;
75cdf0e10cSrcweir     /**
76cdf0e10cSrcweir      * The test method itself.
77cdf0e10cSrcweir      * Don't try to call it from outside, it is started only from qadevOOo runner
78cdf0e10cSrcweir      */
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     /* protected */
gfxcompare()81cdf0e10cSrcweir     public void gfxcompare()
82cdf0e10cSrcweir         {
83cdf0e10cSrcweir             GlobalLogWriter.set(log);
84cdf0e10cSrcweir 
85*bb6af6bcSPedro Giffuni             // check if all need software is installed and accessible
86cdf0e10cSrcweir             checkEnvironment(mustInstalledSoftware());
87cdf0e10cSrcweir 
88cdf0e10cSrcweir             m_aArguments = getGraphicalTestArguments();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             String sFile1 = (String)param.get("FILE1");
91cdf0e10cSrcweir             String sFile2 = (String)param.get("FILE2");
92cdf0e10cSrcweir             compare(sFile1, sFile2);
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     // -----------------------------------------------------------------------------
96cdf0e10cSrcweir 
createJPEG(String _sFile, String _sAdditional)97cdf0e10cSrcweir     String createJPEG(String _sFile, String _sAdditional)
98cdf0e10cSrcweir         {
99cdf0e10cSrcweir             String sJPEGFile = "";
100cdf0e10cSrcweir             if (_sFile.startsWith("file:///"))
101cdf0e10cSrcweir             {
102cdf0e10cSrcweir                 _sFile = FileHelper.getSystemPathFromFileURL(_sFile);
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir             File aFile = new File(_sFile);
105cdf0e10cSrcweir             if (aFile.exists())
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 String sAbsFile = aFile.getAbsolutePath();
108cdf0e10cSrcweir                 if (!sAbsFile.equals(_sFile))
109cdf0e10cSrcweir                 {
110cdf0e10cSrcweir                     _sFile = sAbsFile;
111cdf0e10cSrcweir                 }
112cdf0e10cSrcweir             }
113cdf0e10cSrcweir             else
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 GlobalLogWriter.get().println("File: '" + _sFile + "' doesn't exist.");
116cdf0e10cSrcweir                 return "";
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir             String sFileDir = FileHelper.getPath(_sFile);
119cdf0e10cSrcweir             String sBasename = FileHelper.getBasename(_sFile);
120cdf0e10cSrcweir             String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             String fs = System.getProperty("file.separator");
123cdf0e10cSrcweir             String sTmpDir = util.utils.getUsersTempDir();
124cdf0e10cSrcweir             if (m_aArguments.getOutputPath() != null)
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 sTmpDir = m_aArguments.getOutputPath();
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir             if (_sFile.toLowerCase().endsWith("ps") ||
130cdf0e10cSrcweir                 _sFile.toLowerCase().endsWith("prn") ||
131cdf0e10cSrcweir                 _sFile.toLowerCase().endsWith("pdf"))
132cdf0e10cSrcweir             {
133cdf0e10cSrcweir                 // seems to be a Postscript of PDF file
134cdf0e10cSrcweir 
135cdf0e10cSrcweir                 String[] aList = PRNCompare.createJPEGFromPostscript(sTmpDir, sFileDir, sBasename, m_aArguments.getResolutionInDPI());
136cdf0e10cSrcweir                 sJPEGFile = aList[0];
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir             else if (_sFile.toLowerCase().endsWith("jpg") ||
139cdf0e10cSrcweir                      _sFile.toLowerCase().endsWith("jpeg"))
140cdf0e10cSrcweir             {
141cdf0e10cSrcweir                 // do nothing, it's already a picture.
142cdf0e10cSrcweir                 return _sFile;
143cdf0e10cSrcweir             }
144cdf0e10cSrcweir             else
145cdf0e10cSrcweir             {
146cdf0e10cSrcweir                 // we assume it's an office document.
147cdf0e10cSrcweir                 String sInputURL;
148cdf0e10cSrcweir                 String sOutputURL;
149cdf0e10cSrcweir                 String sPrintFileURL;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir                 String sInputFile = sFileDir + fs + sBasename;
152cdf0e10cSrcweir                 sInputURL = URLHelper.getFileURLFromSystemPath(sInputFile);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir                 String sOutputFile = sTmpDir + fs + sBasename;
155cdf0e10cSrcweir                 sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile);
156cdf0e10cSrcweir 
157cdf0e10cSrcweir                 String sPrintFile = sTmpDir + fs + sNameNoSuffix + _sAdditional + ".ps";
158cdf0e10cSrcweir                 sPrintFileURL = URLHelper.getFileURLFromSystemPath(sPrintFile);
159cdf0e10cSrcweir 
160cdf0e10cSrcweir                 try
161cdf0e10cSrcweir                 {
162cdf0e10cSrcweir                     OfficePrint.printToFile(m_aArguments, sInputURL, sOutputURL, sPrintFileURL);
163cdf0e10cSrcweir                     sJPEGFile = createJPEG(sPrintFile, _sAdditional);
164cdf0e10cSrcweir                 }
165cdf0e10cSrcweir                 catch (ConvWatchCancelException e)
166cdf0e10cSrcweir                 {
167cdf0e10cSrcweir                     GlobalLogWriter.get().println("Exception caught, can't create:" + sPrintFileURL);
168cdf0e10cSrcweir                 }
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir             return sJPEGFile;
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 
compare(String _sFile1, String _sFile2)174cdf0e10cSrcweir     public String compare(String _sFile1, String _sFile2)
175cdf0e10cSrcweir         {
176cdf0e10cSrcweir             String sJPEGFile1 = createJPEG(_sFile1, "-1");
177cdf0e10cSrcweir             String sJPEGFile2 = createJPEG(_sFile2, "-2");
178cdf0e10cSrcweir 
179cdf0e10cSrcweir             if (sJPEGFile1.length() > 0 && sJPEGFile2.length() > 0)
180cdf0e10cSrcweir             {
181cdf0e10cSrcweir                 String sDiffFile = PRNCompare.compareJPEGs(sJPEGFile1, sJPEGFile2);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir                 if (sDiffFile.length() > 0)
184cdf0e10cSrcweir                 {
185cdf0e10cSrcweir                     GlobalLogWriter.get().println("Difference created: " + sDiffFile);
186cdf0e10cSrcweir                 }
187cdf0e10cSrcweir                 return sDiffFile;
188cdf0e10cSrcweir             }
189cdf0e10cSrcweir             return "";
190cdf0e10cSrcweir         }
191cdf0e10cSrcweir 
192cdf0e10cSrcweir }
193