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