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