001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 07.10.2004
021:
022: */package javax.swing;
023:
024: import java.awt.event.FocusEvent;
025: import java.awt.event.FocusListener;
026: import java.lang.reflect.InvocationTargetException;
027: import javax.accessibility.AccessibleContext;
028: import javax.accessibility.AccessibleState;
029:
030: public class AccessibleJComponent_MultithreadedTest extends
031: BasicSwingTestCase {
032: protected JComponent panel;
033:
034: protected JFrame frame;
035:
036: protected JComponent.AccessibleJComponent aContext;
037:
038: protected class ConcreteFocusListener implements FocusListener {
039: public boolean state = false;
040:
041: public void focusGained(final FocusEvent arg0) {
042: state = true;
043: }
044:
045: public void focusLost(final FocusEvent arg0) {
046: state = true;
047: }
048: };
049:
050: protected class SynchronizedPropertyListener extends
051: PropertyChangeController {
052: private static final long serialVersionUID = 1L;
053:
054: public final Object lock = new Object();
055: }
056:
057: /*
058: * @see TestCase#setUp()
059: */
060: @Override
061: protected void setUp() throws Exception {
062: super .setUp();
063: frame = new JFrame();
064: panel = new JPanel();
065: aContext = (JComponent.AccessibleJComponent) panel
066: .getAccessibleContext();
067: }
068:
069: @Override
070: protected void tearDown() throws Exception {
071: panel = null;
072: aContext = null;
073: if (frame != null) {
074: frame.dispose();
075: frame = null;
076: }
077: super .tearDown();
078: }
079:
080: protected void waitListener(
081: final SynchronizedPropertyListener listener) {
082: synchronized (listener.lock) {
083: if (!listener.isChanged()) {
084: try {
085: listener.lock.wait(1000);
086: } catch (InterruptedException e) {
087: fail(e.getMessage());
088: }
089: }
090: }
091: }
092:
093: /*
094: * Class under test for void removePropertyChangeListener(PropertyChangeListener)
095: */
096: @SuppressWarnings("deprecation")
097: public void testRemovePropertyChangeListenerPropertyChangeListener()
098: throws InterruptedException, InvocationTargetException {
099: SynchronizedPropertyListener listener1 = new SynchronizedPropertyListener();
100: SynchronizedPropertyListener listener2 = new SynchronizedPropertyListener();
101: aContext.addPropertyChangeListener(listener1);
102: JComponent button1 = new JPanel();
103: JComponent button2 = new JPanel();
104: panel.add(button1);
105: waitListener(listener1);
106: listener1.checkLastPropertyFired(aContext,
107: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, null,
108: button1.getAccessibleContext());
109: listener1.reset();
110: aContext.addPropertyChangeListener(listener2);
111: aContext.removePropertyChangeListener(listener1);
112: panel.add(button2);
113: waitListener(listener2);
114: assertFalse(listener1.isChanged());
115: listener2.checkLastPropertyFired(aContext,
116: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, null,
117: button2.getAccessibleContext());
118: listener1.reset();
119: listener2.reset();
120: ConcreteFocusListener focusListener = new ConcreteFocusListener();
121: AccessibleContext bContext = button1.getAccessibleContext();
122: bContext.addPropertyChangeListener(listener1);
123: frame.getContentPane().add(panel);
124: panel.add(button2);
125: button1.addFocusListener(focusListener);
126: listener1.reset();
127: frame.pack();
128: frame.show();
129: SwingWaitTestCase.requestFocusInWindowForComponent(button1);
130: listener1.checkLastPropertyFired(
131: button1.getAccessibleContext(),
132: AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null,
133: AccessibleState.FOCUSED);
134: listener1.reset();
135: bContext.removePropertyChangeListener(listener1);
136: SwingWaitTestCase.requestFocusInWindowForComponent(button2);
137: assertFalse(listener1.isChanged());
138: }
139:
140: /*
141: * Class under test for void addPropertyChangeListener(PropertyChangeListener)
142: */
143: @SuppressWarnings("deprecation")
144: public void testAddPropertyChangeListenerPropertyChangeListener()
145: throws InterruptedException, InvocationTargetException {
146: ConcreteFocusListener focusListener = new ConcreteFocusListener();
147: SynchronizedPropertyListener listener = new SynchronizedPropertyListener();
148: aContext.addPropertyChangeListener(listener);
149: JComponent button1 = new JPanel();
150: JComponent button2 = new JPanel();
151: JComponent button3 = new JPanel();
152: panel.add(button1);
153: waitListener(listener);
154: listener.checkLastPropertyFired(aContext,
155: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, null,
156: button1.getAccessibleContext());
157: listener.reset();
158: panel.add(button2);
159: waitListener(listener);
160: listener.checkLastPropertyFired(aContext,
161: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, null,
162: button2.getAccessibleContext());
163: listener.reset();
164: panel.remove(button1);
165: waitListener(listener);
166: listener.checkLastPropertyFired(aContext,
167: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, button1
168: .getAccessibleContext(), null);
169: listener.reset();
170: panel.add(button1);
171: AccessibleContext bContext = button1.getAccessibleContext();
172: bContext.addPropertyChangeListener(listener);
173: frame.getContentPane().add(panel);
174: panel.add(button2);
175: button1.addFocusListener(focusListener);
176: listener.reset();
177: frame.pack();
178: frame.show();
179: SwingWaitTestCase.requestFocusInWindowForComponent(button1);
180: listener.checkLastPropertyFired(button1.getAccessibleContext(),
181: AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null,
182: AccessibleState.FOCUSED);
183: listener.reset();
184: SwingWaitTestCase.requestFocusInWindowForComponent(button2);
185: listener.checkLastPropertyFired(button1.getAccessibleContext(),
186: AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
187: AccessibleState.FOCUSED, null);
188: listener.reset();
189: panel.add(button3);
190: waitListener(listener);
191: listener.checkLastPropertyFired(aContext,
192: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, null,
193: button3.getAccessibleContext());
194: listener.reset();
195: panel.remove(button2);
196: waitListener(listener);
197: listener.checkLastPropertyFired(aContext,
198: AccessibleContext.ACCESSIBLE_CHILD_PROPERTY, button2
199: .getAccessibleContext(), null);
200: listener.reset();
201: SwingWaitTestCase.requestFocusInWindowForComponent(button1);
202: listener.checkLastPropertyFired(button1.getAccessibleContext(),
203: AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null,
204: AccessibleState.FOCUSED);
205: }
206:
207: /*
208: * Class under test for AccessibleStateSet getAccessibleStateSet()
209: */
210: @SuppressWarnings("deprecation")
211: public void testGetAccessibleStateSet()
212: throws InterruptedException, InvocationTargetException {
213: assertTrue("Enabled", aContext.getAccessibleStateSet()
214: .contains(AccessibleState.ENABLED));
215: assertFalse("Focused", aContext.getAccessibleStateSet()
216: .contains(AccessibleState.FOCUSED));
217: assertTrue("Focusable", aContext.getAccessibleStateSet()
218: .contains(AccessibleState.FOCUSABLE));
219: assertTrue("Visible", aContext.getAccessibleStateSet()
220: .contains(AccessibleState.VISIBLE));
221: assertFalse("Showing", aContext.getAccessibleStateSet()
222: .contains(AccessibleState.SHOWING));
223: assertTrue("Opaque", aContext.getAccessibleStateSet().contains(
224: AccessibleState.OPAQUE));
225: assertEquals("Number of states", 4, aContext
226: .getAccessibleStateSet().toArray().length);
227: frame.getContentPane().add(panel);
228: frame.pack();
229: frame.show();
230: SwingWaitTestCase.requestFocusInWindowForComponent(panel);
231: assertTrue("Enabled", aContext.getAccessibleStateSet()
232: .contains(AccessibleState.ENABLED));
233: assertTrue("Focused", aContext.getAccessibleStateSet()
234: .contains(AccessibleState.FOCUSED));
235: assertTrue("Focusable", aContext.getAccessibleStateSet()
236: .contains(AccessibleState.FOCUSABLE));
237: assertTrue("Visible", aContext.getAccessibleStateSet()
238: .contains(AccessibleState.VISIBLE));
239: assertTrue("Showing", aContext.getAccessibleStateSet()
240: .contains(AccessibleState.SHOWING));
241: assertTrue("Opaque", aContext.getAccessibleStateSet().contains(
242: AccessibleState.OPAQUE));
243: assertEquals("Number of states", 6, aContext
244: .getAccessibleStateSet().toArray().length);
245: panel.setVisible(false);
246: panel.setOpaque(false);
247: panel.setEnabled(false);
248: JButton button = new JButton();
249: frame.getContentPane().add(button);
250: SwingWaitTestCase.requestFocusInWindowForComponent(button);
251: assertFalse("Enabled", aContext.getAccessibleStateSet()
252: .contains(AccessibleState.ENABLED));
253: assertFalse("Focused", aContext.getAccessibleStateSet()
254: .contains(AccessibleState.FOCUSED));
255: assertTrue("Focusable", aContext.getAccessibleStateSet()
256: .contains(AccessibleState.FOCUSABLE));
257: assertFalse("Visible", aContext.getAccessibleStateSet()
258: .contains(AccessibleState.VISIBLE));
259: assertFalse("Showing", aContext.getAccessibleStateSet()
260: .contains(AccessibleState.SHOWING));
261: assertFalse("Opaque", aContext.getAccessibleStateSet()
262: .contains(AccessibleState.OPAQUE));
263: assertEquals("Number of states", 1, aContext
264: .getAccessibleStateSet().toArray().length);
265: }
266: }
|