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