Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / view / _XPrintable.java
blob21e470fef18c65c235f8b481baa04db092643791
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.view;
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
24 import util.utils;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.ucb.XSimpleFileAccess;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.view.PaperOrientation;
30 import com.sun.star.view.XPrintable;
32 /**
33 * Testing <code>com.sun.star.view.XPrintable</code>
34 * interface methods :
35 * <ul>
36 * <li><code> getPrinter()</code></li>
37 * <li><code> setPrinter()</code></li>
38 * <li><code> print()</code></li>
39 * </ul> <p>
40 * Test is <b> NOT </b> multithread compliant. <p>
41 * @see com.sun.star.view.XPrintable
43 public class _XPrintable extends MultiMethodTest {
45 public XPrintable oObj = null;
46 public PropertyValue[] the_printer = null;
48 /**
49 * Test calls the method and stores returned value. <p>
50 * Has <b> OK </b> status if the method returns not
51 * <code>null</code> value.
53 public void _getPrinter(){
55 the_printer = oObj.getPrinter();
56 tRes.tested("getPrinter()",the_printer != null);
57 } // finish _getPrinter
59 /**
60 * Changes <code>PaperOrientation</code> property in the old
61 * printer configuration and sets changed value as a new printer.<p>
63 * Has <b> OK </b> status if the <code>getPrinter</code> method
64 * retursn printer with changed property. <p>
66 * The following method tests are to be completed successfully before :
67 * <ul>
68 * <li> <code> getPrinter() </code> : to change one property
69 * in existing printer configuration. </li>
70 * </ul>
72 public void _setPrinter(){
73 requiredMethod("getPrinter()");
74 int propIdx = 0 ;
75 while (!"PaperOrientation".equals(the_printer[propIdx].Name)) {
76 propIdx++ ;
78 PaperOrientation newVal = null ;
79 if (the_printer[propIdx].Value == PaperOrientation.PORTRAIT)
80 newVal = PaperOrientation.LANDSCAPE ;
81 else
82 newVal = PaperOrientation.PORTRAIT ;
84 the_printer[propIdx].Value = newVal ;
86 try {
87 oObj.setPrinter(the_printer);
88 } catch (com.sun.star.lang.IllegalArgumentException ex) {
89 log.println("couldn't set printer");
90 ex.printStackTrace(log);
91 tRes.tested("setPrinter()",false);
94 the_printer = oObj.getPrinter() ;
96 propIdx = 0 ;
97 while (!"PaperOrientation".equals(the_printer[propIdx].Name)) {
98 propIdx++ ;
101 boolean the_same = the_printer[propIdx].Value == newVal;
102 tRes.tested("setPrinter()", the_same);
104 } // finish _setPrinter
107 * Printing performed into file in SOffice temp directory.
108 * First this file is deleted if it already exist (using
109 * <code>com.sun.star.ucb.SimpleFileAccess</code> service.
110 * After that the method with appropriate parameter is
111 * called.<p>
113 * Has <b> OK </b> status if the file to which printing is made
114 * exists. <p>
116 * @throws StatusException if service
117 * <code>com.sun.star.ucb.SimpleFileAccess</code> cann't be
118 * created.
120 public void _print(){
121 boolean result = true ;
123 final String file = "XPrintable.prt" ;
124 final String fileName = utils.getOfficeTempDirSys(tParam.getMSF())+file ;
125 final String fileURL = utils.getOfficeTemp(tParam.getMSF()) + file ;
127 XSimpleFileAccess fAcc = null ;
128 try {
129 Object oFAcc =
130 tParam.getMSF().createInstance
131 ("com.sun.star.ucb.SimpleFileAccess") ;
132 fAcc = UnoRuntime.queryInterface
133 (XSimpleFileAccess.class, oFAcc) ;
134 if (fAcc == null) throw new StatusException
135 (Status.failed("Can't create SimpleFileAccess service")) ;
136 if (fAcc.exists(fileURL)) {
137 log.println("Old file exists and will be deleted");
138 fAcc.kill(fileURL);
140 } catch (com.sun.star.uno.Exception e) {
141 log.println("Error accessing file '" + fileURL + "'");
142 e.printStackTrace(log);
145 try {
146 PropertyValue[] PrintOptions = new PropertyValue[2];
147 PropertyValue firstProp = new PropertyValue();
148 firstProp.Name = "FileName";
149 log.println("Printing to :"+fileName);
150 firstProp.Value = fileName;
151 firstProp.State = com.sun.star.beans.PropertyState.DEFAULT_VALUE;
152 PrintOptions[0] = firstProp;
153 PrintOptions[1] = new PropertyValue();
154 PrintOptions[1].Name = "Wait";
155 PrintOptions[1].Value = Boolean.TRUE;
156 oObj.print(PrintOptions);
158 catch (com.sun.star.lang.IllegalArgumentException ex) {
159 log.println("couldn't print");
160 ex.printStackTrace(log);
161 result = false ;
164 try {
165 boolean fileExists = fAcc.exists(fileURL);
167 log.println("File "+fileName+" exists = "+fileExists);
169 if (result) {
170 result &= fileExists ;
172 } catch (com.sun.star.uno.Exception e) {
173 log.println("Error while while checking file '" +
174 fileURL + "': ");
175 e.printStackTrace(log);
176 result = false ;
179 tRes.tested("print()", result) ;
181 } // finish _print
183 } // finish class _XPrintable