1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package ifc.document;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.Property;
26cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
27cdf0e10cSrcweir import com.sun.star.i18n.XForbiddenCharacters;
28cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
29cdf0e10cSrcweir import java.lang.reflect.Method;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir //import java.awt.print.PrinterJob;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //import javax.print.PrintService;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import lib.MultiPropertyTest;
36cdf0e10cSrcweir import lib.Status;
37cdf0e10cSrcweir import lib.StatusException;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /*
41cdf0e10cSrcweir  * Generic test for all properties contained in this service
42cdf0e10cSrcweir  */
43cdf0e10cSrcweir public class _Settings extends MultiPropertyTest {
44cdf0e10cSrcweir 
45cdf0e10cSrcweir     /**
46cdf0e10cSrcweir      * This property accepts only values in a range of 1-3
47cdf0e10cSrcweir      * @see com.sun.star.document.PrinterIndependentLayout
48cdf0e10cSrcweir      */
_PrinterIndependentLayout()49cdf0e10cSrcweir     public void _PrinterIndependentLayout() {
50cdf0e10cSrcweir         try{
51cdf0e10cSrcweir             Short oldVal = (Short) oObj.getPropertyValue("PrinterIndependentLayout");
52cdf0e10cSrcweir             Short newVal = oldVal.intValue() == 1 ?  new Short("3") : new Short("1");
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir             testProperty("PrinterIndependentLayout", oldVal, newVal);
56cdf0e10cSrcweir 
57cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
58cdf0e10cSrcweir             throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' is unknown."));
59cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
60cdf0e10cSrcweir             throw new StatusException(Status.failed("the property 'PrinterIndependentLayout' could not be tested."));
61cdf0e10cSrcweir         }
62cdf0e10cSrcweir     }
63cdf0e10cSrcweir 
_PrinterName()64cdf0e10cSrcweir     public void _PrinterName() {
65cdf0e10cSrcweir         Object[] oServices = null;
66cdf0e10cSrcweir         Exception ex = null;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         try {
69cdf0e10cSrcweir             Class cPrinterJob = Class.forName("java.awt.print.PrinterJob");
70cdf0e10cSrcweir             Method lookupMethod = cPrinterJob.getDeclaredMethod("lookupPrintServices", new Class[0]);
71cdf0e10cSrcweir             Object retValue = lookupMethod.invoke(cPrinterJob, new Object[0]);
72cdf0e10cSrcweir             oServices = (Object[])retValue;
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         catch(java.lang.ClassNotFoundException e) {
75cdf0e10cSrcweir             ex = e;
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir         catch(java.lang.NoSuchMethodException e) {
78cdf0e10cSrcweir             ex = e;
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir         catch(java.lang.IllegalAccessException e) {
81cdf0e10cSrcweir             ex = e;
82cdf0e10cSrcweir         }
83cdf0e10cSrcweir         catch(java.lang.reflect.InvocationTargetException e) {
84cdf0e10cSrcweir             ex = e;
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         if (ex != null) {
88cdf0e10cSrcweir             // get Java version:
89cdf0e10cSrcweir             String javaVersion = System.getProperty("java.version");
90cdf0e10cSrcweir             throw new StatusException(Status.failed(
91cdf0e10cSrcweir                 "Cannot execute test with current Java version (Java 1.4 required) " +
92cdf0e10cSrcweir                 javaVersion + ": " + ex.getMessage()));
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir //        PrintService[] services = PrinterJob.lookupPrintServices();
95cdf0e10cSrcweir 
96cdf0e10cSrcweir         if (oServices.length > 1) {
97cdf0e10cSrcweir             testProperty("PrinterName", getPrinterNameWithReflection(oServices[0]),
98cdf0e10cSrcweir                             getPrinterNameWithReflection(oServices[1]));
99cdf0e10cSrcweir         } else {
100cdf0e10cSrcweir             log.println(
101cdf0e10cSrcweir                     "checking this property needs at least two printers to be installed on your system");
102cdf0e10cSrcweir             throw new StatusException(Status.failed(
103cdf0e10cSrcweir                                               "only one printer installed so I can't change it"));
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir     }
106cdf0e10cSrcweir 
_ForbiddenCharacters()107cdf0e10cSrcweir     public void _ForbiddenCharacters() {
108cdf0e10cSrcweir         boolean res = true;
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         try {
111cdf0e10cSrcweir             //check if it is read only as specified
112cdf0e10cSrcweir             res &= isReadOnly("ForbiddenCharacters");
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             if (!isReadOnly("ForbiddenCharacters")) {
115cdf0e10cSrcweir                 log.println(
116cdf0e10cSrcweir                         "The Property 'ForbiddenCharacters' isn't readOnly as specified");
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             //check if the property has the right type
120cdf0e10cSrcweir             Object pValue = oObj.getPropertyValue("ForbiddenCharacters");
121cdf0e10cSrcweir             XForbiddenCharacters fc = (XForbiddenCharacters) UnoRuntime.queryInterface(
122cdf0e10cSrcweir                                               XForbiddenCharacters.class,
123cdf0e10cSrcweir                                               pValue);
124cdf0e10cSrcweir             res &= (fc != null);
125cdf0e10cSrcweir         } catch (com.sun.star.beans.UnknownPropertyException e) {
126cdf0e10cSrcweir             log.println(
127cdf0e10cSrcweir                     "Exception while checking property 'ForbiddenCharacters' " +
128cdf0e10cSrcweir                     e.getMessage());
129cdf0e10cSrcweir         } catch (com.sun.star.lang.WrappedTargetException e) {
130cdf0e10cSrcweir             log.println(
131cdf0e10cSrcweir                     "Exception while checking property 'ForbiddenCharacters' " +
132cdf0e10cSrcweir                     e.getMessage());
133cdf0e10cSrcweir         }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir         tRes.tested("ForbiddenCharacters", res);
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
isReadOnly(String PropertyName)138cdf0e10cSrcweir     protected boolean isReadOnly(String PropertyName) {
139cdf0e10cSrcweir         boolean res = false;
140cdf0e10cSrcweir         Property[] props = oObj.getPropertySetInfo().getProperties();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir         for (int i = 0; i < props.length; i++) {
143cdf0e10cSrcweir             if (props[i].Name.equals(PropertyName)) {
144cdf0e10cSrcweir                 res = ((props[i].Attributes & PropertyAttribute.READONLY) != 0);
145cdf0e10cSrcweir             }
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir         return res;
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
getPrinterNameWithReflection(Object pService)151cdf0e10cSrcweir     private String getPrinterNameWithReflection(Object pService) {
152cdf0e10cSrcweir         String pName = null;
153cdf0e10cSrcweir         try {
154cdf0e10cSrcweir             Class cPrintService = Class.forName("javax.print.PrintService");
155cdf0e10cSrcweir             Method getNameMethod = cPrintService.getDeclaredMethod("getName", new Class[0]);
156cdf0e10cSrcweir             Object retValue = getNameMethod.invoke(pService, new Object[0]);
157cdf0e10cSrcweir             pName = (String)retValue;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir         // ignore all excptions: we already ran into one of these if Java is too old
160cdf0e10cSrcweir         catch(java.lang.ClassNotFoundException e) {
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir         catch(java.lang.NoSuchMethodException e) {
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir         catch(java.lang.IllegalAccessException e) {
165cdf0e10cSrcweir         }
166cdf0e10cSrcweir         catch(java.lang.reflect.InvocationTargetException e) {
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir         return pName;
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir }
171