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.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024:
025: import javax.accessibility.AccessibleContext;
026: import javax.accessibility.AccessibleRole;
027: import javax.accessibility.AccessibleState;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * AccessibleAWTCanvas
033: */
034: public class AccessibleAWTCanvasTest extends TestCase {
035: private Canvas canvas;
036: private PropertyChangeListener propListener;
037: private PropertyChangeEvent lastPropEvent;
038: private Robot robot;
039:
040: @Override
041: protected void setUp() throws Exception {
042: super .setUp();
043: robot = new Robot();
044: canvas = new Canvas();
045: lastPropEvent = null;
046: propListener = new PropertyChangeListener() {
047:
048: public void propertyChange(PropertyChangeEvent pce) {
049: lastPropEvent = pce;
050: }
051:
052: };
053: }
054:
055: public final void testAccessibleAWTCanvas() {
056: assertNotNull(canvas);
057: }
058:
059: public final void testGetAccessibleRole() {
060: assertSame(AccessibleRole.CANVAS, canvas.getAccessibleContext()
061: .getAccessibleRole());
062: }
063:
064: public final void testAddPropertyChangeListener() {
065: String propName = AccessibleContext.ACCESSIBLE_STATE_PROPERTY;
066: canvas.getAccessibleContext().addPropertyChangeListener(
067: propListener);
068: assertNull(lastPropEvent);
069: Frame f = new Frame();
070: f.add(canvas);
071: canvas.setFocusable(true); //!
072: f.setVisible(true);
073: waitForEvent();
074:
075: // focus events:
076: assertNotNull(lastPropEvent);
077: assertEquals(propName, lastPropEvent.getPropertyName());
078: assertEquals(AccessibleState.FOCUSED, lastPropEvent
079: .getNewValue());
080: assertNull(lastPropEvent.getOldValue());
081:
082: // component events:
083: lastPropEvent = null;
084: canvas.setVisible(false);
085: waitForEvent();
086: assertNotNull(lastPropEvent);
087: assertEquals(propName, lastPropEvent.getPropertyName());
088: assertEquals(AccessibleState.VISIBLE, lastPropEvent
089: .getOldValue());
090: assertNull(lastPropEvent.getNewValue());
091: f.dispose();
092: }
093:
094: private void waitForEvent() {
095: int time = 0;
096: int timeout = 16;
097: int threshold = 60000;
098: while ((lastPropEvent == null) && (time < threshold)) {
099: robot.delay(timeout);
100: time += timeout;
101: timeout <<= 1;
102: }
103: }
104: }
|