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.awt;
25 
26 import java.io.PrintWriter;
27 
28 import lib.MultiMethodTest;
29 
30 import com.sun.star.awt.XMenuBar;
31 import com.sun.star.awt.XTopWindow;
32 import com.sun.star.awt.XTopWindowListener;
33 import com.sun.star.lang.EventObject;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.text.XTextDocument;
36 import com.sun.star.uno.UnoRuntime;
37 
38 /**
39 * Testing <code>com.sun.star.awt.XTopWindow</code>
40 * interface methods :
41 * <ul>
42 *  <li><code> addTopWindowListener()</code></li>
43 *  <li><code> removeTopWindowListener()</code></li>
44 *  <li><code> toFront()</code></li>
45 *  <li><code> toBack()</code></li>
46 *  <li><code> setMenuBar()</code></li>
47 * </ul> <p>
48 * Test is <b> NOT </b> multithread compilant. <p>
49 * @see com.sun.star.awt.XTopWindow
50 */
51 public class _XTopWindow extends MultiMethodTest {
52 
53     public XTopWindow oObj = null;
54 
55     /**
56     * Listener implementation which sets flags on different
57     * method calls.
58     */
59     protected class TestListener implements XTopWindowListener {
60         private PrintWriter log = null ;
61         public boolean activated = false ;
62         public boolean deactivated = false ;
63 
TestListener(PrintWriter log)64         public TestListener(PrintWriter log) {
65             this.log = log ;
66         }
67 
initListener()68         public void initListener() {
69             activated = false;
70             deactivated = false;
71         }
72 
windowOpened(EventObject e)73         public void windowOpened(EventObject e) {
74             log.println("windowOpened() called") ;
75         }
windowClosing(EventObject e)76         public void windowClosing(EventObject e) {
77             log.println("windowClosing() called") ;
78         }
windowClosed(EventObject e)79         public void windowClosed(EventObject e) {
80             log.println("windowClosed() called") ;
81         }
windowMinimized(EventObject e)82         public void windowMinimized(EventObject e) {
83             log.println("windowMinimized() called") ;
84         }
windowNormalized(EventObject e)85         public void windowNormalized(EventObject e) {
86             log.println("windowNormalized() called") ;
87         }
windowActivated(EventObject e)88         public void windowActivated(EventObject e) {
89             activated = true;
90             log.println("windowActivated() called") ;
91         }
windowDeactivated(EventObject e)92         public void windowDeactivated(EventObject e) {
93             deactivated = true;
94             log.println("windowDeactivated() called") ;
95         }
disposing(EventObject e)96         public void disposing(EventObject e) {}
97     }
98 
99     protected TestListener listener = null ;
100 
101     XTextDocument aTextDoc = null;
102 
103 
before()104     protected void before() {
105         aTextDoc = util.WriterTools.createTextDoc((XMultiServiceFactory)tParam.getMSF());
106     }
107 
108     /**
109      * Adds a listener . <p>
110      *
111      * Has <b>OK</b> status always (listener calls are checked in
112      * other methods. <p>
113      */
_addTopWindowListener()114     public void _addTopWindowListener() {
115         listener = new TestListener(log) ;
116 
117         oObj.addTopWindowListener(listener) ;
118 
119         tRes.tested("addTopWindowListener()", true);
120     }
121 
122     /**
123      * Removes a listener added before. <p>
124      * Has <b>OK</b> status always. <p>
125      * The following method tests are to be completed successfully before :
126      * <ul>
127      *  <li> <code> toBack </code> : to have a definite method execution
128      *    order.</li>
129      * </ul>
130      */
_removeTopWindowListener()131     public void _removeTopWindowListener() {
132         executeMethod("toBack()");
133 
134         oObj.removeTopWindowListener(listener);
135 
136         tRes.tested("removeTopWindowListener()", true);
137     }
138 
139     /**
140      * Moves the window to front and check the listener calls. <p>
141      * Has <b>OK</b> status if listener <code>activated</code> method
142      * was called.
143      */
_toFront()144     public void _toFront() {
145         requiredMethod("addTopWindowListener()");
146         listener.initListener();
147         oObj.toFront();
148         shortWait();
149 
150         tRes.tested("toFront()", listener.activated && !listener.deactivated);
151     }
152 
153     /**
154      * This method doesn't do anything the Office implementation. <p>
155      * So it has always <b>OK</b> status
156      */
_toBack()157     public void _toBack() {
158         oObj.toBack();
159         tRes.tested("toBack()", true);
160     }
161 
162     /**
163     * Creates a simple menu bar and adds to the window. <p>
164     * Has <b>OK</b> status if no runtime exceptions occurred.
165     */
_setMenuBar()166     public void _setMenuBar() {
167         XMenuBar menu = null ;
168         boolean result = true ;
169 
170         try {
171             menu = (XMenuBar) UnoRuntime.queryInterface(XMenuBar.class,
172                 ((XMultiServiceFactory)tParam.getMSF()).
173                 createInstance("com.sun.star.awt.MenuBar")) ;
174         } catch (com.sun.star.uno.Exception e) {
175             log.println("Can't instanciate MenuBar service") ;
176             result = false ;
177         }
178 
179         menu.insertItem((short)1, "MenuItem",
180             com.sun.star.awt.MenuItemStyle.CHECKABLE, (short)1) ;
181 
182         oObj.setMenuBar(menu) ;
183 
184         tRes.tested("setMenuBar()", result) ;
185     }
186 
187     /**
188      * Disposes the document created in <code>before</code> method.
189      */
after()190     protected void after() {
191         aTextDoc.dispose();
192     }
193 
shortWait()194     private void shortWait() {
195         try {
196             Thread.sleep(1000) ;
197         } catch (InterruptedException e) {
198             System.out.println("While waiting :" + e) ;
199         }
200     }
201 }
202 
203