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