1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.frame;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir import util.SOfficeFactory;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
36*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
37*cdf0e10cSrcweir import com.sun.star.frame.XFrame;
38*cdf0e10cSrcweir import com.sun.star.frame.XSynchronousFrameLoader;
39*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
40*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir import com.sun.star.util.URL;
43*cdf0e10cSrcweir import com.sun.star.util.XURLTransformer;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /**
46*cdf0e10cSrcweir  * Testing <code>com.sun.star.frame.XSynchronousFrameLoader</code>
47*cdf0e10cSrcweir  * interface methods :
48*cdf0e10cSrcweir  * <ul>
49*cdf0e10cSrcweir  *  <li><code> load()</code></li>
50*cdf0e10cSrcweir  *  <li><code> cancel()</code></li>
51*cdf0e10cSrcweir  * </ul> <p>
52*cdf0e10cSrcweir  * This test needs the following object relations :
53*cdf0e10cSrcweir  * <ul>
54*cdf0e10cSrcweir  *  <li> <code>'FrameLoader.URL'</code> (of type <code>String</code>):
55*cdf0e10cSrcweir  *   an url of component to be loaded </li>
56*cdf0e10cSrcweir  *  <li> <code>'FrameLoader.Frame'</code> <b>(optional)</b>
57*cdf0e10cSrcweir  *  (of type <code>com.sun.star.frame.XFrame</code>):
58*cdf0e10cSrcweir  *   a target frame where component to be loaded. If this
59*cdf0e10cSrcweir  *   relation is ommited then a text document created and its
60*cdf0e10cSrcweir  *   frame is used. </li>
61*cdf0e10cSrcweir  * <ul> <p>
62*cdf0e10cSrcweir  *
63*cdf0e10cSrcweir  * @see com.sun.star.frame.XSynchronousFrameLoader
64*cdf0e10cSrcweir  */
65*cdf0e10cSrcweir public class _XSynchronousFrameLoader extends MultiMethodTest {
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     public XSynchronousFrameLoader oObj = null; // oObj filled by MultiMethodTest
68*cdf0e10cSrcweir     private String url = null ;
69*cdf0e10cSrcweir     private XFrame frame = null ;
70*cdf0e10cSrcweir     private XComponent frameSup = null ;
71*cdf0e10cSrcweir     private PropertyValue[] descr = null;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir     /**
74*cdf0e10cSrcweir      * Retrieves all relations. If optional relation
75*cdf0e10cSrcweir      * <code>FrameLoader.Frame</code> not found
76*cdf0e10cSrcweir      * creates a new document and otains its frame for loading. <p>
77*cdf0e10cSrcweir      *
78*cdf0e10cSrcweir      * Also <code>MediaDescriptor</code> is created using
79*cdf0e10cSrcweir      * URL from <code>FrameLoader.URL</code> relation.
80*cdf0e10cSrcweir      *
81*cdf0e10cSrcweir      * @throws StatusException If one of required relations not found.
82*cdf0e10cSrcweir      */
83*cdf0e10cSrcweir     public void before() {
84*cdf0e10cSrcweir         url = (String) tEnv.getObjRelation("FrameLoader.URL") ;
85*cdf0e10cSrcweir         frame = (XFrame) tEnv.getObjRelation("FrameLoader.Frame") ;
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir         if (url == null) {
88*cdf0e10cSrcweir             throw new StatusException(Status.failed("Some relations not found")) ;
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         SOfficeFactory SOF = SOfficeFactory.getFactory(
92*cdf0e10cSrcweir                              (XMultiServiceFactory)tParam.getMSF() );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         XURLTransformer xURLTrans = null;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         // if frame is not contained in relations the writer frmame will be used.
97*cdf0e10cSrcweir         if (frame == null) {
98*cdf0e10cSrcweir             try {
99*cdf0e10cSrcweir                 log.println( "creating a textdocument" );
100*cdf0e10cSrcweir                 frameSup = SOF.createTextDoc( null );
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir                 Object oDsk = (
103*cdf0e10cSrcweir                     (XMultiServiceFactory)tParam.getMSF()).createInstance
104*cdf0e10cSrcweir                     ("com.sun.star.frame.Desktop") ;
105*cdf0e10cSrcweir                 XDesktop dsk = (XDesktop) UnoRuntime.queryInterface
106*cdf0e10cSrcweir                     (XDesktop.class, oDsk) ;
107*cdf0e10cSrcweir                 frame = dsk.getCurrentFrame() ;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir                 Object o = (
110*cdf0e10cSrcweir                     (XMultiServiceFactory)tParam.getMSF()).createInstance
111*cdf0e10cSrcweir                     ("com.sun.star.util.URLTransformer") ;
112*cdf0e10cSrcweir                 xURLTrans = (XURLTransformer) UnoRuntime.queryInterface
113*cdf0e10cSrcweir                     (XURLTransformer.class, o) ;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir             } catch ( com.sun.star.uno.Exception e ) {
116*cdf0e10cSrcweir                 // Some exception occures.FAILED
117*cdf0e10cSrcweir                 e.printStackTrace( log );
118*cdf0e10cSrcweir                 throw new StatusException( "Couldn't create a document.", e );
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir         }
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir         URL[] urlS = new URL[1];
123*cdf0e10cSrcweir         urlS[0] = new URL();
124*cdf0e10cSrcweir         urlS[0].Complete = url;
125*cdf0e10cSrcweir         boolean res = xURLTrans.parseStrict(urlS);
126*cdf0e10cSrcweir         log.println("Parsing URL '" + url + "': " + res);
127*cdf0e10cSrcweir         descr = new PropertyValue[1] ;
128*cdf0e10cSrcweir         descr[0] = new PropertyValue();
129*cdf0e10cSrcweir         descr[0].Name = "URL" ;
130*cdf0e10cSrcweir         descr[0].Value = urlS[0] ;
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     /**
135*cdf0e10cSrcweir      * Tries to load component to a frame. <p>
136*cdf0e10cSrcweir      * Has <b> OK </b> status if <code>true</code> is returned.
137*cdf0e10cSrcweir      */
138*cdf0e10cSrcweir     public void _load() {
139*cdf0e10cSrcweir         boolean result = oObj.load(descr, frame) ;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         tRes.tested("load()", result) ;
142*cdf0e10cSrcweir     }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir     /**
145*cdf0e10cSrcweir      * Tries to load component to a frame in separate thread to
146*cdf0e10cSrcweir      * avoid blocking of the current thread and imediately
147*cdf0e10cSrcweir      * cancels loading. <p>
148*cdf0e10cSrcweir      *
149*cdf0e10cSrcweir      * Has <b> OK </b> status if <code>flase</code> is returned,
150*cdf0e10cSrcweir      * i.e. loading was not completed.
151*cdf0e10cSrcweir      */
152*cdf0e10cSrcweir     public void _cancel() {
153*cdf0e10cSrcweir         requiredMethod("load()") ;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         final boolean[] result = new boolean[1] ;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         (new Thread() {
158*cdf0e10cSrcweir             public void run() {
159*cdf0e10cSrcweir                 result[0] = oObj.load(descr, frame);
160*cdf0e10cSrcweir             }
161*cdf0e10cSrcweir         }).start();
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir         oObj.cancel() ;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir         try {
166*cdf0e10cSrcweir             Thread.sleep(1000);
167*cdf0e10cSrcweir         } catch (InterruptedException ex) {}
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir         tRes.tested("cancel()", !result[0]) ;
171*cdf0e10cSrcweir     }
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     /**
174*cdf0e10cSrcweir      * Disposes document if it was created for frame supplying.
175*cdf0e10cSrcweir      */
176*cdf0e10cSrcweir     protected void after() {
177*cdf0e10cSrcweir         if (frameSup != null) {
178*cdf0e10cSrcweir             frameSup.dispose();
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir     }
181*cdf0e10cSrcweir }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 
184