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.XSynchronousFrameLoader;
35cdf0e10cSrcweir import com.sun.star.lang.XComponent;
36cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir import com.sun.star.util.URL;
39cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir /**
42cdf0e10cSrcweir  * Testing <code>com.sun.star.frame.XSynchronousFrameLoader</code>
43cdf0e10cSrcweir  * interface methods :
44cdf0e10cSrcweir  * <ul>
45cdf0e10cSrcweir  *  <li><code> load()</code></li>
46cdf0e10cSrcweir  *  <li><code> cancel()</code></li>
47cdf0e10cSrcweir  * </ul> <p>
48cdf0e10cSrcweir  * This test needs the following object relations :
49cdf0e10cSrcweir  * <ul>
50cdf0e10cSrcweir  *  <li> <code>'FrameLoader.URL'</code> (of type <code>String</code>):
51cdf0e10cSrcweir  *   an url of component to be loaded </li>
52cdf0e10cSrcweir  *  <li> <code>'FrameLoader.Frame'</code> <b>(optional)</b>
53cdf0e10cSrcweir  *  (of type <code>com.sun.star.frame.XFrame</code>):
54cdf0e10cSrcweir  *   a target frame where component to be loaded. If this
55bb6af6bcSPedro Giffuni  *   relation is omitted then a text document created and its
56cdf0e10cSrcweir  *   frame is used. </li>
57cdf0e10cSrcweir  * <ul> <p>
58cdf0e10cSrcweir  *
59cdf0e10cSrcweir  * @see com.sun.star.frame.XSynchronousFrameLoader
60cdf0e10cSrcweir  */
61cdf0e10cSrcweir public class _XSynchronousFrameLoader extends MultiMethodTest {
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     public XSynchronousFrameLoader oObj = null; // oObj filled by MultiMethodTest
64cdf0e10cSrcweir     private String url = null ;
65cdf0e10cSrcweir     private XFrame frame = null ;
66cdf0e10cSrcweir     private XComponent frameSup = null ;
67cdf0e10cSrcweir     private PropertyValue[] descr = null;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     /**
70cdf0e10cSrcweir      * Retrieves all relations. If optional relation
71cdf0e10cSrcweir      * <code>FrameLoader.Frame</code> not found
72cdf0e10cSrcweir      * creates a new document and otains its frame for loading. <p>
73cdf0e10cSrcweir      *
74cdf0e10cSrcweir      * Also <code>MediaDescriptor</code> is created using
75cdf0e10cSrcweir      * URL from <code>FrameLoader.URL</code> relation.
76cdf0e10cSrcweir      *
77cdf0e10cSrcweir      * @throws StatusException If one of required relations not found.
78cdf0e10cSrcweir      */
before()79cdf0e10cSrcweir     public void before() {
80cdf0e10cSrcweir         url = (String) tEnv.getObjRelation("FrameLoader.URL") ;
81cdf0e10cSrcweir         frame = (XFrame) tEnv.getObjRelation("FrameLoader.Frame") ;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         if (url == null) {
84cdf0e10cSrcweir             throw new StatusException(Status.failed("Some relations not found")) ;
85cdf0e10cSrcweir         }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(
88cdf0e10cSrcweir                              (XMultiServiceFactory)tParam.getMSF() );
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         XURLTransformer xURLTrans = null;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         // if frame is not contained in relations the writer frmame will be used.
93cdf0e10cSrcweir         if (frame == null) {
94cdf0e10cSrcweir             try {
95cdf0e10cSrcweir                 log.println( "creating a textdocument" );
96cdf0e10cSrcweir                 frameSup = SOF.createTextDoc( null );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir                 Object oDsk = (
99cdf0e10cSrcweir                     (XMultiServiceFactory)tParam.getMSF()).createInstance
100cdf0e10cSrcweir                     ("com.sun.star.frame.Desktop") ;
101cdf0e10cSrcweir                 XDesktop dsk = (XDesktop) UnoRuntime.queryInterface
102cdf0e10cSrcweir                     (XDesktop.class, oDsk) ;
103cdf0e10cSrcweir                 frame = dsk.getCurrentFrame() ;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir                 Object o = (
106cdf0e10cSrcweir                     (XMultiServiceFactory)tParam.getMSF()).createInstance
107cdf0e10cSrcweir                     ("com.sun.star.util.URLTransformer") ;
108cdf0e10cSrcweir                 xURLTrans = (XURLTransformer) UnoRuntime.queryInterface
109cdf0e10cSrcweir                     (XURLTransformer.class, o) ;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             } catch ( com.sun.star.uno.Exception e ) {
112*30acf5e8Spfg                 // Some exception occured.FAILED
113cdf0e10cSrcweir                 e.printStackTrace( log );
114cdf0e10cSrcweir                 throw new StatusException( "Couldn't create a document.", e );
115cdf0e10cSrcweir             }
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         URL[] urlS = new URL[1];
119cdf0e10cSrcweir         urlS[0] = new URL();
120cdf0e10cSrcweir         urlS[0].Complete = url;
121cdf0e10cSrcweir         boolean res = xURLTrans.parseStrict(urlS);
122cdf0e10cSrcweir         log.println("Parsing URL '" + url + "': " + res);
123cdf0e10cSrcweir         descr = new PropertyValue[1] ;
124cdf0e10cSrcweir         descr[0] = new PropertyValue();
125cdf0e10cSrcweir         descr[0].Name = "URL" ;
126cdf0e10cSrcweir         descr[0].Value = urlS[0] ;
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir     /**
131cdf0e10cSrcweir      * Tries to load component to a frame. <p>
132cdf0e10cSrcweir      * Has <b> OK </b> status if <code>true</code> is returned.
133cdf0e10cSrcweir      */
_load()134cdf0e10cSrcweir     public void _load() {
135cdf0e10cSrcweir         boolean result = oObj.load(descr, frame) ;
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         tRes.tested("load()", result) ;
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     /**
141cdf0e10cSrcweir      * Tries to load component to a frame in separate thread to
142bb6af6bcSPedro Giffuni      * avoid blocking of the current thread and immediately
143cdf0e10cSrcweir      * cancels loading. <p>
144cdf0e10cSrcweir      *
145cdf0e10cSrcweir      * Has <b> OK </b> status if <code>flase</code> is returned,
146cdf0e10cSrcweir      * i.e. loading was not completed.
147cdf0e10cSrcweir      */
_cancel()148cdf0e10cSrcweir     public void _cancel() {
149cdf0e10cSrcweir         requiredMethod("load()") ;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         final boolean[] result = new boolean[1] ;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         (new Thread() {
154cdf0e10cSrcweir             public void run() {
155cdf0e10cSrcweir                 result[0] = oObj.load(descr, frame);
156cdf0e10cSrcweir             }
157cdf0e10cSrcweir         }).start();
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         oObj.cancel() ;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         try {
162cdf0e10cSrcweir             Thread.sleep(1000);
163cdf0e10cSrcweir         } catch (InterruptedException ex) {}
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 
166cdf0e10cSrcweir         tRes.tested("cancel()", !result[0]) ;
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     /**
170cdf0e10cSrcweir      * Disposes document if it was created for frame supplying.
171cdf0e10cSrcweir      */
after()172cdf0e10cSrcweir     protected void after() {
173cdf0e10cSrcweir         if (frameSup != null) {
174cdf0e10cSrcweir             frameSup.dispose();
175cdf0e10cSrcweir         }
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir }
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 
180