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: package java.awt;
018:
019: import java.awt.event.KeyEvent;
020: import java.util.Set;
021:
022: import junit.framework.TestCase;
023:
024: @SuppressWarnings("serial")
025: public class KeyboardFocusManagerRTest extends TestCase {
026: Robot robot;
027: Frame f;
028: KeyboardFocusManager kfm;
029:
030: public static void main(String[] args) {
031: junit.textui.TestRunner.run(KeyboardFocusManagerRTest.class);
032: }
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: robot = new Robot();
038: f = new Frame();
039: kfm = new DefaultKeyboardFocusManager();
040: }
041:
042: @Override
043: protected void tearDown() throws Exception {
044: super .tearDown();
045: if (f != null) {
046: f.dispose();
047: }
048: }
049:
050: public final void testRedispatchEvent() {
051: Component c1 = new Component() {
052: };
053: Component c2 = new Component() {
054: };
055: Panel p = new Panel();
056: c1.setName("comp1");
057: c2.setName("comp2");
058: c1.setFocusable(true);
059: c2.setFocusable(true);
060: p.add(c1);
061: p.add(c2);
062: f.add(p);
063: f.setSize(100, 100);
064: f.setVisible(true);
065: waitFocus(c1);
066: kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
067: assertSame(c1, kfm.getFocusOwner());
068: KeyEvent ke = new KeyEvent(c1, KeyEvent.KEY_PRESSED, 0l, 0,
069: KeyEvent.VK_TAB, KeyEvent.CHAR_UNDEFINED);
070: kfm.redispatchEvent(c1, ke);
071: int timeout = 10;
072: while (timeout-- > 0 && c2 != kfm.getFocusOwner()) {
073: robot.delay(200);
074: }
075: assertSame(c2, kfm.getFocusOwner());
076: }
077:
078: private void waitFocus(Component comp) {
079: int time = 0;
080: int timeout = 32;
081: while (!comp.isFocusOwner() && (time < 30000)) {
082: robot.delay(timeout);
083: time += timeout;
084: timeout <<= 1;
085: }
086: assertTrue("component has focus", comp.isFocusOwner());
087: }
088:
089: public final void testSetDefaultFocusTraversalPolicy() {
090: try {
091: kfm.setDefaultFocusTraversalPolicy(null);
092: fail("IllegalArgumentException was not thrown!");
093: } catch (IllegalArgumentException iae) {
094: }
095:
096: // Regression test for HARMONY-2493
097: KeyboardFocusManagerImpl obj = new KeyboardFocusManagerImpl();
098: Set keys = obj.getDefaultFocusTraversalKeys(1);
099: assertNotNull(keys);
100: assertTrue(keys.size() > 0);
101: }
102:
103: @SuppressWarnings("deprecation")
104: public final void testClearGlobalFocusOwner() {
105: f.setSize(200, 200);
106: f.show();
107: waitFocus(f);
108: kfm.clearGlobalFocusOwner();
109: robot.delay(300);
110: assertFalse("frame is not the focus owner", f.isFocusOwner());
111: assertNull("focus owner is null", kfm.getFocusOwner());
112: assertNull("permanent focus owner is null", kfm
113: .getPermanentFocusOwner());
114: }
115:
116: class KeyboardFocusManagerImpl extends
117: java.awt.KeyboardFocusManager {
118: public KeyboardFocusManagerImpl() {
119: super ();
120: }
121:
122: public boolean dispatchEvent(AWTEvent arg0) {
123: return false;
124: }
125:
126: public boolean dispatchKeyEvent(KeyEvent arg0) {
127: return false;
128: }
129:
130: public boolean postProcessKeyEvent(KeyEvent arg0) {
131: return false;
132: }
133:
134: public void processKeyEvent(Component arg0, KeyEvent arg1) {
135: }
136:
137: protected void enqueueKeyEvents(long arg0, Component arg1) {
138: }
139:
140: protected void dequeueKeyEvents(long arg0, Component arg1) {
141: }
142:
143: protected void discardKeyEvents(Component arg0) {
144: }
145:
146: public void focusNextComponent(Component arg0) {
147: }
148:
149: public void focusPreviousComponent(Component arg0) {
150: }
151:
152: public void upFocusCycle(Component arg0) {
153: }
154:
155: public void downFocusCycle(Container arg0) {
156: }
157: }
158: }
|