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