Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / ui / dialogs / _XFilePickerNotifier.java
blobf39ec63ee80d6d038591e4e88cb97d701f67c6a2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 package ifc.ui.dialogs;
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
34 import com.sun.star.lang.EventObject;
35 import com.sun.star.lang.XMultiServiceFactory;
36 import com.sun.star.ui.dialogs.FilePickerEvent;
37 import com.sun.star.ui.dialogs.XExecutableDialog;
38 import com.sun.star.ui.dialogs.XFilePicker;
39 import com.sun.star.ui.dialogs.XFilePickerListener;
40 import com.sun.star.ui.dialogs.XFilePickerNotifier;
41 import com.sun.star.uno.UnoRuntime;
42 import com.sun.star.util.XCancellable;
45 /**
46 * Testing <code>com.sun.star.ui.XFilePickerNotifier</code>
47 * interface methods :
48 * <ul>
49 * <li><code> addFilePickerListener()</code></li>
50 * <li><code> removeFilePickerListener()</code></li>
51 * </ul> <p>
52 * The object must implement <code>XFilePicker</code>
53 * interface to check if a listener was called. <p>
54 * Test is <b> NOT </b> multithread compilant. <p>
55 * @see com.sun.star.ui.XFilePickerNotifier
57 public class _XFilePickerNotifier extends MultiMethodTest {
59 public XFilePickerNotifier oObj = null;
60 private XFilePicker fps = null ;
61 private String dir1 = null,
62 dir2 = null ;
63 ExecThread eThread = null;
66 /**
67 * Listener implementation which sets a flag if some of its
68 * methods was called.
70 protected class TestListener implements XFilePickerListener {
71 public boolean called = false ;
73 public void dialogSizeChanged() {
74 called = true;
77 public void fileSelectionChanged(FilePickerEvent e) {
78 called = true;
81 public void directoryChanged(FilePickerEvent e) {
82 log.println("***** Directory Changed *****");
83 called = true;
86 public String helpRequested(FilePickerEvent e) {
87 called = true;
88 return "help";
91 public void controlStateChanged(FilePickerEvent e) {
92 called = true;
95 public void disposing(EventObject e) {}
98 TestListener listener = new TestListener() ;
101 * Tries to query object for <code>XFilePicker</code> interface, and
102 * initializes two different URLs for changing file picker directory. <p>
103 * @throw StatusException If object doesn't support <code>XFilePicker</code>
104 * interface.
106 public void before() {
107 fps = (XFilePicker) UnoRuntime.queryInterface
108 (XFilePicker.class, oObj) ;
110 if (fps == null) {
111 log.println("The object doesnt implement XFilePicker") ;
112 throw new StatusException(Status.failed
113 ("The object doesnt implement XFilePicker"));
116 XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
117 XExecutableDialog.class, tEnv.getTestObject());
119 dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
120 dir2 = util.utils.getFullTestURL("");
121 eThread = new ExecThread(exD);
125 * Adds a listener, then tries to change display directory and
126 * checks if the listener was called. <p>
127 * Has <b>OK</b> status if a listener method was called.
129 public void _addFilePickerListener() {
130 oObj.addFilePickerListener(listener) ;
132 try {
133 log.println("***** Setting DisplayDirectory to " + dir1);
134 fps.setDisplayDirectory(dir1) ;
135 log.println("***** Getting: " + fps.getDisplayDirectory());
136 openDialog();
137 log.println("***** Setting DisplayDirectory to " + dir2);
138 fps.setDisplayDirectory(dir2) ;
139 log.println("***** Getting: " + fps.getDisplayDirectory());
141 } catch(com.sun.star.lang.IllegalArgumentException e) {
142 log.println("!!! Exception changing dir !!!") ;
143 e.printStackTrace(log) ;
146 shortWait();
148 if (!listener.called) {
149 log.println("Listener wasn't called :-(");
152 closeDialog();
154 tRes.tested("addFilePickerListener()", listener.called) ;
158 * Removes the listener and changes display directory. <p>
159 * Has <b>OK</b> status if the listener wasn't called. <p>
160 * The following method tests are to be completed successfully before :
161 * <ul>
162 * <li> <code> addFilePickerListener </code> </li>
163 * </ul>
165 public void _removeFilePickerListener() {
166 requiredMethod("addFilePickerListener()") ;
168 oObj.removeFilePickerListener(listener) ;
170 listener.called = false ;
172 try {
173 fps.setDisplayDirectory(dir1) ;
174 openDialog();
175 fps.setDisplayDirectory(dir2) ;
176 } catch(com.sun.star.lang.IllegalArgumentException e) {
177 log.println("!!! Exception changing dir !!!") ;
178 e.printStackTrace(log) ;
181 shortWait();
183 closeDialog();
185 tRes.tested("removeFilePickerListener()", !listener.called) ;
189 * Calls <code>execute()</code> method in a separate thread.
190 * Necessary to check if this method works
192 protected class ExecThread extends Thread {
194 public short execRes = (short) 17 ;
195 private XExecutableDialog Diag = null ;
197 public ExecThread(XExecutableDialog Diag) {
198 this.Diag = Diag ;
201 public void run() {
202 try {
203 execRes = Diag.execute();
204 System.out.println("HERE: "+execRes);
205 } catch (Exception e) {
206 log.println("Thread has been interrupted ...");
212 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
213 * reset</code> call.
215 private void shortWait() {
216 try {
217 Thread.sleep(2000) ;
218 } catch (InterruptedException e) {
219 log.println("While waiting :" + e) ;
223 private void closeDialog() {
224 XCancellable canc = (XCancellable) UnoRuntime.queryInterface(
225 XCancellable.class, tEnv.getTestObject());
226 if (canc != null) {
227 log.println("Cancelling Dialog");
228 canc.cancel();
229 } else {
230 this.disposeEnvironment();
233 long st = System.currentTimeMillis();
234 boolean toLong = false;
236 log.println("waiting for dialog to close");
238 while (eThread.isAlive() && !toLong) {
239 //wait for dialog to close
240 toLong = (System.currentTimeMillis()-st > 10000);
243 log.println("done");
245 try {
246 if (eThread.isAlive()) {
247 log.println("Interrupting Thread");
248 eThread.interrupt();
249 eThread.yield();
251 } catch (Exception e) {
252 // who cares ;-)
255 st = System.currentTimeMillis();
256 toLong = false;
258 log.println("waiting for interruption to work");
260 while (eThread.isAlive() && !toLong) {
261 //wait for dialog to close
262 toLong = (System.currentTimeMillis()-st > 10000);
265 log.println("DialogThread alive: "+eThread.isAlive());
267 log.println("done");
271 private void openDialog() {
272 log.println("Starting Dialog");
273 if (eThread.isAlive()) {
274 log.println("second interrupt");
275 eThread.interrupt();
276 eThread.yield();
279 XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
280 XExecutableDialog.class, tEnv.getTestObject());
282 dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
283 dir2 = util.utils.getFullTestURL("");
284 eThread = new ExecThread(exD);
286 eThread.start();