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