xref: /AOO41X/test/testgui/source/testlib/gui/AppTool.java (revision b4d2d4107fcf77fcfe0de8a08cbffef88f3d2a6b)
1*b4d2d410SLiu Zhe /**************************************************************
2*b4d2d410SLiu Zhe  *
3*b4d2d410SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
4*b4d2d410SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
5*b4d2d410SLiu Zhe  * distributed with this work for additional information
6*b4d2d410SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
7*b4d2d410SLiu Zhe  * to you under the Apache License, Version 2.0 (the
8*b4d2d410SLiu Zhe  * "License"); you may not use this file except in compliance
9*b4d2d410SLiu Zhe  * with the License.  You may obtain a copy of the License at
10*b4d2d410SLiu Zhe  *
11*b4d2d410SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
12*b4d2d410SLiu Zhe  *
13*b4d2d410SLiu Zhe  * Unless required by applicable law or agreed to in writing,
14*b4d2d410SLiu Zhe  * software distributed under the License is distributed on an
15*b4d2d410SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b4d2d410SLiu Zhe  * KIND, either express or implied.  See the License for the
17*b4d2d410SLiu Zhe  * specific language governing permissions and limitations
18*b4d2d410SLiu Zhe  * under the License.
19*b4d2d410SLiu Zhe  *
20*b4d2d410SLiu Zhe  *************************************************************/
21*b4d2d410SLiu Zhe 
22*b4d2d410SLiu Zhe package testlib.gui;
23*b4d2d410SLiu 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.*;
27*b4d2d410SLiu Zhe import static testlib.gui.UIMap.*;
28*b4d2d410SLiu Zhe 
29*b4d2d410SLiu Zhe import org.openoffice.test.common.Condition;
30*b4d2d410SLiu Zhe import org.openoffice.test.common.FileUtil;
31*b4d2d410SLiu Zhe import org.openoffice.test.common.SystemUtil;
32*b4d2d410SLiu Zhe import org.openoffice.test.common.Testspace;
33*b4d2d410SLiu Zhe import org.openoffice.test.vcl.Tester;
34*b4d2d410SLiu Zhe import org.openoffice.test.vcl.widgets.VclWindow;
35*b4d2d410SLiu Zhe 
36*b4d2d410SLiu Zhe public class AppTool extends Tester {
37*b4d2d410SLiu Zhe 	static {
38*b4d2d410SLiu Zhe 		Testspace.getFile("temp").mkdirs();
39*b4d2d410SLiu Zhe 		// TODO move these shortcut into a file
40*b4d2d410SLiu Zhe 		setCustomizedShortcut("copy", "ctrl", "c");
41*b4d2d410SLiu Zhe 		setCustomizedShortcut("select_all", "ctrl", "a");
42*b4d2d410SLiu Zhe 		setCustomizedShortcut("paste", "ctrl", "v");
43*b4d2d410SLiu Zhe 		setCustomizedShortcut("cut", "ctrl", "x");
44*b4d2d410SLiu Zhe 		setCustomizedShortcut("startcenter", "ctrl", "n");
45*b4d2d410SLiu Zhe 		setCustomizedShortcut("find", "ctrl", "f");
46*b4d2d410SLiu Zhe 		setCustomizedShortcut("undo", "ctrl", "z");
47*b4d2d410SLiu Zhe 		setCustomizedShortcut("redo", "ctrl", "y");
48*b4d2d410SLiu Zhe 		if (SystemUtil.isMac()) {
49*b4d2d410SLiu Zhe 			setCustomizedShortcut("copy", "command", "c");
50*b4d2d410SLiu Zhe 			setCustomizedShortcut("select_all", "command", "a");
51*b4d2d410SLiu Zhe 			setCustomizedShortcut("paste", "command", "v");
52*b4d2d410SLiu Zhe 			setCustomizedShortcut("cut", "command", "x");
53*b4d2d410SLiu Zhe 			setCustomizedShortcut("startcenter", "command", "n");
54*b4d2d410SLiu Zhe 			setCustomizedShortcut("find", "command", "f");
55*b4d2d410SLiu Zhe 			setCustomizedShortcut("undo", "command", "z");
56*b4d2d410SLiu Zhe 			setCustomizedShortcut("redo", "command", "shift", "z");
57*b4d2d410SLiu Zhe 		} else if (SystemUtil.isLinux()) {
58*b4d2d410SLiu Zhe 
59*b4d2d410SLiu Zhe 		}
60*b4d2d410SLiu Zhe 	}
61*b4d2d410SLiu 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 
115*b4d2d410SLiu Zhe 	public static void typeKeys(String keys) {
116*b4d2d410SLiu Zhe 		Tester.typeKeys(keys);
117*b4d2d410SLiu Zhe 	}
118*b4d2d410SLiu Zhe 
119*b4d2d410SLiu Zhe 	public static void openStartcenter() {
120*b4d2d410SLiu Zhe 		if (startcenter.exists())
121*b4d2d410SLiu Zhe 			return;
122*b4d2d410SLiu Zhe 
123*b4d2d410SLiu Zhe 		if (SystemUtil.isMac()) {
124*b4d2d410SLiu Zhe 			SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
125*b4d2d410SLiu Zhe 			typeKeys("<command n>");
126*b4d2d410SLiu Zhe 		}
127*b4d2d410SLiu Zhe 
128*b4d2d410SLiu Zhe 	}
129*b4d2d410SLiu Zhe 
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 
141*b4d2d410SLiu Zhe 	public static void submitOpenDlg(String path) {
142*b4d2d410SLiu Zhe 		filePickerPath.setText(path);
143*b4d2d410SLiu Zhe 		filePickerOpen.click();
144*b4d2d410SLiu Zhe 		sleep(1);
145*b4d2d410SLiu Zhe 	}
146*b4d2d410SLiu Zhe 
147*b4d2d410SLiu Zhe 	public static void submitSaveDlg(String path) {
148*b4d2d410SLiu Zhe 		fileSavePath.setText(path);
149*b4d2d410SLiu Zhe 
150*b4d2d410SLiu Zhe 		String extName = FileUtil.getFileExtName(path).toLowerCase();
151*b4d2d410SLiu Zhe 
152*b4d2d410SLiu Zhe 		String[] filters = fileSaveFileType.getItemsText();
153*b4d2d410SLiu Zhe 		int i = 0;
154*b4d2d410SLiu Zhe 		for (; i < filters.length; i++) {
155*b4d2d410SLiu Zhe 			String f = filters[i];
156*b4d2d410SLiu Zhe 			int dotIndex = f.lastIndexOf(".");
157*b4d2d410SLiu Zhe 			if (dotIndex == -1)
158*b4d2d410SLiu Zhe 				continue;
159*b4d2d410SLiu Zhe 			if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
160*b4d2d410SLiu Zhe 				break;
161*b4d2d410SLiu Zhe 		}
162*b4d2d410SLiu Zhe 		if (i == filters.length)
163*b4d2d410SLiu Zhe 			throw new RuntimeException("Can't find the supported doc format!");
164*b4d2d410SLiu Zhe 
165*b4d2d410SLiu Zhe 		fileSaveFileType.select(i);
166*b4d2d410SLiu Zhe 		fileSaveSave.click();
167*b4d2d410SLiu Zhe 		sleep(1);
168*b4d2d410SLiu Zhe 	}
169*b4d2d410SLiu Zhe 
170*b4d2d410SLiu Zhe 	public static void submitSaveDlg(String path, String ext) {
171*b4d2d410SLiu Zhe 		fileSavePath.setText(path);
172*b4d2d410SLiu Zhe 		if (ext != null) {
173*b4d2d410SLiu Zhe 			// change filter
174*b4d2d410SLiu Zhe 			String[] filters = fileSaveFileType.getItemsText();
175*b4d2d410SLiu Zhe 			int i = 0;
176*b4d2d410SLiu Zhe 			for (; i < filters.length; i++) {
177*b4d2d410SLiu Zhe 				String f = filters[i];
178*b4d2d410SLiu Zhe 				int dotIndex = f.lastIndexOf(".");
179*b4d2d410SLiu Zhe 				if (dotIndex == -1)
180*b4d2d410SLiu Zhe 					continue;
181*b4d2d410SLiu Zhe 				if (ext.equals(f.substring(dotIndex + 1, f.length() - 1)))
182*b4d2d410SLiu Zhe 					break;
183*b4d2d410SLiu Zhe 			}
184*b4d2d410SLiu Zhe 			if (i == filters.length)
185*b4d2d410SLiu Zhe 				throw new RuntimeException("Can't find the supported doc format!");
186*b4d2d410SLiu Zhe 		}
187*b4d2d410SLiu Zhe 		fileSaveFileType.click();
188*b4d2d410SLiu Zhe 		sleep(1);
189*b4d2d410SLiu Zhe 	}
190*b4d2d410SLiu Zhe 
191*b4d2d410SLiu Zhe 	public static void handleBlocker(final VclWindow... windows) {
192*b4d2d410SLiu Zhe 		new Condition() {
193*b4d2d410SLiu Zhe 			@Override
194*b4d2d410SLiu Zhe 			public boolean value() {
195*b4d2d410SLiu Zhe 				if (activeMsgBox.exists()) {
196*b4d2d410SLiu Zhe 					try {
197*b4d2d410SLiu Zhe 						activeMsgBox.ok();
198*b4d2d410SLiu Zhe 					} catch (Exception e) {
199*b4d2d410SLiu Zhe 						try {
200*b4d2d410SLiu Zhe 							activeMsgBox.yes();
201*b4d2d410SLiu Zhe 						} catch (Exception e1) {
202*b4d2d410SLiu Zhe 						}
203*b4d2d410SLiu Zhe 					}
204*b4d2d410SLiu Zhe 				}
205*b4d2d410SLiu Zhe 
206*b4d2d410SLiu Zhe 				boolean shown = false;
207*b4d2d410SLiu Zhe 
208*b4d2d410SLiu Zhe 				for (VclWindow w : windows) {
209*b4d2d410SLiu Zhe 					if (w.exists()) {
210*b4d2d410SLiu Zhe 						shown = true;
211*b4d2d410SLiu Zhe 						break;
212*b4d2d410SLiu Zhe 					}
213*b4d2d410SLiu Zhe 				}
214*b4d2d410SLiu Zhe 
215*b4d2d410SLiu Zhe 				if (!shown)
216*b4d2d410SLiu Zhe 					return false;
217*b4d2d410SLiu Zhe 
218*b4d2d410SLiu Zhe 				if (activeMsgBox.exists(2)) {
219*b4d2d410SLiu Zhe 					try {
220*b4d2d410SLiu Zhe 						activeMsgBox.ok();
221*b4d2d410SLiu Zhe 					} catch (Exception e) {
222*b4d2d410SLiu Zhe 						try {
223*b4d2d410SLiu Zhe 							activeMsgBox.yes();
224*b4d2d410SLiu Zhe 						} catch (Exception e1) {
225*b4d2d410SLiu Zhe 						}
226*b4d2d410SLiu Zhe 					}
227*b4d2d410SLiu Zhe 				}
228*b4d2d410SLiu Zhe 
229*b4d2d410SLiu Zhe 				return true;
230*b4d2d410SLiu Zhe 			}
231*b4d2d410SLiu Zhe 
232*b4d2d410SLiu Zhe 		}.waitForTrue("Time out wait window to be active.", 120, 2);
233*b4d2d410SLiu Zhe 	}
234*b4d2d410SLiu Zhe }
235