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.Status;
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.AccessibilityTools;
37 import util.WriterTools;
38 import util.utils;
39 
40 import com.sun.star.accessibility.AccessibleRole;
41 import com.sun.star.accessibility.XAccessible;
42 import com.sun.star.awt.XWindow;
43 import com.sun.star.frame.XController;
44 import com.sun.star.frame.XDispatch;
45 import com.sun.star.frame.XDispatchProvider;
46 import com.sun.star.frame.XModel;
47 import com.sun.star.lang.XMultiServiceFactory;
48 import com.sun.star.text.ControlCharacter;
49 import com.sun.star.text.XText;
50 import com.sun.star.text.XTextCursor;
51 import com.sun.star.text.XTextDocument;
52 import com.sun.star.uno.UnoRuntime;
53 import com.sun.star.uno.XInterface;
54 import com.sun.star.util.URL;
55 import com.sun.star.util.XURLTransformer;
56 
57 public class SwAccessibleDocumentPageView 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.
64     * Switchs the document to Print Preview mode.
65     * Obtains accissible object for the document page view.
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<25; i++){
84                 oText.insertString( oCursor,"Paragraph Number: " + i, false);
85                 oText.insertString( oCursor,
86                     " The quick brown fox jumps over the lazy Dog: SwAccessibleDocumentPageView",
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: SwAccessibleDocumentPageView",
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         XController xController = xTextDoc.getCurrentController();
104 
105         XModel aModel = (XModel)
106             UnoRuntime.queryInterface(XModel.class, xTextDoc);
107 
108         //switch to 'Print Preview' mode
109         try {
110             XDispatchProvider xDispProv = (XDispatchProvider)
111                 UnoRuntime.queryInterface(XDispatchProvider.class, xController);
112             XURLTransformer xParser = (com.sun.star.util.XURLTransformer)
113                 UnoRuntime.queryInterface(XURLTransformer.class,
114             ( (XMultiServiceFactory) Param.getMSF()).createInstance("com.sun.star.util.URLTransformer"));
115             // Because it's an in/out parameter we must use an array of URL objects.
116             URL[] aParseURL = new URL[1];
117             aParseURL[0] = new URL();
118             aParseURL[0].Complete = ".uno:PrintPreview";
119             xParser.parseStrict(aParseURL);
120             URL aURL = aParseURL[0];
121             XDispatch xDispatcher = xDispProv.queryDispatch(aURL, "", 0);
122             if(xDispatcher != null)
123                 xDispatcher.dispatch( aURL, null );
124         } catch (com.sun.star.uno.Exception e) {
125             log.println("Couldn't change mode");
126             throw new StatusException(Status.failed("Couldn't change mode"));
127         }
128 
129         shortWait();
130 
131         AccessibilityTools at = new AccessibilityTools();
132 
133         XWindow xWindow = at.getCurrentWindow( (XMultiServiceFactory) Param.getMSF(), aModel);
134         XAccessible xRoot = at.getAccessibleObject(xWindow);
135 
136         at.getAccessibleObjectForRole(xRoot, AccessibleRole.DOCUMENT );
137 
138         oObj = AccessibilityTools.SearchedContext;
139 
140         log.println("ImplementationName " + utils.getImplName(oObj));
141 
142         TestEnvironment tEnv = new TestEnvironment(oObj);
143 
144         final XText the_text = oText;
145 
146         tEnv.addObjRelation("EventProducer",
147             new ifc.accessibility._XAccessibleEventBroadcaster.EventProducer() {
148                 public void fireEvent() {
149                     String oldText = the_text.getString();
150                     the_text.setString("EVENT FIRED");
151                     shortWait();
152                     the_text.setString(oldText);
153                 }
154             });
155 
156         return tEnv;
157 
158     }
159 
160 
161     /**
162     * Sleeps for 1 sec. to allow StarOffice to react on <code>
163     * reset</code> call.
164     */
165     private void shortWait() {
166         try {
167             Thread.sleep(2000) ;
168         } catch (InterruptedException e) {
169             log.println("While waiting :" + e) ;
170         }
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. In the
188      * implementation does nothing. Subclasses can override to initialize
189      * objects shared among all <code>TestEnvironment</code>s.
190      *
191      * @param tParam test parameters
192      * @param log writer to log information while testing
193      *
194      * @see #initializeTestCase()
195      */
196     protected void initialize(TestParameters Param, PrintWriter log) {
197         log.println( "creating a text document" );
198         xTextDoc = WriterTools.createTextDoc( (XMultiServiceFactory) Param.getMSF());
199     }
200 }