1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski package ifc.frame; 25*b1cdbd2cSJim Jagielski 26*b1cdbd2cSJim Jagielski import lib.MultiMethodTest; 27*b1cdbd2cSJim Jagielski import lib.Status; 28*b1cdbd2cSJim Jagielski import lib.StatusException; 29*b1cdbd2cSJim Jagielski import util.SOfficeFactory; 30*b1cdbd2cSJim Jagielski 31*b1cdbd2cSJim Jagielski import com.sun.star.beans.PropertyValue; 32*b1cdbd2cSJim Jagielski import com.sun.star.frame.XDesktop; 33*b1cdbd2cSJim Jagielski import com.sun.star.frame.XFrame; 34*b1cdbd2cSJim Jagielski import com.sun.star.frame.XSynchronousFrameLoader; 35*b1cdbd2cSJim Jagielski import com.sun.star.lang.XComponent; 36*b1cdbd2cSJim Jagielski import com.sun.star.lang.XMultiServiceFactory; 37*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime; 38*b1cdbd2cSJim Jagielski import com.sun.star.util.URL; 39*b1cdbd2cSJim Jagielski import com.sun.star.util.XURLTransformer; 40*b1cdbd2cSJim Jagielski 41*b1cdbd2cSJim Jagielski /** 42*b1cdbd2cSJim Jagielski * Testing <code>com.sun.star.frame.XSynchronousFrameLoader</code> 43*b1cdbd2cSJim Jagielski * interface methods : 44*b1cdbd2cSJim Jagielski * <ul> 45*b1cdbd2cSJim Jagielski * <li><code> load()</code></li> 46*b1cdbd2cSJim Jagielski * <li><code> cancel()</code></li> 47*b1cdbd2cSJim Jagielski * </ul> <p> 48*b1cdbd2cSJim Jagielski * This test needs the following object relations : 49*b1cdbd2cSJim Jagielski * <ul> 50*b1cdbd2cSJim Jagielski * <li> <code>'FrameLoader.URL'</code> (of type <code>String</code>): 51*b1cdbd2cSJim Jagielski * an url of component to be loaded </li> 52*b1cdbd2cSJim Jagielski * <li> <code>'FrameLoader.Frame'</code> <b>(optional)</b> 53*b1cdbd2cSJim Jagielski * (of type <code>com.sun.star.frame.XFrame</code>): 54*b1cdbd2cSJim Jagielski * a target frame where component to be loaded. If this 55*b1cdbd2cSJim Jagielski * relation is ommited then a text document created and its 56*b1cdbd2cSJim Jagielski * frame is used. </li> 57*b1cdbd2cSJim Jagielski * <ul> <p> 58*b1cdbd2cSJim Jagielski * 59*b1cdbd2cSJim Jagielski * @see com.sun.star.frame.XSynchronousFrameLoader 60*b1cdbd2cSJim Jagielski */ 61*b1cdbd2cSJim Jagielski public class _XSynchronousFrameLoader extends MultiMethodTest { 62*b1cdbd2cSJim Jagielski 63*b1cdbd2cSJim Jagielski public XSynchronousFrameLoader oObj = null; // oObj filled by MultiMethodTest 64*b1cdbd2cSJim Jagielski private String url = null ; 65*b1cdbd2cSJim Jagielski private XFrame frame = null ; 66*b1cdbd2cSJim Jagielski private XComponent frameSup = null ; 67*b1cdbd2cSJim Jagielski private PropertyValue[] descr = null; 68*b1cdbd2cSJim Jagielski 69*b1cdbd2cSJim Jagielski /** 70*b1cdbd2cSJim Jagielski * Retrieves all relations. If optional relation 71*b1cdbd2cSJim Jagielski * <code>FrameLoader.Frame</code> not found 72*b1cdbd2cSJim Jagielski * creates a new document and otains its frame for loading. <p> 73*b1cdbd2cSJim Jagielski * 74*b1cdbd2cSJim Jagielski * Also <code>MediaDescriptor</code> is created using 75*b1cdbd2cSJim Jagielski * URL from <code>FrameLoader.URL</code> relation. 76*b1cdbd2cSJim Jagielski * 77*b1cdbd2cSJim Jagielski * @throws StatusException If one of required relations not found. 78*b1cdbd2cSJim Jagielski */ before()79*b1cdbd2cSJim Jagielski public void before() { 80*b1cdbd2cSJim Jagielski url = (String) tEnv.getObjRelation("FrameLoader.URL") ; 81*b1cdbd2cSJim Jagielski frame = (XFrame) tEnv.getObjRelation("FrameLoader.Frame") ; 82*b1cdbd2cSJim Jagielski 83*b1cdbd2cSJim Jagielski if (url == null) { 84*b1cdbd2cSJim Jagielski throw new StatusException(Status.failed("Some relations not found")) ; 85*b1cdbd2cSJim Jagielski } 86*b1cdbd2cSJim Jagielski 87*b1cdbd2cSJim Jagielski SOfficeFactory SOF = SOfficeFactory.getFactory( 88*b1cdbd2cSJim Jagielski (XMultiServiceFactory)tParam.getMSF() ); 89*b1cdbd2cSJim Jagielski 90*b1cdbd2cSJim Jagielski XURLTransformer xURLTrans = null; 91*b1cdbd2cSJim Jagielski 92*b1cdbd2cSJim Jagielski // if frame is not contained in relations the writer frmame will be used. 93*b1cdbd2cSJim Jagielski if (frame == null) { 94*b1cdbd2cSJim Jagielski try { 95*b1cdbd2cSJim Jagielski log.println( "creating a textdocument" ); 96*b1cdbd2cSJim Jagielski frameSup = SOF.createTextDoc( null ); 97*b1cdbd2cSJim Jagielski 98*b1cdbd2cSJim Jagielski Object oDsk = ( 99*b1cdbd2cSJim Jagielski (XMultiServiceFactory)tParam.getMSF()).createInstance 100*b1cdbd2cSJim Jagielski ("com.sun.star.frame.Desktop") ; 101*b1cdbd2cSJim Jagielski XDesktop dsk = (XDesktop) UnoRuntime.queryInterface 102*b1cdbd2cSJim Jagielski (XDesktop.class, oDsk) ; 103*b1cdbd2cSJim Jagielski frame = dsk.getCurrentFrame() ; 104*b1cdbd2cSJim Jagielski 105*b1cdbd2cSJim Jagielski Object o = ( 106*b1cdbd2cSJim Jagielski (XMultiServiceFactory)tParam.getMSF()).createInstance 107*b1cdbd2cSJim Jagielski ("com.sun.star.util.URLTransformer") ; 108*b1cdbd2cSJim Jagielski xURLTrans = (XURLTransformer) UnoRuntime.queryInterface 109*b1cdbd2cSJim Jagielski (XURLTransformer.class, o) ; 110*b1cdbd2cSJim Jagielski 111*b1cdbd2cSJim Jagielski } catch ( com.sun.star.uno.Exception e ) { 112*b1cdbd2cSJim Jagielski // Some exception occures.FAILED 113*b1cdbd2cSJim Jagielski e.printStackTrace( log ); 114*b1cdbd2cSJim Jagielski throw new StatusException( "Couldn't create a document.", e ); 115*b1cdbd2cSJim Jagielski } 116*b1cdbd2cSJim Jagielski } 117*b1cdbd2cSJim Jagielski 118*b1cdbd2cSJim Jagielski URL[] urlS = new URL[1]; 119*b1cdbd2cSJim Jagielski urlS[0] = new URL(); 120*b1cdbd2cSJim Jagielski urlS[0].Complete = url; 121*b1cdbd2cSJim Jagielski boolean res = xURLTrans.parseStrict(urlS); 122*b1cdbd2cSJim Jagielski log.println("Parsing URL '" + url + "': " + res); 123*b1cdbd2cSJim Jagielski descr = new PropertyValue[1] ; 124*b1cdbd2cSJim Jagielski descr[0] = new PropertyValue(); 125*b1cdbd2cSJim Jagielski descr[0].Name = "URL" ; 126*b1cdbd2cSJim Jagielski descr[0].Value = urlS[0] ; 127*b1cdbd2cSJim Jagielski } 128*b1cdbd2cSJim Jagielski 129*b1cdbd2cSJim Jagielski 130*b1cdbd2cSJim Jagielski /** 131*b1cdbd2cSJim Jagielski * Tries to load component to a frame. <p> 132*b1cdbd2cSJim Jagielski * Has <b> OK </b> status if <code>true</code> is returned. 133*b1cdbd2cSJim Jagielski */ _load()134*b1cdbd2cSJim Jagielski public void _load() { 135*b1cdbd2cSJim Jagielski boolean result = oObj.load(descr, frame) ; 136*b1cdbd2cSJim Jagielski 137*b1cdbd2cSJim Jagielski tRes.tested("load()", result) ; 138*b1cdbd2cSJim Jagielski } 139*b1cdbd2cSJim Jagielski 140*b1cdbd2cSJim Jagielski /** 141*b1cdbd2cSJim Jagielski * Tries to load component to a frame in separate thread to 142*b1cdbd2cSJim Jagielski * avoid blocking of the current thread and imediately 143*b1cdbd2cSJim Jagielski * cancels loading. <p> 144*b1cdbd2cSJim Jagielski * 145*b1cdbd2cSJim Jagielski * Has <b> OK </b> status if <code>flase</code> is returned, 146*b1cdbd2cSJim Jagielski * i.e. loading was not completed. 147*b1cdbd2cSJim Jagielski */ _cancel()148*b1cdbd2cSJim Jagielski public void _cancel() { 149*b1cdbd2cSJim Jagielski requiredMethod("load()") ; 150*b1cdbd2cSJim Jagielski 151*b1cdbd2cSJim Jagielski final boolean[] result = new boolean[1] ; 152*b1cdbd2cSJim Jagielski 153*b1cdbd2cSJim Jagielski (new Thread() { 154*b1cdbd2cSJim Jagielski public void run() { 155*b1cdbd2cSJim Jagielski result[0] = oObj.load(descr, frame); 156*b1cdbd2cSJim Jagielski } 157*b1cdbd2cSJim Jagielski }).start(); 158*b1cdbd2cSJim Jagielski 159*b1cdbd2cSJim Jagielski oObj.cancel() ; 160*b1cdbd2cSJim Jagielski 161*b1cdbd2cSJim Jagielski try { 162*b1cdbd2cSJim Jagielski Thread.sleep(1000); 163*b1cdbd2cSJim Jagielski } catch (InterruptedException ex) {} 164*b1cdbd2cSJim Jagielski 165*b1cdbd2cSJim Jagielski 166*b1cdbd2cSJim Jagielski tRes.tested("cancel()", !result[0]) ; 167*b1cdbd2cSJim Jagielski } 168*b1cdbd2cSJim Jagielski 169*b1cdbd2cSJim Jagielski /** 170*b1cdbd2cSJim Jagielski * Disposes document if it was created for frame supplying. 171*b1cdbd2cSJim Jagielski */ after()172*b1cdbd2cSJim Jagielski protected void after() { 173*b1cdbd2cSJim Jagielski if (frameSup != null) { 174*b1cdbd2cSJim Jagielski frameSup.dispose(); 175*b1cdbd2cSJim Jagielski } 176*b1cdbd2cSJim Jagielski } 177*b1cdbd2cSJim Jagielski } 178*b1cdbd2cSJim Jagielski 179*b1cdbd2cSJim Jagielski 180