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 package fvt.uno.sc.formula;
23 
24 import static org.junit.Assert.*;
25 
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 
31 import org.junit.runner.RunWith;
32 import org.junit.runners.Parameterized;
33 import org.junit.runners.Parameterized.Parameters;
34 
35 import java.util.Arrays;
36 import java.util.Collection;
37 
38 import org.openoffice.test.common.Testspace;
39 import org.openoffice.test.common.Logger;
40 
41 import org.openoffice.test.uno.UnoApp;
42 
43 import testlib.uno.SCUtil;
44 import static testlib.uno.TestUtil.*;
45 
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.document.MacroExecMode;
48 import com.sun.star.lang.XComponent;
49 import com.sun.star.sheet.XSpreadsheet;
50 import com.sun.star.sheet.XSpreadsheetDocument;
51 import com.sun.star.sheet.XSpreadsheets;
52 import com.sun.star.table.XCell;
53 import com.sun.star.uno.Any;
54 import com.sun.star.text.XText;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.util.XModifiable;
57 
58 import java.util.logging.Level;
59 
60 @RunWith(Parameterized.class)
61 public class TestFormulaDocs {
62 
63 	private String filename;
64 	private String type;
65 
66 	@Parameters
data()67 	public static Collection<Object[]> data() {
68 		return Arrays.asList(new Object[][]{
69 				// test documents
70 				{"uno/sc/fvt/FormulaTest1.ods", "FormulaTest1.ods"},
71 				{"uno/sc/fvt/StarBasicYearMonthDateHourMinuteSecondTests.ods", "Basic Year Month Date Hour Minute Second Test"},
72 				{"uno/sc/fvt/StarBasicCLng.ods", "Basic Convert to Long Function Test"},
73 				{"uno/sc/fvt/StarBasicTab.ods", "Basic Tab Function Test"},
74 				{"uno/sc/fvt/DGET on formulas.ods", "DGET on formulas Test"},
75 				{"uno/sc/fvt/Basic Line as variable and Line Input.ods", "Basic Line as variable and Line Input Test"},
76 				{"uno/sc/fvt/comment-in-single-line-if-then-else.ods", "Basic comment after single line if statement Test"},
77 				{"uno/sc/fvt/Bug81233ColumnZReference.xml", "Bug 81233 column Z reference wrongly converts to column A"},
78 				{"uno/sc/fvt/Bug100989MergeAcross0AddsExtraEmptyCell.xml", "Bug 100989 ss:MergeCross=\"0\" adds an extra empty cell to the right"},
79 				{"uno/sc/fvt/Bug128554FractionalSecondsIgnored.xml", "Bug 100989 fractional seconds are silently ignored during import"}
80 		});
81 	}
82 
83 	@Rule
84 	public Logger log = Logger.getLogger(this);
85 
86 	static UnoApp unoApp = new UnoApp();
87 	XComponent scComponent = null;
88 
89 	/**
90 	 * Clean class after testing
91 	 *
92 	 * @throws Exception
93 	 */
94 	@AfterClass
afterClass()95 	public static void afterClass() {
96 		unoApp.close();
97 	}
98 
99 	@Before
setUp()100 	public void setUp() throws Exception {
101 		unoApp.close(); // moved here from tearDown because stopping app there causes a late screenshot
102 		unoApp.start();
103 	}
104 
TestFormulaDocs(String filename, String type)105 	public TestFormulaDocs(String filename, String type) {
106 		this.filename = filename;
107 		this.type = type;
108 	}
109 
110 	// FIXME: only needs a timeout for running tests against AOO41X due to fixes for i112383 and i117960 present in trunk
111 	// haven't been backported to AOO41X yet and causes these tests to hang on an error dialog.
112 	@Test(timeout = 15000)
testOneDoc()113 	public void testOneDoc() throws Exception {
114 		// open the spreadsheet document
115 		String sample = Testspace.prepareData(filename);
116 		// enable macros
117 		PropertyValue prop = new PropertyValue();
118 		prop.Name = "MacroExecutionMode";
119 		prop.Value = MacroExecMode.ALWAYS_EXECUTE_NO_WARN;
120 		XSpreadsheetDocument scDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
121 			XSpreadsheetDocument.class, unoApp.loadDocument(sample, prop));
122 		XSpreadsheet xSheet = SCUtil.getCurrentSheet( scDoc);
123 
124 		// find the "TestID" and "TestOK" markers
125 		int nTestIdCol = -1;
126 		int nTestOkCol = -1;
127 		int nTestRowStart = -1;
128 		for( int y = 0; y < 8; ++y) {
129 			for( int x = 0; x < 26; ++x) {
130 				XCell xCell = xSheet.getCellByPosition( x, y);
131 				XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
132 				String name = xText.getString();
133 				if( name.equals( "TestID")) {
134 					assertTrue( "Multiple rows with TestID marker!", nTestIdCol == -1);
135 					assertTrue( nTestRowStart == -1);
136 					nTestIdCol = x;
137 					nTestRowStart = y + 1;
138 				} else if( name.equals( "TestOK")) {
139 					assertTrue( "Multiple rows with TestOK marker!", nTestOkCol == -1);
140 					assertTrue( "TestID and TestOK marker not in same row!", nTestRowStart == y + 1);
141 					nTestOkCol = x;
142 				}
143 			}
144 		}
145 		assertTrue( "Column \"TestID\" not found!", nTestIdCol >= 0);
146 		assertTrue( "Column \"TestOK\" not found!", nTestOkCol >= 0);
147 
148 		int nTestRowEnd = nTestRowStart + 100; // TODO: get the last row from the sheet
149 		int nTestCount = 0;
150 		int nFailCount = 0;
151 		for( int y = nTestRowStart; y < nTestRowEnd; ++y) {
152 			// get the test id
153 			XCell xCell = xSheet.getCellByPosition( nTestIdCol, y);
154 			XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
155 			String testId = xText.getString();
156 			// ignore rows without test ids
157 			if( testId.length() == 0)
158 				continue;
159 			++nTestCount;
160 
161 			// get and check the test result
162 			xCell = xSheet.getCellByPosition( nTestOkCol, y);
163 			String testOk = ((XText)UnoRuntime.queryInterface( XText.class, xCell)).getString();
164 			assertTrue( "Test result must be TRUE or FALSE", testOk.equals("TRUE") || testOk.equals("FALSE"));
165 			boolean bOK = testOk.equals("TRUE");
166 			// mark evaluated test results
167 			SCUtil.setProperties( xCell, "CellBackColor", (Integer)(bOK ? 0x00FF00 : 0xFF0000));
168 			// handle failed test cases
169 			if( !bOK) {
170 				++nFailCount;
171 				log.log( Level.SEVERE, "\ttest \""+testId+" failed");
172 			}
173 		}
174 
175 		assertTrue( (""+nFailCount+" of "+nTestCount+" tests failed for " + type), nFailCount==0);
176 
177 		XModifiable modified = (XModifiable)UnoRuntime.queryInterface( XModifiable.class, scDoc);
178 		modified.setModified( false);
179 		SCUtil.closeFile( scDoc);
180 	}
181 
182 }
183