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