Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / ui / dialogs / _XFilePickerNotifier.java
blobb13f5fabf0160b4ad5abb4d21152ccc1783a61f5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XFilePickerNotifier.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.ui.dialogs;
33 import lib.MultiMethodTest;
34 import lib.Status;
35 import lib.StatusException;
37 import com.sun.star.lang.EventObject;
38 import com.sun.star.lang.XMultiServiceFactory;
39 import com.sun.star.ui.dialogs.FilePickerEvent;
40 import com.sun.star.ui.dialogs.XExecutableDialog;
41 import com.sun.star.ui.dialogs.XFilePicker;
42 import com.sun.star.ui.dialogs.XFilePickerListener;
43 import com.sun.star.ui.dialogs.XFilePickerNotifier;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.util.XCancellable;
48 /**
49 * Testing <code>com.sun.star.ui.XFilePickerNotifier</code>
50 * interface methods :
51 * <ul>
52 * <li><code> addFilePickerListener()</code></li>
53 * <li><code> removeFilePickerListener()</code></li>
54 * </ul> <p>
55 * The object must implement <code>XFilePicker</code>
56 * interface to check if a listener was called. <p>
57 * Test is <b> NOT </b> multithread compilant. <p>
58 * @see com.sun.star.ui.XFilePickerNotifier
60 public class _XFilePickerNotifier extends MultiMethodTest {
62 public XFilePickerNotifier oObj = null;
63 private XFilePicker fps = null ;
64 private String dir1 = null,
65 dir2 = null ;
66 ExecThread eThread = null;
69 /**
70 * Listener implementation which sets a flag if some of its
71 * methods was called.
73 protected class TestListener implements XFilePickerListener {
74 public boolean called = false ;
76 public void dialogSizeChanged() {
77 called = true;
80 public void fileSelectionChanged(FilePickerEvent e) {
81 called = true;
84 public void directoryChanged(FilePickerEvent e) {
85 log.println("***** Directory Changed *****");
86 called = true;
89 public String helpRequested(FilePickerEvent e) {
90 called = true;
91 return "help";
94 public void controlStateChanged(FilePickerEvent e) {
95 called = true;
98 public void disposing(EventObject e) {}
101 TestListener listener = new TestListener() ;
104 * Tries to query object for <code>XFilePicker</code> interface, and
105 * initializes two different URLs for changing file picker directory. <p>
106 * @throw StatusException If object doesn't support <code>XFilePicker</code>
107 * interface.
109 public void before() {
110 fps = (XFilePicker) UnoRuntime.queryInterface
111 (XFilePicker.class, oObj) ;
113 if (fps == null) {
114 log.println("The object doesnt implement XFilePicker") ;
115 throw new StatusException(Status.failed
116 ("The object doesnt implement XFilePicker"));
119 XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
120 XExecutableDialog.class, tEnv.getTestObject());
122 dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
123 dir2 = util.utils.getFullTestURL("");
124 eThread = new ExecThread(exD);
128 * Adds a listener, then tries to change display directory and
129 * checks if the listener was called. <p>
130 * Has <b>OK</b> status if a listener method was called.
132 public void _addFilePickerListener() {
133 oObj.addFilePickerListener(listener) ;
135 try {
136 log.println("***** Setting DisplayDirectory to " + dir1);
137 fps.setDisplayDirectory(dir1) ;
138 log.println("***** Getting: " + fps.getDisplayDirectory());
139 openDialog();
140 log.println("***** Setting DisplayDirectory to " + dir2);
141 fps.setDisplayDirectory(dir2) ;
142 log.println("***** Getting: " + fps.getDisplayDirectory());
144 } catch(com.sun.star.lang.IllegalArgumentException e) {
145 log.println("!!! Exception changing dir !!!") ;
146 e.printStackTrace(log) ;
149 shortWait();
151 if (!listener.called) {
152 log.println("Listener wasn't called :-(");
155 closeDialog();
157 tRes.tested("addFilePickerListener()", listener.called) ;
161 * Removes the listener and changes display directory. <p>
162 * Has <b>OK</b> status if the listener wasn't called. <p>
163 * The following method tests are to be completed successfully before :
164 * <ul>
165 * <li> <code> addFilePickerListener </code> </li>
166 * </ul>
168 public void _removeFilePickerListener() {
169 requiredMethod("addFilePickerListener()") ;
171 oObj.removeFilePickerListener(listener) ;
173 listener.called = false ;
175 try {
176 fps.setDisplayDirectory(dir1) ;
177 openDialog();
178 fps.setDisplayDirectory(dir2) ;
179 } catch(com.sun.star.lang.IllegalArgumentException e) {
180 log.println("!!! Exception changing dir !!!") ;
181 e.printStackTrace(log) ;
184 shortWait();
186 closeDialog();
188 tRes.tested("removeFilePickerListener()", !listener.called) ;
192 * Calls <code>execute()</code> method in a separate thread.
193 * Necessary to check if this method works
195 protected class ExecThread extends Thread {
197 public short execRes = (short) 17 ;
198 private XExecutableDialog Diag = null ;
200 public ExecThread(XExecutableDialog Diag) {
201 this.Diag = Diag ;
204 public void run() {
205 try {
206 execRes = Diag.execute();
207 System.out.println("HERE: "+execRes);
208 } catch (Exception e) {
209 log.println("Thread has been interrupted ...");
215 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
216 * reset</code> call.
218 private void shortWait() {
219 try {
220 Thread.sleep(2000) ;
221 } catch (InterruptedException e) {
222 log.println("While waiting :" + e) ;
226 private void closeDialog() {
227 XCancellable canc = (XCancellable) UnoRuntime.queryInterface(
228 XCancellable.class, tEnv.getTestObject());
229 if (canc != null) {
230 log.println("Cancelling Dialog");
231 canc.cancel();
232 } else {
233 this.disposeEnvironment();
236 long st = System.currentTimeMillis();
237 boolean toLong = false;
239 log.println("waiting for dialog to close");
241 while (eThread.isAlive() && !toLong) {
242 //wait for dialog to close
243 toLong = (System.currentTimeMillis()-st > 10000);
246 log.println("done");
248 try {
249 if (eThread.isAlive()) {
250 log.println("Interrupting Thread");
251 eThread.interrupt();
252 eThread.yield();
254 } catch (Exception e) {
255 // who cares ;-)
258 st = System.currentTimeMillis();
259 toLong = false;
261 log.println("waiting for interruption to work");
263 while (eThread.isAlive() && !toLong) {
264 //wait for dialog to close
265 toLong = (System.currentTimeMillis()-st > 10000);
268 log.println("DialogThread alive: "+eThread.isAlive());
270 log.println("done");
274 private void openDialog() {
275 log.println("Starting Dialog");
276 if (eThread.isAlive()) {
277 log.println("second interrupt");
278 eThread.interrupt();
279 eThread.yield();
282 XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
283 XExecutableDialog.class, tEnv.getTestObject());
285 dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
286 dir2 = util.utils.getFullTestURL("");
287 eThread = new ExecThread(exD);
289 eThread.start();