1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.form;
29 
30 import lib.MultiPropertyTest;
31 import util.utils;
32 
33 import com.sun.star.form.XLoadable;
34 import com.sun.star.uno.XInterface;
35 
36 /**
37 * Testing <code>com.sun.star.form.DataAwareControlModel</code>
38 * service properties :
39 * <ul>
40 *  <li><code> DataField</code></li>
41 *  <li><code> BoundField</code></li>
42 *  <li><code> LabelControl</code></li>
43 * </ul> <p>
44 * This test need the following object relations :
45 * <ul>
46 *  <li> <code>'FL'</code> as <code>com.sun.star.form.XLoadable</code>
47 *    implementation : used to connect control to data source. </li>
48 *  <li> <code>'LC'</code> as <code>com.sun.star.uno.XInterface</code>
49 *    implementation : as value for LableControl property (see property
50 *    documentation). </li>
51 *  <li> <code>'DataAwareControlModel.NewFieldName'</code> :
52 *   <b>optional</b> <code>String</code> relation which is used
53 *   for new value of DataFiled proeprty.</li>
54 * <ul> <p>
55 * Test is <b> NOT </b> multithread compilant. <p>
56 * After test completion object environment has to be recreated.
57 * @see com.sun.star.form.DataAwareControlModel
58 */
59 public class _DataAwareControlModel extends MultiPropertyTest {
60 
61     /**
62     * First checks if <code>com.sun.star.form.DataAwareControlModel</code>
63     * service is supported by the object. Then <code>load</code> method
64     * of <code>'FL'</code> relation is called to connect control model
65     * to data source. Because the property is READONLY its value is
66     * just checked to be non null. After that model is disconnected. <p>
67     * Has <b> OK </b> status if the property has non null value. <p>
68     * The following property tests are to be completed successfully before :
69     * <ul>
70     *  <li> <code> DataField </code> : to bind the control to some database
71     *    field.</li>
72     * </ul>
73     */
74     public void _BoundField() {
75         requiredMethod("DataField") ;
76 
77         // This property is TRANSIENT and READONLY.
78         boolean bResult = true;
79         Object gValue = null;
80 
81         // get the loader to load the form
82         XLoadable loader = (XLoadable) tEnv.getObjRelation("FL");
83 
84         try {
85             if (loader.isLoaded()) {
86                 loader.unload() ;
87             }
88             loader.load();
89             gValue = oObj.getPropertyValue("BoundField");
90             loader.unload();
91             bResult &= gValue != null;
92         } catch(com.sun.star.beans.UnknownPropertyException e) {
93             e.printStackTrace(log) ;
94             bResult = false;
95         } catch(com.sun.star.lang.WrappedTargetException e) {
96             e.printStackTrace(log) ;
97             bResult = false;
98         }
99         tRes.tested("BoundField", bResult);
100     }
101 
102     /**
103     * Sets the property to a Database field name, and then checks
104     * if it was properly set. If <code>'DataAwareControlModel.NewFieldName'
105     * </code> relation is not found, then new property value is
106     * 'Address'. <p>
107     * Has <b> OK </b> status if the proeprty was properly set
108     * and no exceptions were thrown. If old and new values are equal
109     * the test is OK if no exceptions occured and the value remains
110     * the same.<p>
111     */
112     public void _DataField() {
113         String relVal = (String) tEnv.getObjRelation
114             ("DataAwareControlModel.NewFieldName") ;
115         final String newVal = relVal == null ? "Address" : relVal ;
116         testProperty("DataField", new PropertyTester() {
117             protected Object getNewValue(String p, Object oldVal) {
118                 return newVal ;
119             }
120             protected void checkResult(String propName, Object oldValue,
121                 Object newValue, Object resValue, Exception exception)
122                 throws java.lang.Exception{
123 
124                 if (exception == null && oldValue.equals(newValue)) {
125                     boolean res = true ;
126                     if (newValue.equals(resValue)) {
127                         log.println("Old value is equal to new value ('" +
128                             oldValue + "'). Possibly no other suitable fields found.") ;
129                     } else {
130                         log.println("The value was '" + oldValue + "', set to the" +
131                             " same value, but result is '" + resValue + "' : FAILED") ;
132                         res = false ;
133                     }
134 
135                     tRes.tested(propName, res) ;
136                 } else {
137                     super.checkResult(propName, oldValue, newValue,
138                                       resValue, exception);
139                 }
140             }
141         }) ;
142     }
143 
144     /**
145     * Sets the new value (<code>'LC'</code> relation if <code>null</code>
146     * was before, and <code> null </code> otherwise and then checks if
147     * the value have successfully changed. <p>
148     * Has <b>OK</b> status if the value successfully changed.
149     */
150     public void _LabelControl() {
151         final XInterface xTextLabel = (XInterface)tEnv.getObjRelation("LC");
152 
153         testProperty("LabelControl", new PropertyTester() {
154             protected Object getNewValue(String p, Object oldVal) {
155                 if (utils.isVoid(oldVal)) return xTextLabel ;
156                 else return super.getNewValue("LabelControl",oldVal);
157             }
158         }) ;
159     }
160 
161     /**
162     * Forces environment recreateation.
163     */
164     public void after() {
165         disposeEnvironment() ;
166     }
167 }
168 
169