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._sd;
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.DrawTools;
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.container.XIndexAccess;
45 import com.sun.star.drawing.XDrawPage;
46 import com.sun.star.drawing.XDrawPages;
47 import com.sun.star.drawing.XDrawPagesSupplier;
48 import com.sun.star.drawing.XDrawView;
49 import com.sun.star.drawing.XShape;
50 import com.sun.star.frame.XModel;
51 import com.sun.star.lang.XComponent;
52 import com.sun.star.lang.XMultiServiceFactory;
53 import com.sun.star.uno.AnyConverter;
54 import com.sun.star.uno.Type;
55 import com.sun.star.uno.UnoRuntime;
56 import com.sun.star.uno.XInterface;
57 
58 public class AccessibleDrawDocumentView extends TestCase {
59 
60     XComponent xDrawDoc;
61 
62     /**
63      * Called to create an instance of <code>TestEnvironment</code> with an
64      * object to test and related objects. Subclasses should implement this
65      * method to provide the implementation and related objects. The method is
66      * called from <code>getTestEnvironment()</code>.
67      *
68      * @param tParam test parameters
69      * @param log writer to log information while testing
70      *
71      * @see TestEnvironment
72      * @see #getTestEnvironment()
73      */
74     protected TestEnvironment createTestEnvironment
75             (TestParameters Param, PrintWriter log) {
76         XInterface oObj = null;
77 
78         // get a soffice factory object
79         SOfficeFactory SOF = SOfficeFactory.getFactory(
80                                 (XMultiServiceFactory)Param.getMSF());
81 
82         // get the drawpage of drawing here
83         log.println( "getting Drawpage" );
84         XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
85             UnoRuntime.queryInterface(XDrawPagesSupplier.class, xDrawDoc);
86         XDrawPages oDPn = oDPS.getDrawPages();
87         final XDrawPage fDP2 = oDPn.insertNewByIndex(1);
88         XIndexAccess oDPi = (XIndexAccess)
89             UnoRuntime.queryInterface(XIndexAccess.class, oDPn);
90         XDrawPage oDP = null;
91         try {
92             oDP = (XDrawPage) AnyConverter.toObject(
93                                 new Type(XDrawPage.class),oDPi.getByIndex(0));
94         } catch (com.sun.star.lang.WrappedTargetException e) {
95             e.printStackTrace( log );
96             throw new StatusException("Couldn't get by index", e);
97         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
98             e.printStackTrace( log );
99             throw new StatusException("Couldn't get by index", e);
100         } catch (com.sun.star.lang.IllegalArgumentException e) {
101             e.printStackTrace( log );
102             throw new StatusException("Couldn't get by index", e);
103         }
104 
105         //get a Shape
106         log.println( "inserting a Shape" );
107         XShape oShape = SOF.createShape(xDrawDoc, 5000, 3500, 7500, 5000, "Rectangle");
108         DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc,0)).add(oShape);
109 
110         XModel aModel = (XModel)
111             UnoRuntime.queryInterface(XModel.class, xDrawDoc);
112 
113         AccessibilityTools at = new AccessibilityTools();
114 
115         XWindow xWindow = at.getCurrentWindow (
116                             (XMultiServiceFactory)Param.getMSF(),aModel);
117         XAccessible xRoot = at.getAccessibleObject(xWindow);
118 
119         //com.sun.star.accessibility.AccessibleRole
120         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT);
121 
122         oObj = AccessibilityTools.SearchedContext;
123 
124         log.println("ImplementationName "+utils.getImplName(oObj));
125 
126         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
127 
128         TestEnvironment tEnv = new TestEnvironment(oObj);
129 
130         final XDrawView xView = (XDrawView) UnoRuntime.queryInterface
131             (XDrawView.class, aModel.getCurrentController()) ;
132         final XDrawPage fDP1 = oDP;
133 
134         tEnv.addObjRelation("EventProducer",
135             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
136                 public void fireEvent() {
137                     xView.setCurrentPage(fDP2);
138                     xView.setCurrentPage(fDP1);
139                 }
140             });
141 
142         return tEnv;
143 
144     }
145 
146     /**
147     * Called while disposing a <code>TestEnvironment</code>.
148     * Disposes Impress documents.
149     * @param tParam test parameters
150     * @param tEnv the environment to cleanup
151     * @param log writer to log information while testing
152     */
153     protected void cleanup( TestParameters Param, PrintWriter log) {
154         log.println("disposing Draw document");
155         util.DesktopTools.closeDoc(xDrawDoc);;
156     }
157 
158     /**
159      * Called while the <code>TestCase</code> initialization. In the
160      * implementation does nothing. Subclasses can override to initialize
161      * objects shared among all <code>TestEnvironment</code>s.
162      *
163      * @param tParam test parameters
164      * @param log writer to log information while testing
165      *
166      * @see #initializeTestCase()
167      */
168     protected void initialize(TestParameters Param, PrintWriter log) {
169         // get a soffice factory object
170         SOfficeFactory SOF = SOfficeFactory.getFactory(
171                                     (XMultiServiceFactory)Param.getMSF());
172 
173         try {
174             log.println( "creating a draw document" );
175             xDrawDoc = SOF.createDrawDoc(null);;
176          } catch (com.sun.star.uno.Exception e) {
177             // Some exception occures.FAILED
178             e.printStackTrace( log );
179             throw new StatusException("Couldn't create document", e);
180          }
181     }
182 
183 }
184