1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._sc;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.PrintWriter;
27cdf0e10cSrcweir 
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir import lib.TestCase;
30cdf0e10cSrcweir import lib.TestEnvironment;
31cdf0e10cSrcweir import lib.TestParameters;
32cdf0e10cSrcweir import util.SOfficeFactory;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
35cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
36cdf0e10cSrcweir import com.sun.star.lang.XComponent;
37cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
38cdf0e10cSrcweir import com.sun.star.sheet.XHeaderFooterContent;
39cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
40cdf0e10cSrcweir import com.sun.star.style.XStyle;
41cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
42cdf0e10cSrcweir import com.sun.star.text.XText;
43cdf0e10cSrcweir import com.sun.star.text.XTextContent;
44cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
45cdf0e10cSrcweir import com.sun.star.text.XTextFieldsSupplier;
46cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
47cdf0e10cSrcweir import com.sun.star.uno.Type;
48cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
49cdf0e10cSrcweir import com.sun.star.uno.XInterface;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir /**
52cdf0e10cSrcweir  * Test for object which is represented by collection of
53cdf0e10cSrcweir  * text fields conained in the text of a page header. <p>
54cdf0e10cSrcweir  *
55cdf0e10cSrcweir  * Object implements the following interfaces :
56cdf0e10cSrcweir  * <ul>
57cdf0e10cSrcweir  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
58cdf0e10cSrcweir  *  <li> <code>com::sun::star::util::XRefreshable</code></li>
59cdf0e10cSrcweir  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
60cdf0e10cSrcweir  * </ul> <p>
61cdf0e10cSrcweir  *
62cdf0e10cSrcweir  * @see com.sun.star.container.XEnumerationAccess
63cdf0e10cSrcweir  * @see com.sun.star.util.XRefreshable
64cdf0e10cSrcweir  * @see com.sun.star.container.XElementAccess
65cdf0e10cSrcweir  * @see ifc.container._XEnumerationAccess
66cdf0e10cSrcweir  * @see ifc.util._XRefreshable
67cdf0e10cSrcweir  * @see ifc.container._XElementAccess
68cdf0e10cSrcweir  */
69cdf0e10cSrcweir public class ScHeaderFieldsObj extends TestCase {
70cdf0e10cSrcweir     static XSpreadsheetDocument xSpreadsheetDoc;
71cdf0e10cSrcweir 
72cdf0e10cSrcweir     /**
73cdf0e10cSrcweir      * Creates Spreadsheet document.
74cdf0e10cSrcweir      */
initialize( TestParameters tParam, PrintWriter log )75cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
76cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         try {
79cdf0e10cSrcweir             log.println( "creating a Spreadsheet document" );
80cdf0e10cSrcweir             xSpreadsheetDoc = SOF.createCalcDoc(null);
81cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
82*30acf5e8Spfg             // Some exception occured.FAILED
83cdf0e10cSrcweir             e.printStackTrace( log );
84cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     /**
90cdf0e10cSrcweir      * Disposes Spreadsheet document.
91cdf0e10cSrcweir      */
cleanup( TestParameters tParam, PrintWriter log )92cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
93cdf0e10cSrcweir         log.println( "    disposing xSheetDoc " );
94cdf0e10cSrcweir         XComponent oComp = (XComponent)
95cdf0e10cSrcweir             UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc);
96cdf0e10cSrcweir         util.DesktopTools.closeDoc(oComp);
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     /**
100cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
101cdf0e10cSrcweir      * Retrieves the collection of style families available in the document
102cdf0e10cSrcweir      * using the interface <code>XStyleFamiliesSupplier</code>.
103cdf0e10cSrcweir      * Obtains default style from the style family <code>'PageStyles'</code>.
104cdf0e10cSrcweir      * Retrieves the interface <code>XHeaderFooterContent</code> from the style
105cdf0e10cSrcweir      * using the property <code>'RightPageHeaderContent'</code>. Creates the
106cdf0e10cSrcweir      * instance of the service <code>com.sun.star.text.TextField.Time</code> .
107cdf0e10cSrcweir      * Obtains the text (the interface <code>XText</code>) which is printed in
108cdf0e10cSrcweir      * the left part of the header or footer and inserts in it's content
109cdf0e10cSrcweir      * the created field instance. Then the tested component is obtained
110cdf0e10cSrcweir      * through <code>XTextFieldsSupplier</code> interface of a text.
111cdf0e10cSrcweir      *
112cdf0e10cSrcweir      * @see com.sun.star.style.XStyleFamiliesSupplier
113cdf0e10cSrcweir      * @see com.sun.star.sheet.XHeaderFooterContent
114cdf0e10cSrcweir      * @see com.sun.star.text.XText
115cdf0e10cSrcweir      * @see com.sun.star.text.XTextContent
116cdf0e10cSrcweir      */
createTestEnvironment(TestParameters tParam, PrintWriter log)117cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters tParam, PrintWriter log) {
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         XInterface oObj = null;
120cdf0e10cSrcweir         XPropertySet PropSet;
121cdf0e10cSrcweir         XNameAccess PageStyles = null;
122cdf0e10cSrcweir         XStyle StdStyle = null;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
125cdf0e10cSrcweir             UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
126cdf0e10cSrcweir             xSpreadsheetDoc );
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
129cdf0e10cSrcweir         try{
130cdf0e10cSrcweir             PageStyles = (XNameAccess) AnyConverter.toObject(
131cdf0e10cSrcweir                 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
132cdf0e10cSrcweir             StdStyle = (XStyle) AnyConverter.toObject(
133cdf0e10cSrcweir                         new Type(XStyle.class),PageStyles.getByName("Default"));
134cdf0e10cSrcweir         } catch(com.sun.star.lang.WrappedTargetException e){
135cdf0e10cSrcweir             e.printStackTrace(log);
136cdf0e10cSrcweir             throw new StatusException("Couldn't get by name", e);
137cdf0e10cSrcweir         } catch(com.sun.star.container.NoSuchElementException e){
138cdf0e10cSrcweir             e.printStackTrace(log);
139cdf0e10cSrcweir             throw new StatusException("Couldn't get by name", e);
140cdf0e10cSrcweir         } catch(com.sun.star.lang.IllegalArgumentException e){
141cdf0e10cSrcweir             e.printStackTrace(log);
142cdf0e10cSrcweir             throw new StatusException("Couldn't get by name", e);
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         //get the property-set
146cdf0e10cSrcweir         PropSet = (XPropertySet)
147cdf0e10cSrcweir             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         XHeaderFooterContent RPHC = null;
150cdf0e10cSrcweir         // creation of testobject here
151cdf0e10cSrcweir         // first we write what we are intend to do to log file
152cdf0e10cSrcweir         log.println( "creating a test environment" );
153cdf0e10cSrcweir         try {
154cdf0e10cSrcweir             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
155cdf0e10cSrcweir                 new Type(XHeaderFooterContent.class),
156cdf0e10cSrcweir                     PropSet.getPropertyValue("RightPageHeaderContent"));
157cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
158cdf0e10cSrcweir             e.printStackTrace(log);
159cdf0e10cSrcweir             throw new StatusException("Couldn't get HeaderContent", e);
160cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
161cdf0e10cSrcweir             e.printStackTrace(log);
162cdf0e10cSrcweir             throw new StatusException("Couldn't get HeaderContent", e);
163cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException e) {
164cdf0e10cSrcweir             e.printStackTrace(log);
165cdf0e10cSrcweir             throw new StatusException("Couldn't get HeaderContent", e);
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir 
168cdf0e10cSrcweir         XText left = RPHC.getLeftText();
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         XMultiServiceFactory oDocMSF = (XMultiServiceFactory)
171cdf0e10cSrcweir             UnoRuntime.queryInterface(
172cdf0e10cSrcweir                 XMultiServiceFactory.class,
173cdf0e10cSrcweir                 xSpreadsheetDoc );
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         XTextContent the_Field = null;
176cdf0e10cSrcweir         try {
177cdf0e10cSrcweir             oObj = (XInterface)
178cdf0e10cSrcweir                 oDocMSF.createInstance( "com.sun.star.text.TextField.Time" );
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             the_Field = (XTextContent)
181cdf0e10cSrcweir                 UnoRuntime.queryInterface(XTextContent.class,oObj);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         } catch(com.sun.star.uno.Exception e) {
184cdf0e10cSrcweir             e.printStackTrace(log);
185cdf0e10cSrcweir             throw new StatusException("Couldn't create instance", e);
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         XTextCursor the_Cursor = left.createTextCursor();
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         try {
191cdf0e10cSrcweir             left.insertTextContent(the_Cursor,the_Field, false);
192cdf0e10cSrcweir             PropSet.setPropertyValue("RightPageHeaderContent", RPHC);
193cdf0e10cSrcweir         } catch(com.sun.star.lang.IllegalArgumentException e) {
194cdf0e10cSrcweir             e.printStackTrace(log);
195cdf0e10cSrcweir             throw new StatusException("Couldn't create a test environment", e);
196cdf0e10cSrcweir         } catch(com.sun.star.lang.WrappedTargetException e) {
197cdf0e10cSrcweir             e.printStackTrace(log);
198cdf0e10cSrcweir             throw new StatusException("Couldn't create a test environment", e);
199cdf0e10cSrcweir         } catch(com.sun.star.beans.PropertyVetoException e) {
200cdf0e10cSrcweir             e.printStackTrace(log);
201cdf0e10cSrcweir             throw new StatusException("Couldn't create a test environment", e);
202cdf0e10cSrcweir         } catch(com.sun.star.beans.UnknownPropertyException e) {
203cdf0e10cSrcweir             e.printStackTrace(log);
204cdf0e10cSrcweir             throw new StatusException("Couldn't create a test environment", e);
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir 
207cdf0e10cSrcweir         XTextFieldsSupplier xTFSupp = (XTextFieldsSupplier)
208cdf0e10cSrcweir             UnoRuntime.queryInterface(XTextFieldsSupplier.class, left);
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         oObj = xTFSupp.getTextFields();
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(oObj);
213cdf0e10cSrcweir 
214cdf0e10cSrcweir         return tEnv;
215cdf0e10cSrcweir     } // finish method getTestEnvironment
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
218