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 package mod._svx;
28 
29 import com.sun.star.beans.XPropertySet;
30 import com.sun.star.document.XExporter;
31 import com.sun.star.drawing.XShape;
32 import com.sun.star.lang.XComponent;
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.ucb.XSimpleFileAccess;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.uno.XInterface;
37 import com.sun.star.util.URL;
38 
39 import java.io.PrintWriter;
40 
41 import lib.StatusException;
42 import lib.TestCase;
43 import lib.TestEnvironment;
44 import lib.TestParameters;
45 
46 import util.DrawTools;
47 import util.SOfficeFactory;
48 import util.XMLTools;
49 
50 
51 /**
52  * Test for object which is represented by service
53  * <code>com.sun.star.drawing.GraphicExportFilter</code>. <p>
54  *
55  * Object implements the following interfaces :
56  * <ul>
57  *  <li> <code>com::sun::star::document::XFilter</code></li>
58  *  <li> <code>com::sun::star::document::XMimeTypeInfo</code></li>
59  *  <li> <code>com::sun::star::document::XExporter</code></li>
60  * </ul> <p>
61  *
62  * The following files used by this test :
63  * <ul>
64  *  <li><b> space-metal.jpg </b> : the file used for GraphicObject
65  *  creation. This image must be then exported. </li>
66  * </ul> <p>
67  *
68  * This object test <b> is NOT </b> designed to be run in several
69  * threads concurently.
70  *
71  * @see com.sun.star.document.XFilter
72  * @see com.sun.star.document.XMimeTypeInfo
73  * @see com.sun.star.document.XExporter
74  * @see ifc.document._XFilter
75  * @see ifc.document._XMimeTypeInfo
76  * @see ifc.document._XExporter
77  */
78 public class GraphicExporter extends TestCase {
79     static XComponent xDrawDoc;
80 
81     /**
82      * Creates a new draw document.
83      */
84     protected void initialize(TestParameters tParam, PrintWriter log) {
85         log.println("creating a drawdoc");
86         xDrawDoc = DrawTools.createDrawDoc(
87                            (XMultiServiceFactory) tParam.getMSF());
88     }
89 
90     /**
91      * Disposes the draw document created before
92      */
93     protected void cleanup(TestParameters tParam, PrintWriter log) {
94         log.println("    disposing xDrawDoc ");
95         util.DesktopTools.closeDoc(xDrawDoc);
96     }
97 
98     /**
99      * Creating a Testenvironment for the interfaces to be tested.
100      * Creates an instance of the service
101      * <code>com.sun.star.drawing.GraphicExportFilter</code> as
102      * a tested component. Then a <code>GraphicObjectShape</code>
103      * instance is added into the document and its image is obtained
104      * from JPEG file. This shape content is intended to be exported.
105      *
106      *     Object relations created :
107      * <ul>
108      *  <li> <code>'MediaDescriptor'</code> for
109      *      {@link ifc.document._XFilter} :
110      *      descriptor which contains target file name in
111      *      the temporary directory, file type (JPEG)
112      *      </li>
113      *  <li> <code>'XFilter.Checker'</code> for
114      *      {@link ifc.document._XFilter} :
115      *      checks if the target file exists.
116      *      In the case if SOffice is started in 'Hide' mode
117      *      ('soapi.test.hidewindows' test parameter is 'true')
118      *      the checker always returns <code>true</code>.
119      *      </li>
120      *  <li> <code>'SourceDocument'</code> for
121      *      {@link ifc.document._XExporter} :
122      *      the <code>GraphicObjectShape</code> component
123      *      with loaded image.
124      *   </li>
125      * </ul>
126      */
127     protected TestEnvironment createTestEnvironment(TestParameters tParam,
128                                                     PrintWriter log) {
129         XInterface oObj = null;
130         XShape oShape = null;
131         Object go = null;
132 
133 
134         // creation of testobject here
135         // first we write what we are intend to do to log file
136         log.println("creating a test environment");
137 
138         try {
139             go = ((XMultiServiceFactory) tParam.getMSF()).createInstance(
140                          "com.sun.star.drawing.GraphicExportFilter");
141         } catch (com.sun.star.uno.Exception e) {
142             log.println("Couldn't create instance");
143             e.printStackTrace(log);
144         }
145 
146         // create testobject here
147         SOfficeFactory SOF = SOfficeFactory.getFactory(
148                                      (XMultiServiceFactory) tParam.getMSF());
149         oShape = SOF.createShape(xDrawDoc, 5000, 5000, 1500, 1000,
150                                  "GraphicObject");
151         DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc, 0)).add(oShape);
152 
153         XPropertySet oShapeProps = (XPropertySet) UnoRuntime.queryInterface(
154                                            XPropertySet.class, oShape);
155         XComponent xComp = null;
156 
157         try {
158             oShapeProps.setPropertyValue("GraphicURL",
159                                          util.utils.getFullTestURL(
160                                                  "space-metal.jpg"));
161             xComp = (XComponent) UnoRuntime.queryInterface(XComponent.class,
162                                                            oShape);
163 
164             XExporter xEx = (XExporter) UnoRuntime.queryInterface(
165                                     XExporter.class, (XInterface) go);
166             xEx.setSourceDocument(xComp);
167         } catch (com.sun.star.lang.WrappedTargetException e) {
168             e.printStackTrace(log);
169             throw new StatusException("Error while preparing component", e);
170         } catch (com.sun.star.lang.IllegalArgumentException e) {
171             e.printStackTrace(log);
172             throw new StatusException("Error while preparing component", e);
173         } catch (com.sun.star.beans.PropertyVetoException e) {
174             e.printStackTrace(log);
175             throw new StatusException("Error while preparing component", e);
176         } catch (com.sun.star.beans.UnknownPropertyException e) {
177             e.printStackTrace(log);
178             throw new StatusException("Error while preparing component", e);
179         }
180 
181         final URL aURL = new URL();
182         aURL.Complete = util.utils.getOfficeTemp(
183                                 (XMultiServiceFactory) tParam.getMSF()) +
184                         "picture.jpg";
185 
186         final XSimpleFileAccess fAcc;
187 
188         try {
189             Object oFAcc = ((XMultiServiceFactory) tParam.getMSF()).createInstance(
190                                    "com.sun.star.ucb.SimpleFileAccess");
191             fAcc = (XSimpleFileAccess) UnoRuntime.queryInterface(
192                            XSimpleFileAccess.class, oFAcc);
193 
194             if (fAcc.exists(aURL.Complete)) {
195                 fAcc.kill(aURL.Complete);
196             }
197         } catch (com.sun.star.uno.Exception e) {
198             log.println("Error accessing file system :");
199             e.printStackTrace(log);
200             throw new StatusException("Error accessing file system.", e);
201         }
202 
203         oObj = (XInterface) go;
204         log.println("ImplName " + util.utils.getImplName(oObj));
205 
206         TestEnvironment tEnv = new TestEnvironment(oObj);
207         tEnv.addObjRelation("MediaDescriptor",
208                             XMLTools.createMediaDescriptor(
209                                     new String[] {
210             "FilterName", "URL", "MediaType"
211         }, new Object[] { "JPG", aURL, "image/jpeg" }));
212         tEnv.addObjRelation("SourceDocument", xComp);
213 
214         log.println("adding ObjRelation for XFilter");
215         log.println("This Component doesn't really support the cancel method");
216         log.println("See #101725");
217         tEnv.addObjRelation("NoFilter.cancel()", new Boolean(true));
218 
219         final String hideMode = (String) tParam.get("soapi.test.hidewindows");
220         tEnv.addObjRelation("XFilter.Checker",
221                             new ifc.document._XFilter.FilterChecker() {
222             public boolean checkFilter() {
223                 try {
224                     if ((hideMode != null) && hideMode.equals("true")) {
225                         return true;
226                     }
227 
228                     return fAcc.exists(aURL.Complete);
229                 } catch (com.sun.star.uno.Exception e) {
230                     return false;
231                 }
232             }
233         });
234 
235         return tEnv;
236     } // finish method getTestEnvironment
237 } // finish class GraphicExporter
238