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 
28 import com.sun.star.ui.dialogs.XExecutableDialog;
29 import com.sun.star.uno.UnoRuntime;
30 import com.sun.star.util.XCancellable;
31 
32 /**
33  * Testing <code>com.sun.star.ui.dialogs.XExecutableDialog</code>
34  * interface methods :
35  * <ul>
36  *  <li><code> setTitle()</code></li>
37  *  <li><code> execute()</code></li>
38  * </ul> <p>
39  *
40  * This interface methods cann't be checked, thereby methods
41  * are just called. <code>execute</code> method is not called
42  * at all as the dialog shown cann't be disposed. <p>
43  *
44  * Test is <b> NOT </b> multithread compilant. <p>
45  * @see com.sun.star.ui.dialogs.XExecutableDialog
46  */
47 public class _XExecutableDialog extends MultiMethodTest {
48 
49     public XExecutableDialog oObj = null;
50     private ExecThread eThread = null;
51 
52     /**
53      * Test calls the method. <p>
54      * Has <b> OK </b> status if the method successfully returns
55      * and no exceptions were thrown. <p>
56      */
_setTitle()57     public void _setTitle() {
58         oObj.setTitle("The Title");
59         tRes.tested("setTitle()",true);
60     }
61 
62     /**
63      * This method is excluded from automated test since
64      * we can't close the dialog. <p>
65      * Always has <b>OK</b> status.
66      */
_execute()67     public void _execute() {
68         String aName = tEnv.getTestCase().getObjectName();
69         boolean result = false;
70         if (aName.startsWith("OData") || aName.startsWith("OSQL")) {
71             log.println("dbaccess dialogs can't be closed via API");
72             log.println("therefore they aren't executed");
73             log.println("and the result is set to true");
74             result = true;
75         } else {
76             eThread = new ExecThread(oObj);
77             log.println("Starting Dialog");
78             eThread.start();
79             XCancellable canc = (XCancellable)UnoRuntime.queryInterface
80                     (XCancellable.class, tEnv.getTestObject());
81             shortWait();
82             if (canc != null) {
83                 closeDialog();
84                 short res = eThread.execRes;
85                 log.println("result: "+res);
86                 result = (res == 0);
87             } else {
88                 this.disposeEnvironment();
89                 result=true;
90                 log.println("XCancellable isn't supported and the "+
91                         "environment is killed hard");
92             }
93 
94 
95         }
96         tRes.tested("execute()",result);
97     }
98 
99     /**
100      * Calls <code>execute()</code> method in a separate thread.
101      * Necessary to check if this method works
102      */
103     protected class ExecThread extends Thread {
104 
105         public short execRes = (short) 17 ;
106         private XExecutableDialog Diag = null ;
107 
ExecThread(XExecutableDialog Diag)108         public ExecThread(XExecutableDialog Diag) {
109             this.Diag = Diag ;
110         }
111 
run()112         public void run() {
113             try {
114                 execRes = Diag.execute();
115                 System.out.println("HERE: "+execRes);
116             } catch(Exception e) {
117                 log.println("Thread has been interrupted ... ");
118             }
119         }
120     }
121 
122     /**
123      * Sleeps for 5 sec. to allow StarOffice to react on <code>
124      * reset</code> call.
125      */
shortWait()126     private void shortWait() {
127         try {
128             Thread.sleep(2000) ;
129         } catch (InterruptedException e) {
130             log.println("While waiting :" + e) ;
131         }
132     }
133 
after()134     public void after() {
135         if (eThread.isAlive()) {
136             log.println("Thread didn't die ... cleaning up");
137             disposeEnvironment();
138         }
139     }
140 
closeDialog()141     private void closeDialog() {
142         XCancellable canc = (XCancellable) UnoRuntime.queryInterface(
143                 XCancellable.class, tEnv.getTestObject());
144         if (canc != null) {
145             log.println("Cancelling Dialog");
146             canc.cancel();
147         } else {
148             this.disposeEnvironment();
149         }
150 
151         long st = System.currentTimeMillis();
152         boolean toLong = false;
153 
154         log.println("waiting for dialog to close");
155 
156         while (eThread.isAlive() && !toLong) {
157             //wait for dialog to close
158             toLong = (System.currentTimeMillis()-st > 10000);
159         }
160 
161         log.println("done");
162 
163         try {
164             if (eThread.isAlive()) {
165                 log.println("Interrupting Thread");
166                 eThread.interrupt();
167                 eThread.yield();
168             }
169         } catch (Exception e) {
170             // who cares ;-)
171         }
172 
173         st = System.currentTimeMillis();
174         toLong = false;
175 
176         log.println("waiting for interruption to work");
177 
178         while (eThread.isAlive() && !toLong) {
179             //wait for dialog to close
180             toLong = (System.currentTimeMillis()-st > 10000);
181         }
182 
183         log.println("DialogThread alive: "+eThread.isAlive());
184 
185         log.println("done");
186 
187     }
188 
189 }
190 
191 
192