1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package mod._sw;
24 
25 import java.io.PrintWriter;
26 
27 import lib.StatusException;
28 import lib.TestCase;
29 import lib.TestEnvironment;
30 import lib.TestParameters;
31 import util.AccessibilityTools;
32 import util.WriterTools;
33 import util.utils;
34 
35 import com.sun.star.accessibility.AccessibleRole;
36 import com.sun.star.accessibility.XAccessible;
37 import com.sun.star.accessibility.XAccessibleContext;
38 import com.sun.star.accessibility.XAccessibleValue;
39 import com.sun.star.awt.XWindow;
40 import com.sun.star.frame.XModel;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.text.ControlCharacter;
43 import com.sun.star.text.XText;
44 import com.sun.star.text.XTextCursor;
45 import com.sun.star.text.XTextDocument;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
48 
49 /**
50 * Test of accessible object for the text document.<p>
51 * Object implements the following interfaces :
52 * <ul>
53 *  <li> <code>::com::sun::star::accessibility::XAccessible</code></li>
54 * </ul>
55 * @see com.sun.star.accessibility.XAccessible
56 */
57 public class SwAccessibleDocumentView extends TestCase {
58 
59     XTextDocument xTextDoc = null;
60 
61     /**
62     * Called to create an instance of <code>TestEnvironment</code>
63     * with an object to test and related objects. The method is called from
64     * <code>getTestEnvironment()</code>. Obtains accissible object for
65     * text document.
66     *
67     * @param tParam test parameters
68     * @param log writer to log information while testing
69     *
70     * @see TestEnvironment
71     * @see #getTestEnvironment
72     */
73     protected TestEnvironment createTestEnvironment(
74         TestParameters Param, PrintWriter log) {
75 
76         XInterface oObj = null;
77 
78         XText oText = xTextDoc.getText();
79         XTextCursor oCursor = oText.createTextCursor();
80 
81         log.println( "inserting some lines" );
82         try {
83             for (int i=0; i<5; i++){
84                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
85                 oText.insertString( oCursor,
86                     " The quick brown fox jumps over the lazy Dog: SwXParagraph",
87                     false);
88                 oText.insertControlCharacter(
89                     oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
90                 oText.insertString( oCursor,
91                     "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwXParagraph",
92                     false);
93                 oText.insertControlCharacter(oCursor,
94                     ControlCharacter.PARAGRAPH_BREAK, false );
95                 oText.insertControlCharacter(
96                     oCursor, ControlCharacter.LINE_BREAK, false );
97             }
98         } catch ( com.sun.star.lang.IllegalArgumentException e ){
99             e.printStackTrace(log);
100             throw new StatusException( "Couldn't insert lines", e );
101         }
102 
103         XModel aModel = (XModel)
104             UnoRuntime.queryInterface(XModel.class, xTextDoc);
105 
106         AccessibilityTools at = new AccessibilityTools();
107 
108         XWindow xWindow = at.getCurrentWindow((XMultiServiceFactory)Param.getMSF(), aModel);
109         XAccessible xRoot = at.getAccessibleObject(xWindow);
110 
111         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT);
112 
113         oObj = AccessibilityTools.SearchedContext;
114 
115         log.println("ImplementationName " + utils.getImplName(oObj));
116         at.printAccessibleTree(log, xRoot, Param.getBool(util.PropertyName.DEBUG_IS_ACTIVE));
117 
118         TestEnvironment tEnv = new TestEnvironment(oObj);
119 
120         getAccessibleObjectForRole(xRoot, AccessibleRole.SCROLL_BAR);
121         final XAccessibleValue xAccVal = (XAccessibleValue) UnoRuntime.queryInterface
122                                 (XAccessibleValue.class, SearchedContext) ;
123 
124         tEnv.addObjRelation("EventProducer",
125             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
126                 public void fireEvent() {
127                     xAccVal.setCurrentValue(xAccVal.getMinimumValue());
128                     xAccVal.setCurrentValue(xAccVal.getMaximumValue());
129                 }
130             });
131 
132         return tEnv;
133 
134     }
135 
136     public static boolean first = false;
137     public static XAccessibleContext SearchedContext = null;
138 
139     public static void getAccessibleObjectForRole(XAccessible xacc,short role) {
140         XAccessibleContext ac = xacc.getAccessibleContext();
141         if (ac.getAccessibleRole()==role) {
142             if (first) SearchedContext = ac;
143                 else first=true;
144         } else {
145             int k = ac.getAccessibleChildCount();
146             for (int i=0;i<k;i++) {
147                 try {
148                     getAccessibleObjectForRole(ac.getAccessibleChild(i),role);
149                     if (SearchedContext != null) return ;
150                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
151                     System.out.println("Couldn't get Child");
152                 }
153             }
154         }
155     }
156 
157 
158     /**
159     * Called while disposing a <code>TestEnvironment</code>.
160     * Disposes text document.
161     * @param tParam test parameters
162     * @param tEnv the environment to cleanup
163     * @param log writer to log information while testing
164     */
165     protected void cleanup( TestParameters Param, PrintWriter log) {
166         log.println("dispose text document");
167         util.DesktopTools.closeDoc(xTextDoc);
168     }
169 
170     /**
171      * Called while the <code>TestCase</code> initialization.
172      * Creates a text document.
173      *
174      * @param tParam test parameters
175      * @param log writer to log information while testing
176      *
177      * @see #initializeTestCase
178      */
179     protected void initialize(TestParameters Param, PrintWriter log) {
180         log.println( "creating a text document" );
181         xTextDoc = WriterTools.createTextDoc((XMultiServiceFactory)Param.getMSF());
182     }
183 }
184