1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package mod._sc; 29 30 import java.io.PrintWriter; 31 32 import lib.StatusException; 33 import lib.TestCase; 34 import lib.TestEnvironment; 35 import lib.TestParameters; 36 import util.SOfficeFactory; 37 38 import com.sun.star.beans.PropertyValue; 39 import com.sun.star.beans.XPropertySet; 40 import com.sun.star.container.XIndexAccess; 41 import com.sun.star.lang.XComponent; 42 import com.sun.star.lang.XMultiServiceFactory; 43 import com.sun.star.sheet.ConditionOperator; 44 import com.sun.star.sheet.XSheetConditionalEntries; 45 import com.sun.star.sheet.XSpreadsheet; 46 import com.sun.star.sheet.XSpreadsheetDocument; 47 import com.sun.star.sheet.XSpreadsheets; 48 import com.sun.star.table.CellAddress; 49 import com.sun.star.uno.AnyConverter; 50 import com.sun.star.uno.Type; 51 import com.sun.star.uno.UnoRuntime; 52 import com.sun.star.uno.XInterface; 53 54 /** 55 * Test for object which is represented by service 56 * <code>com.sun.star.sheet.TableConditionalFormat</code>. <p> 57 * Object implements the following interfaces : 58 * <ul> 59 * <li> <code>com::sun::star::container::XNameAccess</code></li> 60 * <li> <code>com::sun::star::container::XIndexAccess</code></li> 61 * <li> <code>com::sun::star::sheet::XSheetConditionalEntries</code></li> 62 * <li> <code>com::sun::star::container::XElementAccess</code></li> 63 * </ul> 64 * @see com.sun.star.sheet.TableConditionalFormat 65 * @see com.sun.star.container.XNameAccess 66 * @see com.sun.star.container.XIndexAccess 67 * @see com.sun.star.sheet.XSheetConditionalEntries 68 * @see com.sun.star.container.XElementAccess 69 * @see ifc.container._XNameAccess 70 * @see ifc.container._XIndexAccess 71 * @see ifc.sheet._XSheetConditionalEntries 72 * @see ifc.container._XElementAccess 73 */ 74 public class ScTableConditionalFormat extends TestCase { 75 76 static XSpreadsheetDocument xSpreadsheetDoc = null; 77 78 /** 79 * Creates Spreadsheet document. 80 */ 81 protected void initialize( TestParameters tParam, PrintWriter log ) { 82 // get a soffice factory object 83 SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF()); 84 85 try { 86 log.println( "creating a sheetdocument" ); 87 xSpreadsheetDoc = SOF.createCalcDoc(null);; 88 } catch (com.sun.star.uno.Exception e) { 89 // Some exception occures.FAILED 90 e.printStackTrace( log ); 91 throw new StatusException( "Couldn't create document", e ); 92 } 93 } 94 95 /** 96 * Disposes Spreadsheet document. 97 */ 98 protected void cleanup( TestParameters tParam, PrintWriter log ) { 99 log.println( " disposing xSheetDoc " ); 100 XComponent oComp = (XComponent) 101 UnoRuntime.queryInterface (XComponent.class, xSpreadsheetDoc) ; 102 util.DesktopTools.closeDoc(oComp); 103 } 104 105 /** 106 * Creating a Testenvironment for the interfaces to be tested. 107 * Retrieves a collection of spreadsheets from the document and takes one of 108 * them. Fills some cells of the spreadsheet. Retrieves value of the property 109 * <code>'ConditionalFormat'</code> that is the collection of the conditions 110 * of a conditional format. Adds new two conditional entries to the 111 * collection using the interface <code>XSheetConditionalEntries</code>. 112 * The retrieved collection is the instance of the service 113 * <code>com.sun.star.sheet.TableConditionalFormat</code>. 114 * @see com.sun.star.sheet.TableConditionalFormat 115 */ 116 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 117 118 XInterface oObj = null; 119 120 log.println("getting sheets"); 121 XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets(); 122 123 log.println("getting a sheet"); 124 XSpreadsheet oSheet = null; 125 XIndexAccess oIndexAccess = (XIndexAccess) 126 UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets); 127 try { 128 oSheet = (XSpreadsheet) AnyConverter.toObject( 129 new Type(XSpreadsheet.class),oIndexAccess.getByIndex(0)); 130 } catch (com.sun.star.lang.WrappedTargetException e) { 131 e.printStackTrace(log); 132 throw new StatusException( "Couldn't get a spreadsheet", e); 133 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 134 e.printStackTrace(log); 135 throw new StatusException( "Couldn't get a spreadsheet", e); 136 } catch (com.sun.star.lang.IllegalArgumentException e) { 137 e.printStackTrace(log); 138 throw new StatusException( "Couldn't get a spreadsheet", e); 139 } 140 141 log.println("filling some cells"); 142 try { 143 oSheet.getCellByPosition(5, 5).setValue(15); 144 oSheet.getCellByPosition(1, 4).setValue(10); 145 oSheet.getCellByPosition(2, 0).setValue(-5.15); 146 } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 147 log.println("Exception occurred while filling cells"); 148 e.printStackTrace(log); 149 } 150 151 XPropertySet Props = (XPropertySet) 152 UnoRuntime.queryInterface(XPropertySet.class, oSheet); 153 try { 154 oObj = (XInterface) AnyConverter.toObject( 155 new Type(XInterface.class), 156 Props.getPropertyValue("ConditionalFormat")); 157 } catch(com.sun.star.lang.WrappedTargetException e) { 158 e.printStackTrace(log); 159 throw new StatusException("Couldn't get ConditionalFromat", e); 160 } catch(com.sun.star.beans.UnknownPropertyException e) { 161 e.printStackTrace(log); 162 throw new StatusException("Couldn't get ConditionalFromat", e); 163 } catch(com.sun.star.lang.IllegalArgumentException e) { 164 e.printStackTrace(log); 165 throw new StatusException("Couldn't get ConditionalFromat", e); 166 } 167 168 XSheetConditionalEntries xSCE = (XSheetConditionalEntries) 169 UnoRuntime.queryInterface(XSheetConditionalEntries.class, oObj); 170 xSCE.addNew(Conditions(5)); 171 xSCE.addNew(Conditions(2)); 172 173 log.println("creating a new environment for object"); 174 TestEnvironment tEnv = new TestEnvironment(oObj); 175 176 return tEnv; 177 } // finish method getTestEnvironment 178 179 /** 180 * Returns the array of the property values that was filled by condition 181 * values. 182 */ 183 public PropertyValue[] Conditions(int nr) { 184 PropertyValue[] con = new PropertyValue[5]; 185 CellAddress ca = new CellAddress(); 186 ca.Column = 1; 187 ca.Row = 5; 188 ca.Sheet = 0; 189 con[0] = new PropertyValue(); 190 con[0].Name = "StyleName"; 191 con[0].Value = "Result2"; 192 con[1] = new PropertyValue(); 193 con[1].Name = "Formula1"; 194 con[1].Value = "$Sheet1.$B$"+nr; 195 con[2] = new PropertyValue(); 196 con[2].Name = "Formula2"; 197 con[2].Value = ""; 198 con[3] = new PropertyValue(); 199 con[3].Name = "Operator"; 200 con[3].Value = ConditionOperator.EQUAL; 201 con[4] = new PropertyValue(); 202 con[4].Name = "SourcePosition"; 203 con[4].Value = ca; 204 return con; 205 } 206 207 } // finish class ScTableConditionalFormat 208 209