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._sw;
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.WriterTools;
37 import util.utils;
38 
39 import com.sun.star.accessibility.AccessibleRole;
40 import com.sun.star.accessibility.XAccessible;
41 import com.sun.star.awt.XWindow;
42 import com.sun.star.beans.XPropertySet;
43 import com.sun.star.container.XNameAccess;
44 import com.sun.star.frame.XModel;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.style.XStyle;
47 import com.sun.star.style.XStyleFamiliesSupplier;
48 import com.sun.star.text.XTextDocument;
49 import com.sun.star.uno.AnyConverter;
50 import com.sun.star.uno.Type;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 
54 /**
55 * Test of accessible object for a header of a text document.<p>
56 * Object implements the following interfaces :
57 * <ul>
58 *  <li> <code>::com::sun::star::accessibility::XAccessible</code></li>
59 * </ul>
60 * @see com.sun.star.accessibility.XAccessible
61 */
62 public class SwAccessibleHeaderView extends TestCase {
63 
64     XTextDocument xTextDoc = null;
65 
66     /**
67     * Called to create an instance of <code>TestEnvironment</code>
68     * with an object to test and related objects. Obtains style
69     * <code>Standard</code> from style family <code>PageStyles</code>.
70     * Changes values of properties <code>HeaderIsOn</code> and
71     * <code>FooterIsOn</code> by <code>true</code>. Obtains accessible component
72     * for header.
73     *
74     * @param tParam test parameters
75     * @param log writer to log information while testing
76     *
77     * @see TestEnvironment
78     * @see #getTestEnvironment()
79     */
80     protected TestEnvironment createTestEnvironment(
81         TestParameters Param, PrintWriter log) {
82 
83         XInterface oObj = null;
84         XNameAccess PageStyles = null;
85         XStyle StdStyle = null;
86 
87         XStyleFamiliesSupplier StyleFam = (XStyleFamiliesSupplier)
88             UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, xTextDoc);
89         XNameAccess StyleFamNames = StyleFam.getStyleFamilies();
90 
91         // obtains style 'Standatd' from style family 'PageStyles'
92         try {
93             PageStyles = (XNameAccess) AnyConverter.toObject(
94                 new Type(XNameAccess.class),StyleFamNames.getByName("PageStyles"));
95             StdStyle = (XStyle) AnyConverter.toObject(
96                     new Type(XStyle.class),PageStyles.getByName("Standard"));
97         } catch ( com.sun.star.lang.WrappedTargetException e ) {
98             e.printStackTrace(log);
99             throw new StatusException("Error getting style by name!", e);
100         } catch ( com.sun.star.container.NoSuchElementException e ) {
101             e.printStackTrace(log);
102             throw new StatusException("Error, no such style name! ", e);
103         } catch ( com.sun.star.lang.IllegalArgumentException e ) {
104             e.printStackTrace(log);
105             throw new StatusException("Error getting style by name!", e);
106         }
107 
108         final XPropertySet PropSet = (XPropertySet)
109             UnoRuntime.queryInterface( XPropertySet.class, StdStyle);
110 
111         // changing/getting some properties
112         try {
113             log.println( "Switching on header" );
114             PropSet.setPropertyValue("HeaderIsOn", new Boolean(true));
115         } catch ( com.sun.star.lang.WrappedTargetException e ) {
116             e.printStackTrace(log);
117             throw new StatusException("Couldn't set propertyValue...", e);
118         }  catch ( com.sun.star.lang.IllegalArgumentException e ) {
119             e.printStackTrace(log);
120             throw new StatusException("Couldn't set propertyValue...", e);
121         } catch ( com.sun.star.beans.PropertyVetoException e ) {
122             e.printStackTrace(log);
123             throw new StatusException("Couldn't set propertyValue...", e);
124         } catch ( com.sun.star.beans.UnknownPropertyException e ) {
125             e.printStackTrace(log);
126             throw new StatusException("Couldn't set propertyValue...", e);
127         }
128 
129         XModel aModel = (XModel)
130             UnoRuntime.queryInterface(XModel.class, xTextDoc);
131 
132         AccessibilityTools at = new AccessibilityTools();
133 
134         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), aModel);
135         XAccessible xRoot = at.getAccessibleObject(xWindow);
136 
137         at.getAccessibleObjectForRole(xRoot, AccessibleRole.HEADER);
138 
139         oObj = AccessibilityTools.SearchedContext;
140 
141         log.println("ImplementationName " + utils.getImplName(oObj));
142         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
143 
144         TestEnvironment tEnv = new TestEnvironment(oObj);
145 
146         tEnv.addObjRelation("EventProducer",
147             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer(){
148                 public void fireEvent() {
149                     try {
150                         PropSet.setPropertyValue("HeaderLeftMargin",
151                             new Integer(1000));
152                     } catch (com.sun.star.uno.Exception e) {
153                         e.printStackTrace();
154                         throw new StatusException("Cann't change footer.", e);
155                     }
156                 }
157             });
158 
159         return tEnv;
160 
161     }
162 
163     /**
164     * Called while disposing a <code>TestEnvironment</code>.
165     * Disposes text document.
166     * @param tParam test parameters
167     * @param tEnv the environment to cleanup
168     * @param log writer to log information while testing
169     */
170     protected void cleanup( TestParameters Param, PrintWriter log) {
171         log.println("dispose text document");
172         util.DesktopTools.closeDoc(xTextDoc);
173     }
174 
175     /**
176      * Called while the <code>TestCase</code> initialization.
177      * Creates a text document.
178      *
179      * @param tParam test parameters
180      * @param log writer to log information while testing
181      *
182      * @see #initializeTestCase()
183      */
184     protected void initialize(TestParameters Param, PrintWriter log) {
185         log.println( "creating a text document" );
186         xTextDoc = WriterTools.createTextDoc((XMultiServiceFactory)Param.getMSF());
187     }
188 }
189