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 saveAndReopen(String path){
108 		saveAs(path);
109 		close();
110 		open(path);
111 	}
112 
113 	public static void discard() {
114 		app.dispatch(".uno:CloseDoc");
115 		if (activeMsgBox.exists(2))
116 			activeMsgBox.no();
117 	}
118 
119 	public static void typeKeys(String keys) {
120 		Tester.typeKeys(keys);
121 	}
122 
123 	public static void openStartcenter() {
124 		if (startcenter.exists())
125 			return;
126 
127 		if (SystemUtil.isMac()) {
128 			SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
129 			typeKeys("<command n>");
130 		}
131 
132 	}
133 
134 	public static String copyAll() {
135 		app.setClipboard(".d.i.r.t.y.");
136 		try {
137 			app.dispatch(".uno:SelectAll");
138 		} catch (Exception e) {
139 			app.dispatch(".uno:Select");
140 		}
141 		app.dispatch(".uno:Copy");
142 		return app.getClipboard();
143 	}
144 
145 	public static void submitOpenDlg(String path) {
146 		filePickerPath.setText(path);
147 		filePickerOpen.click();
148 	}
149 
150 	public static void submitSaveDlg(String path) {
151 		fileSavePath.setText(path);
152 		String extName = FileUtil.getFileExtName(path).toLowerCase();
153 		String[] filters = fileSaveFileType.getItemsText();
154 		int i = 0;
155 		for (; i < filters.length; i++) {
156 			String f = filters[i];
157 			int dotIndex = f.lastIndexOf(".");
158 			if (dotIndex == -1)
159 				continue;
160 			if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
161 				break;
162 		}
163 		if (i == filters.length)
164 			throw new RuntimeException("Can't find the supported doc format!");
165 
166 		fileSaveFileType.select(i);
167 		fileSaveSave.click();
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 	}
189 
190 	public static void handleBlocker(final VclWindow... windows) {
191 		new Condition() {
192 			@Override
193 			public boolean value() {
194 				if (activeMsgBox.exists()) {
195 					try {
196 						activeMsgBox.ok();
197 					} catch (Exception e) {
198 						try {
199 							activeMsgBox.yes();
200 						} catch (Exception e1) {
201 						}
202 					}
203 				}
204 
205 				boolean shown = false;
206 
207 				for (VclWindow w : windows) {
208 					if (w.exists()) {
209 						shown = true;
210 						break;
211 					}
212 				}
213 
214 				if (!shown)
215 					return false;
216 
217 				if (activeMsgBox.exists(2)) {
218 					try {
219 						activeMsgBox.ok();
220 					} catch (Exception e) {
221 						try {
222 							activeMsgBox.yes();
223 						} catch (Exception e1) {
224 						}
225 					}
226 				}
227 
228 				return true;
229 			}
230 
231 		}.waitForTrue("Time out wait window to be active.", 120, 2);
232 	}
233 
234 	public static void insertPicture(String path) {
235 		app.dispatch(".uno:InsertGraphic");
236 		submitOpenDlg(getPath(path));
237 	}
238 }
239