1*44cf0280SCarl Marcum /************************************************************** 2*44cf0280SCarl Marcum * 3*44cf0280SCarl Marcum * Licensed to the Apache Software Foundation (ASF) under one 4*44cf0280SCarl Marcum * or more contributor license agreements. See the NOTICE file 5*44cf0280SCarl Marcum * distributed with this work for additional information 6*44cf0280SCarl Marcum * regarding copyright ownership. The ASF licenses this file 7*44cf0280SCarl Marcum * to you under the Apache License, Version 2.0 (the 8*44cf0280SCarl Marcum * "License"); you may not use this file except in compliance 9*44cf0280SCarl Marcum * with the License. You may obtain a copy of the License at 10*44cf0280SCarl Marcum * 11*44cf0280SCarl Marcum * http://www.apache.org/licenses/LICENSE-2.0 12*44cf0280SCarl Marcum * 13*44cf0280SCarl Marcum * Unless required by applicable law or agreed to in writing, 14*44cf0280SCarl Marcum * software distributed under the License is distributed on an 15*44cf0280SCarl Marcum * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*44cf0280SCarl Marcum * KIND, either express or implied. See the License for the 17*44cf0280SCarl Marcum * specific language governing permissions and limitations 18*44cf0280SCarl Marcum * under the License. 19*44cf0280SCarl Marcum * 20*44cf0280SCarl Marcum *************************************************************/ 21*44cf0280SCarl Marcum 22*44cf0280SCarl Marcum /** 23*44cf0280SCarl Marcum * 24*44cf0280SCarl Marcum */ 25*44cf0280SCarl Marcum package bvt.gui; 26*44cf0280SCarl Marcum 27*44cf0280SCarl Marcum import static org.openoffice.test.common.Testspace.*; 28*44cf0280SCarl Marcum import static testlib.gui.AppTool.*; 29*44cf0280SCarl Marcum import static testlib.gui.UIMap.*; 30*44cf0280SCarl Marcum 31*44cf0280SCarl Marcum import java.io.*; 32*44cf0280SCarl Marcum import java.util.ArrayList; 33*44cf0280SCarl Marcum import java.util.Arrays; 34*44cf0280SCarl Marcum import java.util.List; 35*44cf0280SCarl Marcum import java.util.ListIterator; 36*44cf0280SCarl Marcum import java.util.concurrent.TimeoutException; 37*44cf0280SCarl Marcum import java.io.File; 38*44cf0280SCarl Marcum import java.io.FileNotFoundException; 39*44cf0280SCarl Marcum import java.io.IOException; 40*44cf0280SCarl Marcum import java.io.Reader; 41*44cf0280SCarl Marcum import java.io.InputStreamReader; 42*44cf0280SCarl Marcum import java.lang.RuntimeException; 43*44cf0280SCarl Marcum 44*44cf0280SCarl Marcum import org.junit.After; 45*44cf0280SCarl Marcum import org.junit.AfterClass; 46*44cf0280SCarl Marcum import org.junit.Before; 47*44cf0280SCarl Marcum import org.junit.BeforeClass; 48*44cf0280SCarl Marcum import org.junit.Rule; 49*44cf0280SCarl Marcum import org.junit.Test; 50*44cf0280SCarl Marcum import org.openoffice.test.common.Condition; 51*44cf0280SCarl Marcum import org.openoffice.test.common.DataSheet; 52*44cf0280SCarl Marcum import org.openoffice.test.common.FileUtil; 53*44cf0280SCarl Marcum import org.openoffice.test.common.Logger; 54*44cf0280SCarl Marcum import org.openoffice.test.common.Testspace; 55*44cf0280SCarl Marcum import org.openoffice.test.vcl.widgets.VclDialog; 56*44cf0280SCarl Marcum 57*44cf0280SCarl Marcum 58*44cf0280SCarl Marcum public class FileExport { 59*44cf0280SCarl Marcum private static class TestType { 60*44cf0280SCarl Marcum TestType(boolean doc, boolean spread, boolean slide)61*44cf0280SCarl Marcum public TestType(boolean doc, boolean spread, boolean slide) { 62*44cf0280SCarl Marcum documentT = doc; 63*44cf0280SCarl Marcum spreadsheetT = spread; 64*44cf0280SCarl Marcum slideT = slide; 65*44cf0280SCarl Marcum } 66*44cf0280SCarl Marcum 67*44cf0280SCarl Marcum public boolean documentT; 68*44cf0280SCarl Marcum public boolean spreadsheetT; 69*44cf0280SCarl Marcum public boolean slideT; 70*44cf0280SCarl Marcum }; 71*44cf0280SCarl Marcum 72*44cf0280SCarl Marcum private static class ContinuePoint { 73*44cf0280SCarl Marcum public String path; 74*44cf0280SCarl Marcum public int i; 75*44cf0280SCarl Marcum ContinuePoint()76*44cf0280SCarl Marcum ContinuePoint() { 77*44cf0280SCarl Marcum path = ""; 78*44cf0280SCarl Marcum i = 0; 79*44cf0280SCarl Marcum } 80*44cf0280SCarl Marcum } 81*44cf0280SCarl Marcum 82*44cf0280SCarl Marcum // for example 83*44cf0280SCarl Marcum // the path is "D:\\aoo\\utaoo\\testspace\\ooxmlsamples" 84*44cf0280SCarl Marcum String samplespath = "";// a dir 85*44cf0280SCarl Marcum String outpath = ""; // a dir 86*44cf0280SCarl Marcum static double timeout = 100; 87*44cf0280SCarl Marcum static double interval = 0.1; 88*44cf0280SCarl Marcum double sleeptime = 1; 89*44cf0280SCarl Marcum boolean bContinue = false;// if failed,next execute from the continue point 90*44cf0280SCarl Marcum TestType atest = new TestType(true, true, true);// doc,spreadsheet,slide 91*44cf0280SCarl Marcum ContinuePoint thepoint = null; 92*44cf0280SCarl Marcum BufferedWriter fwContinue = null; 93*44cf0280SCarl Marcum String testedlogfile = ""; 94*44cf0280SCarl Marcum 95*44cf0280SCarl Marcum private static DataSheet result; 96*44cf0280SCarl Marcum private String scenario = null; 97*44cf0280SCarl Marcum private File sourceFile = null; 98*44cf0280SCarl Marcum 99*44cf0280SCarl Marcum public static final VclDialog passwdDlg = dialog(""); 100*44cf0280SCarl Marcum recursionfiles(File path, List<String> resultFileName)101*44cf0280SCarl Marcum public List<String> recursionfiles(File path, List<String> resultFileName) { 102*44cf0280SCarl Marcum File[] files = path.listFiles(); 103*44cf0280SCarl Marcum if (files == null) 104*44cf0280SCarl Marcum return resultFileName; 105*44cf0280SCarl Marcum for (File f : files) { 106*44cf0280SCarl Marcum if (f.isDirectory()) {// a path 107*44cf0280SCarl Marcum if (!f.isHidden() && !f.getName().startsWith(".")) { 108*44cf0280SCarl Marcum sampledirs.add(f.getPath()); 109*44cf0280SCarl Marcum recursionfiles(f, resultFileName); 110*44cf0280SCarl Marcum } 111*44cf0280SCarl Marcum } else {// a file 112*44cf0280SCarl Marcum if (!f.isHidden() && !f.getName().startsWith(".")) { 113*44cf0280SCarl Marcum String apath = f.getPath(); 114*44cf0280SCarl Marcum 115*44cf0280SCarl Marcum int sepIndex = apath.indexOf(File.separatorChar); 116*44cf0280SCarl Marcum String userpath = apath.substring(sepIndex); 117*44cf0280SCarl Marcum String newpath = outpath + userpath; 118*44cf0280SCarl Marcum 119*44cf0280SCarl Marcum File file = new File(newpath); 120*44cf0280SCarl Marcum File parent = file.getParentFile(); 121*44cf0280SCarl Marcum if (parent != null && !parent.exists()) { 122*44cf0280SCarl Marcum parent.mkdirs(); 123*44cf0280SCarl Marcum } 124*44cf0280SCarl Marcum resultFileName.add(f.getPath()); 125*44cf0280SCarl Marcum } 126*44cf0280SCarl Marcum } 127*44cf0280SCarl Marcum } 128*44cf0280SCarl Marcum return resultFileName; 129*44cf0280SCarl Marcum } 130*44cf0280SCarl Marcum getrealoutpath(String p)131*44cf0280SCarl Marcum private String getrealoutpath(String p) { 132*44cf0280SCarl Marcum String apath = p; 133*44cf0280SCarl Marcum 134*44cf0280SCarl Marcum int sepIndex = apath.indexOf(File.separatorChar); 135*44cf0280SCarl Marcum int sepIndexLast = apath.lastIndexOf(File.separatorChar); 136*44cf0280SCarl Marcum String userpath = apath.substring(sepIndex, sepIndexLast); 137*44cf0280SCarl Marcum String newpath = outpath + userpath; 138*44cf0280SCarl Marcum File tempFolderFile = new File(newpath); 139*44cf0280SCarl Marcum if (!tempFolderFile.exists()) { 140*44cf0280SCarl Marcum tempFolderFile.mkdirs(); 141*44cf0280SCarl Marcum } 142*44cf0280SCarl Marcum return newpath; 143*44cf0280SCarl Marcum } 144*44cf0280SCarl Marcum 145*44cf0280SCarl Marcum private List<String> samplelist = null; 146*44cf0280SCarl Marcum private List<String> sampledirs = null; 147*44cf0280SCarl Marcum 148*44cf0280SCarl Marcum @Rule 149*44cf0280SCarl Marcum public Logger log = Logger.getLogger(this); 150*44cf0280SCarl Marcum 151*44cf0280SCarl Marcum @BeforeClass beforeClass()152*44cf0280SCarl Marcum public static void beforeClass() { 153*44cf0280SCarl Marcum app.clean(); 154*44cf0280SCarl Marcum } 155*44cf0280SCarl Marcum 156*44cf0280SCarl Marcum @AfterClass afterClass()157*44cf0280SCarl Marcum public static void afterClass() { 158*44cf0280SCarl Marcum app.stop(); 159*44cf0280SCarl Marcum } 160*44cf0280SCarl Marcum 161*44cf0280SCarl Marcum @Before before()162*44cf0280SCarl Marcum public void before() { 163*44cf0280SCarl Marcum 164*44cf0280SCarl Marcum } 165*44cf0280SCarl Marcum 166*44cf0280SCarl Marcum @After after()167*44cf0280SCarl Marcum public void after() throws Exception { 168*44cf0280SCarl Marcum app.stop(); 169*44cf0280SCarl Marcum } 170*44cf0280SCarl Marcum getcontinuepoint()171*44cf0280SCarl Marcum void getcontinuepoint() { 172*44cf0280SCarl Marcum 173*44cf0280SCarl Marcum if (bContinue == false) { 174*44cf0280SCarl Marcum thepoint.path = ""; 175*44cf0280SCarl Marcum thepoint.i = 0; 176*44cf0280SCarl Marcum return; 177*44cf0280SCarl Marcum } 178*44cf0280SCarl Marcum File ftestedlog = new File(testedlogfile); 179*44cf0280SCarl Marcum Reader reader = null; 180*44cf0280SCarl Marcum try { 181*44cf0280SCarl Marcum reader = new InputStreamReader(new FileInputStream(ftestedlog)); 182*44cf0280SCarl Marcum } catch (FileNotFoundException e1) { 183*44cf0280SCarl Marcum // TODO Auto-generated catch block 184*44cf0280SCarl Marcum e1.printStackTrace(); 185*44cf0280SCarl Marcum } 186*44cf0280SCarl Marcum 187*44cf0280SCarl Marcum BufferedReader br = new BufferedReader(reader); 188*44cf0280SCarl Marcum 189*44cf0280SCarl Marcum String line = null; 190*44cf0280SCarl Marcum int countline = 0; 191*44cf0280SCarl Marcum int m = 0; 192*44cf0280SCarl Marcum try { 193*44cf0280SCarl Marcum if ((line = br.readLine()) != null) { 194*44cf0280SCarl Marcum if (countline == 0) { 195*44cf0280SCarl Marcum thepoint.path = line; 196*44cf0280SCarl Marcum } else { 197*44cf0280SCarl Marcum 198*44cf0280SCarl Marcum m = Integer.parseInt(line); 199*44cf0280SCarl Marcum if (m > 0) 200*44cf0280SCarl Marcum thepoint.i = m; 201*44cf0280SCarl Marcum } 202*44cf0280SCarl Marcum } 203*44cf0280SCarl Marcum } catch (NumberFormatException e1) { 204*44cf0280SCarl Marcum // TODO Auto-generated catch block 205*44cf0280SCarl Marcum e1.printStackTrace(); 206*44cf0280SCarl Marcum } catch (IOException e1) { 207*44cf0280SCarl Marcum // TODO Auto-generated catch block 208*44cf0280SCarl Marcum e1.printStackTrace(); 209*44cf0280SCarl Marcum } 210*44cf0280SCarl Marcum } 211*44cf0280SCarl Marcum 212*44cf0280SCarl Marcum /** 213*44cf0280SCarl Marcum * Test Open/SaveAs ooxml file by Aoo 214*44cf0280SCarl Marcum * 215*44cf0280SCarl Marcum * @throws Exception 216*44cf0280SCarl Marcum */ 217*44cf0280SCarl Marcum @Test testSaveAs()218*44cf0280SCarl Marcum public void testSaveAs() throws Exception { 219*44cf0280SCarl Marcum samplelist = new ArrayList<String>(); 220*44cf0280SCarl Marcum sampledirs = new ArrayList<String>(); 221*44cf0280SCarl Marcum thepoint = new ContinuePoint(); 222*44cf0280SCarl Marcum File spacepath = new File(Testspace.getPath()); 223*44cf0280SCarl Marcum File absspath = spacepath.getAbsoluteFile(); 224*44cf0280SCarl Marcum String abspre = absspath.getParent(); 225*44cf0280SCarl Marcum 226*44cf0280SCarl Marcum result = new DataSheet(getFile("outputlog" + File.separatorChar 227*44cf0280SCarl Marcum + FileExport.class.getName() + ".xml")); 228*44cf0280SCarl Marcum result.addRow("data", "File Path", "File Size", "Scenario", 229*44cf0280SCarl Marcum "Exported File Path", "Exported File Size", "Result", "Error"); 230*44cf0280SCarl Marcum 231*44cf0280SCarl Marcum testedlogfile = abspre + "testgui" + File.separatorChar + "cases_tested.txt"; 232*44cf0280SCarl Marcum samplespath = "samples"; 233*44cf0280SCarl Marcum 234*44cf0280SCarl Marcum if (outpath.length() == 0) { 235*44cf0280SCarl Marcum File workspacepath = Testspace.getFile("output");// ..\\testspace\\output 236*44cf0280SCarl Marcum outpath = workspacepath.getAbsolutePath(); 237*44cf0280SCarl Marcum 238*44cf0280SCarl Marcum // outpath = "D:\\AOOautomation\\Docs sample files\\out"; 239*44cf0280SCarl Marcum } 240*44cf0280SCarl Marcum 241*44cf0280SCarl Marcum if (bContinue) 242*44cf0280SCarl Marcum getcontinuepoint(); 243*44cf0280SCarl Marcum 244*44cf0280SCarl Marcum File samplesDir = Testspace.getFile(samplespath); 245*44cf0280SCarl Marcum recursionfiles(samplesDir, samplelist); 246*44cf0280SCarl Marcum ListIterator<String> it = sampledirs.listIterator(); 247*44cf0280SCarl Marcum 248*44cf0280SCarl Marcum boolean bstartfromthis = false; 249*44cf0280SCarl Marcum while (it.hasNext()) { 250*44cf0280SCarl Marcum 251*44cf0280SCarl Marcum String str = (String) it.next(); 252*44cf0280SCarl Marcum if (!bContinue) { 253*44cf0280SCarl Marcum File afiledir = new File(str); 254*44cf0280SCarl Marcum dotest(afiledir); 255*44cf0280SCarl Marcum } else { 256*44cf0280SCarl Marcum File file = new File(thepoint.path); 257*44cf0280SCarl Marcum File parent = file.getParentFile(); 258*44cf0280SCarl Marcum if (parent != null) { 259*44cf0280SCarl Marcum String pathbegin = parent.getAbsolutePath(); 260*44cf0280SCarl Marcum if (pathbegin.equalsIgnoreCase(str)) { 261*44cf0280SCarl Marcum bstartfromthis = true; 262*44cf0280SCarl Marcum 263*44cf0280SCarl Marcum } 264*44cf0280SCarl Marcum } 265*44cf0280SCarl Marcum if (bstartfromthis == true) { 266*44cf0280SCarl Marcum File afiledir = new File(str); 267*44cf0280SCarl Marcum dotest(afiledir); 268*44cf0280SCarl Marcum } 269*44cf0280SCarl Marcum } 270*44cf0280SCarl Marcum } 271*44cf0280SCarl Marcum } 272*44cf0280SCarl Marcum dotest(File samplesDir)273*44cf0280SCarl Marcum public void dotest(File samplesDir) throws Exception { 274*44cf0280SCarl Marcum FilenameFilter testFilter = new FilenameFilter() { 275*44cf0280SCarl Marcum public boolean accept(File file, String name) { 276*44cf0280SCarl Marcum if (name.endsWith(".doc") || name.endsWith(".docx") 277*44cf0280SCarl Marcum || name.endsWith(".dot") || name.endsWith(".xls") 278*44cf0280SCarl Marcum || name.endsWith(".xlsx") || name.endsWith(".ods") 279*44cf0280SCarl Marcum || name.endsWith(".ppt") || name.endsWith(".pptx") 280*44cf0280SCarl Marcum || name.endsWith(".odp")) { 281*44cf0280SCarl Marcum // filters files 282*44cf0280SCarl Marcum return true; 283*44cf0280SCarl Marcum } else { 284*44cf0280SCarl Marcum return false; 285*44cf0280SCarl Marcum } 286*44cf0280SCarl Marcum } 287*44cf0280SCarl Marcum }; 288*44cf0280SCarl Marcum File[] files = samplesDir.listFiles(testFilter); 289*44cf0280SCarl Marcum Arrays.sort(files); 290*44cf0280SCarl Marcum int nfiles = files.length; 291*44cf0280SCarl Marcum if (nfiles == 0) 292*44cf0280SCarl Marcum return; 293*44cf0280SCarl Marcum 294*44cf0280SCarl Marcum int i = thepoint.i; 295*44cf0280SCarl Marcum for (; i < nfiles; i++) { 296*44cf0280SCarl Marcum File afile = files[i]; 297*44cf0280SCarl Marcum String path = afile.getAbsolutePath(); 298*44cf0280SCarl Marcum 299*44cf0280SCarl Marcum String extName = FileUtil.getFileExtName(path).toLowerCase(); 300*44cf0280SCarl Marcum boolean bShouldTest = false; 301*44cf0280SCarl Marcum if (extName.equals("doc") || extName.equals("docx") 302*44cf0280SCarl Marcum || extName.equals("odt")) { 303*44cf0280SCarl Marcum bShouldTest = true; 304*44cf0280SCarl Marcum if (atest.documentT == false) 305*44cf0280SCarl Marcum continue; 306*44cf0280SCarl Marcum } 307*44cf0280SCarl Marcum if (extName.equals("ppt") || extName.equals("pptx") 308*44cf0280SCarl Marcum || extName.equals("odp")) { 309*44cf0280SCarl Marcum bShouldTest = true; 310*44cf0280SCarl Marcum if (atest.slideT == false) 311*44cf0280SCarl Marcum continue; 312*44cf0280SCarl Marcum } 313*44cf0280SCarl Marcum if (extName.equals("xls") || extName.equals("xlsx") 314*44cf0280SCarl Marcum || extName.equals("ods")) { 315*44cf0280SCarl Marcum bShouldTest = true; 316*44cf0280SCarl Marcum if (atest.spreadsheetT == false) 317*44cf0280SCarl Marcum continue; 318*44cf0280SCarl Marcum } 319*44cf0280SCarl Marcum if (!bShouldTest) 320*44cf0280SCarl Marcum continue; 321*44cf0280SCarl Marcum String exportname = "aoo_" + afile.getName(); 322*44cf0280SCarl Marcum 323*44cf0280SCarl Marcum sourceFile = new File(path); 324*44cf0280SCarl Marcum 325*44cf0280SCarl Marcum app.stop(); 326*44cf0280SCarl Marcum app.start(); 327*44cf0280SCarl Marcum 328*44cf0280SCarl Marcum if(!Open(path)){ 329*44cf0280SCarl Marcum continue; 330*44cf0280SCarl Marcum } 331*44cf0280SCarl Marcum 332*44cf0280SCarl Marcum String newpath = getrealoutpath(path); 333*44cf0280SCarl Marcum 334*44cf0280SCarl Marcum // do testing 335*44cf0280SCarl Marcum if (!savetosameformat(exportname, newpath)) { 336*44cf0280SCarl Marcum continue; 337*44cf0280SCarl Marcum } 338*44cf0280SCarl Marcum 339*44cf0280SCarl Marcum if(!Open(path)) { 340*44cf0280SCarl Marcum continue; 341*44cf0280SCarl Marcum 342*44cf0280SCarl Marcum } 343*44cf0280SCarl Marcum if (!savetodiffformat(exportname, newpath)) { 344*44cf0280SCarl Marcum continue; 345*44cf0280SCarl Marcum } 346*44cf0280SCarl Marcum 347*44cf0280SCarl Marcum if(!Open(path)) { 348*44cf0280SCarl Marcum continue; 349*44cf0280SCarl Marcum 350*44cf0280SCarl Marcum } 351*44cf0280SCarl Marcum 352*44cf0280SCarl Marcum if (!savetopdfformat(exportname, newpath)) { 353*44cf0280SCarl Marcum continue; 354*44cf0280SCarl Marcum } 355*44cf0280SCarl Marcum } 356*44cf0280SCarl Marcum } 357*44cf0280SCarl Marcum Open(String path)358*44cf0280SCarl Marcum private boolean Open(String path) throws Exception { 359*44cf0280SCarl Marcum try { 360*44cf0280SCarl Marcum open(path); 361*44cf0280SCarl Marcum if (!app.exists()) 362*44cf0280SCarl Marcum throw new RuntimeException(); 363*44cf0280SCarl Marcum HandleBlockers(false); 364*44cf0280SCarl Marcum if(statusBar.exists(timeout)) 365*44cf0280SCarl Marcum statusBar.waitForEnabled(timeout, interval); 366*44cf0280SCarl Marcum else 367*44cf0280SCarl Marcum throw new TimeoutException("time out"); 368*44cf0280SCarl Marcum HandleBlockers(false); 369*44cf0280SCarl Marcum if (!app.exists()) 370*44cf0280SCarl Marcum throw new RuntimeException(); 371*44cf0280SCarl Marcum return true; 372*44cf0280SCarl Marcum } catch (Exception e) { 373*44cf0280SCarl Marcum try { 374*44cf0280SCarl Marcum String reason = e.getMessage(); 375*44cf0280SCarl Marcum if (reason == null || reason.isEmpty()) 376*44cf0280SCarl Marcum reason = "Opening"; 377*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 378*44cf0280SCarl Marcum sourceFile.length(), scenario, "", "", "Fail", reason); 379*44cf0280SCarl Marcum } catch (IOException e1) { 380*44cf0280SCarl Marcum // TODO Auto-generated catch block 381*44cf0280SCarl Marcum e1.printStackTrace(); 382*44cf0280SCarl Marcum } 383*44cf0280SCarl Marcum return false; 384*44cf0280SCarl Marcum } 385*44cf0280SCarl Marcum } 386*44cf0280SCarl Marcum savetosameformat(String file, String outpath)387*44cf0280SCarl Marcum private boolean savetosameformat(String file, String outpath) { 388*44cf0280SCarl Marcum try { 389*44cf0280SCarl Marcum File reportDir = Testspace.getFile(outpath); 390*44cf0280SCarl Marcum 391*44cf0280SCarl Marcum String extName = FileUtil.getFileExtName(file).toLowerCase(); 392*44cf0280SCarl Marcum 393*44cf0280SCarl Marcum boolean formatchanged = false; 394*44cf0280SCarl Marcum if (extName.equals("docx")) { 395*44cf0280SCarl Marcum extName = "doc"; 396*44cf0280SCarl Marcum formatchanged = true; 397*44cf0280SCarl Marcum } else if (extName.equals("pptx")) { 398*44cf0280SCarl Marcum extName = "ppt"; 399*44cf0280SCarl Marcum formatchanged = true; 400*44cf0280SCarl Marcum } else if (extName.equals("xlsx")) { 401*44cf0280SCarl Marcum extName = "xls"; 402*44cf0280SCarl Marcum formatchanged = true; 403*44cf0280SCarl Marcum } 404*44cf0280SCarl Marcum 405*44cf0280SCarl Marcum scenario = FileUtil.getFileExtName(file).toLowerCase() + " to " + extName; 406*44cf0280SCarl Marcum 407*44cf0280SCarl Marcum int dotIndex = file.lastIndexOf("."); 408*44cf0280SCarl Marcum String pre = file.substring(0, dotIndex + 1); 409*44cf0280SCarl Marcum String newfile = pre + extName; 410*44cf0280SCarl Marcum 411*44cf0280SCarl Marcum String saveTo = reportDir + File.separator + file; 412*44cf0280SCarl Marcum if (formatchanged) 413*44cf0280SCarl Marcum saveTo = reportDir + File.separator + newfile; 414*44cf0280SCarl Marcum // Save the text document 415*44cf0280SCarl Marcum deleteFile(saveTo); 416*44cf0280SCarl Marcum SaveAs(saveTo); 417*44cf0280SCarl Marcum Close(); 418*44cf0280SCarl Marcum if(!Open(saveTo)) 419*44cf0280SCarl Marcum return false; 420*44cf0280SCarl Marcum 421*44cf0280SCarl Marcum String exception = ""; 422*44cf0280SCarl Marcum String resultflag = ""; 423*44cf0280SCarl Marcum try { 424*44cf0280SCarl Marcum Close(); 425*44cf0280SCarl Marcum resultflag = "Pass"; 426*44cf0280SCarl Marcum } catch (Exception e) { 427*44cf0280SCarl Marcum exception = e.getMessage(); 428*44cf0280SCarl Marcum resultflag = "Fail"; 429*44cf0280SCarl Marcum } 430*44cf0280SCarl Marcum 431*44cf0280SCarl Marcum File targetFile = new File(saveTo); 432*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 433*44cf0280SCarl Marcum sourceFile.length(), scenario, saveTo, targetFile.length(), 434*44cf0280SCarl Marcum resultflag, exception); 435*44cf0280SCarl Marcum 436*44cf0280SCarl Marcum return true; 437*44cf0280SCarl Marcum } catch (Exception e) { 438*44cf0280SCarl Marcum try { 439*44cf0280SCarl Marcum String exception = e.getMessage(); 440*44cf0280SCarl Marcum if (exception == null || exception.isEmpty()) 441*44cf0280SCarl Marcum exception = "Saving to the same format"; 442*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 443*44cf0280SCarl Marcum sourceFile.length(), scenario, "", "", "Fail", exception); 444*44cf0280SCarl Marcum } catch (IOException e1) { 445*44cf0280SCarl Marcum // TODO Auto-generated catch block 446*44cf0280SCarl Marcum e1.printStackTrace(); 447*44cf0280SCarl Marcum } 448*44cf0280SCarl Marcum 449*44cf0280SCarl Marcum return false; 450*44cf0280SCarl Marcum } 451*44cf0280SCarl Marcum } 452*44cf0280SCarl Marcum savetodiffformat(String file, String outpath)453*44cf0280SCarl Marcum private boolean savetodiffformat(String file, String outpath) { 454*44cf0280SCarl Marcum try { 455*44cf0280SCarl Marcum File reportDir = Testspace.getFile(outpath); 456*44cf0280SCarl Marcum 457*44cf0280SCarl Marcum String extName = FileUtil.getFileExtName(file).toLowerCase(); 458*44cf0280SCarl Marcum 459*44cf0280SCarl Marcum String targetExtName = null; 460*44cf0280SCarl Marcum 461*44cf0280SCarl Marcum if (extName.equals("doc") || extName.equals("docx")) 462*44cf0280SCarl Marcum targetExtName = "odt"; 463*44cf0280SCarl Marcum else if (extName.equals("ppt") || extName.equals("pptx")) 464*44cf0280SCarl Marcum targetExtName = "odp"; 465*44cf0280SCarl Marcum else if (extName.equals("xls") || extName.equals("xlsx")) 466*44cf0280SCarl Marcum targetExtName = "ods"; 467*44cf0280SCarl Marcum else if (extName.equals("odt")) 468*44cf0280SCarl Marcum targetExtName = "doc"; 469*44cf0280SCarl Marcum else if (extName.equals("odp")) 470*44cf0280SCarl Marcum targetExtName = "ppt"; 471*44cf0280SCarl Marcum else if (extName.equals("ods")) 472*44cf0280SCarl Marcum targetExtName = "xls"; 473*44cf0280SCarl Marcum 474*44cf0280SCarl Marcum scenario = extName + " to " + targetExtName; 475*44cf0280SCarl Marcum 476*44cf0280SCarl Marcum int dotIndex = file.lastIndexOf("."); 477*44cf0280SCarl Marcum String pre = file.substring(0, dotIndex + 1); 478*44cf0280SCarl Marcum String saveTo = reportDir + File.separator + pre + targetExtName; 479*44cf0280SCarl Marcum deleteFile(saveTo); 480*44cf0280SCarl Marcum // long base = System.currentTimeMillis(); 481*44cf0280SCarl Marcum SaveAs(saveTo); 482*44cf0280SCarl Marcum Close(); 483*44cf0280SCarl Marcum if(!Open(saveTo)) 484*44cf0280SCarl Marcum return false; 485*44cf0280SCarl Marcum 486*44cf0280SCarl Marcum String exception = ""; 487*44cf0280SCarl Marcum String resultflag = ""; 488*44cf0280SCarl Marcum try { 489*44cf0280SCarl Marcum Close(); 490*44cf0280SCarl Marcum resultflag = "Pass"; 491*44cf0280SCarl Marcum } catch (Exception e) { 492*44cf0280SCarl Marcum exception = e.getMessage(); 493*44cf0280SCarl Marcum resultflag = "Fail"; 494*44cf0280SCarl Marcum } 495*44cf0280SCarl Marcum 496*44cf0280SCarl Marcum File targetFile = new File(saveTo); 497*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 498*44cf0280SCarl Marcum sourceFile.length(), scenario, saveTo, targetFile.length(), 499*44cf0280SCarl Marcum resultflag, exception); 500*44cf0280SCarl Marcum 501*44cf0280SCarl Marcum return true; 502*44cf0280SCarl Marcum } catch (Exception e) { 503*44cf0280SCarl Marcum try { 504*44cf0280SCarl Marcum String exception = e.getMessage(); 505*44cf0280SCarl Marcum if (exception == null || exception.isEmpty()) 506*44cf0280SCarl Marcum exception = "Saving to a different format"; 507*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 508*44cf0280SCarl Marcum sourceFile.length(), scenario, "", "", "Fail", exception); 509*44cf0280SCarl Marcum } catch (IOException e1) { 510*44cf0280SCarl Marcum // TODO Auto-generated catch block 511*44cf0280SCarl Marcum e1.printStackTrace(); 512*44cf0280SCarl Marcum } 513*44cf0280SCarl Marcum 514*44cf0280SCarl Marcum return false; 515*44cf0280SCarl Marcum } 516*44cf0280SCarl Marcum 517*44cf0280SCarl Marcum } 518*44cf0280SCarl Marcum Close()519*44cf0280SCarl Marcum private void Close() throws Exception { 520*44cf0280SCarl Marcum close(); 521*44cf0280SCarl Marcum HandleBlockers(false); 522*44cf0280SCarl Marcum } 523*44cf0280SCarl Marcum HandleBlockers(final boolean Positive)524*44cf0280SCarl Marcum public static void HandleBlockers(final boolean Positive) throws Exception { 525*44cf0280SCarl Marcum new Condition() { 526*44cf0280SCarl Marcum @Override 527*44cf0280SCarl Marcum public boolean value() { 528*44cf0280SCarl Marcum while (activeMsgBox.exists()) { 529*44cf0280SCarl Marcum 530*44cf0280SCarl Marcum String context = activeMsgBox.getMessage(); 531*44cf0280SCarl Marcum if (context.toLowerCase().indexOf("has been modified") >= 0 532*44cf0280SCarl Marcum && context.toLowerCase().indexOf( 533*44cf0280SCarl Marcum "do you want to save your changes") >= 0) 534*44cf0280SCarl Marcum throw new RuntimeException("A wrong dirty flag"); 535*44cf0280SCarl Marcum if (context.toLowerCase().indexOf("read-error") >= 0) 536*44cf0280SCarl Marcum throw new RuntimeException("Read Error"); 537*44cf0280SCarl Marcum if (context.toLowerCase().indexOf("does not exist") >= 0) 538*44cf0280SCarl Marcum throw new RuntimeException("File not exist"); 539*44cf0280SCarl Marcum 540*44cf0280SCarl Marcum try { 541*44cf0280SCarl Marcum if (Positive) 542*44cf0280SCarl Marcum activeMsgBox.ok(); 543*44cf0280SCarl Marcum else 544*44cf0280SCarl Marcum activeMsgBox.no(); 545*44cf0280SCarl Marcum } catch (Exception e) { 546*44cf0280SCarl Marcum try { 547*44cf0280SCarl Marcum if (Positive) 548*44cf0280SCarl Marcum activeMsgBox.yes(); 549*44cf0280SCarl Marcum else 550*44cf0280SCarl Marcum activeMsgBox.no(); 551*44cf0280SCarl Marcum } catch (Exception e1) { 552*44cf0280SCarl Marcum try { 553*44cf0280SCarl Marcum activeMsgBox.doDefault(); 554*44cf0280SCarl Marcum } catch (Exception e2) { 555*44cf0280SCarl Marcum try { 556*44cf0280SCarl Marcum activeMsgBox.ok(); 557*44cf0280SCarl Marcum } catch (Exception e3) { 558*44cf0280SCarl Marcum activeMsgBox.yes(); 559*44cf0280SCarl Marcum } 560*44cf0280SCarl Marcum } 561*44cf0280SCarl Marcum } 562*44cf0280SCarl Marcum } 563*44cf0280SCarl Marcum } 564*44cf0280SCarl Marcum if (passwdDlg.exists()) { 565*44cf0280SCarl Marcum String caption = passwdDlg.getCaption(); 566*44cf0280SCarl Marcum if (caption.toLowerCase().indexOf( 567*44cf0280SCarl Marcum "enter password to open file") >= 0) 568*44cf0280SCarl Marcum throw new RuntimeException("A password protected file"); 569*44cf0280SCarl Marcum if (caption.toLowerCase().indexOf("properties") >= 0) 570*44cf0280SCarl Marcum throw new RuntimeException("An unsupported format"); 571*44cf0280SCarl Marcum if (SupportedFormats(caption)) 572*44cf0280SCarl Marcum throw new RuntimeException("An unreadable file"); 573*44cf0280SCarl Marcum } 574*44cf0280SCarl Marcum return true; 575*44cf0280SCarl Marcum } 576*44cf0280SCarl Marcum 577*44cf0280SCarl Marcum }.test(timeout, interval); 578*44cf0280SCarl Marcum } 579*44cf0280SCarl Marcum SupportedFormats(String filename)580*44cf0280SCarl Marcum private static boolean SupportedFormats(String filename) { 581*44cf0280SCarl Marcum if (filename.endsWith(".doc") || filename.endsWith(".docx") 582*44cf0280SCarl Marcum || filename.endsWith(".dot") || filename.endsWith(".xls") 583*44cf0280SCarl Marcum || filename.endsWith(".xlsx") || filename.endsWith(".ods") 584*44cf0280SCarl Marcum || filename.endsWith(".ppt") || filename.endsWith(".pptx") 585*44cf0280SCarl Marcum || filename.endsWith(".odp")) { 586*44cf0280SCarl Marcum return true; 587*44cf0280SCarl Marcum } else { 588*44cf0280SCarl Marcum return false; 589*44cf0280SCarl Marcum } 590*44cf0280SCarl Marcum } 591*44cf0280SCarl Marcum SaveAs(String newfile)592*44cf0280SCarl Marcum private void SaveAs(String newfile) throws Exception { 593*44cf0280SCarl Marcum saveAs(newfile); 594*44cf0280SCarl Marcum HandleBlockers(true); 595*44cf0280SCarl Marcum if(statusBar.exists(timeout)) 596*44cf0280SCarl Marcum statusBar.waitForEnabled(timeout, interval); 597*44cf0280SCarl Marcum else 598*44cf0280SCarl Marcum throw new TimeoutException("time out"); 599*44cf0280SCarl Marcum } 600*44cf0280SCarl Marcum savetopdfformat(String file, String outpath)601*44cf0280SCarl Marcum private boolean savetopdfformat(String file, String outpath) { 602*44cf0280SCarl Marcum try { 603*44cf0280SCarl Marcum File reportDir = Testspace.getFile(outpath); 604*44cf0280SCarl Marcum String extName = "pdf"; 605*44cf0280SCarl Marcum 606*44cf0280SCarl Marcum int dotIndex = file.lastIndexOf("."); 607*44cf0280SCarl Marcum String pre = file.substring(0, dotIndex + 1); 608*44cf0280SCarl Marcum String newfile = pre + extName; 609*44cf0280SCarl Marcum 610*44cf0280SCarl Marcum scenario = FileUtil.getFileExtName(file).toLowerCase() + " to pdf"; 611*44cf0280SCarl Marcum 612*44cf0280SCarl Marcum String saveTo = reportDir + File.separator + newfile; 613*44cf0280SCarl Marcum // Save the text document 614*44cf0280SCarl Marcum app.dispatch(".uno:ExportToPDF"); 615*44cf0280SCarl Marcum pdfGeneralPage.ok(); 616*44cf0280SCarl Marcum 617*44cf0280SCarl Marcum submitSaveDlg(saveTo); 618*44cf0280SCarl Marcum HandleBlockers(true); 619*44cf0280SCarl Marcum 620*44cf0280SCarl Marcum if(statusBar.exists(timeout)) 621*44cf0280SCarl Marcum statusBar.waitForEnabled(timeout, interval); 622*44cf0280SCarl Marcum else 623*44cf0280SCarl Marcum throw new TimeoutException("time out"); 624*44cf0280SCarl Marcum 625*44cf0280SCarl Marcum String outcome = "Pass"; 626*44cf0280SCarl Marcum try { 627*44cf0280SCarl Marcum Close(); 628*44cf0280SCarl Marcum } catch (Exception e) { 629*44cf0280SCarl Marcum if (!e.getMessage().matches("A wrong dirty flag")) 630*44cf0280SCarl Marcum outcome = e.getMessage(); 631*44cf0280SCarl Marcum else 632*44cf0280SCarl Marcum throw e; 633*44cf0280SCarl Marcum } 634*44cf0280SCarl Marcum 635*44cf0280SCarl Marcum 636*44cf0280SCarl Marcum File targetFile = new File(saveTo); 637*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 638*44cf0280SCarl Marcum sourceFile.length(), scenario, saveTo, targetFile.length(), 639*44cf0280SCarl Marcum outcome); 640*44cf0280SCarl Marcum 641*44cf0280SCarl Marcum return true; 642*44cf0280SCarl Marcum } catch (Exception e) { 643*44cf0280SCarl Marcum try { 644*44cf0280SCarl Marcum String reason = e.getMessage(); 645*44cf0280SCarl Marcum if (reason == null || reason.isEmpty()) 646*44cf0280SCarl Marcum reason = "Exporting to pdf format"; 647*44cf0280SCarl Marcum result.addRow("data", sourceFile.getCanonicalPath(), 648*44cf0280SCarl Marcum sourceFile.length(), scenario, "", "", "Fail", reason); 649*44cf0280SCarl Marcum } catch (IOException e1) { 650*44cf0280SCarl Marcum // TODO Auto-generated catch block 651*44cf0280SCarl Marcum e1.printStackTrace(); 652*44cf0280SCarl Marcum } 653*44cf0280SCarl Marcum 654*44cf0280SCarl Marcum return false; 655*44cf0280SCarl Marcum } 656*44cf0280SCarl Marcum } 657*44cf0280SCarl Marcum 658*44cf0280SCarl Marcum } 659