xref: /AOO41X/main/qadevOOo/tests/java/ifc/form/_XFormController.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.form;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.awt.XControl;
33 import com.sun.star.awt.XWindow;
34 import com.sun.star.form.XFormController;
35 import com.sun.star.form.XFormControllerListener;
36 import com.sun.star.lang.EventObject;
37 import com.sun.star.uno.UnoRuntime;
38 
39 /**
40 * Testing <code>com.sun.star.form.XFormController</code>
41 * interface methods :
42 * <ul>
43 *  <li><code> getCurrentControl()</code></li>
44 *  <li><code> addActivateListener()</code></li>
45 *  <li><code> removeActivateListener()</code></li>
46 * </ul> <p>
47 * This test needs the following object relations :
48 * <ul>
49 *  <li> <code>'otherWindow'</code>
50 *  (of type <code>com.sun.star.awt.XWindow</code>):
51 *   The another window is used to activate it, causing deactivating
52 *   of the component tested. </li>
53 * <ul> <p>
54 * Test is <b> NOT </b> multithread compilant. <p>
55 * @see com.sun.star.form.XFormController
56 */
57 public class _XFormController extends MultiMethodTest {
58 
59     public static XFormController oObj = null;
60     XWindow otherWind = null;
61 
62     /**
63      * Listener which determines and stores events occured.
64      */
65     protected class MyListener implements XFormControllerListener {
66         public boolean activated = false ;
67         public boolean deactivated = false ;
68         public void disposing ( EventObject oEvent ) {}
69 
70         public void init() {
71             activated = false;
72             deactivated = false;
73         }
74 
75         public void formActivated(EventObject ev) {
76             activated = true ;
77         }
78 
79         public void formDeactivated(EventObject ev) {
80             deactivated = true ;
81         }
82     }
83 
84     MyListener listener = new MyListener() ;
85 
86     /**
87     * Adds a listener, then switches focus between two windows.
88     * The current controller must be deactivated and activated.<p>
89     *
90     * Has <b> OK </b> status if listener <code>deactivate</code>
91     * and <code>activate</code> methods was called. <p>
92     */
93     public void _addActivateListener() {
94         requiredMethod("getCurrentControl()");
95         oObj.addActivateListener(listener) ;
96 
97         XWindow wind = (XWindow)UnoRuntime.queryInterface(XWindow.class, cntrl);
98         wind.setFocus();
99         shortWait();
100         XWindow otherWind = (XWindow)tEnv.getObjRelation("otherWindow");
101         otherWind.setFocus();
102         shortWait();
103         log.println("activated = " + listener.activated +
104             ", deactivated = " + listener.deactivated) ;
105 
106         tRes.tested("addActivateListener()",
107             listener.deactivated && listener.activated) ;
108     }
109 
110     /**
111     * Removes the litener added before, then switches focus between two windows.
112     *
113     * Has <b> OK </b> status if no listener methods were called. <p>
114     */
115     public void _removeActivateListener() {
116         requiredMethod("addActivateListener()") ;
117 
118         oObj.removeActivateListener(listener);
119         log.println("ActiveListener removed");
120         listener.init();
121 
122         XWindow wind = (XWindow)UnoRuntime.queryInterface(XWindow.class, cntrl);
123         wind.setFocus();
124         shortWait();
125         XWindow otherWind = (XWindow)tEnv.getObjRelation("otherWindow");
126         otherWind.setFocus();
127         shortWait();
128         log.println("activated = " + listener.activated +
129             ", deactivated = " + listener.deactivated) ;
130 
131         tRes.tested("removeActivateListener()",
132             !listener.activated && !listener.deactivated);
133     }
134 
135     XControl cntrl;
136 
137     /**
138      * Retrieves current control and searches for it among child controls.
139      *
140      * Has <b>OK</b> status if the current control was found among component
141      * children.
142      */
143     public void _getCurrentControl() {
144         cntrl = oObj.getCurrentControl();
145         XControl[] children = oObj.getControls() ;
146 
147         boolean res = false;
148         for(int i = 0; i < children.length; i++) {
149             if (children[i].equals(cntrl)) {
150                 log.println("Current control is equal to the object control" +
151                     " #" + i + ":");
152                 log.println(cntrl);
153                 res = true;
154                 break;
155             }
156         }
157 
158         tRes.tested("getCurrentControl()", res) ;
159     }
160 
161     /**
162     * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
163     * reset</code> call.
164     */
165     private void shortWait() {
166         try {
167             Thread.sleep(1000) ;
168         } catch (InterruptedException e) {
169             System.out.println("While waiting :" + e) ;
170         }
171     }
172 }
173 
174