1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.text;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.text.XText;
31 import com.sun.star.text.XTextCursor;
32 import com.sun.star.text.XTextDocument;
33 import com.sun.star.text.XTextRange;
34 import com.sun.star.text.XTextRangeCompare;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 
38 /**
39  * Testing <code>com.sun.star.text.XTextRangeCompare</code>
40  * interface methods :
41  * <ul>
42  *  <li><code> compareRegionStarts()</code></li>
43  *  <li><code> compareRegionEnds()</code></li>
44  * </ul> <p>
45  * This test needs the following object relations :
46  * <ul>
47  *  <li> <code>'TEXTDOC'</code> <b>optional</b>
48  *  (must implement <code>XTextDocument</code>):
49  *   can be used to obtain <code>Text</code> of the document from
50  *   which cursors can be created.
51  *   If the relation does not exist, the relation <code>TEXT</code>
52  *   must be specified. </li>
53  *  <li> <code>'TEXT'</code> <b>optional</b>
54  *  (of type <code>XText</code>):
55  *   used to create text cursor.
56  *   If the relation does not exist, the relation <code>TEXTDOC</code>
57  *   must be specified. </li>
58  * <ul> <p>
59  * Test is <b> NOT </b> multithread compilant. <p>
60  * @see com.sun.star.text.XTextRangeCompare
61  */
62 public class _XTextRangeCompare extends MultiMethodTest {
63 
64     /**
65      * the test object
66      */
67     public XTextRangeCompare oObj = null;
68 
69 
70     String nameStr = null;
71 
72     XTextRange oRange = null;
73     XTextCursor cursor1 = null;
74     XTextCursor cursor2 = null;
75     String startStr = null;
76     String endStr = null;
77     XText oText = null;
78 
79     /**
80      * Retrieves <code>XText</code> interface from relation 'TEXTDOC'
81      * or from 'TEXT'.
82      * @throws StatusException If neither 'TEXTDOC' nore 'TEXT'
83      * relation exists.
84      */
before()85     public void before() {
86         nameStr = this.getClass().getName();
87 
88         XInterface oIfc = (XInterface)tEnv.getObjRelation("TEXTDOC");
89         if (oIfc!=null) {
90             XTextDocument oTDoc = (XTextDocument)UnoRuntime.queryInterface(
91                                       XTextDocument.class, oIfc);
92             oText = oTDoc.getText();
93         }
94         XText aText = (XText) tEnv.getObjRelation("TEXT");
95         if (aText != null) {
96             oText = aText;
97         }
98 
99         if (oText == null) {
100             throw new StatusException(Status.failed
101                 ("Neither 'TEXTDOC' nore 'TEXT' relation not found")) ;
102         }
103     }
104 
105     /**
106      * One cursor is created and to its position a paragraph
107      * inserted, then the fist five characters was selected.
108      * A second cursor was created and the last 7 characteres
109      * was selected.<p>
110      *
111      * Has <b>OK</b> status if the compare returns 1, i.e.
112      * the second cursor end is before the first.
113      */
_compareRegionEnds()114     public void _compareRegionEnds() {
115         boolean bResult = false;
116         short n = 0;
117         log.println( "testing compareRegionEnds()" );
118 
119         try{
120             cursor1 = oText.createTextCursor();
121             oText.insertString(cursor1, nameStr, false);
122 
123             cursor1.gotoStart(false);
124             cursor1.goRight((short)5, true);
125             cursor2 = oText.createTextCursor();
126             cursor2.gotoEnd(false);
127             cursor2.goLeft((short)7, true);
128 
129             log.println("hole text: '" + oText.getString() + "'");
130             log.println("cursor1: '"+cursor1.getString() + "'");
131             log.println("cursor2: '"+cursor2.getString() + "'");
132             log.println("check: oObj.compareRegionStarts(cursor1, cursor2)");
133 
134             n = oObj.compareRegionEnds(cursor1, cursor2);
135 
136             log.println( "Result (short) : " + n );
137         }catch(com.sun.star.lang.IllegalArgumentException e){
138             log.println( "Exception: " + e);
139             e.printStackTrace(log);
140         }
141 
142         if (n == 1){bResult = true;}
143         tRes.tested( "compareRegionEnds()", bResult );
144     }
145 
146     /**
147      * One cursor is created and to its position a paragraph
148      * inserted, then the fist five characters was selected.
149      * A second cursor was created and the last 7 characters
150      * was selected.<p>
151      *
152      * Has <b>OK</b> status if the compare returns 1, i.e.
153      * the second cursor start is before the first.
154      */
_compareRegionStarts()155     public void _compareRegionStarts() {
156         boolean bResult = false;
157         short n = 0;
158 
159         try{
160             cursor1 = oText.createTextCursor();
161             oText.insertString(cursor1, nameStr, false);
162 
163             cursor1.gotoStart(false);
164             cursor1.goRight((short)5, true);
165             cursor2 = oText.createTextCursor();
166             cursor2.gotoEnd(false);
167             cursor2.goLeft((short)7, true);
168 
169             log.println("hole text: '" + oText.getString() + "'");
170             log.println("cursor1: '"+cursor1.getString() + "'");
171             log.println("cursor2: '"+cursor2.getString() + "'");
172             log.println("check: oObj.compareRegionStarts(cursor1, cursor2)");
173             n = oObj.compareRegionStarts(cursor1, cursor2);
174 
175             log.println( "Result (short) : " + n );
176         }catch(com.sun.star.lang.IllegalArgumentException e){
177             log.println( "Exception: " + e);
178             e.printStackTrace(log);
179         }
180         if (n == 1){bResult = true;}
181         tRes.tested( "compareRegionStarts()", bResult );
182     }
183 
184 }
185 
186