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