merged tag ooo/DEV300_m102
[LibreOffice.git] / qadevOOo / runner / convwatch / GraphicalDifferenceCheck.java
blobefce96c1c15b211698844804b342cf7f8e44f405
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package convwatch;
30 import convwatch.GraphicalTestArguments;
31 import convwatch.OfficePrint;
32 import convwatch.ConvWatchCancelException;
33 import convwatch.FileHelper;
34 import java.io.File;
36 import helper.URLHelper;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.frame.XStorable;
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.lang.XServiceInfo;
41 import com.sun.star.uno.UnoRuntime;
43 public class GraphicalDifferenceCheck
45 private static void showVersion()
47 // DEBUG only
48 if (FileHelper.isDebugEnabled())
50 GlobalLogWriter.get().println("");
51 GlobalLogWriter.get().println("+##############################+");
52 GlobalLogWriter.get().println("##### THIS IS CONVWATCH #####");
53 GlobalLogWriter.get().println("##### Debug Version 1.0015 #####");
54 GlobalLogWriter.get().println("+##############################+");
55 GlobalLogWriter.get().println("");
59 /**
60 * Creates references form documents used by the graphical difference check
62 * @param _sInputPath the original document path
63 * @param _sReferencePath the directory where the document will print as file or export as pdf.
65 * @throws ConvWatchException if there are problems, see message
67 * Stops rest, if one creation of reference fails.
69 public static void createReferences(String _sInputPath, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
71 //!
72 // System.out.println("createReferences() InputPath: " + _sInputPath + " refpath: " + _sReferencePath);
73 showVersion();
74 File aInputPath = new File(_sInputPath);
76 // System.out.println("Inputpath in file: " + aInputPath.getAbsolutePath());
77 //!
78 // if (aInputPath.exists())
79 // {
80 // System.out.println("Inputpath exists");
81 // }
82 // else
83 // {
84 // System.out.println("Inputpath doesn't exists");
85 // return;
86 // }
88 if (aInputPath.isDirectory())
90 String fs = System.getProperty("file.separator");
92 String sRemovePath = aInputPath.getAbsolutePath();
93 // a whole directory
95 Object[] aList = DirectoryHelper.traverse(_sInputPath, FileHelper.getFileFilter(), _aGTA.includeSubDirectories());
96 for (int i=0;i<aList.length;i++)
98 String sEntry = (String)aList[i];
99 String sNewReferencePath = _sReferencePath + fs + FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, _sInputPath);
100 createOneReferenceFile(sEntry, sNewReferencePath, _aGTA);
103 else
106 // System.out.println("No directory.");
107 createOneReferenceFile(_sInputPath, _sReferencePath, _aGTA);
113 * Creates a reference for a single document used by the graphical difference check
115 * @param _sInputFile the original document
116 * @param _sReferencePath the directory where the document will print as file or export as pdf.
118 * @throws ConvWatchException if the are problems, see containing message
120 public static boolean createOneReferenceFile(String _sInputFile, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
122 showVersion();
123 if (_aGTA != null)
125 _aGTA.setInputFile(_sInputFile);
127 return OfficePrint.buildReference(_aGTA, _sReferencePath, _sInputFile);
131 * Check if a reference exist
133 * @param _sInputFile the original document
134 * @param _sReferencePath the directory where the document will print as file or export as pdf.
136 * @throws ConvWatchException if the are problems, see containing message
138 public static boolean isReferenceExists(String _sInputFile, String _sReferencePath, GraphicalTestArguments _aGTA)
140 return OfficePrint.isReferenceExists(_aGTA, _sReferencePath, _sInputFile);
145 * Used for the comparance of graphical differences.
146 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
148 * @param _sInputPath the original document path
149 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
150 * needed very much disk space (up to 10MB per page).
151 * The path _sOutputPath must be writeable.
152 * @param _sReferencePath the directory where the document will print as file or export as pdf.
153 * @param _GTA Helper class for lot of parameter to control the office.
155 * Disadvantage: stops rest if one test file has a problem.
157 public static boolean check(String _sInputPath, String _sOutputPath, String _sReferencePath, GraphicalTestArguments _aGTA ) throws ConvWatchException
159 return check(_sInputPath, _sOutputPath, _sReferencePath, null, _aGTA);
163 * Used for the comparance of graphical differences.
164 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
166 * @param _sInputPath the original document path
167 * @param _sReferencePath the directory where the document will print as file or export as pdf.
168 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
169 * needed very much disk space (up to 10MB per page).
170 * The path _sOutputPath must be writeable.
171 * @param _sDiffPath Path to older differences.
172 * @param _GTA Helper class for lot of parameter to control the office.
175 * Stops all, if one creation of reference fails
177 public static boolean check(String _sInputPath, String _sOutputPath, String _sReferencePath, String _sDiffPath, GraphicalTestArguments _aGTA ) throws ConvWatchException
179 showVersion();
181 boolean bOk = true;
183 File aInputPath = new File(_sInputPath);
184 if (aInputPath.isDirectory())
186 String fs = System.getProperty("file.separator");
187 // a whole directory
188 Object[] aList = DirectoryHelper.traverse(_sInputPath, FileHelper.getFileFilter(), _aGTA.includeSubDirectories());
189 if (aList.length != 0)
191 for (int i=0;i<aList.length;i++)
193 String sEntry = (String)aList[i];
194 String sNewSubDir = FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, _sInputPath);
195 String sNewReferencePath = _sReferencePath;
196 String sNewOutputPath = _sOutputPath;
197 String sNewDiffPath = _sDiffPath;
198 if (sNewSubDir.length() > 0)
200 if (sNewReferencePath != null)
202 sNewReferencePath = sNewReferencePath + fs + sNewSubDir;
205 sNewOutputPath = sNewOutputPath + fs + sNewSubDir;
206 if (sNewDiffPath != null)
208 sNewDiffPath = sNewDiffPath + fs + sNewSubDir;
211 bOk &= checkOneFile(sEntry, sNewOutputPath, sNewReferencePath, sNewDiffPath, _aGTA);
215 else
217 bOk = /* GraphicalDifferenceCheck.*/ checkOneFile(_sInputPath, _sOutputPath, _sReferencePath, _sDiffPath, _aGTA);
219 return bOk;
223 * Used for the comparance of graphical differences.
224 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
226 * The path _sOutputPath must be writeable
228 public static boolean checkOneFile(String _sInputFile, String _sOutputPath, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
230 return checkOneFile( _sInputFile, _sOutputPath, _sReferencePath, null, _aGTA);
235 * Used for the comparance of graphical differences.
236 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
238 * For scenarios, where a difference is known and further changes are of interest, differences itself can be compared.
239 * This functionality is provided by the difference path parameter (_sDiffPath). If set, the difference of the current comparance (between input and reference),
240 * will be compared with the (same named) difference document from a earlier comparance.
242 * The path _sOutputPath must be writeable
244 public static boolean checkOneFile(String _sInputFile, String _sOutputPath, String _sReferencePath, String _sDiffPath, GraphicalTestArguments _aGTA ) throws ConvWatchException
246 showVersion();
247 if (_aGTA != null)
249 _aGTA.setInputFile(_sInputFile);
252 boolean bOk = false;
253 if (_sDiffPath != null)
255 // check with an old diff
256 bOk = convwatch.ConvWatch.checkDiffDiff(_aGTA, _sOutputPath, _sInputFile, _sReferencePath, _sDiffPath);
258 else
260 // one file
261 bOk = convwatch.ConvWatch.check(_aGTA, _sOutputPath, _sInputFile, _sReferencePath);
263 return bOk;
267 * Instead of providing a saved document for graphical comparance a StarOffice xComponent
268 * will be saved and afterwards compared.
270 * @param xComponent the test document to be compared as StarOffice component
271 * @param _sOutputPath Path where test results are supposed to been saved. The path _sOutputPath must be writeable.
272 * These documents need sufficient disk space (up to 10MB per page).
273 * A directory structure will be created, which is a mirrored from input path.
275 * @param resultDocName Name by which the xComponent shall be saved as OpenOffice.org XML document.
276 * If provided without suffix, the suffix will be derived from the export filter.
277 * @param _sReferencePath the directory where the document will print as file or export as pdf.
278 * @param _GTA Helper class for lot of parameter to control the office.
280 public static boolean checkOneFile(XComponent xComponent, String _sOutputPath, String _resultDocName, String _sReferencePath, GraphicalTestArguments _aGTA ) throws ConvWatchException
282 showVersion();
284 // one file
285 String sInputFile;
286 sInputFile = createInputFile(xComponent, _sOutputPath, _resultDocName);
287 sInputFile = FileHelper.getSystemPathFromFileURL(sInputFile);
288 return convwatch.ConvWatch.check(_aGTA, _sOutputPath, sInputFile, _sReferencePath);
292 // LLA: old! /**
293 // LLA: old! * Returns 'true' if a reference document on the specific output path exists.
294 // LLA: old! * The name of the document is corresponding to the input document, which can be
295 // LLA: old! * provided by a single name or path.
296 // LLA: old! *
297 // LLA: old! * @param inputPath the original document name (possibly including path)
298 // LLA: old! * @param referencePath the directory where the reference document will be stored
299 // LLA: old!
300 // LLA: old! */
301 // LLA: old! public static boolean isReferencExistent(String inputDocumentPath, String referencePath)
302 // LLA: old! {
303 // LLA: old! // isolate the document name
304 // LLA: old! if(inputDocumentPath.indexOf(File.separator) != -1)
305 // LLA: old! inputDocumentPath = inputDocumentPath.substring(inputDocumentPath.lastIndexOf(File.separator) + 1, inputDocumentPath.length());
306 // LLA: old!
307 // LLA: old! // exchange any arbitray suffix against the refence suffix (.prn)
308 // LLA: old! if(inputDocumentPath.indexOf('.') != -1)
309 // LLA: old! inputDocumentPath = inputDocumentPath.substring(0, inputDocumentPath.lastIndexOf('.'));
310 // LLA: old! inputDocumentPath = inputDocumentPath + ".prn";
311 // LLA: old! System.out.println("GraphicalDifference CheckReferenceDocument: " + inputDocumentPath);
312 // LLA: old!
313 // LLA: old! File refFile = new File(referencePath + inputDocumentPath);
314 // LLA: old! if(refFile.exists()){
315 // LLA: old! return true;
316 // LLA: old! }else
317 // LLA: old! return false;
318 // LLA: old! }
321 private static String createInputFile(XComponent xComponent, String _sOutputPath, String resultDocName)
322 throws ConvWatchCancelException
325 // find the adequate XML StarOffice output filter to save the document and adequate suffix
326 StringBuffer suffix = new StringBuffer();
327 String exportFilter = getXMLOutputFilterforXComponent(xComponent, suffix);
328 if(resultDocName == null)
329 resultDocName = "OOoTestDocument";
330 if(resultDocName.indexOf('.') == -1)
331 resultDocName = suffix.insert(0, resultDocName).toString();
333 // create a result URL for storing the office document
334 String resultURL = URLHelper.getFileURLFromSystemPath(ensureEndingFileSep(_sOutputPath) + resultDocName);
336 XStorable xStorable = null;
337 xStorable = (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(com.sun.star.frame.XStorable.class, xComponent);
338 if(xStorable == null)
340 throw new ConvWatchCancelException("com.sun.star.frame.XStorable could not be instantiated from the office.");
343 PropertyValue pvFilterName = new PropertyValue("FilterName", -1, exportFilter, com.sun.star.beans.PropertyState.getDefault());
344 PropertyValue pvOverwrite = new PropertyValue("Overwrite", -1, new Boolean(true), com.sun.star.beans.PropertyState.getDefault());
348 xStorable.storeAsURL(resultURL, new PropertyValue[]{pvFilterName, pvOverwrite});
350 catch (com.sun.star.io.IOException e)
352 // wrap IOException
353 throw new ConvWatchCancelException("Wrap IOException caught, " + e.getMessage());
356 GlobalLogWriter.get().println("Saving XComponent as " + resultURL);
358 return resultURL;
362 private static String getXMLOutputFilterforXComponent(XComponent xComponent, StringBuffer suffix){
363 XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xComponent);
364 if (xSI.supportsService("com.sun.star.text.TextDocument")){
365 resetBuffer(suffix, ".sxw");
366 return "swriter: StarOffice XML (Writer)";
367 }else if (xSI.supportsService("com.sun.star.sheet.SpreadsheetDocument")){
368 resetBuffer(suffix, ".sxc");
369 return "scalc: StarOffice XML (Calc)";
370 }else if (xSI.supportsService("com.sun.star.presentation.PresentationDocument")){
371 resetBuffer(suffix, ".sxi");
372 return "simpress: StarOffice XML (Impress)";
373 }else if(xSI.supportsService("com.sun.star.drawing.DrawingDocument")){
374 resetBuffer(suffix, ".sxd");
375 return "sdraw: StarOffice XML (Draw)";
376 }else if (xSI.supportsService("com.sun.star.formula.FormulaProperties")){
377 resetBuffer(suffix, ".sxm");
378 return "smath: StarOffice XML (Math)";
380 return null;
383 private static StringBuffer resetBuffer(StringBuffer sb, String suffix)
385 if(sb != null)
387 sb.replace(0, sb.length(), suffix);
389 return sb;
392 private static String ensureEndingFileSep(String s)
394 if(s != null && !s.equals("") && !s.endsWith(File.separator))
396 s = s.trim() + File.separator;
398 else
400 if(s == null)
402 s = "";
406 return s;