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