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.frame;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 import util.SOfficeFactory;
30 
31 import com.sun.star.beans.PropertyValue;
32 import com.sun.star.frame.XDesktop;
33 import com.sun.star.frame.XFrame;
34 import com.sun.star.frame.XFrameLoader;
35 import com.sun.star.frame.XLoadEventListener;
36 import com.sun.star.lang.EventObject;
37 import com.sun.star.lang.XComponent;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.uno.UnoRuntime;
40 
41 
42 /**
43 * Testing <code>com.sun.star.frame.XFrameLoader</code>
44 * interface methods :
45 * <ul>
46 *  <li><code> load()</code></li>
47 *  <li><code> cancel()</code></li>
48 * </ul> <p>
49 * This test needs the following object relations :
50 * <ul>
51 *  <li> <code>'FrameLoader.URL'</code> (of type <code>String</code>):
52 *   an url or component to be loaded </li>
53 *  <li> <code>'FrameLoader.Frame'</code> <b>(optional)</b>
54 *  (of type <code>com.sun.star.frame.XFrame</code>):
55 *   a target frame where component to be loaded. If this
56 *   relation is omitted then a text document created and its
57 *   frame is used. </li>
58 *  <li> <code>'FrameLoader.args'</code> <b>(optional)</b>
59 *   (of type <code>Object[]</code>):
60 *   necessary arguuments for loading  a component. If omitted
61 *   then zero length array is passed as parameter</li>
62 * <ul> <p>
63 * Test is <b> NOT </b> multithread compilant. <p>
64 * @see com.sun.star.frame.XFrameLoader
65 */
66 public class _XFrameLoader extends MultiMethodTest {
67 
68     public XFrameLoader oObj = null; // oObj filled by MultiMethodTest
69     private String url = null ;
70     private XFrame frame = null ;
71     private PropertyValue[] args = new PropertyValue[0] ;
72 
73     /**
74     * Implemetation of load listener which geristers all it's calls.
75     */
76     protected class TestListener implements XLoadEventListener {
77         public boolean finished = false ;
78         public boolean cancelled = false ;
79 
loadFinished(XFrameLoader l)80         public void loadFinished(XFrameLoader l) {
81             finished = true ;
82         }
loadCancelled(XFrameLoader l)83         public void loadCancelled(XFrameLoader l) {
84             cancelled = true ;
85         }
disposing(EventObject e)86         public void disposing(EventObject e) {}
87     }
88 
89     TestListener listener = new TestListener() ;
90     XComponent frameSup = null ;
91 
92     /**
93     * Retrieves all relations. If optional  ones are not found
94     * creates their default values. <p>
95     * @throws StatusException If one of required relations not found.
96     */
before()97     public void before() {
98         url = (String) tEnv.getObjRelation("FrameLoader.URL") ;
99         frame = (XFrame) tEnv.getObjRelation("FrameLoader.Frame") ;
100 
101         if (frame == null) {
102             SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
103 
104             try {
105                 log.println( "creating a textdocument" );
106                 frameSup = SOF.createTextDoc( null );
107 
108                 Object oDsk = ((XMultiServiceFactory)tParam.getMSF())
109                         .createInstance("com.sun.star.frame.Desktop") ;
110                 XDesktop dsk = (XDesktop)
111                     UnoRuntime.queryInterface(XDesktop.class, oDsk) ;
112 
113                 shortWait() ;
114                 frame = dsk.getCurrentFrame() ;
115             } catch ( com.sun.star.uno.Exception e ) {
116                 // Some exception occured.FAILED
117                 e.printStackTrace( log );
118                 throw new StatusException( "Couldn't create a frame.", e );
119             }
120         }
121 
122         Object args = tEnv.getObjRelation("FrameLoader.args") ;
123         if (args != null) this.args = (PropertyValue[]) args ;
124 
125         if (url == null /*|| frame == null*/) {
126             throw new StatusException
127                 (Status.failed("Some relations not found")) ;
128         }
129     }
130 
131     private boolean loaded = false ;
132 
133     /**
134     * Firts <code>cancel</code> method test is called.
135     * If in that test loaing process was interrupted by
136     * <code>cancel</code> call then <code>load</code> test
137     * executes. It loads a component, waits some moment to
138     * listener have a chance to be called  and then checks
139     * if the load listener was called. <p>
140     * Has <b>OK</b> status if <code>cancel</code> method test
141     * didn't interrupt loading and it was successful, or
142     * if in this method it loads successful and listener's
143     * <code>finished</code> method was called.
144     * The following method tests are to be executed before :
145     * <ul>
146     *  <li> <code> cancel() </code> </li>
147     * </ul>
148     */
_load()149     public void _load() {
150         executeMethod("cancel()") ;
151 
152         if (!loaded) {
153             oObj.load(frame, url, args, listener) ;
154 
155             shortWait();
156 
157             loaded = listener.finished ;
158         }
159 
160         tRes.tested("load()", loaded) ;
161     }
162 
163     /**
164     * Starts to load a component and then immediately tries to
165     * cancel the process. <p>
166     * Has <b>OK</b> status if the process was cancelled or
167     * finished (appropriate listener methods were called).
168     */
_cancel()169     public void _cancel() {
170         boolean result = true ;
171 
172         oObj.load(frame, url, args, listener) ;
173         oObj.cancel() ;
174 
175         shortWait();
176 
177         if (listener.cancelled) {
178             log.println("Loading was canceled.") ;
179         }
180         if (listener.finished) {
181             log.println("Loading was finished.") ;
182             loaded = true ;
183         }
184         if (!listener.cancelled && !listener.finished) {
185             log.println("Loading was not canceled and not finished") ;
186             result = false ;
187         }
188 
189         tRes.tested("cancel()", result) ;
190     }
191 
after()192     public void after() {
193         if (frameSup != null) frameSup.dispose() ;
194         frame.dispose();
195     }
196 
shortWait()197     private void shortWait() {
198         try {
199             Thread.sleep(5000);
200         }
201         catch (InterruptedException ex) {
202         }
203 
204     }
205 }
206 
207