1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.sheet;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir import lib.Status;
28cdf0e10cSrcweir import lib.StatusException;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.sheet.XFormulaQuery;
31cdf0e10cSrcweir import com.sun.star.sheet.XSheetCellRanges;
32cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
33cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress;
34cdf0e10cSrcweir import com.sun.star.table.XCell;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /**
38cdf0e10cSrcweir  *
39cdf0e10cSrcweir  * @author  sw93809
40cdf0e10cSrcweir  */
41cdf0e10cSrcweir public class _XFormulaQuery extends MultiMethodTest {
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     public XFormulaQuery oObj;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     protected XSpreadsheet oSheet = null;
46cdf0e10cSrcweir     private XCell mxCell;
47cdf0e10cSrcweir     private int miQueryThisDependentRange = 1;
48cdf0e10cSrcweir     private int miQueryThisPrecedentRange = 1;
49cdf0e10cSrcweir     private int[] miExpectedDependentValues;
50cdf0e10cSrcweir     private int[] miExpectedPrecedentValues;
51cdf0e10cSrcweir 
before()52cdf0e10cSrcweir     protected void before() {
53cdf0e10cSrcweir         oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET");
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         if (oSheet == null) {
56cdf0e10cSrcweir             log.println("Object relation oSheet is missing");
57cdf0e10cSrcweir             log.println("Trying to query the needed Interface");
58cdf0e10cSrcweir             oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
59cdf0e10cSrcweir                              XSpreadsheet.class, tEnv.getTestObject());
60cdf0e10cSrcweir 
61cdf0e10cSrcweir             if (oSheet == null) {
62cdf0e10cSrcweir                 throw new StatusException(Status.failed(
63cdf0e10cSrcweir                                                   "Object relation oSheet is missing"));
64cdf0e10cSrcweir             }
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir         Object o = tEnv.getObjRelation("MAKEENTRYINCELL");
67cdf0e10cSrcweir         if (o != null) {
68cdf0e10cSrcweir             mxCell = (XCell)o;
69cdf0e10cSrcweir         }
70cdf0e10cSrcweir         else {
71cdf0e10cSrcweir             try {
72cdf0e10cSrcweir                 mxCell = oSheet.getCellByPosition(15, 15);
73cdf0e10cSrcweir             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
74cdf0e10cSrcweir                 throw new StatusException(
75cdf0e10cSrcweir                             Status.failed("Couldn't get initial cell"));
76cdf0e10cSrcweir             }
77cdf0e10cSrcweir         }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir         o = tEnv.getObjRelation("RANGEINDICES");
80cdf0e10cSrcweir         if (o != null) {
81cdf0e10cSrcweir             int[]index = (int[])o;
82cdf0e10cSrcweir             miQueryThisDependentRange = index[0];
83cdf0e10cSrcweir             miQueryThisPrecedentRange = index[1];
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir         o = tEnv.getObjRelation("EXPECTEDDEPENDENTVALUES");
87cdf0e10cSrcweir         if (o != null) {
88cdf0e10cSrcweir             miExpectedDependentValues = (int[])o;
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir         else {
91cdf0e10cSrcweir             miExpectedDependentValues = new int[4];
92cdf0e10cSrcweir             miExpectedDependentValues[0] = 15;
93cdf0e10cSrcweir             miExpectedDependentValues[1] = 15;
94cdf0e10cSrcweir             miExpectedDependentValues[2] = 15;
95cdf0e10cSrcweir             miExpectedDependentValues[3] = 15;
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         o = tEnv.getObjRelation("EXPECTEDPRECEDENTVALUES");
99cdf0e10cSrcweir         if (o != null) {
100cdf0e10cSrcweir             miExpectedPrecedentValues = (int[])o;
101cdf0e10cSrcweir         }
102cdf0e10cSrcweir         else {
103cdf0e10cSrcweir             miExpectedPrecedentValues = new int[4];
104cdf0e10cSrcweir             miExpectedPrecedentValues[0] = 0;
105cdf0e10cSrcweir             miExpectedPrecedentValues[1] = 0;
106cdf0e10cSrcweir             miExpectedPrecedentValues[2] = 15;
107cdf0e10cSrcweir             miExpectedPrecedentValues[3] = 15;
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
_queryDependents()111cdf0e10cSrcweir     public void _queryDependents() {
112cdf0e10cSrcweir         boolean res = true;
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         try {
115cdf0e10cSrcweir             mxCell.setFormula("=sum(A1:D1)");
116cdf0e10cSrcweir             oSheet.getCellByPosition(0, 0).setValue(1);
117cdf0e10cSrcweir             oSheet.getCellByPosition(1, 0).setValue(1);
118cdf0e10cSrcweir             oSheet.getCellByPosition(2, 0).setValue(1);
119cdf0e10cSrcweir             oSheet.getCellByPosition(3, 0).setValue(1);
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             log.println(
122cdf0e10cSrcweir                     "calling oObj.queryDependents(false)");
123cdf0e10cSrcweir             XSheetCellRanges getting = oObj.queryDependents(false);
124cdf0e10cSrcweir             CellRangeAddress[] range = getting.getRangeAddresses();
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             res = ((range[miQueryThisDependentRange].StartColumn==miExpectedDependentValues[0]) &&
127cdf0e10cSrcweir                     (range[miQueryThisDependentRange].EndColumn==miExpectedDependentValues[1]) &&
128cdf0e10cSrcweir                    (range[miQueryThisDependentRange].StartRow==miExpectedDependentValues[2]) &&
129cdf0e10cSrcweir                    (range[miQueryThisDependentRange].EndRow==miExpectedDependentValues[3]));
130cdf0e10cSrcweir 
131cdf0e10cSrcweir             if (!res) {
132cdf0e10cSrcweir                 log.println("Getting ("
133cdf0e10cSrcweir                         +(range[miQueryThisDependentRange]).StartColumn+","
134cdf0e10cSrcweir                         +(range[miQueryThisDependentRange]).EndColumn+","
135cdf0e10cSrcweir                         +(range[miQueryThisDependentRange]).StartRow+","
136cdf0e10cSrcweir                         +(range[miQueryThisDependentRange]).EndRow+")");
137cdf0e10cSrcweir                 log.println("Expected (" + miExpectedDependentValues[0] + "," +
138cdf0e10cSrcweir                                            miExpectedDependentValues[1] + "," +
139cdf0e10cSrcweir                                            miExpectedDependentValues[2] + "," +
140cdf0e10cSrcweir                                            miExpectedDependentValues[3] + ")");
141cdf0e10cSrcweir             }
142cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
143cdf0e10cSrcweir             log.println("Couldn't set initial version to cell");
144cdf0e10cSrcweir             res = false;
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         tRes.tested("queryDependents()", res);
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir 
_queryPrecedents()150cdf0e10cSrcweir     public void _queryPrecedents() {
151cdf0e10cSrcweir         boolean res = true;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         try {
154cdf0e10cSrcweir             mxCell.setFormula("=sum(A1:D1)");
155cdf0e10cSrcweir             oSheet.getCellByPosition(0, 0).setValue(1);
156cdf0e10cSrcweir             oSheet.getCellByPosition(1, 0).setValue(1);
157cdf0e10cSrcweir             oSheet.getCellByPosition(2, 0).setValue(1);
158cdf0e10cSrcweir             oSheet.getCellByPosition(3, 0).setValue(1);
159cdf0e10cSrcweir             oSheet.getCellByPosition(1, 2).setFormula("=A16*2");
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             log.println(
162cdf0e10cSrcweir                     "calling oObj.queryPrecedents(false)");
163cdf0e10cSrcweir             XSheetCellRanges getting = oObj.queryPrecedents(false);
164cdf0e10cSrcweir             CellRangeAddress[] range = getting.getRangeAddresses();
165cdf0e10cSrcweir 
166cdf0e10cSrcweir             res = ((range[miQueryThisPrecedentRange].StartColumn==miExpectedPrecedentValues[0]) &&
167cdf0e10cSrcweir                     (range[miQueryThisPrecedentRange].EndColumn==miExpectedPrecedentValues[1]) &&
168cdf0e10cSrcweir                    (range[miQueryThisPrecedentRange].StartRow==miExpectedPrecedentValues[2]) &&
169cdf0e10cSrcweir                    (range[miQueryThisPrecedentRange].EndRow==miExpectedPrecedentValues[3]));
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             if (!res) {
172cdf0e10cSrcweir                 log.println("Getting ("
173cdf0e10cSrcweir                         +(range[miQueryThisPrecedentRange]).StartColumn+","
174cdf0e10cSrcweir                         +(range[miQueryThisPrecedentRange]).EndColumn+","
175cdf0e10cSrcweir                         +(range[miQueryThisPrecedentRange]).StartRow+","
176cdf0e10cSrcweir                         +(range[miQueryThisPrecedentRange]).EndRow+")");
177cdf0e10cSrcweir                 log.println("Expected (" + miExpectedPrecedentValues[0] + "," +
178cdf0e10cSrcweir                                            miExpectedPrecedentValues[1] + "," +
179cdf0e10cSrcweir                                            miExpectedPrecedentValues[2] + "," +
180cdf0e10cSrcweir                                            miExpectedPrecedentValues[3] + ")");
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
183cdf0e10cSrcweir             log.println("Couldn't set initial version to cell");
184cdf0e10cSrcweir             res = false;
185cdf0e10cSrcweir         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         tRes.tested("queryPrecedents()", res);
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir     /**
191cdf0e10cSrcweir     * Forces environment recreation.
192cdf0e10cSrcweir     */
after()193cdf0e10cSrcweir     protected void after() {
194cdf0e10cSrcweir         disposeEnvironment();
195cdf0e10cSrcweir     }
196cdf0e10cSrcweir 
197cdf0e10cSrcweir }
198