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 Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.Component.AccessibleAWTComponent;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.util.Locale;
028:
029: import javax.accessibility.Accessible;
030: import javax.accessibility.AccessibleContext;
031: import javax.accessibility.AccessibleRole;
032: import javax.accessibility.AccessibleState;
033: import javax.accessibility.AccessibleStateSet;
034:
035: import junit.framework.TestCase;
036:
037: /**
038: * AccessibleAWTComponentTest
039: */
040: @SuppressWarnings("serial")
041: public class AccessibleAWTComponentTest extends TestCase {
042:
043: private class MyComponent extends Component implements Accessible {
044: @Override
045: public void requestFocus() {
046: methodCalled = true;
047: super .requestFocus();
048: }
049: }
050:
051: private AccessibleAWTComponent aComponent;
052: private Component comp;
053: private PropertyChangeListener propListener;
054: private FocusListener focusListener;
055: private PropertyChangeEvent lastPropEvent;
056: private FocusEvent lastFocusEvent;
057: protected boolean methodCalled;
058:
059: @Override
060: protected void setUp() throws Exception {
061: super .setUp();
062: comp = new MyComponent();
063: assertNull("accessible context is null", comp
064: .getAccessibleContext());
065: lastPropEvent = null;
066: lastFocusEvent = null;
067: methodCalled = false;
068: aComponent = comp.new AccessibleAWTComponent() {
069: };
070: propListener = new PropertyChangeListener() {
071:
072: public void propertyChange(PropertyChangeEvent pce) {
073: lastPropEvent = pce;
074: }
075:
076: };
077: focusListener = new FocusListener() {
078:
079: public void focusGained(FocusEvent fe) {
080: lastFocusEvent = fe;
081: }
082:
083: public void focusLost(FocusEvent fe) {
084: lastFocusEvent = fe;
085: }
086:
087: };
088: }
089:
090: public final void testGetAccessibleChildrenCount() {
091: assertEquals("No accessible children", 0, aComponent
092: .getAccessibleChildrenCount());
093: }
094:
095: public final void testGetAccessibleIndexInParent() {
096: assertEquals(-1, aComponent.getAccessibleIndexInParent());
097: Container parent = new Panel(); // parent must be accessible
098: parent.add(new Component() {
099: });
100: parent.add(new MyComponent()); // child must also be accessible
101: parent.add(comp);
102: assertEquals(1, aComponent.getAccessibleIndexInParent());
103: parent = new Container() {
104: };
105: parent.add(comp);
106: assertEquals(-1, aComponent.getAccessibleIndexInParent());
107:
108: }
109:
110: public final void testAddRemovePropertyChangeListener() {
111: aComponent.addPropertyChangeListener(propListener);
112: assertEquals(0, comp.getPropertyChangeListeners().length);
113: assertEquals(0,
114: comp.getListeners(PropertyChangeListener.class).length);
115: // handlers are created only if property change listener is present
116: assertNotNull(aComponent.accessibleAWTComponentHandler);
117: assertNotNull(aComponent.accessibleAWTFocusHandler);
118: // they're also added to component's listeners
119: assertSame(aComponent.accessibleAWTFocusHandler, comp
120: .getFocusListeners()[0]);
121: assertSame(aComponent.accessibleAWTComponentHandler, comp
122: .getComponentListeners()[0]);
123: assertNull(lastPropEvent);
124: String name = "component name";
125: aComponent.setAccessibleName(name);
126: assertNotNull("property listener called", lastPropEvent);
127: assertEquals("property name is correct",
128: AccessibleContext.ACCESSIBLE_NAME_PROPERTY,
129: lastPropEvent.getPropertyName());
130: assertEquals(name, lastPropEvent.getNewValue());
131: assertNull(lastPropEvent.getOldValue());
132: // verify handlers:
133:
134: aComponent.removePropertyChangeListener(propListener);
135: // handlers & listeners are removed, if last property listener is removed
136: assertNull(aComponent.accessibleAWTComponentHandler);
137: assertNull(aComponent.accessibleAWTFocusHandler);
138: assertEquals(0, comp.getFocusListeners().length);
139: assertEquals(0, comp.getComponentListeners().length);
140: lastPropEvent = null;
141: aComponent.setAccessibleName(name = "");
142: assertNull(lastPropEvent);
143:
144: }
145:
146: public final void testGetLocale() {
147: new Frame().add(comp);
148: assertEquals(comp.getLocale(), aComponent.getLocale());
149: comp.setLocale(Locale.FRANCE);
150: assertEquals("component's locale is used", comp.getLocale(),
151: aComponent.getLocale());
152: }
153:
154: public final void testGetAccessibleParent() {
155: assertNull(aComponent.getAccessibleParent());
156: Container parent = new Panel(); // accessible parent
157: parent.add(comp);
158: assertSame(parent, aComponent.getAccessibleParent());
159: parent.remove(comp); // no parent
160: assertNull(aComponent.getAccessibleParent());
161: parent = new Container() {
162: }; // non-accessible parent
163: parent.add(comp);
164: assertNull(aComponent.getAccessibleParent());
165: Accessible aParent = new Panel();
166: aComponent.setAccessibleParent(aParent);
167: assertSame(aParent, aComponent.getAccessibleParent());
168: new Panel().add(comp);
169: assertSame(aParent, aComponent.getAccessibleParent());
170: }
171:
172: public final void testGetAccessibleChild() {
173: assertNull(aComponent.getAccessibleChild(-5));
174: assertNull(aComponent.getAccessibleChild(0));
175: assertNull(aComponent.getAccessibleChild(100));
176: }
177:
178: public final void testGetAccessibleComponent() {
179: assertSame(aComponent, aComponent.getAccessibleComponent());
180: }
181:
182: public final void testGetAccessibleDescription() {
183: assertNull(aComponent.getAccessibleDescription());
184: aComponent.setAccessibleDescription("q");
185: assertEquals("q", aComponent.getAccessibleDescription());
186: }
187:
188: public final void testGetAccessibleRole() {
189: assertSame("accessible role is correct",
190: AccessibleRole.AWT_COMPONENT, aComponent
191: .getAccessibleRole());
192: }
193:
194: public final void testGetAccessibleStateSet() {
195: AccessibleStateSet ass = aComponent.getAccessibleStateSet();
196: assertNotNull(ass);
197: assertFalse(ass.contains(AccessibleState.ACTIVE));
198: assertTrue(ass.contains(AccessibleState.ENABLED));
199: assertTrue(ass.contains(AccessibleState.FOCUSABLE));
200: assertTrue(ass.contains(AccessibleState.VISIBLE));
201: assertFalse(ass.contains(AccessibleState.FOCUSED));
202: assertFalse(ass.contains(AccessibleState.OPAQUE));
203: assertFalse(ass.contains(AccessibleState.SHOWING));
204: }
205:
206: public final void testAccessibleAWTComponent() {
207: assertNotNull(aComponent);
208: assertNull(aComponent.accessibleAWTComponentHandler);
209: assertNull(aComponent.accessibleAWTFocusHandler);
210: }
211:
212: public final void testAddRemoveFocusListener() {
213: assertEquals(0, comp.getFocusListeners().length);
214: aComponent.addFocusListener(focusListener);
215: FocusListener[] listeners = comp.getFocusListeners();
216: assertEquals(1, listeners.length);
217: assertSame(focusListener, listeners[0]);
218: assertNull(lastFocusEvent);
219: comp
220: .processEvent(new FocusEvent(comp,
221: FocusEvent.FOCUS_GAINED));
222: assertNotNull("focus listener called", lastFocusEvent);
223: lastFocusEvent = null;
224: aComponent.removeFocusListener(null);
225: listeners = comp.getFocusListeners();
226: assertSame(focusListener, listeners[0]);
227: comp.processEvent(new FocusEvent(comp, FocusEvent.FOCUS_LOST));
228: assertNotNull("focus listener called", lastFocusEvent);
229: lastFocusEvent = null;
230: aComponent.removeFocusListener(focusListener);
231: listeners = comp.getFocusListeners();
232: comp.processEvent(new FocusEvent(comp, FocusEvent.FOCUS_LOST,
233: true));
234: assertEquals(0, listeners.length);
235: assertNull("listener not called", lastFocusEvent);
236:
237: }
238:
239: public final void testContains() {
240: assertFalse(aComponent.contains(new Point()));
241: comp.setSize(15, 20);
242: assertTrue(aComponent.contains(new Point(5, 19)));
243: assertFalse(aComponent.contains(new Point(5, 20)));
244: assertFalse(aComponent.contains(new Point(0, -5)));
245: }
246:
247: public final void testGetAccessibleAt() {
248: comp.setSize(15, 20);
249: Point p = new Point(5, 5);
250: assertNull(aComponent.getAccessibleAt(p));
251: }
252:
253: public final void testGetBackground() {
254: assertEquals(comp.getBackground(), aComponent.getBackground());
255: comp.setBackground(Color.CYAN);
256: assertEquals(comp.getBackground(), aComponent.getBackground());
257: }
258:
259: public final void testGetBounds() {
260: Rectangle r = new Rectangle();
261: assertEquals(r, aComponent.getBounds());
262: r.setBounds(5, 6, 7, 8);
263: comp.setBounds(r);
264: assertEquals(r, aComponent.getBounds());
265: assertNotSame(r, aComponent.getBounds());
266: }
267:
268: public final void testGetCursor() {
269: Cursor cursor = Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR);
270: comp.setCursor(cursor);
271: assertEquals(cursor, aComponent.getCursor());
272: }
273:
274: public final void testGetFont() {
275: Font font = Font.decode("Arial");
276: comp.setFont(font);
277: assertEquals(font, aComponent.getFont());
278: }
279:
280: public final void testGetFontMetrics() {
281: Frame f = new Frame();
282: f.add(comp);
283: f.addNotify();
284: Font font = comp.getFont();
285: assertSame(comp.getFontMetrics(font), aComponent
286: .getFontMetrics(font));
287: f.dispose();
288: }
289:
290: public final void testGetForeground() {
291: assertEquals(comp.getForeground(), aComponent.getForeground());
292: comp.setForeground(Color.BLUE);
293: assertEquals(comp.getForeground(), aComponent.getForeground());
294: }
295:
296: public final void testGetLocation() {
297: assertEquals(comp.getLocation(), aComponent.getLocation());
298: Point loc = new Point(50, 75);
299: comp.setLocation(loc);
300: assertEquals(loc, aComponent.getLocation());
301: assertNotSame(loc, aComponent.getLocation());
302: }
303:
304: @SuppressWarnings("deprecation")
305: public final void testGetLocationOnScreen() {
306: Frame f = new Frame();
307: f.setLayout(null);
308: f.add(comp);
309: Point p = new Point(50, 50);
310: f.setLocation(p);
311: f.show();
312: assertEquals(p, aComponent.getLocationOnScreen());
313: f.dispose();
314: }
315:
316: public final void testGetSize() {
317: assertEquals(comp.getSize(), aComponent.getSize());
318: Dimension size = new Dimension(30, 40);
319: comp.setSize(size);
320: assertEquals(size, aComponent.getSize());
321: assertNotSame(size, aComponent.getSize());
322: }
323:
324: public final void testIsEnabled() {
325: assertTrue(aComponent.isEnabled());
326: comp.setEnabled(false);
327: assertFalse(aComponent.isEnabled());
328: comp.setEnabled(true);
329: assertTrue(aComponent.isEnabled());
330: }
331:
332: public final void testIsFocusTraversable() {
333: assertTrue(aComponent.isFocusTraversable());
334: comp.setFocusable(false);
335: assertFalse(aComponent.isFocusTraversable());
336: comp.setFocusable(true);
337: assertTrue(aComponent.isFocusTraversable());
338: }
339:
340: @SuppressWarnings("deprecation")
341: public final void testIsShowing() {
342: assertFalse(aComponent.isShowing());
343: Frame f = new Frame();
344: f.add(comp);
345: assertFalse(aComponent.isShowing());
346: f.show();
347: assertTrue(aComponent.isShowing());
348: f.dispose();
349: assertFalse(aComponent.isShowing());
350: }
351:
352: public final void testIsVisible() {
353: assertTrue(aComponent.isVisible());
354: comp.setVisible(false);
355: assertFalse(aComponent.isVisible());
356: comp.setVisible(true);
357: assertTrue(aComponent.isVisible());
358: }
359:
360: public final void testRequestFocus() {
361: aComponent.requestFocus();
362: assertTrue("requestFocus() is delegated to Component",
363: methodCalled);
364: }
365:
366: public final void testSetBackground() {
367: Color color = Color.DARK_GRAY;
368: assertFalse(comp.isBackgroundSet());
369: aComponent.setBackground(color);
370: assertEquals(color, aComponent.getBackground());
371: assertTrue("setBackground() is delegated to Component", comp
372: .isBackgroundSet());
373: assertEquals(color, comp.getBackground());
374: }
375:
376: public final void testSetBounds() {
377: Rectangle bounds = new Rectangle(1, 2, 3, 4);
378: assertEquals(new Rectangle(), comp.getBounds());
379: aComponent.setBounds(bounds);
380: assertEquals(bounds, aComponent.getBounds());
381: assertEquals("setBounds() is delegated to Component", bounds,
382: comp.getBounds());
383: }
384:
385: public final void testSetCursor() {
386: Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
387: assertFalse(comp.isCursorSet());
388: aComponent.setCursor(cursor);
389: assertSame(cursor, aComponent.getCursor());
390: assertTrue("setCursor() is delegated to Component", comp
391: .isCursorSet());
392: assertSame(cursor, comp.getCursor());
393: }
394:
395: public final void testSetEnabled() {
396: assertTrue(comp.isEnabled());
397: aComponent.setEnabled(false);
398: assertFalse(aComponent.isEnabled());
399: assertFalse("setEnabled() is delegated to Component", comp
400: .isEnabled());
401: }
402:
403: public final void testSetFont() {
404: Font font = Font.decode(null);
405: assertFalse(comp.isFontSet());
406: aComponent.setFont(font);
407: assertSame(font, aComponent.getFont());
408: assertTrue("setFont() is delegated to Component", comp
409: .isFontSet());
410: assertSame(font, comp.getFont());
411: }
412:
413: public final void testSetForeground() {
414: Color color = Color.GREEN;
415: assertFalse(comp.isForegroundSet());
416: aComponent.setForeground(color);
417: assertEquals(color, aComponent.getForeground());
418: assertTrue("setForeground() is delegated to Component", comp
419: .isForegroundSet());
420: assertEquals(color, comp.getForeground());
421: }
422:
423: public final void testSetLocation() {
424: Point location = new Point(1, 2);
425: assertEquals(new Point(), comp.getLocation());
426: aComponent.setLocation(location);
427: assertEquals(location, aComponent.getLocation());
428: assertEquals("setLocation() is delegated to Component",
429: location, comp.getLocation());
430: }
431:
432: public final void testSetSize() {
433: Dimension size = new Dimension(3, 4);
434: assertEquals(new Dimension(), comp.getSize());
435: aComponent.setSize(size);
436: assertEquals(size, aComponent.getSize());
437: assertEquals("setSize() is delegated to Component", size, comp
438: .getSize());
439: }
440:
441: public final void testSetVisible() {
442: assertTrue(comp.isVisible());
443: aComponent.setVisible(false);
444: assertFalse(aComponent.isVisible());
445: assertFalse("setVisible() is delegated to Component", comp
446: .isVisible());
447: }
448:
449: }
|