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 package ifc.document;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.beans.Property;
30*cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
31*cdf0e10cSrcweir import com.sun.star.i18n.XForbiddenCharacters;
32*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33*cdf0e10cSrcweir import java.lang.reflect.Method;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //import java.awt.print.PrinterJob;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir //import javax.print.PrintService;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import lib.MultiPropertyTest;
40*cdf0e10cSrcweir import lib.Status;
41*cdf0e10cSrcweir import lib.StatusException;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /*
45*cdf0e10cSrcweir  * Generic test for all properties contained in this service
46*cdf0e10cSrcweir  */
47*cdf0e10cSrcweir public class _Settings extends MultiPropertyTest {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     /**
50*cdf0e10cSrcweir      * This property accepts only values in a range of 1-3
51*cdf0e10cSrcweir      * @see com.sun.star.document.PrinterIndependentLayout
52*cdf0e10cSrcweir      */
53*cdf0e10cSrcweir     public void _PrinterIndependentLayout() {
54*cdf0e10cSrcweir         try{
55*cdf0e10cSrcweir             Short oldVal = (Short) oObj.getPropertyValue("PrinterIndependentLayout");
56*cdf0e10cSrcweir             Short newVal = oldVal.intValue() == 1 ?  new Short("3") : new Short("1");
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir             testProperty("PrinterIndependentLayout", oldVal, newVal);
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
62*cdf0e10cSrcweir             throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' is unknown."));
63*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
64*cdf0e10cSrcweir             throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' could not be tested."));
65*cdf0e10cSrcweir         }
66*cdf0e10cSrcweir     }
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir     public void _PrinterName() {
69*cdf0e10cSrcweir         Object[] oServices = null;
70*cdf0e10cSrcweir         Exception ex = null;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir         try {
73*cdf0e10cSrcweir             Class cPrinterJob = Class.forName("java.awt.print.PrinterJob");
74*cdf0e10cSrcweir             Method lookupMethod = cPrinterJob.getDeclaredMethod("lookupPrintServices", new Class[0]);
75*cdf0e10cSrcweir             Object retValue = lookupMethod.invoke(cPrinterJob, new Object[0]);
76*cdf0e10cSrcweir             oServices = (Object[])retValue;
77*cdf0e10cSrcweir         }
78*cdf0e10cSrcweir         catch(java.lang.ClassNotFoundException e) {
79*cdf0e10cSrcweir             ex = e;
80*cdf0e10cSrcweir         }
81*cdf0e10cSrcweir         catch(java.lang.NoSuchMethodException e) {
82*cdf0e10cSrcweir             ex = e;
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         catch(java.lang.IllegalAccessException e) {
85*cdf0e10cSrcweir             ex = e;
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir         catch(java.lang.reflect.InvocationTargetException e) {
88*cdf0e10cSrcweir             ex = e;
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         if (ex != null) {
92*cdf0e10cSrcweir             // get Java version:
93*cdf0e10cSrcweir             String javaVersion = System.getProperty("java.version");
94*cdf0e10cSrcweir             throw new StatusException(Status.failed(
95*cdf0e10cSrcweir                 "Cannot execute test with current Java version (Java 1.4 required) " +
96*cdf0e10cSrcweir                 javaVersion + ": " + ex.getMessage()));
97*cdf0e10cSrcweir         }
98*cdf0e10cSrcweir //        PrintService[] services = PrinterJob.lookupPrintServices();
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir         if (oServices.length > 1) {
101*cdf0e10cSrcweir             testProperty("PrinterName", getPrinterNameWithReflection(oServices[0]),
102*cdf0e10cSrcweir                             getPrinterNameWithReflection(oServices[1]));
103*cdf0e10cSrcweir         } else {
104*cdf0e10cSrcweir             log.println(
105*cdf0e10cSrcweir                     "checking this property needs at least two printers to be installed on your system");
106*cdf0e10cSrcweir             throw new StatusException(Status.failed(
107*cdf0e10cSrcweir                                               "only one printer installed so I can't change it"));
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir     }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     public void _ForbiddenCharacters() {
112*cdf0e10cSrcweir         boolean res = true;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         try {
115*cdf0e10cSrcweir             //check if it is read only as specified
116*cdf0e10cSrcweir             res &= isReadOnly("ForbiddenCharacters");
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir             if (!isReadOnly("ForbiddenCharacters")) {
119*cdf0e10cSrcweir                 log.println(
120*cdf0e10cSrcweir                         "The Property 'ForbiddenCharacters' isn't readOnly as specified");
121*cdf0e10cSrcweir             }
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir             //check if the property has the right type
124*cdf0e10cSrcweir             Object pValue = oObj.getPropertyValue("ForbiddenCharacters");
125*cdf0e10cSrcweir             XForbiddenCharacters fc = (XForbiddenCharacters) UnoRuntime.queryInterface(
126*cdf0e10cSrcweir                                               XForbiddenCharacters.class,
127*cdf0e10cSrcweir                                               pValue);
128*cdf0e10cSrcweir             res &= (fc != null);
129*cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
130*cdf0e10cSrcweir             log.println(
131*cdf0e10cSrcweir                     "Exception while checking property 'ForbiddenCharacters' " +
132*cdf0e10cSrcweir                     e.getMessage());
133*cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
134*cdf0e10cSrcweir             log.println(
135*cdf0e10cSrcweir                     "Exception while checking property 'ForbiddenCharacters' " +
136*cdf0e10cSrcweir                     e.getMessage());
137*cdf0e10cSrcweir         }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir         tRes.tested("ForbiddenCharacters", res);
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     protected boolean isReadOnly(String PropertyName) {
143*cdf0e10cSrcweir         boolean res = false;
144*cdf0e10cSrcweir         Property[] props = oObj.getPropertySetInfo().getProperties();
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         for (int i = 0; i < props.length; i++) {
147*cdf0e10cSrcweir             if (props[i].Name.equals(PropertyName)) {
148*cdf0e10cSrcweir                 res = ((props[i].Attributes & PropertyAttribute.READONLY) != 0);
149*cdf0e10cSrcweir             }
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir         return res;
153*cdf0e10cSrcweir     }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir     private String getPrinterNameWithReflection(Object pService) {
156*cdf0e10cSrcweir         String pName = null;
157*cdf0e10cSrcweir         try {
158*cdf0e10cSrcweir             Class cPrintService = Class.forName("javax.print.PrintService");
159*cdf0e10cSrcweir             Method getNameMethod = cPrintService.getDeclaredMethod("getName", new Class[0]);
160*cdf0e10cSrcweir             Object retValue = getNameMethod.invoke(pService, new Object[0]);
161*cdf0e10cSrcweir             pName = (String)retValue;
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         // ignore all excptions: we already ran into one of these if Java is too old
164*cdf0e10cSrcweir         catch(java.lang.ClassNotFoundException e) {
165*cdf0e10cSrcweir         }
166*cdf0e10cSrcweir         catch(java.lang.NoSuchMethodException e) {
167*cdf0e10cSrcweir         }
168*cdf0e10cSrcweir         catch(java.lang.IllegalAccessException e) {
169*cdf0e10cSrcweir         }
170*cdf0e10cSrcweir         catch(java.lang.reflect.InvocationTargetException e) {
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir         return pName;
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir }
175