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 ifc.view; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import lib.MultiMethodTest; 31*cdf0e10cSrcweir import lib.Status; 32*cdf0e10cSrcweir import lib.StatusException; 33*cdf0e10cSrcweir import util.utils; 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 36*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 37*cdf0e10cSrcweir import com.sun.star.ucb.XSimpleFileAccess; 38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 39*cdf0e10cSrcweir import com.sun.star.view.PaperOrientation; 40*cdf0e10cSrcweir import com.sun.star.view.XPrintable; 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir /** 43*cdf0e10cSrcweir * Testing <code>com.sun.star.view.XPrintable</code> 44*cdf0e10cSrcweir * interface methods : 45*cdf0e10cSrcweir * <ul> 46*cdf0e10cSrcweir * <li><code> getPrinter()</code></li> 47*cdf0e10cSrcweir * <li><code> setPrinter()</code></li> 48*cdf0e10cSrcweir * <li><code> print()</code></li> 49*cdf0e10cSrcweir * </ul> <p> 50*cdf0e10cSrcweir * Test is <b> NOT </b> multithread compilant. <p> 51*cdf0e10cSrcweir * @see com.sun.star.view.XPrintable 52*cdf0e10cSrcweir */ 53*cdf0e10cSrcweir public class _XPrintable extends MultiMethodTest { 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir public XPrintable oObj = null; 56*cdf0e10cSrcweir public PropertyValue[] the_printer = null; 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir /** 59*cdf0e10cSrcweir * Test calls the method and stores returned value. <p> 60*cdf0e10cSrcweir * Has <b> OK </b> status if the method returns not 61*cdf0e10cSrcweir * <code>null</code> value. 62*cdf0e10cSrcweir */ 63*cdf0e10cSrcweir public void _getPrinter(){ 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir the_printer = oObj.getPrinter(); 66*cdf0e10cSrcweir tRes.tested("getPrinter()",the_printer != null); 67*cdf0e10cSrcweir } // finish _getPrinter 68*cdf0e10cSrcweir 69*cdf0e10cSrcweir /** 70*cdf0e10cSrcweir * Changes <code>PaperOrientation</code> property in the old 71*cdf0e10cSrcweir * printer configuration and sets changed value as a new printer.<p> 72*cdf0e10cSrcweir * 73*cdf0e10cSrcweir * Has <b> OK </b> status if the <code>getPrinter</code> method 74*cdf0e10cSrcweir * retursn printer with changed property. <p> 75*cdf0e10cSrcweir * 76*cdf0e10cSrcweir * The following method tests are to be completed successfully before : 77*cdf0e10cSrcweir * <ul> 78*cdf0e10cSrcweir * <li> <code> getPrinter() </code> : to change one property 79*cdf0e10cSrcweir * in existing printer configuration. </li> 80*cdf0e10cSrcweir * </ul> 81*cdf0e10cSrcweir */ 82*cdf0e10cSrcweir public void _setPrinter(){ 83*cdf0e10cSrcweir requiredMethod("getPrinter()"); 84*cdf0e10cSrcweir int propIdx = 0 ; 85*cdf0e10cSrcweir while (!"PaperOrientation".equals(the_printer[propIdx].Name)) { 86*cdf0e10cSrcweir propIdx++ ; 87*cdf0e10cSrcweir } 88*cdf0e10cSrcweir PaperOrientation newVal = null ; 89*cdf0e10cSrcweir if (the_printer[propIdx].Value == PaperOrientation.PORTRAIT) 90*cdf0e10cSrcweir newVal = PaperOrientation.LANDSCAPE ; 91*cdf0e10cSrcweir else 92*cdf0e10cSrcweir newVal = PaperOrientation.PORTRAIT ; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir the_printer[propIdx].Value = newVal ; 95*cdf0e10cSrcweir 96*cdf0e10cSrcweir try { 97*cdf0e10cSrcweir oObj.setPrinter(the_printer); 98*cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException ex) { 99*cdf0e10cSrcweir log.println("couldn't set printer"); 100*cdf0e10cSrcweir ex.printStackTrace(log); 101*cdf0e10cSrcweir tRes.tested("setPrinter()",false); 102*cdf0e10cSrcweir } 103*cdf0e10cSrcweir 104*cdf0e10cSrcweir //oObj.setPrinter(the_printer); 105*cdf0e10cSrcweir the_printer = oObj.getPrinter() ; 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir propIdx = 0 ; 108*cdf0e10cSrcweir while (!"PaperOrientation".equals(the_printer[propIdx].Name)) { 109*cdf0e10cSrcweir propIdx++ ; 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir boolean the_same = the_printer[propIdx].Value == newVal; 113*cdf0e10cSrcweir tRes.tested("setPrinter()", the_same); 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir } // finish _setPrinter 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir /** 118*cdf0e10cSrcweir * Printing performed into file in SOffice temp directory. 119*cdf0e10cSrcweir * First this file is deleted if it already exist (using 120*cdf0e10cSrcweir * <code>com.sun.star.ucb.SimpleFileAccess</code> service. 121*cdf0e10cSrcweir * After that the method with appropriate parameter is 122*cdf0e10cSrcweir * called.<p> 123*cdf0e10cSrcweir * 124*cdf0e10cSrcweir * Has <b> OK </b> status if the file to which printing is made 125*cdf0e10cSrcweir * exists. <p> 126*cdf0e10cSrcweir * 127*cdf0e10cSrcweir * @throws StatusException if service 128*cdf0e10cSrcweir * <code>com.sun.star.ucb.SimpleFileAccess</code> cann't be 129*cdf0e10cSrcweir * created. 130*cdf0e10cSrcweir */ 131*cdf0e10cSrcweir public void _print(){ 132*cdf0e10cSrcweir boolean result = true ; 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir final String file = "XPrintable.prt" ; 135*cdf0e10cSrcweir final String fileName = utils.getOfficeTempDirSys((XMultiServiceFactory)tParam.getMSF())+file ; 136*cdf0e10cSrcweir final String fileURL = utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) + file ; 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir XSimpleFileAccess fAcc = null ; 139*cdf0e10cSrcweir try { 140*cdf0e10cSrcweir Object oFAcc = 141*cdf0e10cSrcweir ((XMultiServiceFactory)tParam.getMSF()).createInstance 142*cdf0e10cSrcweir ("com.sun.star.ucb.SimpleFileAccess") ; 143*cdf0e10cSrcweir fAcc = (XSimpleFileAccess) UnoRuntime.queryInterface 144*cdf0e10cSrcweir (XSimpleFileAccess.class, oFAcc) ; 145*cdf0e10cSrcweir if (fAcc == null) throw new StatusException 146*cdf0e10cSrcweir (Status.failed("Can't create SimpleFileAccess service")) ; 147*cdf0e10cSrcweir if (fAcc.exists(fileURL)) { 148*cdf0e10cSrcweir log.println("Old file exists and will be deleted"); 149*cdf0e10cSrcweir fAcc.kill(fileURL); 150*cdf0e10cSrcweir } 151*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 152*cdf0e10cSrcweir log.println("Error accessing file '" + fileURL + "'"); 153*cdf0e10cSrcweir e.printStackTrace(log); 154*cdf0e10cSrcweir } 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir try { 157*cdf0e10cSrcweir PropertyValue[] PrintOptions = new PropertyValue[2]; 158*cdf0e10cSrcweir PropertyValue firstProp = new PropertyValue(); 159*cdf0e10cSrcweir firstProp.Name = "FileName"; 160*cdf0e10cSrcweir log.println("Printing to :"+fileName); 161*cdf0e10cSrcweir firstProp.Value = fileName; 162*cdf0e10cSrcweir firstProp.State = com.sun.star.beans.PropertyState.DEFAULT_VALUE; 163*cdf0e10cSrcweir PrintOptions[0] = firstProp; 164*cdf0e10cSrcweir PrintOptions[1] = new PropertyValue(); 165*cdf0e10cSrcweir PrintOptions[1].Name = "Wait"; 166*cdf0e10cSrcweir PrintOptions[1].Value = new Boolean(true); 167*cdf0e10cSrcweir oObj.print(PrintOptions); 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir catch (com.sun.star.lang.IllegalArgumentException ex) { 170*cdf0e10cSrcweir log.println("couldn't print"); 171*cdf0e10cSrcweir ex.printStackTrace(log); 172*cdf0e10cSrcweir result = false ; 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir try { 176*cdf0e10cSrcweir boolean fileExists = fAcc.exists(fileURL); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir log.println("File "+fileName+" exists = "+fileExists); 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir if (result) { 181*cdf0e10cSrcweir result &= fileExists ; 182*cdf0e10cSrcweir } 183*cdf0e10cSrcweir } catch (com.sun.star.uno.Exception e) { 184*cdf0e10cSrcweir log.println("Error while while checking file '" + 185*cdf0e10cSrcweir fileURL + "': "); 186*cdf0e10cSrcweir e.printStackTrace(log); 187*cdf0e10cSrcweir result = false ; 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir tRes.tested("print()", result) ; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir } // finish _print 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir } // finish class _XPrintable 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir 197