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.Container.AccessibleAWTContainer;
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025:
026: import javax.accessibility.Accessible;
027: import javax.accessibility.AccessibleContext;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * AccessibleAWTContainerTest
033: */
034: @SuppressWarnings("serial")
035: public class AccessibleAWTContainerTest extends TestCase {
036:
037: private AccessibleAWTContainer aContainer;
038: private Container container;
039: private PropertyChangeListener propListener;
040: private PropertyChangeEvent lastPropEvent;
041:
042: private class MyContainer extends Container implements Accessible {
043: AccessibleAWTContainer aac;
044:
045: @Override
046: public AccessibleContext getAccessibleContext() {
047: if (aac == null) {
048: aac = new AccessibleAWTContainer() {
049: };
050: }
051: return aac;
052: }
053:
054: }
055:
056: private class MyComponent extends Component implements Accessible {
057: AccessibleAWTComponent aac;
058:
059: @Override
060: public AccessibleContext getAccessibleContext() {
061: if (aac == null) {
062: aac = new AccessibleAWTComponent() {
063: };
064: }
065: return aac;
066: }
067:
068: }
069:
070: public static void main(String[] args) {
071: junit.textui.TestRunner.run(AccessibleAWTContainerTest.class);
072: }
073:
074: /*
075: * @see TestCase#setUp()
076: */
077: @Override
078: protected void setUp() throws Exception {
079: super .setUp();
080: container = new MyContainer();
081:
082: lastPropEvent = null;
083: aContainer = (AccessibleAWTContainer) container
084: .getAccessibleContext();
085: propListener = new PropertyChangeListener() {
086:
087: public void propertyChange(PropertyChangeEvent pce) {
088: lastPropEvent = pce;
089: }
090:
091: };
092: }
093:
094: /*
095: * @see TestCase#tearDown()
096: */
097: @Override
098: protected void tearDown() throws Exception {
099: super .tearDown();
100: }
101:
102: public final void testGetAccessibleChildrenCount() {
103: assertEquals("no accessible children", 0, aContainer
104: .getAccessibleChildrenCount());
105: container.add(new Component() {
106: });
107: assertEquals("no accessible children", 0, aContainer
108: .getAccessibleChildrenCount());
109: container.add(new MyComponent());
110: assertEquals("1 accessible child", 1, aContainer
111: .getAccessibleChildrenCount());
112: container.add(new MyContainer(), 0);
113: assertEquals("2 accessible children", 2, aContainer
114: .getAccessibleChildrenCount());
115: }
116:
117: public final void testAddPropertyChangeListener() {
118: String propName = AccessibleContext.ACCESSIBLE_CHILD_PROPERTY;
119: aContainer.addPropertyChangeListener(propListener);
120: assertNotNull(aContainer.accessibleContainerHandler);
121: assertSame(container.getContainerListeners()[0],
122: aContainer.accessibleContainerHandler);
123: Component c = new MyComponent();
124: container.add(c);
125: assertNotNull(lastPropEvent);
126: assertEquals(propName, lastPropEvent.getPropertyName());
127: assertNull(lastPropEvent.getOldValue());
128: assertSame(c.getAccessibleContext(), lastPropEvent
129: .getNewValue());
130: lastPropEvent = null;
131: container.remove(c);
132: assertNotNull(lastPropEvent);
133: assertEquals(propName, lastPropEvent.getPropertyName());
134: assertNull(lastPropEvent.getNewValue());
135: assertSame(c.getAccessibleContext(), lastPropEvent
136: .getOldValue());
137: lastPropEvent = null;
138: aContainer.removePropertyChangeListener(propListener);
139: container.add(c);
140: assertNull("listener not called", lastPropEvent);
141: }
142:
143: public final void testGetAccessibleChild() {
144: container.add(new Component() {
145: });
146: Component c = new MyComponent();
147: container.add(c);
148: assertNull(aContainer.getAccessibleChild(-1));
149: assertSame(c, aContainer.getAccessibleChild(0));
150: assertNull(aContainer.getAccessibleChild(1));
151: }
152:
153: @SuppressWarnings("deprecation")
154: public final void testGetAccessibleAt() {
155: container.setSize(100, 100);
156:
157: Point p = null;
158: assertSame(container, aContainer.getAccessibleAt(p));
159:
160: p = new Point();
161: assertSame(container, aContainer.getAccessibleAt(p));
162:
163: Component c1 = new MyComponent();
164: Component c2 = new MyComponent();
165: Component c = new Component() {
166: };
167: Container cont1 = new MyContainer();
168: Component c3 = new MyComponent();
169: c3.setSize(13, 13);
170: c.setSize(40, 40);
171: c1.setBounds(3, 3, 10, 20);
172: c2.setSize(20, 20);
173: cont1.setBounds(27, 27, 60, 56);
174: cont1.add(c3);
175: container.add(c);
176: container.add(c2);
177: container.add(cont1);
178: container.add(c1);
179:
180: p.setLocation(2, 2);
181: assertSame(container, aContainer.getAccessibleAt(p));
182: Frame f = new Frame();
183: f.add(container);
184: f.setSize(100, 300);
185: f.show();
186:
187: try {
188: assertSame(c2, aContainer.getAccessibleAt(p));
189: p.translate(cont1.getX(), cont1.getY());
190:
191: assertSame(cont1, aContainer.getAccessibleAt(p));
192: p = new Point();
193: assertSame(c3, cont1.getAccessibleContext()
194: .getAccessibleComponent().getAccessibleAt(p));
195: } finally {
196: f.dispose();
197: }
198: }
199:
200: public final void testAccessibleAWTContainer() {
201: assertNotNull(aContainer);
202: assertNull(aContainer.accessibleContainerHandler);
203: }
204:
205: }
|