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 .
20 import java
.io
.PrintWriter
;
21 import java
.util
.ArrayList
;
23 import lib
.StatusException
;
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
.text
.ControlCharacter
;
33 import com
.sun
.star
.text
.XParagraphCursor
;
34 import com
.sun
.star
.text
.XSimpleText
;
35 import com
.sun
.star
.text
.XTextCursor
;
36 import com
.sun
.star
.text
.XTextDocument
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.uno
.XInterface
;
42 * Test for object which is represented by service
43 * <code>com.sun.star.text.TextCursor</code>. <p>
44 * Object implements the following interfaces :
46 * <li> <code>com::sun::star::text::XTextCursor</code></li>
47 * <li> <code>com::sun::star::text::XWordCursor</code></li>
48 * <li> <code>com::sun::star::style::CharacterPropertiesComplex</code></li>
49 * <li> <code>com::sun::star::text::XTextRange</code></li>
50 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
51 * <li> <code>com::sun::star::container::XContentEnumerationAccess</code></li>
52 * <li> <code>com::sun::star::beans::XPropertyState</code></li>
53 * <li> <code>com::sun::star::style::CharacterProperties</code></li>
54 * <li> <code>com::sun::star::text::XSentenceCursor</code></li>
55 * <li> <code>com::sun::star::style::ParagraphProperties</code></li>
56 * <li> <code>com::sun::star::text::XParagraphCursor</code></li>
57 * <li> <code>com::sun::star::document::XDocumentInsertable</code></li>
58 * <li> <code>com::sun::star::util::XSortable</code></li>
59 * <li> <code>com::sun::star::style::CharacterPropertiesAsian</code></li>
61 * This object test <b> is NOT </b> designed to be run in several
62 * threads concurently.
63 * @see com.sun.star.text.XTextCursor
64 * @see com.sun.star.text.XWordCursor
65 * @see com.sun.star.style.CharacterPropertiesComplex
66 * @see com.sun.star.text.XTextRange
67 * @see com.sun.star.beans.XPropertySet
68 * @see com.sun.star.container.XContentEnumerationAccess
69 * @see com.sun.star.beans.XPropertyState
70 * @see com.sun.star.style.CharacterProperties
71 * @see com.sun.star.text.XSentenceCursor
72 * @see com.sun.star.style.ParagraphProperties
73 * @see com.sun.star.text.XParagraphCursor
74 * @see com.sun.star.document.XDocumentInsertable
75 * @see com.sun.star.util.XSortable
76 * @see com.sun.star.style.CharacterPropertiesAsian
77 * @see ifc.text._XTextCursor
78 * @see ifc.text._XWordCursor
79 * @see ifc.style._CharacterPropertiesComplex
80 * @see ifc.text._XTextRange
81 * @see ifc.beans._XPropertySet
82 * @see ifc.container._XContentEnumerationAccess
83 * @see ifc.beans._XPropertyState
84 * @see ifc.style._CharacterProperties
85 * @see ifc.text._XSentenceCursor
86 * @see ifc.style._ParagraphProperties
87 * @see ifc.text._XParagraphCursor
88 * @see ifc.document._XDocumentInsertable
89 * @see ifc.util._XSortable
90 * @see ifc.style._CharacterPropertiesAsian
92 public class SwXTextCursor
extends TestCase
{
93 XTextDocument xTextDoc
;
96 * Creates text document.
99 protected void initialize(TestParameters tParam
, PrintWriter log
) {
100 SOfficeFactory SOF
= SOfficeFactory
.getFactory( tParam
.getMSF());
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.
115 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
116 log
.println(" disposing xTextDoc ");
117 util
.DesktopTools
.closeDoc(xTextDoc
);
121 * Creating a Testenvironment for the interfaces to be tested. After major
122 * text of text document is obtained, text cursor is created and several
123 * paragraphs within the text are inserted to a text document. Finally,
124 * text cursor is returned as a test component.
125 * Object relations created :
127 * <li> <code>'XTEXT'</code> for
128 * {@link ifc.text._XTextRange} : major text of text document</li>
132 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
,
134 XInterface oObj
= null;
136 log
.println("creating a test environment");
139 // get the bodytext of textdocument here
140 log
.println("getting the TextCursor");
142 final XSimpleText aText
= xTextDoc
.getText();
143 final XTextCursor textCursor
= aText
.createTextCursor();
146 log
.println("inserting some text");
149 for (int i
= 0; i
< 3; i
++) {
150 aText
.insertString(textCursor
, "" + (3 - i
), false);
152 for (int j
= 0; j
< 5; j
++) {
153 aText
.insertString(textCursor
, "XTextCursor,XTextCursor",
155 aText
.insertString(textCursor
, "The quick brown fox ",
157 aText
.insertString(textCursor
, "jumps over the lazy dog ",
161 aText
.insertControlCharacter(textCursor
,
162 ControlCharacter
.PARAGRAPH_BREAK
,
164 aText
.insertControlCharacter(textCursor
,
165 ControlCharacter
.LINE_BREAK
,
168 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
169 log
.println("Error, insert text to text document.");
170 e
.printStackTrace(log
);
173 log
.println("creating a new environment for SwXTextCursor object");
175 TestEnvironment tEnv
= new TestEnvironment(oObj
);
177 tEnv
.addObjRelation("XTEXT", xTextDoc
.getText());
179 XPropertySet xCursorProp
= UnoRuntime
.queryInterface(
180 XPropertySet
.class, oObj
);
181 tEnv
.addObjRelation("PropertyNames", getPropertyNames(xCursorProp
));
183 //Adding relation for util.XSortable
184 final XParagraphCursor paragrCursor
= UnoRuntime
.queryInterface(
185 XParagraphCursor
.class,
187 final PrintWriter finalLog
= log
;
189 tEnv
.addObjRelation("SORTCHECKER",
190 new ifc
.util
._XSortable
.XSortChecker() {
191 PrintWriter out
= finalLog
;
193 public void setPrintWriter(PrintWriter log
) {
197 public void prepareToSort() {
198 textCursor
.gotoEnd(false);
201 aText
.insertControlCharacter(textCursor
,
202 ControlCharacter
.PARAGRAPH_BREAK
,
204 aText
.insertString(textCursor
, "4", false);
205 aText
.insertControlCharacter(textCursor
,
206 ControlCharacter
.PARAGRAPH_BREAK
,
208 aText
.insertString(textCursor
, "b", false);
209 aText
.insertControlCharacter(textCursor
,
210 ControlCharacter
.PARAGRAPH_BREAK
,
212 aText
.insertString(textCursor
, "3", false);
213 aText
.insertControlCharacter(textCursor
,
214 ControlCharacter
.PARAGRAPH_BREAK
,
216 aText
.insertString(textCursor
, "a", false);
217 aText
.insertControlCharacter(textCursor
,
218 ControlCharacter
.PARAGRAPH_BREAK
,
220 aText
.insertString(textCursor
, "23", false);
221 aText
.insertControlCharacter(textCursor
,
222 ControlCharacter
.PARAGRAPH_BREAK
,
224 aText
.insertString(textCursor
, "ab", false);
225 aText
.insertControlCharacter(textCursor
,
226 ControlCharacter
.PARAGRAPH_BREAK
,
228 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
229 out
.println("Unexpected exception:" + e
);
233 "Preparing cursor to sorting. Text before sorting:");
234 paragrCursor
.gotoEnd(true);
236 for (int i
= 0; i
< 6; i
++) {
237 paragrCursor
.gotoPreviousParagraph(true);
240 out
.println(textCursor
.getString());
243 public boolean checkSort(boolean isSortNumbering
,
244 boolean isSortAscending
) {
245 out
.println("Sort checking...");
247 String ls
= System
.getProperty("line.separator");
249 String text
= paragrCursor
.getString();
250 out
.println("Text after sorting:\n" + text
);
254 if (isSortNumbering
) {
255 if (isSortAscending
) {
256 res
= text
.endsWith(ls
+"3"+ls
+"4"+ls
+"23");
259 out
.println("Text must ends by:\n" + "\r\n3\r\n4\r\n23\r\n");
262 res
= text
.startsWith("23"+ls
+"4"+ls
+"3"+ls
);
265 out
.println("Text must starts with:\n" + "23\r\n4\r\n3\r\n");
269 if (isSortAscending
) {
270 res
= text
.equals(ls
+"23"+ls
+"3"+ls
+"4"+ls
+"a"+ls
+"ab"+ls
+"b");
273 out
.println("Text must be equal to:\n" + "\r\n23\r\n3\r\n4\r\na\r\nab\r\nb\r\n");
276 res
= text
.endsWith("b"+ls
+"ab"+ls
+"a"+ls
+"4"+ls
+"3"+ls
+"23"+ls
);
279 out
.println("Text must be equal to:\n" + "b\r\nab\r\na\r\n4\r\n3\r\n23\r\n");
285 out
.println("Sorted correctly");
293 } // finish method getTestEnvironment
295 public String
[] getPropertyNames(XPropertySet props
) {
296 Property
[] the_props
= props
.getPropertySetInfo().getProperties();
297 ArrayList
<String
> names
= new ArrayList
<String
>();
299 for (int i
= 0; i
< the_props
.length
; i
++) {
300 boolean isWritable
= ((the_props
[i
].Attributes
& PropertyAttribute
.READONLY
) == 0);
303 names
.add(the_props
[i
].Name
);
307 return names
.toArray(new String
[names
.size()]);
309 } // finish class SwXTextCursor