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