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 
31 import lib.MultiMethodTest;
32 
33 import com.sun.star.form.XLoadable;
34 
35 /**
36 * Testing <code>com.sun.star.form.XLoadable</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> load()</code></li>
40 *  <li><code> unload()</code></li>
41 *  <li><code> reload()</code></li>
42 *  <li><code> isLoaded()</code></li>
43 *  <li><code> addLoadListener()</code></li>
44 *  <li><code> removeLoadListener()</code></li>
45 * </ul> <p>
46 * Test is <b> NOT </b> multithread compilant. <p>
47 * @see com.sun.star.form.XLoadable
48 */
49 public class _XLoadable extends MultiMethodTest {
50 
51     public XLoadable oObj = null;
52 
53     /**
54     * Listener implementation which sets flags on appropriate method calls
55     */
56     protected class TestLoadListener implements com.sun.star.form.XLoadListener {
57         public boolean disposingCalled = false ;
58         public boolean loadedCalled = false ;
59         public boolean reloadedCalled = false ;
60         public boolean reloadingCalled = false ;
61         public boolean unloadedCalled = false ;
62         public boolean unloadingCalled = false ;
63         private java.io.PrintWriter log = null ;
64 
65         public TestLoadListener(java.io.PrintWriter log) {
66             this.log = log ;
67         }
68 
69         public void disposing(com.sun.star.lang.EventObject e) {
70             disposingCalled = true ;
71             log.println(" disposing was called.") ;
72         }
73 
74         public void loaded(com.sun.star.lang.EventObject e) {
75             loadedCalled = true ;
76             log.println(" loaded was called.") ;
77         }
78 
79         public void reloaded(com.sun.star.lang.EventObject e) {
80             reloadedCalled = true ;
81             log.println(" reloaded was called.") ;
82         }
83 
84         public void reloading(com.sun.star.lang.EventObject e) {
85             reloadingCalled = true ;
86             log.println(" reloading was called.") ;
87         }
88 
89         public void unloaded(com.sun.star.lang.EventObject e) {
90             unloadedCalled = true ;
91             log.println(" unloaded was called.") ;
92         }
93 
94         public void unloading(com.sun.star.lang.EventObject e) {
95             unloadingCalled = true ;
96             log.println(" unloading was called.") ;
97         }
98     }
99 
100     TestLoadListener loadListener = null ;
101 
102     /**
103     * Creates new listener.
104     */
105     public void before() {
106         loadListener = new TestLoadListener(log) ;
107     }
108 
109     /**
110      * Waits for 0.1 second. Used to get time for load completion.
111      */
112     private void shortWait() {
113         try {
114             Thread.sleep(100);
115         } catch (InterruptedException e) {}
116     }
117 
118     /**
119     * Loads the form. <p>
120     * Has <b> OK </b> status if <code>isLoaded()</code> returns
121     * <code>true</code> and listener method <code>loaded()</code>
122     * is called.
123     * The following method tests are to be completed successfully before :
124     * <ul>
125     *  <li> <code> isLoaded() </code> : to be sure form is not loaded </li>
126     *  <li> <code> addLoadListener() </code> : to check if this listener method
127     *  is called. </li>
128     * </ul>
129     */
130     public void _load() {
131         requiredMethod("isLoaded()") ;
132         requiredMethod("addLoadListener()") ;
133 
134         boolean result = true ;
135         oObj.load() ;
136 
137         shortWait() ;
138         result = oObj.isLoaded() && loadListener.loadedCalled ;
139 
140         tRes.tested("load()", result) ;
141     }
142 
143     /**
144     * Unloads the form. <p>
145     * Has <b> OK </b> status if <code>isLoaded()</code> returns
146     * <code>false</code> and listener method <code>unloaded()</code>
147     * is called.
148     * The following method tests are to be completed successfully before :
149     * <ul>
150     *  <li> <code> reload() </code> : to be sure the form is loaded </li>
151     *  <li> <code> addLoadListener() </code> : to check if this listener method
152     *  is called. </li>
153     * </ul>
154     */
155     public void _unload() {
156         requiredMethod("reload()") ;
157         requiredMethod("addLoadListener()") ;
158 
159         boolean result = true ;
160         oObj.unload() ;
161 
162         shortWait() ;
163         result = !oObj.isLoaded() && loadListener.unloadedCalled ;
164 
165         tRes.tested("unload()", result) ;
166     }
167 
168     /**
169     * Reloads the form. <p>
170     * Has <b> OK </b> status if <code>isLoaded()</code> returns
171     * <code>true</code> and listener method <code>reloaded()</code>
172     * is called.
173     * The following method tests are to be completed successfully before :
174     * <ul>
175     *  <li> <code> load() </code> : to be sure form is loaded </li>
176     *  <li> <code> addLoadListener() </code> : to check if this listener method
177     *  is called. </li>
178     * </ul>
179     */
180     public void _reload() {
181         requiredMethod("load()") ;
182         requiredMethod("addLoadListener()") ;
183 
184         boolean result = true ;
185         oObj.reload() ;
186 
187         shortWait() ;
188         result = oObj.isLoaded() && loadListener.reloadedCalled;
189 
190         tRes.tested("reload()", result) ;
191     }
192 
193     /**
194     * Checks if the component is already loaded. If yes it unloads
195     * it <p>
196     * Has <b> OK </b> status if finally <code>isLoaded()</code> method
197     * returns <code>false</code>.
198     */
199     public void _isLoaded() {
200 
201         boolean isLoaded = oObj.isLoaded() ;
202         if (isLoaded) oObj.unload();
203         isLoaded = oObj.isLoaded() ;
204 
205         tRes.tested("isLoaded()", !isLoaded) ;
206     }
207 
208     /**
209     * Adds a listener. If its methods are called or not is checked
210     * in other object methods. <p>
211     * Has <b> OK </b> status if no runtime exceptions occured.
212     */
213     public void _addLoadListener() {
214 
215         boolean result = true ;
216         oObj.addLoadListener(loadListener) ;
217 
218         tRes.tested("addLoadListener()", result) ;
219     }
220 
221     /**
222     * Removes the listener added before. <p>
223     * Has <b> OK </b> status if after <code>load()</code> call no
224     * listener methods were called. <p>
225     * The following method tests are to be completed successfully before :
226     * <ul>
227     *  <li> <code> unload() </code> : to make this test run finally.</li>
228     * </ul>
229     */
230     public void _removeLoadListener() {
231         requiredMethod("unload()") ;
232 
233         boolean result = true ;
234         oObj.removeLoadListener(loadListener) ;
235         loadListener.loadedCalled = false ;
236         oObj.load();
237 
238         result = ! loadListener.loadedCalled ;
239 
240         tRes.tested("removeLoadListener()", result) ;
241     }
242 
243     protected void after() {
244         disposeEnvironment();
245     }
246 }
247 
248