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