bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sw / SwXStyle.java
blob0371ffe866b0e2d4bc18a5021e721302c3e867e6
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 protected void initialize( TestParameters tParam, PrintWriter log ) {
72 SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
73 try {
74 log.println( "creating a textdocument" );
75 xTextDoc = SOF.createTextDoc( null );
76 } catch ( com.sun.star.uno.Exception e ) {
77 e.printStackTrace( log );
78 throw new StatusException( "Couldn't create document", e );
82 /**
83 * Disposes text document.
85 protected void cleanup( TestParameters tParam, PrintWriter log ) {
86 log.println( " disposing xTextDoc " );
87 util.DesktopTools.closeDoc(xTextDoc);
90 /**
91 * Creating a Testenvironment for the interfaces to be tested.
92 * At first style families are gotten from a text document using
93 * <code>XStyleFamiliesSupplier</code> interface, then family indexed '0' is
94 * gotten from this style family using <code>XIndexAccess</code> interface.
95 * Next, method creates an instance of the service
96 * <code>com.sun.star.style.CharacterStyle</code> and inserts it to a
97 * previously obtained style family using <code>XNameContainer</code>
98 * interface. Finally, method creates a cursor of a major text of text
99 * document and sets it's property 'CharStyleName' value to the name of
100 * previously created style's name.
101 * Object relations created :
102 * <ul>
103 * <li> <code>'PoolStyle'</code> for
104 * {@link ifc.style._XStyle} : slyle indexed '10' obtained from
105 * StyleFamily indexed '0' from text document using
106 * <code>XIndexAccess</code> interface.</li>
107 * </ul>
109 protected synchronized TestEnvironment createTestEnvironment
110 (TestParameters Param, PrintWriter log) {
112 TestEnvironment tEnv = null;
113 XNameAccess oSFNA = null;
114 XStyle oStyle = null;
115 XStyle oMyStyle = null;
117 log.println("creating a test environment");
119 try {
120 log.println("getting style");
121 XStyleFamiliesSupplier oSFS = UnoRuntime.queryInterface(XStyleFamiliesSupplier.class,
122 xTextDoc);
123 XNameAccess oSF = oSFS.getStyleFamilies();
124 XIndexAccess oSFsIA = UnoRuntime.queryInterface(XIndexAccess.class, oSF);
125 oSFNA = (XNameAccess) AnyConverter.toObject(
126 new Type(XNameAccess.class),oSFsIA.getByIndex(0));
127 XIndexAccess oSFIA = UnoRuntime.queryInterface(XIndexAccess.class, oSFNA);
128 oStyle = (XStyle) AnyConverter.toObject(
129 new Type(XStyle.class),oSFIA.getByIndex(10));
130 } catch ( com.sun.star.lang.WrappedTargetException e ) {
131 log.println("Error: exception occurred.");
132 e.printStackTrace(log);
133 throw new StatusException( "Couldn't create environment ", e );
134 } catch ( com.sun.star.lang.IndexOutOfBoundsException e ) {
135 log.println("Error: exception occurred.");
136 e.printStackTrace(log);
137 throw new StatusException( "Couldn't create environment ", e );
138 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
139 log.println("Error: exception occurred.");
140 e.printStackTrace(log);
141 throw new StatusException( "Couldn't create environment ", e );
144 try {
145 log.print("Creating a user-defined style... ");
146 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);
147 XInterface oInt = (XInterface)
148 oMSF.createInstance("com.sun.star.style.CharacterStyle");
149 oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
150 } catch ( com.sun.star.uno.Exception e ) {
151 log.println("Error: exception occurred.");
152 e.printStackTrace(log);
153 throw new StatusException( "Couldn't create environment ", e );
157 if (oMyStyle == null)
158 log.println("FAILED");
159 else
160 log.println("OK");
161 XNameContainer oSFNC = UnoRuntime.queryInterface(XNameContainer.class, oSFNA);
163 try {
164 if ( oSFNC.hasByName("My Style") )
165 oSFNC.removeByName("My Style");
166 oSFNC.insertByName("My Style", oMyStyle);
167 } catch ( com.sun.star.lang.WrappedTargetException e ) {
168 e.printStackTrace(log);
169 throw new StatusException( "Couldn't create environment ", e );
170 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
171 e.printStackTrace(log);
172 throw new StatusException( "Couldn't create environment ", e );
173 } catch ( com.sun.star.container.NoSuchElementException e ) {
174 e.printStackTrace(log);
175 throw new StatusException( "Couldn't create environment ", e );
176 } catch ( com.sun.star.container.ElementExistException e ) {
177 e.printStackTrace(log);
178 throw new StatusException( "Couldn't create environment ", e );
181 XText oText = xTextDoc.getText();
182 XTextCursor oCursor = oText.createTextCursor();
183 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, oCursor);
185 try {
186 xProp.setPropertyValue("CharStyleName", oMyStyle.getName());
187 } catch ( com.sun.star.lang.WrappedTargetException e ) {
188 e.printStackTrace( log );
189 throw new StatusException( "Couldn't create environment ", e );
190 } catch ( com.sun.star.lang.IllegalArgumentException e ) {
191 e.printStackTrace( log );
192 throw new StatusException( "Couldn't create environment ", e );
193 } catch ( com.sun.star.beans.PropertyVetoException e ) {
194 e.printStackTrace( log );
195 throw new StatusException( "Couldn't create environment ", e );
196 } catch ( com.sun.star.beans.UnknownPropertyException e ) {
197 e.printStackTrace( log );
198 throw new StatusException( "Couldn't create environment ", e );
201 log.println("creating a new environment for object");
202 tEnv = new TestEnvironment(oMyStyle);
203 tEnv.addObjRelation("PoolStyle", oStyle);
205 XPropertySet xStyleProp = UnoRuntime.queryInterface(XPropertySet.class, oMyStyle);
206 tEnv.addObjRelation("PropertyNames",getPropertyNames(xStyleProp));
208 return tEnv;
211 public String[] getPropertyNames(XPropertySet props) {
212 Property[] the_props = props.getPropertySetInfo().getProperties();
213 String[] names = new String[the_props.length];
214 String placebo = "";
215 for (int i=0;i<the_props.length;i++) {
216 boolean isWritable =
217 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
218 if (isWritable) placebo=the_props[i].Name;
220 for (int i=0;i<the_props.length;i++) {
221 boolean isWritable =
222 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
223 if (isWritable) {
224 names[i]=the_props[i].Name;
225 } else {
226 names[i] = placebo;
229 return names;
232 } // finish class SwXStyle