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 com.sun.star.text.XTextRange;
25 import com.sun.star.beans.XPropertySet;
26 import com.sun.star.table.XCell;
27 import com.sun.star.table.XCellRange;
28 import com.sun.star.text.XTextCursor;
29 import com.sun.star.text.XTextTable;
30 import com.sun.star.text.XText;
31 import com.sun.star.text.XTextDocument;
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.uno.UnoRuntime;
34 import org.openoffice.test.tools.DocumentType;
35 import static org.junit.Assert.*;
36 
37 /**
38  * implements the {@link DocumentTest} interface on top of a spreadsheet document
39  * @author frank.schoenheit@oracle.com
40  */
41 public class WriterDocumentTest extends DocumentTestBase
42 {
WriterDocumentTest( final XMultiServiceFactory i_orb )43     public WriterDocumentTest( final XMultiServiceFactory i_orb ) throws com.sun.star.uno.Exception
44     {
45         super( i_orb, DocumentType.WRITER );
46     }
47 
getDocumentDescription()48     public String getDocumentDescription()
49     {
50         return "text document";
51     }
52 
initializeDocument()53     public void initializeDocument() throws com.sun.star.uno.Exception
54     {
55         // TODO?
56     }
57 
doSingleModification()58     public void doSingleModification() throws com.sun.star.uno.Exception
59     {
60         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
61         final XText docText = textDoc.getText();
62         docText.setString( s_blindText );
63     }
64 
verifyInitialDocumentState()65     public void verifyInitialDocumentState() throws com.sun.star.uno.Exception
66     {
67         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
68         final XText docText = textDoc.getText();
69         assertEquals( "document should be empty", "", docText.getString() );
70     }
71 
verifySingleModificationDocumentState()72     public void verifySingleModificationDocumentState() throws com.sun.star.uno.Exception
73     {
74         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
75         final XText docText = textDoc.getText();
76         assertEquals( "blind text not found", s_blindText, docText.getString() );
77     }
78 
doMultipleModifications()79     public int doMultipleModifications() throws com.sun.star.uno.Exception
80     {
81         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
82         final XText docText = textDoc.getText();
83 
84         int expectedUndoActions = 0;
85 
86         // create a cursor
87         final XTextCursor cursor = docText.createTextCursor();
88 
89         // create a table
90         final XTextTable textTable = UnoRuntime.queryInterface( XTextTable.class,
91             getDocument().createInstance( "com.sun.star.text.TextTable" ) );
92         textTable.initialize( 3, 3 );
93         final XPropertySet tableProps = UnoRuntime.queryInterface( XPropertySet.class, textTable );
94         tableProps.setPropertyValue( "BackColor", 0xCCFF44 );
95 
96         // insert the table into the doc
97         docText.insertTextContent( cursor, textTable, false );
98         ++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!
99         ++expectedUndoActions;
100 
101         // write some content into the center cell
102         final XCellRange cellRange = UnoRuntime.queryInterface( XCellRange.class, textTable );
103         final XCell centerCell = cellRange.getCellByPosition( 1, 1 );
104         final XTextRange cellText = UnoRuntime.queryInterface( XTextRange.class, centerCell );
105         cellText.setString( "Undo Manager API Test" );
106         ++expectedUndoActions;
107 
108         // give it another color
109         final XPropertySet cellProps = UnoRuntime.queryInterface( XPropertySet.class, centerCell );
110         cellProps.setPropertyValue( "BackColor", 0x44CCFF );
111         ++expectedUndoActions;
112 
113         return expectedUndoActions;
114     }
115 
116     private static final String s_blindText =
117         "Lorem ipsum dolor. Sit amet penatibus. A cum turpis. Aenean ac eu. " +
118         "Ligula est urna nulla vestibulum ullamcorper. Nec sit in amet tincidunt mus. " +
119         "Tellus sagittis mi. Suscipit cursus in vestibulum in eros ipsum felis cursus lectus " +
120         "nunc quis condimentum in risus nec wisi aenean luctus hendrerit magna habitasse commodo orci. " +
121         "Nisl etiam quis. Vestibulum justo eleifend aliquet luctus sed turpis volutpat ullamcorper " +
122         "aliquam penatibus sagittis pede tincidunt egestas. Nibh massa lectus. Sem mattis purus morbi " +
123         "scelerisque turpis donec urna phasellus. Quis at lacus. Viverra mauris mollis. " +
124         "Dolor tincidunt condimentum.";
125 }
126