xref: /trunk/test/testgui/source/svt/gui/TestSample.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*80a6f5c5SLiu Zhe /**************************************************************
2*80a6f5c5SLiu Zhe  *
3*80a6f5c5SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*80a6f5c5SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*80a6f5c5SLiu Zhe  * distributed with this work for additional information
6*80a6f5c5SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*80a6f5c5SLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*80a6f5c5SLiu Zhe  * "License"); you may not use this file except in compliance
9*80a6f5c5SLiu Zhe  * with the License.  You may obtain a copy of the License at
10*80a6f5c5SLiu Zhe  *
11*80a6f5c5SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*80a6f5c5SLiu Zhe  *
13*80a6f5c5SLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*80a6f5c5SLiu Zhe  * software distributed under the License is distributed on an
15*80a6f5c5SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*80a6f5c5SLiu Zhe  * KIND, either express or implied.  See the License for the
17*80a6f5c5SLiu Zhe  * specific language governing permissions and limitations
18*80a6f5c5SLiu Zhe  * under the License.
19*80a6f5c5SLiu Zhe  *
20*80a6f5c5SLiu Zhe  *************************************************************/
21*80a6f5c5SLiu Zhe 
22*80a6f5c5SLiu Zhe package svt.gui;
23*80a6f5c5SLiu Zhe 
24*80a6f5c5SLiu Zhe import static org.openoffice.test.common.Testspace.*;
25*80a6f5c5SLiu Zhe import static org.openoffice.test.vcl.Tester.*;
26*80a6f5c5SLiu Zhe import static testlib.gui.AppTool.*;
27*80a6f5c5SLiu Zhe import static testlib.gui.UIMap.*;
28*80a6f5c5SLiu Zhe 
29*80a6f5c5SLiu Zhe import java.io.File;
30*80a6f5c5SLiu Zhe import java.util.ArrayList;
31*80a6f5c5SLiu Zhe import java.util.Collection;
32*80a6f5c5SLiu Zhe 
33*80a6f5c5SLiu Zhe import junit.framework.Assert;
34*80a6f5c5SLiu Zhe 
35*80a6f5c5SLiu Zhe import org.junit.After;
36*80a6f5c5SLiu Zhe import org.junit.Before;
37*80a6f5c5SLiu Zhe import org.junit.Rule;
38*80a6f5c5SLiu Zhe import org.junit.Test;
39*80a6f5c5SLiu Zhe import org.junit.runner.RunWith;
40*80a6f5c5SLiu Zhe import org.junit.runners.Parameterized;
41*80a6f5c5SLiu Zhe import org.junit.runners.Parameterized.Parameters;
42*80a6f5c5SLiu Zhe import org.openoffice.test.common.Condition;
43*80a6f5c5SLiu Zhe import org.openoffice.test.common.FileUtil;
44*80a6f5c5SLiu Zhe import org.openoffice.test.common.Logger;
45*80a6f5c5SLiu Zhe 
46*80a6f5c5SLiu Zhe import testlib.gui.SDTool;
47*80a6f5c5SLiu Zhe 
48*80a6f5c5SLiu Zhe @RunWith(Parameterized.class)
49*80a6f5c5SLiu Zhe public class TestSample {
50*80a6f5c5SLiu Zhe 
51*80a6f5c5SLiu Zhe     public static String repos = "samples";
52*80a6f5c5SLiu Zhe 
53*80a6f5c5SLiu Zhe     public static String[][] params = {};
54*80a6f5c5SLiu Zhe 
55*80a6f5c5SLiu Zhe     @Parameters
data()56*80a6f5c5SLiu Zhe     public static Collection<Object[]> data() {
57*80a6f5c5SLiu Zhe         File dir = new File(repos);
58*80a6f5c5SLiu Zhe         ArrayList<Object[]> list = new ArrayList<Object[]>();
59*80a6f5c5SLiu Zhe         collect(dir, list);
60*80a6f5c5SLiu Zhe         return list;
61*80a6f5c5SLiu Zhe     }
62*80a6f5c5SLiu Zhe 
63*80a6f5c5SLiu Zhe     /**
64*80a6f5c5SLiu Zhe      * @see <a href="http://www.ibm.com">Manual Case</a>
65*80a6f5c5SLiu Zhe      * @param dir
66*80a6f5c5SLiu Zhe      * @param list
67*80a6f5c5SLiu Zhe      */
collect(File dir, ArrayList<Object[]> list)68*80a6f5c5SLiu Zhe     public static void collect(File dir, ArrayList<Object[]> list) {
69*80a6f5c5SLiu Zhe         File[] files = dir.listFiles();
70*80a6f5c5SLiu Zhe         if (files == null)
71*80a6f5c5SLiu Zhe             return;
72*80a6f5c5SLiu Zhe 
73*80a6f5c5SLiu Zhe         for (File file : files) {
74*80a6f5c5SLiu Zhe             if (file.isDirectory()) {
75*80a6f5c5SLiu Zhe                 collect(file, list);
76*80a6f5c5SLiu Zhe             } else {
77*80a6f5c5SLiu Zhe                 String fileName = file.getName().toLowerCase();
78*80a6f5c5SLiu Zhe                 for (String[] param : params) {
79*80a6f5c5SLiu Zhe                     String filter = param[0];
80*80a6f5c5SLiu Zhe                     if (filter != null && fileName.matches(filter)) {
81*80a6f5c5SLiu Zhe                         Object[] data = { file, param[1], param[2] };
82*80a6f5c5SLiu Zhe                         list.add(data);
83*80a6f5c5SLiu Zhe                         System.out.println(file + param[1] + param[2]);
84*80a6f5c5SLiu Zhe                         break;
85*80a6f5c5SLiu Zhe                     }
86*80a6f5c5SLiu Zhe                 }
87*80a6f5c5SLiu Zhe             }
88*80a6f5c5SLiu Zhe         }
89*80a6f5c5SLiu Zhe     }
90*80a6f5c5SLiu Zhe 
91*80a6f5c5SLiu Zhe     private static final String writerFilter = ".*\\.((odt)|(ott)|(sxw)|(stw)|(doc)|(dot)|(docx)|(docm)|(dotx)|(dotm))$";
92*80a6f5c5SLiu Zhe     private static final String calcFilter = ".*\\.((ods)|(ots)|(sxc)|(stc)|(xls)|(xlt)|(xlsx)|(xltx)|(xlsm)|(xltm))$";
93*80a6f5c5SLiu Zhe     private static final String impressFilter = ".*\\.((odp)|(otp)|(sxi)|(sti)|(ppt)|(pot)|(pptx)|(pptm)|(potm)|(potx))$";
94*80a6f5c5SLiu Zhe     private static final String drawFilter = ".*\\.((odg)|(otg)|(sxd)|(sxt))$";
95*80a6f5c5SLiu Zhe     private static final String databaseFilter = ".*\\.(odb)$";
96*80a6f5c5SLiu Zhe 
97*80a6f5c5SLiu Zhe     @Rule
98*80a6f5c5SLiu Zhe     public Logger log = Logger.getLogger(this);
99*80a6f5c5SLiu Zhe     private File originalFile = null;
100*80a6f5c5SLiu Zhe     private String saveas = null;
101*80a6f5c5SLiu Zhe     private String editor = null;
102*80a6f5c5SLiu Zhe     private File file = null;
103*80a6f5c5SLiu Zhe     private String saveTo = null;
104*80a6f5c5SLiu Zhe     private boolean passed = false;
105*80a6f5c5SLiu Zhe 
TestSample(File file, String saveas, String editor)106*80a6f5c5SLiu Zhe     public TestSample(File file, String saveas, String editor) {
107*80a6f5c5SLiu Zhe         this.originalFile = file;
108*80a6f5c5SLiu Zhe         this.saveas = saveas;
109*80a6f5c5SLiu Zhe         this.editor = editor;
110*80a6f5c5SLiu Zhe     }
111*80a6f5c5SLiu Zhe 
112*80a6f5c5SLiu Zhe     /**
113*80a6f5c5SLiu Zhe      * @throws java.lang.Exception
114*80a6f5c5SLiu Zhe      */
115*80a6f5c5SLiu Zhe     @Before
setUp()116*80a6f5c5SLiu Zhe     public void setUp() {
117*80a6f5c5SLiu Zhe         app.start();
118*80a6f5c5SLiu Zhe 
119*80a6f5c5SLiu Zhe         FileUtil.deleteFile(getPath("temp"));
120*80a6f5c5SLiu Zhe         File temp = new File(getPath("temp"));
121*80a6f5c5SLiu Zhe         temp.mkdirs();
122*80a6f5c5SLiu Zhe         log.info("Load sample file from \"" + originalFile.getAbsolutePath() + "\"");
123*80a6f5c5SLiu Zhe         file = new File(temp + "/origin", "sample." + FileUtil.getFileExtName(originalFile.getName()) /*
124*80a6f5c5SLiu Zhe                                                                                                      * file
125*80a6f5c5SLiu Zhe                                                                                                      * .
126*80a6f5c5SLiu Zhe                                                                                                      * getName
127*80a6f5c5SLiu Zhe                                                                                                      * (
128*80a6f5c5SLiu Zhe                                                                                                      * )
129*80a6f5c5SLiu Zhe                                                                                                      */);
130*80a6f5c5SLiu Zhe         FileUtil.copyFile(originalFile, file); // We use the copy to do test
131*80a6f5c5SLiu Zhe         saveTo = getPath("temp/" + file.getName() + (saveas == null ? "" : "." + saveas));
132*80a6f5c5SLiu Zhe     }
133*80a6f5c5SLiu Zhe 
134*80a6f5c5SLiu Zhe     @After
tearDown()135*80a6f5c5SLiu Zhe     public void tearDown() {
136*80a6f5c5SLiu Zhe         if (!passed) {
137*80a6f5c5SLiu Zhe             // Collect the failed sample files.
138*80a6f5c5SLiu Zhe             File failedDir = new File(getPath("output/TestSample.Failed"));
139*80a6f5c5SLiu Zhe             FileUtil.copyFile(originalFile, new File(failedDir, originalFile.getName()));
140*80a6f5c5SLiu Zhe         }
141*80a6f5c5SLiu Zhe     }
142*80a6f5c5SLiu Zhe 
143*80a6f5c5SLiu Zhe     @Test
test()144*80a6f5c5SLiu Zhe     public void test() {
145*80a6f5c5SLiu Zhe         if (editor == null) {
146*80a6f5c5SLiu Zhe             String name = file.getName();
147*80a6f5c5SLiu Zhe             if (name.matches(writerFilter)) {
148*80a6f5c5SLiu Zhe                 testWriter();
149*80a6f5c5SLiu Zhe             } else if (name.matches(calcFilter)) {
150*80a6f5c5SLiu Zhe                 testCalc();
151*80a6f5c5SLiu Zhe             } else if (name.matches(impressFilter)) {
152*80a6f5c5SLiu Zhe                 testImpress();
153*80a6f5c5SLiu Zhe             } else if (name.matches(drawFilter)) {
154*80a6f5c5SLiu Zhe 
155*80a6f5c5SLiu Zhe             } else if (name.matches(databaseFilter)) {
156*80a6f5c5SLiu Zhe 
157*80a6f5c5SLiu Zhe             } else {
158*80a6f5c5SLiu Zhe                 Assert.assertTrue("It's supported", false);
159*80a6f5c5SLiu Zhe             }
160*80a6f5c5SLiu Zhe         } else {
161*80a6f5c5SLiu Zhe             if (editor.equals("writer"))
162*80a6f5c5SLiu Zhe                 testWriter();
163*80a6f5c5SLiu Zhe             if (editor.equals("calc"))
164*80a6f5c5SLiu Zhe                 testCalc();
165*80a6f5c5SLiu Zhe             if (editor.equals("impress"))
166*80a6f5c5SLiu Zhe                 testImpress();
167*80a6f5c5SLiu Zhe             if (editor.equals("draw"))
168*80a6f5c5SLiu Zhe                 testDraw();
169*80a6f5c5SLiu Zhe             if (editor.equals("database"))
170*80a6f5c5SLiu Zhe                 testDatabase();
171*80a6f5c5SLiu Zhe         }
172*80a6f5c5SLiu Zhe     }
173*80a6f5c5SLiu Zhe 
testDatabase()174*80a6f5c5SLiu Zhe     private void testDatabase() {
175*80a6f5c5SLiu Zhe         // TODO Auto-generated method stub
176*80a6f5c5SLiu Zhe 
177*80a6f5c5SLiu Zhe     }
178*80a6f5c5SLiu Zhe 
testDraw()179*80a6f5c5SLiu Zhe     private void testDraw() {
180*80a6f5c5SLiu Zhe         // TODO Auto-generated method stub
181*80a6f5c5SLiu Zhe 
182*80a6f5c5SLiu Zhe     }
183*80a6f5c5SLiu Zhe 
testWriter()184*80a6f5c5SLiu Zhe     public void testWriter() {
185*80a6f5c5SLiu Zhe         open(file.getAbsolutePath());
186*80a6f5c5SLiu Zhe         handleBlocker(writer);
187*80a6f5c5SLiu Zhe         sleep(10);
188*80a6f5c5SLiu Zhe 
189*80a6f5c5SLiu Zhe         // Assert.assertTrue("File Passed:" + file,
190*80a6f5c5SLiu Zhe         // writer.getCaption().contains(file.getName()));
191*80a6f5c5SLiu Zhe         saveAs(saveTo);
192*80a6f5c5SLiu Zhe         if (alienFormatDlg.exists(3))
193*80a6f5c5SLiu Zhe             alienFormatDlg.ok();
194*80a6f5c5SLiu Zhe         sleep(2);
195*80a6f5c5SLiu Zhe         writer.waitForEnabled(120, 2);
196*80a6f5c5SLiu Zhe         close();
197*80a6f5c5SLiu Zhe 
198*80a6f5c5SLiu Zhe         open(saveTo);
199*80a6f5c5SLiu Zhe         handleBlocker(writer);
200*80a6f5c5SLiu Zhe         sleep(10);
201*80a6f5c5SLiu Zhe 
202*80a6f5c5SLiu Zhe         // Assert.assertTrue("File Passed:" + file,
203*80a6f5c5SLiu Zhe         // writer.getCaption().contains(file.getName()));
204*80a6f5c5SLiu Zhe         close();
205*80a6f5c5SLiu Zhe         passed = true;
206*80a6f5c5SLiu Zhe     }
207*80a6f5c5SLiu Zhe 
testCalc()208*80a6f5c5SLiu Zhe     public void testCalc() {
209*80a6f5c5SLiu Zhe         startcenter.menuItem("File->Open...").select();
210*80a6f5c5SLiu Zhe         submitOpenDlg(file.getAbsolutePath());
211*80a6f5c5SLiu Zhe         handleBlocker(calc);
212*80a6f5c5SLiu Zhe         sleep(10); // Wait. Crash maybe occurs when the file is shown!
213*80a6f5c5SLiu Zhe 
214*80a6f5c5SLiu Zhe         // Assert.assertTrue("File Passed:" + file,
215*80a6f5c5SLiu Zhe         // calc.getCaption().contains(file.getName()));
216*80a6f5c5SLiu Zhe 
217*80a6f5c5SLiu Zhe         calc.menuItem("File->Save As...").select();
218*80a6f5c5SLiu Zhe         submitSaveDlg(saveTo);
219*80a6f5c5SLiu Zhe         if (alienFormatDlg.exists(3))
220*80a6f5c5SLiu Zhe             alienFormatDlg.ok();
221*80a6f5c5SLiu Zhe         sleep(2);
222*80a6f5c5SLiu Zhe 
223*80a6f5c5SLiu Zhe         new Condition() {
224*80a6f5c5SLiu Zhe             @Override
225*80a6f5c5SLiu Zhe             public boolean value() {
226*80a6f5c5SLiu Zhe                 if (msgBox_AdditionalRowsNotSaved.exists()) {
227*80a6f5c5SLiu Zhe                     msgBox_AdditionalRowsNotSaved.ok();
228*80a6f5c5SLiu Zhe                 }
229*80a6f5c5SLiu Zhe                 return calc.isEnabled();
230*80a6f5c5SLiu Zhe             }
231*80a6f5c5SLiu Zhe 
232*80a6f5c5SLiu Zhe         }.waitForTrue("Time out to wait the control to be enabled!", 120, 2);
233*80a6f5c5SLiu Zhe 
234*80a6f5c5SLiu Zhe         calc.menuItem("File->Close").select();
235*80a6f5c5SLiu Zhe         openStartcenter();
236*80a6f5c5SLiu Zhe         // Reopen the saved file
237*80a6f5c5SLiu Zhe         startcenter.menuItem("File->Open...").select();
238*80a6f5c5SLiu Zhe         submitOpenDlg(saveTo);
239*80a6f5c5SLiu Zhe         handleBlocker(calc);
240*80a6f5c5SLiu Zhe         sleep(10);
241*80a6f5c5SLiu Zhe 
242*80a6f5c5SLiu Zhe         // Assert.assertTrue("File Passed:" + file,
243*80a6f5c5SLiu Zhe         // calc.getCaption().contains(file.getName()));
244*80a6f5c5SLiu Zhe         calc.menuItem("File->Close").select();
245*80a6f5c5SLiu Zhe         passed = true;
246*80a6f5c5SLiu Zhe     }
247*80a6f5c5SLiu Zhe 
testImpress()248*80a6f5c5SLiu Zhe     public void testImpress() {
249*80a6f5c5SLiu Zhe         startcenter.menuItem("File->Open...").select();
250*80a6f5c5SLiu Zhe         submitOpenDlg(file.getAbsolutePath());
251*80a6f5c5SLiu Zhe         handleBlocker(impress, impressSlideSorter, impressOutline, impressHandout);
252*80a6f5c5SLiu Zhe         sleep(10); // Wait. Crash maybe occurs when the file is shown!
253*80a6f5c5SLiu Zhe         SDTool.getActiveView().menuItem("View->Normal").select();
254*80a6f5c5SLiu Zhe 
255*80a6f5c5SLiu Zhe         // Assert.assertTrue("File Passed:" + file,
256*80a6f5c5SLiu Zhe         // impress.getCaption().contains(file.getName()));
257*80a6f5c5SLiu Zhe 
258*80a6f5c5SLiu Zhe         impress.menuItem("File->Save As...").select();
259*80a6f5c5SLiu Zhe         submitSaveDlg(saveTo);
260*80a6f5c5SLiu Zhe         if (alienFormatDlg.exists(3))
261*80a6f5c5SLiu Zhe             alienFormatDlg.ok();
262*80a6f5c5SLiu Zhe         sleep(2);
263*80a6f5c5SLiu Zhe         impress.waitForEnabled(120, 2);
264*80a6f5c5SLiu Zhe         impress.menuItem("File->Close").select();
265*80a6f5c5SLiu Zhe         openStartcenter();
266*80a6f5c5SLiu Zhe         // Reopen the saved file
267*80a6f5c5SLiu Zhe         startcenter.menuItem("File->Open...").select();
268*80a6f5c5SLiu Zhe         submitOpenDlg(saveTo);
269*80a6f5c5SLiu Zhe         handleBlocker(impress);
270*80a6f5c5SLiu Zhe         sleep(10); // Wait.
271*80a6f5c5SLiu Zhe 
272*80a6f5c5SLiu Zhe         // Assert.assertTrue("File Passed:" + file,
273*80a6f5c5SLiu Zhe         // impress.getCaption().contains(file.getName()));
274*80a6f5c5SLiu Zhe         impress.menuItem("File->Close").select();
275*80a6f5c5SLiu Zhe         passed = true;
276*80a6f5c5SLiu Zhe     }
277*80a6f5c5SLiu Zhe }
278