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