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.Status;
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.AccessibilityTools;
34 import util.SOfficeFactory;
35 import util.utils;
36 
37 import com.sun.star.accessibility.AccessibleRole;
38 import com.sun.star.accessibility.XAccessible;
39 import com.sun.star.awt.XWindow;
40 import com.sun.star.beans.XPropertySet;
41 import com.sun.star.container.XIndexAccess;
42 import com.sun.star.container.XNameAccess;
43 import com.sun.star.frame.XController;
44 import com.sun.star.frame.XDispatch;
45 import com.sun.star.frame.XDispatchProvider;
46 import com.sun.star.frame.XModel;
47 import com.sun.star.lang.XMultiServiceFactory;
48 import com.sun.star.sheet.XHeaderFooterContent;
49 import com.sun.star.sheet.XSpreadsheet;
50 import com.sun.star.sheet.XSpreadsheetDocument;
51 import com.sun.star.sheet.XSpreadsheets;
52 import com.sun.star.style.XStyle;
53 import com.sun.star.style.XStyleFamiliesSupplier;
54 import com.sun.star.table.XCell;
55 import com.sun.star.text.XText;
56 import com.sun.star.uno.AnyConverter;
57 import com.sun.star.uno.Type;
58 import com.sun.star.uno.UnoRuntime;
59 import com.sun.star.uno.XInterface;
60 import com.sun.star.util.URL;
61 import com.sun.star.util.XCloseable;
62 import com.sun.star.util.XURLTransformer;
63 
64 /**
65  * Test for object which is represented by accessible component of
66  * a printed header in 'Page Preview' mode.
67  *
68  * Object implements the following interfaces :
69  * <ul>
70  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
71  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
72  * </ul> <p>
73  *
74  * @see com.sun.star.accessibility.XAccessibleComponent
75  * @see com.sun.star.accessibility.XAccessibleContext
76  * @see ifc.n.star.accessibility._XAccessibleComponent
77  * @see ifc.n.star.accessibility._XAccessibleContext
78  */
79 public class ScAccessiblePageHeader extends TestCase {
80 
81     static XSpreadsheetDocument xSpreadsheetDoc = null;
82 
83     /**
84      * Called to create an instance of <code>TestEnvironment</code>
85      * with an object to test and related objects.
86      * Switchs the document to Print Preview mode.
87      * Obtains accissible object for the page view.
88      *
89      * @param tParam test parameters
90      * @param log writer to log information while testing
91      *
92      * @see TestEnvironment
93      * @see #getTestEnvironment
94      */
95     protected TestEnvironment createTestEnvironment(
96         TestParameters Param, PrintWriter log) {
97 
98         XInterface oObj = null;
99 
100         // inserting some content to have non-empty page preview
101         XCell xCell = null;
102         try {
103             XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
104             XIndexAccess oIndexSheets = (XIndexAccess)
105                 UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
106             XSpreadsheet oSheet = null;
107             try {
108                 oSheet = (XSpreadsheet) AnyConverter.toObject(
109                         new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
110             } catch (com.sun.star.lang.IllegalArgumentException iae) {
111                 throw new StatusException("couldn't get sheet",iae);
112             }
113             xCell = oSheet.getCellByPosition(0, 0) ;
114             xCell.setFormula("ScAccessiblePageHeader");
115         } catch(com.sun.star.lang.WrappedTargetException e) {
116             log.println("Exception ceating relation :");
117             e.printStackTrace(log);
118         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
119             log.println("Exception ceating relation :");
120             e.printStackTrace(log);
121         }
122 
123         XModel aModel = (XModel)
124             UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc);
125 
126         XController xController = aModel.getCurrentController();
127 
128         // switching to 'Page Preview' mode
129         try {
130             XDispatchProvider xDispProv = (XDispatchProvider)
131                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
132             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
133                 UnoRuntime.queryInterface(XURLTransformer.class,
134             ( (XMultiServiceFactory) Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
135             // Because it's an in/out parameter we must use an array of URL objects.
136             URL[] aParseURL = new URL[1];
137             aParseURL[0] = new URL();
138             aParseURL[0].Complete = ".uno:PrintPreview";
139             xParser.parseStrict(aParseURL);
140             URL aURL = aParseURL[0];
141             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
142             if(xDispatcher != null)
143                 xDispatcher.dispatch( aURL, null );
144         } catch (com.sun.star.uno.Exception e) {
145             log.println("Couldn't change mode");
146             throw new StatusException(Status.failed("Couldn't change mode"));
147         }
148 
149         try {
150             Thread.sleep(500);
151         } catch (InterruptedException ex) {}
152 
153         AccessibilityTools at = new AccessibilityTools();
154 
155         XWindow xWindow = at.getCurrentWindow( (XMultiServiceFactory) Param.getMSF(), aModel);
156         XAccessible xRoot = at.getAccessibleObject(xWindow);
157 
158         oObj = at.getAccessibleObjectForRole
159             (xRoot, AccessibleRole.HEADER, "");
160 
161         log.println("ImplementationName " + utils.getImplName(oObj));
162         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
163 
164         TestEnvironment tEnv = new TestEnvironment(oObj);
165 
166         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
167             UnoRuntime.queryInterface(
168                 XStyleFamiliesSupplier.class,
169                 xSpreadsheetDoc );
170         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
171         XStyle StdStyle = null;
172 
173         try{
174             XNameAccess PageStyles = (XNameAccess) AnyConverter.toObject(
175                             new Type(XNameAccess.class),
176                                         StyleFamNames.getByName("PageStyles"));
177             StdStyle = (XStyle) AnyConverter.toObject(
178                 new Type(XStyle.class), PageStyles.getByName("Default"));
179         } catch(com.sun.star.lang.WrappedTargetException e){
180             e.printStackTrace(log);
181             throw new StatusException("Couldn't get by name", e);
182         } catch(com.sun.star.container.NoSuchElementException e){
183             e.printStackTrace(log);
184             throw new StatusException("Couldn't get by name", e);
185         } catch (com.sun.star.lang.IllegalArgumentException iae) {
186            throw new StatusException("Couldn't convert any", iae);
187         }
188 
189         //get the property-set
190         final XPropertySet PropSet = (XPropertySet)
191             UnoRuntime.queryInterface(XPropertySet.class, StdStyle);
192 
193         XHeaderFooterContent RPHC = null;
194         // creation of testobject here
195         // first we write what we are intend to do to log file
196         log.println( "creating a test environment" );
197         try {
198             RPHC = (XHeaderFooterContent) AnyConverter.toObject(
199                 new Type(XHeaderFooterContent.class),
200                     PropSet.getPropertyValue("RightPageHeaderContent"));
201         } catch(com.sun.star.lang.WrappedTargetException e){
202             e.printStackTrace(log);
203             throw new StatusException("Couldn't get HeaderContent", e);
204         } catch(com.sun.star.beans.UnknownPropertyException e){
205             e.printStackTrace(log);
206             throw new StatusException("Couldn't get HeaderContent", e);
207         } catch(com.sun.star.lang.IllegalArgumentException e){
208             e.printStackTrace(log);
209             throw new StatusException("Couldn't get HeaderContent", e);
210         }
211 
212         final XHeaderFooterContent RPHC2 = RPHC;
213 
214         final XText center = RPHC2.getCenterText();
215         final XText left = RPHC2.getLeftText();
216         final XText right = RPHC2.getRightText();
217 
218         tEnv.addObjRelation("EventProducer",
219             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
220                 public void fireEvent() {
221                     center.setString("CENTER");
222                     left.setString("LEFT");
223                     right.setString("RIGHT");
224                     try {
225                         PropSet.setPropertyValue("RightPageHeaderContent",RPHC2);
226                     } catch (com.sun.star.beans.UnknownPropertyException e) {
227                     } catch (com.sun.star.beans.PropertyVetoException e) {
228                     } catch (com.sun.star.lang.IllegalArgumentException e) {
229                     } catch (com.sun.star.lang.WrappedTargetException e) {}
230                 }
231             });
232 
233 
234         return tEnv;
235 
236     }
237 
238     /**
239      * Called while disposing a <code>TestEnvironment</code>.
240      * Disposes calc document.
241      * @param tParam test parameters
242      * @param tEnv the environment to cleanup
243      * @param log writer to log information while testing
244      */
245     protected void cleanup( TestParameters Param, PrintWriter log) {
246         log.println( "    disposing xSheetDoc " );
247         try {
248         XCloseable oComp = (XCloseable)
249             UnoRuntime.queryInterface (XCloseable.class, xSpreadsheetDoc) ;
250         oComp.close(true);
251         } catch(com.sun.star.util.CloseVetoException e) {
252             log.println("Couldn't close document: "+e.getMessage());
253         }
254     }
255 
256     /**
257      * Called while the <code>TestCase</code> initialization. In the
258      * implementation does nothing. Subclasses can override to initialize
259      * objects shared among all <code>TestEnvironment</code>s.
260      *
261      * @param tParam test parameters
262      * @param log writer to log information while testing
263      *
264      * @see #initializeTestCase
265      */
266     protected void initialize(TestParameters Param, PrintWriter log) {
267         // get a soffice factory object
268         SOfficeFactory SOF = SOfficeFactory.getFactory(  (XMultiServiceFactory) Param.getMSF());
269 
270         try {
271             log.println("creating a spreadsheetdocument");
272             xSpreadsheetDoc = SOF.createCalcDoc(null);
273         } catch (com.sun.star.uno.Exception e) {
274             e.printStackTrace( log );
275             throw new StatusException( "Couldn't create document ", e );
276         }
277     }
278 }
279