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