1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.ui.dialogs;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.lang.EventObject;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.ui.dialogs.FilePickerEvent;
33 import com.sun.star.ui.dialogs.XExecutableDialog;
34 import com.sun.star.ui.dialogs.XFilePicker;
35 import com.sun.star.ui.dialogs.XFilePickerListener;
36 import com.sun.star.ui.dialogs.XFilePickerNotifier;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.util.XCancellable;
39 
40 
41 /**
42  * Testing <code>com.sun.star.ui.XFilePickerNotifier</code>
43  * interface methods :
44  * <ul>
45  *  <li><code> addFilePickerListener()</code></li>
46  *  <li><code> removeFilePickerListener()</code></li>
47  * </ul> <p>
48  * The object must implement <code>XFilePicker</code>
49  * interface to check if a listener was called. <p>
50  * Test is <b> NOT </b> multithread compilant. <p>
51  * @see com.sun.star.ui.XFilePickerNotifier
52  */
53 public class _XFilePickerNotifier extends MultiMethodTest {
54 
55     public XFilePickerNotifier oObj = null;
56     private XFilePicker fps = null ;
57     private String dir1 = null,
58             dir2 = null ;
59     ExecThread eThread = null;
60 
61 
62     /**
63      * Listener implementation which sets a flag if some of its
64      * methods was called.
65      */
66     protected class TestListener implements XFilePickerListener {
67         public boolean called = false ;
68 
dialogSizeChanged()69         public void dialogSizeChanged() {
70             called = true;
71         }
72 
fileSelectionChanged(FilePickerEvent e)73         public void fileSelectionChanged(FilePickerEvent e) {
74             called = true;
75         }
76 
directoryChanged(FilePickerEvent e)77         public void directoryChanged(FilePickerEvent e) {
78             log.println("***** Directory Changed *****");
79             called = true;
80         }
81 
helpRequested(FilePickerEvent e)82         public String helpRequested(FilePickerEvent e) {
83             called = true;
84             return "help";
85         }
86 
controlStateChanged(FilePickerEvent e)87         public void controlStateChanged(FilePickerEvent e) {
88             called = true;
89         }
90 
disposing(EventObject e)91         public void disposing(EventObject e) {}
92     }
93 
94     TestListener listener = new TestListener() ;
95 
96     /**
97      * Tries to query object for <code>XFilePicker</code> interface, and
98      * initializes two different URLs for changing file picker directory. <p>
99      * @throw StatusException If object doesn't support <code>XFilePicker</code>
100      * interface.
101      */
before()102     public void before() {
103         fps = (XFilePicker) UnoRuntime.queryInterface
104                 (XFilePicker.class, oObj) ;
105 
106         if (fps == null) {
107             log.println("The object doesnt implement XFilePicker") ;
108             throw new StatusException(Status.failed
109                     ("The object doesnt implement XFilePicker"));
110         }
111 
112         XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
113                 XExecutableDialog.class, tEnv.getTestObject());
114 
115         dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
116         dir2 = util.utils.getFullTestURL("");
117         eThread = new ExecThread(exD);
118     }
119 
120     /**
121      * Adds a listener, then tries to change display directory and
122      * checks if the listener was called. <p>
123      * Has <b>OK</b> status if a listener method was called.
124      */
_addFilePickerListener()125     public void _addFilePickerListener() {
126         oObj.addFilePickerListener(listener) ;
127 
128         try {
129             log.println("***** Setting DisplayDirectory to " + dir1);
130             fps.setDisplayDirectory(dir1) ;
131             log.println("***** Getting: " + fps.getDisplayDirectory());
132             openDialog();
133             log.println("***** Setting DisplayDirectory to " + dir2);
134             fps.setDisplayDirectory(dir2) ;
135             log.println("***** Getting: " + fps.getDisplayDirectory());
136 
137         } catch(com.sun.star.lang.IllegalArgumentException e) {
138             log.println("!!! Exception changing dir !!!") ;
139             e.printStackTrace(log) ;
140         }
141 
142         shortWait();
143 
144         if (!listener.called) {
145             log.println("Listener wasn't called :-(");
146         }
147 
148         closeDialog();
149 
150         tRes.tested("addFilePickerListener()", listener.called) ;
151     }
152 
153     /**
154      * Removes the listener and changes display directory. <p>
155      * Has <b>OK</b> status if the listener wasn't called. <p>
156      * The following method tests are to be completed successfully before :
157      * <ul>
158      *  <li> <code> addFilePickerListener </code> </li>
159      * </ul>
160      */
_removeFilePickerListener()161     public void _removeFilePickerListener() {
162         requiredMethod("addFilePickerListener()") ;
163 
164         oObj.removeFilePickerListener(listener) ;
165 
166         listener.called = false ;
167 
168         try {
169             fps.setDisplayDirectory(dir1) ;
170             openDialog();
171             fps.setDisplayDirectory(dir2) ;
172         } catch(com.sun.star.lang.IllegalArgumentException e) {
173             log.println("!!! Exception changing dir !!!") ;
174             e.printStackTrace(log) ;
175         }
176 
177         shortWait();
178 
179         closeDialog();
180 
181         tRes.tested("removeFilePickerListener()", !listener.called) ;
182     }
183 
184     /**
185      * Calls <code>execute()</code> method in a separate thread.
186      * Necessary to check if this method works
187      */
188     protected class ExecThread extends Thread {
189 
190         public short execRes = (short) 17 ;
191         private XExecutableDialog Diag = null ;
192 
ExecThread(XExecutableDialog Diag)193         public ExecThread(XExecutableDialog Diag) {
194             this.Diag = Diag ;
195         }
196 
run()197         public void run() {
198             try {
199                 execRes = Diag.execute();
200                 System.out.println("HERE: "+execRes);
201             } catch (Exception e) {
202                 log.println("Thread has been interrupted ...");
203             }
204         }
205     }
206 
207     /**
208      * Sleeps for 0.5 sec. to allow StarOffice to react on <code>
209      * reset</code> call.
210      */
shortWait()211     private void shortWait() {
212         try {
213             Thread.sleep(2000) ;
214         } catch (InterruptedException e) {
215             log.println("While waiting :" + e) ;
216         }
217     }
218 
closeDialog()219     private void closeDialog() {
220         XCancellable canc = (XCancellable) UnoRuntime.queryInterface(
221                 XCancellable.class, tEnv.getTestObject());
222         if (canc != null) {
223             log.println("Cancelling Dialog");
224             canc.cancel();
225         } else {
226             this.disposeEnvironment();
227         }
228 
229         long st = System.currentTimeMillis();
230         boolean toLong = false;
231 
232         log.println("waiting for dialog to close");
233 
234         while (eThread.isAlive() && !toLong) {
235             //wait for dialog to close
236             toLong = (System.currentTimeMillis()-st > 10000);
237         }
238 
239         log.println("done");
240 
241         try {
242             if (eThread.isAlive()) {
243                 log.println("Interrupting Thread");
244                 eThread.interrupt();
245                 eThread.yield();
246             }
247         } catch (Exception e) {
248             // who cares ;-)
249         }
250 
251         st = System.currentTimeMillis();
252         toLong = false;
253 
254         log.println("waiting for interruption to work");
255 
256         while (eThread.isAlive() && !toLong) {
257             //wait for dialog to close
258             toLong = (System.currentTimeMillis()-st > 10000);
259         }
260 
261         log.println("DialogThread alive: "+eThread.isAlive());
262 
263         log.println("done");
264 
265     }
266 
openDialog()267     private void openDialog() {
268         log.println("Starting Dialog");
269         if (eThread.isAlive()) {
270             log.println("second interrupt");
271             eThread.interrupt();
272             eThread.yield();
273         }
274 
275         XExecutableDialog exD = (XExecutableDialog) UnoRuntime.queryInterface(
276                 XExecutableDialog.class, tEnv.getTestObject());
277 
278         dir1 = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF());
279         dir2 = util.utils.getFullTestURL("");
280         eThread = new ExecThread(exD);
281 
282         eThread.start();
283     }
284 }
285 
286 
287