bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _sc / ScStyleObj.java
blobf7099412e38342b3b1c3cf87e5ecd0e5432d2ad5
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._sc;
21 import java.io.PrintWriter;
22 import java.util.ArrayList;
24 import lib.StatusException;
25 import lib.TestCase;
26 import lib.TestEnvironment;
27 import lib.TestParameters;
28 import util.SOfficeFactory;
30 import com.sun.star.beans.Property;
31 import com.sun.star.beans.PropertyAttribute;
32 import com.sun.star.beans.XPropertySet;
33 import com.sun.star.container.XIndexAccess;
34 import com.sun.star.container.XNameAccess;
35 import com.sun.star.container.XNameContainer;
36 import com.sun.star.lang.XComponent;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.style.XStyle;
42 import com.sun.star.style.XStyleFamiliesSupplier;
43 import com.sun.star.table.XCell;
44 import com.sun.star.uno.AnyConverter;
45 import com.sun.star.uno.Type;
46 import com.sun.star.uno.UnoRuntime;
47 import com.sun.star.uno.XInterface;
49 /**
50 * Test for object which is represented by service
51 * <code>com.sun.star.style.Style</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 * <li> <code>com::sun::star::container::XNamed</code></li>
55 * <li> <code>com::sun::star::style::Style</code></li>
56 * <li> <code>com::sun::star::style::XStyle</code></li>
57 * </ul>
58 * @see com.sun.star.style.Style
59 * @see com.sun.star.container.XNamed
60 * @see com.sun.star.style.Style
61 * @see com.sun.star.style.XStyle
62 * @see ifc.container._XNamed
63 * @see ifc.style._Style
64 * @see ifc.style._XStyle
66 public class ScStyleObj extends TestCase {
67 private XSpreadsheetDocument xSpreadsheetDoc = null;
69 /**
70 * Creates Spreadsheet document.
72 @Override
73 protected void initialize( TestParameters tParam, PrintWriter log ) {
74 SOfficeFactory SOF = SOfficeFactory.getFactory( tParam.getMSF() );
76 try {
77 log.println( "creating a Spreadsheet document" );
78 xSpreadsheetDoc = SOF.createCalcDoc(null);
79 } catch ( com.sun.star.uno.Exception e ) {
80 // Some exception occurs.FAILED
81 e.printStackTrace( log );
82 throw new StatusException( "Couldn't create document", e );
86 /**
87 * Disposes Spreadsheet document.
89 @Override
90 protected void cleanup( TestParameters tParam, PrintWriter log ) {
91 log.println( " disposing xSheetDoc " );
92 XComponent oComp = UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc);
93 util.DesktopTools.closeDoc(oComp);
96 /**
97 * Creating a Testenvironment for the interfaces to be tested.
98 * Retrieves the collection of style families from the document
99 * using the interface <code>XStyleFamiliesSupplier</code>.
100 * Obtains style family with index 0 from the collection and obtains
101 * style with index 0 from this style family. Creates the instance of the
102 * service <code>com.sun.star.style.CellStyle</code>. Inserts the created
103 * style to the obtained style family. Retrieves a collection of spreadsheets
104 * from the document and takes one of them. Gets a cell from the spreadsheet
105 * and sets the value of property <code>'CellStyle'</code> to the created
106 * style. The created style is the instance of the service
107 * <code>com.sun.star.style.Style</code> also.
108 * Object relations created :
109 * <ul>
110 * <li> <code>'PoolStyle'</code> for
111 * {@link ifc.style._XStyle}(the style with index 0 that was obtained
112 * from the collection)</li>
113 * </ul>
114 * @see com.sun.star.style.CellStyle
115 * @see com.sun.star.style.Style
116 * @see com.sun.star.style.XStyleFamiliesSupplier
118 @Override
119 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
122 // creation of the testobject here
123 // first we write what we are intend to do to log file
125 log.println("creating a test environment");
127 log.println("getting style");
128 XStyleFamiliesSupplier oStyleFamiliesSupplier = UnoRuntime.queryInterface(
129 XStyleFamiliesSupplier.class, xSpreadsheetDoc);
130 XNameAccess oStyleFamilies = oStyleFamiliesSupplier.getStyleFamilies();
131 XIndexAccess oStyleFamiliesIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class, oStyleFamilies);
132 XNameAccess oStyleFamilyNameAccess = null;
133 XStyle oStyle = null;
134 try {
135 oStyleFamilyNameAccess = (XNameAccess) AnyConverter.toObject(
136 new Type(XNameAccess.class),
137 oStyleFamiliesIndexAccess.getByIndex(0));
139 XIndexAccess oStyleFamilyIndexAccess = UnoRuntime.queryInterface(XIndexAccess.class,
140 oStyleFamilyNameAccess);
141 oStyle = (XStyle) AnyConverter.toObject(
142 new Type(XStyle.class),oStyleFamilyIndexAccess.getByIndex(0));
143 } catch(com.sun.star.lang.WrappedTargetException e) {
144 e.printStackTrace(log);
145 throw new StatusException("Couldn't get by index", e);
146 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
147 e.printStackTrace(log);
148 throw new StatusException("Couldn't get by index", e);
149 } catch(com.sun.star.lang.IllegalArgumentException e) {
150 e.printStackTrace(log);
151 throw new StatusException("Couldn't get by index", e);
154 log.println("Creating a user-defined style");
155 XMultiServiceFactory oMSF = UnoRuntime.queryInterface(
156 XMultiServiceFactory.class, xSpreadsheetDoc);
158 XInterface oInt = null;
159 try {
160 oInt = (XInterface)
161 oMSF.createInstance("com.sun.star.style.CellStyle");
162 } catch(com.sun.star.uno.Exception e) {
163 e.printStackTrace(log);
164 throw new StatusException("Couldn't create instance", e);
166 XStyle oMyStyle = UnoRuntime.queryInterface(XStyle.class, oInt);
168 XNameContainer oStyleFamilyNameContainer = UnoRuntime.
169 queryInterface(XNameContainer.class, oStyleFamilyNameAccess);
171 try {
172 if (oStyleFamilyNameContainer.hasByName("My Style")) {
173 oStyleFamilyNameContainer.removeByName("My Style");
176 oStyleFamilyNameContainer.insertByName("My Style", oMyStyle);
177 } catch(com.sun.star.lang.WrappedTargetException e) {
178 e.printStackTrace(log);
179 throw new StatusException("Couldn't create test environment", e);
180 } catch(com.sun.star.container.NoSuchElementException e) {
181 e.printStackTrace(log);
182 throw new StatusException("Couldn't create test environment", e);
183 } catch(com.sun.star.container.ElementExistException e) {
184 e.printStackTrace(log);
185 throw new StatusException("Couldn't create test environment", e);
186 } catch(com.sun.star.lang.IllegalArgumentException e) {
187 e.printStackTrace(log);
188 throw new StatusException("Couldn't create test environment", e);
192 //using the style
193 log.println("Getting spreadsheet") ;
194 XSpreadsheets oSheets = xSpreadsheetDoc.getSheets() ;
195 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(XIndexAccess.class, oSheets);
197 XCell aCell = null;
198 try {
199 XSpreadsheet oSheet = (XSpreadsheet) AnyConverter.toObject(
200 new Type(XSpreadsheet.class),oIndexSheets.getByIndex(0));
201 log.println("Getting a cell from sheet") ;
202 aCell = oSheet.getCellByPosition(2,3) ;
203 } catch(com.sun.star.lang.WrappedTargetException e) {
204 e.printStackTrace(log);
205 throw new StatusException("Couldn't get spreadsheet by index", e);
206 } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
207 e.printStackTrace(log);
208 throw new StatusException("Couldn't get spreadsheet by index", e);
209 } catch(com.sun.star.lang.IllegalArgumentException e) {
210 e.printStackTrace(log);
211 throw new StatusException("Couldn't get spreadsheet by index", e);
214 XPropertySet xProp = UnoRuntime.queryInterface(XPropertySet.class, aCell);
216 try {
217 xProp.setPropertyValue("CellStyle", oMyStyle.getName());
218 } catch(com.sun.star.lang.WrappedTargetException e) {
219 e.printStackTrace(log);
220 throw new StatusException("Couldn't set property CellStyle", e);
221 } catch(com.sun.star.lang.IllegalArgumentException e) {
222 e.printStackTrace(log);
223 throw new StatusException("Couldn't set property CellStyle", e);
224 } catch(com.sun.star.beans.PropertyVetoException e) {
225 e.printStackTrace(log);
226 throw new StatusException("Couldn't set property CellStyle", e);
227 } catch(com.sun.star.beans.UnknownPropertyException e) {
228 e.printStackTrace(log);
229 throw new StatusException("Couldn't set property CellStyle", e);
232 log.println("creating a new environment for object");
233 TestEnvironment tEnv = new TestEnvironment(oMyStyle);
235 tEnv.addObjRelation("PoolStyle", oStyle);
237 tEnv.addObjRelation("PropertyNames", getPropertyNames
238 (UnoRuntime.queryInterface
239 (XPropertySet.class, oMyStyle)));
241 return tEnv;
244 public String[] getPropertyNames(XPropertySet props) {
245 Property[] the_props = props.getPropertySetInfo().getProperties();
246 ArrayList<String> names = new ArrayList<String>() ;
248 for (int i=0;i<the_props.length;i++) {
249 boolean isWritable =
250 ((the_props[i].Attributes & PropertyAttribute.READONLY) == 0);
251 if (isWritable) {
252 names.add(the_props[i].Name);
255 return names.toArray(new String[names.size()]) ;
257 } // finish class ScStyleObj