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 
26 package svt.gui.sc;
27 
28 import static org.openoffice.test.common.Testspace.*;
29 import static org.openoffice.test.vcl.Tester.*;
30 import static testlib.gui.AppTool.*;
31 import static testlib.gui.UIMap.*;
32 
33 import java.io.FileOutputStream;
34 import java.io.PrintStream;
35 import java.util.HashMap;
36 
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.TestName;
45 import org.openoffice.test.OpenOffice;
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 import testlib.gui.SCTool;
53 
54 public class BasicFuncOnCalc {
55 	@Rule
56 	public Logger log = Logger.getLogger(this);
57 
58 	@Rule
59 	public TestName testname = new TestName();
60 
61 	private static DataSheet xmlResult;
62 
63 	private String pid = null;
64 
65 	private static int iterator = 100;
66 
67 	private int i = 0;
68 
69 	/**
70 	 * @throws java.lang.Exception
71 	 */
72 	@BeforeClass
73 	public static void beforeClass() throws Exception {
74 
75 		xmlResult = new DataSheet(getFile("output/svt_gui_sc.xml"));
76 		xmlResult.addRow("BasicFuncOnCalc", "Method", "Iterator",
77 				"Consumed Time(MS)", "Memory(KB)", "CPU(%)");
78 	}
79 
80 	@AfterClass
81 	public static void afterClass() throws Exception {
82 		app.stop();
83 	}
84 
85 	@Before
86 	public void before()throws Exception{
87 		app.start(true);
88 	}
89 
90 
91 	@Test
92 	public void saveNewSCWithNumberFormat() {
93 		for (i = 1; i <= iterator; i++) {
94 			long start = System.currentTimeMillis();
95 			createNewSC();
96 			SCTool.selectRange("A1");
97 			typeKeys("0.3");
98 			sleep(1);
99 			app.dispatch(".uno:FormatCellDialog");
100 			sleep(2);
101 			scFormatCode.setText("0%");
102 			typeKeys("<enter>");
103 			sleep(1);
104 			saveAndReopenNewSC(i);
105 			long end = System.currentTimeMillis();
106 			addRecord(i, start, end);
107 		}
108 	}
109 
110 	@Test
111 	public void saveNewSCWithPrintRange() throws Exception {
112 		for (i = 1; i <= iterator; i++) {
113 			long start = System.currentTimeMillis();
114 			createNewSC();
115 			app.dispatch(".uno:EditPrintArea");
116 			sleep(1);
117 			scPrintAreaType.select("- user defined -");
118 			scPrintArea.setText("$A$1:$C$4");
119 			typeKeys("<enter>");
120 			SCTool.selectRange("A1");
121 			typeKeys("test");
122 			saveAndReopenNewSC(i);
123 			long end = System.currentTimeMillis();
124 			addRecord(i, start, end);
125 		}
126 	}
127 
128 	@Test
129 	public void saveNewSCWithText() throws Exception {
130 		for (i = 1; i <= iterator; i++) {
131 			long start = System.currentTimeMillis();
132 			createNewSC();
133 			SCTool.selectRange("A1");
134 			typeKeys("test");
135 			saveAndReopenNewSC(i);
136 			long end = System.currentTimeMillis();
137 			addRecord(i, start, end);
138 		}
139 	}
140 
141 	@Test
142 	public void saveNewSCWithDataSort() throws Exception {
143 		for (i = 1; i <= iterator; i++) {
144 			long start = System.currentTimeMillis();
145 			createNewSC();
146 			SCTool.selectRange("A1");
147 			typeKeys("3<down>2<down>5<down>1<down>6<down>4<down>10<down>8<down>9<down>7");
148 			sleep(1);
149 			app.dispatch(".uno:DataSort");
150 			sortOptionsPage.select();
151 			sortOptionsPageRangeContainsColumnLabels.uncheck();
152 			sortPage.select();
153 			sortPageBy1.select(1); // "Column A"
154 			sortPageAscending1.check();
155 			sortPage.ok();
156 			saveAndReopenNewSC(i);
157 			long end = System.currentTimeMillis();
158 			addRecord(i, start, end);
159 		}
160 	}
161 
162 	@Test
163 	public void saveNewSCWithInsertSheets() throws Exception {
164 		for (i = 1; i <= iterator; i++) {
165 			long start = System.currentTimeMillis();
166 			createNewSC();
167 			app.dispatch(".uno:Insert");
168 			scAfterCurrentSheet.check();
169 			scNewSheetName.setText("Instant Filter");
170 			scInsertSheetDlg.ok();
171 			sleep(5);
172 			saveAndReopenNewSC(i);
173 			long end = System.currentTimeMillis();
174 			addRecord(i, start, end);
175 		}
176 	}
177 
178 	@Test
179 	public void saveNewSCWithStandardFilter() throws Exception {
180 		for (i = 1; i <= iterator; i++) {
181 			long start = System.currentTimeMillis();
182 			createNewSC();
183 			SCTool.selectRange("A1");
184 			typeKeys("A<down>1<down>2<down>3<down>1<down>2<down>3<down>1<down>2<down>3");
185 			sleep(1);
186 			SCTool.selectRange("A1");
187 			app.dispatch(".uno:DataFilterStandardFilter");
188 			sleep(2);
189 			filterValue1.setText("1");
190 			standardFilterDlg.ok();
191 			saveAndReopenNewSC(i);
192 			long end = System.currentTimeMillis();
193 			addRecord(i, start, end);
194 		}
195 	}
196 
197 	@Test
198 	public void saveNewSCWithInsertPic() throws Exception {
199 		String pic = prepareData("image/blue_256x256.jpg");
200 		for (i = 1; i <= iterator; i++) {
201 			long start = System.currentTimeMillis();
202 			createNewSC();
203 			calc.menuItem("Insert->Picture->From File...").select();
204 			sleep(2);
205 			filePickerPath.setText(pic);
206 			sleep(1);
207 			filePickerOpen.click();
208 			sleep(2);
209 			typeKeys("<esc>");
210 			sleep(2);
211 			saveAndReopenNewSC(i);
212 			long end = System.currentTimeMillis();
213 			addRecord(i, start, end);
214 		}
215 	}
216 
217 	@Test
218 	public void saveNewSCWithValidaty() throws Exception {
219 		for (i = 1; i <= iterator; i++) {
220 			long start = System.currentTimeMillis();
221 			createNewSC();
222 			app.dispatch(".uno:Insert");
223 			scAfterCurrentSheet.check();
224 			scNewSheetName.setText("Data Validate");
225 			scInsertSheetDlg.ok();
226 			sleep(5);
227 			SCTool.selectRange("B2:E5");
228 			sleep(2);
229 
230 			app.dispatch(".uno:Validation");
231 			scValidityCriteriaTabpage.select();
232 			scValidityCriteriaAllowList.select("Whole Numbers");
233 			scValidityDecimalCompareOperator.select("greater than");
234 			scValiditySourceInput.setText("1");
235 			scValidityErrorAlertTabPage.select();
236 			scValidityShowErrorMessage.check();
237 			scValidityErrorMessageTitle.setText("Error");
238 			scValidityErrorMessage.setText("Must greater than 1");
239 			scValidityErrorAlertTabPage.ok();
240 
241 			SCTool.selectRange("B2");
242 			typeKeys("0<enter>");
243 			activeMsgBox.ok();
244 			sleep(1);
245 
246 			SCTool.selectRange("E5");
247 			typeKeys("1<enter>");
248 			activeMsgBox.ok();
249 			sleep(1);
250 
251 			SCTool.selectRange("E2");
252 			typeKeys("2<enter>");
253 			sleep(5);
254 			saveAndReopenNewSC(i);
255 			long end = System.currentTimeMillis();
256 			addRecord(i, start, end);
257 		}
258 	}
259 
260 	@Test
261 	public void insertChartToOpenedSC() throws Exception {
262 		String file = prepareData("pvt/plain_11s.ods");
263 		String[][] inputStr = { { "Area", "Item", "Count" }, { "1", "2", "3" },
264 				{ "4", "5", "6" }, { "7", "8", "9" }, { "10", "11", "12" } };
265 		for (i = 1; i <= iterator; i++) {
266 			long start = System.currentTimeMillis();
267 			app.dispatch(".uno:Open");
268 			submitOpenDlg(file);
269 			calc.waitForExistence(20, 2);
270 			sleep(2);
271 
272 			// Insert Chart
273 			app.dispatch(".uno:Insert");
274 			scAfterCurrentSheet.check();
275 			scNewSheetName.setText("Instant Chart");
276 			scInsertSheetDlg.ok();
277 			sleep(5);
278 			inputCells(inputStr);
279 			sleep(2);
280 			SCTool.selectRange("A1:C5");
281 			calc.menuItem("Insert->Chart...").select();
282 			sleep(1);
283 			chartWizard.ok();
284 			sleep(5);
285 			calc.typeKeys("<esc>");
286 			sleep(5);
287 			calc.typeKeys("<esc>");
288 			sleep(5);
289 			calc.typeKeys("<esc>");
290 			sleep(5);
291 			closeWithoutSaveSC();
292 			long end = System.currentTimeMillis();
293 			addRecord(i, start, end);
294 		}
295 	}
296 
297 	@Test
298 	public void insertPicToOpenedSC() throws Exception {
299 		String file = prepareData("pvt/plain_11s.ods");
300 		String pic = prepareData("image/blue_256x256.jpg");
301 		for (i = 1; i <= iterator; i++) {
302 			long start = System.currentTimeMillis();
303 			app.dispatch(".uno:Open");
304 			submitOpenDlg(file);
305 			calc.waitForExistence(20, 2);
306 			sleep(2);
307 
308 			// Insert Chart
309 			app.dispatch(".uno:Insert");
310 			scAfterCurrentSheet.check();
311 			scNewSheetName.setText("Instant Graphic");
312 			scInsertSheetDlg.ok();
313 			sleep(5);
314 			calc.menuItem("Insert->Picture->From File...").select();
315 			sleep(2);
316 			filePickerPath.setText(pic);
317 			sleep(1);
318 			filePickerOpen.click();
319 			sleep(5);
320 			calc.typeKeys("<esc>");
321 			sleep(2);
322 			closeWithoutSaveSC();
323 			long end = System.currentTimeMillis();
324 			addRecord(i, start, end);
325 		}
326 	}
327 
328 	@Test
329 	public void insertFontWorkToOpenedSC() throws Exception {
330 		String file = prepareData("pvt/plain_11s.ods");
331 		for (i = 1; i <= iterator; i++) {
332 			long start = System.currentTimeMillis();
333 			app.dispatch(".uno:Open");
334 			submitOpenDlg(file);
335 			calc.waitForExistence(20, 2);
336 			sleep(2);
337 
338 			calc.menuItem("View->Toolbars->Fontwork").select();
339 			sleep(2);
340 			app.dispatch(".uno:FontworkGalleryFloater");
341 			sleep(2);
342 			typeKeys("<right>");
343 			fontworkGalleryDlg.ok();
344 			sleep(2);
345 			calc.typeKeys("<esc>");
346 			sleep(2);
347 
348 			closeWithoutSaveSC();
349 			long end = System.currentTimeMillis();
350 			addRecord(i, start, end);
351 		}
352 	}
353 
354 	@Test
355 	public void switchFileContainingChart() throws Exception {
356 		String file1 = prepareData("svt/64K_Chart1.ods");
357 		String file2 = prepareData("svt/64K_Chart2.ods");
358 		String file3 = prepareData("svt/64K_Chart3.ods");
359 		String file4 = prepareData("svt/64K_Chart4.ods");
360 		String file5 = prepareData("svt/64K_Chart5.ods");
361 		String file6 = prepareData("svt/64K_Chart6.ods");
362 
363 		for (i = 1; i <= iterator; i++) {
364 			long start = System.currentTimeMillis();
365 			app.dispatch(".uno:Open");
366 			submitOpenDlg(file1);
367 			calc.waitForExistence(20, 2);
368 			sleep(2);
369 
370 			app.dispatch(".uno:Open");
371 			submitOpenDlg(file2);
372 			calc.waitForExistence(20, 2);
373 			sleep(2);
374 
375 			app.dispatch(".uno:Open");
376 			submitOpenDlg(file3);
377 			calc.waitForExistence(20, 2);
378 			sleep(2);
379 
380 			app.dispatch(".uno:Open");
381 			submitOpenDlg(file4);
382 			calc.waitForExistence(20, 2);
383 			sleep(2);
384 
385 			app.dispatch(".uno:Open");
386 			submitOpenDlg(file5);
387 			calc.waitForExistence(20, 2);
388 			sleep(2);
389 
390 			app.dispatch(".uno:Open");
391 			submitOpenDlg(file6);
392 			calc.waitForExistence(20, 2);
393 
394 			sleep(2);
395 
396 			selectCertainFile(2);
397 			sleep(2);
398 			selectCertainFile(3);
399 			sleep(2);
400 			selectCertainFile(5);
401 			sleep(2);
402 
403 			closeAlldocuments();
404 			sleep(3);
405 			long end = System.currentTimeMillis();
406 			addRecord(i, start, end);
407 		}
408 	}
409 
410 	public void selectCertainFile(int k) {
411 		calc.menuItem("Window").select();
412 		k += 5;
413 		for (int i = 0; i < k; i++) {
414 			typeKeys("<down>");
415 		}
416 		typeKeys("<enter>");
417 		sleep(2);
418 	}
419 
420 	public void closeAlldocuments() {
421 		for (int index = 3; index < 9; index++) {
422 			calc.menuItem("File->Close").select();
423 			sleep(2);
424 		}
425 
426 	}
427 
428 	public static void inputCells(String[][] inputs) {
429 		String back = "";
430 		for (int i = 0; i < inputs.length; i++) {
431 			calc.typeKeys(back);
432 			sleep(1);
433 			back = "";
434 			for (int j = 0; j < inputs[i].length; j++) {
435 				typeKeys(inputs[i][j]);
436 				typeKeys("<Right>");
437 				sleep(0.5);
438 				back += "<Left>";
439 			}
440 			back += "<enter>";
441 		}
442 		sleep(2);
443 	}
444 
445 	private void createNewSC() {
446 		app.dispatch("private:factory/scalc");
447 	}
448 
449 	private void saveAndReopenNewSC(int iterator) {
450 		String saveTo = getPath("temp/" + "tempSC_New" + iterator + ".ods");
451 		calc.menuItem("File->Save As...").select();
452 		FileUtil.deleteFile(saveTo);
453 		submitSaveDlg(saveTo);
454 		if (activeMsgBox.exists()) {
455 			activeMsgBox.yes();
456 			sleep(2);
457 		}
458 		app.dispatch(".uno:CloseDoc");
459 		// sleep(2);
460 		startCenterOpenButton.waitForExistence(30, 2);
461 		startCenterOpenButton.click();
462 		submitOpenDlg(saveTo);
463 		sleep(1);
464 		app.dispatch(".uno:CloseDoc");
465 	}
466 
467 	private void closeWithoutSaveSC() {
468 		calc.menuItem("File->Close").select();
469 		activeMsgBox.no();
470 		sleep(2);
471 	}
472 
473 	private HashMap<String, Object> getPerfData() {
474 		HashMap<String, Object> proccessInfo = SystemUtil
475 				.findProcess(".*(soffice\\.bin|soffice\\.exe|soffice).*");
476 		String pid = (String) proccessInfo.get("pid");
477 		return SystemUtil.getProcessPerfData(pid);
478 	}
479 
480 	private void addRecord(int i, long start, long end) {
481 		HashMap<String, Object> perf = getPerfData();
482 		xmlResult.addRow("BasicFuncOnCalc",testname.getMethodName(), i, (end - start),
483 				perf.get("rss"), perf.get("pcpu"));
484 	}
485 }
486