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.XFormulaQuery;
35 import com.sun.star.sheet.XSheetCellRanges;
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  *
43  * @author  sw93809
44  */
45 public class _XFormulaQuery extends MultiMethodTest {
46 
47     public XFormulaQuery oObj;
48 
49     protected XSpreadsheet oSheet = null;
50     private XCell mxCell;
51     private int miQueryThisDependentRange = 1;
52     private int miQueryThisPrecedentRange = 1;
53     private int[] miExpectedDependentValues;
54     private int[] miExpectedPrecedentValues;
55 
56     protected void before() {
57         oSheet = (XSpreadsheet)tEnv.getObjRelation("SHEET");
58 
59         if (oSheet == null) {
60             log.println("Object relation oSheet is missing");
61             log.println("Trying to query the needed Interface");
62             oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
63                              XSpreadsheet.class, tEnv.getTestObject());
64 
65             if (oSheet == null) {
66                 throw new StatusException(Status.failed(
67                                                   "Object relation oSheet is missing"));
68             }
69         }
70         Object o = tEnv.getObjRelation("MAKEENTRYINCELL");
71         if (o != null) {
72             mxCell = (XCell)o;
73         }
74         else {
75             try {
76                 mxCell = oSheet.getCellByPosition(15, 15);
77             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
78                 throw new StatusException(
79                             Status.failed("Couldn't get initial cell"));
80             }
81         }
82 
83         o = tEnv.getObjRelation("RANGEINDICES");
84         if (o != null) {
85             int[]index = (int[])o;
86             miQueryThisDependentRange = index[0];
87             miQueryThisPrecedentRange = index[1];
88         }
89 
90         o = tEnv.getObjRelation("EXPECTEDDEPENDENTVALUES");
91         if (o != null) {
92             miExpectedDependentValues = (int[])o;
93         }
94         else {
95             miExpectedDependentValues = new int[4];
96             miExpectedDependentValues[0] = 15;
97             miExpectedDependentValues[1] = 15;
98             miExpectedDependentValues[2] = 15;
99             miExpectedDependentValues[3] = 15;
100         }
101 
102         o = tEnv.getObjRelation("EXPECTEDPRECEDENTVALUES");
103         if (o != null) {
104             miExpectedPrecedentValues = (int[])o;
105         }
106         else {
107             miExpectedPrecedentValues = new int[4];
108             miExpectedPrecedentValues[0] = 0;
109             miExpectedPrecedentValues[1] = 0;
110             miExpectedPrecedentValues[2] = 15;
111             miExpectedPrecedentValues[3] = 15;
112         }
113     }
114 
115     public void _queryDependents() {
116         boolean res = true;
117 
118         try {
119             mxCell.setFormula("=sum(A1:D1)");
120             oSheet.getCellByPosition(0, 0).setValue(1);
121             oSheet.getCellByPosition(1, 0).setValue(1);
122             oSheet.getCellByPosition(2, 0).setValue(1);
123             oSheet.getCellByPosition(3, 0).setValue(1);
124 
125             log.println(
126                     "calling oObj.queryDependents(false)");
127             XSheetCellRanges getting = oObj.queryDependents(false);
128             CellRangeAddress[] range = getting.getRangeAddresses();
129 
130             res = ((range[miQueryThisDependentRange].StartColumn==miExpectedDependentValues[0]) &&
131                     (range[miQueryThisDependentRange].EndColumn==miExpectedDependentValues[1]) &&
132                    (range[miQueryThisDependentRange].StartRow==miExpectedDependentValues[2]) &&
133                    (range[miQueryThisDependentRange].EndRow==miExpectedDependentValues[3]));
134 
135             if (!res) {
136                 log.println("Getting ("
137                         +(range[miQueryThisDependentRange]).StartColumn+","
138                         +(range[miQueryThisDependentRange]).EndColumn+","
139                         +(range[miQueryThisDependentRange]).StartRow+","
140                         +(range[miQueryThisDependentRange]).EndRow+")");
141                 log.println("Expected (" + miExpectedDependentValues[0] + "," +
142                                            miExpectedDependentValues[1] + "," +
143                                            miExpectedDependentValues[2] + "," +
144                                            miExpectedDependentValues[3] + ")");
145             }
146         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
147             log.println("Couldn't set initial version to cell");
148             res = false;
149         }
150 
151         tRes.tested("queryDependents()", res);
152     }
153 
154     public void _queryPrecedents() {
155         boolean res = true;
156 
157         try {
158             mxCell.setFormula("=sum(A1:D1)");
159             oSheet.getCellByPosition(0, 0).setValue(1);
160             oSheet.getCellByPosition(1, 0).setValue(1);
161             oSheet.getCellByPosition(2, 0).setValue(1);
162             oSheet.getCellByPosition(3, 0).setValue(1);
163             oSheet.getCellByPosition(1, 2).setFormula("=A16*2");
164 
165             log.println(
166                     "calling oObj.queryPrecedents(false)");
167             XSheetCellRanges getting = oObj.queryPrecedents(false);
168             CellRangeAddress[] range = getting.getRangeAddresses();
169 
170             res = ((range[miQueryThisPrecedentRange].StartColumn==miExpectedPrecedentValues[0]) &&
171                     (range[miQueryThisPrecedentRange].EndColumn==miExpectedPrecedentValues[1]) &&
172                    (range[miQueryThisPrecedentRange].StartRow==miExpectedPrecedentValues[2]) &&
173                    (range[miQueryThisPrecedentRange].EndRow==miExpectedPrecedentValues[3]));
174 
175             if (!res) {
176                 log.println("Getting ("
177                         +(range[miQueryThisPrecedentRange]).StartColumn+","
178                         +(range[miQueryThisPrecedentRange]).EndColumn+","
179                         +(range[miQueryThisPrecedentRange]).StartRow+","
180                         +(range[miQueryThisPrecedentRange]).EndRow+")");
181                 log.println("Expected (" + miExpectedPrecedentValues[0] + "," +
182                                            miExpectedPrecedentValues[1] + "," +
183                                            miExpectedPrecedentValues[2] + "," +
184                                            miExpectedPrecedentValues[3] + ")");
185             }
186         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
187             log.println("Couldn't set initial version to cell");
188             res = false;
189         }
190 
191         tRes.tested("queryPrecedents()", res);
192     }
193 
194     /**
195     * Forces environment recreation.
196     */
197     protected void after() {
198         disposeEnvironment();
199     }
200 
201 }
202