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 
23 
24 package testlib;
25 import static testlib.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 AppUtil 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 typeKeys(String keys) {
61 		Tester.typeKeys(keys);
62 	}
63 
64 	public static void openStartcenter() {
65 		if (startcenter.exists())
66 			return;
67 
68 		if (SystemUtil.isMac()) {
69 			SystemUtil.execScript("osascript -e 'tell app \"OpenOffice.org\" to activate'");
70 			typeKeys("<command n>");
71 		}
72 
73 	}
74 
75 
76 	public static void submitOpenDlg(String path) {
77 		FilePicker_Path.setText(path);
78 		FilePicker_Open.click();
79 		sleep(1);
80 	}
81 
82 	/**
83 	 * Fetch a test file to test space
84 	 * @param path
85 	 * @return a absolute path on the local
86 	 */
87 	public static String testFile(String path) {
88 		return Testspace.prepareData(path);
89 	}
90 
91 	public static String fullPath(String relativePath) {
92 		return Testspace.getPath(relativePath);
93 	}
94 
95 	public static void submitSaveDlg(String path) {
96 		FileSave_Path.setText(path);
97 
98 		String extName = FileUtil.getFileExtName(path).toLowerCase();
99 
100 		String[] filters = FileSave_FileType.getItemsText();
101 		int i = 0;
102 		for (; i < filters.length; i++) {
103 			String f = filters[i];
104 			int dotIndex = f.lastIndexOf(".");
105 			if (dotIndex == -1)
106 				continue;
107 			if (extName.equals(f.substring(dotIndex + 1, f.length() - 1)))
108 				break;
109 		}
110 		if (i == filters.length)
111 			throw new RuntimeException("Can't find the supported doc format!");
112 
113 		FileSave_FileType.select(i);
114 		FileSave_Save.click();
115 		sleep(1);
116 	}
117 
118 	public static void submitSaveDlg(String path, String ext) {
119 		FileSave_Path.setText(path);
120 		if (ext != null) {
121 			// change filter
122 			String[] filters = FileSave_FileType.getItemsText();
123 			int i = 0;
124 			for (; i < filters.length; i++) {
125 				String f = filters[i];
126 				int dotIndex = f.lastIndexOf(".");
127 				if (dotIndex == -1)
128 					continue;
129 				if (ext.equals(f.substring(dotIndex + 1, f.length() - 1)))
130 					break;
131 			}
132 			if (i == filters.length)
133 				throw new RuntimeException("Can't find the supported doc format!");
134 		}
135 		FileSave_FileType.click();
136 		sleep(1);
137 	}
138 
139 	public static void handleBlocker(final VclWindow... windows) {
140 		new Condition() {
141 			@Override
142 			public boolean value() {
143 				if (ActiveMsgBox.exists()) {
144 					try {
145 						ActiveMsgBox.ok();
146 					} catch (Exception e) {
147 						try {
148 							ActiveMsgBox.yes();
149 						} catch (Exception e1) {
150 						}
151 					}
152 				}
153 
154 				boolean shown = false;
155 
156 				for(VclWindow w : windows) {
157 					if (w.exists()) {
158 						shown = true;
159 						break;
160 					}
161 				}
162 
163 				if (!shown)
164 					return false;
165 
166 				if (ActiveMsgBox.exists(2)) {
167 					try {
168 						ActiveMsgBox.ok();
169 					} catch (Exception e) {
170 						try {
171 							ActiveMsgBox.yes();
172 						} catch (Exception e1) {
173 						}
174 					}
175 				}
176 
177 				return true;
178 			}
179 
180 		}.waitForTrue("Time out wait window to be active.",  120, 2);
181 	}
182 }
183