xref: /AOO41X/main/qadevOOo/tests/java/mod/_sw/SwXTextView.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package mod._sw;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException;
31*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
32*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage;
33*cdf0e10cSrcweir import com.sun.star.drawing.XShape;
34*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
35*cdf0e10cSrcweir import java.io.PrintWriter;
36*cdf0e10cSrcweir import java.util.Comparator;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir import lib.StatusException;
39*cdf0e10cSrcweir import lib.TestCase;
40*cdf0e10cSrcweir import lib.TestEnvironment;
41*cdf0e10cSrcweir import lib.TestParameters;
42*cdf0e10cSrcweir import util.SOfficeFactory;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
45*cdf0e10cSrcweir import com.sun.star.form.XForm;
46*cdf0e10cSrcweir import com.sun.star.frame.XController;
47*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
48*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
49*cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
50*cdf0e10cSrcweir import com.sun.star.text.XText;
51*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
52*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
53*cdf0e10cSrcweir import com.sun.star.text.XTextFrame;
54*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
55*cdf0e10cSrcweir import com.sun.star.uno.Type;
56*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
57*cdf0e10cSrcweir import com.sun.star.util.XSearchDescriptor;
58*cdf0e10cSrcweir import com.sun.star.util.XSearchable;
59*cdf0e10cSrcweir import com.sun.star.view.XSelectionSupplier;
60*cdf0e10cSrcweir import util.FormTools;
61*cdf0e10cSrcweir import util.WriterTools;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir /**
64*cdf0e10cSrcweir  *
65*cdf0e10cSrcweir  * initial description
66*cdf0e10cSrcweir  * @see com.sun.star.text.XTextViewCursorSupplier
67*cdf0e10cSrcweir  * @see com.sun.star.view.XControlAccess
68*cdf0e10cSrcweir  * @see com.sun.star.view.XSelectionSupplier
69*cdf0e10cSrcweir  * @see com.sun.star.view.XViewSettingsSupplier
70*cdf0e10cSrcweir  *
71*cdf0e10cSrcweir  */
72*cdf0e10cSrcweir public class SwXTextView extends TestCase {
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     XTextDocument xTextDoc;
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     boolean debug = false;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir     /**
79*cdf0e10cSrcweir      * in general this method creates a testdocument
80*cdf0e10cSrcweir      *
81*cdf0e10cSrcweir      *  @param tParam    class which contains additional test parameters
82*cdf0e10cSrcweir      *  @param log        class to log the test state and result
83*cdf0e10cSrcweir      *
84*cdf0e10cSrcweir      *
85*cdf0e10cSrcweir      *  @see TestParameters
86*cdf0e10cSrcweir      *  *    @see PrintWriter
87*cdf0e10cSrcweir      *
88*cdf0e10cSrcweir      */
89*cdf0e10cSrcweir     protected void initialize( TestParameters tParam, PrintWriter log ) {
90*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         try {
93*cdf0e10cSrcweir             log.println( "creating a textdocument" );
94*cdf0e10cSrcweir             xTextDoc = SOF.createTextDoc( null );
95*cdf0e10cSrcweir             debug = tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         } catch ( com.sun.star.uno.Exception e ) {
98*cdf0e10cSrcweir             // Some exception occures.FAILED
99*cdf0e10cSrcweir             e.printStackTrace( log );
100*cdf0e10cSrcweir             throw new StatusException( "Couldn't create document", e );
101*cdf0e10cSrcweir         }
102*cdf0e10cSrcweir     }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir     /**
105*cdf0e10cSrcweir      * in general this method disposes the testenvironment and document
106*cdf0e10cSrcweir      *
107*cdf0e10cSrcweir      *  @param tParam    class which contains additional test parameters
108*cdf0e10cSrcweir      *  @param log        class to log the test state and result
109*cdf0e10cSrcweir      *
110*cdf0e10cSrcweir      *
111*cdf0e10cSrcweir      *  @see TestParameters
112*cdf0e10cSrcweir      *  *    @see PrintWriter
113*cdf0e10cSrcweir      *
114*cdf0e10cSrcweir      */
115*cdf0e10cSrcweir     protected void cleanup( TestParameters tParam, PrintWriter log ) {
116*cdf0e10cSrcweir         log.println( "    disposing xTextDoc " );
117*cdf0e10cSrcweir         util.DesktopTools.closeDoc(xTextDoc);
118*cdf0e10cSrcweir     }
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     /**
122*cdf0e10cSrcweir      *  *    creating a Testenvironment for the interfaces to be tested
123*cdf0e10cSrcweir      *
124*cdf0e10cSrcweir      *  @param tParam    class which contains additional test parameters
125*cdf0e10cSrcweir      *  @param log        class to log the test state and result
126*cdf0e10cSrcweir      *
127*cdf0e10cSrcweir      *  @return    Status class
128*cdf0e10cSrcweir      *
129*cdf0e10cSrcweir      *  @see TestParameters
130*cdf0e10cSrcweir      *  *    @see PrintWriter
131*cdf0e10cSrcweir      */
132*cdf0e10cSrcweir     public TestEnvironment createTestEnvironment( TestParameters tParam,
133*cdf0e10cSrcweir                               PrintWriter log )throws StatusException {
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         // creation of testobject here
137*cdf0e10cSrcweir         log.println( "creating a test environment" );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         XController xContr = xTextDoc.getCurrentController();
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment(xContr);
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         util.dbg.getSuppServices(xContr);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir         SOfficeFactory SOF=SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
146*cdf0e10cSrcweir         XTextFrame first =null;
147*cdf0e10cSrcweir         XTextFrame second =null;
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         Object oFrame1 = null;
150*cdf0e10cSrcweir         Object oFrame2 = null;
151*cdf0e10cSrcweir         try {
152*cdf0e10cSrcweir             XText oText = xTextDoc.getText();
153*cdf0e10cSrcweir             XTextCursor oCursor = oText.createTextCursor();
154*cdf0e10cSrcweir             oFrame1 = SOF.createInstance
155*cdf0e10cSrcweir                 (xTextDoc, "com.sun.star.text.TextFrame" );
156*cdf0e10cSrcweir             first = (XTextFrame)UnoRuntime.queryInterface
157*cdf0e10cSrcweir                 ( XTextFrame.class, oFrame1);
158*cdf0e10cSrcweir             oText.insertTextContent(oCursor,first, false);
159*cdf0e10cSrcweir             first.getText().setString("Frame 1");
160*cdf0e10cSrcweir             oFrame2 = SOF.createInstance(xTextDoc, "com.sun.star.text.TextFrame" );
161*cdf0e10cSrcweir             second = (XTextFrame)UnoRuntime.queryInterface
162*cdf0e10cSrcweir                 ( XTextFrame.class, oFrame2);
163*cdf0e10cSrcweir             oText.insertTextContent(oCursor,second, false);
164*cdf0e10cSrcweir             second.getText().setString("Frame 2");
165*cdf0e10cSrcweir             oText.insertString( oCursor,
166*cdf0e10cSrcweir                 "SwXTextRanges...SwXTextRanges...SwXTextRanges", false);
167*cdf0e10cSrcweir             oText.insertControlCharacter( oCursor,
168*cdf0e10cSrcweir                 ControlCharacter.PARAGRAPH_BREAK, false);
169*cdf0e10cSrcweir             oText.insertString( oCursor,
170*cdf0e10cSrcweir                 "bla...bla...", false);
171*cdf0e10cSrcweir         } catch (Exception Ex ) {
172*cdf0e10cSrcweir             Ex.printStackTrace(log);
173*cdf0e10cSrcweir             throw new StatusException("Couldn't insert text table ", Ex);
174*cdf0e10cSrcweir         }
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir         XSearchable oSearch = (XSearchable)UnoRuntime.queryInterface
177*cdf0e10cSrcweir             (XSearchable.class, xTextDoc);
178*cdf0e10cSrcweir         XSearchDescriptor xSDesc = oSearch.createSearchDescriptor();
179*cdf0e10cSrcweir         xSDesc.setSearchString("SwXTextRanges");
180*cdf0e10cSrcweir         XIndexAccess textRanges1 = oSearch.findAll(xSDesc);
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         xSDesc.setSearchString("bla");
183*cdf0e10cSrcweir         XIndexAccess textRanges2 = oSearch.findAll(xSDesc);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         tEnv.addObjRelation("Selections", new Object[] {
186*cdf0e10cSrcweir             oFrame1, oFrame2, textRanges1, textRanges2});
187*cdf0e10cSrcweir         tEnv.addObjRelation("Comparer", new Comparator() {
188*cdf0e10cSrcweir             public int compare(Object o1, Object o2) {
189*cdf0e10cSrcweir                 XServiceInfo serv1 = (XServiceInfo)
190*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XServiceInfo.class, o1);
191*cdf0e10cSrcweir                 XServiceInfo serv2 = (XServiceInfo)
192*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XServiceInfo.class, o2);
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir                 String implName1 = serv1.getImplementationName();
195*cdf0e10cSrcweir                 String implName2 = serv2.getImplementationName();
196*cdf0e10cSrcweir                 if (!implName1.equals(implName2)) {
197*cdf0e10cSrcweir                     return -1;
198*cdf0e10cSrcweir                 }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir                 XIndexAccess indAc1 = (XIndexAccess)
201*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XIndexAccess.class, o1);
202*cdf0e10cSrcweir                 XIndexAccess indAc2 = (XIndexAccess)
203*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XIndexAccess.class, o2);
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir                 if (indAc1 != null && indAc2 != null) {
206*cdf0e10cSrcweir                     int c1 = indAc1.getCount();
207*cdf0e10cSrcweir                     int c2 = indAc2.getCount();
208*cdf0e10cSrcweir                     return c1 == c2 ? 0 : 1;
209*cdf0e10cSrcweir                 }
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir                 XText text1 = (XText)
212*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XText.class, o1);
213*cdf0e10cSrcweir                 XText text2 = (XText)
214*cdf0e10cSrcweir                     UnoRuntime.queryInterface(XText.class, o2);
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir                 if (text1 != null && text2 != null) {
217*cdf0e10cSrcweir                     return text1.getString().equals(text2.getString()) ? 0 : 1;
218*cdf0e10cSrcweir                 }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir                 return -1;
221*cdf0e10cSrcweir             }
222*cdf0e10cSrcweir             public boolean equals(Object obj) {
223*cdf0e10cSrcweir                 return compare(this, obj) == 0;
224*cdf0e10cSrcweir             } });
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir         XSelectionSupplier xsel = (XSelectionSupplier)
227*cdf0e10cSrcweir             UnoRuntime.queryInterface(XSelectionSupplier.class,xContr);
228*cdf0e10cSrcweir         try {
229*cdf0e10cSrcweir             xsel.select(second);
230*cdf0e10cSrcweir         } catch (Exception e) {
231*cdf0e10cSrcweir             log.println("Couldn't select");
232*cdf0e10cSrcweir             throw new StatusException( "Couldn't select", e );
233*cdf0e10cSrcweir         }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         tEnv.addObjRelation("DOCUMENT",xTextDoc);
236*cdf0e10cSrcweir         XForm myForm = null;
237*cdf0e10cSrcweir         String kindOfControl="CommandButton";
238*cdf0e10cSrcweir         XShape aShape = null;
239*cdf0e10cSrcweir         try{
240*cdf0e10cSrcweir             log.println("adding contol shape '" + kindOfControl + "'");
241*cdf0e10cSrcweir             aShape = FormTools.createControlShape(xTextDoc, 3000,
242*cdf0e10cSrcweir                                                             4500, 15000, 10000,
243*cdf0e10cSrcweir                                                             kindOfControl);
244*cdf0e10cSrcweir         } catch (Exception e){
245*cdf0e10cSrcweir             e.printStackTrace(log);
246*cdf0e10cSrcweir             throw new StatusException("Couldn't create following control shape : '" +
247*cdf0e10cSrcweir                                         kindOfControl + "': ", e);
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir         log.println("adding relation for com.sun.star.view.XFormLayerAccess: XForm");
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir         WriterTools.getDrawPage(xTextDoc).add((XShape) aShape);
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir         try {
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir             XDrawPage xDP = WriterTools.getDrawPage(xTextDoc);
259*cdf0e10cSrcweir             if (xDP == null)
260*cdf0e10cSrcweir                 log.println("ERROR: could not get DrawPage");
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir             XNameContainer xForms = FormTools.getForms(xDP);
263*cdf0e10cSrcweir             if (xForms == null)
264*cdf0e10cSrcweir                 log.println("ERROR: could not get Forms");
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir                 log.println("the draw page contains folowing elemtens:");
267*cdf0e10cSrcweir                 String[] elements = FormTools.getForms(WriterTools.getDrawPage(xTextDoc)).getElementNames();
268*cdf0e10cSrcweir                 for (int i = 0; i< elements.length; i++){
269*cdf0e10cSrcweir                     log.println("Element[" + i + "] :" + elements[i]);
270*cdf0e10cSrcweir                 }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir             myForm = (XForm) AnyConverter.toObject(new Type(XForm.class), xForms.getByName("Standard"));
273*cdf0e10cSrcweir                 if (myForm == null){
274*cdf0e10cSrcweir                     log.println("ERROR: could not get 'Standard' from drawpage!");
275*cdf0e10cSrcweir             if (debug){
276*cdf0e10cSrcweir                 log.println("the draw page contains folowing elemtens:");
277*cdf0e10cSrcweir //                String[] elements = FormTools.getForms(WriterTools.getDrawPage(xTextDoc)).getElementNames();
278*cdf0e10cSrcweir //                for (int i = 0; i< elements.length; i++){
279*cdf0e10cSrcweir //                    log.println("Element[" + i + "] :" + elements[i]);
280*cdf0e10cSrcweir //                }
281*cdf0e10cSrcweir             }
282*cdf0e10cSrcweir             }
283*cdf0e10cSrcweir             else
284*cdf0e10cSrcweir                 tEnv.addObjRelation("XFormLayerAccess.XForm", myForm);
285*cdf0e10cSrcweir         } catch (WrappedTargetException ex) {
286*cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
287*cdf0e10cSrcweir         } catch (com.sun.star.lang.IllegalArgumentException ex) {
288*cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
289*cdf0e10cSrcweir         } catch (NoSuchElementException ex) {
290*cdf0e10cSrcweir             log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString());
291*cdf0e10cSrcweir         }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir         return tEnv;
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     } // finish method getTestEnvironment
297*cdf0e10cSrcweir }    // finish class SwXTextView
298