Line 0
Link Here
|
|
|
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 |
package testcase.gui; |
24 |
|
25 |
import static org.junit.Assert.assertTrue; |
26 |
import static org.openoffice.test.common.Testspace.*; |
27 |
import org.openoffice.test.common.Condition; |
28 |
import static testlib.gui.AppUtil.*; |
29 |
import static testlib.gui.UIMap.*; |
30 |
|
31 |
import java.io.File; |
32 |
import java.io.BufferedInputStream; |
33 |
import java.io.BufferedOutputStream; |
34 |
import java.io.FileOutputStream; |
35 |
import java.io.FileReader; |
36 |
import java.io.BufferedReader; |
37 |
import java.net.URL; |
38 |
import java.util.ArrayList; |
39 |
import java.util.Collection; |
40 |
|
41 |
import junit.framework.Assert; |
42 |
|
43 |
import org.junit.After; |
44 |
import org.junit.Before; |
45 |
import org.junit.Rule; |
46 |
import org.junit.Test; |
47 |
import org.junit.runner.RunWith; |
48 |
import org.junit.runners.Parameterized; |
49 |
import org.junit.runners.Parameterized.Parameters; |
50 |
import org.openoffice.test.common.FileUtil; |
51 |
import org.openoffice.test.vcl.widgets.VclWindow; |
52 |
import org.openoffice.test.common.Logger; |
53 |
|
54 |
/** |
55 |
* read sample file names(absolute path in a ftp server) from a .suite file. |
56 |
* download sample file locally, then save it as specific formats, close and reopen the new saved file if AOO support. |
57 |
* only support the following processes: |
58 |
* originalFile-->(saveasFormat11, saveasFormat12) |
59 |
* originalFile-->(saveasFormat11)-->(saveasFormat21,saveasFormat22), in this process, saveasFormat11 can't be "pdf" |
60 |
*/ |
61 |
@RunWith(Parameterized.class) |
62 |
public class TestSampleNew { |
63 |
|
64 |
public static String suiteDir = ""; |
65 |
public static String[][] params = {}; |
66 |
|
67 |
|
68 |
@Parameters |
69 |
public static Collection<Object[]> data() throws Exception{ |
70 |
ArrayList<Object[]> list = new ArrayList<Object[]>(); |
71 |
String suitePath = ""; |
72 |
FileReader fileReader = null; |
73 |
BufferedReader reader = null; |
74 |
File suites = new File(suiteDir); |
75 |
for(File file: suites.listFiles()){ |
76 |
if(FileUtil.getFileExtName(file.getName()).toLowerCase().equals("suite")){ |
77 |
suitePath = file.getAbsolutePath(); |
78 |
break; |
79 |
} |
80 |
} |
81 |
|
82 |
try{ |
83 |
fileReader = new FileReader(suitePath); |
84 |
reader = new BufferedReader(fileReader); |
85 |
String line = null; |
86 |
String lineLowerCase = null; |
87 |
while((line = reader.readLine()) != null){ |
88 |
lineLowerCase = line.toLowerCase(); |
89 |
for (String[] param : params) { |
90 |
String filter = param[0]; |
91 |
if (filter != null && lineLowerCase.matches(filter)) { |
92 |
Object[] data = {line, param[1], param[2], param[3]}; |
93 |
list.add(data); |
94 |
//System.out.println(line + " " + param[1] + " " + param[2] + " " + param[3]); |
95 |
break; |
96 |
} |
97 |
} |
98 |
} |
99 |
}catch(Exception e){ |
100 |
throw new Exception("throw exception when read suite file. " + e.getMessage()); |
101 |
}finally{ |
102 |
try{ |
103 |
if(reader != null){ |
104 |
reader.close(); |
105 |
reader = null; |
106 |
} |
107 |
if(fileReader != null){ |
108 |
fileReader.close(); |
109 |
fileReader = null; |
110 |
} |
111 |
}catch(Exception io){ |
112 |
//ignore; |
113 |
} |
114 |
} |
115 |
|
116 |
return list; |
117 |
} |
118 |
|
119 |
|
120 |
@Rule |
121 |
public Logger LOG = Logger.getLogger(this); |
122 |
private String originalFilePath = null; //sample file path in server |
123 |
private String localFilePath = null; //local sample file path |
124 |
private String saveasFormat11 = null; |
125 |
private String saveasFormat12 = null; |
126 |
private String saveasFormat21 = null; |
127 |
private String saveasFormat22 = null; |
128 |
private String saveToPath11 = null; |
129 |
private String saveToPath12 = null; |
130 |
private String saveToPath21 = null; |
131 |
private String saveToPath22 = null; |
132 |
|
133 |
private String saveas = null; |
134 |
private String saveas2= null; |
135 |
private String editor = null; |
136 |
private boolean passed = false; |
137 |
|
138 |
public TestSampleNew(String filePath, String saveas, String saveas2, String editor) { |
139 |
this.originalFilePath = filePath; |
140 |
this.saveas = saveas; |
141 |
this.saveas2 = saveas2; |
142 |
this.editor = editor; |
143 |
} |
144 |
|
145 |
|
146 |
|
147 |
/** |
148 |
* @throws java.lang.Exception |
149 |
*/ |
150 |
@Before |
151 |
public void setUp() throws Exception { |
152 |
|
153 |
this.downloadFile(); |
154 |
this.parseParameter(); |
155 |
|
156 |
app.start(); |
157 |
} |
158 |
|
159 |
@After |
160 |
public void tearDown() { |
161 |
if (!passed) { |
162 |
// Collect the failed sample files. |
163 |
String failedDir = getPath("output/TestSample.Failed"); |
164 |
String failedFile = new File(failedDir, new File(this.localFilePath).getName()).getAbsolutePath(); |
165 |
FileUtil.copyFile(this.localFilePath, failedFile); |
166 |
} |
167 |
app.close(); |
168 |
} |
169 |
|
170 |
@Test |
171 |
public void test() { |
172 |
VclWindow window = null; |
173 |
if (editor == null) { |
174 |
Assert.assertTrue("no editor name", false); |
175 |
} else { |
176 |
if (editor.equals("writer")) |
177 |
window = window("SW_HID_EDIT_WIN"); |
178 |
if (editor.equals("calc")) |
179 |
window = window("SC_HID_SC_WIN_GRIDWIN"); |
180 |
if (editor.equals("impress")) |
181 |
window = window("SD_HID_SDDRAWVIEWSHELL"); |
182 |
if (editor.equals("draw")) |
183 |
Assert.assertTrue("not supported editor: " + editor, false); |
184 |
if (editor.equals("database")) |
185 |
Assert.assertTrue("not supported editor: " + editor, false); |
186 |
|
187 |
testEditor(window); |
188 |
} |
189 |
} |
190 |
|
191 |
public void testEditor(VclWindow window) { |
192 |
app.dispatch(".uno:Open"); |
193 |
submitOpenDlg(this.localFilePath); |
194 |
window.waitForExistence(10, 2); |
195 |
|
196 |
|
197 |
//saveasFormat11 |
198 |
this.handleFile(saveasFormat11, saveToPath11, window); |
199 |
|
200 |
//saveasFormat12 |
201 |
if(null != saveasFormat12){ |
202 |
app.dispatch(".uno:CloseDoc"); |
203 |
|
204 |
app.dispatch(".uno:Open"); |
205 |
submitOpenDlg(this.localFilePath); |
206 |
window.waitForExistence(10, 2); |
207 |
|
208 |
this.handleFile(saveasFormat12, saveToPath12, window); |
209 |
}else{ |
210 |
//saveasFormat21 |
211 |
if(null != saveasFormat21){ |
212 |
this.handleFile(saveasFormat21, saveToPath21, window); |
213 |
} |
214 |
|
215 |
if(null != saveasFormat22){ |
216 |
app.dispatch(".uno:CloseDoc"); |
217 |
|
218 |
app.dispatch(".uno:Open"); |
219 |
submitOpenDlg(saveToPath11); |
220 |
window.waitForExistence(10, 2); |
221 |
|
222 |
this.handleFile(saveasFormat22, saveToPath22, window); |
223 |
} |
224 |
|
225 |
} |
226 |
|
227 |
// Close it by clicking main menu |
228 |
app.dispatch(".uno:CloseDoc"); |
229 |
} |
230 |
|
231 |
private void handleFile(String saveasFormat, String saveToPath, VclWindow window) |
232 |
{ |
233 |
if(saveasFormat.equalsIgnoreCase("pdf")){ |
234 |
app.dispatch(".uno:ExportToPDF"); |
235 |
PDFGeneralPage.ok(); ; |
236 |
submitSaveDlg(saveToPath); |
237 |
//in 120 seconds, test pdf was exported |
238 |
passed = this.waitForFileExist(saveToPath, 120, 2); |
239 |
assertTrue("PDF is exported.", passed); |
240 |
}else{ |
241 |
app.dispatch(".uno:SaveAs"); |
242 |
submitSaveDlg(saveToPath); |
243 |
if (AlienFormatDlg.exists(3)) |
244 |
AlienFormatDlg.ok(); |
245 |
|
246 |
if(window.getId().equals("SC_HID_SC_WIN_GRIDWIN")){ |
247 |
new Condition() { |
248 |
@Override |
249 |
public boolean value() { |
250 |
if (MsgBox_AdditionalRowsNotSaved.exists()) { |
251 |
MsgBox_AdditionalRowsNotSaved.ok(); |
252 |
} |
253 |
return calc.isEnabled(); |
254 |
} |
255 |
|
256 |
}.waitForTrue("Time out to wait the control to be enabled!", 120, 2); |
257 |
}else{ |
258 |
window.waitForEnabled(120, 2); |
259 |
} |
260 |
// Close it by clicking main menu |
261 |
app.dispatch(".uno:CloseDoc"); |
262 |
|
263 |
openStartcenter(); |
264 |
// Reopen the saved file |
265 |
app.dispatch(".uno:Open"); |
266 |
submitOpenDlg(saveToPath); |
267 |
window.waitForExistence(10, 2); |
268 |
passed = true; |
269 |
Assert.assertTrue("save as to: " + saveToPath, passed); |
270 |
} |
271 |
} |
272 |
|
273 |
private boolean waitForFileExist(String filePath, int iTimeoutSecond, int interval) |
274 |
{ |
275 |
long startTime = System.currentTimeMillis(); |
276 |
while (System.currentTimeMillis() - startTime < iTimeoutSecond * 1000) { |
277 |
if (FileUtil.fileExists(filePath)) |
278 |
return true; |
279 |
try { |
280 |
Thread.sleep((long) (interval * 1000)); |
281 |
} catch (InterruptedException e) { |
282 |
} |
283 |
} |
284 |
|
285 |
return FileUtil.fileExists(filePath); |
286 |
} |
287 |
|
288 |
private void downloadFile() throws Exception |
289 |
{ |
290 |
FileUtil.deleteFile(getPath("temp")); |
291 |
File temp = new File(getPath("temp")); |
292 |
|
293 |
//LOG.info("Download sample file from \"" + originalFilePath + "\""); |
294 |
File localFileTemp = new File(temp + "/origin", new File(originalFilePath).getName()); |
295 |
File localFile = new File(temp + "/origin", new File(originalFilePath.replaceAll("%20", " ")).getName()); |
296 |
localFileTemp.renameTo(localFile); |
297 |
localFile.getParentFile().mkdirs(); |
298 |
//FileUtil.copyFile(originalFilePath, file.getAbsolutePath()); |
299 |
|
300 |
this.localFilePath = localFile.getAbsolutePath(); |
301 |
|
302 |
File file = new File(originalFilePath); |
303 |
if (!file.exists() || !file.isFile()) { |
304 |
BufferedInputStream bin = null; |
305 |
BufferedOutputStream bout = null; |
306 |
try { |
307 |
URL url = new URL(originalFilePath); |
308 |
LOG.info("Download " + url + " to " + localFile.getAbsolutePath()); |
309 |
byte[] buffer = new byte[10240]; |
310 |
int read = 0; |
311 |
bin = new BufferedInputStream(url.openStream()); |
312 |
bout = new BufferedOutputStream(new FileOutputStream(localFile)); |
313 |
while ((read = bin.read(buffer)) > -1) { |
314 |
bout.write(buffer, 0, read); |
315 |
} |
316 |
}finally { |
317 |
if (bin != null) |
318 |
try { |
319 |
bin.close(); |
320 |
} catch (Exception e) { |
321 |
} |
322 |
if (bout !=null) |
323 |
try { |
324 |
bout.close(); |
325 |
} catch (Exception e) { |
326 |
} |
327 |
} |
328 |
} |
329 |
|
330 |
} |
331 |
|
332 |
|
333 |
/** |
334 |
* only support the following processes: |
335 |
* originalFile-->(saveasFormat11, saveasFormat12) |
336 |
* originalFile-->(saveasFormat11)-->(saveasFormat21,saveasFormat22), in this process, saveasFormat11 can't be pdf |
337 |
*/ |
338 |
private void parseParameter() |
339 |
{ |
340 |
saveasFormat11 = null; |
341 |
saveasFormat12 = null; |
342 |
saveasFormat21 = null; |
343 |
saveasFormat22 = null; |
344 |
saveToPath11 = null; |
345 |
saveToPath12 = null; |
346 |
saveToPath21 = null; |
347 |
saveToPath22 = null; |
348 |
//parse |
349 |
int index = this.saveas.indexOf(","); |
350 |
if(index > 0){ |
351 |
saveasFormat11 = this.saveas.substring(0, index); |
352 |
saveasFormat12 = this.saveas.substring(index + 1); |
353 |
|
354 |
saveToPath11 = getPath("temp/" + new File(localFilePath).getName() + "." + saveasFormat11); |
355 |
saveToPath12 = getPath("temp/" + new File(localFilePath).getName() + "." + saveasFormat12); |
356 |
|
357 |
}else{ |
358 |
saveasFormat11 = this.saveas; |
359 |
saveToPath11 = getPath("temp/" + new File(localFilePath).getName() + "." + saveasFormat11); |
360 |
|
361 |
if(!saveasFormat11.equalsIgnoreCase("pdf")){ |
362 |
index = this.saveas2.indexOf(","); |
363 |
if(index > 0){ |
364 |
saveasFormat21 = this.saveas2.substring(0,index); |
365 |
saveasFormat22 = this.saveas2.substring(index + 1); |
366 |
|
367 |
saveToPath21 = getPath("temp/" + new File(localFilePath).getName() + "." + saveasFormat11 + "." + saveasFormat21); |
368 |
saveToPath22 = getPath("temp/" + new File(localFilePath).getName() + "." + saveasFormat11+ "." + saveasFormat22); |
369 |
}else{ |
370 |
saveasFormat21 = this.saveas2; |
371 |
saveToPath21 = getPath("temp/" + new File(localFilePath).getName() + "." + saveasFormat11 + "." + saveasFormat21); |
372 |
} |
373 |
} |
374 |
} |
375 |
} |
376 |
|
377 |
} |