1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.text;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import com.sun.star.text.XText;
35*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
36*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
37*cdf0e10cSrcweir import com.sun.star.text.XTextRange;
38*cdf0e10cSrcweir import com.sun.star.text.XTextRangeCompare;
39*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
40*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir /**
43*cdf0e10cSrcweir  * Testing <code>com.sun.star.text.XTextRangeCompare</code>
44*cdf0e10cSrcweir  * interface methods :
45*cdf0e10cSrcweir  * <ul>
46*cdf0e10cSrcweir  *  <li><code> compareRegionStarts()</code></li>
47*cdf0e10cSrcweir  *  <li><code> compareRegionEnds()</code></li>
48*cdf0e10cSrcweir  * </ul> <p>
49*cdf0e10cSrcweir  * This test needs the following object relations :
50*cdf0e10cSrcweir  * <ul>
51*cdf0e10cSrcweir  *  <li> <code>'TEXTDOC'</code> <b>optional</b>
52*cdf0e10cSrcweir  *  (must implement <code>XTextDocument</code>):
53*cdf0e10cSrcweir  *   can be used to obtain <code>Text</code> of the document from
54*cdf0e10cSrcweir  *   which cursors can be created.
55*cdf0e10cSrcweir  *   If the relation does not exist, the relation <code>TEXT</code>
56*cdf0e10cSrcweir  *   must be specified. </li>
57*cdf0e10cSrcweir  *  <li> <code>'TEXT'</code> <b>optional</b>
58*cdf0e10cSrcweir  *  (of type <code>XText</code>):
59*cdf0e10cSrcweir  *   used to create text cursor.
60*cdf0e10cSrcweir  *   If the relation does not exist, the relation <code>TEXTDOC</code>
61*cdf0e10cSrcweir  *   must be specified. </li>
62*cdf0e10cSrcweir  * <ul> <p>
63*cdf0e10cSrcweir  * Test is <b> NOT </b> multithread compilant. <p>
64*cdf0e10cSrcweir  * @see com.sun.star.text.XTextRangeCompare
65*cdf0e10cSrcweir  */
66*cdf0e10cSrcweir public class _XTextRangeCompare extends MultiMethodTest {
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     /**
69*cdf0e10cSrcweir      * the test object
70*cdf0e10cSrcweir      */
71*cdf0e10cSrcweir     public XTextRangeCompare oObj = null;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     String nameStr = null;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     XTextRange oRange = null;
77*cdf0e10cSrcweir     XTextCursor cursor1 = null;
78*cdf0e10cSrcweir     XTextCursor cursor2 = null;
79*cdf0e10cSrcweir     String startStr = null;
80*cdf0e10cSrcweir     String endStr = null;
81*cdf0e10cSrcweir     XText oText = null;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     /**
84*cdf0e10cSrcweir      * Retrieves <code>XText</code> interface from relation 'TEXTDOC'
85*cdf0e10cSrcweir      * or from 'TEXT'.
86*cdf0e10cSrcweir      * @throws StatusException If neither 'TEXTDOC' nore 'TEXT'
87*cdf0e10cSrcweir      * relation exists.
88*cdf0e10cSrcweir      */
89*cdf0e10cSrcweir     public void before() {
90*cdf0e10cSrcweir         nameStr = this.getClass().getName();
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         XInterface oIfc = (XInterface)tEnv.getObjRelation("TEXTDOC");
93*cdf0e10cSrcweir         if (oIfc!=null) {
94*cdf0e10cSrcweir             XTextDocument oTDoc = (XTextDocument)UnoRuntime.queryInterface(
95*cdf0e10cSrcweir                                       XTextDocument.class, oIfc);
96*cdf0e10cSrcweir             oText = oTDoc.getText();
97*cdf0e10cSrcweir         }
98*cdf0e10cSrcweir         XText aText = (XText) tEnv.getObjRelation("TEXT");
99*cdf0e10cSrcweir         if (aText != null) {
100*cdf0e10cSrcweir             oText = aText;
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         if (oText == null) {
104*cdf0e10cSrcweir             throw new StatusException(Status.failed
105*cdf0e10cSrcweir                 ("Neither 'TEXTDOC' nore 'TEXT' relation not found")) ;
106*cdf0e10cSrcweir         }
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     /**
110*cdf0e10cSrcweir      * One cursor is created and to its position a paragraph
111*cdf0e10cSrcweir      * inserted, then the fist five characters was selected.
112*cdf0e10cSrcweir      * A second cursor was created and the last 7 characteres
113*cdf0e10cSrcweir      * was selected.<p>
114*cdf0e10cSrcweir      *
115*cdf0e10cSrcweir      * Has <b>OK</b> status if the compare returns 1, i.e.
116*cdf0e10cSrcweir      * the second cursor end is before the first.
117*cdf0e10cSrcweir      */
118*cdf0e10cSrcweir     public void _compareRegionEnds() {
119*cdf0e10cSrcweir         boolean bResult = false;
120*cdf0e10cSrcweir         short n = 0;
121*cdf0e10cSrcweir         log.println( "testing compareRegionEnds()" );
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         try{
124*cdf0e10cSrcweir             cursor1 = oText.createTextCursor();
125*cdf0e10cSrcweir             oText.insertString(cursor1, nameStr, false);
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir             cursor1.gotoStart(false);
128*cdf0e10cSrcweir             cursor1.goRight((short)5, true);
129*cdf0e10cSrcweir             cursor2 = oText.createTextCursor();
130*cdf0e10cSrcweir             cursor2.gotoEnd(false);
131*cdf0e10cSrcweir             cursor2.goLeft((short)7, true);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir             log.println("hole text: '" + oText.getString() + "'");
134*cdf0e10cSrcweir             log.println("cursor1: '"+cursor1.getString() + "'");
135*cdf0e10cSrcweir             log.println("cursor2: '"+cursor2.getString() + "'");
136*cdf0e10cSrcweir             log.println("check: oObj.compareRegionStarts(cursor1, cursor2)");
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             n = oObj.compareRegionEnds(cursor1, cursor2);
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir             log.println( "Result (short) : " + n );
141*cdf0e10cSrcweir         }catch(com.sun.star.lang.IllegalArgumentException e){
142*cdf0e10cSrcweir             log.println( "Exception: " + e);
143*cdf0e10cSrcweir             e.printStackTrace(log);
144*cdf0e10cSrcweir         }
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         if (n == 1){bResult = true;}
147*cdf0e10cSrcweir         tRes.tested( "compareRegionEnds()", bResult );
148*cdf0e10cSrcweir     }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     /**
151*cdf0e10cSrcweir      * One cursor is created and to its position a paragraph
152*cdf0e10cSrcweir      * inserted, then the fist five characters was selected.
153*cdf0e10cSrcweir      * A second cursor was created and the last 7 characters
154*cdf0e10cSrcweir      * was selected.<p>
155*cdf0e10cSrcweir      *
156*cdf0e10cSrcweir      * Has <b>OK</b> status if the compare returns 1, i.e.
157*cdf0e10cSrcweir      * the second cursor start is before the first.
158*cdf0e10cSrcweir      */
159*cdf0e10cSrcweir     public void _compareRegionStarts() {
160*cdf0e10cSrcweir         boolean bResult = false;
161*cdf0e10cSrcweir         short n = 0;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         try{
164*cdf0e10cSrcweir             cursor1 = oText.createTextCursor();
165*cdf0e10cSrcweir             oText.insertString(cursor1, nameStr, false);
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir             cursor1.gotoStart(false);
168*cdf0e10cSrcweir             cursor1.goRight((short)5, true);
169*cdf0e10cSrcweir             cursor2 = oText.createTextCursor();
170*cdf0e10cSrcweir             cursor2.gotoEnd(false);
171*cdf0e10cSrcweir             cursor2.goLeft((short)7, true);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir             log.println("hole text: '" + oText.getString() + "'");
174*cdf0e10cSrcweir             log.println("cursor1: '"+cursor1.getString() + "'");
175*cdf0e10cSrcweir             log.println("cursor2: '"+cursor2.getString() + "'");
176*cdf0e10cSrcweir             log.println("check: oObj.compareRegionStarts(cursor1, cursor2)");
177*cdf0e10cSrcweir             n = oObj.compareRegionStarts(cursor1, cursor2);
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir             log.println( "Result (short) : " + n );
180*cdf0e10cSrcweir         }catch(com.sun.star.lang.IllegalArgumentException e){
181*cdf0e10cSrcweir             log.println( "Exception: " + e);
182*cdf0e10cSrcweir             e.printStackTrace(log);
183*cdf0e10cSrcweir         }
184*cdf0e10cSrcweir         if (n == 1){bResult = true;}
185*cdf0e10cSrcweir         tRes.tested( "compareRegionStarts()", bResult );
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir }
189*cdf0e10cSrcweir 
190