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