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