xref: /AOO42X/test/testuno/source/fvt/uno/sc/formula/TestFormulaDocs.java (revision 228319fb69de765cf0374cf319264e9c4b6f8357)
111e9ca94SHerbert Dürr /**************************************************************
211e9ca94SHerbert Dürr  *
311e9ca94SHerbert Dürr  * Licensed to the Apache Software Foundation (ASF) under one
411e9ca94SHerbert Dürr  * or more contributor license agreements.  See the NOTICE file
511e9ca94SHerbert Dürr  * distributed with this work for additional information
611e9ca94SHerbert Dürr  * regarding copyright ownership.  The ASF licenses this file
711e9ca94SHerbert Dürr  * to you under the Apache License, Version 2.0 (the
811e9ca94SHerbert Dürr  * "License"); you may not use this file except in compliance
911e9ca94SHerbert Dürr  * with the License.  You may obtain a copy of the License at
1011e9ca94SHerbert Dürr  *
1111e9ca94SHerbert Dürr  *   http://www.apache.org/licenses/LICENSE-2.0
1211e9ca94SHerbert Dürr  *
1311e9ca94SHerbert Dürr  * Unless required by applicable law or agreed to in writing,
1411e9ca94SHerbert Dürr  * software distributed under the License is distributed on an
1511e9ca94SHerbert Dürr  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1611e9ca94SHerbert Dürr  * KIND, either express or implied.  See the License for the
1711e9ca94SHerbert Dürr  * specific language governing permissions and limitations
1811e9ca94SHerbert Dürr  * under the License.
1911e9ca94SHerbert Dürr  *
2011e9ca94SHerbert Dürr  *************************************************************/
2111e9ca94SHerbert Dürr 
2211e9ca94SHerbert Dürr package fvt.uno.sc.formula;
2311e9ca94SHerbert Dürr 
2411e9ca94SHerbert Dürr import static org.junit.Assert.*;
2511e9ca94SHerbert Dürr 
26fcf28a01SCarl Marcum import org.junit.AfterClass;
2711e9ca94SHerbert Dürr import org.junit.Before;
2811e9ca94SHerbert Dürr import org.junit.Rule;
2911e9ca94SHerbert Dürr import org.junit.Test;
3011e9ca94SHerbert Dürr 
31fcf28a01SCarl Marcum import org.junit.runner.RunWith;
32fcf28a01SCarl Marcum import org.junit.runners.Parameterized;
33fcf28a01SCarl Marcum import org.junit.runners.Parameterized.Parameters;
34fcf28a01SCarl Marcum 
35fcf28a01SCarl Marcum import java.util.Arrays;
36fcf28a01SCarl Marcum import java.util.Collection;
37fcf28a01SCarl Marcum 
3811e9ca94SHerbert Dürr import org.openoffice.test.common.Testspace;
3911e9ca94SHerbert Dürr import org.openoffice.test.common.Logger;
4011e9ca94SHerbert Dürr 
4111e9ca94SHerbert Dürr import org.openoffice.test.uno.UnoApp;
4211e9ca94SHerbert Dürr 
4311e9ca94SHerbert Dürr import testlib.uno.SCUtil;
4411e9ca94SHerbert Dürr import static testlib.uno.TestUtil.*;
4511e9ca94SHerbert Dürr 
46c7723a22SDamjan Jovanovic import com.sun.star.beans.PropertyValue;
47c7723a22SDamjan Jovanovic import com.sun.star.document.MacroExecMode;
4811e9ca94SHerbert Dürr import com.sun.star.lang.XComponent;
4911e9ca94SHerbert Dürr import com.sun.star.sheet.XSpreadsheet;
5011e9ca94SHerbert Dürr import com.sun.star.sheet.XSpreadsheetDocument;
5111e9ca94SHerbert Dürr import com.sun.star.sheet.XSpreadsheets;
5211e9ca94SHerbert Dürr import com.sun.star.table.XCell;
5311e9ca94SHerbert Dürr import com.sun.star.uno.Any;
5411e9ca94SHerbert Dürr import com.sun.star.text.XText;
5511e9ca94SHerbert Dürr import com.sun.star.uno.UnoRuntime;
5611e9ca94SHerbert Dürr import com.sun.star.util.XModifiable;
5711e9ca94SHerbert Dürr 
5811e9ca94SHerbert Dürr import java.util.logging.Level;
5911e9ca94SHerbert Dürr 
60fcf28a01SCarl Marcum @RunWith(Parameterized.class)
6111e9ca94SHerbert Dürr public class TestFormulaDocs {
6211e9ca94SHerbert Dürr 
63fcf28a01SCarl Marcum     private String filename;
64fcf28a01SCarl Marcum     private String type;
65fcf28a01SCarl Marcum 
66fcf28a01SCarl Marcum     @Parameters
data()67fcf28a01SCarl Marcum     public static Collection<Object[]> data() {
68fcf28a01SCarl Marcum         return Arrays.asList(new Object[][]{
69fcf28a01SCarl Marcum                 // test documents
70fcf28a01SCarl Marcum                 {"uno/sc/fvt/FormulaTest1.ods", "FormulaTest1.ods"},
7148b860b6SDamjan Jovanovic                 {"uno/sc/fvt/StarBasicYearMonthDateHourMinuteSecondTests.ods", "Basic Year Month Date Hour Minute Second Test"},
7248b860b6SDamjan Jovanovic                 {"uno/sc/fvt/StarBasicCLng.ods", "Basic Convert to Long Function Test"},
73a1409b3eSCarl Marcum                 {"uno/sc/fvt/StarBasicTab.ods", "Basic Tab Function Test"},
7448b860b6SDamjan Jovanovic                 {"uno/sc/fvt/DGET on formulas.ods", "DGET on formulas Test"},
7548b860b6SDamjan Jovanovic                 {"uno/sc/fvt/Basic Line as variable and Line Input.ods", "Basic Line as variable and Line Input Test"},
763b5ec47bSCarl Marcum                 {"uno/sc/fvt/comment-in-single-line-if-then-else.ods", "Basic comment after single line if statement Test"},
770f570a54SDamjan Jovanovic                 {"uno/sc/fvt/Bug81233ColumnZReference.xml", "Bug 81233 column Z reference wrongly converts to column A"},
78b9aa0698SDamjan Jovanovic                 {"uno/sc/fvt/Bug100989MergeAcross0AddsExtraEmptyCell.xml", "Bug 100989 ss:MergeCross=\"0\" adds an extra empty cell to the right"},
7948b860b6SDamjan Jovanovic                 {"uno/sc/fvt/Bug128554FractionalSecondsIgnored.xml", "Bug 100989 fractional seconds are silently ignored during import"}
80fcf28a01SCarl Marcum         });
81fcf28a01SCarl Marcum     }
82fcf28a01SCarl Marcum 
8311e9ca94SHerbert Dürr     @Rule
8411e9ca94SHerbert Dürr     public Logger log = Logger.getLogger(this);
8511e9ca94SHerbert Dürr 
86fcf28a01SCarl Marcum     static UnoApp unoApp = new UnoApp();
8711e9ca94SHerbert Dürr     XComponent scComponent = null;
8811e9ca94SHerbert Dürr 
8911e9ca94SHerbert Dürr     /**
90fcf28a01SCarl Marcum      * Clean class after testing
9111e9ca94SHerbert Dürr      *
9211e9ca94SHerbert Dürr      * @throws Exception
9311e9ca94SHerbert Dürr      */
94fcf28a01SCarl Marcum     @AfterClass
afterClass()95fcf28a01SCarl Marcum     public static void afterClass() {
96fcf28a01SCarl Marcum         unoApp.close();
9711e9ca94SHerbert Dürr     }
9811e9ca94SHerbert Dürr 
99fcf28a01SCarl Marcum     @Before
setUp()100fcf28a01SCarl Marcum     public void setUp() throws Exception {
101fcf28a01SCarl Marcum         unoApp.close(); // moved here from tearDown because stopping app there causes a late screenshot
102fcf28a01SCarl Marcum         unoApp.start();
103fcf28a01SCarl Marcum     }
104fcf28a01SCarl Marcum 
TestFormulaDocs(String filename, String type)105fcf28a01SCarl Marcum     public TestFormulaDocs(String filename, String type) {
106fcf28a01SCarl Marcum         this.filename = filename;
107fcf28a01SCarl Marcum         this.type = type;
108fcf28a01SCarl Marcum     }
109fcf28a01SCarl Marcum 
110fcf28a01SCarl Marcum     // FIXME: only needs a timeout for running tests against AOO41X due to fixes for i112383 and i117960 present in trunk
111fcf28a01SCarl Marcum     // haven't been backported to AOO41X yet and causes these tests to hang on an error dialog.
112fcf28a01SCarl Marcum     @Test(timeout = 15000)
testOneDoc()113fcf28a01SCarl Marcum     public void testOneDoc() throws Exception {
11411e9ca94SHerbert Dürr         // open the spreadsheet document
11511e9ca94SHerbert Dürr         String sample = Testspace.prepareData(filename);
116c7723a22SDamjan Jovanovic         // enable macros
117c7723a22SDamjan Jovanovic         PropertyValue prop = new PropertyValue();
118c7723a22SDamjan Jovanovic         prop.Name = "MacroExecutionMode";
119c7723a22SDamjan Jovanovic         prop.Value = MacroExecMode.ALWAYS_EXECUTE_NO_WARN;
120c7723a22SDamjan Jovanovic         XSpreadsheetDocument scDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
121c7723a22SDamjan Jovanovic             XSpreadsheetDocument.class, unoApp.loadDocument(sample, prop));
12211e9ca94SHerbert Dürr         XSpreadsheet xSheet = SCUtil.getCurrentSheet( scDoc);
12311e9ca94SHerbert Dürr 
12411e9ca94SHerbert Dürr         // find the "TestID" and "TestOK" markers
12511e9ca94SHerbert Dürr         int nTestIdCol = -1;
12611e9ca94SHerbert Dürr         int nTestOkCol = -1;
12711e9ca94SHerbert Dürr         int nTestRowStart = -1;
12811e9ca94SHerbert Dürr         for( int y = 0; y < 8; ++y) {
129c7723a22SDamjan Jovanovic             for( int x = 0; x < 26; ++x) {
13011e9ca94SHerbert Dürr                 XCell xCell = xSheet.getCellByPosition( x, y);
13111e9ca94SHerbert Dürr                 XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
13211e9ca94SHerbert Dürr                 String name = xText.getString();
13311e9ca94SHerbert Dürr                 if( name.equals( "TestID")) {
13411e9ca94SHerbert Dürr                     assertTrue( "Multiple rows with TestID marker!", nTestIdCol == -1);
13511e9ca94SHerbert Dürr                     assertTrue( nTestRowStart == -1);
13611e9ca94SHerbert Dürr                     nTestIdCol = x;
13711e9ca94SHerbert Dürr                     nTestRowStart = y + 1;
13811e9ca94SHerbert Dürr                 } else if( name.equals( "TestOK")) {
13911e9ca94SHerbert Dürr                     assertTrue( "Multiple rows with TestOK marker!", nTestOkCol == -1);
14011e9ca94SHerbert Dürr                     assertTrue( "TestID and TestOK marker not in same row!", nTestRowStart == y + 1);
14111e9ca94SHerbert Dürr                     nTestOkCol = x;
14211e9ca94SHerbert Dürr                 }
14311e9ca94SHerbert Dürr             }
14411e9ca94SHerbert Dürr         }
14511e9ca94SHerbert Dürr         assertTrue( "Column \"TestID\" not found!", nTestIdCol >= 0);
14611e9ca94SHerbert Dürr         assertTrue( "Column \"TestOK\" not found!", nTestOkCol >= 0);
14711e9ca94SHerbert Dürr 
1489930860eSHerbert Dürr         int nTestRowEnd = nTestRowStart + 100; // TODO: get the last row from the sheet
14911e9ca94SHerbert Dürr         int nTestCount = 0;
15011e9ca94SHerbert Dürr         int nFailCount = 0;
15111e9ca94SHerbert Dürr         for( int y = nTestRowStart; y < nTestRowEnd; ++y) {
15211e9ca94SHerbert Dürr             // get the test id
15311e9ca94SHerbert Dürr             XCell xCell = xSheet.getCellByPosition( nTestIdCol, y);
15411e9ca94SHerbert Dürr             XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
15511e9ca94SHerbert Dürr             String testId = xText.getString();
15611e9ca94SHerbert Dürr             // ignore rows without test ids
15711e9ca94SHerbert Dürr             if( testId.length() == 0)
15811e9ca94SHerbert Dürr                 continue;
15911e9ca94SHerbert Dürr             ++nTestCount;
16011e9ca94SHerbert Dürr 
16111e9ca94SHerbert Dürr             // get and check the test result
16211e9ca94SHerbert Dürr             xCell = xSheet.getCellByPosition( nTestOkCol, y);
1639930860eSHerbert Dürr             String testOk = ((XText)UnoRuntime.queryInterface( XText.class, xCell)).getString();
1649930860eSHerbert Dürr             assertTrue( "Test result must be TRUE or FALSE", testOk.equals("TRUE") || testOk.equals("FALSE"));
1659930860eSHerbert Dürr             boolean bOK = testOk.equals("TRUE");
16611e9ca94SHerbert Dürr             // mark evaluated test results
16711e9ca94SHerbert Dürr             SCUtil.setProperties( xCell, "CellBackColor", (Integer)(bOK ? 0x00FF00 : 0xFF0000));
16811e9ca94SHerbert Dürr             // handle failed test cases
16911e9ca94SHerbert Dürr             if( !bOK) {
17011e9ca94SHerbert Dürr                 ++nFailCount;
17111e9ca94SHerbert Dürr                 log.log( Level.SEVERE, "\ttest \""+testId+" failed");
17211e9ca94SHerbert Dürr             }
17311e9ca94SHerbert Dürr         }
17411e9ca94SHerbert Dürr 
175*228319fbSJohn Bampton         assertTrue( (nFailCount+" of "+nTestCount+" tests failed for " + type), nFailCount==0);
17611e9ca94SHerbert Dürr 
17711e9ca94SHerbert Dürr         XModifiable modified = (XModifiable)UnoRuntime.queryInterface( XModifiable.class, scDoc);
17811e9ca94SHerbert Dürr         modified.setModified( false);
17911e9ca94SHerbert Dürr         SCUtil.closeFile( scDoc);
18011e9ca94SHerbert Dürr     }
18111e9ca94SHerbert Dürr 
18211e9ca94SHerbert Dürr }
183