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 package ifc.form.validation;
24 
25 import com.sun.star.beans.Property;
26 import com.sun.star.beans.PropertyAttribute;
27 import com.sun.star.beans.XMultiPropertySet;
28 import com.sun.star.beans.XPropertySetInfo;
29 import com.sun.star.form.validation.XFormComponentValidityListener;
30 import com.sun.star.form.validation.XValidatableFormComponent;
31 import com.sun.star.uno.UnoRuntime;
32 
33 import lib.MultiMethodTest;
34 
35 import util.ValueChanger;
36 
37 import java.util.StringTokenizer;
38 
39 
40 public class _XValidatableFormComponent extends MultiMethodTest
41 {
42     public XValidatableFormComponent oObj;
43     protected XFormComponentValidityListener listener = null;
44     public boolean listenerCalled = false;
45     private String[] testPropsNames = null;
46     private int testPropsAmount = 0;
47 
_addFormComponentValidityListener()48     public void _addFormComponentValidityListener()
49     {
50         listener = new MyListener();
51 
52         boolean res = true;
53 
54         try
55         {
56             oObj.addFormComponentValidityListener(listener);
57         }
58         catch (com.sun.star.lang.NullPointerException e)
59         {
60             res = false;
61             e.printStackTrace();
62         }
63 
64         changeAllProperties();
65         res &= listenerCalled;
66         tRes.tested("addFormComponentValidityListener()", res);
67     }
68 
_getCurrentValue()69     public void _getCurrentValue()
70     {
71         Object cValue = oObj.getCurrentValue();
72         tRes.tested("getCurrentValue()", true);
73     }
74 
_isValid()75     public void _isValid()
76     {
77         boolean res = oObj.isValid();
78         tRes.tested("isValid()", res);
79     }
80 
_removeFormComponentValidityListener()81     public void _removeFormComponentValidityListener()
82     {
83         requiredMethod("isValid()");
84 
85         boolean res = true;
86 
87         try
88         {
89             oObj.removeFormComponentValidityListener(listener);
90         }
91         catch (com.sun.star.lang.NullPointerException e)
92         {
93             res = false;
94             e.printStackTrace();
95         }
96 
97         listenerCalled = false;
98         changeAllProperties();
99         res &= !listenerCalled;
100         tRes.tested("removeFormComponentValidityListener()", true);
101     }
102 
changeAllProperties()103     protected void changeAllProperties()
104     {
105         XMultiPropertySet mProps =
106             (XMultiPropertySet) UnoRuntime.queryInterface(
107                 XMultiPropertySet.class, tEnv.getTestObject()
108             );
109         XPropertySetInfo propertySetInfo = mProps.getPropertySetInfo();
110         Property[] properties = propertySetInfo.getProperties();
111         getPropsToTest(properties);
112         log.println("Changing all properties");
113 
114         Object[] gValues = mProps.getPropertyValues(testPropsNames);
115 
116         for (int i = 0; i < testPropsAmount; i++)
117         {
118             Object oldValue = gValues[i];
119 
120             if (
121                 testPropsNames[i].equals("Value")
122                     || testPropsNames[i].equals("Time")
123                     || testPropsNames[i].equals("EffectiveValue")
124             )
125             {
126                 oldValue = new Integer(10);
127             }
128 
129             Object newValue = ValueChanger.changePValue(oldValue);
130             gValues[i] = newValue;
131 
132             //            System.out.println("#############################################");
133             //            System.out.println("Name: "+testPropsNames[i]);
134             //            System.out.println("OldValue: "+oldValue);
135             //            System.out.println("NewValue: "+newValue);
136             //            System.out.println("#############################################");
137         }
138 
139         try
140         {
141             mProps.setPropertyValues(testPropsNames, gValues);
142         }
143         catch (com.sun.star.beans.PropertyVetoException e)
144         {
145             log.println("Exception occurred while setting properties");
146             e.printStackTrace(log);
147         }
148         catch (com.sun.star.lang.IllegalArgumentException e)
149         {
150             log.println("Exception occurred while setting properties");
151             e.printStackTrace(log);
152         }
153         catch (com.sun.star.lang.WrappedTargetException e)
154         {
155             log.println("Exception occurred while setting properties");
156             e.printStackTrace(log);
157         }
158          // end of try-catch
159     }
160 
161     //Get the properties being tested
getPropsToTest(Property[] properties)162     private void getPropsToTest(Property[] properties)
163     {
164         String bound = "";
165 
166         for (int i = 0; i < properties.length; i++)
167         {
168             Property property = properties[i];
169             String name = property.Name;
170             boolean isWritable =
171                 ((property.Attributes & PropertyAttribute.READONLY) == 0);
172             boolean isNotNull =
173                 ((property.Attributes & PropertyAttribute.MAYBEVOID) == 0);
174             boolean isBound =
175                 ((property.Attributes & PropertyAttribute.BOUND) != 0);
176 
177             //these have values that are interfaces we can't change
178             if (
179                 name.equals("TextUserDefinedAttributes")
180                     || name.equals("ReferenceDevice")
181                     || name.equals("ParaUserDefinedAttributes")
182             )
183             {
184                 isWritable = false;
185             }
186 
187             if (
188                 name.equals("Value") || name.equals("Time")
189                     || name.equals("Date")
190             )
191             {
192                 bound = (name + ";");
193             }
194 
195             if (
196                 isWritable && isNotNull && (name.indexOf("Format") < 0)
197                     && !name.equals("Enabled")
198             )
199             {
200                 bound += (name + ";");
201             }
202         }
203          // endfor
204 
205         //get a array of bound properties
206         if (bound.equals(""))
207         {
208             bound = "none";
209         }
210 
211         if (tEnv.getTestCase().getObjectName().indexOf("Formatted") > 0)
212         {
213             bound = "EffectiveValue;";
214         }
215 
216         StringTokenizer ST = new StringTokenizer(bound, ";");
217         int nr = ST.countTokens();
218         testPropsNames = new String[nr];
219 
220         for (int i = 0; i < nr; i++)
221             testPropsNames[i] = ST.nextToken();
222 
223         testPropsAmount = nr;
224 
225         return;
226     }
227 
228     protected class MyListener implements XFormComponentValidityListener
229     {
componentValidityChanged( com.sun.star.lang.EventObject eventObject )230         public void componentValidityChanged(
231             com.sun.star.lang.EventObject eventObject
232         )
233         {
234             System.out.println("componentValidityChanged called");
235             listenerCalled = true;
236         }
237 
disposing(com.sun.star.lang.EventObject eventObject)238         public void disposing(com.sun.star.lang.EventObject eventObject)
239         {
240             System.out.println("Listener Disposed");
241         }
242     }
243 }
244