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