Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _svx / GraphicExporter.java
blobb67f2ab547acc465f7d237efea4fe47b7fe1ef0d
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package mod._svx;
20 import com.sun.star.beans.XPropertySet;
21 import com.sun.star.document.XExporter;
22 import com.sun.star.drawing.XShape;
23 import com.sun.star.lang.XComponent;
24 import com.sun.star.ucb.XSimpleFileAccess;
25 import com.sun.star.uno.UnoRuntime;
26 import com.sun.star.uno.XInterface;
27 import com.sun.star.util.URL;
29 import java.io.PrintWriter;
31 import lib.StatusException;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
36 import util.DrawTools;
37 import util.SOfficeFactory;
38 import util.XMLTools;
41 /**
42 * Test for object which is represented by service
43 * <code>com.sun.star.drawing.GraphicExportFilter</code>. <p>
45 * Object implements the following interfaces :
46 * <ul>
47 * <li> <code>com::sun::star::document::XFilter</code></li>
48 * <li> <code>com::sun::star::document::XMimeTypeInfo</code></li>
49 * <li> <code>com::sun::star::document::XExporter</code></li>
50 * </ul> <p>
52 * The following files used by this test :
53 * <ul>
54 * <li><b> space-metal.jpg </b> : the file used for GraphicObject
55 * creation. This image must be then exported. </li>
56 * </ul> <p>
58 * This object test <b> is NOT </b> designed to be run in several
59 * threads concurently.
61 * @see com.sun.star.document.XFilter
62 * @see com.sun.star.document.XMimeTypeInfo
63 * @see com.sun.star.document.XExporter
64 * @see ifc.document._XFilter
65 * @see ifc.document._XMimeTypeInfo
66 * @see ifc.document._XExporter
68 public class GraphicExporter extends TestCase {
69 static XComponent xDrawDoc;
71 /**
72 * Creates a new draw document.
74 @Override
75 protected void initialize(TestParameters tParam, PrintWriter log) {
76 log.println("creating a drawdoc");
77 xDrawDoc = DrawTools.createDrawDoc(
78 tParam.getMSF());
81 /**
82 * Disposes the draw document created before
84 @Override
85 protected void cleanup(TestParameters tParam, PrintWriter log) {
86 log.println(" disposing xDrawDoc ");
87 util.DesktopTools.closeDoc(xDrawDoc);
90 /**
91 * Creating a Testenvironment for the interfaces to be tested.
92 * Creates an instance of the service
93 * <code>com.sun.star.drawing.GraphicExportFilter</code> as
94 * a tested component. Then a <code>GraphicObjectShape</code>
95 * instance is added into the document and its image is obtained
96 * from JPEG file. This shape content is intended to be exported.
98 * Object relations created :
99 * <ul>
100 * <li> <code>'MediaDescriptor'</code> for
101 * {@link ifc.document._XFilter} :
102 * descriptor which contains target file name in
103 * the temporary directory, file type (JPEG)
104 * </li>
105 * <li> <code>'XFilter.Checker'</code> for
106 * {@link ifc.document._XFilter} :
107 * checks if the target file exists.
108 * In the case if SOffice is started in 'Hide' mode
109 * ('soapi.test.hidewindows' test parameter is 'true')
110 * the checker always returns <code>true</code>.
111 * </li>
112 * <li> <code>'SourceDocument'</code> for
113 * {@link ifc.document._XExporter} :
114 * the <code>GraphicObjectShape</code> component
115 * with loaded image.
116 * </li>
117 * </ul>
119 @Override
120 protected TestEnvironment createTestEnvironment(TestParameters tParam,
121 PrintWriter log) {
122 XInterface oObj = null;
123 XShape oShape = null;
124 Object go = null;
127 // creation of testobject here
128 // first we write what we are intend to do to log file
129 log.println("creating a test environment");
131 try {
132 go = tParam.getMSF().createInstance(
133 "com.sun.star.drawing.GraphicExportFilter");
134 } catch (com.sun.star.uno.Exception e) {
135 log.println("Couldn't create instance");
136 e.printStackTrace(log);
139 // create testobject here
140 SOfficeFactory SOF = SOfficeFactory.getFactory(
141 tParam.getMSF());
142 oShape = SOF.createShape(xDrawDoc, 5000, 5000, 1500, 1000,
143 "GraphicObject");
144 DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc, 0)).add(oShape);
146 XPropertySet oShapeProps = UnoRuntime.queryInterface(
147 XPropertySet.class, oShape);
148 XComponent xComp = null;
150 try {
151 oShapeProps.setPropertyValue("GraphicURL",
152 util.utils.getFullTestURL(
153 "space-metal.jpg"));
154 xComp = UnoRuntime.queryInterface(XComponent.class,
155 oShape);
157 XExporter xEx = UnoRuntime.queryInterface(
158 XExporter.class, go);
159 xEx.setSourceDocument(xComp);
160 } catch (com.sun.star.lang.WrappedTargetException e) {
161 e.printStackTrace(log);
162 throw new StatusException("Error while preparing component", e);
163 } catch (com.sun.star.lang.IllegalArgumentException e) {
164 e.printStackTrace(log);
165 throw new StatusException("Error while preparing component", e);
166 } catch (com.sun.star.beans.PropertyVetoException e) {
167 e.printStackTrace(log);
168 throw new StatusException("Error while preparing component", e);
169 } catch (com.sun.star.beans.UnknownPropertyException e) {
170 e.printStackTrace(log);
171 throw new StatusException("Error while preparing component", e);
174 final URL aURL = new URL();
175 aURL.Complete = util.utils.getOfficeTemp(
176 tParam.getMSF()) +
177 "picture.jpg";
179 final XSimpleFileAccess fAcc;
181 try {
182 Object oFAcc = tParam.getMSF().createInstance(
183 "com.sun.star.ucb.SimpleFileAccess");
184 fAcc = UnoRuntime.queryInterface(
185 XSimpleFileAccess.class, oFAcc);
187 if (fAcc.exists(aURL.Complete)) {
188 fAcc.kill(aURL.Complete);
190 } catch (com.sun.star.uno.Exception e) {
191 log.println("Error accessing file system :");
192 e.printStackTrace(log);
193 throw new StatusException("Error accessing file system.", e);
196 oObj = (XInterface) go;
197 log.println("ImplName " + util.utils.getImplName(oObj));
199 TestEnvironment tEnv = new TestEnvironment(oObj);
200 tEnv.addObjRelation("MediaDescriptor",
201 XMLTools.createMediaDescriptor(
202 new String[] {
203 "FilterName", "URL", "MediaType"
204 }, new Object[] { "JPG", aURL, "image/jpeg" }));
205 tEnv.addObjRelation("SourceDocument", xComp);
207 log.println("adding ObjRelation for XFilter");
208 log.println("This Component doesn't really support the cancel method");
209 log.println("See #101725");
210 tEnv.addObjRelation("NoFilter.cancel()", Boolean.TRUE);
212 final String hideMode = (String) tParam.get("soapi.test.hidewindows");
213 tEnv.addObjRelation("XFilter.Checker",
214 new ifc.document._XFilter.FilterChecker() {
215 public boolean checkFilter() {
216 try {
217 if ((hideMode != null) && hideMode.equals("true")) {
218 return true;
221 return fAcc.exists(aURL.Complete);
222 } catch (com.sun.star.uno.Exception e) {
223 return false;
228 return tEnv;
229 } // finish method getTestEnvironment
230 } // finish class GraphicExporter