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