Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScHeaderFooterTextObj.java
blob990d6183a11de2e6fae9714c3809271ebb859bab
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package mod._sc;
21 import java.io.PrintWriter;
23 import lib.TestCase;
24 import lib.TestEnvironment;
25 import lib.TestParameters;
26 import util.DefaultDsc;
27 import util.InstCreator;
28 import util.ParagraphDsc;
29 import util.SOfficeFactory;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.container.XNameAccess;
33 import com.sun.star.lang.XComponent;
34 import com.sun.star.sheet.XHeaderFooterContent;
35 import com.sun.star.sheet.XSpreadsheetDocument;
36 import com.sun.star.style.XStyle;
37 import com.sun.star.style.XStyleFamiliesSupplier;
38 import com.sun.star.text.ControlCharacter;
39 import com.sun.star.text.XText;
40 import com.sun.star.text.XTextCursor;
41 import com.sun.star.text.XTextRange;
42 import com.sun.star.uno.AnyConverter;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
47 /**
48 * Test for object which is represented by service
49 * <code>com.sun.star.text.Text</code>. <p>
50 * Object implements the following interfaces :
51 * <ul>
52 * <li> <code>com::sun::star::text::XTextRangeMover</code></li>
53 * <li> <code>com::sun::star::text::XSimpleText</code></li>
54 * <li> <code>com::sun::star::text::XTextRange</code></li>
55 * <li> <code>com::sun::star::text::XRelativeTextContentInsert</code></li>
56 * <li> <code>com::sun::star::text::XTextRangeCompare</code></li>
57 * <li> <code>com::sun::star::container::XElementAccess</code></li>
58 * <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
59 * <li> <code>com::sun::star::text::XText</code></li>
60 * </ul>
61 * @see com.sun.star.text.Text
62 * @see com.sun.star.text.XTextRangeMover
63 * @see com.sun.star.text.XSimpleText
64 * @see com.sun.star.text.XTextRange
65 * @see com.sun.star.text.XRelativeTextContentInsert
66 * @see com.sun.star.text.XTextRangeCompare
67 * @see com.sun.star.container.XElementAccess
68 * @see com.sun.star.container.XEnumerationAccess
69 * @see com.sun.star.text.XText
70 * @see ifc.text._XTextRangeMover
71 * @see ifc.text._XSimpleText
72 * @see ifc.text._XTextRange
73 * @see ifc.text._XRelativeTextContentInsert
74 * @see ifc.text._XTextRangeCompare
75 * @see ifc.container._XElementAccess
76 * @see ifc.container._XEnumerationAccess
77 * @see ifc.text._XText
79 public class ScHeaderFooterTextObj extends TestCase {
81 private XSpreadsheetDocument xSpreadsheetDoc;
83 /**
84 * Creates Spreadsheet document.
86 @Override
87 protected void initialize( TestParameters tParam, PrintWriter log ) throws Exception {
88 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
90 log.println( "creating a Spreadsheet document" );
91 xSpreadsheetDoc = SOF.createCalcDoc(null);
94 /**
95 * Disposes Spreadsheet document.
97 @Override
98 protected void cleanup( TestParameters tParam, PrintWriter log ) {
99 log.println( " disposing xSheetDoc " );
100 XComponent oComp = UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc);
101 util.DesktopTools.closeDoc(oComp);
105 * Creating a TestEnvironment for the interfaces to be tested.
106 * Retrieves the collection of style families available in the document
107 * using the interface <code>XStyleFamiliesSupplier</code>.
108 * Obtains default style from the style family <code>'PageStyles'</code>.
109 * Retrieves value of the property <code>'RightPageHeaderContent'</code>.
110 * Creates text cursor for the text which is printed in the center part of
111 * the header or footer using the interface <code>XHeaderFooterContent</code>.
112 * Insert some lines using the created cursor and sets new value of the
113 * property <code>'RightPageHeaderContent'</code>. The value of the property
114 * <code>'RightPageHeaderContent'</code> is the instance of the service
115 * <code>com.sun.star.text.Text</code>.
116 * Object relations created :
117 * <ul>
118 * <li> <code>'RangeForMove'</code> for
119 * {@link ifc.text._XTextRangeMover} (the range to be moved)</li>
120 * <li> <code>'XTextRange'</code> for
121 * {@link ifc.text._XTextRangeMover} (the range that includes moving
122 * range)</li>
123 * <li> <code>'XTEXTINFO'</code> for
124 * {@link ifc.text._XRelativeTextContentInsert}(the instance creator
125 * which can create instances of some text content service)</li>
126 * <li> <code>'PARA'</code> for
127 * {@link ifc.text._XRelativeTextContentInsert}(the instance creator
128 * which can create instances of <code>com.sun.star.text.Paragraph</code>
129 * service)</li>
130 * </ul>
131 * @see com.sun.star.text.Text
133 @Override
134 protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) throws Exception {
136 XInterface oObj = null;
137 XPropertySet PropSet;
138 XNameAccess PageStyles = null;
139 XStyle StdStyle = null;
141 XStyleFamiliesSupplier StyleFam = UnoRuntime.queryInterface(
142 XStyleFamiliesSupplier.class,
143 xSpreadsheetDoc );
144 XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
145 PageStyles = (XNameAccess) AnyConverter.toObject(
146 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
147 StdStyle = (XStyle) AnyConverter.toObject(
148 new Type(XStyle.class),PageStyles.getByName("Default"));
150 //get the property-set
151 PropSet = UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
153 XHeaderFooterContent RPHC = null;
154 // creation of testobject here
155 // first we write what we are intend to do to log file
156 log.println( "creating a test environment" );
157 RPHC = (XHeaderFooterContent) AnyConverter.toObject(
158 new Type(XHeaderFooterContent.class),
159 PropSet.getPropertyValue("RightPageHeaderContent"));
161 XText center = RPHC.getCenterText();
163 XTextRange text_to_move = null;
165 log.println( "inserting some lines" );
166 XTextCursor oCursor = center.createTextCursor();
167 center.insertControlCharacter(
168 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
169 center.insertControlCharacter(
170 oCursor, ControlCharacter.LINE_BREAK, false );
171 center.insertString(oCursor,"Paragraph 1", false);
172 center.insertString(oCursor,": ScHeaderFooterTextObj", false);
173 center.insertControlCharacter(
174 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
175 center.insertString(oCursor, "THE QUICK BROWN FOX JUMPS OVER THE" +
176 " LAZY DOG: ScHeaderFooterTextObj", false );
177 center.insertControlCharacter(
178 oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
179 center.insertControlCharacter(
180 oCursor, ControlCharacter.LINE_BREAK, false );
181 oCursor.setString("TextForMove");
182 text_to_move = oCursor;
184 XTextCursor oCursor1 = center.createTextCursorByRange(center.getEnd());
185 center.insertString(oCursor1,"Paragraph 2", false);
186 center.insertString(oCursor1,": ScHeaderFooterTextObj", false);
187 center.insertControlCharacter(
188 oCursor1, ControlCharacter.PARAGRAPH_BREAK, false );
189 center.insertString( oCursor1, "THE QUICK BROWN FOX JUMPS OVER THE" +
190 " LAZY DOG: ScHeaderFooterTextObj", false);
191 center.insertControlCharacter(
192 oCursor1, ControlCharacter.PARAGRAPH_BREAK, false );
193 center.insertControlCharacter(
194 oCursor1, ControlCharacter.LINE_BREAK, false );
196 PropSet.setPropertyValue("RightPageHeaderContent", RPHC);
198 // create testobject here
199 oObj = center;
200 TestEnvironment tEnv = new TestEnvironment(oObj);
202 DefaultDsc tDsc = new DefaultDsc(
203 "com.sun.star.text.XTextContent","com.sun.star.text.TextField.Time");
205 log.println( "adding InstCreator object" );
206 tEnv.addObjRelation(
207 "XTEXTINFO", new InstCreator( xSpreadsheetDoc, tDsc ) );
209 ParagraphDsc pDsc = new ParagraphDsc();
210 tEnv.addObjRelation( "PARA", new InstCreator(xSpreadsheetDoc, pDsc) );
212 log.println("adding TextRange for XTextRangeMover" );
213 tEnv.addObjRelation("RangeForMove", text_to_move);
214 tEnv.addObjRelation("XTextRange", RPHC.getCenterText());
216 return tEnv;
218 } // finish method getTestEnvironment
219 } // finish class ScHeaderFooterTextObj