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