1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package ifc.view; 25 26 import lib.MultiMethodTest; 27 import lib.Status; 28 import lib.StatusException; 29 import util.utils; 30 31 import com.sun.star.beans.PropertyValue; 32 import com.sun.star.lang.XMultiServiceFactory; 33 import com.sun.star.ucb.XSimpleFileAccess; 34 import com.sun.star.uno.UnoRuntime; 35 import com.sun.star.view.PaperOrientation; 36 import com.sun.star.view.XPrintable; 37 38 /** 39 * Testing <code>com.sun.star.view.XPrintable</code> 40 * interface methods : 41 * <ul> 42 * <li><code> getPrinter()</code></li> 43 * <li><code> setPrinter()</code></li> 44 * <li><code> print()</code></li> 45 * </ul> <p> 46 * Test is <b> NOT </b> multithread compilant. <p> 47 * @see com.sun.star.view.XPrintable 48 */ 49 public class _XPrintable extends MultiMethodTest { 50 51 public XPrintable oObj = null; 52 public PropertyValue[] the_printer = null; 53 54 /** 55 * Test calls the method and stores returned value. <p> 56 * Has <b> OK </b> status if the method returns not 57 * <code>null</code> value. 58 */ _getPrinter()59 public void _getPrinter(){ 60 61 the_printer = oObj.getPrinter(); 62 tRes.tested("getPrinter()",the_printer != null); 63 } // finish _getPrinter 64 65 /** 66 * Changes <code>PaperOrientation</code> property in the old 67 * printer configuration and sets changed value as a new printer.<p> 68 * 69 * Has <b> OK </b> status if the <code>getPrinter</code> method 70 * retursn printer with changed property. <p> 71 * 72 * The following method tests are to be completed successfully before : 73 * <ul> 74 * <li> <code> getPrinter() </code> : to change one property 75 * in existing printer configuration. </li> 76 * </ul> 77 */ _setPrinter()78 public void _setPrinter(){ 79 requiredMethod("getPrinter()"); 80 int propIdx = 0 ; 81 while (!"PaperOrientation".equals(the_printer[propIdx].Name)) { 82 propIdx++ ; 83 } 84 PaperOrientation newVal = null ; 85 if (the_printer[propIdx].Value == PaperOrientation.PORTRAIT) 86 newVal = PaperOrientation.LANDSCAPE ; 87 else 88 newVal = PaperOrientation.PORTRAIT ; 89 90 the_printer[propIdx].Value = newVal ; 91 92 try { 93 oObj.setPrinter(the_printer); 94 } catch (com.sun.star.lang.IllegalArgumentException ex) { 95 log.println("couldn't set printer"); 96 ex.printStackTrace(log); 97 tRes.tested("setPrinter()",false); 98 } 99 100 //oObj.setPrinter(the_printer); 101 the_printer = oObj.getPrinter() ; 102 103 propIdx = 0 ; 104 while (!"PaperOrientation".equals(the_printer[propIdx].Name)) { 105 propIdx++ ; 106 } 107 108 boolean the_same = the_printer[propIdx].Value == newVal; 109 tRes.tested("setPrinter()", the_same); 110 111 } // finish _setPrinter 112 113 /** 114 * Printing performed into file in SOffice temp directory. 115 * First this file is deleted if it already exist (using 116 * <code>com.sun.star.ucb.SimpleFileAccess</code> service. 117 * After that the method with appropriate parameter is 118 * called.<p> 119 * 120 * Has <b> OK </b> status if the file to which printing is made 121 * exists. <p> 122 * 123 * @throws StatusException if service 124 * <code>com.sun.star.ucb.SimpleFileAccess</code> cann't be 125 * created. 126 */ _print()127 public void _print(){ 128 boolean result = true ; 129 130 final String file = "XPrintable.prt" ; 131 final String fileName = utils.getOfficeTempDirSys((XMultiServiceFactory)tParam.getMSF())+file ; 132 final String fileURL = utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) + file ; 133 134 XSimpleFileAccess fAcc = null ; 135 try { 136 Object oFAcc = 137 ((XMultiServiceFactory)tParam.getMSF()).createInstance 138 ("com.sun.star.ucb.SimpleFileAccess") ; 139 fAcc = (XSimpleFileAccess) UnoRuntime.queryInterface 140 (XSimpleFileAccess.class, oFAcc) ; 141 if (fAcc == null) throw new StatusException 142 (Status.failed("Can't create SimpleFileAccess service")) ; 143 if (fAcc.exists(fileURL)) { 144 log.println("Old file exists and will be deleted"); 145 fAcc.kill(fileURL); 146 } 147 } catch (com.sun.star.uno.Exception e) { 148 log.println("Error accessing file '" + fileURL + "'"); 149 e.printStackTrace(log); 150 } 151 152 try { 153 PropertyValue[] PrintOptions = new PropertyValue[2]; 154 PropertyValue firstProp = new PropertyValue(); 155 firstProp.Name = "FileName"; 156 log.println("Printing to :"+fileName); 157 firstProp.Value = fileName; 158 firstProp.State = com.sun.star.beans.PropertyState.DEFAULT_VALUE; 159 PrintOptions[0] = firstProp; 160 PrintOptions[1] = new PropertyValue(); 161 PrintOptions[1].Name = "Wait"; 162 PrintOptions[1].Value = new Boolean(true); 163 oObj.print(PrintOptions); 164 } 165 catch (com.sun.star.lang.IllegalArgumentException ex) { 166 log.println("couldn't print"); 167 ex.printStackTrace(log); 168 result = false ; 169 } 170 171 try { 172 boolean fileExists = fAcc.exists(fileURL); 173 174 log.println("File "+fileName+" exists = "+fileExists); 175 176 if (result) { 177 result &= fileExists ; 178 } 179 } catch (com.sun.star.uno.Exception e) { 180 log.println("Error while while checking file '" + 181 fileURL + "': "); 182 e.printStackTrace(log); 183 result = false ; 184 } 185 186 tRes.tested("print()", result) ; 187 188 } // finish _print 189 190 } // finish class _XPrintable 191 192 193