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 package mod._sw;
24 
25 import com.sun.star.beans.XPropertySet;
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.container.XIndexAccess;
35 import com.sun.star.container.XNameAccess;
36 import com.sun.star.lang.XMultiServiceFactory;
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.text.XTextEmbeddedObjectsSupplier;
42 import com.sun.star.text.XTextFrame;
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 import com.sun.star.util.XCloseable;
48 
49 
50 /**
51  *
52  * initial description
53  * @see com.sun.star.container.XNamed
54  * @see com.sun.star.document.XEmbeddedObjectSupplier
55  * @see com.sun.star.lang.XComponent
56  * @see com.sun.star.text.TextEmbeddedObject
57  * @see com.sun.star.text.XTextContent
58  * @see com.sun.star.text.XTextEmbeddedObject
59  *
60  */
61 public class SwXTextEmbeddedObject extends TestCase {
62     XTextDocument xTextDoc;
63 
64     /**
65      * in general this method disposes the testenvironment and document
66      *
67      *  @param tParam    class which contains additional test parameters
68      *  @param log        class to log the test state and result
69      *
70      *
71      *  @see TestParameters
72      *    @see PrintWriter
73      *
74      */
cleanup(TestParameters tParam, PrintWriter log)75     protected void cleanup(TestParameters tParam, PrintWriter log) {
76         log.println("    disposing xTextDoc ");
77 
78         try {
79             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
80                                         XCloseable.class, xTextDoc);
81             closer.close(true);
82         } catch (com.sun.star.util.CloseVetoException e) {
83             log.println("couldn't close document");
84         } catch (com.sun.star.lang.DisposedException e) {
85             log.println("couldn't close document");
86         }
87     }
88 
89     /**
90      *    creating a Testenvironment for the interfaces to be tested
91      *
92      *  @param tParam    class which contains additional test parameters
93      *  @param log        class to log the test state and result
94      *
95      *  @return    Status class
96      *
97      *  @see TestParameters
98      *    @see PrintWriter
99      */
createTestEnvironment(TestParameters tParam, PrintWriter log)100     protected TestEnvironment createTestEnvironment(TestParameters tParam,
101                                                     PrintWriter log) {
102         XInterface oObj = null;
103 
104         // create testobject here
105         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory) tParam.getMSF());
106         try {
107             xTextDoc = SOF.createTextDoc(null);
108         } catch (com.sun.star.uno.Exception e) {
109             e.printStackTrace(log);
110             throw new StatusException("Couldn't open document", e);
111         }
112 
113         XTextCursor xCursor = xTextDoc.getText().createTextCursor();
114         try {
115             XMultiServiceFactory xMultiServiceFactory = (XMultiServiceFactory)
116                 UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
117             Object o = xMultiServiceFactory.createInstance("com.sun.star.text.TextEmbeddedObject" );
118             XTextContent xTextContent = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, o);
119             String sChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
120             XPropertySet xPropertySet = (XPropertySet)
121                 UnoRuntime.queryInterface(XPropertySet.class, xTextContent);
122             xPropertySet.setPropertyValue( "CLSID", sChartClassID );
123 
124             xTextDoc.getText().insertTextContent( xCursor, xTextContent, false );
125         }
126         catch(com.sun.star.uno.Exception e) {
127             e.printStackTrace((java.io.PrintWriter)log);
128         }
129 
130         XTextEmbeddedObjectsSupplier oTEOS = (XTextEmbeddedObjectsSupplier) UnoRuntime.queryInterface(
131                                                      XTextEmbeddedObjectsSupplier.class,
132                                                      xTextDoc);
133 
134         XNameAccess oEmObj = oTEOS.getEmbeddedObjects();
135         XIndexAccess oEmIn = (XIndexAccess) UnoRuntime.queryInterface(
136                                      XIndexAccess.class, oEmObj);
137 
138         try {
139             oObj = (XInterface) AnyConverter.toObject(
140                            new Type(XInterface.class), oEmIn.getByIndex(0));
141         } catch (com.sun.star.uno.Exception e) {
142             e.printStackTrace(log);
143             throw new StatusException("Couldn't get Object", e);
144         }
145 
146         TestEnvironment tEnv = new TestEnvironment(oObj);
147 
148         tEnv.addObjRelation("NoAttach", "SwXTextEmbeddedObject");
149 
150         XTextFrame aFrame = SOF.createTextFrame(xTextDoc, 500, 500);
151         XText oText = xTextDoc.getText();
152         XTextCursor oCursor = oText.createTextCursor();
153         XTextContent the_content = (XTextContent) UnoRuntime.queryInterface(
154                                            XTextContent.class, aFrame);
155 
156         try {
157             oText.insertTextContent(oCursor, the_content, true);
158         } catch (com.sun.star.lang.IllegalArgumentException e) {
159             log.println("Couldn't insert frame " + e.getMessage());
160         }
161 
162         tEnv.addObjRelation("TextFrame", aFrame);
163 
164         tEnv.addObjRelation("NoSetSize", "SwXTextEmbeddedObject");
165         tEnv.addObjRelation("NoPos", "SwXTextEmbeddedObject");
166 
167         return tEnv;
168     } // finish method getTestEnvironment
169 } // finish class SwXTextEmbeddedObject
170