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.Status;
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.AccessibilityTools;
33 import util.WriterTools;
34 import util.utils;
35 
36 import com.sun.star.accessibility.AccessibleRole;
37 import com.sun.star.accessibility.XAccessible;
38 import com.sun.star.awt.XWindow;
39 import com.sun.star.frame.XController;
40 import com.sun.star.frame.XDispatch;
41 import com.sun.star.frame.XDispatchProvider;
42 import com.sun.star.frame.XModel;
43 import com.sun.star.lang.XMultiServiceFactory;
44 import com.sun.star.text.ControlCharacter;
45 import com.sun.star.text.XText;
46 import com.sun.star.text.XTextCursor;
47 import com.sun.star.text.XTextDocument;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.uno.XInterface;
50 import com.sun.star.util.URL;
51 import com.sun.star.util.XURLTransformer;
52 
53 public class SwAccessibleDocumentPageView extends TestCase {
54 
55     XTextDocument xTextDoc = null;
56 
57     /**
58     * Called to create an instance of <code>TestEnvironment</code>
59     * with an object to test and related objects.
60     * Switchs the document to Print Preview mode.
61     * Obtains accissible object for the document page view.
62     *
63     * @param tParam test parameters
64     * @param log writer to log information while testing
65     *
66     * @see TestEnvironment
67     * @see #getTestEnvironment()
68     */
69     protected TestEnvironment createTestEnvironment(
70         TestParameters Param, PrintWriter log) {
71 
72         XInterface oObj = null;
73 
74         XText oText = xTextDoc.getText();
75         XTextCursor oCursor = oText.createTextCursor();
76 
77         log.println( "inserting some lines" );
78         try {
79             for (int i=0; i<25; i++){
80                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
81                 oText.insertString( oCursor,
82                     " The quick brown fox jumps over the lazy Dog: SwAccessibleDocumentPageView",
83                     false);
84                 oText.insertControlCharacter(
85                     oCursor, ControlCharacter.PARAGRAPH_BREAK, false );
86                 oText.insertString( oCursor,
87                     "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG: SwAccessibleDocumentPageView",
88                     false);
89                 oText.insertControlCharacter(oCursor,
90                     ControlCharacter.PARAGRAPH_BREAK, false );
91                 oText.insertControlCharacter(
92                     oCursor, ControlCharacter.LINE_BREAK, false );
93             }
94         } catch ( com.sun.star.lang.IllegalArgumentException e ){
95             e.printStackTrace(log);
96             throw new StatusException( "Couldn't insert lines", e );
97         }
98 
99         XController xController = xTextDoc.getCurrentController();
100 
101         XModel aModel = (XModel)
102             UnoRuntime.queryInterface(XModel.class, xTextDoc);
103 
104         //switch to 'Print Preview' mode
105         try {
106             XDispatchProvider xDispProv = (XDispatchProvider)
107                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
108             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
109                 UnoRuntime.queryInterface(XURLTransformer.class,
110             ( (XMultiServiceFactory) Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
111             // Because it's an in/out parameter we must use an array of URL objects.
112             URL[] aParseURL = new URL[1];
113             aParseURL[0] = new URL();
114             aParseURL[0].Complete = ".uno:PrintPreview";
115             xParser.parseStrict(aParseURL);
116             URL aURL = aParseURL[0];
117             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
118             if(xDispatcher != null)
119                 xDispatcher.dispatch( aURL, null );
120         } catch (com.sun.star.uno.Exception e) {
121             log.println("Couldn't change mode");
122             throw new StatusException(Status.failed("Couldn't change mode"));
123         }
124 
125         shortWait();
126 
127         AccessibilityTools at = new AccessibilityTools();
128 
129         XWindow xWindow = at.getCurrentWindow( (XMultiServiceFactory) Param.getMSF(), aModel);
130         XAccessible xRoot = at.getAccessibleObject(xWindow);
131 
132         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT );
133 
134         oObj = AccessibilityTools.SearchedContext;
135 
136         log.println("ImplementationName " + utils.getImplName(oObj));
137 
138         TestEnvironment tEnv = new TestEnvironment(oObj);
139 
140         final XText the_text = oText;
141 
142         tEnv.addObjRelation("EventProducer",
143             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
144                 public void fireEvent() {
145                     String oldText = the_text.getString();
146                     the_text.setString("EVENT FIRED");
147                     shortWait();
148                     the_text.setString(oldText);
149                 }
150             });
151 
152         return tEnv;
153 
154     }
155 
156 
157     /**
158     * Sleeps for 1 sec. to allow StarOffice to react on <code>
159     * reset</code> call.
160     */
161     private void shortWait() {
162         try {
163             Thread.sleep(2000) ;
164         } catch (InterruptedException e) {
165             log.println("While waiting :" + e) ;
166         }
167     }
168 
169 
170     /**
171     * Called while disposing a <code>TestEnvironment</code>.
172     * Disposes text document.
173     * @param tParam test parameters
174     * @param tEnv the environment to cleanup
175     * @param log writer to log information while testing
176     */
177     protected void cleanup( TestParameters Param, PrintWriter log) {
178         log.println("dispose text document");
179         util.DesktopTools.closeDoc(xTextDoc);
180     }
181 
182     /**
183      * Called while the <code>TestCase</code> initialization. In the
184      * implementation does nothing. Subclasses can override to initialize
185      * objects shared among all <code>TestEnvironment</code>s.
186      *
187      * @param tParam test parameters
188      * @param log writer to log information while testing
189      *
190      * @see #initializeTestCase()
191      */
192     protected void initialize(TestParameters Param, PrintWriter log) {
193         log.println( "creating a text document" );
194         xTextDoc = WriterTools.createTextDoc( (XMultiServiceFactory) Param.getMSF());
195     }
196 }