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 28*cdf0e10cSrcweir package mod._sc; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.container.NoSuchElementException; 31*cdf0e10cSrcweir import com.sun.star.container.XNameContainer; 32*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage; 33*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPages; 34*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier; 35*cdf0e10cSrcweir import com.sun.star.drawing.XShape; 36*cdf0e10cSrcweir import com.sun.star.form.XForm; 37*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 38*cdf0e10cSrcweir import java.io.PrintWriter; 39*cdf0e10cSrcweir import java.util.Comparator; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir import lib.StatusException; 42*cdf0e10cSrcweir import lib.TestCase; 43*cdf0e10cSrcweir import lib.TestEnvironment; 44*cdf0e10cSrcweir import lib.TestParameters; 45*cdf0e10cSrcweir import util.FormTools; 46*cdf0e10cSrcweir import util.SOfficeFactory; 47*cdf0e10cSrcweir import util.ValueComparer; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 50*cdf0e10cSrcweir import com.sun.star.frame.XModel; 51*cdf0e10cSrcweir import com.sun.star.lang.XComponent; 52*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 53*cdf0e10cSrcweir import com.sun.star.sheet.XCellRangeAddressable; 54*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 55*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 56*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets; 57*cdf0e10cSrcweir import com.sun.star.table.CellRangeAddress; 58*cdf0e10cSrcweir import com.sun.star.table.XCell; 59*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 60*cdf0e10cSrcweir import com.sun.star.uno.Type; 61*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 62*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir /** 65*cdf0e10cSrcweir * Test for object which is represented by service 66*cdf0e10cSrcweir * <code>com.sun.star.sheet.SpreadsheetView</code>. <p> 67*cdf0e10cSrcweir * Object implements the following interfaces : 68*cdf0e10cSrcweir * <ul> 69*cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::XViewSplitable</code></li> 70*cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::XViewFreezable</code></li> 71*cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::SpreadsheetViewSettings</code></li> 72*cdf0e10cSrcweir * <li> <code>com::sun::star::beans::XPropertySet</code></li> 73*cdf0e10cSrcweir * <li> <code>com::sun::star::container::XIndexAccess</code></li> 74*cdf0e10cSrcweir * <li> <code>com::sun::star::container::XElementAccess</code></li> 75*cdf0e10cSrcweir * <li> <code>com::sun::star::sheet::XSpreadsheetView</code></li> 76*cdf0e10cSrcweir * <li> <code>com::sun::star::view::XSelectionSupplier</code></li> 77*cdf0e10cSrcweir * </ul> 78*cdf0e10cSrcweir * @see com.sun.star.sheet.XViewSplitable 79*cdf0e10cSrcweir * @see com.sun.star.sheet.XViewFreezable 80*cdf0e10cSrcweir * @see com.sun.star.sheet.SpreadsheetViewSettings 81*cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet 82*cdf0e10cSrcweir * @see com.sun.star.container.XIndexAccess 83*cdf0e10cSrcweir * @see com.sun.star.container.XElementAccess 84*cdf0e10cSrcweir * @see com.sun.star.sheet.XSpreadsheetView 85*cdf0e10cSrcweir * @see com.sun.star.view.XSelectionSupplier 86*cdf0e10cSrcweir * @see ifc.sheet._XViewSplitable 87*cdf0e10cSrcweir * @see ifc.sheet._XViewFreezable 88*cdf0e10cSrcweir * @see ifc.sheet._SpreadsheetViewSettings 89*cdf0e10cSrcweir * @see ifc.beans._XPropertySet 90*cdf0e10cSrcweir * @see ifc.container._XIndexAccess 91*cdf0e10cSrcweir * @see ifc.container._XElementAccess 92*cdf0e10cSrcweir * @see ifc.sheet._XSpreadsheetView 93*cdf0e10cSrcweir * @see ifc.view._XSelectionSupplier 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir public class ScTabViewObj extends TestCase { 96*cdf0e10cSrcweir public static XSpreadsheetDocument xSpreadsheetDoc; 97*cdf0e10cSrcweir public static XSpreadsheetDocument xSpreadsheetDoc2; 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir /** 100*cdf0e10cSrcweir * Creates Spreadsheet document. 101*cdf0e10cSrcweir */ 102*cdf0e10cSrcweir public void initialize( TestParameters Param, PrintWriter log ) { 103*cdf0e10cSrcweir // get a soffice factory object 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)Param.getMSF()); 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir try { 108*cdf0e10cSrcweir log.println("creating two spreadsheet documents"); 109*cdf0e10cSrcweir xSpreadsheetDoc = SOF.createCalcDoc(null); 110*cdf0e10cSrcweir try { 111*cdf0e10cSrcweir Thread.sleep(1000); 112*cdf0e10cSrcweir } catch (java.lang.InterruptedException e) {} 113*cdf0e10cSrcweir xSpreadsheetDoc2 = SOF.createCalcDoc(null); 114*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 115*cdf0e10cSrcweir e.printStackTrace( log ); 116*cdf0e10cSrcweir throw new StatusException( "Couldn't create document ", e ); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir } 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir /** 121*cdf0e10cSrcweir * Disposes Spreadsheet document. 122*cdf0e10cSrcweir */ 123*cdf0e10cSrcweir protected void cleanup( TestParameters tParam, PrintWriter log ) { 124*cdf0e10cSrcweir log.println( " disposing xSheetDoc " ); 125*cdf0e10cSrcweir XComponent oComp = (XComponent) 126*cdf0e10cSrcweir UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ; 127*cdf0e10cSrcweir util.DesktopTools.closeDoc(oComp); 128*cdf0e10cSrcweir XComponent oComp2 = (XComponent) 129*cdf0e10cSrcweir UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc2) ; 130*cdf0e10cSrcweir util.DesktopTools.closeDoc(oComp2); 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir /** 134*cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 135*cdf0e10cSrcweir * Retieves the current controller of the spreadsheet document using the 136*cdf0e10cSrcweir * interface <code>XModel</code>. The controller is the instance of the 137*cdf0e10cSrcweir * service <code>com.sun.star.sheet.SpreadsheetView</code>. Retrieves 138*cdf0e10cSrcweir * a collection of spreadsheets from the document and takes one of them. 139*cdf0e10cSrcweir * Fills two cells in the spreadsheet. 140*cdf0e10cSrcweir * Object relations created : 141*cdf0e10cSrcweir * <ul> 142*cdf0e10cSrcweir * <li> <code>'Sheet'</code> for 143*cdf0e10cSrcweir * {@link ifc.sheet._XSpreadsheetView}(the retrieved spreadsheet)</li> 144*cdf0e10cSrcweir * </ul> 145*cdf0e10cSrcweir * @see com.sun.star.frame.XModel 146*cdf0e10cSrcweir * @see com.sun.star.sheet.SpreadsheetView 147*cdf0e10cSrcweir */ 148*cdf0e10cSrcweir protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 149*cdf0e10cSrcweir XDrawPage oDrawPage = null; 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir XModel aModel = (XModel) 152*cdf0e10cSrcweir UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir XModel aSecondModel = (XModel) 155*cdf0e10cSrcweir UnoRuntime.queryInterface(XModel.class, xSpreadsheetDoc2); 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir XInterface oObj = aModel.getCurrentController(); 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir log.println("getting sheets"); 160*cdf0e10cSrcweir XSpreadsheets xSpreadsheets = (XSpreadsheets)xSpreadsheetDoc.getSheets(); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir log.println("getting a sheet"); 163*cdf0e10cSrcweir XSpreadsheet oSheet = null; 164*cdf0e10cSrcweir XIndexAccess oIndexAccess = (XIndexAccess) 165*cdf0e10cSrcweir UnoRuntime.queryInterface(XIndexAccess.class, xSpreadsheets); 166*cdf0e10cSrcweir try { 167*cdf0e10cSrcweir oSheet = (XSpreadsheet) AnyConverter.toObject( 168*cdf0e10cSrcweir new Type(XSpreadsheet.class), oIndexAccess.getByIndex(1)); 169*cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 170*cdf0e10cSrcweir e.printStackTrace(log); 171*cdf0e10cSrcweir throw new StatusException( "Couldn't get a spreadsheet", e); 172*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 173*cdf0e10cSrcweir e.printStackTrace(log); 174*cdf0e10cSrcweir throw new StatusException( "Couldn't get a spreadsheet", e); 175*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 176*cdf0e10cSrcweir e.printStackTrace(log); 177*cdf0e10cSrcweir throw new StatusException( "Couldn't get a spreadsheet", e); 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment(oObj); 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir tEnv.addObjRelation("XDispatchProvider.URL", ".uno:Copy") ; 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir log.println("adding 'Sheet' as ObjRelation"); 185*cdf0e10cSrcweir tEnv.addObjRelation("Sheet", oSheet); 186*cdf0e10cSrcweir tEnv.addObjRelation("Frame",aModel.getCurrentController().getFrame()); 187*cdf0e10cSrcweir tEnv.addObjRelation("SecondModel",aSecondModel); 188*cdf0e10cSrcweir tEnv.addObjRelation("FirstModel",aModel); 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir //Relation for XControlAccess 191*cdf0e10cSrcweir tEnv.addObjRelation("DOCUMENT", UnoRuntime.queryInterface(XComponent.class,xSpreadsheetDoc)); 192*cdf0e10cSrcweir tEnv.addObjRelation("XControlAccess.isSheet", Boolean.TRUE); 193*cdf0e10cSrcweir //Relations for XSelectionSupplier 194*cdf0e10cSrcweir XCell cell_1 = null; 195*cdf0e10cSrcweir XCell cell_2 = null; 196*cdf0e10cSrcweir Object cellRange = null; 197*cdf0e10cSrcweir try { 198*cdf0e10cSrcweir cellRange = (Object)oSheet.getCellRangeByPosition(0, 0, 3, 3); 199*cdf0e10cSrcweir cell_1 = oSheet.getCellByPosition(5,5); 200*cdf0e10cSrcweir cell_2 = oSheet.getCellByPosition(7,7); 201*cdf0e10cSrcweir cell_2.setValue(17.5); 202*cdf0e10cSrcweir cell_1.setValue(5.5); 203*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException e) { 204*cdf0e10cSrcweir e.printStackTrace(log); 205*cdf0e10cSrcweir throw new StatusException("Couldn't get some cell", e); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir Object[] selections = {oSheet, cellRange, cell_1, cell_2}; 209*cdf0e10cSrcweir tEnv.addObjRelation("Selections", selections); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir tEnv.addObjRelation("Comparer", new Comparator() { 212*cdf0e10cSrcweir public int compare(Object o1, Object o2) { 213*cdf0e10cSrcweir XCellRangeAddressable adr1 = (XCellRangeAddressable) 214*cdf0e10cSrcweir UnoRuntime.queryInterface(XCellRangeAddressable.class, o1); 215*cdf0e10cSrcweir XCellRangeAddressable adr2 = (XCellRangeAddressable) 216*cdf0e10cSrcweir UnoRuntime.queryInterface(XCellRangeAddressable.class, o2); 217*cdf0e10cSrcweir if (adr1 == null || adr2 == null) return -1; 218*cdf0e10cSrcweir CellRangeAddress range1 = adr1.getRangeAddress(); 219*cdf0e10cSrcweir CellRangeAddress range2 = adr2.getRangeAddress(); 220*cdf0e10cSrcweir return ValueComparer.equalValue(range1, range2) ? 0 : 1 ; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir public boolean equals(Object obj) { 223*cdf0e10cSrcweir return compare(this, obj) == 0; 224*cdf0e10cSrcweir } }); 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir tEnv.addObjRelation("XUserInputInterception.XModel", aModel); 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir // XForm for com.sun.star.view.XFormLayerAccess 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir XForm myForm = null; 231*cdf0e10cSrcweir String kindOfControl="CommandButton"; 232*cdf0e10cSrcweir XShape aShape = null; 233*cdf0e10cSrcweir try{ 234*cdf0e10cSrcweir log.println("adding contol shape '" + kindOfControl + "'"); 235*cdf0e10cSrcweir XComponent oComp = (XComponent) UnoRuntime.queryInterface(XComponent.class, xSpreadsheetDoc) ; 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir aShape = FormTools.createControlShape(oComp, 3000, 4500, 15000, 10000, kindOfControl); 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir } catch (Exception e){ 240*cdf0e10cSrcweir e.printStackTrace(log); 241*cdf0e10cSrcweir throw new StatusException("Couldn't create following control shape : '" + 242*cdf0e10cSrcweir kindOfControl + "': ", e); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir log.println("adding relation for com.sun.star.view.XFormLayerAccess: XForm"); 246*cdf0e10cSrcweir try { 247*cdf0e10cSrcweir log.println( "getting Drawpages" ); 248*cdf0e10cSrcweir XDrawPagesSupplier oDPS = (XDrawPagesSupplier) 249*cdf0e10cSrcweir UnoRuntime.queryInterface(XDrawPagesSupplier.class,xSpreadsheetDoc); 250*cdf0e10cSrcweir XDrawPages oDP = (XDrawPages) oDPS.getDrawPages(); 251*cdf0e10cSrcweir oDP.insertNewByIndex(1); 252*cdf0e10cSrcweir oDP.insertNewByIndex(2); 253*cdf0e10cSrcweir oDrawPage = (XDrawPage) AnyConverter.toObject( 254*cdf0e10cSrcweir new Type(XDrawPage.class),oDP.getByIndex(0)); 255*cdf0e10cSrcweir if (oDrawPage == null) 256*cdf0e10cSrcweir log.println("ERROR: could not get DrawPage: null"); 257*cdf0e10cSrcweir 258*cdf0e10cSrcweir oDrawPage.add(aShape); 259*cdf0e10cSrcweir log.println("getting XForm"); 260*cdf0e10cSrcweir XNameContainer xForm = FormTools.getForms(oDrawPage); 261*cdf0e10cSrcweir try { 262*cdf0e10cSrcweir myForm = (XForm) AnyConverter.toObject(new Type(XForm.class),xForm.getByName("Standard")); 263*cdf0e10cSrcweir } catch (WrappedTargetException ex) { 264*cdf0e10cSrcweir log.println("ERROR: could not XFormm 'Standard': " + ex.toString()); 265*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException ex) { 266*cdf0e10cSrcweir log.println("ERROR: could not XFormm 'Standard': " + ex.toString()); 267*cdf0e10cSrcweir } catch (NoSuchElementException ex) { 268*cdf0e10cSrcweir log.println("ERROR: could not XFormm 'Standard': " + ex.toString()); 269*cdf0e10cSrcweir } 270*cdf0e10cSrcweir if (myForm == null) 271*cdf0e10cSrcweir log.println("ERROR: could not get XForm: null"); 272*cdf0e10cSrcweir tEnv.addObjRelation("XFormLayerAccess.XForm", myForm); 273*cdf0e10cSrcweir } catch (com.sun.star.lang.IndexOutOfBoundsException ex) { 274*cdf0e10cSrcweir log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString()); 275*cdf0e10cSrcweir } catch (WrappedTargetException ex) { 276*cdf0e10cSrcweir log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString()); 277*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException ex) { 278*cdf0e10cSrcweir log.println("ERROR: could not add ObjectRelation 'XFormLayerAccess.XForm': " + ex.toString()); 279*cdf0e10cSrcweir } 280*cdf0e10cSrcweir return tEnv; 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir } // finish class ScTabViewObj 284