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 javax.accessibility.AccessibleContext;
027: import javax.accessibility.AccessibleRole;
028: import javax.swing.border.Border;
029: import javax.swing.border.CompoundBorder;
030: import javax.swing.border.EmptyBorder;
031: import javax.swing.border.TitledBorder;
032:
033: public class AccessibleJComponentTest extends SwingTestCase {
034: protected class ConcreteFocusListener implements FocusListener {
035: public boolean state = false;
036:
037: public void focusGained(final FocusEvent arg0) {
038: state = true;
039: }
040:
041: public void focusLost(final FocusEvent arg0) {
042: state = true;
043: }
044: };
045:
046: protected JComponent panel;
047:
048: protected JComponent.AccessibleJComponent aContext;
049:
050: /*
051: * @see TestCase#setUp()
052: */
053: @Override
054: protected void setUp() throws Exception {
055: super .setUp();
056: panel = new JComponent() {
057: private static final long serialVersionUID = 1L;
058:
059: @Override
060: public AccessibleContext getAccessibleContext() {
061: return new AccessibleJComponent() {
062: private static final long serialVersionUID = 1L;
063: };
064: }
065: };
066: aContext = (JComponent.AccessibleJComponent) panel
067: .getAccessibleContext();
068: }
069:
070: public void testGetAccessibleChildrenCount() {
071: assertEquals(aContext.getAccessibleChildrenCount(), 0);
072: panel.add(new JPanel());
073: panel.add(new JPanel());
074: panel.add(new JPanel());
075: assertEquals(aContext.getAccessibleChildrenCount(), 3);
076: }
077:
078: /*
079: * Class under test for AccessibleRole getAccessibleRole()
080: */
081: public void testGetAccessibleRole() {
082: assertTrue(aContext.getAccessibleRole().equals(
083: AccessibleRole.SWING_COMPONENT));
084: }
085:
086: public void testGetAccessibleKeyBinding() {
087: assertNull(aContext.getAccessibleKeyBinding());
088: }
089:
090: /*
091: * Class under test for Accessible getAccessibleChild(int)
092: */
093: public void testGetAccessibleChildint() {
094: JPanel panel1 = new JPanel();
095: JPanel panel2 = new JPanel();
096: JPanel panel3 = new JPanel();
097: assertNull(aContext.getAccessibleChild(0));
098: panel.add(panel1);
099: panel.add(panel2);
100: panel.add(panel3);
101: assertSame(aContext.getAccessibleChild(0), panel1);
102: assertSame(aContext.getAccessibleChild(1), panel2);
103: assertSame(aContext.getAccessibleChild(2), panel3);
104: }
105:
106: public void testGetToolTipText() {
107: panel.setToolTipText("text");
108: assertNull(aContext.getToolTipText());
109: panel = new JButton();
110: aContext = (JComponent.AccessibleJComponent) panel
111: .getAccessibleContext();
112: String text = "text";
113: panel.setToolTipText(text);
114: assertTrue(aContext.getToolTipText().equals(text));
115: }
116:
117: public void testGetTitledBorderText() {
118: String title1 = "title1";
119: String title2 = "title2";
120: assertNull(aContext.getTitledBorderText());
121: panel.setBorder(new TitledBorder(title1));
122: assertEquals(aContext.getTitledBorderText(), title1);
123: panel.setBorder(new CompoundBorder(new TitledBorder(title1),
124: new TitledBorder(title2)));
125: assertNull(aContext.getTitledBorderText());
126: }
127:
128: public void testGetBorderTitle() {
129: String title1 = "title1";
130: String title2 = "title2";
131: String title3 = "title3";
132: String title4 = "title3";
133: Border border = null;
134: assertNull(aContext.getBorderTitle(border));
135: border = new TitledBorder(title1);
136: assertNotNull(aContext.getBorderTitle(border));
137: assertEquals(aContext.getBorderTitle(border), title1);
138: border = new CompoundBorder(new TitledBorder(title1),
139: new EmptyBorder(1, 1, 1, 1));
140: assertNotNull(aContext.getBorderTitle(border));
141: assertEquals(aContext.getBorderTitle(border), title1);
142: border = new CompoundBorder(new EmptyBorder(1, 1, 1, 1),
143: new TitledBorder(title2));
144: assertNotNull(aContext.getBorderTitle(border));
145: assertEquals(aContext.getBorderTitle(border), title2);
146: border = new CompoundBorder(new TitledBorder(title1),
147: new TitledBorder(title2));
148: assertNotNull(aContext.getBorderTitle(border));
149: assertEquals(aContext.getBorderTitle(border), title2);
150: border = new CompoundBorder(new CompoundBorder(
151: new TitledBorder(title1), new TitledBorder(title2)),
152: new CompoundBorder(new TitledBorder(title3),
153: new TitledBorder(title4)));
154: assertNotNull(aContext.getBorderTitle(border));
155: assertEquals(aContext.getBorderTitle(border), title4);
156: border = new CompoundBorder(new CompoundBorder(
157: new TitledBorder(title1), new TitledBorder(title2)),
158: new CompoundBorder(new TitledBorder(title3),
159: new EmptyBorder(1, 1, 1, 1)));
160: assertNotNull(aContext.getBorderTitle(border));
161: assertEquals(aContext.getBorderTitle(border), title3);
162: border = new CompoundBorder(new CompoundBorder(
163: new TitledBorder(title1), new TitledBorder(title2)),
164: new CompoundBorder(new EmptyBorder(1, 1, 1, 1),
165: new EmptyBorder(1, 1, 1, 1)));
166: assertNotNull(aContext.getBorderTitle(border));
167: assertEquals(aContext.getBorderTitle(border), title2);
168: border = new CompoundBorder(new CompoundBorder(new EmptyBorder(
169: 1, 1, 1, 1), new TitledBorder(title2)),
170: new CompoundBorder(new EmptyBorder(1, 1, 1, 1),
171: new EmptyBorder(1, 1, 1, 1)));
172: assertNotNull(aContext.getBorderTitle(border));
173: assertEquals(aContext.getBorderTitle(border), title2);
174: border = new CompoundBorder(new CompoundBorder(
175: new TitledBorder(title1), new EmptyBorder(1, 1, 1, 1)),
176: new CompoundBorder(new EmptyBorder(1, 1, 1, 1),
177: new EmptyBorder(1, 1, 1, 1)));
178: assertNotNull(aContext.getBorderTitle(border));
179: assertEquals(aContext.getBorderTitle(border), title1);
180: }
181:
182: /*
183: * Class under test for String getAccessibleName()
184: */
185: public void testGetAccessibleName() {
186: String text1 = "text1";
187: String text2 = "text2";
188: assertNull(aContext.getAccessibleName());
189: panel.setName(text2);
190: assertNull(aContext.getAccessibleName());
191: aContext.setAccessibleName(text1);
192: assertEquals(aContext.getAccessibleName(), text1);
193: }
194:
195: /*
196: * Class under test for String getAccessibleDescription()
197: */
198: public void testGetAccessibleDescription() {
199: String text1 = "text1";
200: String text2 = "text2";
201: panel = new JPanel();
202: aContext = (JComponent.AccessibleJComponent) panel
203: .getAccessibleContext();
204: assertNull(aContext.getAccessibleDescription());
205: aContext.setAccessibleDescription(text2);
206: assertTrue(aContext.getAccessibleDescription().equals(text2));
207: panel.setToolTipText(text1);
208: assertTrue(aContext.getAccessibleDescription() != null);
209: assertTrue(aContext.getAccessibleDescription().equals(text2));
210: }
211: }
|