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 java.io.PrintWriter;
27 
28 import lib.StatusException;
29 import lib.TestCase;
30 import lib.TestEnvironment;
31 import lib.TestParameters;
32 import util.SOfficeFactory;
33 
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.text.TextContentAnchorType;
37 import com.sun.star.text.XText;
38 import com.sun.star.text.XTextContent;
39 import com.sun.star.text.XTextCursor;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.uno.XInterface;
43 
44 public class SwXTextGraphicObject extends TestCase {
45 
46     XTextDocument xTextDoc;
47 
48     /**
49      * in general this method creates a testdocument
50      *
51      *  @param tParam    class which contains additional test parameters
52      *  @param log        class to log the test state and result
53      *
54      *
55      *  @see TestParameters
56      *    @see PrintWriter
57      *
58      */
initialize( TestParameters tParam, PrintWriter log )59     protected void initialize( TestParameters tParam, PrintWriter log ) {
60         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
61         try {
62             log.println( "creating a textdoc" );
63             xTextDoc = SOF.createTextDoc( null );
64         } catch ( Exception e ) {
65             // Some exception occures.FAILED
66             e.printStackTrace( log );
67             throw new StatusException( "Couldn't create document", e );
68         }
69     }
70 
71     /**
72      * in general this method disposes the testenvironment and document
73      *
74      *  @param tParam    class which contains additional test parameters
75      *  @param log        class to log the test state and result
76      *
77      *
78      *  @see TestParameters
79      *    @see PrintWriter
80      *
81      */
cleanup( TestParameters tParam, PrintWriter log )82     protected void cleanup( TestParameters tParam, PrintWriter log ) {
83         log.println( "    disposing xDrawDoc " );
84         util.DesktopTools.closeDoc(xTextDoc);
85     }
86 
87 
88     /**
89      *    creating a Testenvironment for the interfaces to be tested
90      *
91      *  @param tParam    class which contains additional test parameters
92      *  @param log        class to log the test state and result
93      *
94      *  @return    Status class
95      *
96      *  @see TestParameters
97      *    @see PrintWriter
98      */
createTestEnvironment(TestParameters tParam, PrintWriter log)99     protected TestEnvironment createTestEnvironment
100                 (TestParameters tParam, PrintWriter log) {
101 
102             XInterface oObj = null;
103             Object oGObject = null;
104             Object xTextFrame = null;
105             SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
106 
107             Object instance = null;
108 
109             try {
110                 oGObject = SOF.createInstance
111                     (xTextDoc,"com.sun.star.text.GraphicObject");
112                 instance = SOF.createInstance
113                     (xTextDoc,"com.sun.star.text.GraphicObject");
114                 xTextFrame = SOF.createTextFrame(xTextDoc, 500, 500);
115             }
116             catch (Exception ex) {
117                 log.println("Couldn't create instance");
118                 ex.printStackTrace(log);
119                 throw new StatusException("Couldn't create instance", ex );
120             }
121 
122             oObj = (XInterface) oGObject;
123 
124             XText the_text = xTextDoc.getText();
125             XTextCursor the_cursor = the_text.createTextCursor();
126             XTextContent the_content = (XTextContent)
127                 UnoRuntime.queryInterface(XTextContent.class,oObj);
128 
129             log.println("inserting Frame");
130             try{
131                 XTextContent Framecontent = (XTextContent) UnoRuntime.queryInterface(
132                                                    XTextContent.class, xTextFrame);
133                 the_text.insertTextContent(the_cursor, Framecontent, true);
134             } catch (Exception e) {
135                 System.out.println("Couldn't insert text frame");
136                 e.printStackTrace();
137                 throw new StatusException("Couldn't insert text frame", e );
138             }
139 
140 
141            log.println( "inserting graphic" );
142             try {
143                 the_text.insertTextContent(the_cursor,the_content,true);
144             } catch (Exception e) {
145                 System.out.println("Couldn't insert Content");
146                 e.printStackTrace();
147                 throw new StatusException("Couldn't insert Content", e );
148             }
149 
150             log.println( "adding graphic" );
151             XPropertySet oProps = (XPropertySet)
152                 UnoRuntime.queryInterface(XPropertySet.class,oObj);
153             try {
154                 String wat = util.utils.getFullTestURL("space-metal.jpg");
155                 oProps.setPropertyValue("AnchorType",
156                     TextContentAnchorType.AT_PARAGRAPH);
157                 oProps.setPropertyValue("GraphicURL",wat);
158                 oProps.setPropertyValue("HoriOrientPosition",new Integer(5500));
159                 oProps.setPropertyValue("VertOrientPosition",new Integer(4200));
160                 oProps.setPropertyValue("Width",new Integer(4400));
161                 oProps.setPropertyValue("Height",new Integer(4000));
162             } catch (Exception e) {
163                 System.out.println("Couldn't set property 'GraphicURL'");
164                 e.printStackTrace();
165                 throw new StatusException
166                     ("Couldn't set property 'GraphicURL'", e );
167             }
168 
169                     TestEnvironment tEnv = new TestEnvironment( oObj );
170 
171                     log.println( "adding ObjRelation for XShape "
172                         +"(get/setPosition won't work there)" );
173                     tEnv.addObjRelation("NoPos", "SwXTextGraphicObject");
174                     tEnv.addObjRelation("NoSetSize","SwXTextGraphicObject");
175             Object ImgMap = null;
176             //creating ObjectRelation for the property
177             // 'ImageMap' of 'TextGraphicObject'
178             try {
179                 XMultiServiceFactory xDocMSF = (XMultiServiceFactory)
180                     UnoRuntime.queryInterface
181                     (XMultiServiceFactory.class,xTextDoc);
182                 ImgMap = xDocMSF.createInstance
183                     ("com.sun.star.image.ImageMapRectangleObject");
184             }
185             catch (Exception ex) {
186                 ex.printStackTrace();
187             }
188 
189             tEnv.addObjRelation("IMGMAP",ImgMap);
190 
191             tEnv.addObjRelation("CONTENT", (XTextContent)
192                         UnoRuntime.queryInterface(XTextContent.class,instance));
193             tEnv.addObjRelation("RANGE", xTextDoc.getText().createTextCursor());
194 
195             //object relation for text.BaseFrameProperties
196             tEnv.addObjRelation("TextFrame", xTextFrame);
197 
198             return tEnv;
199 
200     } // finish method getTestEnvironment
201 
202 }    // finish class SwXTextGraphicObject
203 
204