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