1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package complex.sfx2.undo;
20 
21 import com.sun.star.text.XTextRange;
22 import com.sun.star.beans.XPropertySet;
23 import com.sun.star.table.XCell;
24 import com.sun.star.table.XCellRange;
25 import com.sun.star.text.XTextCursor;
26 import com.sun.star.text.XTextTable;
27 import com.sun.star.text.XText;
28 import com.sun.star.text.XTextDocument;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.uno.UnoRuntime;
31 import org.openoffice.test.tools.DocumentType;
32 import static org.junit.Assert.*;
33 
34 /**
35  * implements the {@link DocumentTest} interface on top of a spreadsheet document
36  */
37 public class WriterDocumentTest extends DocumentTestBase
38 {
WriterDocumentTest( final XMultiServiceFactory i_orb )39     public WriterDocumentTest( final XMultiServiceFactory i_orb ) throws com.sun.star.uno.Exception
40     {
41         super( i_orb, DocumentType.WRITER );
42     }
43 
getDocumentDescription()44     public String getDocumentDescription()
45     {
46         return "text document";
47     }
48 
initializeDocument()49     public void initializeDocument() throws com.sun.star.uno.Exception
50     {
51         // TODO?
52     }
53 
doSingleModification()54     public void doSingleModification() throws com.sun.star.uno.Exception
55     {
56         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
57         final XText docText = textDoc.getText();
58         docText.setString( s_blindText );
59     }
60 
verifyInitialDocumentState()61     public void verifyInitialDocumentState() throws com.sun.star.uno.Exception
62     {
63         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
64         final XText docText = textDoc.getText();
65         assertEquals( "document should be empty", "", docText.getString() );
66     }
67 
verifySingleModificationDocumentState()68     public void verifySingleModificationDocumentState() throws com.sun.star.uno.Exception
69     {
70         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
71         final XText docText = textDoc.getText();
72         assertEquals( "blind text not found", s_blindText, docText.getString() );
73     }
74 
doMultipleModifications()75     public int doMultipleModifications() throws com.sun.star.uno.Exception
76     {
77         final XTextDocument textDoc = UnoRuntime.queryInterface( XTextDocument.class, getDocument().getDocument() );
78         final XText docText = textDoc.getText();
79 
80         int expectedUndoActions = 0;
81 
82         // create a cursor
83         final XTextCursor cursor = docText.createTextCursor();
84 
85         // create a table
86         final XTextTable textTable = UnoRuntime.queryInterface( XTextTable.class,
87             getDocument().createInstance( "com.sun.star.text.TextTable" ) );
88         textTable.initialize( 3, 3 );
89         final XPropertySet tableProps = UnoRuntime.queryInterface( XPropertySet.class, textTable );
90         tableProps.setPropertyValue( "BackColor", 0xCCFF44 );
91 
92         // insert the table into the doc
93         docText.insertTextContent( cursor, textTable, false );
94         ++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!
95         ++expectedUndoActions;
96 
97         // write some content into the center cell
98         final XCellRange cellRange = UnoRuntime.queryInterface( XCellRange.class, textTable );
99         final XCell centerCell = cellRange.getCellByPosition( 1, 1 );
100         final XTextRange cellText = UnoRuntime.queryInterface( XTextRange.class, centerCell );
101         cellText.setString( "Undo Manager API Test" );
102         ++expectedUndoActions;
103 
104         // give it another color
105         final XPropertySet cellProps = UnoRuntime.queryInterface( XPropertySet.class, centerCell );
106         cellProps.setPropertyValue( "BackColor", 0x44CCFF );
107         ++expectedUndoActions;
108 
109         return expectedUndoActions;
110     }
111 
112     private static final String s_blindText =
113         "Lorem ipsum dolor. Sit amet penatibus. A cum turpis. Aenean ac eu. " +
114         "Ligula est urna nulla vestibulum ullamcorper. Nec sit in amet tincidunt mus. " +
115         "Tellus sagittis mi. Suscipit cursus in vestibulum in eros ipsum felis cursus lectus " +
116         "nunc quis condimentum in risus nec wisi aenean luctus hendrerit magna habitasse commodo orci. " +
117         "Nisl etiam quis. Vestibulum justo eleifend aliquet luctus sed turpis volutpat ullamcorper " +
118         "aliquam penatibus sagittis pede tincidunt egestas. Nibh massa lectus. Sem mattis purus morbi " +
119         "scelerisque turpis donec urna phasellus. Quis at lacus. Viverra mauris mollis. " +
120         "Dolor tincidunt condimentum.";
121 }
122