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