1b164ae3eSLei De Bin /**************************************************************
2b164ae3eSLei De Bin  *
3b164ae3eSLei De Bin  * Licensed to the Apache Software Foundation (ASF) under one
4b164ae3eSLei De Bin  * or more contributor license agreements.  See the NOTICE file
5b164ae3eSLei De Bin  * distributed with this work for additional information
6b164ae3eSLei De Bin  * regarding copyright ownership.  The ASF licenses this file
7b164ae3eSLei De Bin  * to you under the Apache License, Version 2.0 (the
8b164ae3eSLei De Bin  * "License"); you may not use this file except in compliance
9b164ae3eSLei De Bin  * with the License.  You may obtain a copy of the License at
10b164ae3eSLei De Bin  *
11b164ae3eSLei De Bin  *   http://www.apache.org/licenses/LICENSE-2.0
12b164ae3eSLei De Bin  *
13b164ae3eSLei De Bin  * Unless required by applicable law or agreed to in writing,
14b164ae3eSLei De Bin  * software distributed under the License is distributed on an
15b164ae3eSLei De Bin  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b164ae3eSLei De Bin  * KIND, either express or implied.  See the License for the
17b164ae3eSLei De Bin  * specific language governing permissions and limitations
18b164ae3eSLei De Bin  * under the License.
19b164ae3eSLei De Bin  *
20b164ae3eSLei De Bin  *************************************************************/
21b164ae3eSLei De Bin 
22*80a6f5c5SLiu Zhe package svt.gui;
23faa4b864SLei De Bin 
2495269d92SLiu Zhe import static org.openoffice.test.common.Testspace.*;
252fc12ec5SLiu Zhe import static org.openoffice.test.vcl.Tester.*;
26b4d2d410SLiu Zhe import static testlib.gui.AppTool.*;
2795269d92SLiu Zhe import static testlib.gui.UIMap.*;
282fc12ec5SLiu Zhe 
29faa4b864SLei De Bin import java.io.File;
30faa4b864SLei De Bin import java.util.ArrayList;
31faa4b864SLei De Bin import java.util.Collection;
32faa4b864SLei De Bin 
33faa4b864SLei De Bin import junit.framework.Assert;
34faa4b864SLei De Bin 
35faa4b864SLei De Bin import org.junit.After;
36faa4b864SLei De Bin import org.junit.Before;
37faa4b864SLei De Bin import org.junit.Rule;
38faa4b864SLei De Bin import org.junit.Test;
39faa4b864SLei De Bin import org.junit.runner.RunWith;
40faa4b864SLei De Bin import org.junit.runners.Parameterized;
41faa4b864SLei De Bin import org.junit.runners.Parameterized.Parameters;
42faa4b864SLei De Bin import org.openoffice.test.common.Condition;
43faa4b864SLei De Bin import org.openoffice.test.common.FileUtil;
4422a14f28SLiu Zhe import org.openoffice.test.common.Logger;
45faa4b864SLei De Bin 
46b4d2d410SLiu Zhe import testlib.gui.SDTool;
47faa4b864SLei De Bin 
48faa4b864SLei De Bin @RunWith(Parameterized.class)
49faa4b864SLei De Bin public class TestSample {
5022a14f28SLiu Zhe 
51faa4b864SLei De Bin 	public static String repos = "samples";
5222a14f28SLiu Zhe 
53faa4b864SLei De Bin 	public static String[][] params = {};
5422a14f28SLiu Zhe 
55faa4b864SLei De Bin 	@Parameters
data()5622a14f28SLiu Zhe 	public static Collection<Object[]> data() {
57faa4b864SLei De Bin 		File dir = new File(repos);
58faa4b864SLei De Bin 		ArrayList<Object[]> list = new ArrayList<Object[]>();
59faa4b864SLei De Bin 		collect(dir, list);
60faa4b864SLei De Bin 		return list;
61faa4b864SLei De Bin 	}
62faa4b864SLei De Bin 
63faa4b864SLei De Bin 	/**
64faa4b864SLei De Bin 	 * @see <a href="http://www.ibm.com">Manual Case</a>
65faa4b864SLei De Bin 	 * @param dir
66faa4b864SLei De Bin 	 * @param list
67faa4b864SLei De Bin 	 */
collect(File dir, ArrayList<Object[]> list)6822a14f28SLiu Zhe 	public static void collect(File dir, ArrayList<Object[]> list) {
6922a14f28SLiu Zhe 		File[] files = dir.listFiles();
7022a14f28SLiu Zhe 		if (files == null)
7122a14f28SLiu Zhe 			return;
7222a14f28SLiu Zhe 
7322a14f28SLiu Zhe 		for (File file : files) {
7422a14f28SLiu Zhe 			if (file.isDirectory()) {
7522a14f28SLiu Zhe 				collect(file, list);
7622a14f28SLiu Zhe 			} else {
7722a14f28SLiu Zhe 				String fileName = file.getName().toLowerCase();
7822a14f28SLiu Zhe 				for (String[] param : params) {
7922a14f28SLiu Zhe 					String filter = param[0];
8022a14f28SLiu Zhe 					if (filter != null && fileName.matches(filter)) {
8122a14f28SLiu Zhe 						Object[] data = { file, param[1], param[2] };
8222a14f28SLiu Zhe 						list.add(data);
8322a14f28SLiu Zhe 						System.out.println(file + param[1] + param[2]);
8422a14f28SLiu Zhe 						break;
8522a14f28SLiu Zhe 					}
8622a14f28SLiu Zhe 				}
8722a14f28SLiu Zhe 			}
8822a14f28SLiu Zhe 		}
8922a14f28SLiu Zhe 	}
90faa4b864SLei De Bin 
91faa4b864SLei De Bin 	private static final String writerFilter = ".*\\.((odt)|(ott)|(sxw)|(stw)|(doc)|(dot)|(docx)|(docm)|(dotx)|(dotm))$";
92faa4b864SLei De Bin 	private static final String calcFilter = ".*\\.((ods)|(ots)|(sxc)|(stc)|(xls)|(xlt)|(xlsx)|(xltx)|(xlsm)|(xltm))$";
93faa4b864SLei De Bin 	private static final String impressFilter = ".*\\.((odp)|(otp)|(sxi)|(sti)|(ppt)|(pot)|(pptx)|(pptm)|(potm)|(potx))$";
94faa4b864SLei De Bin 	private static final String drawFilter = ".*\\.((odg)|(otg)|(sxd)|(sxt))$";
95faa4b864SLei De Bin 	private static final String databaseFilter = ".*\\.(odb)$";
9622a14f28SLiu Zhe 
97faa4b864SLei De Bin 	@Rule
9822a14f28SLiu Zhe 	public Logger log = Logger.getLogger(this);
99faa4b864SLei De Bin 	private File originalFile = null;
100faa4b864SLei De Bin 	private String saveas = null;
101faa4b864SLei De Bin 	private String editor = null;
102faa4b864SLei De Bin 	private File file = null;
103faa4b864SLei De Bin 	private String saveTo = null;
104faa4b864SLei De Bin 	private boolean passed = false;
10522a14f28SLiu Zhe 
TestSample(File file, String saveas, String editor)106faa4b864SLei De Bin 	public TestSample(File file, String saveas, String editor) {
107faa4b864SLei De Bin 		this.originalFile = file;
108faa4b864SLei De Bin 		this.saveas = saveas;
109faa4b864SLei De Bin 		this.editor = editor;
110faa4b864SLei De Bin 	}
11122a14f28SLiu Zhe 
112faa4b864SLei De Bin 	/**
113faa4b864SLei De Bin 	 * @throws java.lang.Exception
114faa4b864SLei De Bin 	 */
115faa4b864SLei De Bin 	@Before
setUp()116faa4b864SLei De Bin 	public void setUp() {
1172fc12ec5SLiu Zhe 		app.start();
11822a14f28SLiu Zhe 
11995269d92SLiu Zhe 		FileUtil.deleteFile(getPath("temp"));
12095269d92SLiu Zhe 		File temp = new File(getPath("temp"));
121faa4b864SLei De Bin 		temp.mkdirs();
12222a14f28SLiu Zhe 		log.info("Load sample file from \"" + originalFile.getAbsolutePath() + "\"");
12322a14f28SLiu Zhe 		file = new File(temp + "/origin", "sample." + FileUtil.getFileExtName(originalFile.getName()) /*
12422a14f28SLiu Zhe 																									 * file
12522a14f28SLiu Zhe 																									 * .
12622a14f28SLiu Zhe 																									 * getName
12722a14f28SLiu Zhe 																									 * (
12822a14f28SLiu Zhe 																									 * )
12922a14f28SLiu Zhe 																									 */);
130faa4b864SLei De Bin 		FileUtil.copyFile(originalFile, file); // We use the copy to do test
13122a14f28SLiu Zhe 		saveTo = getPath("temp/" + file.getName() + (saveas == null ? "" : "." + saveas));
132faa4b864SLei De Bin 	}
133faa4b864SLei De Bin 
134faa4b864SLei De Bin 	@After
tearDown()135faa4b864SLei De Bin 	public void tearDown() {
136faa4b864SLei De Bin 		if (!passed) {
137faa4b864SLei De Bin 			// Collect the failed sample files.
13895269d92SLiu Zhe 			File failedDir = new File(getPath("output/TestSample.Failed"));
139faa4b864SLei De Bin 			FileUtil.copyFile(originalFile, new File(failedDir, originalFile.getName()));
140faa4b864SLei De Bin 		}
141faa4b864SLei De Bin 	}
14222a14f28SLiu Zhe 
143faa4b864SLei De Bin 	@Test
test()144faa4b864SLei De Bin 	public void test() {
145faa4b864SLei De Bin 		if (editor == null) {
146faa4b864SLei De Bin 			String name = file.getName();
147faa4b864SLei De Bin 			if (name.matches(writerFilter)) {
148faa4b864SLei De Bin 				testWriter();
149faa4b864SLei De Bin 			} else if (name.matches(calcFilter)) {
150faa4b864SLei De Bin 				testCalc();
151faa4b864SLei De Bin 			} else if (name.matches(impressFilter)) {
152faa4b864SLei De Bin 				testImpress();
153faa4b864SLei De Bin 			} else if (name.matches(drawFilter)) {
15422a14f28SLiu Zhe 
155faa4b864SLei De Bin 			} else if (name.matches(databaseFilter)) {
15622a14f28SLiu Zhe 
157faa4b864SLei De Bin 			} else {
158faa4b864SLei De Bin 				Assert.assertTrue("It's supported", false);
159faa4b864SLei De Bin 			}
160faa4b864SLei De Bin 		} else {
161faa4b864SLei De Bin 			if (editor.equals("writer"))
162faa4b864SLei De Bin 				testWriter();
163faa4b864SLei De Bin 			if (editor.equals("calc"))
164faa4b864SLei De Bin 				testCalc();
165faa4b864SLei De Bin 			if (editor.equals("impress"))
166faa4b864SLei De Bin 				testImpress();
167faa4b864SLei De Bin 			if (editor.equals("draw"))
168faa4b864SLei De Bin 				testDraw();
169faa4b864SLei De Bin 			if (editor.equals("database"))
170faa4b864SLei De Bin 				testDatabase();
171faa4b864SLei De Bin 		}
172faa4b864SLei De Bin 	}
173faa4b864SLei De Bin 
testDatabase()174faa4b864SLei De Bin 	private void testDatabase() {
175faa4b864SLei De Bin 		// TODO Auto-generated method stub
17622a14f28SLiu Zhe 
177faa4b864SLei De Bin 	}
178faa4b864SLei De Bin 
testDraw()179faa4b864SLei De Bin 	private void testDraw() {
180faa4b864SLei De Bin 		// TODO Auto-generated method stub
18122a14f28SLiu Zhe 
182faa4b864SLei De Bin 	}
183faa4b864SLei De Bin 
testWriter()184faa4b864SLei De Bin 	public void testWriter() {
185fc8fec4eSLiu Zhe 		open(file.getAbsolutePath());
186faa4b864SLei De Bin 		handleBlocker(writer);
187faa4b864SLei De Bin 		sleep(10);
18822a14f28SLiu Zhe 
18922a14f28SLiu Zhe 		// Assert.assertTrue("File Passed:" + file,
19022a14f28SLiu Zhe 		// writer.getCaption().contains(file.getName()));
191fc8fec4eSLiu Zhe 		saveAs(saveTo);
192b4d2d410SLiu Zhe 		if (alienFormatDlg.exists(3))
193b4d2d410SLiu Zhe 			alienFormatDlg.ok();
194faa4b864SLei De Bin 		sleep(2);
195faa4b864SLei De Bin 		writer.waitForEnabled(120, 2);
196fc8fec4eSLiu Zhe 		close();
197fc8fec4eSLiu Zhe 
198fc8fec4eSLiu Zhe 		open(saveTo);
199faa4b864SLei De Bin 		handleBlocker(writer);
200faa4b864SLei De Bin 		sleep(10);
20122a14f28SLiu Zhe 
20222a14f28SLiu Zhe 		// Assert.assertTrue("File Passed:" + file,
20322a14f28SLiu Zhe 		// writer.getCaption().contains(file.getName()));
204fc8fec4eSLiu Zhe 		close();
205faa4b864SLei De Bin 		passed = true;
206faa4b864SLei De Bin 	}
207faa4b864SLei De Bin 
testCalc()208faa4b864SLei De Bin 	public void testCalc() {
209faa4b864SLei De Bin 		startcenter.menuItem("File->Open...").select();
210faa4b864SLei De Bin 		submitOpenDlg(file.getAbsolutePath());
211faa4b864SLei De Bin 		handleBlocker(calc);
212faa4b864SLei De Bin 		sleep(10); // Wait. Crash maybe occurs when the file is shown!
21322a14f28SLiu Zhe 
21422a14f28SLiu Zhe 		// Assert.assertTrue("File Passed:" + file,
21522a14f28SLiu Zhe 		// calc.getCaption().contains(file.getName()));
21622a14f28SLiu Zhe 
217faa4b864SLei De Bin 		calc.menuItem("File->Save As...").select();
21822a14f28SLiu Zhe 		submitSaveDlg(saveTo);
219b4d2d410SLiu Zhe 		if (alienFormatDlg.exists(3))
220b4d2d410SLiu Zhe 			alienFormatDlg.ok();
221faa4b864SLei De Bin 		sleep(2);
222faa4b864SLei De Bin 
223faa4b864SLei De Bin 		new Condition() {
224faa4b864SLei De Bin 			@Override
225faa4b864SLei De Bin 			public boolean value() {
226b4d2d410SLiu Zhe 				if (msgBox_AdditionalRowsNotSaved.exists()) {
227b4d2d410SLiu Zhe 					msgBox_AdditionalRowsNotSaved.ok();
228faa4b864SLei De Bin 				}
229faa4b864SLei De Bin 				return calc.isEnabled();
230faa4b864SLei De Bin 			}
231faa4b864SLei De Bin 
23222a14f28SLiu Zhe 		}.waitForTrue("Time out to wait the control to be enabled!", 120, 2);
233faa4b864SLei De Bin 
234faa4b864SLei De Bin 		calc.menuItem("File->Close").select();
235faa4b864SLei De Bin 		openStartcenter();
236faa4b864SLei De Bin 		// Reopen the saved file
237faa4b864SLei De Bin 		startcenter.menuItem("File->Open...").select();
238faa4b864SLei De Bin 		submitOpenDlg(saveTo);
239faa4b864SLei De Bin 		handleBlocker(calc);
240faa4b864SLei De Bin 		sleep(10);
24122a14f28SLiu Zhe 
24222a14f28SLiu Zhe 		// Assert.assertTrue("File Passed:" + file,
24322a14f28SLiu Zhe 		// calc.getCaption().contains(file.getName()));
244faa4b864SLei De Bin 		calc.menuItem("File->Close").select();
245faa4b864SLei De Bin 		passed = true;
246faa4b864SLei De Bin 	}
24722a14f28SLiu Zhe 
testImpress()248faa4b864SLei De Bin 	public void testImpress() {
249faa4b864SLei De Bin 		startcenter.menuItem("File->Open...").select();
250faa4b864SLei De Bin 		submitOpenDlg(file.getAbsolutePath());
251b4d2d410SLiu Zhe 		handleBlocker(impress, impressSlideSorter, impressOutline, impressHandout);
252faa4b864SLei De Bin 		sleep(10); // Wait. Crash maybe occurs when the file is shown!
253b4d2d410SLiu Zhe 		SDTool.getActiveView().menuItem("View->Normal").select();
25422a14f28SLiu Zhe 
25522a14f28SLiu Zhe 		// Assert.assertTrue("File Passed:" + file,
25622a14f28SLiu Zhe 		// impress.getCaption().contains(file.getName()));
25722a14f28SLiu Zhe 
258faa4b864SLei De Bin 		impress.menuItem("File->Save As...").select();
25922a14f28SLiu Zhe 		submitSaveDlg(saveTo);
260b4d2d410SLiu Zhe 		if (alienFormatDlg.exists(3))
261b4d2d410SLiu Zhe 			alienFormatDlg.ok();
262faa4b864SLei De Bin 		sleep(2);
263faa4b864SLei De Bin 		impress.waitForEnabled(120, 2);
264faa4b864SLei De Bin 		impress.menuItem("File->Close").select();
265faa4b864SLei De Bin 		openStartcenter();
266faa4b864SLei De Bin 		// Reopen the saved file
267faa4b864SLei De Bin 		startcenter.menuItem("File->Open...").select();
268faa4b864SLei De Bin 		submitOpenDlg(saveTo);
269faa4b864SLei De Bin 		handleBlocker(impress);
27022a14f28SLiu Zhe 		sleep(10); // Wait.
27122a14f28SLiu Zhe 
27222a14f28SLiu Zhe 		// Assert.assertTrue("File Passed:" + file,
27322a14f28SLiu Zhe 		// impress.getCaption().contains(file.getName()));
274faa4b864SLei De Bin 		impress.menuItem("File->Close").select();
275faa4b864SLei De Bin 		passed = true;
276faa4b864SLei De Bin 	}
277faa4b864SLei De Bin }
278