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