tdf#130857 qt weld: Introduce QtInstanceScrolledWindow
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXTextCursor.java
blob5b641b1e72af3beb795031c18f48c8354a1e45a3
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;
22 import java.util.Arrays;
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.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;
41 /**
42 * Test for object which is represented by service
43 * <code>com.sun.star.text.TextCursor</code>. <p>
44 * Object implements the following interfaces :
45 * <ul>
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>
60 * </ul> <p>
61 * This object test <b> is NOT </b> designed to be run in several
62 * threads concurrently.
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;
95 /**
96 * Creates text document.
98 @Override
99 protected void initialize(TestParameters tParam, PrintWriter log) throws Exception {
100 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
101 log.println("creating a textdocument");
102 xTextDoc = SOF.createTextDoc(null);
106 * Disposes text document.
108 @Override
109 protected void cleanup(TestParameters tParam, PrintWriter log) {
110 log.println(" disposing xTextDoc ");
111 util.DesktopTools.closeDoc(xTextDoc);
115 * Creating a TestEnvironment for the interfaces to be tested. After major
116 * text of text document is obtained, text cursor is created and several
117 * paragraphs within the text are inserted to a text document. Finally,
118 * text cursor is returned as a test component.
119 * Object relations created :
120 * <ul>
121 * <li> <code>'XTEXT'</code> for
122 * {@link ifc.text._XTextRange} : major text of text document</li>
123 * </ul>
125 @Override
126 protected TestEnvironment createTestEnvironment(TestParameters Param,
127 PrintWriter log) {
128 XInterface oObj = null;
130 log.println("creating a test environment");
133 // get the bodytext of textdocument here
134 log.println("getting the TextCursor");
136 final XSimpleText aText = xTextDoc.getText();
137 final XTextCursor textCursor = aText.createTextCursor();
138 oObj = textCursor;
140 log.println("inserting some text");
142 try {
143 for (int i = 0; i < 3; i++) {
144 aText.insertString(textCursor, "" + (3 - i), false);
146 for (int j = 0; j < 5; j++) {
147 aText.insertString(textCursor, "XTextCursor,XTextCursor",
148 false);
149 aText.insertString(textCursor, "The quick brown fox ",
150 false);
151 aText.insertString(textCursor, "jumps over the lazy dog ",
152 false);
155 aText.insertControlCharacter(textCursor,
156 ControlCharacter.PARAGRAPH_BREAK,
157 false);
158 aText.insertControlCharacter(textCursor,
159 ControlCharacter.LINE_BREAK,
160 false);
162 } catch (com.sun.star.lang.IllegalArgumentException e) {
163 log.println("Error, insert text to text document.");
164 e.printStackTrace(log);
167 log.println("creating a new environment for SwXTextCursor object");
169 TestEnvironment tEnv = new TestEnvironment(oObj);
171 tEnv.addObjRelation("XTEXT", xTextDoc.getText());
173 XPropertySet xCursorProp = UnoRuntime.queryInterface(
174 XPropertySet.class, oObj);
176 String[] skipNames = { "ParaBackGraphicURL" };
177 String[] propertyNames = getPropertyNames(xCursorProp, skipNames);
179 tEnv.addObjRelation("PropertyNames", propertyNames);
180 tEnv.addObjRelation("SkipProperties", propertyNames);
182 //Adding relation for util.XSortable
183 final XParagraphCursor paragrCursor = UnoRuntime.queryInterface(
184 XParagraphCursor.class,
185 oObj);
186 final PrintWriter finalLog = log;
188 tEnv.addObjRelation("SORTCHECKER",
189 new ifc.util._XSortable.XSortChecker() {
190 PrintWriter out = finalLog;
192 public void setPrintWriter(PrintWriter log) {
193 out = log;
196 public void prepareToSort() {
197 textCursor.gotoEnd(false);
199 try {
200 aText.insertControlCharacter(textCursor,
201 ControlCharacter.PARAGRAPH_BREAK,
202 false);
203 aText.insertString(textCursor, "4", false);
204 aText.insertControlCharacter(textCursor,
205 ControlCharacter.PARAGRAPH_BREAK,
206 false);
207 aText.insertString(textCursor, "b", false);
208 aText.insertControlCharacter(textCursor,
209 ControlCharacter.PARAGRAPH_BREAK,
210 false);
211 aText.insertString(textCursor, "3", false);
212 aText.insertControlCharacter(textCursor,
213 ControlCharacter.PARAGRAPH_BREAK,
214 false);
215 aText.insertString(textCursor, "a", false);
216 aText.insertControlCharacter(textCursor,
217 ControlCharacter.PARAGRAPH_BREAK,
218 false);
219 aText.insertString(textCursor, "23", false);
220 aText.insertControlCharacter(textCursor,
221 ControlCharacter.PARAGRAPH_BREAK,
222 false);
223 aText.insertString(textCursor, "ab", false);
224 aText.insertControlCharacter(textCursor,
225 ControlCharacter.PARAGRAPH_BREAK,
226 false);
227 } catch (com.sun.star.lang.IllegalArgumentException e) {
228 out.println("Unexpected exception:" + e);
231 out.println(
232 "Preparing cursor to sorting. Text before sorting:");
233 paragrCursor.gotoEnd(true);
235 for (int i = 0; i < 6; i++) {
236 paragrCursor.gotoPreviousParagraph(true);
239 out.println(textCursor.getString());
242 public boolean checkSort(boolean isSortNumbering,
243 boolean isSortAscending) {
244 out.println("Sort checking...");
246 String ls = System.getProperty("line.separator");
248 String text = paragrCursor.getString();
249 out.println("Text after sorting:\n" + text);
251 boolean res = false;
253 if (isSortNumbering) {
254 if (isSortAscending) {
255 res = text.endsWith(ls+"3"+ls+"4"+ls+"23");
257 if (!res) {
258 out.println("Text must ends by:\n" + "\r\n3\r\n4\r\n23\r\n");
260 } else {
261 res = text.startsWith("23"+ls+"4"+ls+"3"+ls);
263 if (!res) {
264 out.println("Text must starts with:\n" + "23\r\n4\r\n3\r\n");
267 } else {
268 if (isSortAscending) {
269 res = text.equals(ls+"23"+ls+"3"+ls+"4"+ls+"a"+ls+"ab"+ls+"b");
271 if (!res) {
272 out.println("Text must be equal to:\n" + "\r\n23\r\n3\r\n4\r\na\r\nab\r\nb\r\n");
274 } else {
275 res = text.endsWith("b"+ls+"ab"+ls+"a"+ls+"4"+ls+"3"+ls+"23"+ls);
277 if (!res) {
278 out.println("Text must be equal to:\n" + "b\r\nab\r\na\r\n4\r\n3\r\n23\r\n");
283 if (res) {
284 out.println("Sorted correctly");
287 return res;
291 return tEnv;
292 } // finish method getTestEnvironment
294 public String[] getPropertyNames(XPropertySet props, String[] skipList) {
295 Property[] the_props = props.getPropertySetInfo().getProperties();
296 ArrayList<String> names = new ArrayList<String>();
298 for (int i = 0; i < the_props.length; i++) {
299 boolean isWritable = ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
300 if (isWritable && !Arrays.asList(skipList).contains(the_props[i].Name)) {
301 names.add(the_props[i].Name);
305 return names.toArray(new String[names.size()]);
307 } // finish class SwXTextCursor