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