Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / text / _XTextRangeCompare.java
blob8c4bceb6de14471c91049ec91d2524d7ac1a6cfa
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 .
19 package ifc.text;
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
25 import com.sun.star.text.XText;
26 import com.sun.star.text.XTextCursor;
27 import com.sun.star.text.XTextDocument;
28 import com.sun.star.text.XTextRange;
29 import com.sun.star.text.XTextRangeCompare;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XInterface;
33 /**
34 * Testing <code>com.sun.star.text.XTextRangeCompare</code>
35 * interface methods :
36 * <ul>
37 * <li><code> compareRegionStarts()</code></li>
38 * <li><code> compareRegionEnds()</code></li>
39 * </ul> <p>
40 * This test needs the following object relations :
41 * <ul>
42 * <li> <code>'TEXTDOC'</code> <b>optional</b>
43 * (must implement <code>XTextDocument</code>):
44 * can be used to obtain <code>Text</code> of the document from
45 * which cursors can be created.
46 * If the relation does not exist, the relation <code>TEXT</code>
47 * must be specified. </li>
48 * <li> <code>'TEXT'</code> <b>optional</b>
49 * (of type <code>XText</code>):
50 * used to create text cursor.
51 * If the relation does not exist, the relation <code>TEXTDOC</code>
52 * must be specified. </li>
53 * <ul> <p>
54 * Test is <b> NOT </b> multithread compliant. <p>
55 * @see com.sun.star.text.XTextRangeCompare
57 public class _XTextRangeCompare extends MultiMethodTest {
59 /**
60 * the test object
62 public XTextRangeCompare oObj = null;
65 String nameStr = null;
67 XTextRange oRange = null;
68 XTextCursor cursor1 = null;
69 XTextCursor cursor2 = null;
70 String startStr = null;
71 String endStr = null;
72 XText oText = null;
74 /**
75 * Retrieves <code>XText</code> interface from relation 'TEXTDOC'
76 * or from 'TEXT'.
77 * @throws StatusException If neither 'TEXTDOC' nore 'TEXT'
78 * relation exists.
80 @Override
81 public void before() {
82 nameStr = this.getClass().getName();
84 XInterface oIfc = (XInterface)tEnv.getObjRelation("TEXTDOC");
85 if (oIfc!=null) {
86 XTextDocument oTDoc = UnoRuntime.queryInterface(
87 XTextDocument.class, oIfc);
88 oText = oTDoc.getText();
90 XText aText = (XText) tEnv.getObjRelation("TEXT");
91 if (aText != null) {
92 oText = aText;
95 if (oText == null) {
96 throw new StatusException(Status.failed
97 ("Neither 'TEXTDOC' nore 'TEXT' relation not found")) ;
102 * One cursor is created and to its position a paragraph
103 * inserted, then the fist five characters was selected.
104 * A second cursor was created and the last 7 characteres
105 * was selected.<p>
107 * Has <b>OK</b> status if the compare returns 1, i.e.
108 * the second cursor end is before the first.
110 public void _compareRegionEnds() {
111 boolean bResult = false;
112 short n = 0;
113 log.println( "testing compareRegionEnds()" );
115 try{
116 cursor1 = oText.createTextCursor();
117 oText.insertString(cursor1, nameStr, false);
119 cursor1.gotoStart(false);
120 cursor1.goRight((short)5, true);
121 cursor2 = oText.createTextCursor();
122 cursor2.gotoEnd(false);
123 cursor2.goLeft((short)7, true);
125 log.println("hole text: '" + oText.getString() + "'");
126 log.println("cursor1: '"+cursor1.getString() + "'");
127 log.println("cursor2: '"+cursor2.getString() + "'");
128 log.println("check: oObj.compareRegionStarts(cursor1, cursor2)");
130 n = oObj.compareRegionEnds(cursor1, cursor2);
132 log.println( "Result (short) : " + n );
133 }catch(com.sun.star.lang.IllegalArgumentException e){
134 log.println( "Exception: " + e);
135 e.printStackTrace(log);
138 if (n == 1){bResult = true;}
139 tRes.tested( "compareRegionEnds()", bResult );
143 * One cursor is created and to its position a paragraph
144 * inserted, then the fist five characters was selected.
145 * A second cursor was created and the last 7 characters
146 * was selected.<p>
148 * Has <b>OK</b> status if the compare returns 1, i.e.
149 * the second cursor start is before the first.
151 public void _compareRegionStarts() {
152 boolean bResult = false;
153 short n = 0;
155 try{
156 cursor1 = oText.createTextCursor();
157 oText.insertString(cursor1, nameStr, false);
159 cursor1.gotoStart(false);
160 cursor1.goRight((short)5, true);
161 cursor2 = oText.createTextCursor();
162 cursor2.gotoEnd(false);
163 cursor2.goLeft((short)7, true);
165 log.println("hole text: '" + oText.getString() + "'");
166 log.println("cursor1: '"+cursor1.getString() + "'");
167 log.println("cursor2: '"+cursor2.getString() + "'");
168 log.println("check: oObj.compareRegionStarts(cursor1, cursor2)");
169 n = oObj.compareRegionStarts(cursor1, cursor2);
171 log.println( "Result (short) : " + n );
172 }catch(com.sun.star.lang.IllegalArgumentException e){
173 log.println( "Exception: " + e);
174 e.printStackTrace(log);
176 if (n == 1){bResult = true;}
177 tRes.tested( "compareRegionStarts()", bResult );