Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / frame / _XSynchronousFrameLoader.java
blobb56d9d89d20b531d970bcbbbcd3287cf9b0a8df0
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package ifc.frame;
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
24 import util.SOfficeFactory;
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.frame.XDesktop;
28 import com.sun.star.frame.XFrame;
29 import com.sun.star.frame.XSynchronousFrameLoader;
30 import com.sun.star.lang.XComponent;
31 import com.sun.star.uno.UnoRuntime;
32 import com.sun.star.util.URL;
33 import com.sun.star.util.XURLTransformer;
35 /**
36 * Testing <code>com.sun.star.frame.XSynchronousFrameLoader</code>
37 * interface methods :
38 * <ul>
39 * <li><code> load()</code></li>
40 * <li><code> cancel()</code></li>
41 * </ul> <p>
42 * This test needs the following object relations :
43 * <ul>
44 * <li> <code>'FrameLoader.URL'</code> (of type <code>String</code>):
45 * a url of component to be loaded </li>
46 * <li> <code>'FrameLoader.Frame'</code> <b>(optional)</b>
47 * (of type <code>com.sun.star.frame.XFrame</code>):
48 * a target frame where component to be loaded. If this
49 * relation is omitted then a text document created and its
50 * frame is used. </li>
51 * <ul> <p>
53 * @see com.sun.star.frame.XSynchronousFrameLoader
55 public class _XSynchronousFrameLoader extends MultiMethodTest {
57 public XSynchronousFrameLoader oObj = null; // oObj filled by MultiMethodTest
58 private XFrame frame = null ;
59 private XComponent frameSup = null ;
60 private PropertyValue[] descr = null;
62 /**
63 * Retrieves all relations. If optional relation
64 * <code>FrameLoader.Frame</code> not found
65 * creates a new document and obtains its frame for loading. <p>
67 * Also <code>MediaDescriptor</code> is created using
68 * URL from <code>FrameLoader.URL</code> relation.
70 * @throws StatusException If one of required relations not found.
72 @Override
73 public void before() {
74 String url = (String) tEnv.getObjRelation("FrameLoader.URL") ;
75 frame = (XFrame) tEnv.getObjRelation("FrameLoader.Frame") ;
77 if (url == null) {
78 throw new StatusException(Status.failed("Some relations not found")) ;
81 SOfficeFactory SOF = SOfficeFactory.getFactory(
82 tParam.getMSF() );
84 XURLTransformer xURLTrans = null;
86 // if frame is not contained in relations the writer frame will be used.
87 if (frame == null) {
88 try {
89 log.println( "creating a textdocument" );
90 frameSup = SOF.createTextDoc( null );
92 Object oDsk = tParam.getMSF().createInstance
93 ("com.sun.star.frame.Desktop") ;
94 XDesktop dsk = UnoRuntime.queryInterface
95 (XDesktop.class, oDsk) ;
96 frame = dsk.getCurrentFrame() ;
98 Object o = tParam.getMSF().createInstance
99 ("com.sun.star.util.URLTransformer") ;
100 xURLTrans = UnoRuntime.queryInterface
101 (XURLTransformer.class, o) ;
103 } catch ( com.sun.star.uno.Exception e ) {
104 // Some exception occurs.FAILED
105 e.printStackTrace( log );
106 throw new StatusException( "Couldn't create a document.", e );
110 URL[] urlS = new URL[1];
111 urlS[0] = new URL();
112 urlS[0].Complete = url;
113 if (xURLTrans == null)
114 throw new IllegalStateException("xURLTrans unexpected null");
115 boolean res = xURLTrans.parseStrict(urlS);
116 log.println("Parsing URL '" + url + "': " + res);
117 descr = new PropertyValue[1] ;
118 descr[0] = new PropertyValue();
119 descr[0].Name = "URL" ;
120 descr[0].Value = urlS[0] ;
125 * Tries to load component to a frame. <p>
126 * Has <b> OK </b> status if <code>true</code> is returned.
128 public void _load() {
129 boolean result = oObj.load(descr, frame) ;
131 tRes.tested("load()", result) ;
135 * Tries to load component to a frame in separate thread to
136 * avoid blocking of the current thread and immediately
137 * cancels loading. <p>
139 * Has <b> OK </b> status if <code>flase</code> is returned,
140 * i.e. loading was not completed.
142 public void _cancel() {
143 requiredMethod("load()") ;
145 final boolean[] result = new boolean[1] ;
147 (new Thread() {
148 @Override
149 public void run() {
150 result[0] = oObj.load(descr, frame);
152 }).start();
154 oObj.cancel() ;
156 waitForEventIdle();
158 tRes.tested("cancel()", !result[0]) ;
162 * Disposes document if it was created for frame supplying.
164 @Override
165 protected void after() {
166 if (frameSup != null) {
167 frameSup.dispose();