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