bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXStyle.java
blobc17daf18d90a259e5e93b947162d85d3c6f17380
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 .
19 package mod._sw;
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.beans.Property;
30 import com.sun.star.beans.PropertyAttribute;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.container.XIndexAccess;
33 import com.sun.star.container.XNameAccess;
34 import com.sun.star.container.XNameContainer;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.style.XStyle;
37 import com.sun.star.style.XStyleFamiliesSupplier;
38 import com.sun.star.text.XText;
39 import com.sun.star.text.XTextCursor;
40 import com.sun.star.text.XTextDocument;
41 import com.sun.star.uno.AnyConverter;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
46 /**
47 * Test for object which is represented by service
48 * <code>com.sun.star.style.Style</code>. <p>
49 * Object implements the following interfaces :
50 * <ul>
51 * <li> <code>com::sun::star::container::XNamed</code></li>
52 * <li> <code>com::sun::star::style::Style</code></li>
53 * <li> <code>com::sun::star::style::XStyle</code></li>
54 * </ul> <p>
55 * This object test <b> is NOT </b> designed to be run in several
56 * threads concurently.
57 * @see com.sun.star.container.XNamed
58 * @see com.sun.star.style.Style
59 * @see com.sun.star.style.XStyle
60 * @see ifc.container._XNamed
61 * @see ifc.style._Style
62 * @see ifc.style._XStyle
64 public class SwXStyle extends TestCase {
65 XTextDocument xTextDoc;
66 SOfficeFactory SOF = null;
68 /**
69 * Creates text document.
71 @Override
72 protected void initialize( TestParameters tParam, PrintWriter log ) {
73 SOF = SOfficeFactory.getFactory( tParam.getMSF() );
74 try {
75 log.println( "creating a textdocument" );
76 xTextDoc = SOF.createTextDoc( null );
77 } catch ( com.sun.star.uno.Exception e ) {
78 e.printStackTrace( log );
79 throw new StatusException( "Couldn't create document", e );
83 /**
84 * Disposes text document.
86 @Override
87 protected void cleanup( TestParameters tParam, PrintWriter log ) {
88 log.println( " disposing xTextDoc " );
89 util.DesktopTools.closeDoc(xTextDoc);
92 /**
93 * Creating a Testenvironment for the interfaces to be tested.
94 * At first style families are gotten from a text document using
95 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
96 * gotten from this style family using <code>XIndexAccess</code> interface.
97 * Next, method creates an instance of the service
98 * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
99 * previously obtained style family using <code>XNameContainer</code>
100 * interface. Finally, method creates a cursor of a major text of text
101 * document and sets it's property 'CharStyleName' value to the name of
102 * previously created style's name.
103 * Object relations created :
104 * <ul>
105 * <li> <code>'PoolStyle'</code> for
106 * {@link ifc.style._XStyle} : slyle indexed '10' obtained from
107 * StyleFamily indexed '0' from text document using
108 * <code>XIndexAccess</code> interface.</li>
109 * </ul>
111 @Override
112 protected synchronized TestEnvironment createTestEnvironment
113 (TestParameters Param, PrintWriter log) {
115 TestEnvironment tEnv = null;
116 XNameAccess oSFNA = null;
117 XStyle oStyle = null;
118 XStyle oMyStyle = null;
120 log.println("creating a test environment");
122 try {
123 log.println("getting style");
124 XStyleFamiliesSupplier oSFS = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
125 xTextDoc);
126 XNameAccess oSF = oSFS.getStyleFamilies();
127 XIndexAccess oSFsIA = UnoRuntime.queryInterface(XIndexAccess.class, oSF);
128 oSFNA = (XNameAccess) AnyConverter.toObject(
129 new Type(XNameAccess.class),oSFsIA.getByIndex(0));
130 XIndexAccess oSFIA = UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
131 oStyle = (XStyle) AnyConverter.toObject(
132 new Type(XStyle.class),oSFIA.getByIndex(10));
133 } catch ( com.sun.star.lang.WrappedTargetException e ) {
134 log.println("Error: exception occurred.");
135 e.printStackTrace(log);
136 throw new StatusException( "Couldn't create environment ", e );
137 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
138 log.println("Error: exception occurred.");
139 e.printStackTrace(log);
140 throw new StatusException( "Couldn't create environment ", e );
141 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
142 log.println("Error: exception occurred.");
143 e.printStackTrace(log);
144 throw new StatusException( "Couldn't create environment ", e );
147 try {
148 log.print("Creating a user-defined style... ");
149 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
150 XInterface oInt = (XInterface)
151 oMSF.createInstance("com.sun.star.style.CharacterStyle");
152 oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
153 } catch ( com.sun.star.uno.Exception e ) {
154 log.println("Error: exception occurred.");
155 e.printStackTrace(log);
156 throw new StatusException( "Couldn't create environment ", e );
160 if (oMyStyle == null)
161 log.println("FAILED");
162 else
163 log.println("OK");
164 XNameContainer oSFNC = UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
166 try {
167 if ( oSFNC.hasByName("My Style") )
168 oSFNC.removeByName("My Style");
169 oSFNC.insertByName("My Style", oMyStyle);
170 } catch ( com.sun.star.lang.WrappedTargetException e ) {
171 e.printStackTrace(log);
172 throw new StatusException( "Couldn't create environment ", e );
173 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
174 e.printStackTrace(log);
175 throw new StatusException( "Couldn't create environment ", e );
176 } catch ( com.sun.star.container.NoSuchElementException e ) {
177 e.printStackTrace(log);
178 throw new StatusException( "Couldn't create environment ", e );
179 } catch ( com.sun.star.container.ElementExistException e ) {
180 e.printStackTrace(log);
181 throw new StatusException( "Couldn't create environment ", e );
184 XText oText = xTextDoc.getText();
185 XTextCursor oCursor = oText.createTextCursor();
186 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, oCursor);
188 try {
189 xProp.setPropertyValue("CharStyleName", oMyStyle.getName());
190 } catch ( com.sun.star.lang.WrappedTargetException e ) {
191 e.printStackTrace( log );
192 throw new StatusException( "Couldn't create environment ", e );
193 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
194 e.printStackTrace( log );
195 throw new StatusException( "Couldn't create environment ", e );
196 } catch ( com.sun.star.beans.PropertyVetoException e ) {
197 e.printStackTrace( log );
198 throw new StatusException( "Couldn't create environment ", e );
199 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
200 e.printStackTrace( log );
201 throw new StatusException( "Couldn't create environment ", e );
204 log.println("creating a new environment for object");
205 tEnv = new TestEnvironment(oMyStyle);
206 tEnv.addObjRelation("PoolStyle", oStyle);
208 XPropertySet xStyleProp = UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
209 tEnv.addObjRelation("PropertyNames",getPropertyNames(xStyleProp));
211 return tEnv;
214 public String[] getPropertyNames(XPropertySet props) {
215 Property[] the_props = props.getPropertySetInfo().getProperties();
216 String[] names = new String[the_props.length];
217 String placebo = "";
218 for (int i=0;i<the_props.length;i++) {
219 boolean isWritable =
220 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
221 if (isWritable) placebo=the_props[i].Name;
223 for (int i=0;i<the_props.length;i++) {
224 boolean isWritable =
225 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
226 if (isWritable) {
227 names[i]=the_props[i].Name;
228 } else {
229 names[i] = placebo;
232 return names;
235 } // finish class SwXStyle