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  */
25 package svt.gui.sw;
26 
27 import static org.openoffice.test.common.Testspace.getFile;
28 import static org.openoffice.test.common.Testspace.getPath;
29 import static org.openoffice.test.common.Testspace.prepareData;
30 import static org.openoffice.test.vcl.Tester.*;
31 import static testlib.gui.AppTool.*;
32 import static testlib.gui.UIMap.*;
33 
34 import java.io.FileOutputStream;
35 import java.io.PrintStream;
36 import java.util.HashMap;
37 
38 import org.junit.After;
39 import org.junit.AfterClass;
40 import org.junit.Before;
41 import org.junit.BeforeClass;
42 import org.junit.Ignore;
43 import org.junit.Rule;
44 import org.junit.Test;
45 import org.junit.rules.TestName;
46 import org.openoffice.test.common.DataSheet;
47 import org.openoffice.test.common.FileUtil;
48 import org.openoffice.test.common.Logger;
49 import org.openoffice.test.common.SystemUtil;
50 import org.openoffice.test.common.Testspace;
51 
52 public class BasicFuncOnWriter {
53 	@Rule
54 	public Logger log = Logger.getLogger(this);
55 
56 	@Rule
57 	public TestName testname = new TestName();
58 
59 	private static DataSheet xmlResult;
60 
61 	private String pid = null;
62 
63 	private static int iterator = 100;
64 
65 	private int i = 0;
66 
67 	/**
68 	 * @throws java.lang.Exception
69 	 */
70 	@BeforeClass
71 	public static void beforeClass() throws Exception {
72 
73 		xmlResult = new DataSheet(getFile("output/svt_gui_sw.xml"));
74 		xmlResult.addRow("BasicFuncOnWriter","Method", "Iterator", "Consumed Time(MS)",
75 				"Memory(KB)", "CPU(%)");
76 	}
77 
78 	@AfterClass
79 	public static void afterClass() throws Exception {
80 		app.stop();
81 	}
82 
83 	@Before
84 	public void before()throws Exception{
85 		app.start(true);
86 	}
87 
88 	@Test
89 	public void saveNewSWWithPic() throws Exception {
90 		String pic = prepareData("image/blue_256x256.jpg");
91 		for (i = 1; i <= iterator; i++) {
92 			long start = System.currentTimeMillis();
93 			createNewSW();
94 			writer.menuItem("Insert->Picture->From File...").select();
95 			sleep(2);
96 			filePickerPath.setText(pic);
97 			sleep(1);
98 			filePickerOpen.click();
99 			sleep(5);
100 			typeKeys("<esc>");
101 			sleep(2);
102 			saveAndReopenNewSW(i);
103 			long end = System.currentTimeMillis();
104 			addRecord(i, start, end);
105 		}
106 	}
107 
108 
109 	@Test
110 	public void saveNewSWWithBullet() throws Exception {
111 		for (i = 1; i <= iterator; i++) {
112 			long start = System.currentTimeMillis();
113 			createNewSW();
114 			writer.typeKeys("The first");
115 			writer.typeKeys("<enter>");
116 			sleep(3);
117 			writer.typeKeys("The second");
118 			app.dispatch(".uno:SelectAll");
119 			writer.menuItem("Format->Bullets and Numbering...").select();
120 			sleep(3);
121 			bulletPage.ok();
122 			writer.typeKeys("<ctrl z");
123 			sleep(1);
124 			writer.typeKeys("<ctrl y>");
125 			sleep(1);
126 			writer.typeKeys("<ctrl end>");
127 			writer.typeKeys("<enter>");
128 			sleep(2);
129 
130 			writer.focus();
131 			saveAndReopenNewSW(i);
132 			long end = System.currentTimeMillis();
133 			addRecord(i, start, end);
134 		}
135 	}
136 
137 	@Test
138 	public void saveNewSWWithUnDoDelete()throws Exception{
139 		if(!sdDrawingToolbar.exists()){
140 			app.dispatch(".uno:AvailableToolbars?Toolbar:string=toolbar");
141 		}
142 		for (i = 1; i <= iterator; i++) {
143 			long start = System.currentTimeMillis();
144 			createNewSW();
145 			app.dispatch(".uno:Ellipse");
146 			writer.focus();
147 			writer.drag(200, 200, 300, 300);
148 			typeKeys("<Delete>");
149 			typeKeys("<ctrl z>");
150 			typeKeys("<ctrl y>");
151 
152 			saveAndReopenNewSW(i);
153 			long end = System.currentTimeMillis();
154 			addRecord(i, start, end);
155 		}
156 	}
157 
158 
159 	@Test
160 	public void saveNewSWWithSpellCheck() throws Exception {
161 		for (i = 1; i <= iterator; i++) {
162 			long start = System.currentTimeMillis();
163 			createNewSW();
164 			writer.typeKeys("goood");
165 			sleep(2);
166 			writer.menuItem("Tools->Spelling and Grammar...").select();
167 			sleep(2);
168 			spellcheckDlgSuggestionList.select(0);
169 			spellcheckDlgIgnoreOnce.click();
170 			activeMsgBox.ok();
171 			sleep(2);
172 			saveAndReopenNewSW(i);
173 			long end = System.currentTimeMillis();
174 			addRecord(i, start, end);
175 		}
176 	}
177 
178 	@Test
179 	public void saveNewSWWithHeaderFooter() throws Exception {
180 		for (i = 1; i <= iterator; i++) {
181 			long start = System.currentTimeMillis();
182 			createNewSW();
183 			writer.menuItem("Insert->Header->Default").select();
184 			sleep(2);
185 			writer.typeKeys("Header");
186 			sleep(2);
187 			writer.menuItem("Insert->Footer->Default").select();
188 			sleep(2);
189 			writer.typeKeys("Footer");
190 			sleep(2);
191 			writer.typeKeys("<ctrl end>");
192 			sleep(2);
193 			writer.typeKeys("<enter>");
194 			sleep(2);
195 			saveAndReopenNewSW(i);
196 			long end = System.currentTimeMillis();
197 			addRecord(i, start, end);
198 		}
199 	}
200 
201 	@Test
202 	public void saveNewSWWithFrameAnchor() throws Exception {
203 		for (i = 1; i <= iterator; i++) {
204 			long start = System.currentTimeMillis();
205 			createNewSW();
206 			writer.menuItem("Insert->Frame...").select();
207 			sleep(2);
208 			writerFrameDlg.ok();
209 			writer.menuItem("Format->Anchor->To Page").select();
210 			sleep(2);
211 			writer.menuItem("Format->Anchor->To Paragraph").select();
212 			sleep(2);
213 			writer.menuItem("Format->Anchor->To Character").select();
214 			sleep(2);
215 			writer.menuItem("Format->Anchor->As Character").select();
216 			sleep(2);
217 			writer.typeKeys("<esc>");
218 			sleep(2);
219 			writer.typeKeys("<enter>");
220 			sleep(2);
221 			saveAndReopenNewSW(i);
222 			long end = System.currentTimeMillis();
223 			addRecord(i, start, end);
224 		}
225 	}
226 
227 	@Test
228 	public void saveNewSWWithSDAsOLE()throws Exception{
229 		String linkFile = prepareData("pvt/plain_200p.odp");
230 		for (i = 1; i <= iterator; i++) {
231 			long start = System.currentTimeMillis();
232 			createNewSW();
233 			app.dispatch(".uno:InsertObject");
234 			sleep(2);
235 			insertObject.waitForExistence(60, 2);
236 			objectFromFile.check();
237 			objectFilePath.setText(linkFile);
238 			objectFileLink.check();
239 			insertObject.ok();
240 			sleep(10);
241 
242 			writer.focus();
243 			writer.doubleClick(200, 300);
244 			sleep(15);
245 			writer.activate();
246 			writer.click(10, 10);
247 			sleep(5);
248 
249 			saveAndReopenNewSW(i);
250 			long end = System.currentTimeMillis();
251 			addRecord(i, start, end);
252 		}
253 	}
254 
255 	@Test
256 	public void saveNewSWWithTOC() throws Exception {
257 		for (i = 1; i <= iterator; i++) {
258 			long start = System.currentTimeMillis();
259 			createNewSW();
260 			for (int j = 0; j < 6; j++) {
261 				writer.typeKeys(String.valueOf(j + 1));
262 				writer.typeKeys("<enter>");
263 			}
264 			writer.typeKeys("<ctrl home>");
265 			for (int k = 0; k < 2; k++) {
266 				writer.typeKeys("<shift down>");
267 			}
268 			if (!styleAndFormattingDlg.exists()) {
269 				app.dispatch(".uno:DesignerDialog");
270 				sleep(2);
271 			}
272 
273 			styleAndFormattingList.doubleClick(0.5, 0.25);
274 			sleep(2);
275 			writer.typeKeys("<down>");
276 			writer.typeKeys("<right>");
277 			writer.typeKeys("<left>");
278 			for (int k = 0; k < 2; k++) {
279 				writer.typeKeys("<shift down>");
280 			}
281 			styleAndFormattingList.doubleClick(0.5, 0.3);
282 			sleep(2);
283 			writer.typeKeys("<ctrl home>");
284 			sleep(2);
285 			writer.menuItem("Insert->Indexes and Tables->Indexes and Tables...").select();
286 			sleep(2);
287 			insertIndexDlg.ok();
288 			sleep(2);
289 			writer.typeKeys("<ctrl end>");
290 			saveAndReopenNewSW(i);
291 			long end = System.currentTimeMillis();
292 			addRecord(i, start, end);
293 		}
294 	}
295 
296 	@Test
297 	public void splitMergeTableOnOpenedSW() throws Exception {
298 		String file = prepareData("svt/swdesign.odt");
299 		for (i = 1; i <= iterator; i++) {
300 			long start = System.currentTimeMillis();
301 			app.dispatch(".uno:Open");
302 			submitOpenDlg(file);
303 			writer.waitForExistence(10, 2);
304 			writer.typeKeys("<down>");
305 			writer.typeKeys("<down>");
306 			sleep(2);
307 
308 			writer.menuItem("Table->Split Cells").select();
309 			sleep(2);
310 			writerSplitCellDlg.ok();
311 			sleep(2);
312 
313 			writer.typeKeys("<shift down>");
314 			writer.typeKeys("<shift down>");
315 			writer.typeKeys("<shift down>");
316 			writer.typeKeys("<shift down>");
317 			writer.typeKeys("<shift down>");
318 			sleep(2);
319 			writer.menuItem("Table->Merge Cells").select();
320 			sleep(2);
321 
322 			writer.typeKeys("<enter>");
323 			closeWithoutSaveSW();
324 			long end = System.currentTimeMillis();
325 			addRecord(i, start, end);
326 		}
327 	}
328 
329 	@Test
330 	public void addDelColumnsRowsInTableOnOpenedSW() throws Exception {
331 		String file = prepareData("svt/swdesign.odt");
332 		for (i = 1; i <= iterator; i++) {
333 			long start = System.currentTimeMillis();
334 			app.dispatch(".uno:Open");
335 			submitOpenDlg(file);
336 			writer.waitForExistence(10, 2);
337 			writer.typeKeys("<down>");
338 			writer.typeKeys("<down>");
339 			sleep(2);
340 
341 			app.dispatch(".uno:InsertRowDialog");
342 			writerInsertRowsDlg.ok();
343 			sleep(2);
344 
345 			writer.menuItem("Table->Delete->Rows").select();
346 			sleep(2);
347 
348 			app.dispatch(".uno:InsertColumnDialog");
349 			writerInsertColumnsDlg.ok();
350 			sleep(2);
351 
352 			writer.menuItem("Table->Delete->Columns").select();
353 			sleep(2);
354 
355 			closeWithoutSaveSW();
356 			long end = System.currentTimeMillis();
357 			addRecord(i, start, end);
358 		}
359 	}
360 
361 	@Test
362 	public void setParaPropertoesOnOpenedSW() throws Exception {
363 		String file = prepareData("pvt/plain_200p.odt");
364 		for (i = 1; i <= iterator; i++) {
365 			long start = System.currentTimeMillis();
366 			app.dispatch(".uno:Open");
367 			submitOpenDlg(file);
368 			writer.waitForExistence(10, 2);
369 			writer.focus();
370 			app.dispatch(".uno:ParagraphDialog");
371 			alignPage.select();
372 			alignPageRight.check();
373 			sleep(2);
374 			alignPageVerticalAlign.select("Bottom");
375 			sleep(2);
376 			alignPage.ok();
377 			sleep(2);
378 			writer.typeKeys("<ctrl end>");
379 			sleep(5);
380 
381 			closeWithoutSaveSW();
382 			long end = System.currentTimeMillis();
383 			addRecord(i, start, end);
384 		}
385 	}
386 
387 	@Test
388 	public void InsertPicToOpenedSW() throws Exception {
389 		String file = prepareData("pvt/plain_200p.odt");
390 		String pic = prepareData("image/blue_256x256.jpg");
391 		for (i = 1; i <= iterator; i++) {
392 			long start = System.currentTimeMillis();
393 			app.dispatch(".uno:Open");
394 			submitOpenDlg(file);
395 			writer.waitForExistence(10, 2);
396 			writer.focus();
397 			writer.menuItem("Insert->Picture->From File...").select();
398 			sleep(2);
399 			filePickerPath.setText(pic);
400 			sleep(1);
401 			filePickerOpen.click();
402 			sleep(5);
403 			writer.typeKeys("<esc>");
404 			sleep(2);
405 			closeWithoutSaveSW();
406 			long end = System.currentTimeMillis();
407 			addRecord(i, start, end);
408 		}
409 	}
410 
411 	@Test
412 	public void InsertOLEToOpenedSW() throws Exception {
413 		String file = prepareData("pvt/plain_200p.odt");
414 		for (i = 1; i <= iterator; i++) {
415 			long start = System.currentTimeMillis();
416 			app.dispatch(".uno:Open");
417 			submitOpenDlg(file);
418 			writer.waitForExistence(10, 2);
419 			writer.focus();
420 			app.dispatch(".uno:InsertObject");
421 			sleep(2);
422 			insertObject.ok();
423 			sleep(10);
424 			typeKeys("<esc>");
425 			sleep(5);
426 			typeKeys("<esc>");
427 			sleep(5);
428 			closeWithoutSaveSW();
429 			long end = System.currentTimeMillis();
430 			addRecord(i, start, end);
431 		}
432 	}
433 
434 	@Test
435 	public void InsertChartToOpenedSW() throws Exception {
436 		String file = prepareData("pvt/plain_200p.odt");
437 		for (i = 1; i <= iterator; i++) {
438 			long start = System.currentTimeMillis();
439 			app.dispatch(".uno:Open");
440 			submitOpenDlg(file);
441 			writer.waitForExistence(10, 2);
442 			writer.focus();
443 			writer.menuItem("Insert->Object->Chart...").select();
444 			sleep(5);
445 			typeKeys("<esc>");
446 			sleep(5);
447 			typeKeys("<esc>");
448 			sleep(5);
449 			typeKeys("<esc>");
450 			sleep(5);
451 			closeWithoutSaveSW();
452 			long end = System.currentTimeMillis();
453 			addRecord(i, start, end);
454 		}
455 	}
456 
457 	@Test
458 	public void openSWWithXForm()throws Exception{
459 		String file = prepareData("svt/sickness_self-certification_meory issue.odt");
460 		for (i = 1; i <= iterator; i++) {
461 			long start = System.currentTimeMillis();
462 			app.dispatch(".uno:Open");
463 			submitOpenDlg(file);
464 			writer.waitForExistence(10, 2);
465 			app.dispatch(".uno:CloseDoc");
466 
467 			long end = System.currentTimeMillis();
468 			addRecord(i, start, end);
469 		}
470 	}
471 
472 	@Test
473 	public void openSWWithSecuritySignature()throws Exception{
474 		String file = prepareData("svt/howtouse.odt");
475 		for (i = 1; i <= iterator; i++) {
476 			long start = System.currentTimeMillis();
477 			app.dispatch(".uno:Open");
478 			submitOpenDlg(file);
479 			writer.waitForExistence(10, 2);
480 			app.dispatch(".uno:CloseDoc");
481 
482 			long end = System.currentTimeMillis();
483 			addRecord(i, start, end);
484 		}
485 	}
486 
487 
488 	private void createNewSW() {
489 		app.dispatch("private:factory/swriter");
490 	}
491 
492 	private void saveAndReopenNewSW(int iterator) {
493 		String saveTo = getPath("temp/" + "tempSW_New" + iterator + ".odt");
494 		writer.menuItem("File->Save As...").select();
495 		FileUtil.deleteFile(saveTo);
496 		submitSaveDlg(saveTo);
497 		if (activeMsgBox.exists()) {
498 			activeMsgBox.yes();
499 			sleep(2);
500 		}
501 		app.dispatch(".uno:CloseDoc");
502 		startCenterOpenButton.waitForExistence(30, 2);
503 		startCenterOpenButton.click();
504 		submitOpenDlg(saveTo);
505 		sleep(1);
506 		app.dispatch(".uno:CloseDoc");
507 	}
508 
509 	private void closeWithoutSaveSW() {
510 		writer.menuItem("File->Close").select();
511 		if (activeMsgBox.exists()) {
512 			activeMsgBox.no();
513 			sleep(2);
514 		}
515 	}
516 
517 	private HashMap<String, Object> getPerfData() {
518 		HashMap<String, Object> proccessInfo = SystemUtil
519 				.findProcess(".*(soffice\\.bin|soffice\\.exe|soffice).*");
520 		String pid = (String) proccessInfo.get("pid");
521 		return SystemUtil.getProcessPerfData(pid);
522 	}
523 
524 	private void addRecord(int i, long start, long end) {
525 		HashMap<String, Object> perf = getPerfData();
526 		xmlResult.addRow("BasicFuncOnWriter",testname.getMethodName(), i, (end - start),
527 				perf.get("rss"), perf.get("pcpu"));
528 	}
529 
530 }
531