1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._sc;
29 
30 import java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.container.XNameAccess;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.sheet.XHeaderFooterContent;
43 import com.sun.star.sheet.XSpreadsheetDocument;
44 import com.sun.star.style.XStyle;
45 import com.sun.star.style.XStyleFamiliesSupplier;
46 import com.sun.star.text.XText;
47 import com.sun.star.text.XTextContent;
48 import com.sun.star.text.XTextCursor;
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.TextField</code>. <p>
57 * Object implements the following interfaces :
58 * <ul>
59 *  <li> <code>com::sun::star::lang::XComponent</code></li>
60 *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
61 *  <li> <code>com::sun::star::text::XTextField</code></li>
62 *  <li> <code>com::sun::star::text::XTextContent</code></li>
63 *  <li> <code>com::sun::star::text::TextContent</code></li>
64 * </ul>
65 * @see com.sun.star.text.TextField
66 * @see com.sun.star.lang.XComponent
67 * @see com.sun.star.beans.XPropertySet
68 * @see com.sun.star.text.XTextField
69 * @see com.sun.star.text.XTextContent
70 * @see com.sun.star.text.TextContent
71 * @see ifc.lang._XComponent
72 * @see ifc.beans._XPropertySet
73 * @see ifc.text._XTextField
74 * @see ifc.text._XTextContent
75 * @see ifc.text._TextContent
76 */
77 public class ScHeaderFieldObj extends TestCase {
78     static XSpreadsheetDocument xSpreadsheetDoc;
79 
80     /**
81     * Creates Spreadsheet document.
82     */
83     protected void initialize( TestParameters tParam, PrintWriter log ) {
84         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
85 
86         try {
87             log.println( "creating a Spreadsheet document" );
88             xSpreadsheetDoc = SOF.createCalcDoc(null);
89         } catch ( com.sun.star.uno.Exception e ) {
90             // Some exception occures.FAILED
91             e.printStackTrace( log );
92             throw new StatusException( "Couldn't create document", e );
93         }
94 
95     }
96 
97     /**
98     * Disposes Spreadsheet document.
99     */
100     protected void cleanup( TestParameters tParam, PrintWriter log ) {
101         log.println( "    disposing xSheetDoc " );
102         XComponent oComp = (XComponent)
103             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc);
104         util.DesktopTools.closeDoc(oComp);
105     }
106 
107     /**
108     * Creating a Testenvironment for the interfaces to be tested.
109     * Retrieves the collection of style families available in the document
110     * using the interface <code>XStyleFamiliesSupplier</code>.
111     * Obtains default style from the style family <code>'PageStyles'</code>.
112     * Retrieves the interface <code>XHeaderFooterContent</code> from the style
113     * using the property <code>'RightPageHeaderContent'</code>. Creates the
114     * instance of the service <code>com.sun.star.text.TextField.Time</code> and
115     * the instance of the service <code>com.sun.star.text.TextField.Date</code>
116     * Obtains the text (the interface <code>XText</code>) which is printed in
117     * the left part of the header or footer and inserts in it's content
118     * the second created instance.
119     * Object relations created :
120     * <ul>
121     *  <li> <code>'CONTENT'</code> for
122     *      {@link ifc.text._XTextContent}(the interface <code>XTextContent</code>
123     *      that was queried from the second created instance) </li>
124     *  <li> <code>'TEXT'</code> for
125     *      {@link ifc.text._XTextContent}(the the text which is printed in the
126     *       right part of the header or footer) </li>
127     * </ul>
128     * @see com.sun.star.style.XStyleFamiliesSupplier
129     * @see com.sun.star.sheet.XHeaderFooterContent
130     * @see com.sun.star.text.XText
131     * @see com.sun.star.text.XTextContent
132     */
133     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
134 
135         XInterface oObj = null;
136         XPropertySet PropSet;
137         XNameAccess PageStyles = null;
138         XStyle StdStyle = null;
139         XTextContent oContent = null;
140         XInterface aField = null;
141 
142         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
143             UnoRuntime.queryInterface(
144                 XStyleFamiliesSupplier.class,
145                 xSpreadsheetDoc );
146 
147         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
148         try{
149             PageStyles = (XNameAccess) AnyConverter.toObject(
150                 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
151             StdStyle = (XStyle) AnyConverter.toObject(
152                         new Type(XStyle.class),PageStyles.getByName("Default"));
153         } catch(com.sun.star.lang.WrappedTargetException e){
154             e.printStackTrace(log);
155             throw new StatusException("Couldn't get by name", e);
156         } catch(com.sun.star.container.NoSuchElementException e){
157             e.printStackTrace(log);
158             throw new StatusException("Couldn't get by name", e);
159         } catch(com.sun.star.lang.IllegalArgumentException e){
160             e.printStackTrace(log);
161             throw new StatusException("Couldn't get by name", e);
162         }
163 
164         //get the property-set
165         PropSet = (XPropertySet)
166             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
167 
168         XHeaderFooterContent RPHC = null;
169         // creation of testobject here
170         // first we write what we are intend to do to log file
171         log.println( "creating a test environment" );
172         try {
173             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
174                 new Type(XHeaderFooterContent.class),
175                     PropSet.getPropertyValue("RightPageHeaderContent"));
176         } catch (com.sun.star.lang.WrappedTargetException e) {
177             e.printStackTrace(log);
178             throw new StatusException("Couldn't get HeaderContent", e);
179         } catch (com.sun.star.beans.UnknownPropertyException e) {
180             e.printStackTrace(log);
181             throw new StatusException("Couldn't get HeaderContent", e);
182         } catch (com.sun.star.lang.IllegalArgumentException e) {
183             e.printStackTrace(log);
184             throw new StatusException("Couldn't get HeaderContent", e);
185         }
186 
187         XText left = RPHC.getLeftText();
188 
189         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
190             UnoRuntime.queryInterface(
191                 XMultiServiceFactory.class,
192                 xSpreadsheetDoc );
193 
194         XTextContent the_Field = null;
195         try {
196             oObj = (XInterface)
197                 oDocMSF.createInstance( "com.sun.star.text.TextField.Time" );
198 
199             the_Field = (XTextContent)
200                 UnoRuntime.queryInterface(XTextContent.class,oObj);
201 
202             aField = (XInterface)
203                 oDocMSF.createInstance("com.sun.star.text.TextField.Date");
204         } catch(com.sun.star.uno.Exception e) {
205             e.printStackTrace(log);
206             throw new StatusException("Couldn't create instance", e);
207         }
208 
209         oContent = (XTextContent)
210             UnoRuntime.queryInterface(XTextContent.class, aField);
211 
212         XTextCursor the_Cursor = left.createTextCursor();
213 
214         try {
215             left.insertTextContent(the_Cursor,the_Field, false);
216             PropSet.setPropertyValue("RightPageHeaderContent", RPHC);
217         } catch(com.sun.star.lang.IllegalArgumentException e) {
218             e.printStackTrace(log);
219             throw new StatusException("Couldn't create a test environment", e);
220         } catch(com.sun.star.lang.WrappedTargetException e) {
221             e.printStackTrace(log);
222             throw new StatusException("Couldn't create a test environment", e);
223         } catch(com.sun.star.beans.PropertyVetoException e) {
224             e.printStackTrace(log);
225             throw new StatusException("Couldn't create a test environment", e);
226         } catch(com.sun.star.beans.UnknownPropertyException e) {
227             e.printStackTrace(log);
228             throw new StatusException("Couldn't create a test environment", e);
229         }
230 
231         TestEnvironment tEnv = new TestEnvironment(oObj);
232 
233         tEnv.addObjRelation("CONTENT",oContent);
234         tEnv.addObjRelation("TEXT", RPHC.getRightText());
235 
236         return tEnv;
237 
238     } // finish method getTestEnvironment
239 
240 }    // finish class ScHeaderFieldObj
241 
242