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