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.SOfficeFactory;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XHeaderFooterContent;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.style.XStyle;
41 import com.sun.star.style.XStyleFamiliesSupplier;
42 import com.sun.star.text.XText;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 /**
49 * Test for object which is represented by service
50 * <code>com.sun.star.text.TextCursor</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::text::XTextCursor</code></li>
54 *  <li> <code>com::sun::star::text::XWordCursor</code></li>
55 *  <li> <code>com::sun::star::style::CharacterPropertiesComplex</code></li>
56 *  <li> <code>com::sun::star::text::XTextRange</code></li>
57 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
58 *  <li> <code>com::sun::star::container::XContentEnumerationAccess</code></li>
59 *  <li> <code>com::sun::star::beans::XPropertyState</code></li>
60 *  <li> <code>com::sun::star::style::CharacterProperties</code></li>
61 *  <li> <code>com::sun::star::text::XSentenceCursor</code></li>
62 *  <li> <code>com::sun::star::style::ParagraphProperties</code></li>
63 *  <li> <code>com::sun::star::text::XParagraphCursor</code></li>
64 *  <li> <code>com::sun::star::document::XDocumentInsertable</code></li>
65 *  <li> <code>com::sun::star::util::XSortable</code></li>
66 *  <li> <code>com::sun::star::style::CharacterPropertiesAsian</code></li>
67 * </ul>
68 * @see com.sun.star.text.TextCursor
69 * @see com.sun.star.text.XTextCursor
70 * @see com.sun.star.text.XWordCursor
71 * @see com.sun.star.style.CharacterPropertiesComplex
72 * @see com.sun.star.text.XTextRange
73 * @see com.sun.star.beans.XPropertySet
74 * @see com.sun.star.container.XContentEnumerationAccess
75 * @see com.sun.star.beans.XPropertyState
76 * @see com.sun.star.style.CharacterProperties
77 * @see com.sun.star.text.XSentenceCursor
78 * @see com.sun.star.style.ParagraphProperties
79 * @see com.sun.star.text.XParagraphCursor
80 * @see com.sun.star.document.XDocumentInsertable
81 * @see com.sun.star.util.XSortable
82 * @see com.sun.star.style.CharacterPropertiesAsian
83 * @see ifc.text._XTextCursor
84 * @see ifc.text._XWordCursor
85 * @see ifc.style._CharacterPropertiesComplex
86 * @see ifc.text._XTextRange
87 * @see ifc.beans._XPropertySet
88 * @see ifc.container._XContentEnumerationAccess
89 * @see ifc.beans._XPropertyState
90 * @see ifc.style._CharacterProperties
91 * @see ifc.text._XSentenceCursor
92 * @see ifc.style._ParagraphProperties
93 * @see ifc.text._XParagraphCursor
94 * @see ifc.document._XDocumentInsertable
95 * @see ifc.util._XSortable
96 * @see ifc.style._CharacterPropertiesAsian
97 */
98 public class ScHeaderFooterTextCursor extends TestCase {
99     static XSpreadsheetDocument xSpreadsheetDoc;
100 
101     /**
102     * Creates Spreadsheet document.
103     */
initialize( TestParameters tParam, PrintWriter log )104     protected void initialize( TestParameters tParam, PrintWriter log ) {
105         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
106 
107         try {
108             log.println( "creating a Spreadsheet document" );
109             xSpreadsheetDoc = SOF.createCalcDoc(null);
110         } catch ( com.sun.star.uno.Exception e ) {
111             // Some exception occured.FAILED
112             e.printStackTrace( log );
113             throw new StatusException( "Couldn't create document", e );
114         }
115     }
116 
117     /**
118     * Disposes Spreadsheet document.
119     */
cleanup( TestParameters tParam, PrintWriter log )120     protected void cleanup( TestParameters tParam, PrintWriter log ) {
121         log.println( "    disposing xSheetDoc " );
122         XComponent oComp = (XComponent)
123             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ;
124         util.DesktopTools.closeDoc(oComp);
125     }
126 
127     /**
128     * Creating a Testenvironment for the interfaces to be tested.
129     * Retrieves the collection of style families available in the document
130     * using the interface <code>XStyleFamiliesSupplier</code>.
131     * Obtains default style from the style family <code>'PageStyles'</code>.
132     * Retrieves value of the property <code>'RightPageHeaderContent'</code>.
133     * Sets some string for the text which is printed in the center part of the
134     * header or footer using the interface <code>XHeaderFooterContent</code>
135     * and sets new value of the property <code>'RightPageHeaderContent'</code>.
136     * Creates text cursor for the text which is printed in the center part of
137     * the header or footer. This text cursor is the instance of the service
138     * <code>com.sun.star.text.TextCursor</code>.
139     * </ul>
140     */
createTestEnvironment(TestParameters tParam, PrintWriter log)141     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
142 
143         XInterface oObj = null;
144         XPropertySet PropSet;
145         XNameAccess PageStyles = null;
146         XStyle StdStyle = null;
147 
148         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
149             UnoRuntime.queryInterface(
150                 XStyleFamiliesSupplier.class,
151                 xSpreadsheetDoc );
152 
153         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
154         try{
155             PageStyles = (XNameAccess) AnyConverter.toObject(
156                 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
157             StdStyle = (XStyle) AnyConverter.toObject(
158                         new Type(XStyle.class),PageStyles.getByName("Default"));
159         } catch(com.sun.star.lang.WrappedTargetException e){
160             e.printStackTrace(log);
161             throw new StatusException("Couldn't get by name", e);
162         } catch(com.sun.star.container.NoSuchElementException e){
163             e.printStackTrace(log);
164             throw new StatusException("Couldn't get by name", e);
165         } catch(com.sun.star.lang.IllegalArgumentException e){
166             e.printStackTrace(log);
167             throw new StatusException("Couldn't get by name", e);
168         }
169 
170         //get the property-set
171         PropSet = (XPropertySet)
172             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
173 
174         XHeaderFooterContent RPHC = null;
175         // creation of testobject here
176         // first we write what we are intend to do to log file
177         log.println( "creating a test environment" );
178         try {
179             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
180                 new Type(XHeaderFooterContent.class),
181                     PropSet.getPropertyValue("RightPageHeaderContent"));
182         } catch(com.sun.star.lang.WrappedTargetException e){
183             e.printStackTrace(log);
184             throw new StatusException("Couldn't get HeaderContent", e);
185         } catch(com.sun.star.beans.UnknownPropertyException e){
186             e.printStackTrace(log);
187             throw new StatusException("Couldn't get HeaderContent", e);
188         } catch(com.sun.star.lang.IllegalArgumentException e){
189             e.printStackTrace(log);
190             throw new StatusException("Couldn't get HeaderContent", e);
191         }
192 
193         XText center = RPHC.getCenterText();
194         center.setString("CENTER");
195 
196         try {
197             PropSet.setPropertyValue("RightPageHeaderContent",RPHC);
198         } catch (com.sun.star.lang.WrappedTargetException e) {
199             e.printStackTrace(log);
200             throw new StatusException("Couldn't set HeaderContent", e);
201         } catch (com.sun.star.lang.IllegalArgumentException e) {
202             e.printStackTrace(log);
203             throw new StatusException("Couldn't set HeaderContent", e);
204         } catch (com.sun.star.beans.PropertyVetoException e) {
205             e.printStackTrace(log);
206             throw new StatusException("Couldn't set HeaderContent", e);
207         } catch (com.sun.star.beans.UnknownPropertyException e) {
208             e.printStackTrace(log);
209             throw new StatusException("Couldn't set HeaderContent", e);
210         }
211 
212         // create testobject here
213         oObj = center.createTextCursor();
214         TestEnvironment tEnv = new TestEnvironment(oObj);
215 
216         // add relation for XTextRange
217         tEnv.addObjRelation("XTEXT",
218             UnoRuntime.queryInterface(XText.class,center));
219 
220         return tEnv;
221 
222     } // finish method getTestEnvironment
223 
224 }    // finish class ScHeaderFooterTextCursor
225