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.accessibility.XAccessibleContext;
42 import com.sun.star.accessibility.XAccessibleValue;
43 import com.sun.star.awt.XWindow;
44 import com.sun.star.frame.XModel;
45 import com.sun.star.lang.XMultiServiceFactory;
46 import com.sun.star.text.ControlCharacter;
47 import com.sun.star.text.XText;
48 import com.sun.star.text.XTextCursor;
49 import com.sun.star.text.XTextDocument;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.uno.XInterface;
52 
53 /**
54 * Test of accessible object for the text document.<p>
55 * Object implements the following interfaces :
56 * <ul>
57 *  <li> <code>::com::sun::star::accessibility::XAccessible</code></li>
58 * </ul>
59 * @see com.sun.star.accessibility.XAccessible
60 */
61 public class SwAccessibleDocumentView extends TestCase {
62 
63     XTextDocument xTextDoc = null;
64 
65     /**
66     * Called to create an instance of <code>TestEnvironment</code>
67     * with an object to test and related objects. The method is called from
68     * <code>getTestEnvironment()</code>. Obtains accissible object for
69     * text document.
70     *
71     * @param tParam test parameters
72     * @param log writer to log information while testing
73     *
74     * @see TestEnvironment
75     * @see #getTestEnvironment()
76     */
77     protected TestEnvironment createTestEnvironment(
78         TestParameters Param, PrintWriter log) {
79 
80         XInterface oObj = null;
81 
82         XText oText = xTextDoc.getText();
83         XTextCursor oCursor = oText.createTextCursor();
84 
85         log.println( "inserting some lines" );
86         try {
87             for (int i=0; i<5; i++){
88                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
89                 oText.insertString( oCursor,
90                     " The quick brown fox jumps over the lazy Dog: SwXParagraph",
91                     false);
92                 oText.insertControlCharacter(
93                     oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
94                 oText.insertString( oCursor,
95                     "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
96                     false);
97                 oText.insertControlCharacter(oCursor,
98                     ControlCharacter.PARAGRAPH_BREAK, false );
99                 oText.insertControlCharacter(
100                     oCursor, ControlCharacter.LINE_BREAK, false );
101             }
102         } catch ( com.sun.star.lang.IllegalArgumentException e ){
103             e.printStackTrace(log);
104             throw new StatusException( "Couldn't insert lines", e );
105         }
106 
107         XModel aModel = (XModel)
108             UnoRuntime.queryInterface(XModel.class, xTextDoc);
109 
110         AccessibilityTools at = new AccessibilityTools();
111 
112         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), aModel);
113         XAccessible xRoot = at.getAccessibleObject(xWindow);
114 
115         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT);
116 
117         oObj = AccessibilityTools.SearchedContext;
118 
119         log.println("ImplementationName " + utils.getImplName(oObj));
120         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
121 
122         TestEnvironment tEnv = new TestEnvironment(oObj);
123 
124         getAccessibleObjectForRole(xRoot, AccessibleRole.SCROLL_BAR);
125         final XAccessibleValue xAccVal = (XAccessibleValue) UnoRuntime.queryInterface
126                                 (XAccessibleValue.class, SearchedContext) ;
127 
128         tEnv.addObjRelation("EventProducer",
129             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
130                 public void fireEvent() {
131                     xAccVal.setCurrentValue(xAccVal.getMinimumValue());
132                     xAccVal.setCurrentValue(xAccVal.getMaximumValue());
133                 }
134             });
135 
136         return tEnv;
137 
138     }
139 
140     public static boolean first = false;
141     public static XAccessibleContext SearchedContext = null;
142 
143     public static void getAccessibleObjectForRole(XAccessible xacc,short role) {
144         XAccessibleContext ac = xacc.getAccessibleContext();
145         if (ac.getAccessibleRole()==role) {
146             if (first) SearchedContext = ac;
147                 else first=true;
148         } else {
149             int k = ac.getAccessibleChildCount();
150             for (int i=0;i<k;i++) {
151                 try {
152                     getAccessibleObjectForRole(ac.getAccessibleChild(i),role);
153                     if (SearchedContext != null) return ;
154                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
155                     System.out.println("Couldn't get Child");
156                 }
157             }
158         }
159     }
160 
161 
162     /**
163     * Called while disposing a <code>TestEnvironment</code>.
164     * Disposes text document.
165     * @param tParam test parameters
166     * @param tEnv the environment to cleanup
167     * @param log writer to log information while testing
168     */
169     protected void cleanup( TestParameters Param, PrintWriter log) {
170         log.println("dispose text document");
171         util.DesktopTools.closeDoc(xTextDoc);
172     }
173 
174     /**
175      * Called while the <code>TestCase</code> initialization.
176      * Creates a text document.
177      *
178      * @param tParam test parameters
179      * @param log writer to log information while testing
180      *
181      * @see #initializeTestCase()
182      */
183     protected void initialize(TestParameters Param, PrintWriter log) {
184         log.println( "creating a text document" );
185         xTextDoc = WriterTools.createTextDoc((XMultiServiceFactory)Param.getMSF());
186     }
187 }
188