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._sc; 29 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.DefaultDsc; 37 import util.InstCreator; 38 import util.ParagraphDsc; 39 import util.SOfficeFactory; 40 41 import com.sun.star.beans.XPropertySet; 42 import com.sun.star.container.XNameAccess; 43 import com.sun.star.lang.XComponent; 44 import com.sun.star.lang.XMultiServiceFactory; 45 import com.sun.star.sheet.XHeaderFooterContent; 46 import com.sun.star.sheet.XSpreadsheetDocument; 47 import com.sun.star.style.XStyle; 48 import com.sun.star.style.XStyleFamiliesSupplier; 49 import com.sun.star.text.ControlCharacter; 50 import com.sun.star.text.XText; 51 import com.sun.star.text.XTextCursor; 52 import com.sun.star.text.XTextRange; 53 import com.sun.star.uno.AnyConverter; 54 import com.sun.star.uno.Type; 55 import com.sun.star.uno.UnoRuntime; 56 import com.sun.star.uno.XInterface; 57 58 /** 59 * Test for object which is represented by service 60 * <code>com.sun.star.text.Text</code>. <p> 61 * Object implements the following interfaces : 62 * <ul> 63 * <li> <code>com::sun::star::text::XTextRangeMover</code></li> 64 * <li> <code>com::sun::star::text::XSimpleText</code></li> 65 * <li> <code>com::sun::star::text::XTextRange</code></li> 66 * <li> <code>com::sun::star::text::XRelativeTextContentInsert</code></li> 67 * <li> <code>com::sun::star::text::XTextRangeCompare</code></li> 68 * <li> <code>com::sun::star::container::XElementAccess</code></li> 69 * <li> <code>com::sun::star::container::XEnumerationAccess</code></li> 70 * <li> <code>com::sun::star::text::XText</code></li> 71 * </ul> 72 * @see com.sun.star.text.Text 73 * @see com.sun.star.text.XTextRangeMover 74 * @see com.sun.star.text.XSimpleText 75 * @see com.sun.star.text.XTextRange 76 * @see com.sun.star.text.XRelativeTextContentInsert 77 * @see com.sun.star.text.XTextRangeCompare 78 * @see com.sun.star.container.XElementAccess 79 * @see com.sun.star.container.XEnumerationAccess 80 * @see com.sun.star.text.XText 81 * @see ifc.text._XTextRangeMover 82 * @see ifc.text._XSimpleText 83 * @see ifc.text._XTextRange 84 * @see ifc.text._XRelativeTextContentInsert 85 * @see ifc.text._XTextRangeCompare 86 * @see ifc.container._XElementAccess 87 * @see ifc.container._XEnumerationAccess 88 * @see ifc.text._XText 89 */ 90 public class ScHeaderFooterTextObj extends TestCase { 91 92 static XSpreadsheetDocument xSpreadsheetDoc; 93 94 /** 95 * Creates Spreadsheet document. 96 */ 97 protected void initialize( TestParameters tParam, PrintWriter log ) { 98 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() ); 99 100 try { 101 log.println( "creating a Spreadsheet document" ); 102 xSpreadsheetDoc = SOF.createCalcDoc(null); 103 } catch ( com.sun.star.uno.Exception e ) { 104 // Some exception occures.FAILED 105 e.printStackTrace( log ); 106 throw new StatusException( "Couldn't create document", e ); 107 } 108 109 } 110 111 /** 112 * Disposes Spreadsheet document. 113 */ 114 protected void cleanup( TestParameters tParam, PrintWriter log ) { 115 log.println( " disposing xSheetDoc " ); 116 XComponent oComp = (XComponent) 117 UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc); 118 util.DesktopTools.closeDoc(oComp); 119 } 120 121 /** 122 * Creating a Testenvironment for the interfaces to be tested. 123 * Retrieves the collection of style families available in the document 124 * using the interface <code>XStyleFamiliesSupplier</code>. 125 * Obtains default style from the style family <code>'PageStyles'</code>. 126 * Retrieves value of the property <code>'RightPageHeaderContent'</code>. 127 * Creates text cursor for the text which is printed in the center part of 128 * the header or footer using the interface <code>XHeaderFooterContent</code>. 129 * Insert some lines using the created cursor and sets new value of the 130 * property <code>'RightPageHeaderContent'</code>. The value of the property 131 * <code>'RightPageHeaderContent'</code> is the instance of the service 132 * <code>com.sun.star.text.Text</code>. 133 * Object relations created : 134 * <ul> 135 * <li> <code>'RangeForMove'</code> for 136 * {@link ifc.text._XTextRangeMover} (the range to be moved)</li> 137 * <li> <code>'XTextRange'</code> for 138 * {@link ifc.text._XTextRangeMover} (the range that includes moving 139 * range)</li> 140 * <li> <code>'XTEXTINFO'</code> for 141 * {@link ifc.text._XRelativeTextContentInsert}(the instance creator 142 * which can create instances of some text content service)</li> 143 * <li> <code>'PARA'</code> for 144 * {@link ifc.text._XRelativeTextContentInsert}(the instance creator 145 * which can create instances of <code>com.sun.star.text.Paragraph</code> 146 * service)</li> 147 * </ul> 148 * @see com.sun.star.text.Text 149 */ 150 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) { 151 152 XInterface oObj = null; 153 XPropertySet PropSet; 154 XNameAccess PageStyles = null; 155 XStyle StdStyle = null; 156 157 XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier) 158 UnoRuntime.queryInterface( 159 XStyleFamiliesSupplier.class, 160 xSpreadsheetDoc ); 161 XNameAccess StyleFamNames = StyleFam.getStyleFamilies(); 162 try{ 163 PageStyles = (XNameAccess) AnyConverter.toObject( 164 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles")); 165 StdStyle = (XStyle) AnyConverter.toObject( 166 new Type(XStyle.class),PageStyles.getByName("Default")); 167 } catch(com.sun.star.lang.WrappedTargetException e){ 168 e.printStackTrace(log); 169 throw new StatusException("Couldn't get by name", e); 170 } catch(com.sun.star.container.NoSuchElementException e){ 171 e.printStackTrace(log); 172 throw new StatusException("Couldn't get by name", e); 173 } catch(com.sun.star.lang.IllegalArgumentException e){ 174 e.printStackTrace(log); 175 throw new StatusException("Couldn't get by name", e); 176 } 177 178 //get the property-set 179 PropSet = (XPropertySet) 180 UnoRuntime.queryInterface(XPropertySet.class, StdStyle); 181 182 XHeaderFooterContent RPHC = null; 183 // creation of testobject here 184 // first we write what we are intend to do to log file 185 log.println( "creating a test environment" ); 186 try { 187 RPHC = (XHeaderFooterContent) AnyConverter.toObject( 188 new Type(XHeaderFooterContent.class), 189 PropSet.getPropertyValue("RightPageHeaderContent")); 190 } catch(com.sun.star.lang.WrappedTargetException e){ 191 e.printStackTrace(log); 192 throw new StatusException("Couldn't get HeaderContent", e); 193 } catch(com.sun.star.beans.UnknownPropertyException e){ 194 e.printStackTrace(log); 195 throw new StatusException("Couldn't get HeaderContent", e); 196 } catch(com.sun.star.lang.IllegalArgumentException e){ 197 e.printStackTrace(log); 198 throw new StatusException("Couldn't get HeaderContent", e); 199 } 200 201 XText center = RPHC.getCenterText(); 202 203 XTextRange text_to_move = null; 204 205 log.println( "inserting some lines" ); 206 try { 207 XTextCursor oCursor = center.createTextCursor(); 208 center.insertControlCharacter( 209 oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 210 center.insertControlCharacter( 211 oCursor, ControlCharacter.LINE_BREAK, false ); 212 center.insertString(oCursor,"Paragraph 1", false); 213 center.insertString(oCursor,": ScHeaderFooterTextObj", false); 214 center.insertControlCharacter( 215 oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 216 center.insertString(oCursor, "THE QUICK BROWN FOX JUMPS OVER THE" + 217 " LAZY DOG: ScHeaderFooterTextObj", false ); 218 center.insertControlCharacter( 219 oCursor, ControlCharacter.PARAGRAPH_BREAK, false ); 220 center.insertControlCharacter( 221 oCursor, ControlCharacter.LINE_BREAK, false ); 222 oCursor.setString("TextForMove"); 223 text_to_move = oCursor; 224 225 XTextCursor oCursor1 = center.createTextCursorByRange(center.getEnd()); 226 center.insertString(oCursor1,"Paragraph 2", false); 227 center.insertString(oCursor1,": ScHeaderFooterTextObj", false); 228 center.insertControlCharacter( 229 oCursor1, ControlCharacter.PARAGRAPH_BREAK, false ); 230 center.insertString( oCursor1, "THE QUICK BROWN FOX JUMPS OVER THE" + 231 " LAZY DOG: ScHeaderFooterTextObj", false); 232 center.insertControlCharacter( 233 oCursor1, ControlCharacter.PARAGRAPH_BREAK, false ); 234 center.insertControlCharacter( 235 oCursor1, ControlCharacter.LINE_BREAK, false ); 236 } catch(com.sun.star.lang.IllegalArgumentException e){ 237 e.printStackTrace(log); 238 throw new StatusException("Couldn't insert Text ", e); 239 } 240 241 try { 242 PropSet.setPropertyValue("RightPageHeaderContent", RPHC); 243 } catch (com.sun.star.lang.WrappedTargetException e) { 244 e.printStackTrace(log); 245 throw new StatusException("Couldn't set HeaderContent", e); 246 } catch (com.sun.star.lang.IllegalArgumentException e) { 247 e.printStackTrace(log); 248 throw new StatusException("Couldn't set HeaderContent", e); 249 } catch (com.sun.star.beans.PropertyVetoException e) { 250 e.printStackTrace(log); 251 throw new StatusException("Couldn't set HeaderContent", e); 252 } catch (com.sun.star.beans.UnknownPropertyException e) { 253 e.printStackTrace(log); 254 throw new StatusException("Couldn't set HeaderContent", e); 255 } 256 257 // create testobject here 258 oObj = center; 259 TestEnvironment tEnv = new TestEnvironment(oObj); 260 261 DefaultDsc tDsc = new DefaultDsc( 262 "com.sun.star.text.XTextContent","com.sun.star.text.TextField.Time"); 263 264 log.println( "adding InstCreator object" ); 265 tEnv.addObjRelation( 266 "XTEXTINFO", new InstCreator( xSpreadsheetDoc, tDsc ) ); 267 268 ParagraphDsc pDsc = new ParagraphDsc(); 269 tEnv.addObjRelation( "PARA", new InstCreator(xSpreadsheetDoc, pDsc) ); 270 271 log.println("adding TextRange for XTextRangeMover" ); 272 tEnv.addObjRelation("RangeForMove", text_to_move); 273 tEnv.addObjRelation("XTextRange", RPHC.getCenterText()); 274 275 return tEnv; 276 277 } // finish method getTestEnvironment 278 } // finish class ScHeaderFooterTextObj 279