summaryrefslogtreecommitdiff
path: root/sfx2/qa/complex/sfx2/undo/CalcDocumentTest.java
blob: 34825fdbada977cbf46c93db72ab4ffdd4ada42b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package complex.sfx2.undo;

import org.openoffice.test.tools.SpreadsheetDocument;
import com.sun.star.table.XCellRange;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.table.XCell;
import com.sun.star.uno.UnoRuntime;
import org.openoffice.test.tools.DocumentType;
import static org.junit.Assert.*;

/**
 * implements the {@link DocumentTest} interface on top of a spreadsheet document
 * @author frank.schoenheit@oracle.com
 */
public class CalcDocumentTest extends DocumentTestBase
{
    public CalcDocumentTest( final XMultiServiceFactory i_orb ) throws Exception
    {
        super( i_orb, DocumentType.CALC );
    }

    public String getDocumentDescription()
    {
        return "spreadsheet document";
    }

    public void initializeDocument() throws com.sun.star.uno.Exception
    {
        final XCell cellA1 = getCellA1();
        cellA1.setValue( INIT_VALUE );
        assertEquals( "initializing the cell value didn't work", cellA1.getValue(), INIT_VALUE, 0 );

        XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
            ((SpreadsheetDocument)m_document).getSheet(0) );

        for ( int i=0; i<12; ++i )
        {
            XCell cell = range.getCellByPosition( 1, i );
            cell.setFormula( "" );
        }
    }

    public void doSingleModification() throws com.sun.star.uno.Exception
    {
        final XCell cellA1 = getCellA1();
        assertEquals( "initial cell value not as expected", INIT_VALUE, cellA1.getValue(), 0 );
        cellA1.setValue( MODIFIED_VALUE );
        assertEquals( "modified cell value not as expected", MODIFIED_VALUE, cellA1.getValue(), 0 );
    }

    public void verifyInitialDocumentState() throws com.sun.star.uno.Exception
    {
        final XCell cellA1 = getCellA1();
        assertEquals( "cell A1 doesn't have its initial value", INIT_VALUE, cellA1.getValue(), 0 );

        XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
            ((SpreadsheetDocument)m_document).getSheet(0) );
        for ( int i=0; i<12; ++i )
        {
            final XCell cell = range.getCellByPosition( 1, i );
            assertEquals( "Cell B" + (i+1) + " not having its initial value (an empty string)", "", cell.getFormula() );
        }
    }

    public void verifySingleModificationDocumentState() throws com.sun.star.uno.Exception
    {
        final XCell cellA1 = getCellA1();
        assertEquals( "cell A1 doesn't have the value which we gave it", MODIFIED_VALUE, cellA1.getValue(), 0 );
    }

    public int doMultipleModifications() throws com.sun.star.uno.Exception
    {
        XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
            ((SpreadsheetDocument)m_document).getSheet(0) );

        final String[] months = new String[] {
            "January", "February", "March", "April", "May", "June", "July", "August",
            "September", "October", "November", "December" };
        for ( int i=0; i<12; ++i )
        {
            final XCell cell = range.getCellByPosition( 1, i );
            cell.setFormula( months[i] );
        }
        return 12;
    }

    private XCell getCellA1() throws com.sun.star.uno.Exception
    {
        XCellRange range = UnoRuntime.queryInterface( XCellRange.class,
            ((SpreadsheetDocument)m_document).getSheet(0) );
        return range.getCellByPosition( 0, 0 );
    }

    private static final double INIT_VALUE = 100.0;
    private static final double MODIFIED_VALUE = 200.0;
}