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