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.form;
25 
26 import lib.MultiPropertyTest;
27 import util.utils;
28 
29 import com.sun.star.form.XLoadable;
30 import com.sun.star.uno.XInterface;
31 
32 /**
33 * Testing <code>com.sun.star.form.DataAwareControlModel</code>
34 * service properties :
35 * <ul>
36 *  <li><code> DataField</code></li>
37 *  <li><code> BoundField</code></li>
38 *  <li><code> LabelControl</code></li>
39 * </ul> <p>
40 * This test need the following object relations :
41 * <ul>
42 *  <li> <code>'FL'</code> as <code>com.sun.star.form.XLoadable</code>
43 *    implementation : used to connect control to data source. </li>
44 *  <li> <code>'LC'</code> as <code>com.sun.star.uno.XInterface</code>
45 *    implementation : as value for LableControl property (see property
46 *    documentation). </li>
47 *  <li> <code>'DataAwareControlModel.NewFieldName'</code> :
48 *   <b>optional</b> <code>String</code> relation which is used
49 *   for new value of DataFiled property.</li>
50 * <ul> <p>
51 * Test is <b> NOT </b> multithread compilant. <p>
52 * After test completion object environment has to be recreated.
53 * @see com.sun.star.form.DataAwareControlModel
54 */
55 public class _DataAwareControlModel extends MultiPropertyTest {
56 
57     /**
58     * First checks if <code>com.sun.star.form.DataAwareControlModel</code>
59     * service is supported by the object. Then <code>load</code> method
60     * of <code>'FL'</code> relation is called to connect control model
61     * to data source. Because the property is READONLY its value is
62     * just checked to be non null. After that model is disconnected. <p>
63     * Has <b> OK </b> status if the property has non null value. <p>
64     * The following property tests are to be completed successfully before :
65     * <ul>
66     *  <li> <code> DataField </code> : to bind the control to some database
67     *    field.</li>
68     * </ul>
69     */
_BoundField()70     public void _BoundField() {
71         requiredMethod("DataField") ;
72 
73         // This property is TRANSIENT and READONLY.
74         boolean bResult = true;
75         Object gValue = null;
76 
77         // get the loader to load the form
78         XLoadable loader = (XLoadable) tEnv.getObjRelation("FL");
79 
80         try {
81             if (loader.isLoaded()) {
82                 loader.unload() ;
83             }
84             loader.load();
85             gValue = oObj.getPropertyValue("BoundField");
86             loader.unload();
87             bResult &= gValue != null;
88         } catch(com.sun.star.beans.UnknownPropertyException e) {
89             e.printStackTrace(log) ;
90             bResult = false;
91         } catch(com.sun.star.lang.WrappedTargetException e) {
92             e.printStackTrace(log) ;
93             bResult = false;
94         }
95         tRes.tested("BoundField", bResult);
96     }
97 
98     /**
99     * Sets the property to a Database field name, and then checks
100     * if it was properly set. If <code>'DataAwareControlModel.NewFieldName'
101     * </code> relation is not found, then new property value is
102     * 'Address'. <p>
103     * Has <b> OK </b> status if the property was properly set
104     * and no exceptions were thrown. If old and new values are equal
105     * the test is OK if no exceptions occurred and the value remains
106     * the same.<p>
107     */
_DataField()108     public void _DataField() {
109         String relVal = (String) tEnv.getObjRelation
110             ("DataAwareControlModel.NewFieldName") ;
111         final String newVal = relVal == null ? "Address" : relVal ;
112         testProperty("DataField", new PropertyTester() {
113             protected Object getNewValue(String p, Object oldVal) {
114                 return newVal ;
115             }
116             protected void checkResult(String propName, Object oldValue,
117                 Object newValue, Object resValue, Exception exception)
118                 throws java.lang.Exception{
119 
120                 if (exception == null && oldValue.equals(newValue)) {
121                     boolean res = true ;
122                     if (newValue.equals(resValue)) {
123                         log.println("Old value is equal to new value ('" +
124                             oldValue + "'). Possibly no other suitable fields found.") ;
125                     } else {
126                         log.println("The value was '" + oldValue + "', set to the" +
127                             " same value, but result is '" + resValue + "' : FAILED") ;
128                         res = false ;
129                     }
130 
131                     tRes.tested(propName, res) ;
132                 } else {
133                     super.checkResult(propName, oldValue, newValue,
134                                       resValue, exception);
135                 }
136             }
137         }) ;
138     }
139 
140     /**
141     * Sets the new value (<code>'LC'</code> relation if <code>null</code>
142     * was before, and <code> null </code> otherwise and then checks if
143     * the value have successfully changed. <p>
144     * Has <b>OK</b> status if the value successfully changed.
145     */
_LabelControl()146     public void _LabelControl() {
147         final XInterface xTextLabel = (XInterface)tEnv.getObjRelation("LC");
148 
149         testProperty("LabelControl", new PropertyTester() {
150             protected Object getNewValue(String p, Object oldVal) {
151                 if (utils.isVoid(oldVal)) return xTextLabel ;
152                 else return super.getNewValue("LabelControl",oldVal);
153             }
154         }) ;
155     }
156 
157     /**
158     * Forces environment recreateation.
159     */
after()160     public void after() {
161         disposeEnvironment() ;
162     }
163 }
164 
165