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 com.sun.star.awt.Point;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.container.XIndexAccess;
28 import com.sun.star.container.XNamed;
29 import com.sun.star.drawing.XDrawPage;
30 import com.sun.star.drawing.XDrawPagesSupplier;
31 import com.sun.star.drawing.XShape;
32 import com.sun.star.frame.XDispatchHelper;
33 import com.sun.star.frame.XDispatchProvider;
34 import com.sun.star.frame.XModel;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.sheet.XDocumentAuditing;
37 import com.sun.star.sheet.XSheetAuditing;
38 import com.sun.star.sheet.XSpreadsheet;
39 import com.sun.star.sheet.XSpreadsheetDocument;
40 import com.sun.star.sheet.XSpreadsheets;
41 import com.sun.star.table.CellAddress;
42 import com.sun.star.uno.UnoRuntime;
43 import lib.MultiMethodTest;
44 import lib.Status;
45 import lib.StatusException;
46 
47 /**
48  *
49  */
50 public class _XDocumentAuditing extends MultiMethodTest {
51     public XDocumentAuditing oObj = null;
52     XDrawPage xDrawPage = null;
53     XSpreadsheet[] xSheet = null;
54     int elementCount = 0;
55     String sheetName = null;
56     Point pos = null;
57 
58     public void before() {
59         Exception ex = null;
60         // get two sheets
61         xSheet = new XSpreadsheet[2];
62         try {
63             XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)
64                     UnoRuntime.queryInterface(XSpreadsheetDocument.class, oObj);
65             XSpreadsheets oSheets = xSpreadsheetDocument.getSheets();
66             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
67                                                 XIndexAccess.class, oSheets);
68             XSpreadsheet oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
69                                       XSpreadsheet.class, oIndexSheets.getByIndex(0));
70             xSheet[0] = oSheet;
71             oSheet = (XSpreadsheet) UnoRuntime.queryInterface(
72                                       XSpreadsheet.class, oIndexSheets.getByIndex(1));
73             xSheet[1] = oSheet;
74         }
75         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
76             ex = e;
77         }
78         catch(com.sun.star.lang.WrappedTargetException e) {
79             ex = e;
80         }
81         catch(java.lang.NullPointerException e) {
82             ex = e;
83         }
84         if (ex != null) {
85             throw new StatusException("Could not get two sheets.", ex);
86         }
87 
88         // get the draw page for checking the shapes
89         xDrawPage = (XDrawPage)tEnv.getObjRelation("XDocumentAuditing.DrawPage");
90         if (xDrawPage == null) { // get from object
91             try {
92                 XDrawPagesSupplier oDPS = (XDrawPagesSupplier)
93                     UnoRuntime.queryInterface(XDrawPagesSupplier.class, oObj);
94                 Object o = oDPS.getDrawPages().getByIndex(1);
95                 xDrawPage = (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, o);
96             }
97             catch(com.sun.star.lang.IndexOutOfBoundsException e) {
98             } // ignore exceptions, we'll run into next if statement anyway
99             catch(com.sun.star.lang.WrappedTargetException e) {
100             }
101         }
102         if (xDrawPage == null) {
103             throw new StatusException(Status.failed("'XSheetAuditing.DrawPage' object relation not found."));
104         }
105         if (xDrawPage.hasElements()) {
106             elementCount = xDrawPage.getCount();
107         }
108 
109         // switch off the automatic refresh
110         PropertyValue[] props = new PropertyValue[1];
111         props[0] = new PropertyValue();
112         props[0].Name = "AutoRefreshArrows";
113         props[0].Value = Boolean.FALSE;
114         XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, oObj);
115         dispatch(xModel.getCurrentController().getFrame(), (XMultiServiceFactory)tParam.getMSF(), ".uno:AutoRefreshArrows", props);
116 
117         // prepare the sheets
118         try {
119             xSheet[0].getCellByPosition(6, 6).setValue(9);
120             XNamed xNamed = (XNamed)UnoRuntime.queryInterface(XNamed.class, xSheet[0]);
121             sheetName = xNamed.getName();
122             xSheet[1].getCellByPosition(6, 6).setValue(16);
123             xSheet[1].getCellByPosition(6, 7).setFormula("= SQRT(G7)");
124             XSheetAuditing xSheetAuditing = (XSheetAuditing)UnoRuntime.queryInterface(XSheetAuditing.class, xSheet[1]);
125             CellAddress add = new CellAddress((short)1, 6, 7);
126             xSheetAuditing.showPrecedents(add);
127             boolean ok = hasRightAmountOfShapes(1);
128             if (!ok)
129                 throw new StatusException(Status.failed("Wrong amount of shapes on page."));
130         }
131         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
132             throw new StatusException("Could not set formulas on sheets.", e);
133         }
134     }
135 
136     public void after() {
137         // switch the automatic refresh back on
138         PropertyValue[] props = new PropertyValue[1];
139         props[0] = new PropertyValue();
140         props[0].Name = "AutoRefreshArrows";
141         props[0].Value = Boolean.TRUE;
142         XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, oObj);
143         dispatch(xModel.getCurrentController().getFrame(), (XMultiServiceFactory)tParam.getMSF(), ".uno:AutoRefreshArrows", props);
144     }
145 
146     public void _refreshArrows() {
147         boolean result = true;
148 
149         Point p0 = pos;
150 
151         try {
152             result &= xSheet[1].getCellByPosition(6, 7).getValue() == 4;
153             xSheet[1].getCellByPosition(6, 7).setFormula("= SQRT(" + sheetName + ".G7)");
154             result &= xSheet[1].getCellByPosition(6, 7).getValue() == 3;
155         }
156         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
157             throw new StatusException("Could not set formulas on sheets.", e);
158         }
159 
160         result &= hasRightAmountOfShapes(1);
161         Point p1 = pos;
162 
163         // points have to be the same: if not we have an auto update
164         boolean res = (p0.X == p1.X && p0.Y == p1.Y);
165         result &= res;
166         if (!res)
167             log.println("Arrow has been refreshed, but this should have been switched off.");
168 
169         oObj.refreshArrows();
170 
171         result &= hasRightAmountOfShapes(1);
172         Point p2 = pos;
173 
174         // points have to differ
175         res = (p1.X != p2.X || p1.Y != p2.Y);
176         result &= res;
177         if (!res)
178             log.println("Arrow has not been refreshed.");
179 
180         tRes.tested("refreshArrows()", result);
181     }
182 
183     /**
184      * Check if the amount of shapes is the right one after displaying that stuff
185      * 2do improve this: check taht the shapes are the correct ones -> convwatch
186      * @desiredValue That's the amount of shapes that have to be here.
187      * @return True, if the actual count of shapes is the same
188      */
189     private boolean hasRightAmountOfShapes(int desiredValue) {
190         int newCount = xDrawPage.getCount();
191         if (newCount != elementCount + desiredValue) {
192             return false;
193         }
194         else {
195             if (desiredValue >= 0) {
196                 for (int i=elementCount; i<newCount; i++) {
197                     try {
198                         Object o = xDrawPage.getByIndex(i);
199                         XShape xShape = (XShape)UnoRuntime.queryInterface(XShape.class, o);
200                         pos = xShape.getPosition();
201                         System.out.println("Shape Type: " + xShape.getShapeType());
202                     }
203                     catch(com.sun.star.uno.Exception e) {
204                         e.printStackTrace();
205                     }
206                 }
207             }
208         }
209         return true;
210     }
211 
212     private void dispatch(Object oProvider, XMultiServiceFactory xMSF, String url, PropertyValue[] prop) {
213         XDispatchProvider xDispatchProvider = (XDispatchProvider)UnoRuntime.queryInterface(XDispatchProvider.class, oProvider);
214         Object dispatcher = null;
215         try {
216             dispatcher = xMSF.createInstance("com.sun.star.frame.DispatchHelper");
217         }
218         catch(com.sun.star.uno.Exception e) {
219         }
220 
221         XDispatchHelper xDispatchHelper = (XDispatchHelper)UnoRuntime.queryInterface(XDispatchHelper.class, dispatcher);
222         xDispatchHelper.executeDispatch(xDispatchProvider, url, "", 0, prop);
223     }
224 }
225