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 package ifc.sheet;
24 
25 import lib.MultiMethodTest;
26 import lib.Status;
27 import lib.StatusException;
28 
29 import com.sun.star.sheet.SubTotalColumn;
30 import com.sun.star.sheet.XSpreadsheet;
31 import com.sun.star.sheet.XSubTotalCalculatable;
32 import com.sun.star.sheet.XSubTotalDescriptor;
33 import com.sun.star.uno.UnoRuntime;
34 
35 
36 public class _XSubTotalCalculatable extends MultiMethodTest {
37     public XSubTotalCalculatable oObj;
38     protected XSubTotalDescriptor desc;
39     protected XSpreadsheet oSheet;
40 
before()41     protected void before() {
42         oSheet = (XSpreadsheet) tEnv.getObjRelation("SHEET");
43 
44         if (oSheet == null) {
45             log.println("Object relation oSheet is missing");
46             log.println("Trying to query the needed Interface");
47             oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
48                              XSpreadsheet.class, tEnv.getTestObject());
49 
50             if (oSheet == null) {
51                 throw new StatusException(Status.failed(
52                                                   "Object relation oSheet is missing"));
53             }
54         }
55     }
56 
_applySubTotals()57     public void _applySubTotals() {
58         requiredMethod("createSubTotalDescriptor()");
59 
60         boolean res = true;
61 
62         try {
63             oSheet.getCellByPosition(0, 0).setFormula("first");
64             oSheet.getCellByPosition(1, 0).setFormula("second");
65             oSheet.getCellByPosition(0, 3).setFormula("");
66             oSheet.getCellByPosition(0, 1).setValue(5);
67             oSheet.getCellByPosition(0, 2).setValue(5);
68             oSheet.getCellByPosition(1, 1).setValue(17);
69             oSheet.getCellByPosition(1, 2).setValue(25);
70             oObj.applySubTotals(desc, true);
71 
72             String formula = oSheet.getCellByPosition(0, 3).getFormula();
73             String expected = "=SUBTOTAL(9;$A$2:$A$3)";
74             res = formula.equals(expected);
75 
76             if (!res) {
77                 log.println("getting: " + formula);
78                 log.println("expected: " + expected);
79             }
80         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
81             log.println("Couldn't fill cells" + e.getLocalizedMessage());
82             res = false;
83         }
84 
85         tRes.tested("applySubTotals()", res);
86     }
87 
_createSubTotalDescriptor()88     public void _createSubTotalDescriptor() {
89         desc = oObj.createSubTotalDescriptor(true);
90 
91         SubTotalColumn[] columns = new SubTotalColumn[1];
92         columns[0] = new SubTotalColumn();
93         columns[0].Column = 0;
94         columns[0].Function = com.sun.star.sheet.GeneralFunction.SUM;
95         desc.addNew(columns, 0);
96         tRes.tested("createSubTotalDescriptor()", true);
97     }
98 
_removeSubTotals()99     public void _removeSubTotals() {
100         requiredMethod("applySubTotals()");
101 
102         boolean res = true;
103 
104         try {
105             oObj.removeSubTotals();
106 
107             String formula = oSheet.getCellByPosition(0, 3).getFormula();
108             String expected = "";
109             res = formula.equals(expected);
110 
111             if (!res) {
112                 log.println("getting: " + formula);
113                 log.println("expected: " + expected);
114             }
115         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
116             log.println("Couldn't get cell" + e.getLocalizedMessage());
117         }
118 
119         tRes.tested("removeSubTotals()", res);
120     }
121 }