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._sm;
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.AccessibilityTools;
37 import util.SOfficeFactory;
38 import util.utils;
39 
40 import com.sun.star.accessibility.AccessibleRole;
41 import com.sun.star.accessibility.XAccessible;
42 import com.sun.star.accessibility.XAccessibleContext;
43 import com.sun.star.awt.XWindow;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.frame.XModel;
46 import com.sun.star.lang.XComponent;
47 import com.sun.star.lang.XMultiServiceFactory;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 
51 /**
52  * Object implements the following interfaces :
53  * <ul>
54  *  <li> <code>::com::sun::star::accessibility::XAccessible</code></li>
55  *  <li> <code>::com::sun::star::accessibility::XAccessibleComponent</code></li>
56  *  <li> <code>::com::sun::star::accessibility::XAccessibleContext</code></li>
57  *  <li> <code>::com::sun::star::accessibility::XAccessibleEventBroadcaster</code></li>
58  *  <li> <code>::com::sun::star::accessibility::XAccessibleText</code></li>
59  * </ul> <p>
60  *
61  * @see com.sun.star.accessibility.XAccessible
62  * @see com.sun.star.accessibility.XAccessibleComponent
63  * @see com.sun.star.accessibility.XAccessibleContext
64  * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
65  * @see com.sun.star.accessibility.XAccessibleText
66  * @see ifc.accessibility._XAccessible
67  * @see ifc.accessibility._XAccessibleComponent
68  * @see ifc.accessibility._XAccessibleContext
69  * @see ifc.accessibility._XAccessibleEventBroadcaster
70  * @see ifc.accessibility._XAccessibleText
71  */
72 public class SmEditAccessible extends TestCase {
73     XComponent xMathDoc;
74 
75     /**
76      * Creates a <code>StarMath</code> document and obtains an accessibility
77      * component with the role <code>AccessibleRole.PANEL</code>.
78      */
79     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
80 
81         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF() );
82         try {
83             xMathDoc = SOF.openDoc("smath","_blank");
84         } catch (com.sun.star.lang.IllegalArgumentException ex) {
85             ex.printStackTrace( log );
86             throw new StatusException( "Couldn't create document", ex );
87         } catch (com.sun.star.io.IOException ex) {
88             ex.printStackTrace( log );
89             throw new StatusException( "Couldn't create document", ex );
90         } catch (com.sun.star.uno.Exception ex) {
91             ex.printStackTrace( log );
92             throw new StatusException( "Couldn't create document", ex );
93         }
94 
95         // setting a formula in document
96         final String expFormula = "sum hat a";
97         final XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface
98             (XPropertySet.class, xMathDoc);
99         try {
100             xPS.setPropertyValue("Formula", expFormula);
101         } catch(com.sun.star.lang.WrappedTargetException e) {
102             log.println("Couldn't set property value");
103             e.printStackTrace(log);
104         } catch(com.sun.star.lang.IllegalArgumentException e) {
105             log.println("Couldn't set property value");
106             e.printStackTrace(log);
107         } catch(com.sun.star.beans.PropertyVetoException e) {
108             log.println("Couldn't set property value");
109             e.printStackTrace(log);
110         } catch(com.sun.star.beans.UnknownPropertyException e) {
111             log.println("Couldn't set property value");
112             e.printStackTrace(log);
113         }
114 
115         XInterface oObj = null;
116 
117         XModel aModel = (XModel)
118             UnoRuntime.queryInterface(XModel.class, xMathDoc);
119 
120 
121         AccessibilityTools at = new AccessibilityTools();
122 
123         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), aModel);
124         XAccessible xRoot = at.getAccessibleObject(xWindow);
125 
126         oObj = at.getAccessibleObjectForRole
127             (xRoot, AccessibleRole.PANEL, "", "SmEditAccessible");
128 
129         log.println("ImplementationName " + utils.getImplName(oObj));
130         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
131         TestEnvironment tEnv = new TestEnvironment(oObj);
132 
133         tEnv.addObjRelation("Destroy", new Boolean(true));
134 
135         final XAccessibleContext con = (XAccessibleContext) UnoRuntime.queryInterface(XAccessibleContext.class, oObj);
136         tEnv.addObjRelation("EventProducer",
137             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
138                 public void fireEvent() {
139                     try {
140                         System.out.println("Childs "+con.getAccessibleChildCount());
141                         xPS.setPropertyValue("Formula", "sum hat x \n int a \n sum b");
142                         shortWait();
143                         System.out.println("Childs "+con.getAccessibleChildCount());
144                         xPS.setPropertyValue("Formula", expFormula);
145                         shortWait();
146                     } catch(com.sun.star.lang.WrappedTargetException e) {
147                     } catch(com.sun.star.lang.IllegalArgumentException e) {
148                     } catch(com.sun.star.beans.PropertyVetoException e) {
149                     } catch(com.sun.star.beans.UnknownPropertyException e) {
150                     }
151                 }
152             });
153 
154         return tEnv;
155     }
156 
157     /**
158      * Disposes the document created in <code>createTestEnvironment</code>
159      * method.
160      */
161     protected void cleanup( TestParameters Param, PrintWriter log) {
162 
163         log.println( "    disposing xMathDoc " );
164         xMathDoc.dispose();
165     }
166 
167     /**
168     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
169     * reset</code> call.
170     */
171     private void shortWait() {
172         try {
173             Thread.sleep(1000) ;
174         } catch (InterruptedException e) {
175             log.println("While waiting :" + e) ;
176         }
177     }
178 }    // finish class SmModel
179