bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / sheet / _XDocumentAuditing.java
blob7e72330e4ea341e940f1a484c19070d8c481abbf
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package ifc.sheet;
20 import com.sun.star.awt.Point;
21 import com.sun.star.beans.PropertyValue;
22 import com.sun.star.container.XIndexAccess;
23 import com.sun.star.container.XNamed;
24 import com.sun.star.drawing.XDrawPage;
25 import com.sun.star.drawing.XDrawPagesSupplier;
26 import com.sun.star.drawing.XShape;
27 import com.sun.star.frame.XDispatchHelper;
28 import com.sun.star.frame.XDispatchProvider;
29 import com.sun.star.frame.XModel;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.sheet.XDocumentAuditing;
32 import com.sun.star.sheet.XSheetAuditing;
33 import com.sun.star.sheet.XSpreadsheet;
34 import com.sun.star.sheet.XSpreadsheetDocument;
35 import com.sun.star.sheet.XSpreadsheets;
36 import com.sun.star.table.CellAddress;
37 import com.sun.star.uno.UnoRuntime;
38 import lib.MultiMethodTest;
39 import lib.Status;
40 import lib.StatusException;
42 /**
45 public class _XDocumentAuditing extends MultiMethodTest {
46 public XDocumentAuditing oObj = null;
47 XDrawPage xDrawPage = null;
48 XSpreadsheet[] xSheet = null;
49 int elementCount = 0;
50 String sheetName = null;
51 Point pos = null;
53 public void before() {
54 Exception ex = null;
55 // get two sheets
56 xSheet = new XSpreadsheet[2];
57 try {
58 XSpreadsheetDocument xSpreadsheetDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class, oObj);
59 XSpreadsheets oSheets = xSpreadsheetDocument.getSheets();
60 XIndexAccess oIndexSheets = UnoRuntime.queryInterface(
61 XIndexAccess.class, oSheets);
62 XSpreadsheet oSheet = UnoRuntime.queryInterface(
63 XSpreadsheet.class, oIndexSheets.getByIndex(0));
64 xSheet[0] = oSheet;
65 oSheet = UnoRuntime.queryInterface(
66 XSpreadsheet.class, oIndexSheets.getByIndex(1));
67 xSheet[1] = oSheet;
69 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
70 ex = e;
72 catch(com.sun.star.lang.WrappedTargetException e) {
73 ex = e;
75 catch(java.lang.NullPointerException e) {
76 ex = e;
78 if (ex != null) {
79 throw new StatusException("Could not get two sheets.", ex);
82 // get the draw page for checking the shapes
83 xDrawPage = (XDrawPage)tEnv.getObjRelation("XDocumentAuditing.DrawPage");
84 if (xDrawPage == null) { // get from object
85 try {
86 XDrawPagesSupplier oDPS = UnoRuntime.queryInterface(XDrawPagesSupplier.class, oObj);
87 Object o = oDPS.getDrawPages().getByIndex(1);
88 xDrawPage = UnoRuntime.queryInterface(XDrawPage.class, o);
90 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
91 } // ignore exceptions, we'll run into next if statement anyway
92 catch(com.sun.star.lang.WrappedTargetException e) {
95 if (xDrawPage == null) {
96 throw new StatusException(Status.failed("'XSheetAuditing.DrawPage' object relation not found."));
98 if (xDrawPage.hasElements()) {
99 elementCount = xDrawPage.getCount();
102 // switch off the automatic refresh
103 PropertyValue[] props = new PropertyValue[1];
104 props[0] = new PropertyValue();
105 props[0].Name = "AutoRefreshArrows";
106 props[0].Value = Boolean.FALSE;
107 XModel xModel = UnoRuntime.queryInterface(XModel.class, oObj);
108 dispatch(xModel.getCurrentController().getFrame(), (XMultiServiceFactory)tParam.getMSF(), ".uno:AutoRefreshArrows", props);
110 // prepare the sheets
111 try {
112 xSheet[0].getCellByPosition(6, 6).setValue(9);
113 XNamed xNamed = UnoRuntime.queryInterface(XNamed.class, xSheet[0]);
114 sheetName = xNamed.getName();
115 xSheet[1].getCellByPosition(6, 6).setValue(16);
116 xSheet[1].getCellByPosition(6, 7).setFormula("= SQRT(G7)");
117 XSheetAuditing xSheetAuditing = UnoRuntime.queryInterface(XSheetAuditing.class, xSheet[1]);
118 CellAddress add = new CellAddress((short)1, 6, 7);
119 xSheetAuditing.showPrecedents(add);
120 boolean ok = hasRightAmountOfShapes(1);
121 if (!ok)
122 throw new StatusException(Status.failed("Wrong amount of shapes on page."));
124 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
125 throw new StatusException("Could not set formulas on sheets.", e);
129 public void after() {
130 // switch the automatic refresh back on
131 PropertyValue[] props = new PropertyValue[1];
132 props[0] = new PropertyValue();
133 props[0].Name = "AutoRefreshArrows";
134 props[0].Value = Boolean.TRUE;
135 XModel xModel = UnoRuntime.queryInterface(XModel.class, oObj);
136 dispatch(xModel.getCurrentController().getFrame(), (XMultiServiceFactory)tParam.getMSF(), ".uno:AutoRefreshArrows", props);
139 private static boolean maxDelta(int x, int y, int max) {
140 assert max >= 0;
141 return Math.abs(((long) x) - ((long) y)) <= max;
144 public void _refreshArrows() {
145 boolean result = true;
147 Point p0 = pos;
149 try {
150 result &= xSheet[1].getCellByPosition(6, 7).getValue() == 4;
151 xSheet[1].getCellByPosition(6, 7).setFormula("= SQRT(" + sheetName + ".G7)");
152 result &= xSheet[1].getCellByPosition(6, 7).getValue() == 3;
154 catch(com.sun.star.lang.IndexOutOfBoundsException e) {
155 throw new StatusException("Could not set formulas on sheets.", e);
158 result &= hasRightAmountOfShapes(1);
159 Point p1 = pos;
161 // points have to be the same: if not we have an auto update
162 boolean res = maxDelta(p0.X, p1.X, 1) && maxDelta(p0.Y, p1.Y, 1);
163 result &= res;
164 if (!res)
165 log.println("Arrow has been refreshed, but this should have been switched off.");
167 oObj.refreshArrows();
169 result &= hasRightAmountOfShapes(1);
170 Point p2 = pos;
172 // points have to differ
173 res = !(maxDelta(p1.X, p2.X, 1) && maxDelta(p1.Y, p2.Y, 1));
174 result &= res;
175 if (!res)
176 log.println("Arrow has not been refreshed.");
178 tRes.tested("refreshArrows()", result);
182 * Check if the amount of shapes is the right one after displaying that stuff
183 * 2do improve this: check that the shapes are the correct ones -> convwatch
184 * @desiredValue That's the amount of shapes that have to be here.
185 * @return True, if the actual count of shapes is the same
187 private boolean hasRightAmountOfShapes(int desiredValue) {
188 int newCount = xDrawPage.getCount();
189 if (newCount != elementCount + desiredValue) {
190 return false;
192 else {
193 if (desiredValue >= 0) {
194 for (int i=elementCount; i<newCount; i++) {
195 try {
196 Object o = xDrawPage.getByIndex(i);
197 XShape xShape = UnoRuntime.queryInterface(XShape.class, o);
198 pos = xShape.getPosition();
199 System.out.println("Shape Type: " + xShape.getShapeType());
201 catch(com.sun.star.uno.Exception e) {
202 e.printStackTrace();
207 return true;
210 private void dispatch(Object oProvider, XMultiServiceFactory xMSF, String url, PropertyValue[] prop) {
211 XDispatchProvider xDispatchProvider = UnoRuntime.queryInterface(XDispatchProvider.class, oProvider);
212 Object dispatcher = null;
213 try {
214 dispatcher = xMSF.createInstance("com.sun.star.frame.DispatchHelper");
216 catch(com.sun.star.uno.Exception e) {
219 XDispatchHelper xDispatchHelper = UnoRuntime.queryInterface(XDispatchHelper.class, dispatcher);
220 xDispatchHelper.executeDispatch(xDispatchProvider, url, "", 0, prop);