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