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