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 ifc.sheet;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.sheet.XArrayFormulaRange;
31 import com.sun.star.sheet.XCellRangeAddressable;
32 import com.sun.star.sheet.XSpreadsheet;
33 import com.sun.star.table.CellRangeAddress;
34 import com.sun.star.table.XCell;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38 * Testing <code>com.sun.star.sheet.XArrayFormulaRange</code>
39 * interface methods :
40 * <ul>
41 *  <li><code> getArrayFormula()</code></li>
42 *  <li><code> setArrayFormula()</code></li>
43 * </ul> <p>
44 * This test needs the following object relations :
45 * <ul>
46 *  <li> <code>'SHEET'</code> (of type <code>XSpreadsheet</code>):
47 *   to check contents of spreadsheet </li>
48 *  <li> <code>'noArray'</code> (of type <code>Object</code>):
49 *   if the relation is null then given component doesn't really support
50 *   this interface </li>
51 * <ul> <p>
52 * Test object must implements interface <code>XCellRangeAddressable</code> also.
53 * @see com.sun.star.sheet.XArrayFormulaRange
54 * @see com.sun.star.sheet.XSpreadsheet
55 * @see com.sun.star.sheet.XCellRangeAddressable
56 */
57 public class _XArrayFormulaRange extends MultiMethodTest {
58 
59     public XArrayFormulaRange oObj = null;
60     String formula = "=1 + 2 * 5";
61 
62     /**
63     * Test calls the method and then checks content sof spreadsheet using
64     * object relation <code>'SHEET'</code>. <p>
65     * Has <b> OK </b> status if values in cells of spreadsheet are equal to 11
66     * or ArrayFormula not supported.<p>
67     */
_setArrayFormula()68     public void _setArrayFormula() {
69         Object noArray = tEnv.getObjRelation("noArray");
70         if (noArray != null) {
71             log.println("Component " + noArray.toString() +
72                 " doesn't really support this Interface");
73             log.println("It doesn't make sense to set an ArrayFormula over"
74                 + " the whole sheet");
75             tRes.tested("setArrayFormula()", true);
76             return;
77         }
78 
79         boolean result = true;
80         double dresult = 11;
81 
82         log.println("setArrayFormula() ...");
83 
84         oObj.setArrayFormula(formula);
85 
86         log.println("checking that formula was set correctly...");
87         XCellRangeAddressable crAddr =
88             (XCellRangeAddressable)
89                  UnoRuntime.queryInterface(XCellRangeAddressable.class, oObj);
90         CellRangeAddress addr = crAddr.getRangeAddress() ;
91         XSpreadsheet oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET");
92         if (oSheet == null) throw new StatusException(Status.failed
93             ("Relation 'SHEET' not found"));
94 
95         XCell oCell = null;
96         double value;
97 
98         for (int i = addr.StartColumn; i <= addr.EndColumn; i++)
99             for (int j = addr.StartRow; j <= addr.EndRow; j++) {
100                 try {
101                     oCell = oSheet.getCellByPosition(i, j);
102                 } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
103                     e.printStackTrace(log);
104                     result = false;
105                     break;
106                 }
107 
108                 value = oCell.getValue();
109                 result &= (value == dresult);
110             }
111 
112         tRes.tested("setArrayFormula()", result) ;
113 
114     } // end setArrayFormula()
115 
116     /**
117     * Test calls the method and compare formula that set by method
118     * <code>setArrayFormula</code> with returned value ignoring spaces. <p>
119     *
120     * Has <b> OK </b> status if values are equal or
121     * ArrayFormula not supported. <p>
122     *
123     * The following method tests are to be completed successfully before :
124     * <ul>
125     *  <li> <code> setArrayFormula </code> : to set formula </li>
126     * </ul>
127     */
_getArrayFormula()128     public void _getArrayFormula() {
129 
130         Object noArray = tEnv.getObjRelation("noArray");
131         if (noArray != null) {
132             log.println("Component "+noArray.toString()+" doesn't really support this Interface");
133             log.println("It doesn't make sense to set an ArrayFormula over the whole sheet");
134             log.println("and therefore 'getArrayFormula()' won't work");
135             tRes.tested("getArrayFormula()",true);
136             return;
137         }
138 
139         requiredMethod("setArrayFormula()");
140         boolean result = true;
141         log.println("Testing getArrayFormula() ...");
142         String gFormula = oObj.getArrayFormula() ;
143         result &= equalIgnoreSpaces("{" + formula + "}", gFormula);
144         if (!result)
145             log.println("Method returned : '" + oObj.getArrayFormula() + "'") ;
146         tRes.tested("getArrayFormula()", result) ;
147 
148     } // end getArrayFormula()
149 
150     /**
151      * Method compares two string ignoring spaces.
152      * @return <code>true</code> if the argument
153      * is not null and the Strings are equal,
154      * ignoring spaces; <code>false</code> otherwise.
155      */
equalIgnoreSpaces(String s1, String s2)156     private boolean equalIgnoreSpaces(String s1, String s2) {
157         int p1 = 0, p2 = 0 ;
158         s1 = s1.trim() ;
159         s2 = s2.trim() ;
160         while (p1 < s1.length() && p2 < s2.length()) {
161             while (s1.charAt(p1) == ' ') p1 ++ ;
162             while (s2.charAt(p2) == ' ') p2 ++ ;
163             if (s1.charAt(p1) != s2.charAt(p2)) return false ;
164             p1 ++ ;
165             p2 ++ ;
166         }
167 
168         return p1 == s1.length() && p2 == s2.length() ;
169     }
170 
171     /**
172     * Forces environment recreation.
173     */
after()174     protected void after() {
175         disposeEnvironment();
176     }
177 }
178 
179