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 package ifc.sheet;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.awt.Point;
26cdf0e10cSrcweir import com.sun.star.sheet.TableOperationMode;
27cdf0e10cSrcweir import com.sun.star.sheet.XCellAddressable;
28cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable;
29cdf0e10cSrcweir import com.sun.star.sheet.XMultipleOperation;
30cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
31cdf0e10cSrcweir import com.sun.star.table.XCell;
32cdf0e10cSrcweir import com.sun.star.table.XCellRange;
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import lib.MultiMethodTest;
36cdf0e10cSrcweir import lib.Status;
37cdf0e10cSrcweir import lib.StatusException;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir public class _XMultipleOperation extends MultiMethodTest {
41cdf0e10cSrcweir     public XMultipleOperation oObj = null;
42cdf0e10cSrcweir     protected XSpreadsheet oSheet = null;
43cdf0e10cSrcweir     boolean both = true;
44cdf0e10cSrcweir 
before()45cdf0e10cSrcweir     protected void before() {
46cdf0e10cSrcweir         oSheet = (XSpreadsheet) tEnv.getObjRelation("SHEET");
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         if (oSheet == null) {
49cdf0e10cSrcweir             throw new StatusException(Status.failed(
50cdf0e10cSrcweir                                               "Object relation oSheet is missing"));
51cdf0e10cSrcweir         }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir         if (UnoRuntime.queryInterface(XSpreadsheet.class, tEnv.getTestObject()) != null) {
54cdf0e10cSrcweir             log.println("We have a sheet and won't do TableOperationMode.BOTH");
55cdf0e10cSrcweir             both = false;
56cdf0e10cSrcweir         }
57cdf0e10cSrcweir     }
58cdf0e10cSrcweir 
_setTableOperation()59cdf0e10cSrcweir     public void _setTableOperation() {
60cdf0e10cSrcweir         boolean res = true;
61cdf0e10cSrcweir         XCellRange cellRange = oSheet.getCellRangeByName("$A$17:$A$17");
62cdf0e10cSrcweir         XCellRangeAddressable CRA = (XCellRangeAddressable) UnoRuntime.queryInterface(
63cdf0e10cSrcweir                                             XCellRangeAddressable.class,
64cdf0e10cSrcweir                                             cellRange);
65cdf0e10cSrcweir         XCell cell = null;
66cdf0e10cSrcweir         XCell cell2 = null;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         try {
69cdf0e10cSrcweir             cell = oSheet.getCellByPosition(0, 16);
70cdf0e10cSrcweir             cell.setFormula("=a15+a16");
71cdf0e10cSrcweir             cell = oSheet.getCellByPosition(0, 14);
72cdf0e10cSrcweir             cell2 = oSheet.getCellByPosition(0, 15);
73cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
74cdf0e10cSrcweir             log.println("Exception while getting Cell " + e.getMessage());
75cdf0e10cSrcweir         }
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         XCellAddressable CA = (XCellAddressable) UnoRuntime.queryInterface(
78cdf0e10cSrcweir                                       XCellAddressable.class, cell);
79cdf0e10cSrcweir         XCellAddressable CA2 = (XCellAddressable) UnoRuntime.queryInterface(
80cdf0e10cSrcweir                                        XCellAddressable.class, cell2);
81cdf0e10cSrcweir         Point[] cellCoords = new Point[3];
82cdf0e10cSrcweir         double[] cellValues = new double[3];
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         log.println("filling cells");
85cdf0e10cSrcweir         fillCells();
86cdf0e10cSrcweir         log.println("setting TableOperation with parameter ROW");
87cdf0e10cSrcweir         oObj.setTableOperation(CRA.getRangeAddress(), TableOperationMode.ROW,
88cdf0e10cSrcweir                                CA.getCellAddress(), CA2.getCellAddress());
89cdf0e10cSrcweir         log.println("checking values");
90cdf0e10cSrcweir         cellCoords = new Point[] {
91cdf0e10cSrcweir             new Point(1, 1), new Point(2, 1), new Point(3, 1)
92cdf0e10cSrcweir         };
93cdf0e10cSrcweir         cellValues = new double[] { 5, 10, 15 };
94cdf0e10cSrcweir         res &= checkValues(cellCoords, cellValues);
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         log.println("filling cells");
97cdf0e10cSrcweir         fillCells();
98cdf0e10cSrcweir         log.println("setting TableOperation with parameter COLUMN");
99cdf0e10cSrcweir         oObj.setTableOperation(CRA.getRangeAddress(),
100cdf0e10cSrcweir                                TableOperationMode.COLUMN, CA.getCellAddress(),
101cdf0e10cSrcweir                                CA2.getCellAddress());
102cdf0e10cSrcweir         log.println("checking values");
103cdf0e10cSrcweir         cellCoords = new Point[] {
104cdf0e10cSrcweir             new Point(1, 1), new Point(1, 2), new Point(1, 3)
105cdf0e10cSrcweir         };
106cdf0e10cSrcweir         cellValues = new double[] { 12, 24, 36 };
107cdf0e10cSrcweir         res &= checkValues(cellCoords, cellValues);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         if (both) {
110cdf0e10cSrcweir             log.println("filling cells");
111cdf0e10cSrcweir             fillCells();
112cdf0e10cSrcweir             log.println("setting TableOperation with parameter BOTH");
113cdf0e10cSrcweir             oObj.setTableOperation(CRA.getRangeAddress(),
114cdf0e10cSrcweir                                    TableOperationMode.BOTH,
115cdf0e10cSrcweir                                    CA.getCellAddress(), CA2.getCellAddress());
116cdf0e10cSrcweir             log.println("checking values");
117cdf0e10cSrcweir             cellCoords = new Point[] {
118cdf0e10cSrcweir                 new Point(1, 1), new Point(2, 2), new Point(3, 3)
119cdf0e10cSrcweir             };
120cdf0e10cSrcweir             cellValues = new double[] { 17, 34, 51 };
121cdf0e10cSrcweir             res &= checkValues(cellCoords, cellValues);
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir         tRes.tested("setTableOperation()", res);
125cdf0e10cSrcweir     }
126cdf0e10cSrcweir 
fillCells()127cdf0e10cSrcweir     protected void fillCells() {
128cdf0e10cSrcweir         XCell cell = null;
129cdf0e10cSrcweir 
130cdf0e10cSrcweir         try {
131cdf0e10cSrcweir             for (int k = 1; k < 5; k++) {
132cdf0e10cSrcweir                 cell = oSheet.getCellByPosition(0, k);
133cdf0e10cSrcweir                 cell.setValue(k * 12);
134cdf0e10cSrcweir                 cell = oSheet.getCellByPosition(k, 0);
135cdf0e10cSrcweir                 cell.setValue(k * 5);
136cdf0e10cSrcweir             }
137cdf0e10cSrcweir         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
138cdf0e10cSrcweir             log.println("Exception while filling Cells " + e.getMessage());
139cdf0e10cSrcweir         }
140cdf0e10cSrcweir     }
141cdf0e10cSrcweir 
checkValues(Point[] cellCoords, double[] cellValues)142cdf0e10cSrcweir     protected boolean checkValues(Point[] cellCoords, double[] cellValues) {
143cdf0e10cSrcweir         boolean res = true;
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         for (int i = 0; i < cellValues.length; i++) {
146cdf0e10cSrcweir             try {
147cdf0e10cSrcweir                 boolean locres = oSheet.getCellByPosition(cellCoords[i].X,
148cdf0e10cSrcweir                                                           cellCoords[i].Y)
149cdf0e10cSrcweir                                        .getValue() == cellValues[i];
150cdf0e10cSrcweir                 res &= locres;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir                 if (!locres) {
153cdf0e10cSrcweir                     log.println("Result differs for cell (" +
154cdf0e10cSrcweir                                 cellCoords[i].X + "," + cellCoords[i].Y +
155cdf0e10cSrcweir                                 ")");
156cdf0e10cSrcweir                     log.println("Expected: " + cellValues[i]);
157cdf0e10cSrcweir                     log.println("Getting: " +
158cdf0e10cSrcweir                                 oSheet.getCellByPosition(cellCoords[i].X,
159cdf0e10cSrcweir                                                          cellCoords[i].Y)
160cdf0e10cSrcweir                                       .getValue());
161cdf0e10cSrcweir                 }
162cdf0e10cSrcweir             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
163cdf0e10cSrcweir                 log.println("Exception while checking Values " +
164cdf0e10cSrcweir                             e.getMessage());
165cdf0e10cSrcweir                 res &= false;
166cdf0e10cSrcweir             }
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         return res;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     /**
173cdf0e10cSrcweir      * Restores initial component text.
174cdf0e10cSrcweir      */
after()175cdf0e10cSrcweir     protected void after() {
176cdf0e10cSrcweir         disposeEnvironment();
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir }