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