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 complex.sfx2.undo;
23 
24 import org.openoffice.test.tools.SpreadsheetDocument;
25 import com.sun.star.table.XCellRange;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.table.XCell;
28 import com.sun.star.uno.UnoRuntime;
29 import org.openoffice.test.tools.DocumentType;
30 import static org.junit.Assert.*;
31 
32 /**
33  * implements the {@link DocumentTest} interface on top of a spreadsheet document
34  * @author frank.schoenheit@oracle.com
35  */
36 public class CalcDocumentTest extends DocumentTestBase
37 {
CalcDocumentTest( final XMultiServiceFactory i_orb )38     public CalcDocumentTest( final XMultiServiceFactory i_orb ) throws Exception
39     {
40         super( i_orb, DocumentType.CALC );
41     }
42 
getDocumentDescription()43     public String getDocumentDescription()
44     {
45         return "spreadsheet document";
46     }
47 
initializeDocument()48     public void initializeDocument() throws com.sun.star.uno.Exception
49     {
50         final XCell cellA1 = getCellA1();
51         cellA1.setValue( INIT_VALUE );
52         assertEquals( "initializing the cell value didn't work", cellA1.getValue(), INIT_VALUE, 0 );
53 
54         XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
55             ((SpreadsheetDocument)m_document).getSheet(0) );
56 
57         for ( int i=0; i<12; ++i )
58         {
59             XCell cell = range.getCellByPosition( 1, i );
60             cell.setFormula( "" );
61         }
62     }
63 
doSingleModification()64     public void doSingleModification() throws com.sun.star.uno.Exception
65     {
66         final XCell cellA1 = getCellA1();
67         assertEquals( "initial cell value not as expected", INIT_VALUE, cellA1.getValue(), 0 );
68         cellA1.setValue( MODIFIED_VALUE );
69         assertEquals( "modified cell value not as expected", MODIFIED_VALUE, cellA1.getValue(), 0 );
70     }
71 
verifyInitialDocumentState()72     public void verifyInitialDocumentState() throws com.sun.star.uno.Exception
73     {
74         final XCell cellA1 = getCellA1();
75         assertEquals( "cell A1 doesn't have its initial value", INIT_VALUE, cellA1.getValue(), 0 );
76 
77         XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
78             ((SpreadsheetDocument)m_document).getSheet(0) );
79         for ( int i=0; i<12; ++i )
80         {
81             final XCell cell = range.getCellByPosition( 1, i );
82             assertEquals( "Cell B" + (i+1) + " not having its initial value (an empty string)", "", cell.getFormula() );
83         }
84     }
85 
verifySingleModificationDocumentState()86     public void verifySingleModificationDocumentState() throws com.sun.star.uno.Exception
87     {
88         final XCell cellA1 = getCellA1();
89         assertEquals( "cell A1 doesn't have the value which we gave it", MODIFIED_VALUE, cellA1.getValue(), 0 );
90     }
91 
doMultipleModifications()92     public int doMultipleModifications() throws com.sun.star.uno.Exception
93     {
94         XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
95             ((SpreadsheetDocument)m_document).getSheet(0) );
96 
97         final String[] months = new String[] {
98             "January", "February", "March", "April", "May", "June", "July", "August",
99             "September", "October", "November", "December" };
100         for ( int i=0; i<12; ++i )
101         {
102             final XCell cell = range.getCellByPosition( 1, i );
103             cell.setFormula( months[i] );
104         }
105         return 12;
106     }
107 
getCellA1()108     private XCell getCellA1() throws com.sun.star.uno.Exception
109     {
110         XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
111             ((SpreadsheetDocument)m_document).getSheet(0) );
112         return range.getCellByPosition( 0, 0 );
113     }
114 
115     private static final double INIT_VALUE = 100.0;
116     private static final double MODIFIED_VALUE = 200.0;
117 }
118