xref: /trunk/test/testuno/source/fvt/uno/sc/formula/TestFormulaDocs.java (revision 4b7284141611cd548e8cb3cffccb02ede08ae72c)
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.openoffice.test.common.FileProvider;
33 import org.openoffice.test.common.FileProvider.FileRepos;
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 import static org.openoffice.test.common.Testspace.*;
46 
47 import com.sun.star.beans.PropertyValue;
48 import com.sun.star.document.MacroExecMode;
49 import com.sun.star.lang.XComponent;
50 import com.sun.star.sheet.XSpreadsheet;
51 import com.sun.star.sheet.XSpreadsheetDocument;
52 import com.sun.star.sheet.XSpreadsheets;
53 import com.sun.star.table.XCell;
54 import com.sun.star.uno.Any;
55 import com.sun.star.text.XText;
56 import com.sun.star.uno.UnoRuntime;
57 import com.sun.star.util.XModifiable;
58 
59 import java.util.logging.Level;
60 
61 @RunWith(FileProvider.class)
62 public class TestFormulaDocs {
63 
64     private String filename;
65 
66     @FileRepos
67     public static String repos = getDataPath("uno/sc/fvt");
68 
69     @Rule
70     public Logger log = Logger.getLogger(this);
71 
72     static UnoApp unoApp = new UnoApp();
73     XComponent scComponent = null;
74 
75     /**
76      * Clean class after testing
77      *
78      * @throws Exception
79      */
80     @AfterClass
afterClass()81     public static void afterClass() {
82         unoApp.close();
83     }
84 
85     @Before
setUp()86     public void setUp() throws Exception {
87         unoApp.close(); // moved here from tearDown because stopping app there causes a late screenshot
88         unoApp.start();
89     }
90 
TestFormulaDocs(String filename)91     public TestFormulaDocs(String filename) {
92         this.filename = filename;
93     }
94 
95     // FIXME: only needs a timeout for running tests against AOO41X due to fixes for i112383 and i117960 present in trunk
96     // haven't been backported to AOO41X yet and causes these tests to hang on an error dialog.
97     @Test(timeout = 15000)
testOneDoc()98     public void testOneDoc() throws Exception {
99         // open the spreadsheet document
100         String sample = Testspace.prepareData(filename);
101         // enable macros
102         PropertyValue prop = new PropertyValue();
103         prop.Name = "MacroExecutionMode";
104         prop.Value = MacroExecMode.ALWAYS_EXECUTE_NO_WARN;
105         XSpreadsheetDocument scDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(
106             XSpreadsheetDocument.class, unoApp.loadDocument(sample, prop));
107         XSpreadsheet xSheet = SCUtil.getCurrentSheet( scDoc);
108 
109         // find the "TestID" and "TestOK" markers
110         int nTestIdCol = -1;
111         int nTestOkCol = -1;
112         int nTestRowStart = -1;
113         for( int y = 0; y < 8; ++y) {
114             for( int x = 0; x < 26; ++x) {
115                 XCell xCell = xSheet.getCellByPosition( x, y);
116                 XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
117                 String name = xText.getString();
118                 if( name.equals( "TestID")) {
119                     assertTrue( "Multiple rows with TestID marker!", nTestIdCol == -1);
120                     assertTrue( nTestRowStart == -1);
121                     nTestIdCol = x;
122                     nTestRowStart = y + 1;
123                 } else if( name.equals( "TestOK")) {
124                     assertTrue( "Multiple rows with TestOK marker!", nTestOkCol == -1);
125                     assertTrue( "TestID and TestOK marker not in same row!", nTestRowStart == y + 1);
126                     nTestOkCol = x;
127                 }
128             }
129         }
130         assertTrue( "Column \"TestID\" not found!", nTestIdCol >= 0);
131         assertTrue( "Column \"TestOK\" not found!", nTestOkCol >= 0);
132 
133         int nTestRowEnd = nTestRowStart + 100; // TODO: get the last row from the sheet
134         int nTestCount = 0;
135         int nFailCount = 0;
136         for( int y = nTestRowStart; y < nTestRowEnd; ++y) {
137             // get the test id
138             XCell xCell = xSheet.getCellByPosition( nTestIdCol, y);
139             XText xText = (XText)UnoRuntime.queryInterface( XText.class, xCell);
140             String testId = xText.getString();
141             // ignore rows without test ids
142             if( testId.length() == 0)
143                 continue;
144             ++nTestCount;
145 
146             // get and check the test result
147             xCell = xSheet.getCellByPosition( nTestOkCol, y);
148             String testOk = ((XText)UnoRuntime.queryInterface( XText.class, xCell)).getString();
149             assertTrue( "Test result must be TRUE or FALSE", testOk.equals("TRUE") || testOk.equals("FALSE"));
150             boolean bOK = testOk.equals("TRUE");
151             // mark evaluated test results
152             SCUtil.setProperties( xCell, "CellBackColor", (Integer)(bOK ? 0x00FF00 : 0xFF0000));
153             // handle failed test cases
154             if( !bOK) {
155                 ++nFailCount;
156                 log.log( Level.SEVERE, "\ttest \""+testId+" failed");
157             }
158         }
159 
160         assertTrue( (nFailCount+" of "+nTestCount+" tests failed for " + filename), nFailCount==0);
161 
162         XModifiable modified = (XModifiable)UnoRuntime.queryInterface( XModifiable.class, scDoc);
163         modified.setModified( false);
164         SCUtil.closeFile( scDoc);
165     }
166 
167 }
168