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 
2295269d92SLiu Zhe package testlib.gui;
2322a14f28SLiu Zhe 
24b4d2d410SLiu Zhe import static org.openoffice.test.common.Testspace.*;
2595269d92SLiu Zhe import static testlib.gui.UIMap.*;
26faa4b864SLei De Bin 
27faa4b864SLei De Bin import org.openoffice.test.common.Condition;
28faa4b864SLei De Bin import org.openoffice.test.common.FileUtil;
29faa4b864SLei De Bin import org.openoffice.test.common.SystemUtil;
30afcfe8bdSLiu Zhe import org.openoffice.test.common.Testspace;
31faa4b864SLei De Bin import org.openoffice.test.vcl.Tester;
32faa4b864SLei De Bin import org.openoffice.test.vcl.widgets.VclWindow;
33faa4b864SLei De Bin 
34b4d2d410SLiu Zhe public class AppTool extends Tester {
35faa4b864SLei De Bin 	static {
mkdirs()36faa4b864SLei De Bin 		Testspace.getFile("temp").mkdirs();
37faa4b864SLei De Bin 		// TODO move these shortcut into a file
38faa4b864SLei De Bin 		setCustomizedShortcut("copy", "ctrl", "c");
39faa4b864SLei De Bin 		setCustomizedShortcut("select_all", "ctrl", "a");
40faa4b864SLei De Bin 		setCustomizedShortcut("paste", "ctrl", "v");
41faa4b864SLei De Bin 		setCustomizedShortcut("cut", "ctrl", "x");
42faa4b864SLei De Bin 		setCustomizedShortcut("startcenter", "ctrl", "n");
43faa4b864SLei De Bin 		setCustomizedShortcut("find", "ctrl", "f");
44faa4b864SLei De Bin 		setCustomizedShortcut("undo", "ctrl", "z");
45faa4b864SLei De Bin 		setCustomizedShortcut("redo", "ctrl", "y");
46faa4b864SLei De Bin 		if (SystemUtil.isMac()) {
47faa4b864SLei De Bin 			setCustomizedShortcut("copy", "command", "c");
48faa4b864SLei De Bin 			setCustomizedShortcut("select_all", "command", "a");
49faa4b864SLei De Bin 			setCustomizedShortcut("paste", "command", "v");
50faa4b864SLei De Bin 			setCustomizedShortcut("cut", "command", "x");
51faa4b864SLei De Bin 			setCustomizedShortcut("startcenter", "command", "n");
52faa4b864SLei De Bin 			setCustomizedShortcut("find", "command", "f");
53faa4b864SLei De Bin 			setCustomizedShortcut("undo", "command", "z");
54faa4b864SLei De Bin 			setCustomizedShortcut("redo", "command", "shift", "z");
55faa4b864SLei De Bin 		} else if (SystemUtil.isLinux()) {
5622a14f28SLiu Zhe 
5722a14f28SLiu Zhe 		}
58faa4b864SLei De Bin 	}
5922a14f28SLiu Zhe 
newTextDocument()60b4d2d410SLiu Zhe 	public static void newTextDocument() {
61b4d2d410SLiu Zhe 		app.dispatch("private:factory/swriter");
62b4d2d410SLiu Zhe 		writer.waitForExistence(10, 2);
63b4d2d410SLiu Zhe 		sleep(1);
64b4d2d410SLiu Zhe 	}
65b4d2d410SLiu Zhe 
newSpreadsheet()66b4d2d410SLiu Zhe 	public static void newSpreadsheet() {
67b4d2d410SLiu Zhe 		app.dispatch("private:factory/scalc");
68b4d2d410SLiu Zhe 		calc.waitForExistence(10, 2);
69b4d2d410SLiu Zhe 		sleep(1);
70b4d2d410SLiu Zhe 	}
71b4d2d410SLiu Zhe 
newPresentation()72b4d2d410SLiu Zhe 	public static void newPresentation() {
73b4d2d410SLiu Zhe 		app.dispatch("private:factory/simpress");
74b4d2d410SLiu Zhe 		impress.waitForExistence(10, 2);
75b4d2d410SLiu Zhe 		sleep(1);
76b4d2d410SLiu Zhe 	}
77b4d2d410SLiu Zhe 
newDrawing()78b4d2d410SLiu Zhe 	public static void newDrawing() {
79b4d2d410SLiu Zhe 		app.dispatch("private:factory/sdraw");
80b4d2d410SLiu Zhe 		draw.waitForExistence(10, 2);
81b4d2d410SLiu Zhe 		sleep(1);
82b4d2d410SLiu Zhe 	}
83b4d2d410SLiu Zhe 
newFormula()84b4d2d410SLiu Zhe 	public static void newFormula() {
85b4d2d410SLiu Zhe 		app.dispatch("private:factory/smath");
86b4d2d410SLiu Zhe 		mathEditWindow.waitForExistence(10, 2);
87b4d2d410SLiu Zhe 		sleep(1);
88b4d2d410SLiu Zhe 	}
89b4d2d410SLiu Zhe 
open(String path)90b4d2d410SLiu Zhe 	public static void open(String path) {
91b4d2d410SLiu Zhe 		app.dispatch(".uno:Open");
92b4d2d410SLiu Zhe 		submitOpenDlg(getPath(path));
93b4d2d410SLiu Zhe 	}
94b4d2d410SLiu Zhe 
saveAs(String path)95b4d2d410SLiu Zhe 	public static void saveAs(String path) {
96b4d2d410SLiu Zhe 		app.dispatch(".uno:SaveAs");
97b4d2d410SLiu Zhe 		submitSaveDlg(getPath(path));
98b4d2d410SLiu Zhe 		if (alienFormatDlg.exists(3))
99b4d2d410SLiu Zhe 			alienFormatDlg.ok();
100*f76c5999SHerbert Dürr 		if( activeMsgBox.exists(1)) {
101*f76c5999SHerbert Dürr 			String msg = activeMsgBox.getMessage();
102*f76c5999SHerbert Dürr 			// #i123142# confirm overwriting of test files
103*f76c5999SHerbert Dürr 			if( msg.indexOf( "overwrite changes") >= 0)
104*f76c5999SHerbert Dürr 				activeMsgBox.yes();
105*f76c5999SHerbert Dürr 		}
106*f76c5999SHerbert Dürr 		app.waitSlot(5 * 60); // 5 minutes
107b4d2d410SLiu Zhe 	}
108b4d2d410SLiu Zhe 
close()109b4d2d410SLiu Zhe 	public static void close() {
110b4d2d410SLiu Zhe 		app.dispatch(".uno:CloseDoc");
111b4d2d410SLiu Zhe 	}
112b4d2d410SLiu Zhe 
saveAndReopen(String path)113c37bcbf4SLi Feng Wang 	public static void saveAndReopen(String path){
114c37bcbf4SLi Feng Wang 		saveAs(path);
115c37bcbf4SLi Feng Wang 		close();
116c37bcbf4SLi Feng Wang 		open(path);
117c37bcbf4SLi Feng Wang 	}
118c37bcbf4SLi Feng Wang 
discard()119b4d2d410SLiu Zhe 	public static void discard() {
120b4d2d410SLiu Zhe 		app.dispatch(".uno:CloseDoc");
121b4d2d410SLiu Zhe 		if (activeMsgBox.exists(2))
122b4d2d410SLiu Zhe 			activeMsgBox.no();
123b4d2d410SLiu Zhe 	}
124b4d2d410SLiu Zhe 
typeKeys(String keys)125b6113d73SLiu Zhe 	public static void typeKeys(String keys) {
126b6113d73SLiu Zhe 		Tester.typeKeys(keys);
127b6113d73SLiu Zhe 	}
1287dd2b5bbSLiu Zhe 
openStartcenter()129faa4b864SLei De Bin 	public static void openStartcenter() {
130faa4b864SLei De Bin 		if (startcenter.exists())
131faa4b864SLei De Bin 			return;
132f46d12a6SLiu Zhe 
133faa4b864SLei De Bin 		if (SystemUtil.isMac()) {
134140164b7SLiu Zhe 			SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
135faa4b864SLei De Bin 			typeKeys("<command n>");
136faa4b864SLei De Bin 		}
137f46d12a6SLiu Zhe 
138faa4b864SLei De Bin 	}
139faa4b864SLei De Bin 
copyAll()140b4d2d410SLiu Zhe 	public static String copyAll() {
141b4d2d410SLiu Zhe 		app.setClipboard(".d.i.r.t.y.");
142b4d2d410SLiu Zhe 		try {
143b4d2d410SLiu Zhe 			app.dispatch(".uno:SelectAll");
144b4d2d410SLiu Zhe 		} catch (Exception e) {
145b4d2d410SLiu Zhe 			app.dispatch(".uno:Select");
146b4d2d410SLiu Zhe 		}
147b4d2d410SLiu Zhe 		app.dispatch(".uno:Copy");
148b4d2d410SLiu Zhe 		return app.getClipboard();
149b4d2d410SLiu Zhe 	}
150b4d2d410SLiu Zhe 
submitOpenDlg(String path)151faa4b864SLei De Bin 	public static void submitOpenDlg(String path) {
152b4d2d410SLiu Zhe 		filePickerPath.setText(path);
153b4d2d410SLiu Zhe 		filePickerOpen.click();
154faa4b864SLei De Bin 	}
15595269d92SLiu Zhe 
submitSaveDlg(String path)156faa4b864SLei De Bin 	public static void submitSaveDlg(String path) {
157b4d2d410SLiu Zhe 		fileSavePath.setText(path);
158faa4b864SLei De Bin 		String extName = FileUtil.getFileExtName(path).toLowerCase();
159b4d2d410SLiu Zhe 		String[] filters = fileSaveFileType.getItemsText();
160faa4b864SLei De Bin 		int i = 0;
161faa4b864SLei De Bin 		for (; i < filters.length; i++) {
162faa4b864SLei De Bin 			String f = filters[i];
163faa4b864SLei De Bin 			int dotIndex = f.lastIndexOf(".");
164faa4b864SLei De Bin 			if (dotIndex == -1)
165faa4b864SLei De Bin 				continue;
166faa4b864SLei De Bin 			if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
167faa4b864SLei De Bin 				break;
168faa4b864SLei De Bin 		}
169faa4b864SLei De Bin 		if (i == filters.length)
170faa4b864SLei De Bin 			throw new RuntimeException("Can't find the supported doc format!");
17122a14f28SLiu Zhe 
172b4d2d410SLiu Zhe 		fileSaveFileType.select(i);
173b4d2d410SLiu Zhe 		fileSaveSave.click();
174faa4b864SLei De Bin 	}
17522a14f28SLiu Zhe 
submitSaveDlg(String path, String ext)176faa4b864SLei De Bin 	public static void submitSaveDlg(String path, String ext) {
177b4d2d410SLiu Zhe 		fileSavePath.setText(path);
178faa4b864SLei De Bin 		if (ext != null) {
179faa4b864SLei De Bin 			// change filter
180b4d2d410SLiu Zhe 			String[] filters = fileSaveFileType.getItemsText();
181faa4b864SLei De Bin 			int i = 0;
182faa4b864SLei De Bin 			for (; i < filters.length; i++) {
183faa4b864SLei De Bin 				String f = filters[i];
184faa4b864SLei De Bin 				int dotIndex = f.lastIndexOf(".");
185faa4b864SLei De Bin 				if (dotIndex == -1)
186faa4b864SLei De Bin 					continue;
187faa4b864SLei De Bin 				if (ext.equals(f.substring(dotIndex + 1, f.length() - 1)))
188faa4b864SLei De Bin 					break;
189faa4b864SLei De Bin 			}
190faa4b864SLei De Bin 			if (i == filters.length)
191faa4b864SLei De Bin 				throw new RuntimeException("Can't find the supported doc format!");
192faa4b864SLei De Bin 		}
193b4d2d410SLiu Zhe 		fileSaveFileType.click();
194faa4b864SLei De Bin 	}
19522a14f28SLiu Zhe 
handleBlocker(final VclWindow... windows)196faa4b864SLei De Bin 	public static void handleBlocker(final VclWindow... windows) {
197faa4b864SLei De Bin 		new Condition() {
198faa4b864SLei De Bin 			@Override
199faa4b864SLei De Bin 			public boolean value() {
200b4d2d410SLiu Zhe 				if (activeMsgBox.exists()) {
201faa4b864SLei De Bin 					try {
202b4d2d410SLiu Zhe 						activeMsgBox.ok();
203faa4b864SLei De Bin 					} catch (Exception e) {
204faa4b864SLei De Bin 						try {
205b4d2d410SLiu Zhe 							activeMsgBox.yes();
206faa4b864SLei De Bin 						} catch (Exception e1) {
207faa4b864SLei De Bin 						}
208faa4b864SLei De Bin 					}
209faa4b864SLei De Bin 				}
21022a14f28SLiu Zhe 
211faa4b864SLei De Bin 				boolean shown = false;
21222a14f28SLiu Zhe 
21322a14f28SLiu Zhe 				for (VclWindow w : windows) {
214faa4b864SLei De Bin 					if (w.exists()) {
215faa4b864SLei De Bin 						shown = true;
216faa4b864SLei De Bin 						break;
217faa4b864SLei De Bin 					}
218faa4b864SLei De Bin 				}
21922a14f28SLiu Zhe 
220faa4b864SLei De Bin 				if (!shown)
221faa4b864SLei De Bin 					return false;
22222a14f28SLiu Zhe 
223b4d2d410SLiu Zhe 				if (activeMsgBox.exists(2)) {
224faa4b864SLei De Bin 					try {
225b4d2d410SLiu Zhe 						activeMsgBox.ok();
226faa4b864SLei De Bin 					} catch (Exception e) {
227faa4b864SLei De Bin 						try {
228b4d2d410SLiu Zhe 							activeMsgBox.yes();
229faa4b864SLei De Bin 						} catch (Exception e1) {
230faa4b864SLei De Bin 						}
231faa4b864SLei De Bin 					}
232faa4b864SLei De Bin 				}
23322a14f28SLiu Zhe 
234faa4b864SLei De Bin 				return true;
235faa4b864SLei De Bin 			}
23622a14f28SLiu Zhe 
23722a14f28SLiu Zhe 		}.waitForTrue("Time out wait window to be active.", 120, 2);
238faa4b864SLei De Bin 	}
239f46d12a6SLiu Zhe 
insertPicture(String path)240f46d12a6SLiu Zhe 	public static void insertPicture(String path) {
241f46d12a6SLiu Zhe 		app.dispatch(".uno:InsertGraphic");
242f46d12a6SLiu Zhe 		submitOpenDlg(getPath(path));
243f46d12a6SLiu Zhe 	}
244faa4b864SLei De Bin }
245