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.ui.dialogs;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
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;
43 
44 
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
56  */
57 public class _XFilePickerNotifier extends MultiMethodTest {
58 
59     public XFilePickerNotifier oObj = null;
60     private XFilePicker fps = null ;
61     private String dir1 = null,
62             dir2 = null ;
63     ExecThread eThread = null;
64 
65 
66     /**
67      * Listener implementation which sets a flag if some of its
68      * methods was called.
69      */
70     protected class TestListener implements XFilePickerListener {
71         public boolean called = false ;
72 
73         public void dialogSizeChanged() {
74             called = true;
75         }
76 
77         public void fileSelectionChanged(FilePickerEvent e) {
78             called = true;
79         }
80 
81         public void directoryChanged(FilePickerEvent e) {
82             log.println("***** Directory Changed *****");
83             called = true;
84         }
85 
86         public String helpRequested(FilePickerEvent e) {
87             called = true;
88             return "help";
89         }
90 
91         public void controlStateChanged(FilePickerEvent e) {
92             called = true;
93         }
94 
95         public void disposing(EventObject e) {}
96     }
97 
98     TestListener listener = new TestListener() ;
99 
100     /**
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.
105      */
106     public void before() {
107         fps = (XFilePicker) UnoRuntime.queryInterface
108                 (XFilePicker.class, oObj) ;
109 
110         if (fps == null) {
111             log.println("The object doesnt implement XFilePicker") ;
112             throw new StatusException(Status.failed
113                     ("The object doesnt implement XFilePicker"));
114         }
115 
116         XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
117                 XExecutableDialog.class, tEnv.getTestObject());
118 
119         dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
120         dir2 = util.utils.getFullTestURL("");
121         eThread = new ExecThread(exD);
122     }
123 
124     /**
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.
128      */
129     public void _addFilePickerListener() {
130         oObj.addFilePickerListener(listener) ;
131 
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());
140 
141         } catch(com.sun.star.lang.IllegalArgumentException e) {
142             log.println("!!! Exception changing dir !!!") ;
143             e.printStackTrace(log) ;
144         }
145 
146         shortWait();
147 
148         if (!listener.called) {
149             log.println("Listener wasn't called :-(");
150         }
151 
152         closeDialog();
153 
154         tRes.tested("addFilePickerListener()", listener.called) ;
155     }
156 
157     /**
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>
164      */
165     public void _removeFilePickerListener() {
166         requiredMethod("addFilePickerListener()") ;
167 
168         oObj.removeFilePickerListener(listener) ;
169 
170         listener.called = false ;
171 
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) ;
179         }
180 
181         shortWait();
182 
183         closeDialog();
184 
185         tRes.tested("removeFilePickerListener()", !listener.called) ;
186     }
187 
188     /**
189      * Calls <code>execute()</code> method in a separate thread.
190      * Necessary to check if this method works
191      */
192     protected class ExecThread extends Thread {
193 
194         public short execRes = (short) 17 ;
195         private XExecutableDialog Diag = null ;
196 
197         public ExecThread(XExecutableDialog Diag) {
198             this.Diag = Diag ;
199         }
200 
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 ...");
207             }
208         }
209     }
210 
211     /**
212      * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
213      * reset</code> call.
214      */
215     private void shortWait() {
216         try {
217             Thread.sleep(2000) ;
218         } catch (InterruptedException e) {
219             log.println("While waiting :" + e) ;
220         }
221     }
222 
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();
231         }
232 
233         long st = System.currentTimeMillis();
234         boolean toLong = false;
235 
236         log.println("waiting for dialog to close");
237 
238         while (eThread.isAlive() && !toLong) {
239             //wait for dialog to close
240             toLong = (System.currentTimeMillis()-st > 10000);
241         }
242 
243         log.println("done");
244 
245         try {
246             if (eThread.isAlive()) {
247                 log.println("Interrupting Thread");
248                 eThread.interrupt();
249                 eThread.yield();
250             }
251         } catch (Exception e) {
252             // who cares ;-)
253         }
254 
255         st = System.currentTimeMillis();
256         toLong = false;
257 
258         log.println("waiting for interruption to work");
259 
260         while (eThread.isAlive() && !toLong) {
261             //wait for dialog to close
262             toLong = (System.currentTimeMillis()-st > 10000);
263         }
264 
265         log.println("DialogThread alive: "+eThread.isAlive());
266 
267         log.println("done");
268 
269     }
270 
271     private void openDialog() {
272         log.println("Starting Dialog");
273         if (eThread.isAlive()) {
274             log.println("second interrupt");
275             eThread.interrupt();
276             eThread.yield();
277         }
278 
279         XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
280                 XExecutableDialog.class, tEnv.getTestObject());
281 
282         dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
283         dir2 = util.utils.getFullTestURL("");
284         eThread = new ExecThread(exD);
285 
286         eThread.start();
287     }
288 }
289 
290 
291