1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package mod._sw;
28 
29 import java.io.PrintWriter;
30 import java.util.Vector;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.Property;
39 import com.sun.star.beans.PropertyAttribute;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.text.ControlCharacter;
43 import com.sun.star.text.XParagraphCursor;
44 import com.sun.star.text.XSimpleText;
45 import com.sun.star.text.XTextCursor;
46 import com.sun.star.text.XTextDocument;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 
51 /**
52  * Test for object which is represented by service
53  * <code>com.sun.star.text.TextCursor</code>. <p>
54  * Object implements the following interfaces :
55  * <ul>
56  *  <li> <code>com::sun::star::text::XTextCursor</code></li>
57  *  <li> <code>com::sun::star::text::XWordCursor</code></li>
58  *  <li> <code>com::sun::star::style::CharacterPropertiesComplex</code></li>
59  *  <li> <code>com::sun::star::text::XTextRange</code></li>
60  *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
61  *  <li> <code>com::sun::star::container::XContentEnumerationAccess</code></li>
62  *  <li> <code>com::sun::star::beans::XPropertyState</code></li>
63  *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
64  *  <li> <code>com::sun::star::text::XSentenceCursor</code></li>
65  *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
66  *  <li> <code>com::sun::star::text::XParagraphCursor</code></li>
67  *  <li> <code>com::sun::star::document::XDocumentInsertable</code></li>
68  *  <li> <code>com::sun::star::util::XSortable</code></li>
69  *  <li> <code>com::sun::star::style::CharacterPropertiesAsian</code></li>
70  * </ul> <p>
71  * This object test <b> is NOT </b> designed to be run in several
72  * threads concurently.
73  * @see com.sun.star.text.XTextCursor
74  * @see com.sun.star.text.XWordCursor
75  * @see com.sun.star.style.CharacterPropertiesComplex
76  * @see com.sun.star.text.XTextRange
77  * @see com.sun.star.beans.XPropertySet
78  * @see com.sun.star.container.XContentEnumerationAccess
79  * @see com.sun.star.beans.XPropertyState
80  * @see com.sun.star.style.CharacterProperties
81  * @see com.sun.star.text.XSentenceCursor
82  * @see com.sun.star.style.ParagraphProperties
83  * @see com.sun.star.text.XParagraphCursor
84  * @see com.sun.star.document.XDocumentInsertable
85  * @see com.sun.star.util.XSortable
86  * @see com.sun.star.style.CharacterPropertiesAsian
87  * @see ifc.text._XTextCursor
88  * @see ifc.text._XWordCursor
89  * @see ifc.style._CharacterPropertiesComplex
90  * @see ifc.text._XTextRange
91  * @see ifc.beans._XPropertySet
92  * @see ifc.container._XContentEnumerationAccess
93  * @see ifc.beans._XPropertyState
94  * @see ifc.style._CharacterProperties
95  * @see ifc.text._XSentenceCursor
96  * @see ifc.style._ParagraphProperties
97  * @see ifc.text._XParagraphCursor
98  * @see ifc.document._XDocumentInsertable
99  * @see ifc.util._XSortable
100  * @see ifc.style._CharacterPropertiesAsian
101  */
102 public class SwXTextCursor extends TestCase {
103     XTextDocument xTextDoc;
104 
105     /**
106     * Creates text document.
107     */
108     protected void initialize(TestParameters tParam, PrintWriter log) {
109         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF());
110 
111         try {
112             log.println("creating a textdocument");
113             xTextDoc = SOF.createTextDoc(null);
114         } catch (com.sun.star.uno.Exception e) {
115             e.printStackTrace(log);
116             throw new StatusException("Couldn't create document", e);
117         }
118     }
119 
120     /**
121     * Disposes text document.
122     */
123     protected void cleanup(TestParameters tParam, PrintWriter log) {
124         log.println("    disposing xTextDoc ");
125         util.DesktopTools.closeDoc(xTextDoc);
126     }
127 
128     /**
129     * Creating a Testenvironment for the interfaces to be tested. After major
130     * text of text document is obtained, text cursor is created and several
131     * paragraphs within the text are inserted to a text document. Finally,
132     * text cursor is returned as a test component.
133     *     Object relations created :
134     * <ul>
135     *  <li> <code>'XTEXT'</code> for
136     *      {@link ifc.text._XTextRange} : major text of text document</li>
137     * </ul>
138     */
139     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
140                                                                  PrintWriter log) {
141         XInterface oObj = null;
142 
143         log.println("creating a test environment");
144 
145 
146         // get the bodytext of textdocument here
147         log.println("getting the TextCursor");
148 
149         final XSimpleText aText = xTextDoc.getText();
150         final XTextCursor textCursor = aText.createTextCursor();
151         oObj = textCursor;
152 
153         log.println("inserting some text");
154 
155         try {
156             for (int i = 0; i < 3; i++) {
157                 aText.insertString(textCursor, "" + (3 - i), false);
158 
159                 for (int j = 0; j < 5; j++) {
160                     aText.insertString(textCursor, "XTextCursor,XTextCursor",
161                                        false);
162                     aText.insertString(textCursor, "The quick brown fox ",
163                                        false);
164                     aText.insertString(textCursor, "jumps over the lazy dog ",
165                                        false);
166                 }
167 
168                 aText.insertControlCharacter(textCursor,
169                                              ControlCharacter.PARAGRAPH_BREAK,
170                                              false);
171                 aText.insertControlCharacter(textCursor,
172                                              ControlCharacter.LINE_BREAK,
173                                              false);
174             }
175         } catch (com.sun.star.lang.IllegalArgumentException e) {
176             log.println("Error, insert text to text document.");
177             e.printStackTrace(log);
178         }
179 
180         log.println("creating a new environment for SwXTextCursor object");
181 
182         TestEnvironment tEnv = new TestEnvironment(oObj);
183 
184         tEnv.addObjRelation("XTEXT", xTextDoc.getText());
185 
186         XPropertySet xCursorProp = (XPropertySet) UnoRuntime.queryInterface(
187                                            XPropertySet.class, oObj);
188         tEnv.addObjRelation("PropertyNames", getPropertyNames(xCursorProp));
189 
190         //Adding relation for util.XSortable
191         final XParagraphCursor paragrCursor = (XParagraphCursor) UnoRuntime.queryInterface(
192                                                       XParagraphCursor.class,
193                                                       oObj);
194         final PrintWriter finalLog = log;
195 
196         tEnv.addObjRelation("SORTCHECKER",
197                             new ifc.util._XSortable.XSortChecker() {
198             PrintWriter out = finalLog;
199 
200             public void setPrintWriter(PrintWriter log) {
201                 out = log;
202             }
203 
204             public void prepareToSort() {
205                 textCursor.gotoEnd(false);
206 
207                 try {
208                     aText.insertControlCharacter(textCursor,
209                                                  ControlCharacter.PARAGRAPH_BREAK,
210                                                  false);
211                     aText.insertString(textCursor, "4", false);
212                     aText.insertControlCharacter(textCursor,
213                                                  ControlCharacter.PARAGRAPH_BREAK,
214                                                  false);
215                     aText.insertString(textCursor, "b", false);
216                     aText.insertControlCharacter(textCursor,
217                                                  ControlCharacter.PARAGRAPH_BREAK,
218                                                  false);
219                     aText.insertString(textCursor, "3", false);
220                     aText.insertControlCharacter(textCursor,
221                                                  ControlCharacter.PARAGRAPH_BREAK,
222                                                  false);
223                     aText.insertString(textCursor, "a", false);
224                     aText.insertControlCharacter(textCursor,
225                                                  ControlCharacter.PARAGRAPH_BREAK,
226                                                  false);
227                     aText.insertString(textCursor, "23", false);
228                     aText.insertControlCharacter(textCursor,
229                                                  ControlCharacter.PARAGRAPH_BREAK,
230                                                  false);
231                     aText.insertString(textCursor, "ab", false);
232                     aText.insertControlCharacter(textCursor,
233                                                  ControlCharacter.PARAGRAPH_BREAK,
234                                                  false);
235                 } catch (com.sun.star.lang.IllegalArgumentException e) {
236                     out.println("Unexpected exception:" + e);
237                 }
238 
239                 out.println(
240                         "Preparing cursor to sorting. Text before sorting:");
241                 paragrCursor.gotoEnd(true);
242 
243                 for (int i = 0; i < 6; i++) {
244                     paragrCursor.gotoPreviousParagraph(true);
245                 }
246 
247                 out.println(textCursor.getString());
248             }
249 
250             public boolean checkSort(boolean isSortNumbering,
251                                      boolean isSortAscending) {
252                 out.println("Sort checking...");
253 
254                 String ls = System.getProperty("line.separator");
255 
256                 String text = paragrCursor.getString();
257                 out.println("Text after sorting:\n" + text);
258 
259                 boolean res = false;
260 
261                 if (isSortNumbering) {
262                     if (isSortAscending) {
263                         res = text.endsWith(ls+"3"+ls+"4"+ls+"23");
264 
265                         if (!res) {
266                             out.println("Text must ends by:\n" + "\r\n3\r\n4\r\n23\r\n");
267                         }
268                     } else {
269                         res = text.startsWith("23"+ls+"4"+ls+"3"+ls);
270 
271                         if (!res) {
272                             out.println("Text must starts with:\n" + "23\r\n4\r\n3\r\n");
273                         }
274                     }
275                 } else {
276                     if (isSortAscending) {
277                         res = text.equals(ls+"23"+ls+"3"+ls+"4"+ls+"a"+ls+"ab"+ls+"b");
278 
279                         if (!res) {
280                             out.println("Text must be equal to:\n" + "\r\n23\r\n3\r\n4\r\na\r\nab\r\nb\r\n");
281                         }
282                     } else {
283                         res = text.endsWith("b"+ls+"ab"+ls+"a"+ls+"4"+ls+"3"+ls+"23"+ls);
284 
285                         if (!res) {
286                             out.println("Text must be equal to:\n" + "b\r\nab\r\na\r\n4\r\n3\r\n23\r\n");
287                         }
288                     }
289                 }
290 
291                 if (res) {
292                     out.println("Sorted correctly");
293                 }
294 
295                 return res;
296             }
297         });
298 
299         return tEnv;
300     } // finish method getTestEnvironment
301 
302     public String[] getPropertyNames(XPropertySet props) {
303         Property[] the_props = props.getPropertySetInfo().getProperties();
304         Vector names = new Vector();
305 
306         for (int i = 0; i < the_props.length; i++) {
307             boolean isWritable = ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
308 
309             if (isWritable) {
310                 names.add(the_props[i].Name);
311             }
312         }
313 
314         return (String[]) names.toArray(new String[names.size()]);
315     }
316 } // finish class SwXTextCursor
317