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