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