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.form.XReset;
29 import com.sun.star.form.XResetListener;
30 import com.sun.star.lang.EventObject;
31 
32 /**
33 * Testing <code>com.sun.star.form.XReset</code>
34 * interface methods :
35 * <ul>
36 *  <li><code> reset()</code></li>
37 *  <li><code> addResetListener()</code></li>
38 *  <li><code> removeResetListener()</code></li>
39 * </ul>
40 * Test is <b> NOT </b> multithread compilant. <p>
41 * @see com.sun.star.form.XReset
42 */
43 public class _XReset extends MultiMethodTest {
44 
45     public static XReset oObj = null;
46 
47     /**
48     * Indicates if listeners must approve restes requests or not.
49     */
50     protected boolean approve = true;
51     /**
52     * Array of two elements, each of them indicates <code>resetted</code>
53     * call of appropriate listener.
54     */
55       protected boolean resetted[] = new boolean[2];
56     /**
57     * Array of two elements, each of them indicates
58     * <code>approveReset</code> call of appropriate listener.
59     */
60     protected boolean approveReset[] = new boolean[2];
61 
62     /**
63     * The listener which sets flags (in array elements with index 0)
64     * on <code>resetted</code> and
65     * <code>approveReset</code> events. It approves reset request
66     * depending on <code>approve</code> field.
67     */
68     protected class MyResetListener implements XResetListener {
disposing( EventObject oEvent )69            public void disposing ( EventObject oEvent ) {}
approveReset( EventObject oEvent )70                public boolean approveReset ( EventObject oEvent ) {
71                    approveReset[0] = true;
72                    //cancel the reset action
73                    return approve;
74                    }
resetted( EventObject oEvent )75                public void resetted ( EventObject oEvent ) {
76                    resetted[0] = true;
77                    }
78       }
79 
80 
81     /**
82     * The listener which sets flags (in array elements with index 1)
83     * on <code>resetted</code> and
84     * <code>approveReset</code> events. It approves reset request
85     * depending on <code>approve</code> field.
86     */
87     protected class MyResetListener2 implements XResetListener {
disposing( EventObject oEvent )88            public void disposing ( EventObject oEvent ) {}
approveReset( EventObject oEvent )89                public boolean approveReset ( EventObject oEvent ) {
90                    approveReset[1] = true;
91                    //don't cancel the reset action
92                    return true;
93                    }
resetted( EventObject oEvent )94                public void resetted ( EventObject oEvent ) {
95                    resetted[1] = true;
96                    }
97     }
98 
99     /**
100     * Listener which is added in test
101     */
102     protected XResetListener listener1 = new MyResetListener();
103     /**
104     * Listener which is added in test
105     */
106     protected XResetListener listener2 = new MyResetListener2();
107 
108     /**
109     * Just adds two reset listeners. <p>
110     * Status for it is set later in <code>reset</code> method test.
111     */
_addResetListener()112     public void _addResetListener() {
113 
114          log.println("Testing addResetListener ...");
115          oObj.addResetListener( listener2 );
116          oObj.addResetListener( listener1 );
117 
118     } // finished _addResetListener()
119 
120     /**
121     * First calls <code>reset</code> method without approving
122     * the request, in this case only <code>approveReset</code>
123     * event must be called. Second calls <code>reset</code> with
124     * approving the request. In this case both listener's events
125     * must be called. <p>
126     * Has <b>OK</b> status for <code>reset</code> method if in
127     * the first case only <code>approveReset</code> method was
128     * called. <p>
129     * Has <b>OK</b> status for <code>addResetListener</code> method
130     * if in the second case both listener's methods were called.<p>
131     * The following method tests are to be completed successfully before :
132     * <ul>
133     *  <li> <code> addResetListener </code> : to have listeners added.</li>
134     * </ul>
135     */
_reset()136     public void _reset() {
137 
138          executeMethod("addResetListener()");
139          log.println("Testing reset() ...");
140          approve = false;
141          oObj.reset();
142          shortWait();
143          tRes.tested("reset()", (approveReset[0] && (! resetted[0])));
144          approve = true;
145          oObj.reset();
146          shortWait();
147          tRes.tested("addResetListener()", (approveReset[1] && resetted[1]));
148 
149     } // finished _reset
150 
151     /**
152     * Removes the first listener, clears it's call flags, and
153     * calls <code>reset</code> method.<p>
154     * Has <b> OK </b> status if no methods of the listener removed
155     * were called. <p>
156     * The following method tests are to be completed successfully before :
157     * <ul>
158     *  <li> <code> reset </code> : to test this method last. </li>
159     * </ul>
160     */
_removeResetListener()161     public void _removeResetListener() {
162          requiredMethod("reset()");
163          log.println("Testing removeResetListener ...");
164          approveReset[0] = resetted[0] = false;
165          oObj.removeResetListener(listener1);
166          oObj.reset();
167          shortWait();
168          tRes.tested("removeResetListener()", !approveReset[0] && !resetted[0]);
169          //removing the second listener here may avoid crashing the office
170 
171          return;
172 
173     } // finished _removeResetListener()
174 
175     /**
176     * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
177     * reset</code> call.
178     */
shortWait()179     private void shortWait() {
180         try {
181             Thread.sleep(500) ;
182         } catch (InterruptedException e) {
183             log.println("While waiting :" + e) ;
184         }
185     }
186 
187 
188 } // finished class _XRefresh
189 
190 
191