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.MultiMethodTest;
27 
28 import com.sun.star.awt.MouseEvent;
29 import com.sun.star.awt.XControl;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.form.XSubmit;
32 import com.sun.star.form.XSubmitListener;
33 import com.sun.star.lang.EventObject;
34 import com.sun.star.uno.UnoRuntime;
35 
36 /**
37 * Testing <code>com.sun.star.form.XSubmit</code>
38 * interface methods :
39 * <ul>
40 *  <li><code> submit()</code></li>
41 *  <li><code> addSubmitListener()</code></li>
42 *  <li><code> removeSubmitListener()</code></li>
43 * </ul> <p>
44 *
45 * This test needs the following object relations :
46 * <ul>
47 *  <li> <code>'XSubmit.Control'</code> <b>optional</b>
48 *  (of type <code>com.sun.star.awt.XControl</code>):
49 *   is used to pass as parameters to <code>submit</code>
50 *   method. <code>null</code> is passed if the relation
51 *   is not found.</li>
52 * <ul> <p>
53 *
54 * Other <b> prerequicity </b> is the object must have
55 * <code>TargetURL</code> property. <p>
56 *
57 * Short description : test adds two listeners, call
58 * <code> submit </code> method and cecks if both listeners
59 * were called. Then one listener is removed and after
60 * <code> submit </code> method call it must not be called. <p>
61 *
62 * Test is <b> NOT </b> multithread compilant. <p>
63 * @see com.sun.star.form.XSubmit
64 */
65 public class _XSubmit extends MultiMethodTest {
66 
67     public static XSubmit oObj = null;
68 
69     public class MySubmitListener implements XSubmitListener {
70         public int called = 0 ;
disposing( EventObject oEvent )71         public void disposing ( EventObject oEvent ) {
72         }
approveSubmit( EventObject oEvent )73         public boolean approveSubmit( EventObject oEvent ) {
74             called += 1;
75             System.out.println("Listener called");
76             return true;
77         }
78     }
79 
80 
81     MySubmitListener listener1 = new MySubmitListener();
82     MySubmitListener listener2 = new MySubmitListener();
83 
84     /**
85     * Just adds two submit listeners. <p>
86     * Status of this method test is defined in <code>
87     * submit </code> method test.
88     */
_addSubmitListener()89     public void _addSubmitListener() {
90         log.println("Testing addSubmitListener ...");
91         oObj.addSubmitListener( listener1 );
92         oObj.addSubmitListener( listener2 );
93     }
94 
95     /**
96     * Before submision tries to set 'TargetURL' property
97     * of component to some value assuming that component
98     * supports <code>com.sun.star.form.HTMLForm</code>
99     * service.
100     * Then calls the <code> submit </code> method and checks
101     * if listener removed were not called, and other was
102     * called only once.<p>
103     *
104     * Has <b> OK </b> status for <code>submit</code> if
105     * listener was called at least ones, for
106     * <code>addSubmitListener</code> method if the remaining
107     * listener was called only once, for
108     * <code>removeSubmitListener</code> method if the removed
109     * listener was not called. <p>
110     *
111     * The following method tests are to be completed successfully before :
112     * <ul>
113     *  <li> <code> removeSubmitListener </code> : to have one listener
114     *  added and other removed.</li>
115     * </ul>
116     */
_submit()117     public void _submit() {
118         executeMethod("removeSubmitListener()");
119         log.println("Testing submit() ...");
120         XControl cntrl = (XControl) tEnv.getObjRelation("XSubmit.Control") ;
121 
122         XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface
123             (XPropertySet.class, oObj) ;
124 
125         if (xPS != null) {
126             try {
127                 xPS.setPropertyValue("TargetURL", "someserver");
128             } catch (com.sun.star.lang.WrappedTargetException e) {
129                 e.printStackTrace(log);
130             } catch (com.sun.star.lang.IllegalArgumentException e) {
131                 e.printStackTrace(log);
132             } catch (com.sun.star.beans.PropertyVetoException e) {
133                 e.printStackTrace(log);
134             } catch (com.sun.star.beans.UnknownPropertyException e) {
135                 e.printStackTrace(log);
136             }
137         } else {
138             log.println("!!! The tested compoennt doesn't support XPropertySet ");
139         }
140 
141         oObj.submit(cntrl, new MouseEvent());
142         shortWait();
143 
144         log.println("Listener1 called " + listener1.called + " times");
145         log.println("Listener2 called " + listener2.called + " times");
146 
147         tRes.tested("addSubmitListener()", listener2.called == 1);
148         tRes.tested("removeSubmitListener()", listener1.called == 0);
149         tRes.tested("submit()", listener2.called > 0);
150         oObj.removeSubmitListener(listener2);
151     }
152 
153     /**
154     * Just removes one of submit listeners. <p>
155     * Status of this method test is defined in <code>
156     * submit </code> method test.
157     * The following method tests are to be completed successfully before :
158     * <ul>
159     *  <li> <code> removeSubmitListener </code> : to have listeners added</li>
160     * </ul>
161     */
_removeSubmitListener()162     public void _removeSubmitListener() {
163         requiredMethod("addSubmitListener()");
164         oObj.removeSubmitListener(listener1);
165     }
166 
167     /**
168     * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
169     * reset</code> call.
170     */
shortWait()171     private void shortWait() {
172         try {
173             Thread.sleep(200) ;
174         } catch (InterruptedException e) {
175             log.println("While waiting :" + e) ;
176         }
177     }
178 
179     /**
180     * Forces environment recreation.
181     */
after()182     protected void after() {
183         disposeEnvironment();
184     }
185 
186 }
187 
188