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.sheet.HeaderFooterContent</code>. <p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>com::sun::star::sheet::XHeaderFooterContent</code></li>
54 * </ul>
55 * @see com.sun.star.sheet.HeaderFooterContent
56 * @see com.sun.star.sheet.XHeaderFooterContent
57 * @see ifc.sheet._XHeaderFooterContent
58 */
59 public class ScHeaderFooterContentObj extends TestCase {
60     static XSpreadsheetDocument xSpreadsheetDoc;
61 
62     /**
63     * Creates Spreadsheet document.
64     */
initialize( TestParameters tParam, PrintWriter log )65     protected void initialize( TestParameters tParam, PrintWriter log ) {
66         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
67 
68         try {
69             log.println( "creating a Spreadsheet document" );
70             xSpreadsheetDoc = SOF.createCalcDoc(null);
71         } catch ( com.sun.star.uno.Exception e ) {
72             // Some exception occures.FAILED
73             e.printStackTrace( log );
74             throw new StatusException( "Couldn't create document", e );
75         }
76 
77     }
78 
79     /**
80     * Disposes Spreadsheet document.
81     */
cleanup( TestParameters tParam, PrintWriter log )82     protected void cleanup( TestParameters tParam, PrintWriter log ) {
83         log.println( "    disposing xSheetDoc " );
84         XComponent oComp = (XComponent)
85             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc);
86         util.DesktopTools.closeDoc(oComp);
87     }
88 
89     /**
90     * Creating a Testenvironment for the interfaces to be tested.
91     * Retrieves the collection of style families available in the document
92     * using the interface <code>XStyleFamiliesSupplier</code>.
93     * Obtains default style from the style family <code>'PageStyles'</code>.
94     * Retrieves value of the property <code>'RightPageHeaderContent'</code>.
95     * Sets some text for every part of header or footer using the interface
96     * <code>XHeaderFooterContent</code> and sets new value of the property
97     * <code>'RightPageHeaderContent'</code>. The value of this property is the
98     * instance of the service <code>com.sun.star.sheet.HeaderFooterContent</code>.
99     * @see com.sun.star.style.XStyleFamiliesSupplier
100     * @see com.sun.star.sheet.XHeaderFooterContent
101     * @see com.sun.star.sheet.HeaderFooterContent
102     */
createTestEnvironment(TestParameters tParam, PrintWriter log)103     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
104 
105         XInterface oObj = null;
106         XPropertySet PropSet;
107         XNameAccess PageStyles = null;
108         XStyle StdStyle = null;
109 
110         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
111             UnoRuntime.queryInterface(
112                 XStyleFamiliesSupplier.class,
113                 xSpreadsheetDoc );
114         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
115         try{
116             PageStyles = (XNameAccess) AnyConverter.toObject(
117                 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
118             StdStyle = (XStyle) AnyConverter.toObject(
119                         new Type(XStyle.class),PageStyles.getByName("Default"));
120         } catch(com.sun.star.lang.WrappedTargetException e){
121             e.printStackTrace(log);
122             throw new StatusException("Couldn't get by name", e);
123         } catch(com.sun.star.container.NoSuchElementException e){
124             e.printStackTrace(log);
125             throw new StatusException("Couldn't get by name", e);
126         } catch(com.sun.star.lang.IllegalArgumentException e){
127             e.printStackTrace(log);
128             throw new StatusException("Couldn't get by name", e);
129         }
130 
131         //get the property-set
132         PropSet = (XPropertySet)
133             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
134 
135         XHeaderFooterContent RPHC = null;
136         // creation of testobject here
137         // first we write what we are intend to do to log file
138         log.println( "creating a test environment" );
139         try {
140             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
141                 new Type(XHeaderFooterContent.class),
142                     PropSet.getPropertyValue("RightPageHeaderContent"));
143         } catch(com.sun.star.lang.WrappedTargetException e){
144             e.printStackTrace(log);
145             throw new StatusException("Couldn't get HeaderContent", e);
146         } catch(com.sun.star.beans.UnknownPropertyException e){
147             e.printStackTrace(log);
148             throw new StatusException("Couldn't get HeaderContent", e);
149         } catch(com.sun.star.lang.IllegalArgumentException e){
150             e.printStackTrace(log);
151             throw new StatusException("Couldn't get HeaderContent", e);
152         }
153 
154         XText center = RPHC.getCenterText();
155         XText left = RPHC.getLeftText();
156         XText right = RPHC.getRightText();
157 
158         center.setString("CENTER");
159         left.setString("LEFT");
160         right.setString("RIGHT");
161 
162         try {
163             PropSet.setPropertyValue("RightPageHeaderContent", RPHC);
164         } catch (com.sun.star.lang.WrappedTargetException e) {
165             e.printStackTrace(log);
166             throw new StatusException("Couldn't set HeaderContent", e);
167         } catch (com.sun.star.lang.IllegalArgumentException e) {
168             e.printStackTrace(log);
169             throw new StatusException("Couldn't set HeaderContent", e);
170         } catch (com.sun.star.beans.PropertyVetoException e) {
171             e.printStackTrace(log);
172             throw new StatusException("Couldn't set HeaderContent", e);
173         } catch (com.sun.star.beans.UnknownPropertyException e) {
174             e.printStackTrace(log);
175             throw new StatusException("Couldn't set HeaderContent", e);
176         }
177 
178         // create testobject here
179         oObj = RPHC;
180         TestEnvironment tEnv = new TestEnvironment(oObj);
181 
182         return tEnv;
183 
184     } // finish method getTestEnvironment
185 
186 }    // finish class ScHeaderFooterContentObj
187