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 Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.KeyboardFocusManager;
023: import java.awt.event.InputEvent;
024: import java.awt.event.KeyEvent;
025: import java.util.Set;
026: import javax.swing.JEditorPane;
027: import javax.swing.JTextArea;
028: import javax.swing.JTextField;
029: import javax.swing.KeyStroke;
030: import javax.swing.SwingTestCase;
031: import javax.swing.text.AbstractDocument;
032: import javax.swing.text.JTextComponent;
033:
034: public class BasicTextUIRTest extends SwingTestCase {
035: private void checkEditableFTK(final JTextComponent comp,
036: final int hashCode1, final int hashCode2) {
037: Set<?> keys = comp
038: .getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
039: assertEquals(1, keys.size());
040: assertEquals(hashCode1, keys.hashCode());
041: assertTrue(keys.contains(KeyStroke.getKeyStroke(
042: KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK)));
043: keys = comp
044: .getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
045: assertEquals(1, keys.size());
046: assertEquals(hashCode2, keys.hashCode());
047: assertTrue(keys.contains(KeyStroke.getKeyStroke(
048: KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK
049: | InputEvent.SHIFT_DOWN_MASK)));
050: }
051:
052: private void checkNotEditableFTK(final JTextComponent comp) {
053: Set<?> keys = comp
054: .getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
055: assertEquals(2, keys.size());
056: assertTrue(keys.contains(KeyStroke.getKeyStroke(
057: KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK)));
058: assertTrue(keys.contains(KeyStroke.getKeyStroke(
059: KeyEvent.VK_TAB, 0)));
060: keys = comp
061: .getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
062: assertEquals(2, keys.size());
063: assertTrue(keys.contains(KeyStroke.getKeyStroke(
064: KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK
065: | InputEvent.SHIFT_DOWN_MASK)));
066: assertTrue(keys.contains(KeyStroke.getKeyStroke(
067: KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK)));
068: }
069:
070: public void testFocusTraversalKeys() {
071: JTextComponent textComp = new JTextArea();
072: int forwardFTKHashCode = textComp.getFocusTraversalKeys(
073: KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS).hashCode();
074: int backwardFTKHashCode = textComp.getFocusTraversalKeys(
075: KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
076: .hashCode();
077: checkEditableFTK(textComp, forwardFTKHashCode,
078: backwardFTKHashCode);
079: textComp.setEditable(false);
080: checkNotEditableFTK(textComp);
081: textComp.setEditable(true);
082: checkEditableFTK(textComp, forwardFTKHashCode,
083: backwardFTKHashCode);
084: textComp = new JEditorPane();
085: forwardFTKHashCode = textComp.getFocusTraversalKeys(
086: KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS).hashCode();
087: backwardFTKHashCode = textComp.getFocusTraversalKeys(
088: KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
089: .hashCode();
090: checkEditableFTK(textComp, forwardFTKHashCode,
091: backwardFTKHashCode);
092: textComp.setEditable(false);
093: checkNotEditableFTK(textComp);
094: textComp.setEditable(true);
095: checkEditableFTK(textComp, forwardFTKHashCode,
096: backwardFTKHashCode);
097: textComp = new JTextField();
098: forwardFTKHashCode = textComp.getFocusTraversalKeys(
099: KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS).hashCode();
100: backwardFTKHashCode = textComp.getFocusTraversalKeys(
101: KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)
102: .hashCode();
103: checkNotEditableFTK(textComp);
104: textComp.setEditable(false);
105: checkNotEditableFTK(textComp);
106: textComp.setEditable(true);
107: checkNotEditableFTK(textComp);
108: }
109:
110: public void testUninstallUI() {
111: // Regression for HARMONY-1475
112: JTextComponent textComp = new JTextField();
113: AbstractDocument doc = ((AbstractDocument) textComp
114: .getDocument());
115: assertTrue("listeners installed",
116: doc.getDocumentListeners().length > 0);
117: textComp.getUI().uninstallUI(textComp);
118: assertEquals("no listeners installed", 0, doc
119: .getDocumentListeners().length);
120: }
121: }
|