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 mod._sw; 29 30 import com.sun.star.beans.PropertyAttribute; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.container.XIndexAccess; 33 import com.sun.star.container.XNameAccess; 34 import com.sun.star.container.XNameContainer; 35 import com.sun.star.lang.XMultiServiceFactory; 36 import com.sun.star.style.XStyle; 37 import com.sun.star.style.XStyleFamiliesSupplier; 38 import com.sun.star.text.XText; 39 import com.sun.star.text.XTextCursor; 40 import com.sun.star.text.XTextDocument; 41 import com.sun.star.uno.UnoRuntime; 42 import com.sun.star.uno.XInterface; 43 import java.io.PrintWriter; 44 import lib.StatusException; 45 import lib.TestCase; 46 import lib.TestEnvironment; 47 import lib.TestParameters; 48 import util.DesktopTools; 49 import util.SOfficeFactory; 50 import util.utils; 51 52 /** 53 * Test for object which is represented by service 54 * <code>com.sun.star.style.CharacterStyle</code>. <p> 55 * @see com.sun.star.style.CharacterStyle 56 */ 57 public class CharacterStyle extends TestCase { 58 private XTextDocument xTextDoc; 59 60 /** 61 * Creates text document. 62 */ 63 protected void initialize( TestParameters tParam, PrintWriter log ) { 64 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 65 try { 66 log.println( "creating a textdocument" ); 67 xTextDoc = SOF.createTextDoc( null ); 68 } catch ( com.sun.star.uno.Exception e ) { 69 e.printStackTrace( log ); 70 throw new StatusException( "Couldn't create document", e ); 71 } 72 } 73 74 /** 75 * Disposes text document. 76 */ 77 protected void cleanup( TestParameters tParam, PrintWriter log ) { 78 log.println( " disposing xTextDoc " ); 79 DesktopTools.closeDoc(xTextDoc); 80 } 81 82 /** 83 * Creating a Testenvironment for the interfaces to be tested. 84 * At first style families are gotten from a text document using 85 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is 86 * gotten from this style family using <code>XIndexAccess</code> interface. 87 * Next, method creates an instance of the service 88 * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a 89 * previously obtained style family using <code>XNameContainer</code> 90 * interface. Finally, method creates a cursor of a major text of text 91 * document and sets it's property 'CharStyleName' value to the name of 92 * previously created style's name. 93 * Object relations created : 94 * <ul> 95 * <li> <code>'PoolStyle'</code> for 96 * {@link ifc.style._XStyle} : style indexed '10' obtained from 97 * StyleFamily indexed '0' from text document using 98 * <code>XIndexAccess</code> interface.</li> 99 * </ul> 100 */ 101 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) { 102 103 TestEnvironment tEnv = null; 104 XNameAccess oSFNA = null; 105 XStyle oStyle = null; 106 XStyle oMyStyle = null; 107 108 log.println("creating a test environment"); 109 110 try { 111 log.println("getting style"); 112 XStyleFamiliesSupplier oSFS = (XStyleFamiliesSupplier) 113 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, 114 xTextDoc); 115 XNameAccess oSF = oSFS.getStyleFamilies(); 116 XIndexAccess oSFsIA = (XIndexAccess) 117 UnoRuntime.queryInterface(XIndexAccess.class, oSF); 118 oSFNA = (XNameAccess) UnoRuntime.queryInterface( 119 XNameAccess.class,oSFsIA.getByIndex(0)); 120 XIndexAccess oSFIA = (XIndexAccess) 121 UnoRuntime.queryInterface(XIndexAccess.class, oSFNA); 122 oStyle = (XStyle) UnoRuntime.queryInterface( 123 XStyle.class,oSFIA.getByIndex(0)); 124 } catch ( com.sun.star.lang.WrappedTargetException e ) { 125 log.println("Error: exception occured."); 126 e.printStackTrace(log); 127 throw new StatusException( "Couldn't create environment ", e ); 128 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) { 129 log.println("Error: exception occured."); 130 e.printStackTrace(log); 131 throw new StatusException( "Couldn't create environment ", e ); 132 } 133 134 try { 135 log.print("Creating a user-defined style... "); 136 XMultiServiceFactory oMSF = (XMultiServiceFactory) 137 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc); 138 XInterface oInt = (XInterface) 139 oMSF.createInstance("com.sun.star.style.CharacterStyle"); 140 oMyStyle = (XStyle) UnoRuntime.queryInterface(XStyle.class, oInt); 141 } catch ( com.sun.star.uno.Exception e ) { 142 log.println("Error: exception occured."); 143 e.printStackTrace(log); 144 throw new StatusException( "Couldn't create environment ", e ); 145 } 146 147 148 if (oMyStyle == null) 149 log.println("FAILED"); 150 else 151 log.println("OK"); 152 XNameContainer oSFNC = (XNameContainer) 153 UnoRuntime.queryInterface(XNameContainer.class, oSFNA); 154 155 try { 156 if ( oSFNC.hasByName("My Style") ) 157 oSFNC.removeByName("My Style"); 158 oSFNC.insertByName("My Style", oMyStyle); 159 } catch ( com.sun.star.lang.WrappedTargetException e ) { 160 e.printStackTrace(log); 161 throw new StatusException( "Couldn't create environment ", e ); 162 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 163 e.printStackTrace(log); 164 throw new StatusException( "Couldn't create environment ", e ); 165 } catch ( com.sun.star.container.NoSuchElementException e ) { 166 e.printStackTrace(log); 167 throw new StatusException( "Couldn't create environment ", e ); 168 } catch ( com.sun.star.container.ElementExistException e ) { 169 e.printStackTrace(log); 170 throw new StatusException( "Couldn't create environment ", e ); 171 } 172 173 XText oText = xTextDoc.getText(); 174 XTextCursor oCursor = oText.createTextCursor(); 175 XPropertySet xProp = (XPropertySet) 176 UnoRuntime.queryInterface(XPropertySet.class, oCursor); 177 178 try { 179 xProp.setPropertyValue("CharStyleName", oMyStyle.getName()); 180 } catch ( com.sun.star.lang.WrappedTargetException e ) { 181 e.printStackTrace( log ); 182 throw new StatusException( "Couldn't create environment ", e ); 183 } catch ( com.sun.star.lang.IllegalArgumentException e ) { 184 e.printStackTrace( log ); 185 throw new StatusException( "Couldn't create environment ", e ); 186 } catch ( com.sun.star.beans.PropertyVetoException e ) { 187 e.printStackTrace( log ); 188 throw new StatusException( "Couldn't create environment ", e ); 189 } catch ( com.sun.star.beans.UnknownPropertyException e ) { 190 e.printStackTrace( log ); 191 throw new StatusException( "Couldn't create environment ", e ); 192 } 193 194 log.println("creating a new environment for object"); 195 tEnv = new TestEnvironment(oMyStyle); 196 tEnv.addObjRelation("PoolStyle", oStyle); 197 198 XPropertySet xStyleProp = (XPropertySet) 199 UnoRuntime.queryInterface(XPropertySet.class, oMyStyle); 200 201 short exclude = PropertyAttribute.MAYBEVOID + PropertyAttribute.READONLY; 202 tEnv.addObjRelation("PropertyNames",utils.getFilteredPropertyNames(xStyleProp, (short)0, exclude)); 203 204 return tEnv; 205 } 206 207 } 208