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 java.io.PrintWriter;
31 
32 import lib.StatusException;
33 import lib.TestCase;
34 import lib.TestEnvironment;
35 import lib.TestParameters;
36 import util.SOfficeFactory;
37 
38 import com.sun.star.beans.XPropertySet;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.text.XText;
41 import com.sun.star.text.XTextContent;
42 import com.sun.star.text.XTextCursor;
43 import com.sun.star.text.XTextDocument;
44 import com.sun.star.text.XTextGraphicObjectsSupplier;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 
48 public class SwXTextGraphicObjects extends TestCase {
49 
50     XTextDocument xTextDoc;
51 
52     /**
53      * in general this method creates a testdocument
54      *
55      *  @param tParam    class which contains additional test parameters
56      *  @param log        class to log the test state and result
57      *
58      *
59      *  @see TestParameters
60      *    @see PrintWriter
61      *
62      */
63     protected void initialize( TestParameters tParam, PrintWriter log ) {
64         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
65         try {
66             log.println( "creating a textdoc" );
67             xTextDoc = SOF.createTextDoc( null );;
68         } catch ( Exception e ) {
69             // Some exception occures.FAILED
70             e.printStackTrace( log );
71             throw new StatusException( "Couldn't create document", e );
72         }
73     }
74 
75     /**
76      * in general this method disposes the testenvironment and document
77      *
78      *  @param tParam    class which contains additional test parameters
79      *  @param log        class to log the test state and result
80      *
81      *
82      *  @see TestParameters
83      *    @see PrintWriter
84      *
85      */
86     protected void cleanup( TestParameters tParam, PrintWriter log ) {
87         log.println( "    disposing xTextDoc " );
88         util.DesktopTools.closeDoc(xTextDoc);
89     }
90 
91 
92     /**
93      *    creating a Testenvironment for the interfaces to be tested
94      *
95      *  @param tParam    class which contains additional test parameters
96      *  @param log        class to log the test state and result
97      *
98      *  @return    Status class
99      *
100      *  @see TestParameters
101      *    @see PrintWriter
102      */
103     public TestEnvironment createTestEnvironment( TestParameters tParam,
104                                                   PrintWriter log )
105                                                     throws StatusException {
106 
107         XInterface oObj = null;
108         Object oGObject = null;
109         SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
110 
111         try {
112             oGObject = SOF.createInstance
113                 (xTextDoc,"com.sun.star.text.GraphicObject");
114         }
115         catch (Exception ex) {
116             log.println("Couldn't create instance");
117             ex.printStackTrace(log);
118             throw new StatusException("Couldn't create instance", ex );
119         }
120 
121         oObj = (XInterface) oGObject;
122 
123         XText the_text = xTextDoc.getText();
124         XTextCursor the_cursor = the_text.createTextCursor();
125         XTextContent the_content = (XTextContent)
126                             UnoRuntime.queryInterface(XTextContent.class,oObj);
127 
128        log.println( "inserting graphic" );
129         try {
130             the_text.insertTextContent(the_cursor,the_content,true);
131         } catch (Exception e) {
132             System.out.println("Couldn't insert Content");
133             e.printStackTrace();
134             throw new StatusException("Couldn't insert Content", e );
135         }
136 
137         log.println( "adding graphic" );
138         XPropertySet oProps = (XPropertySet)
139                             UnoRuntime.queryInterface(XPropertySet.class,oObj);
140         try {
141             String wat = util.utils.getFullTestURL("space-metal.jpg");
142             oProps.setPropertyValue("GraphicURL",wat);
143             oProps.setPropertyValue("HoriOrientPosition",new Integer(5500));
144             oProps.setPropertyValue("VertOrientPosition",new Integer(4200));
145             oProps.setPropertyValue("Width",new Integer(4400));
146             oProps.setPropertyValue("Height",new Integer(4000));
147         } catch (Exception e) {
148             System.out.println("Couldn't set property 'GraphicURL'");
149             e.printStackTrace();
150             throw new StatusException("Couldn't set property 'GraphicURL'", e );
151         }
152 
153         XTextGraphicObjectsSupplier xTGS = (XTextGraphicObjectsSupplier)
154             UnoRuntime.queryInterface(XTextGraphicObjectsSupplier.class,
155             xTextDoc);
156         oObj = xTGS.getGraphicObjects();
157 
158         TestEnvironment tEnv = new TestEnvironment( oObj );
159 
160         return tEnv;
161 
162     } // finish method getTestEnvironment
163 
164 }    // finish class SwXTextGraphicObjects
165 
166