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.text.XTextContent;
44 import com.sun.star.text.XTextCursor;
45 import com.sun.star.uno.AnyConverter;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.UnoRuntime;
48 import com.sun.star.uno.XInterface;
49 
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.text.TextField</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 *  <li> <code>com::sun::star::lang::XComponent</code></li>
56 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
57 *  <li> <code>com::sun::star::text::XTextField</code></li>
58 *  <li> <code>com::sun::star::text::XTextContent</code></li>
59 *  <li> <code>com::sun::star::text::TextContent</code></li>
60 * </ul>
61 * @see com.sun.star.text.TextField
62 * @see com.sun.star.lang.XComponent
63 * @see com.sun.star.beans.XPropertySet
64 * @see com.sun.star.text.XTextField
65 * @see com.sun.star.text.XTextContent
66 * @see com.sun.star.text.TextContent
67 * @see ifc.lang._XComponent
68 * @see ifc.beans._XPropertySet
69 * @see ifc.text._XTextField
70 * @see ifc.text._XTextContent
71 * @see ifc.text._TextContent
72 */
73 public class ScHeaderFieldObj extends TestCase {
74     static XSpreadsheetDocument xSpreadsheetDoc;
75 
76     /**
77     * Creates Spreadsheet document.
78     */
initialize( TestParameters tParam, PrintWriter log )79     protected void initialize( TestParameters tParam, PrintWriter log ) {
80         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
81 
82         try {
83             log.println( "creating a Spreadsheet document" );
84             xSpreadsheetDoc = SOF.createCalcDoc(null);
85         } catch ( com.sun.star.uno.Exception e ) {
86             // Some exception occures.FAILED
87             e.printStackTrace( log );
88             throw new StatusException( "Couldn't create document", e );
89         }
90 
91     }
92 
93     /**
94     * Disposes Spreadsheet document.
95     */
cleanup( TestParameters tParam, PrintWriter log )96     protected void cleanup( TestParameters tParam, PrintWriter log ) {
97         log.println( "    disposing xSheetDoc " );
98         XComponent oComp = (XComponent)
99             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc);
100         util.DesktopTools.closeDoc(oComp);
101     }
102 
103     /**
104     * Creating a Testenvironment for the interfaces to be tested.
105     * Retrieves the collection of style families available in the document
106     * using the interface <code>XStyleFamiliesSupplier</code>.
107     * Obtains default style from the style family <code>'PageStyles'</code>.
108     * Retrieves the interface <code>XHeaderFooterContent</code> from the style
109     * using the property <code>'RightPageHeaderContent'</code>. Creates the
110     * instance of the service <code>com.sun.star.text.TextField.Time</code> and
111     * the instance of the service <code>com.sun.star.text.TextField.Date</code>
112     * Obtains the text (the interface <code>XText</code>) which is printed in
113     * the left part of the header or footer and inserts in it's content
114     * the second created instance.
115     * Object relations created :
116     * <ul>
117     *  <li> <code>'CONTENT'</code> for
118     *      {@link ifc.text._XTextContent}(the interface <code>XTextContent</code>
119     *      that was queried from the second created instance) </li>
120     *  <li> <code>'TEXT'</code> for
121     *      {@link ifc.text._XTextContent}(the the text which is printed in the
122     *       right part of the header or footer) </li>
123     * </ul>
124     * @see com.sun.star.style.XStyleFamiliesSupplier
125     * @see com.sun.star.sheet.XHeaderFooterContent
126     * @see com.sun.star.text.XText
127     * @see com.sun.star.text.XTextContent
128     */
createTestEnvironment(TestParameters tParam, PrintWriter log)129     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
130 
131         XInterface oObj = null;
132         XPropertySet PropSet;
133         XNameAccess PageStyles = null;
134         XStyle StdStyle = null;
135         XTextContent oContent = null;
136         XInterface aField = null;
137 
138         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
139             UnoRuntime.queryInterface(
140                 XStyleFamiliesSupplier.class,
141                 xSpreadsheetDoc );
142 
143         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
144         try{
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"));
149         } catch(com.sun.star.lang.WrappedTargetException e){
150             e.printStackTrace(log);
151             throw new StatusException("Couldn't get by name", e);
152         } catch(com.sun.star.container.NoSuchElementException e){
153             e.printStackTrace(log);
154             throw new StatusException("Couldn't get by name", e);
155         } catch(com.sun.star.lang.IllegalArgumentException e){
156             e.printStackTrace(log);
157             throw new StatusException("Couldn't get by name", e);
158         }
159 
160         //get the property-set
161         PropSet = (XPropertySet)
162             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
163 
164         XHeaderFooterContent RPHC = null;
165         // creation of testobject here
166         // first we write what we are intend to do to log file
167         log.println( "creating a test environment" );
168         try {
169             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
170                 new Type(XHeaderFooterContent.class),
171                     PropSet.getPropertyValue("RightPageHeaderContent"));
172         } catch (com.sun.star.lang.WrappedTargetException e) {
173             e.printStackTrace(log);
174             throw new StatusException("Couldn't get HeaderContent", e);
175         } catch (com.sun.star.beans.UnknownPropertyException e) {
176             e.printStackTrace(log);
177             throw new StatusException("Couldn't get HeaderContent", e);
178         } catch (com.sun.star.lang.IllegalArgumentException e) {
179             e.printStackTrace(log);
180             throw new StatusException("Couldn't get HeaderContent", e);
181         }
182 
183         XText left = RPHC.getLeftText();
184 
185         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
186             UnoRuntime.queryInterface(
187                 XMultiServiceFactory.class,
188                 xSpreadsheetDoc );
189 
190         XTextContent the_Field = null;
191         try {
192             oObj = (XInterface)
193                 oDocMSF.createInstance( "com.sun.star.text.TextField.Time" );
194 
195             the_Field = (XTextContent)
196                 UnoRuntime.queryInterface(XTextContent.class,oObj);
197 
198             aField = (XInterface)
199                 oDocMSF.createInstance("com.sun.star.text.TextField.Date");
200         } catch(com.sun.star.uno.Exception e) {
201             e.printStackTrace(log);
202             throw new StatusException("Couldn't create instance", e);
203         }
204 
205         oContent = (XTextContent)
206             UnoRuntime.queryInterface(XTextContent.class, aField);
207 
208         XTextCursor the_Cursor = left.createTextCursor();
209 
210         try {
211             left.insertTextContent(the_Cursor,the_Field, false);
212             PropSet.setPropertyValue("RightPageHeaderContent", RPHC);
213         } catch(com.sun.star.lang.IllegalArgumentException e) {
214             e.printStackTrace(log);
215             throw new StatusException("Couldn't create a test environment", e);
216         } catch(com.sun.star.lang.WrappedTargetException e) {
217             e.printStackTrace(log);
218             throw new StatusException("Couldn't create a test environment", e);
219         } catch(com.sun.star.beans.PropertyVetoException e) {
220             e.printStackTrace(log);
221             throw new StatusException("Couldn't create a test environment", e);
222         } catch(com.sun.star.beans.UnknownPropertyException e) {
223             e.printStackTrace(log);
224             throw new StatusException("Couldn't create a test environment", e);
225         }
226 
227         TestEnvironment tEnv = new TestEnvironment(oObj);
228 
229         tEnv.addObjRelation("CONTENT",oContent);
230         tEnv.addObjRelation("TEXT", RPHC.getRightText());
231 
232         return tEnv;
233 
234     } // finish method getTestEnvironment
235 
236 }    // finish class ScHeaderFieldObj
237 
238