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.text.XTextFieldsSupplier;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.Type;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 
55 /**
56  * Test for object which is represented by collection of
57  * text fields conained in the text of a page header. <p>
58  *
59  * Object implements the following interfaces :
60  * <ul>
61  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
62  *  <li> <code>com::sun::star::util::XRefreshable</code></li>
63  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
64  * </ul> <p>
65  *
66  * @see com.sun.star.container.XEnumerationAccess
67  * @see com.sun.star.util.XRefreshable
68  * @see com.sun.star.container.XElementAccess
69  * @see ifc.container._XEnumerationAccess
70  * @see ifc.util._XRefreshable
71  * @see ifc.container._XElementAccess
72  */
73 public class ScHeaderFieldsObj extends TestCase {
74     static XSpreadsheetDocument xSpreadsheetDoc;
75 
76     /**
77      * Creates Spreadsheet document.
78      */
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      */
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> .
111      * Obtains the text (the interface <code>XText</code>) which is printed in
112      * the left part of the header or footer and inserts in it's content
113      * the created field instance. Then the tested component is obtained
114      * through <code>XTextFieldsSupplier</code> interface of a text.
115      *
116      * @see com.sun.star.style.XStyleFamiliesSupplier
117      * @see com.sun.star.sheet.XHeaderFooterContent
118      * @see com.sun.star.text.XText
119      * @see com.sun.star.text.XTextContent
120      */
121     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
122 
123         XInterface oObj = null;
124         XPropertySet PropSet;
125         XNameAccess PageStyles = null;
126         XStyle StdStyle = null;
127 
128         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
129             UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
130             xSpreadsheetDoc );
131 
132         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
133         try{
134             PageStyles = (XNameAccess) AnyConverter.toObject(
135                 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
136             StdStyle = (XStyle) AnyConverter.toObject(
137                         new Type(XStyle.class),PageStyles.getByName("Default"));
138         } catch(com.sun.star.lang.WrappedTargetException e){
139             e.printStackTrace(log);
140             throw new StatusException("Couldn't get by name", e);
141         } catch(com.sun.star.container.NoSuchElementException e){
142             e.printStackTrace(log);
143             throw new StatusException("Couldn't get by name", e);
144         } catch(com.sun.star.lang.IllegalArgumentException e){
145             e.printStackTrace(log);
146             throw new StatusException("Couldn't get by name", e);
147         }
148 
149         //get the property-set
150         PropSet = (XPropertySet)
151             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
152 
153         XHeaderFooterContent RPHC = null;
154         // creation of testobject here
155         // first we write what we are intend to do to log file
156         log.println( "creating a test environment" );
157         try {
158             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
159                 new Type(XHeaderFooterContent.class),
160                     PropSet.getPropertyValue("RightPageHeaderContent"));
161         } catch (com.sun.star.lang.WrappedTargetException e) {
162             e.printStackTrace(log);
163             throw new StatusException("Couldn't get HeaderContent", e);
164         } catch (com.sun.star.beans.UnknownPropertyException e) {
165             e.printStackTrace(log);
166             throw new StatusException("Couldn't get HeaderContent", e);
167         } catch (com.sun.star.lang.IllegalArgumentException e) {
168             e.printStackTrace(log);
169             throw new StatusException("Couldn't get HeaderContent", e);
170         }
171 
172         XText left = RPHC.getLeftText();
173 
174         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
175             UnoRuntime.queryInterface(
176                 XMultiServiceFactory.class,
177                 xSpreadsheetDoc );
178 
179         XTextContent the_Field = null;
180         try {
181             oObj = (XInterface)
182                 oDocMSF.createInstance( "com.sun.star.text.TextField.Time" );
183 
184             the_Field = (XTextContent)
185                 UnoRuntime.queryInterface(XTextContent.class,oObj);
186 
187         } catch(com.sun.star.uno.Exception e) {
188             e.printStackTrace(log);
189             throw new StatusException("Couldn't create instance", e);
190         }
191 
192         XTextCursor the_Cursor = left.createTextCursor();
193 
194         try {
195             left.insertTextContent(the_Cursor,the_Field, false);
196             PropSet.setPropertyValue("RightPageHeaderContent", RPHC);
197         } catch(com.sun.star.lang.IllegalArgumentException e) {
198             e.printStackTrace(log);
199             throw new StatusException("Couldn't create a test environment", e);
200         } catch(com.sun.star.lang.WrappedTargetException e) {
201             e.printStackTrace(log);
202             throw new StatusException("Couldn't create a test environment", e);
203         } catch(com.sun.star.beans.PropertyVetoException e) {
204             e.printStackTrace(log);
205             throw new StatusException("Couldn't create a test environment", e);
206         } catch(com.sun.star.beans.UnknownPropertyException e) {
207             e.printStackTrace(log);
208             throw new StatusException("Couldn't create a test environment", e);
209         }
210 
211         XTextFieldsSupplier xTFSupp = (XTextFieldsSupplier)
212             UnoRuntime.queryInterface(XTextFieldsSupplier.class, left);
213 
214         oObj = xTFSupp.getTextFields();
215 
216         TestEnvironment tEnv = new TestEnvironment(oObj);
217 
218         return tEnv;
219     } // finish method getTestEnvironment
220 }
221 
222