xref: /trunk/test/testgui/source/bvt/gui/FileExport.java (revision 126941563920c285023d1f6559a20f4a22ed64e1)
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(File.separatorChar);
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(File.separatorChar);
137         int sepIndexLast = apath.lastIndexOf(File.separatorChar);
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 = new File(Testspace.getPath());
225         File absspath = spacepath.getAbsoluteFile();
226         String abspre = absspath.getParent();
227 
228         result = new DataSheet(getFile("outputlog" + File.separatorChar
229                 + FileExport.class.getName() + ".xml"));
230         result.addRow("data", "File Path", "File Size", "Scenario",
231                 "Exported File Path", "Exported File Size", "Result", "Error");
232 
233         testedlogfile = abspre + "testgui" + File.separatorChar + "cases_tested.txt";
234         samplespath = "samples";
235 
236         if (outpath.length() == 0) {
237             File workspacepath = Testspace.getFile("output");// ..\\testspace\\output
238             outpath = workspacepath.getAbsolutePath();
239 
240             // outpath = "D:\\AOOautomation\\Docs sample files\\out";
241         }
242 
243         if (bContinue)
244             getcontinuepoint();
245 
246         File samplesDir = Testspace.getFile(samplespath);
247         recursionfiles(samplesDir, samplelist);
248         ListIterator<String> it = sampledirs.listIterator();
249 
250         boolean bstartfromthis = false;
251         while (it.hasNext()) {
252 
253             String str = (String) it.next();
254             if (!bContinue) {
255                 File afiledir = new File(str);
256                 dotest(afiledir);
257             } else {
258                 File file = new File(thepoint.path);
259                 File parent = file.getParentFile();
260                 if (parent != null) {
261                     String pathbegin = parent.getAbsolutePath();
262                     if (pathbegin.equalsIgnoreCase(str)) {
263                         bstartfromthis = true;
264 
265                     }
266                 }
267                 if (bstartfromthis == true) {
268                     File afiledir = new File(str);
269                     dotest(afiledir);
270                 }
271             }
272         }
273     }
274 
275     public void dotest(File samplesDir) throws Exception {
276         FilenameFilter testFilter = new FilenameFilter() {
277             public boolean accept(File file, String name) {
278                 if (name.endsWith(".doc") || name.endsWith(".docx")
279                         || name.endsWith(".dot") || name.endsWith(".xls")
280                         || name.endsWith(".xlsx") || name.endsWith(".ods")
281                         || name.endsWith(".ppt") || name.endsWith(".pptx")
282                         || name.endsWith(".odp")) {
283                     // filters files
284                     return true;
285                 } else {
286                     return false;
287                 }
288             }
289         };
290         File[] files = samplesDir.listFiles(testFilter);
291         Arrays.sort(files);
292         int nfiles = files.length;
293         if (nfiles == 0)
294             return;
295 
296         int i = thepoint.i;
297         for (; i < nfiles; i++) {
298             File afile = files[i];
299             String path = afile.getAbsolutePath();
300 
301             String extName = FileUtil.getFileExtName(path).toLowerCase();
302             boolean bShouldTest = false;
303             if (extName.equals("doc") || extName.equals("docx")
304                     || extName.equals("odt")) {
305                 bShouldTest = true;
306                 if (atest.documentT == false)
307                     continue;
308             }
309             if (extName.equals("ppt") || extName.equals("pptx")
310                     || extName.equals("odp")) {
311                 bShouldTest = true;
312                 if (atest.slideT == false)
313                     continue;
314             }
315             if (extName.equals("xls") || extName.equals("xlsx")
316                     || extName.equals("ods")) {
317                 bShouldTest = true;
318                 if (atest.spreadsheetT == false)
319                     continue;
320             }
321             if (!bShouldTest)
322                 continue;
323             String exportname = "aoo_" + afile.getName();
324 
325             sourceFile = new File(path);
326 
327             app.stop();
328             app.start();
329 
330             if(!Open(path)){
331                 continue;
332             }
333 
334             String newpath = getrealoutpath(path);
335 
336             // do testing
337             if (!savetosameformat(exportname, newpath)) {
338                 continue;
339             }
340 
341             if(!Open(path)) {
342                 continue;
343 
344             }
345             if (!savetodiffformat(exportname, newpath)) {
346                 continue;
347             }
348 
349             if(!Open(path)) {
350                 continue;
351 
352             }
353 
354             if (!savetopdfformat(exportname, newpath)) {
355                 continue;
356             }
357         }
358     }
359 
360     private boolean Open(String path) throws Exception {
361         try {
362             open(path);
363             if (!app.exists())
364                 throw new RuntimeException();
365             HandleBlockers(false);
366             if(statusBar.exists(timeout))
367                 statusBar.waitForEnabled(timeout, interval);
368             else
369                 throw new TimeoutException("time out");
370             HandleBlockers(false);
371             if (!app.exists())
372                 throw new RuntimeException();
373             return true;
374         } catch (Exception e) {
375             try {
376                 String reason = e.getMessage();
377                 if (reason == null || reason.isEmpty())
378                     reason = "Opening";
379                 result.addRow("data", sourceFile.getCanonicalPath(),
380                         sourceFile.length(), scenario, "", "", "Fail", reason);
381             } catch (IOException e1) {
382                 // TODO Auto-generated catch block
383                 e1.printStackTrace();
384             }
385             return false;
386         }
387     }
388 
389     private boolean savetosameformat(String file, String outpath) {
390         try {
391             File reportDir = Testspace.getFile(outpath);
392 
393             String extName = FileUtil.getFileExtName(file).toLowerCase();
394 
395             boolean formatchanged = false;
396             if (extName.equals("docx")) {
397                 extName = "doc";
398                 formatchanged = true;
399             } else if (extName.equals("pptx")) {
400                 extName = "ppt";
401                 formatchanged = true;
402             } else if (extName.equals("xlsx")) {
403                 extName = "xls";
404                 formatchanged = true;
405             }
406 
407             scenario = FileUtil.getFileExtName(file).toLowerCase() + " to " + extName;
408 
409             int dotIndex = file.lastIndexOf(".");
410             String pre = file.substring(0, dotIndex + 1);
411             String newfile = pre + extName;
412 
413             String saveTo = reportDir + File.separator + file;
414             if (formatchanged)
415                 saveTo = reportDir + File.separator + newfile;
416             // Save the text document
417             deleteFile(saveTo);
418             SaveAs(saveTo);
419             Close();
420             if(!Open(saveTo))
421                 return false;
422 
423             String exception = "";
424             String resultflag = "";
425             try {
426                 Close();
427                 resultflag = "Pass";
428             } catch (Exception e) {
429                 exception = e.getMessage();
430                 resultflag = "Fail";
431             }
432 
433             File targetFile = new File(saveTo);
434             result.addRow("data", sourceFile.getCanonicalPath(),
435                     sourceFile.length(), scenario, saveTo, targetFile.length(),
436                     resultflag, exception);
437 
438             return true;
439         } catch (Exception e) {
440             try {
441                 String exception = e.getMessage();
442                 if (exception == null || exception.isEmpty())
443                     exception = "Saving to the same format";
444                 result.addRow("data", sourceFile.getCanonicalPath(),
445                         sourceFile.length(), scenario, "", "", "Fail", exception);
446             } catch (IOException e1) {
447                 // TODO Auto-generated catch block
448                 e1.printStackTrace();
449             }
450 
451             return false;
452         }
453     }
454 
455     private boolean savetodiffformat(String file, String outpath) {
456         try {
457             File reportDir = Testspace.getFile(outpath);
458 
459             String extName = FileUtil.getFileExtName(file).toLowerCase();
460 
461             String targetExtName = null;
462 
463             if (extName.equals("doc") || extName.equals("docx"))
464                 targetExtName = "odt";
465             else if (extName.equals("ppt") || extName.equals("pptx"))
466                 targetExtName = "odp";
467             else if (extName.equals("xls") || extName.equals("xlsx"))
468                 targetExtName = "ods";
469             else if (extName.equals("odt"))
470                 targetExtName = "doc";
471             else if (extName.equals("odp"))
472                 targetExtName = "ppt";
473             else if (extName.equals("ods"))
474                 targetExtName = "xls";
475 
476             scenario = extName + " to " + targetExtName;
477 
478             int dotIndex = file.lastIndexOf(".");
479             String pre = file.substring(0, dotIndex + 1);
480             String saveTo = reportDir + File.separator + pre + targetExtName;
481             deleteFile(saveTo);
482             // long base = System.currentTimeMillis();
483             SaveAs(saveTo);
484             Close();
485             if(!Open(saveTo))
486                 return false;
487 
488             String exception = "";
489             String resultflag = "";
490             try {
491                 Close();
492                 resultflag = "Pass";
493             } catch (Exception e) {
494                 exception = e.getMessage();
495                 resultflag = "Fail";
496             }
497 
498             File targetFile = new File(saveTo);
499             result.addRow("data", sourceFile.getCanonicalPath(),
500                     sourceFile.length(), scenario, saveTo, targetFile.length(),
501                     resultflag, exception);
502 
503             return true;
504         } catch (Exception e) {
505             try {
506                 String exception = e.getMessage();
507                 if (exception == null || exception.isEmpty())
508                     exception = "Saving to a different format";
509                 result.addRow("data", sourceFile.getCanonicalPath(),
510                         sourceFile.length(), scenario, "", "", "Fail", exception);
511             } catch (IOException e1) {
512                 // TODO Auto-generated catch block
513                 e1.printStackTrace();
514             }
515 
516             return false;
517         }
518 
519     }
520 
521     private void Close() throws Exception {
522         close();
523         HandleBlockers(false);
524     }
525 
526     public static void HandleBlockers(final boolean Positive) throws Exception {
527         new Condition() {
528             @Override
529             public boolean value() {
530                 while (activeMsgBox.exists()) {
531 
532                     String context = activeMsgBox.getMessage();
533                     if (context.toLowerCase().indexOf("has been modified") >= 0
534                             && context.toLowerCase().indexOf(
535                                     "do you want to save your changes") >= 0)
536                         throw new RuntimeException("A wrong dirty flag");
537                     if (context.toLowerCase().indexOf("read-error") >= 0)
538                         throw new RuntimeException("Read Error");
539                     if (context.toLowerCase().indexOf("does not exist") >= 0)
540                         throw new RuntimeException("File not exist");
541 
542                     try {
543                         if (Positive)
544                             activeMsgBox.ok();
545                         else
546                             activeMsgBox.no();
547                     } catch (Exception e) {
548                         try {
549                             if (Positive)
550                                 activeMsgBox.yes();
551                             else
552                                 activeMsgBox.no();
553                         } catch (Exception e1) {
554                             try {
555                                 activeMsgBox.doDefault();
556                             } catch (Exception e2) {
557                                 try {
558                                     activeMsgBox.ok();
559                                 } catch (Exception e3) {
560                                     activeMsgBox.yes();
561                                 }
562                             }
563                         }
564                     }
565                 }
566                 if (passwdDlg.exists()) {
567                     String caption = passwdDlg.getCaption();
568                     if (caption.toLowerCase().indexOf(
569                             "enter password to open file") >= 0)
570                         throw new RuntimeException("A password protected file");
571                     if (caption.toLowerCase().indexOf("properties") >= 0)
572                         throw new RuntimeException("An unsupported format");
573                     if (SupportedFormats(caption))
574                         throw new RuntimeException("An unreadable file");
575                 }
576                 return true;
577             }
578 
579         }.test(timeout, interval);
580     }
581 
582     private static boolean SupportedFormats(String filename) {
583         if (filename.endsWith(".doc") || filename.endsWith(".docx")
584                 || filename.endsWith(".dot") || filename.endsWith(".xls")
585                 || filename.endsWith(".xlsx") || filename.endsWith(".ods")
586                 || filename.endsWith(".ppt") || filename.endsWith(".pptx")
587                 || filename.endsWith(".odp")) {
588             return true;
589         } else {
590             return false;
591         }
592     }
593 
594     private void SaveAs(String newfile) throws Exception {
595         saveAs(newfile);
596         HandleBlockers(true);
597         if(statusBar.exists(timeout))
598             statusBar.waitForEnabled(timeout, interval);
599         else
600             throw new TimeoutException("time out");
601     }
602 
603     private boolean savetopdfformat(String file, String outpath) {
604         try {
605             File reportDir = Testspace.getFile(outpath);
606             String extName = "pdf";
607 
608             int dotIndex = file.lastIndexOf(".");
609             String pre = file.substring(0, dotIndex + 1);
610             String newfile = pre + extName;
611 
612             scenario = FileUtil.getFileExtName(file).toLowerCase() + " to pdf";
613 
614             String saveTo = reportDir + File.separator + newfile;
615             // Save the text document
616             app.dispatch(".uno:ExportToPDF");
617             pdfGeneralPage.ok();
618 
619             submitSaveDlg(saveTo);
620             HandleBlockers(true);
621 
622             if(statusBar.exists(timeout))
623                 statusBar.waitForEnabled(timeout, interval);
624             else
625                 throw new TimeoutException("time out");
626 
627             String outcome = "Pass";
628             try {
629                 Close();
630             } catch (Exception e) {
631                 if (!e.getMessage().matches("A wrong dirty flag"))
632                     outcome = e.getMessage();
633                 else
634                     throw e;
635             }
636 
637 
638             File targetFile = new File(saveTo);
639             result.addRow("data", sourceFile.getCanonicalPath(),
640                     sourceFile.length(), scenario, saveTo, targetFile.length(),
641                     outcome);
642 
643             return true;
644         } catch (Exception e) {
645             try {
646                 String reason = e.getMessage();
647                 if (reason == null || reason.isEmpty())
648                     reason = "Exporting to pdf format";
649                 result.addRow("data", sourceFile.getCanonicalPath(),
650                         sourceFile.length(), scenario, "", "", "Fail", reason);
651             } catch (IOException e1) {
652                 // TODO Auto-generated catch block
653                 e1.printStackTrace();
654             }
655 
656             return false;
657         }
658     }
659 
660 }
661