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 
24*b4d2d410SLiu Zhe import static org.openoffice.test.common.Testspace.*;
25*b4d2d410SLiu Zhe import static org.openoffice.test.vcl.Tester.*;
26*b4d2d410SLiu Zhe import static testlib.gui.AppTool.*;
2795269d92SLiu Zhe import static testlib.gui.UIMap.*;
28faa4b864SLei De Bin 
29faa4b864SLei De Bin import org.openoffice.test.common.Condition;
30faa4b864SLei De Bin import org.openoffice.test.common.FileUtil;
31faa4b864SLei De Bin import org.openoffice.test.common.SystemUtil;
32afcfe8bdSLiu Zhe import org.openoffice.test.common.Testspace;
33faa4b864SLei De Bin import org.openoffice.test.vcl.Tester;
34faa4b864SLei De Bin import org.openoffice.test.vcl.widgets.VclWindow;
35faa4b864SLei De Bin 
36*b4d2d410SLiu Zhe public class AppTool extends Tester {
37faa4b864SLei De Bin 	static {
38faa4b864SLei De Bin 		Testspace.getFile("temp").mkdirs();
39faa4b864SLei De Bin 		// TODO move these shortcut into a file
40faa4b864SLei De Bin 		setCustomizedShortcut("copy", "ctrl", "c");
41faa4b864SLei De Bin 		setCustomizedShortcut("select_all", "ctrl", "a");
42faa4b864SLei De Bin 		setCustomizedShortcut("paste", "ctrl", "v");
43faa4b864SLei De Bin 		setCustomizedShortcut("cut", "ctrl", "x");
44faa4b864SLei De Bin 		setCustomizedShortcut("startcenter", "ctrl", "n");
45faa4b864SLei De Bin 		setCustomizedShortcut("find", "ctrl", "f");
46faa4b864SLei De Bin 		setCustomizedShortcut("undo", "ctrl", "z");
47faa4b864SLei De Bin 		setCustomizedShortcut("redo", "ctrl", "y");
48faa4b864SLei De Bin 		if (SystemUtil.isMac()) {
49faa4b864SLei De Bin 			setCustomizedShortcut("copy", "command", "c");
50faa4b864SLei De Bin 			setCustomizedShortcut("select_all", "command", "a");
51faa4b864SLei De Bin 			setCustomizedShortcut("paste", "command", "v");
52faa4b864SLei De Bin 			setCustomizedShortcut("cut", "command", "x");
53faa4b864SLei De Bin 			setCustomizedShortcut("startcenter", "command", "n");
54faa4b864SLei De Bin 			setCustomizedShortcut("find", "command", "f");
55faa4b864SLei De Bin 			setCustomizedShortcut("undo", "command", "z");
56faa4b864SLei De Bin 			setCustomizedShortcut("redo", "command", "shift", "z");
57faa4b864SLei De Bin 		} else if (SystemUtil.isLinux()) {
5822a14f28SLiu Zhe 
5922a14f28SLiu Zhe 		}
60faa4b864SLei De Bin 	}
6122a14f28SLiu Zhe 
62*b4d2d410SLiu Zhe 	public static void newTextDocument() {
63*b4d2d410SLiu Zhe 		app.dispatch("private:factory/swriter");
64*b4d2d410SLiu Zhe 		writer.waitForExistence(10, 2);
65*b4d2d410SLiu Zhe 		sleep(1);
66*b4d2d410SLiu Zhe 	}
67*b4d2d410SLiu Zhe 
68*b4d2d410SLiu Zhe 	public static void newSpreadsheet() {
69*b4d2d410SLiu Zhe 		app.dispatch("private:factory/scalc");
70*b4d2d410SLiu Zhe 		calc.waitForExistence(10, 2);
71*b4d2d410SLiu Zhe 		sleep(1);
72*b4d2d410SLiu Zhe 	}
73*b4d2d410SLiu Zhe 
74*b4d2d410SLiu Zhe 	public static void newPresentation() {
75*b4d2d410SLiu Zhe 		app.dispatch("private:factory/simpress");
76*b4d2d410SLiu Zhe 		impress.waitForExistence(10, 2);
77*b4d2d410SLiu Zhe 		sleep(1);
78*b4d2d410SLiu Zhe 	}
79*b4d2d410SLiu Zhe 
80*b4d2d410SLiu Zhe 	public static void newDrawing() {
81*b4d2d410SLiu Zhe 		app.dispatch("private:factory/sdraw");
82*b4d2d410SLiu Zhe 		draw.waitForExistence(10, 2);
83*b4d2d410SLiu Zhe 		sleep(1);
84*b4d2d410SLiu Zhe 	}
85*b4d2d410SLiu Zhe 
86*b4d2d410SLiu Zhe 	public static void newFormula() {
87*b4d2d410SLiu Zhe 		app.dispatch("private:factory/smath");
88*b4d2d410SLiu Zhe 		mathEditWindow.waitForExistence(10, 2);
89*b4d2d410SLiu Zhe 		sleep(1);
90*b4d2d410SLiu Zhe 	}
91*b4d2d410SLiu Zhe 
92*b4d2d410SLiu Zhe 	public static void open(String path) {
93*b4d2d410SLiu Zhe 		app.dispatch(".uno:Open");
94*b4d2d410SLiu Zhe 		submitOpenDlg(getPath(path));
95*b4d2d410SLiu Zhe 	}
96*b4d2d410SLiu Zhe 
97*b4d2d410SLiu Zhe 	public static void saveAs(String path) {
98*b4d2d410SLiu Zhe 		app.dispatch(".uno:SaveAs");
99*b4d2d410SLiu Zhe 		submitSaveDlg(getPath(path));
100*b4d2d410SLiu Zhe 		if (alienFormatDlg.exists(3))
101*b4d2d410SLiu Zhe 			alienFormatDlg.ok();
102*b4d2d410SLiu Zhe 		app.waitSlot(5 * 60); // 10 minutes
103*b4d2d410SLiu Zhe 	}
104*b4d2d410SLiu Zhe 
105*b4d2d410SLiu Zhe 	public static void close() {
106*b4d2d410SLiu Zhe 		app.dispatch(".uno:CloseDoc");
107*b4d2d410SLiu Zhe 	}
108*b4d2d410SLiu Zhe 
109*b4d2d410SLiu Zhe 	public static void discard() {
110*b4d2d410SLiu Zhe 		app.dispatch(".uno:CloseDoc");
111*b4d2d410SLiu Zhe 		if (activeMsgBox.exists(2))
112*b4d2d410SLiu Zhe 			activeMsgBox.no();
113*b4d2d410SLiu Zhe 	}
114*b4d2d410SLiu Zhe 
115b6113d73SLiu Zhe 	public static void typeKeys(String keys) {
116b6113d73SLiu Zhe 		Tester.typeKeys(keys);
117b6113d73SLiu Zhe 	}
1187dd2b5bbSLiu Zhe 
119faa4b864SLei De Bin 	public static void openStartcenter() {
120faa4b864SLei De Bin 		if (startcenter.exists())
121faa4b864SLei De Bin 			return;
12222a14f28SLiu Zhe 
123faa4b864SLei De Bin 		if (SystemUtil.isMac()) {
124140164b7SLiu Zhe 			SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
125faa4b864SLei De Bin 			typeKeys("<command n>");
126faa4b864SLei De Bin 		}
12722a14f28SLiu Zhe 
128faa4b864SLei De Bin 	}
129faa4b864SLei De Bin 
130*b4d2d410SLiu Zhe 	public static String copyAll() {
131*b4d2d410SLiu Zhe 		app.setClipboard(".d.i.r.t.y.");
132*b4d2d410SLiu Zhe 		try {
133*b4d2d410SLiu Zhe 			app.dispatch(".uno:SelectAll");
134*b4d2d410SLiu Zhe 		} catch (Exception e) {
135*b4d2d410SLiu Zhe 			app.dispatch(".uno:Select");
136*b4d2d410SLiu Zhe 		}
137*b4d2d410SLiu Zhe 		app.dispatch(".uno:Copy");
138*b4d2d410SLiu Zhe 		return app.getClipboard();
139*b4d2d410SLiu Zhe 	}
140*b4d2d410SLiu Zhe 
141faa4b864SLei De Bin 	public static void submitOpenDlg(String path) {
142*b4d2d410SLiu Zhe 		filePickerPath.setText(path);
143*b4d2d410SLiu Zhe 		filePickerOpen.click();
144faa4b864SLei De Bin 		sleep(1);
145faa4b864SLei De Bin 	}
14695269d92SLiu Zhe 
147faa4b864SLei De Bin 	public static void submitSaveDlg(String path) {
148*b4d2d410SLiu Zhe 		fileSavePath.setText(path);
14922a14f28SLiu Zhe 
150faa4b864SLei De Bin 		String extName = FileUtil.getFileExtName(path).toLowerCase();
15122a14f28SLiu Zhe 
152*b4d2d410SLiu Zhe 		String[] filters = fileSaveFileType.getItemsText();
153faa4b864SLei De Bin 		int i = 0;
154faa4b864SLei De Bin 		for (; i < filters.length; i++) {
155faa4b864SLei De Bin 			String f = filters[i];
156faa4b864SLei De Bin 			int dotIndex = f.lastIndexOf(".");
157faa4b864SLei De Bin 			if (dotIndex == -1)
158faa4b864SLei De Bin 				continue;
159faa4b864SLei De Bin 			if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
160faa4b864SLei De Bin 				break;
161faa4b864SLei De Bin 		}
162faa4b864SLei De Bin 		if (i == filters.length)
163faa4b864SLei De Bin 			throw new RuntimeException("Can't find the supported doc format!");
16422a14f28SLiu Zhe 
165*b4d2d410SLiu Zhe 		fileSaveFileType.select(i);
166*b4d2d410SLiu Zhe 		fileSaveSave.click();
167faa4b864SLei De Bin 		sleep(1);
168faa4b864SLei De Bin 	}
16922a14f28SLiu Zhe 
170faa4b864SLei De Bin 	public static void submitSaveDlg(String path, String ext) {
171*b4d2d410SLiu Zhe 		fileSavePath.setText(path);
172faa4b864SLei De Bin 		if (ext != null) {
173faa4b864SLei De Bin 			// change filter
174*b4d2d410SLiu Zhe 			String[] filters = fileSaveFileType.getItemsText();
175faa4b864SLei De Bin 			int i = 0;
176faa4b864SLei De Bin 			for (; i < filters.length; i++) {
177faa4b864SLei De Bin 				String f = filters[i];
178faa4b864SLei De Bin 				int dotIndex = f.lastIndexOf(".");
179faa4b864SLei De Bin 				if (dotIndex == -1)
180faa4b864SLei De Bin 					continue;
181faa4b864SLei De Bin 				if (ext.equals(f.substring(dotIndex + 1, f.length() - 1)))
182faa4b864SLei De Bin 					break;
183faa4b864SLei De Bin 			}
184faa4b864SLei De Bin 			if (i == filters.length)
185faa4b864SLei De Bin 				throw new RuntimeException("Can't find the supported doc format!");
186faa4b864SLei De Bin 		}
187*b4d2d410SLiu Zhe 		fileSaveFileType.click();
188faa4b864SLei De Bin 		sleep(1);
189faa4b864SLei De Bin 	}
19022a14f28SLiu Zhe 
191faa4b864SLei De Bin 	public static void handleBlocker(final VclWindow... windows) {
192faa4b864SLei De Bin 		new Condition() {
193faa4b864SLei De Bin 			@Override
194faa4b864SLei De Bin 			public boolean value() {
195*b4d2d410SLiu Zhe 				if (activeMsgBox.exists()) {
196faa4b864SLei De Bin 					try {
197*b4d2d410SLiu Zhe 						activeMsgBox.ok();
198faa4b864SLei De Bin 					} catch (Exception e) {
199faa4b864SLei De Bin 						try {
200*b4d2d410SLiu Zhe 							activeMsgBox.yes();
201faa4b864SLei De Bin 						} catch (Exception e1) {
202faa4b864SLei De Bin 						}
203faa4b864SLei De Bin 					}
204faa4b864SLei De Bin 				}
20522a14f28SLiu Zhe 
206faa4b864SLei De Bin 				boolean shown = false;
20722a14f28SLiu Zhe 
20822a14f28SLiu Zhe 				for (VclWindow w : windows) {
209faa4b864SLei De Bin 					if (w.exists()) {
210faa4b864SLei De Bin 						shown = true;
211faa4b864SLei De Bin 						break;
212faa4b864SLei De Bin 					}
213faa4b864SLei De Bin 				}
21422a14f28SLiu Zhe 
215faa4b864SLei De Bin 				if (!shown)
216faa4b864SLei De Bin 					return false;
21722a14f28SLiu Zhe 
218*b4d2d410SLiu Zhe 				if (activeMsgBox.exists(2)) {
219faa4b864SLei De Bin 					try {
220*b4d2d410SLiu Zhe 						activeMsgBox.ok();
221faa4b864SLei De Bin 					} catch (Exception e) {
222faa4b864SLei De Bin 						try {
223*b4d2d410SLiu Zhe 							activeMsgBox.yes();
224faa4b864SLei De Bin 						} catch (Exception e1) {
225faa4b864SLei De Bin 						}
226faa4b864SLei De Bin 					}
227faa4b864SLei De Bin 				}
22822a14f28SLiu Zhe 
229faa4b864SLei De Bin 				return true;
230faa4b864SLei De Bin 			}
23122a14f28SLiu Zhe 
23222a14f28SLiu Zhe 		}.waitForTrue("Time out wait window to be active.", 120, 2);
233faa4b864SLei De Bin 	}
234faa4b864SLei De Bin }
235