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.XFrameLoader;
39 import com.sun.star.frame.XLoadEventListener;
40 import com.sun.star.lang.EventObject;
41 import com.sun.star.lang.XComponent;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.uno.UnoRuntime;
44 
45 
46 /**
47 * Testing <code>com.sun.star.frame.XFrameLoader</code>
48 * interface methods :
49 * <ul>
50 *  <li><code> load()</code></li>
51 *  <li><code> cancel()</code></li>
52 * </ul> <p>
53 * This test needs the following object relations :
54 * <ul>
55 *  <li> <code>'FrameLoader.URL'</code> (of type <code>String</code>):
56 *   an url or component to be loaded </li>
57 *  <li> <code>'FrameLoader.Frame'</code> <b>(optional)</b>
58 *  (of type <code>com.sun.star.frame.XFrame</code>):
59 *   a target frame where component to be loaded. If this
60 *   relation is ommited then a text document created and its
61 *   frame is used. </li>
62 *  <li> <code>'FrameLoader.args'</code> <b>(optional)</b>
63 *   (of type <code>Object[]</code>):
64 *   necessary arguuments for loading  a component. If ommited
65 *   then zero length array is passed as parameter</li>
66 * <ul> <p>
67 * Test is <b> NOT </b> multithread compilant. <p>
68 * @see com.sun.star.frame.XFrameLoader
69 */
70 public class _XFrameLoader extends MultiMethodTest {
71 
72     public XFrameLoader oObj = null; // oObj filled by MultiMethodTest
73     private String url = null ;
74     private XFrame frame = null ;
75     private PropertyValue[] args = new PropertyValue[0] ;
76 
77     /**
78     * Implemetation of load listener which geristers all it's calls.
79     */
80     protected class TestListener implements XLoadEventListener {
81         public boolean finished = false ;
82         public boolean cancelled = false ;
83 
84         public void loadFinished(XFrameLoader l) {
85             finished = true ;
86         }
87         public void loadCancelled(XFrameLoader l) {
88             cancelled = true ;
89         }
90         public void disposing(EventObject e) {}
91     }
92 
93     TestListener listener = new TestListener() ;
94     XComponent frameSup = null ;
95 
96     /**
97     * Retrieves all relations. If optional  ones are not found
98     * creates their default values. <p>
99     * @throws StatusException If one of required relations not found.
100     */
101     public void before() {
102         url = (String) tEnv.getObjRelation("FrameLoader.URL") ;
103         frame = (XFrame) tEnv.getObjRelation("FrameLoader.Frame") ;
104 
105         if (frame == null) {
106             SOfficeFactory SOF = SOfficeFactory.getFactory( (XMultiServiceFactory)tParam.getMSF() );
107 
108             try {
109                 log.println( "creating a textdocument" );
110                 frameSup = SOF.createTextDoc( null );
111 
112                 Object oDsk = ((XMultiServiceFactory)tParam.getMSF())
113                         .createInstance("com.sun.star.frame.Desktop") ;
114                 XDesktop dsk = (XDesktop)
115                     UnoRuntime.queryInterface(XDesktop.class, oDsk) ;
116 
117                 shortWait() ;
118                 frame = dsk.getCurrentFrame() ;
119             } catch ( com.sun.star.uno.Exception e ) {
120                 // Some exception occures.FAILED
121                 e.printStackTrace( log );
122                 throw new StatusException( "Couldn't create a frame.", e );
123             }
124         }
125 
126         Object args = tEnv.getObjRelation("FrameLoader.args") ;
127         if (args != null) this.args = (PropertyValue[]) args ;
128 
129         if (url == null /*|| frame == null*/) {
130             throw new StatusException
131                 (Status.failed("Some relations not found")) ;
132         }
133     }
134 
135     private boolean loaded = false ;
136 
137     /**
138     * Firts <code>cancel</code> method test is called.
139     * If in that test loaing process was interrupted by
140     * <code>cancel</code> call then <code>load</code> test
141     * executes. It loads a component, waits some moment to
142     * listener have a chance to be called  and then checks
143     * if the load listener was called. <p>
144     * Has <b>OK</b> status if <code>cancel</code> method test
145     * didn't interrupt loading and it was successfull, or
146     * if in this method it loads successfull and listener's
147     * <code>finished</code> method was called.
148     * The following method tests are to be executed before :
149     * <ul>
150     *  <li> <code> cancel() </code> </li>
151     * </ul>
152     */
153     public void _load() {
154         executeMethod("cancel()") ;
155 
156         if (!loaded) {
157             oObj.load(frame, url, args, listener) ;
158 
159             shortWait();
160 
161             loaded = listener.finished ;
162         }
163 
164         tRes.tested("load()", loaded) ;
165     }
166 
167     /**
168     * Starts to load a component and then immediatly tries to
169     * cancel the process. <p>
170     * Has <b>OK</b> status if the process was cancelled or
171     * finished (appropriate listener methods were called).
172     */
173     public void _cancel() {
174         boolean result = true ;
175 
176         oObj.load(frame, url, args, listener) ;
177         oObj.cancel() ;
178 
179         shortWait();
180 
181         if (listener.cancelled) {
182             log.println("Loading was canceled.") ;
183         }
184         if (listener.finished) {
185             log.println("Loading was finished.") ;
186             loaded = true ;
187         }
188         if (!listener.cancelled && !listener.finished) {
189             log.println("Loading was not canceled and not finished") ;
190             result = false ;
191         }
192 
193         tRes.tested("cancel()", result) ;
194     }
195 
196     public void after() {
197         if (frameSup != null) frameSup.dispose() ;
198         frame.dispose();
199     }
200 
201     private void shortWait() {
202         try {
203             Thread.sleep(5000);
204         }
205         catch (InterruptedException ex) {
206         }
207 
208     }
209 }
210 
211