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 package mod._svtools;
28 
29 import java.io.PrintWriter;
30 
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.AccessibilityTools;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.accessibility.AccessibleRole;
39 import com.sun.star.accessibility.XAccessible;
40 import com.sun.star.awt.PosSize;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.lang.XMultiServiceFactory;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 import com.sun.star.util.XCloseable;
48 
49 
50 public class AccessibleTabBar extends TestCase {
51     static XComponent xDoc;
52 
53     /**
54      * Disposes the document, if exists, created in
55      * <code>createTestEnvironment</code> method.
56      */
57     protected void cleanup(TestParameters Param, PrintWriter log) {
58         log.println("disposing xCalcDoc");
59 
60         if (xDoc != null) {
61             closeDoc(xDoc);
62         }
63     }
64 
65     /**
66      * Creates a spreadsheet document. Then obtains an accessible object with
67      * the role <code>AccessibleRole.PAGETAB</code>.
68      * Object relations created :
69      * <ul>
70      *  <li> <code>'EventProducer'</code> for
71      *      {@link ifc.accessibility._XAccessibleEventBroadcaster}:
72      *   </li>
73      * </ul>
74      *
75      * @param tParam test parameters
76      * @param log writer to log information while testing
77      *
78      * @see com.sun.star.awt.Toolkit
79      * @see com.sun.star.accessibility.AccessibleRole
80      * @see ifc.accessibility._XAccessibleEventBroadcaster
81      * @see com.sun.star.accessibility.XAccessibleEventBroadcaster
82      */
83     protected TestEnvironment createTestEnvironment(TestParameters tParam,
84                                                     PrintWriter log) {
85         log.println("creating a test environment");
86 
87         if (xDoc != null) {
88             closeDoc(xDoc);
89         }
90 
91         XMultiServiceFactory msf = (XMultiServiceFactory) tParam.getMSF();
92 
93         // get a soffice factory object
94         SOfficeFactory SOF = SOfficeFactory.getFactory(msf);
95 
96         try {
97             log.println("creating a calc document");
98             xDoc = (XComponent) UnoRuntime.queryInterface(XComponent.class,
99                                                           SOF.createCalcDoc(
100                                                                   null));
101         } catch (com.sun.star.uno.Exception e) {
102             // Some exception occures.FAILED
103             e.printStackTrace(log);
104             throw new StatusException("Couldn't create document", e);
105         }
106 
107         shortWait();
108 
109         XInterface oObj = null;
110 
111         AccessibilityTools at = new AccessibilityTools();
112 
113         shortWait();
114 
115         XWindow xWindow = UnoRuntime.queryInterface(XModel.class, xDoc).
116             getCurrentController().getFrame().getContainerWindow();
117 
118         XAccessible xRoot = at.getAccessibleObject(xWindow);
119         at.printAccessibleTree(log, xRoot, tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
120         oObj = at.getAccessibleObjectForRole(xRoot, AccessibleRole.PANEL);
121 
122         log.println("ImplementationName: " + util.utils.getImplName(oObj));
123 
124         TestEnvironment tEnv = new TestEnvironment(oObj);
125 
126         final XWindow aWin = xWindow;
127 
128         tEnv.addObjRelation("EventProducer",
129                             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
130             public void fireEvent() {
131                 aWin.setPosSize(100,100, 500, 500, PosSize.POSSIZE);
132             }
133         });
134 
135         return tEnv;
136     }
137 
138     /**
139     * Sleeps for 3 sec. to allow StarOffice to react on <code>
140     * reset</code> call.
141     */
142     private void shortWait() {
143         try {
144             Thread.sleep(5000);
145         } catch (InterruptedException e) {
146             System.out.println("While waiting :" + e);
147         }
148     }
149 
150     protected void closeDoc(XComponent xDoc) {
151         XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
152                                     XCloseable.class, xDoc);
153 
154         try {
155             closer.close(true);
156         } catch (com.sun.star.util.CloseVetoException e) {
157             log.println("Couldn't close document " + e.getMessage());
158         } catch (java.lang.NullPointerException e) {
159             log.println("Couldn't close document " + e.getMessage());
160         }
161     }
162 }