1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski package mod._sw; 25*b1cdbd2cSJim Jagielski 26*b1cdbd2cSJim Jagielski import java.io.PrintWriter; 27*b1cdbd2cSJim Jagielski 28*b1cdbd2cSJim Jagielski import lib.StatusException; 29*b1cdbd2cSJim Jagielski import lib.TestCase; 30*b1cdbd2cSJim Jagielski import lib.TestEnvironment; 31*b1cdbd2cSJim Jagielski import lib.TestParameters; 32*b1cdbd2cSJim Jagielski import util.DefaultDsc; 33*b1cdbd2cSJim Jagielski import util.InstCreator; 34*b1cdbd2cSJim Jagielski import util.SOfficeFactory; 35*b1cdbd2cSJim Jagielski import com.sun.star.container.XNameAccess; 36*b1cdbd2cSJim Jagielski import com.sun.star.lang.XMultiServiceFactory; 37*b1cdbd2cSJim Jagielski import com.sun.star.text.XAutoTextContainer; 38*b1cdbd2cSJim Jagielski import com.sun.star.text.XAutoTextEntry; 39*b1cdbd2cSJim Jagielski import com.sun.star.text.XAutoTextGroup; 40*b1cdbd2cSJim Jagielski import com.sun.star.text.XText; 41*b1cdbd2cSJim Jagielski import com.sun.star.text.XTextDocument; 42*b1cdbd2cSJim Jagielski import com.sun.star.text.XTextRange; 43*b1cdbd2cSJim Jagielski import com.sun.star.uno.AnyConverter; 44*b1cdbd2cSJim Jagielski import com.sun.star.uno.Type; 45*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime; 46*b1cdbd2cSJim Jagielski import com.sun.star.uno.XInterface; 47*b1cdbd2cSJim Jagielski 48*b1cdbd2cSJim Jagielski 49*b1cdbd2cSJim Jagielski /** 50*b1cdbd2cSJim Jagielski * Test for object which is represented by service 51*b1cdbd2cSJim Jagielski * <code>com.sun.star.text.AutoTextEntry</code>.<p> 52*b1cdbd2cSJim Jagielski * Object implements the following interfaces : 53*b1cdbd2cSJim Jagielski * <ul> 54*b1cdbd2cSJim Jagielski * <li> <code>com::sun::star::text::XSimpleText</code></li> 55*b1cdbd2cSJim Jagielski * <li> <code>com::sun::star::text::XAutoTextEntry</code></li> 56*b1cdbd2cSJim Jagielski * <li> <code>com::sun::star::text::XTextRange</code></li> 57*b1cdbd2cSJim Jagielski * <li> <code>com::sun::star::text::XText</code></li> 58*b1cdbd2cSJim Jagielski * </ul> <p> 59*b1cdbd2cSJim Jagielski * This object test <b> is NOT </b> designed to be run in several 60*b1cdbd2cSJim Jagielski * threads concurently. 61*b1cdbd2cSJim Jagielski * @see com.sun.star.text.AutoTextEntry 62*b1cdbd2cSJim Jagielski * @see com.sun.star.text.AutoTextContainer 63*b1cdbd2cSJim Jagielski * @see com.sun.star.text.XSimpleText 64*b1cdbd2cSJim Jagielski * @see com.sun.star.text.XAutoTextEntry 65*b1cdbd2cSJim Jagielski * @see com.sun.star.text.XTextRange 66*b1cdbd2cSJim Jagielski * @see com.sun.star.text.XText 67*b1cdbd2cSJim Jagielski * @see ifc.text._XSimpleText 68*b1cdbd2cSJim Jagielski * @see ifc.text._XAutoTextEntry 69*b1cdbd2cSJim Jagielski * @see ifc.text._XTextRange 70*b1cdbd2cSJim Jagielski * @see ifc.text._XText 71*b1cdbd2cSJim Jagielski */ 72*b1cdbd2cSJim Jagielski public class SwXAutoTextEntry extends TestCase { 73*b1cdbd2cSJim Jagielski XTextDocument xTextDoc; 74*b1cdbd2cSJim Jagielski XAutoTextGroup oGroup; 75*b1cdbd2cSJim Jagielski 76*b1cdbd2cSJim Jagielski /** 77*b1cdbd2cSJim Jagielski * Creates text document. 78*b1cdbd2cSJim Jagielski */ initialize( TestParameters tParam, PrintWriter log )79*b1cdbd2cSJim Jagielski protected void initialize( TestParameters tParam, PrintWriter log ) { 80*b1cdbd2cSJim Jagielski XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF(); 81*b1cdbd2cSJim Jagielski SOfficeFactory SOF = SOfficeFactory.getFactory( msf ); 82*b1cdbd2cSJim Jagielski try { 83*b1cdbd2cSJim Jagielski log.println( "creating a textdocument" ); 84*b1cdbd2cSJim Jagielski xTextDoc = SOF.createTextDoc( null ); 85*b1cdbd2cSJim Jagielski } catch ( com.sun.star.uno.Exception e ) { 86*b1cdbd2cSJim Jagielski e.printStackTrace( log ); 87*b1cdbd2cSJim Jagielski throw new StatusException( "Couldn't create document", e ); 88*b1cdbd2cSJim Jagielski } 89*b1cdbd2cSJim Jagielski } 90*b1cdbd2cSJim Jagielski 91*b1cdbd2cSJim Jagielski /** 92*b1cdbd2cSJim Jagielski * Removes added element from AutoTextGroup 93*b1cdbd2cSJim Jagielski */ cleanup( TestParameters Param, PrintWriter log)94*b1cdbd2cSJim Jagielski protected void cleanup( TestParameters Param, PrintWriter log) { 95*b1cdbd2cSJim Jagielski try { 96*b1cdbd2cSJim Jagielski if ( oGroup.hasByName("NewEntryName") ) { 97*b1cdbd2cSJim Jagielski log.println("Removing 'NewEntryName' element"); 98*b1cdbd2cSJim Jagielski oGroup.removeByName("NewEntryName"); 99*b1cdbd2cSJim Jagielski } 100*b1cdbd2cSJim Jagielski } catch ( com.sun.star.container.NoSuchElementException e ) { 101*b1cdbd2cSJim Jagielski log.println("Cannot remove TextEntry from group..."); 102*b1cdbd2cSJim Jagielski e.printStackTrace(log); 103*b1cdbd2cSJim Jagielski } 104*b1cdbd2cSJim Jagielski log.println( "disposing xTextDoc " ); 105*b1cdbd2cSJim Jagielski util.DesktopTools.closeDoc(xTextDoc); 106*b1cdbd2cSJim Jagielski } 107*b1cdbd2cSJim Jagielski 108*b1cdbd2cSJim Jagielski 109*b1cdbd2cSJim Jagielski /** 110*b1cdbd2cSJim Jagielski * Creating a Testenvironment for the interfaces to be tested. 111*b1cdbd2cSJim Jagielski * Creates an instance of the service 112*b1cdbd2cSJim Jagielski * <code>com.sun.star.text.AutoTextContainer</code>, then selects the 'mytexts' 113*b1cdbd2cSJim Jagielski * group from the given container using <code>XNameAccess</code> interface, 114*b1cdbd2cSJim Jagielski * and inserts some text entry to this group. Then entry passed as test 115*b1cdbd2cSJim Jagielski * component.<p> 116*b1cdbd2cSJim Jagielski * Object relations created : 117*b1cdbd2cSJim Jagielski * <ul> 118*b1cdbd2cSJim Jagielski * <li><code>'XTEXTINFO'</code> for 119*b1cdbd2cSJim Jagielski * {@link ifc.text._XText} : creates tables 6x4</li> 120*b1cdbd2cSJim Jagielski * <li><code>'TEXTDOC'</code> for 121*b1cdbd2cSJim Jagielski * {@link ifc.text._XAutoTextEntry} : text document</li> 122*b1cdbd2cSJim Jagielski * </ul> 123*b1cdbd2cSJim Jagielski */ createTestEnvironment(TestParameters Param, PrintWriter log)124*b1cdbd2cSJim Jagielski protected synchronized TestEnvironment createTestEnvironment 125*b1cdbd2cSJim Jagielski (TestParameters Param, PrintWriter log) { 126*b1cdbd2cSJim Jagielski 127*b1cdbd2cSJim Jagielski XAutoTextEntry oEntry = null; 128*b1cdbd2cSJim Jagielski XAutoTextContainer oContainer; 129*b1cdbd2cSJim Jagielski XInterface oObj = null; 130*b1cdbd2cSJim Jagielski int n = 0; 131*b1cdbd2cSJim Jagielski int nCount = 0; 132*b1cdbd2cSJim Jagielski 133*b1cdbd2cSJim Jagielski log.println( "creating a test environment" ); 134*b1cdbd2cSJim Jagielski try { 135*b1cdbd2cSJim Jagielski XMultiServiceFactory myMSF = (XMultiServiceFactory)Param.getMSF(); 136*b1cdbd2cSJim Jagielski Object oInst = myMSF.createInstance 137*b1cdbd2cSJim Jagielski ("com.sun.star.text.AutoTextContainer"); 138*b1cdbd2cSJim Jagielski oContainer = (XAutoTextContainer) 139*b1cdbd2cSJim Jagielski UnoRuntime.queryInterface(XAutoTextContainer.class,oInst); 140*b1cdbd2cSJim Jagielski } catch (com.sun.star.uno.Exception e) { 141*b1cdbd2cSJim Jagielski e.printStackTrace(log); 142*b1cdbd2cSJim Jagielski throw new StatusException("Couldn't create AutoTextContainer", e); 143*b1cdbd2cSJim Jagielski } 144*b1cdbd2cSJim Jagielski 145*b1cdbd2cSJim Jagielski XNameAccess oContNames = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oContainer); 146*b1cdbd2cSJim Jagielski 147*b1cdbd2cSJim Jagielski String contNames[] = oContNames.getElementNames(); 148*b1cdbd2cSJim Jagielski for (int i =0; i < contNames.length; i++){ 149*b1cdbd2cSJim Jagielski log.println("ContainerNames[ "+ i + "]: " + contNames[i]); 150*b1cdbd2cSJim Jagielski } 151*b1cdbd2cSJim Jagielski 152*b1cdbd2cSJim Jagielski try{ 153*b1cdbd2cSJim Jagielski oObj = (XInterface) AnyConverter.toObject(new Type(XInterface.class),oContNames.getByName("mytexts")); 154*b1cdbd2cSJim Jagielski } catch (com.sun.star.uno.Exception e) { 155*b1cdbd2cSJim Jagielski e.printStackTrace(log); 156*b1cdbd2cSJim Jagielski throw new StatusException("Couldn't get AutoTextGroup", e); 157*b1cdbd2cSJim Jagielski } 158*b1cdbd2cSJim Jagielski 159*b1cdbd2cSJim Jagielski oGroup = (XAutoTextGroup) UnoRuntime.queryInterface 160*b1cdbd2cSJim Jagielski (XAutoTextGroup.class, oObj); 161*b1cdbd2cSJim Jagielski String[] oENames = oGroup.getElementNames(); 162*b1cdbd2cSJim Jagielski for (int i=0; i<oENames.length; i++) { 163*b1cdbd2cSJim Jagielski log.println("AutoTextEntryNames[" + i + "]: " + oENames[i]); 164*b1cdbd2cSJim Jagielski } 165*b1cdbd2cSJim Jagielski 166*b1cdbd2cSJim Jagielski XText oText = xTextDoc.getText(); 167*b1cdbd2cSJim Jagielski oText.insertString(oText.getStart(), "New AutoText", true); 168*b1cdbd2cSJim Jagielski XTextRange oTextRange = (XTextRange) oText; 169*b1cdbd2cSJim Jagielski try { 170*b1cdbd2cSJim Jagielski if ( oGroup.hasByName("NewEntryName") ) { 171*b1cdbd2cSJim Jagielski oGroup.removeByName("NewEntryName"); 172*b1cdbd2cSJim Jagielski log.println("Element 'NewEntryName' exists, removing..."); 173*b1cdbd2cSJim Jagielski } 174*b1cdbd2cSJim Jagielski log.println("Adding new element 'NewEntryName' to group..."); 175*b1cdbd2cSJim Jagielski oGroup.insertNewByName("NewEntryName", "NewEntryTitle", oTextRange); 176*b1cdbd2cSJim Jagielski oEntry = (XAutoTextEntry) AnyConverter.toObject( 177*b1cdbd2cSJim Jagielski new Type(XAutoTextEntry.class),oGroup.getByName("NewEntryName")); 178*b1cdbd2cSJim Jagielski } catch ( com.sun.star.container.ElementExistException e ) { 179*b1cdbd2cSJim Jagielski e.printStackTrace(log); 180*b1cdbd2cSJim Jagielski } catch ( com.sun.star.container.NoSuchElementException e ) { 181*b1cdbd2cSJim Jagielski e.printStackTrace(log); 182*b1cdbd2cSJim Jagielski } catch ( com.sun.star.lang.WrappedTargetException e ) { 183*b1cdbd2cSJim Jagielski e.printStackTrace(log); 184*b1cdbd2cSJim Jagielski } catch ( com.sun.star.lang.IllegalArgumentException e ) { 185*b1cdbd2cSJim Jagielski e.printStackTrace(log); 186*b1cdbd2cSJim Jagielski } 187*b1cdbd2cSJim Jagielski 188*b1cdbd2cSJim Jagielski oObj = oEntry; 189*b1cdbd2cSJim Jagielski 190*b1cdbd2cSJim Jagielski log.println("Trying to use XText as TextRange in the method applyTo"); 191*b1cdbd2cSJim Jagielski oEntry.applyTo(oTextRange); 192*b1cdbd2cSJim Jagielski 193*b1cdbd2cSJim Jagielski oTextRange = oText.createTextCursor(); 194*b1cdbd2cSJim Jagielski log.println("Trying to use XTextCursor as TextRange in the method applyTo"); 195*b1cdbd2cSJim Jagielski oEntry.applyTo(oTextRange); 196*b1cdbd2cSJim Jagielski 197*b1cdbd2cSJim Jagielski log.println( "creating a new environment for AutoTextEntry object" ); 198*b1cdbd2cSJim Jagielski TestEnvironment tEnv = new TestEnvironment( oObj ); 199*b1cdbd2cSJim Jagielski 200*b1cdbd2cSJim Jagielski // adding relation for XText 201*b1cdbd2cSJim Jagielski DefaultDsc tDsc = new DefaultDsc("com.sun.star.text.XTextContent", 202*b1cdbd2cSJim Jagielski "com.sun.star.text.TextField.DateTime"); 203*b1cdbd2cSJim Jagielski log.println( " adding InstCreator object" ); 204*b1cdbd2cSJim Jagielski tEnv.addObjRelation( "XTEXTINFO", new InstCreator( xTextDoc, tDsc ) ); 205*b1cdbd2cSJim Jagielski 206*b1cdbd2cSJim Jagielski log.println( "adding TextDocument as mod relation to environment" ); 207*b1cdbd2cSJim Jagielski tEnv.addObjRelation("TEXTDOC", xTextDoc); 208*b1cdbd2cSJim Jagielski 209*b1cdbd2cSJim Jagielski return tEnv; 210*b1cdbd2cSJim Jagielski } // finish method getTestEnvironment 211*b1cdbd2cSJim Jagielski 212*b1cdbd2cSJim Jagielski 213*b1cdbd2cSJim Jagielski } // finish class SwXAutoTextEntry 214