Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXTextEmbeddedObject.java
blobc8318815bbdb1ea8060999255c8afa782ddc47fd
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._sw;
20 import com.sun.star.beans.XPropertySet;
21 import java.io.PrintWriter;
23 import lib.StatusException;
24 import lib.TestCase;
25 import lib.TestEnvironment;
26 import lib.TestParameters;
27 import util.SOfficeFactory;
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.container.XNameAccess;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.text.XText;
33 import com.sun.star.text.XTextContent;
34 import com.sun.star.text.XTextCursor;
35 import com.sun.star.text.XTextDocument;
36 import com.sun.star.text.XTextEmbeddedObjectsSupplier;
37 import com.sun.star.text.XTextFrame;
38 import com.sun.star.uno.AnyConverter;
39 import com.sun.star.uno.Type;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.util.XCloseable;
45 /**
47 * initial description
48 * @see com.sun.star.container.XNamed
49 * @see com.sun.star.document.XEmbeddedObjectSupplier
50 * @see com.sun.star.lang.XComponent
51 * @see com.sun.star.text.TextEmbeddedObject
52 * @see com.sun.star.text.XTextContent
55 public class SwXTextEmbeddedObject extends TestCase {
56 XTextDocument xTextDoc;
58 /**
59 * in general this method disposes the testenvironment and document
61 * @param tParam class which contains additional test parameters
62 * @param log class to log the test state and result
65 * @see TestParameters
66 * @see PrintWriter
69 @Override
70 protected void cleanup(TestParameters tParam, PrintWriter log) {
71 log.println(" disposing xTextDoc ");
73 try {
74 XCloseable closer = UnoRuntime.queryInterface(
75 XCloseable.class, xTextDoc);
76 closer.close(true);
77 } catch (com.sun.star.util.CloseVetoException e) {
78 log.println("couldn't close document");
79 } catch (com.sun.star.lang.DisposedException e) {
80 log.println("couldn't close document");
84 /**
85 * creating a Testenvironment for the interfaces to be tested
87 * @param tParam class which contains additional test parameters
88 * @param log class to log the test state and result
90 * @return Status class
92 * @see TestParameters
93 * @see PrintWriter
95 @Override
96 protected TestEnvironment createTestEnvironment(TestParameters tParam,
97 PrintWriter log) {
98 XInterface oObj = null;
100 // create testobject here
101 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF());
102 try {
103 xTextDoc = SOF.createTextDoc(null);
104 } catch (com.sun.star.uno.Exception e) {
105 e.printStackTrace(log);
106 throw new StatusException("Couldn't open document", e);
109 XTextCursor xCursor = xTextDoc.getText().createTextCursor();
110 try {
111 XMultiServiceFactory xMultiServiceFactory = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
112 Object o = xMultiServiceFactory.createInstance("com.sun.star.text.TextEmbeddedObject" );
113 XTextContent xTextContent = UnoRuntime.queryInterface(XTextContent.class, o);
114 String sChartClassID = "12dcae26-281f-416f-a234-c3086127382e";
115 XPropertySet xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, xTextContent);
116 xPropertySet.setPropertyValue( "CLSID", sChartClassID );
118 xTextDoc.getText().insertTextContent( xCursor, xTextContent, false );
120 catch(com.sun.star.uno.Exception e) {
121 e.printStackTrace(log);
124 XTextEmbeddedObjectsSupplier oTEOS = UnoRuntime.queryInterface(
125 XTextEmbeddedObjectsSupplier.class,
126 xTextDoc);
128 XNameAccess oEmObj = oTEOS.getEmbeddedObjects();
129 XIndexAccess oEmIn = UnoRuntime.queryInterface(
130 XIndexAccess.class, oEmObj);
132 try {
133 oObj = (XInterface) AnyConverter.toObject(
134 new Type(XInterface.class), oEmIn.getByIndex(0));
135 } catch (com.sun.star.uno.Exception e) {
136 e.printStackTrace(log);
137 throw new StatusException("Couldn't get Object", e);
140 TestEnvironment tEnv = new TestEnvironment(oObj);
142 tEnv.addObjRelation("NoAttach", "SwXTextEmbeddedObject");
144 XTextFrame aFrame = SOfficeFactory.createTextFrame(xTextDoc, 500, 500);
145 XText oText = xTextDoc.getText();
146 XTextCursor oCursor = oText.createTextCursor();
147 XTextContent the_content = UnoRuntime.queryInterface(
148 XTextContent.class, aFrame);
150 try {
151 oText.insertTextContent(oCursor, the_content, true);
152 } catch (com.sun.star.lang.IllegalArgumentException e) {
153 log.println("Couldn't insert frame " + e.getMessage());
156 tEnv.addObjRelation("TextFrame", aFrame);
158 tEnv.addObjRelation("NoSetSize", "SwXTextEmbeddedObject");
159 tEnv.addObjRelation("NoPos", "SwXTextEmbeddedObject");
161 return tEnv;
162 } // finish method getTestEnvironment
163 } // finish class SwXTextEmbeddedObject