Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / mod / _svx / GraphicExporter.java
blobb47073de9d2fcf8fe267ea494c634e0690a44c58
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.TestCase;
32 import lib.TestEnvironment;
33 import lib.TestParameters;
35 import util.DrawTools;
36 import util.SOfficeFactory;
37 import util.XMLTools;
40 /**
41 * Test for object which is represented by service
42 * <code>com.sun.star.drawing.GraphicExportFilter</code>. <p>
44 * Object implements the following interfaces :
45 * <ul>
46 * <li> <code>com::sun::star::document::XFilter</code></li>
47 * <li> <code>com::sun::star::document::XMimeTypeInfo</code></li>
48 * <li> <code>com::sun::star::document::XExporter</code></li>
49 * </ul> <p>
51 * The following files used by this test :
52 * <ul>
53 * <li><b> space-metal.jpg </b> : the file used for GraphicObject
54 * creation. This image must be then exported. </li>
55 * </ul> <p>
57 * This object test <b> is NOT </b> designed to be run in several
58 * threads concurrently.
60 * @see com.sun.star.document.XFilter
61 * @see com.sun.star.document.XMimeTypeInfo
62 * @see com.sun.star.document.XExporter
63 * @see ifc.document._XFilter
64 * @see ifc.document._XMimeTypeInfo
65 * @see ifc.document._XExporter
67 public class GraphicExporter extends TestCase {
68 static XComponent xDrawDoc;
70 /**
71 * Creates a new draw document.
73 @Override
74 protected void initialize(TestParameters tParam, PrintWriter log) throws Exception {
75 log.println("creating a drawdoc");
76 xDrawDoc = DrawTools.createDrawDoc(
77 tParam.getMSF());
80 /**
81 * Disposes the draw document created before
83 @Override
84 protected void cleanup(TestParameters tParam, PrintWriter log) {
85 log.println(" disposing xDrawDoc ");
86 util.DesktopTools.closeDoc(xDrawDoc);
89 /**
90 * Creating a TestEnvironment for the interfaces to be tested.
91 * Creates an instance of the service
92 * <code>com.sun.star.drawing.GraphicExportFilter</code> as
93 * a tested component. Then a <code>GraphicObjectShape</code>
94 * instance is added into the document and its image is obtained
95 * from JPEG file. This shape content is intended to be exported.
97 * Object relations created :
98 * <ul>
99 * <li> <code>'MediaDescriptor'</code> for
100 * {@link ifc.document._XFilter} :
101 * descriptor which contains target file name in
102 * the temporary directory, file type (JPEG)
103 * </li>
104 * <li> <code>'XFilter.Checker'</code> for
105 * {@link ifc.document._XFilter} :
106 * checks if the target file exists.
107 * In the case if SOffice is started in 'Hide' mode
108 * ('soapi.test.hidewindows' test parameter is 'true')
109 * the checker always returns <code>true</code>.
110 * </li>
111 * <li> <code>'SourceDocument'</code> for
112 * {@link ifc.document._XExporter} :
113 * the <code>GraphicObjectShape</code> component
114 * with loaded image.
115 * </li>
116 * </ul>
118 @Override
119 protected TestEnvironment createTestEnvironment(TestParameters tParam,
120 PrintWriter log) throws Exception {
121 XInterface oObj = null;
122 XShape oShape = null;
123 Object go = null;
126 // creation of testobject here
127 // first we write what we are intend to do to log file
128 log.println("creating a test environment");
130 try {
131 go = tParam.getMSF().createInstance(
132 "com.sun.star.drawing.GraphicExportFilter");
133 } catch (com.sun.star.uno.Exception e) {
134 log.println("Couldn't create instance");
135 e.printStackTrace(log);
138 // create testobject here
139 SOfficeFactory SOF = SOfficeFactory.getFactory(
140 tParam.getMSF());
141 oShape = SOF.createShape(xDrawDoc, 5000, 5000, 1500, 1000,
142 "GraphicObject");
143 DrawTools.getShapes(DrawTools.getDrawPage(xDrawDoc, 0)).add(oShape);
145 XPropertySet oShapeProps = UnoRuntime.queryInterface(
146 XPropertySet.class, oShape);
147 XComponent xComp = null;
149 oShapeProps.setPropertyValue("GraphicURL",
150 util.utils.getFullTestURL(
151 "space-metal.jpg"));
152 xComp = UnoRuntime.queryInterface(XComponent.class,
153 oShape);
155 XExporter xEx = UnoRuntime.queryInterface(
156 XExporter.class, go);
157 xEx.setSourceDocument(xComp);
159 final URL aURL = new URL();
160 aURL.Complete = util.utils.getOfficeTemp(
161 tParam.getMSF()) +
162 "picture.jpg";
164 final XSimpleFileAccess fAcc;
166 Object oFAcc = tParam.getMSF().createInstance(
167 "com.sun.star.ucb.SimpleFileAccess");
168 fAcc = UnoRuntime.queryInterface(
169 XSimpleFileAccess.class, oFAcc);
171 if (fAcc.exists(aURL.Complete)) {
172 fAcc.kill(aURL.Complete);
175 oObj = (XInterface) go;
176 log.println("ImplName " + util.utils.getImplName(oObj));
178 TestEnvironment tEnv = new TestEnvironment(oObj);
179 tEnv.addObjRelation("MediaDescriptor",
180 XMLTools.createMediaDescriptor(
181 new String[] {
182 "FilterName", "URL", "MediaType"
183 }, new Object[] { "JPG", aURL, "image/jpeg" }));
184 tEnv.addObjRelation("SourceDocument", xComp);
186 log.println("adding ObjRelation for XFilter");
187 log.println("This Component doesn't really support the cancel method");
188 log.println("See #101725");
189 tEnv.addObjRelation("NoFilter.cancel()", Boolean.TRUE);
191 final String hideMode = (String) tParam.get("soapi.test.hidewindows");
192 tEnv.addObjRelation("XFilter.Checker",
193 new ifc.document._XFilter.FilterChecker() {
194 public boolean checkFilter() {
195 try {
196 if ((hideMode != null) && hideMode.equals("true")) {
197 return true;
200 return fAcc.exists(aURL.Complete);
201 } catch (com.sun.star.uno.Exception e) {
202 return false;
207 return tEnv;
208 } // finish method getTestEnvironment
209 } // finish class GraphicExporter