bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / ui / dialogs / _XFilePickerNotifier.java
blobfca80b59589e788e5a94730803a7df1dbc9cbe9f
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.ui.dialogs;
21 import lib.MultiMethodTest;
22 import lib.Status;
23 import lib.StatusException;
25 import com.sun.star.lang.EventObject;
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.ui.dialogs.FilePickerEvent;
28 import com.sun.star.ui.dialogs.XExecutableDialog;
29 import com.sun.star.ui.dialogs.XFilePicker;
30 import com.sun.star.ui.dialogs.XFilePickerListener;
31 import com.sun.star.ui.dialogs.XFilePickerNotifier;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.util.XCancellable;
36 /**
37 * Testing <code>com.sun.star.ui.XFilePickerNotifier</code>
38 * interface methods :
39 * <ul>
40 * <li><code> addFilePickerListener()</code></li>
41 * <li><code> removeFilePickerListener()</code></li>
42 * </ul> <p>
43 * The object must implement <code>XFilePicker</code>
44 * interface to check if a listener was called. <p>
45 * Test is <b> NOT </b> multithread compilant. <p>
46 * @see com.sun.star.ui.XFilePickerNotifier
48 public class _XFilePickerNotifier extends MultiMethodTest {
50 public XFilePickerNotifier oObj = null;
51 private XFilePicker fps = null ;
52 private String dir1 = null,
53 dir2 = null ;
54 ExecThread eThread = null;
57 /**
58 * Listener implementation which sets a flag if some of its
59 * methods was called.
61 protected class TestListener implements XFilePickerListener {
62 public boolean called = false ;
64 public void dialogSizeChanged() {
65 called = true;
68 public void fileSelectionChanged(FilePickerEvent e) {
69 called = true;
72 public void directoryChanged(FilePickerEvent e) {
73 log.println("***** Directory Changed *****");
74 called = true;
77 public String helpRequested(FilePickerEvent e) {
78 called = true;
79 return "help";
82 public void controlStateChanged(FilePickerEvent e) {
83 called = true;
86 public void disposing(EventObject e) {}
89 TestListener listener = new TestListener() ;
91 /**
92 * Tries to query object for <code>XFilePicker</code> interface, and
93 * initializes two different URLs for changing file picker directory. <p>
94 * @throw StatusException If object doesn't support <code>XFilePicker</code>
95 * interface.
97 public void before() {
98 fps = UnoRuntime.queryInterface
99 (XFilePicker.class, oObj) ;
101 if (fps == null) {
102 log.println("The object doesn't implement XFilePicker") ;
103 throw new StatusException(Status.failed
104 ("The object doesn't implement XFilePicker"));
107 XExecutableDialog exD = UnoRuntime.queryInterface(
108 XExecutableDialog.class, tEnv.getTestObject());
110 dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
111 dir2 = util.utils.getFullTestURL("");
112 eThread = new ExecThread(exD);
116 * Adds a listener, then tries to change display directory and
117 * checks if the listener was called. <p>
118 * Has <b>OK</b> status if a listener method was called.
120 public void _addFilePickerListener() {
121 oObj.addFilePickerListener(listener) ;
123 try {
124 log.println("***** Setting DisplayDirectory to " + dir1);
125 fps.setDisplayDirectory(dir1) ;
126 log.println("***** Getting: " + fps.getDisplayDirectory());
127 openDialog();
128 log.println("***** Setting DisplayDirectory to " + dir2);
129 fps.setDisplayDirectory(dir2) ;
130 log.println("***** Getting: " + fps.getDisplayDirectory());
132 } catch(com.sun.star.lang.IllegalArgumentException e) {
133 log.println("!!! Exception changing dir !!!") ;
134 e.printStackTrace(log) ;
137 shortWait();
139 if (!listener.called) {
140 log.println("Listener wasn't called :-(");
143 closeDialog();
145 tRes.tested("addFilePickerListener()", listener.called) ;
149 * Removes the listener and changes display directory. <p>
150 * Has <b>OK</b> status if the listener wasn't called. <p>
151 * The following method tests are to be completed successfully before :
152 * <ul>
153 * <li> <code> addFilePickerListener </code> </li>
154 * </ul>
156 public void _removeFilePickerListener() {
157 requiredMethod("addFilePickerListener()") ;
159 oObj.removeFilePickerListener(listener) ;
161 listener.called = false ;
163 try {
164 fps.setDisplayDirectory(dir1) ;
165 openDialog();
166 fps.setDisplayDirectory(dir2) ;
167 } catch(com.sun.star.lang.IllegalArgumentException e) {
168 log.println("!!! Exception changing dir !!!") ;
169 e.printStackTrace(log) ;
172 shortWait();
174 closeDialog();
176 tRes.tested("removeFilePickerListener()", !listener.called) ;
180 * Calls <code>execute()</code> method in a separate thread.
181 * Necessary to check if this method works
183 protected class ExecThread extends Thread {
185 public short execRes = (short) 17 ;
186 private XExecutableDialog Diag = null ;
188 public ExecThread(XExecutableDialog Diag) {
189 this.Diag = Diag ;
192 public void run() {
193 try {
194 execRes = Diag.execute();
195 System.out.println("HERE: "+execRes);
196 } catch (Exception e) {
197 log.println("Thread has been interrupted ...");
203 * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
204 * reset</code> call.
206 private void shortWait() {
207 try {
208 Thread.sleep(2000) ;
209 } catch (InterruptedException e) {
210 log.println("While waiting :" + e) ;
214 private void closeDialog() {
215 XCancellable canc = UnoRuntime.queryInterface(
216 XCancellable.class, tEnv.getTestObject());
217 if (canc != null) {
218 log.println("Cancelling Dialog");
219 canc.cancel();
220 } else {
221 this.disposeEnvironment();
224 long st = System.currentTimeMillis();
225 boolean toLong = false;
227 log.println("waiting for dialog to close");
229 while (eThread.isAlive() && !toLong) {
230 //wait for dialog to close
231 toLong = (System.currentTimeMillis()-st > 10000);
234 log.println("done");
236 try {
237 if (eThread.isAlive()) {
238 log.println("Interrupting Thread");
239 eThread.interrupt();
240 Thread.yield();
242 } catch (Exception e) {
243 // who cares ;-)
246 st = System.currentTimeMillis();
247 toLong = false;
249 log.println("waiting for interruption to work");
251 while (eThread.isAlive() && !toLong) {
252 //wait for dialog to close
253 toLong = (System.currentTimeMillis()-st > 10000);
256 log.println("DialogThread alive: "+eThread.isAlive());
258 log.println("done");
262 private void openDialog() {
263 log.println("Starting Dialog");
264 if (eThread.isAlive()) {
265 log.println("second interrupt");
266 eThread.interrupt();
267 Thread.yield();
270 XExecutableDialog exD = UnoRuntime.queryInterface(
271 XExecutableDialog.class, tEnv.getTestObject());
273 dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
274 dir2 = util.utils.getFullTestURL("");
275 eThread = new ExecThread(exD);
277 eThread.start();