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