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.MenuItem.AccessibleAWTMenuItem;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025:
026: import javax.accessibility.AccessibleAction;
027: import javax.accessibility.AccessibleContext;
028: import javax.accessibility.AccessibleRole;
029: import javax.accessibility.AccessibleValue;
030:
031: import junit.framework.TestCase;
032:
033: /**
034: * AccessibleAWTMenuItemTest
035: */
036: public class AccessibleAWTMenuItemTest extends TestCase {
037: MenuItem item;
038: AccessibleContext ac;
039: AccessibleValue value;
040: AccessibleAction action;
041: ActionEvent event;
042: private Robot robot;
043: private ActionListener actionListener;
044:
045: @Override
046: protected void setUp() throws Exception {
047: super .setUp();
048: item = new MenuItem();
049: ac = item.getAccessibleContext();
050: action = ac.getAccessibleAction();
051: value = ac.getAccessibleValue();
052: robot = new Robot();
053: event = null;
054: actionListener = new ActionListener() {
055: public void actionPerformed(ActionEvent ae) {
056: event = ae;
057: }
058: };
059: }
060:
061: public final void testAccessibleAWTMenuItem() {
062: assertTrue(ac instanceof AccessibleAWTMenuItem);
063: }
064:
065: public final void testGetAccessibleName() {
066: String name = "name";
067: assertEquals(item.getLabel(), ac.getAccessibleName());
068: item.setLabel(name);
069: assertEquals(name, ac.getAccessibleName());
070: String accessibleName = "accessible name";
071: ac.setAccessibleName(accessibleName);
072: assertEquals(accessibleName, ac.getAccessibleName());
073: ac.setAccessibleName(null);
074: assertEquals(name, ac.getAccessibleName());
075: }
076:
077: public final void testGetAccessibleRole() {
078: assertSame(AccessibleRole.MENU_ITEM, ac.getAccessibleRole());
079: }
080:
081: public final void testGetAccessibleAction() {
082: assertSame(ac, ac.getAccessibleAction());
083: }
084:
085: public final void testGetAccessibleValue() {
086: assertSame(ac, ac.getAccessibleValue());
087: }
088:
089: public final void testGetAccessibleActionCount() {
090: assertEquals(1, action.getAccessibleActionCount());
091: }
092:
093: public final void testDoAccessibleAction() {
094: assertFalse(action.doAccessibleAction(1));
095: assertFalse(action.doAccessibleAction(-1));
096: String command = "action";
097: item.setActionCommand(command);
098: item.addActionListener(actionListener);
099: assertNull(event);
100: assertTrue(action.doAccessibleAction(0));
101: waitForAction(); // event is posted to the event queue, so have to wait
102: assertNotNull(event);
103: assertEquals(ActionEvent.ACTION_PERFORMED, event.getID());
104: assertEquals(command, event.getActionCommand());
105: assertEquals(0, event.getModifiers());
106: long delta = System.currentTimeMillis() - event.getWhen();
107: assertTrue(delta >= 0);
108: assertTrue(delta < 1000);
109: }
110:
111: public final void testGetAccessibleActionDescription() {
112: assertNull(action.getAccessibleActionDescription(-1));
113: assertEquals("click", action.getAccessibleActionDescription(0));
114: assertNull(action.getAccessibleActionDescription(1));
115: }
116:
117: public final void testGetCurrentAccessibleValue() {
118: assertEquals(new Integer(0), value.getCurrentAccessibleValue());
119: }
120:
121: public final void testGetMaximumAccessibleValue() {
122: assertEquals(new Integer(0), value.getMaximumAccessibleValue());
123: }
124:
125: public final void testGetMinimumAccessibleValue() {
126: assertEquals(new Integer(0), value.getMinimumAccessibleValue());
127: }
128:
129: public final void testSetCurrentAccessibleValue() {
130: assertFalse(value.setCurrentAccessibleValue(new Integer(-1)));
131: assertEquals(new Integer(0), value.getCurrentAccessibleValue());
132: assertFalse(value.setCurrentAccessibleValue(new Integer(0)));
133: assertFalse(value.setCurrentAccessibleValue(new Integer(1)));
134: assertEquals(new Integer(0), value.getCurrentAccessibleValue());
135: }
136:
137: private void waitForAction() {
138: int time = 0;
139: int timeout = 32;
140: int threshold = 60000;
141: while ((event == null) && (time < threshold)) {
142: robot.delay(timeout);
143: time += timeout;
144: timeout <<= 1;
145: }
146: }
147:
148: }
|