summaryrefslogtreecommitdiffstats
path: root/sfx2/qa/complex/sfx2/undo/WriterDocumentTest.java
blob: 624c2d7d247f519ac87090b245215e6bad489bb6 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package complex.sfx2.undo;

import com.sun.star.text.XTextRange;
import com.sun.star.beans.XPropertySet;
import com.sun.star.table.XCell;
import com.sun.star.table.XCellRange;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XText;
import com.sun.star.text.XTextDocument;
import com.sun.star.lang.XMultiServiceFactory;
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
 */
public class WriterDocumentTest extends DocumentTestBase
{
    public WriterDocumentTest( final XMultiServiceFactory i_orb ) throws com.sun.star.uno.Exception
    {
        super( i_orb, DocumentType.WRITER );
    }

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

    public void initializeDocument() throws com.sun.star.uno.Exception
    {
        // TODO?
    }

    public void doSingleModification() throws com.sun.star.uno.Exception
    {
        final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
        final XText docText = textDoc.getText();
        docText.setString( s_blindText );
    }

    public void verifyInitialDocumentState() throws com.sun.star.uno.Exception
    {
        final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
        final XText docText = textDoc.getText();
        assertEquals( "document should be empty", "", docText.getString() );
    }

    public void verifySingleModificationDocumentState() throws com.sun.star.uno.Exception
    {
        final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
        final XText docText = textDoc.getText();
        assertEquals( "blind text not found", s_blindText, docText.getString() );
    }

    public int doMultipleModifications() throws com.sun.star.uno.Exception
    {
        final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
        final XText docText = textDoc.getText();

        int expectedUndoActions = 0;

        // create a cursor
        final XTextCursor cursor = docText.createTextCursor();

        // create a table
        final XTextTable textTable = UnoRuntime.queryInterface( XTextTable.class,
            getDocument().createInstance( "com.sun.star.text.TextTable" ) );
        textTable.initialize( 3, 3 );
        final XPropertySet tableProps = UnoRuntime.queryInterface( XPropertySet.class, textTable );
        tableProps.setPropertyValue( "BackColor", 0xCCFF44 );

        // insert the table into the doc
        docText.insertTextContent( cursor, textTable, false );
        ++expectedUndoActions; //FIXME this will create 2 actions! currently the event is sent for every individual action; should it be sent for top-level actions only? how many internal actions are created is an implementation detail!
        ++expectedUndoActions;

        // write some content into the center cell
        final XCellRange cellRange = UnoRuntime.queryInterface( XCellRange.class, textTable );
        final XCell centerCell = cellRange.getCellByPosition( 1, 1 );
        final XTextRange cellText = UnoRuntime.queryInterface( XTextRange.class, centerCell );
        cellText.setString( "Undo Manager API Test" );
        ++expectedUndoActions;

        // give it another color
        final XPropertySet cellProps = UnoRuntime.queryInterface( XPropertySet.class, centerCell );
        cellProps.setPropertyValue( "BackColor", 0x44CCFF );
        ++expectedUndoActions;

        return expectedUndoActions;
    }

    private static final String s_blindText =
        "Lorem ipsum dolor. Sit amet penatibus. A cum turpis. Aenean ac eu. " +
        "Ligula est urna nulla vestibulum ullamcorper. Nec sit in amet tincidunt mus. " +
        "Tellus sagittis mi. Suscipit cursus in vestibulum in eros ipsum felis cursus lectus " +
        "nunc quis condimentum in risus nec wisi aenean luctus hendrerit magna habitasse commodo orci. " +
        "Nisl etiam quis. Vestibulum justo eleifend aliquet luctus sed turpis volutpat ullamcorper " +
        "aliquam penatibus sagittis pede tincidunt egestas. Nibh massa lectus. Sem mattis purus morbi " +
        "scelerisque turpis donec urna phasellus. Quis at lacus. Viverra mauris mollis. " +
        "Dolor tincidunt condimentum.";
}