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 {
mkdirs()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 
newTextDocument()60 	public static void newTextDocument() {
61 		app.dispatch("private:factory/swriter");
62 		writer.waitForExistence(10, 2);
63 		sleep(1);
64 	}
65 
newSpreadsheet()66 	public static void newSpreadsheet() {
67 		app.dispatch("private:factory/scalc");
68 		calc.waitForExistence(10, 2);
69 		sleep(1);
70 	}
71 
newPresentation()72 	public static void newPresentation() {
73 		app.dispatch("private:factory/simpress");
74 		impress.waitForExistence(10, 2);
75 		sleep(1);
76 	}
77 
newDrawing()78 	public static void newDrawing() {
79 		app.dispatch("private:factory/sdraw");
80 		draw.waitForExistence(10, 2);
81 		sleep(1);
82 	}
83 
newFormula()84 	public static void newFormula() {
85 		app.dispatch("private:factory/smath");
86 		mathEditWindow.waitForExistence(10, 2);
87 		sleep(1);
88 	}
89 
open(String path)90 	public static void open(String path) {
91 		app.dispatch(".uno:Open");
92 		submitOpenDlg(getPath(path));
93 	}
94 
saveAs(String path)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 		if( activeMsgBox.exists(1)) {
101 			String msg = activeMsgBox.getMessage();
102 			// #i123142# confirm overwriting of test files
103 			if( msg.indexOf( "overwrite changes") >= 0)
104 				activeMsgBox.yes();
105 		}
106 		app.waitSlot(5 * 60); // 5 minutes
107 	}
108 
close()109 	public static void close() {
110 		app.dispatch(".uno:CloseDoc");
111 	}
112 
saveAndReopen(String path)113 	public static void saveAndReopen(String path){
114 		saveAs(path);
115 		close();
116 		open(path);
117 	}
118 
discard()119 	public static void discard() {
120 		app.dispatch(".uno:CloseDoc");
121 		if (activeMsgBox.exists(2))
122 			activeMsgBox.no();
123 	}
124 
typeKeys(String keys)125 	public static void typeKeys(String keys) {
126 		Tester.typeKeys(keys);
127 	}
128 
openStartcenter()129 	public static void openStartcenter() {
130 		if (startcenter.exists())
131 			return;
132 
133 		if (SystemUtil.isMac()) {
134 			SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
135 			typeKeys("<command n>");
136 		}
137 
138 	}
139 
copyAll()140 	public static String copyAll() {
141 		app.setClipboard(".d.i.r.t.y.");
142 		try {
143 			app.dispatch(".uno:SelectAll");
144 		} catch (Exception e) {
145 			app.dispatch(".uno:Select");
146 		}
147 		app.dispatch(".uno:Copy");
148 		return app.getClipboard();
149 	}
150 
submitOpenDlg(String path)151 	public static void submitOpenDlg(String path) {
152 		filePickerPath.setText(path);
153 		filePickerOpen.click();
154 	}
155 
submitSaveDlg(String path)156 	public static void submitSaveDlg(String path) {
157 		fileSavePath.setText(path);
158 		String extName = FileUtil.getFileExtName(path).toLowerCase();
159 		String[] filters = fileSaveFileType.getItemsText();
160 		int i = 0;
161 		for (; i < filters.length; i++) {
162 			String f = filters[i];
163 			int dotIndex = f.lastIndexOf(".");
164 			if (dotIndex == -1)
165 				continue;
166 			if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
167 				break;
168 		}
169 		if (i == filters.length)
170 			throw new RuntimeException("Can't find the supported doc format!");
171 
172 		fileSaveFileType.select(i);
173 		fileSaveSave.click();
174 	}
175 
submitSaveDlg(String path, String ext)176 	public static void submitSaveDlg(String path, String ext) {
177 		fileSavePath.setText(path);
178 		if (ext != null) {
179 			// change filter
180 			String[] filters = fileSaveFileType.getItemsText();
181 			int i = 0;
182 			for (; i < filters.length; i++) {
183 				String f = filters[i];
184 				int dotIndex = f.lastIndexOf(".");
185 				if (dotIndex == -1)
186 					continue;
187 				if (ext.equals(f.substring(dotIndex + 1, f.length() - 1)))
188 					break;
189 			}
190 			if (i == filters.length)
191 				throw new RuntimeException("Can't find the supported doc format!");
192 		}
193 		fileSaveFileType.click();
194 	}
195 
handleBlocker(final VclWindow... windows)196 	public static void handleBlocker(final VclWindow... windows) {
197 		new Condition() {
198 			@Override
199 			public boolean value() {
200 				if (activeMsgBox.exists()) {
201 					try {
202 						activeMsgBox.ok();
203 					} catch (Exception e) {
204 						try {
205 							activeMsgBox.yes();
206 						} catch (Exception e1) {
207 						}
208 					}
209 				}
210 
211 				boolean shown = false;
212 
213 				for (VclWindow w : windows) {
214 					if (w.exists()) {
215 						shown = true;
216 						break;
217 					}
218 				}
219 
220 				if (!shown)
221 					return false;
222 
223 				if (activeMsgBox.exists(2)) {
224 					try {
225 						activeMsgBox.ok();
226 					} catch (Exception e) {
227 						try {
228 							activeMsgBox.yes();
229 						} catch (Exception e1) {
230 						}
231 					}
232 				}
233 
234 				return true;
235 			}
236 
237 		}.waitForTrue("Time out wait window to be active.", 120, 2);
238 	}
239 
insertPicture(String path)240 	public static void insertPicture(String path) {
241 		app.dispatch(".uno:InsertGraphic");
242 		submitOpenDlg(getPath(path));
243 	}
244 }
245