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