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.text;
021:
022: import java.awt.ComponentOrientation;
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025: import javax.accessibility.AccessibleContext;
026: import javax.swing.Action;
027: import javax.swing.JFrame;
028: import javax.swing.JTextArea;
029: import javax.swing.SwingTestCase;
030: import javax.swing.UIManager;
031: import javax.swing.plaf.InsetsUIResource;
032: import javax.swing.plaf.TextUI;
033: import javax.swing.plaf.basic.BasicTextAreaUI;
034: import javax.swing.plaf.basic.BasicTextUI;
035: import javax.swing.plaf.basic.TextCompUI;
036:
037: public class JTextComponent_byAuxiliaryComponentTest extends
038: SwingTestCase {
039: JFrame jf;
040:
041: JTextArea jtc;
042:
043: SimplePropertyChangeListener pChListener;
044:
045: String sRTL = "\u05DC";
046:
047: String sLTR = "\u0061";
048:
049: JTextComp jtComp;
050:
051: String pattern = "@[^,}]*";
052:
053: void assertEqualsPropertyChangeEvent(final String name,
054: final Object oldValue, final Object newValue,
055: final PropertyChangeEvent e) {
056: assertEquals(name, e.getPropertyName());
057: assertEquals(oldValue, e.getOldValue());
058: assertEquals(newValue, e.getNewValue());
059: }
060:
061: class JTextComp extends JTextComponent {
062: private static final long serialVersionUID = 1L;
063:
064: String UIClassId = "TextCompUIFirst";
065:
066: @Override
067: public String getUIClassID() {
068: return (UIClassId != null) ? UIClassId : "TextCompUIFirst";
069: }
070: }
071:
072: class SimplePropertyChangeListener implements
073: PropertyChangeListener {
074: PropertyChangeEvent event;
075:
076: public void propertyChange(final PropertyChangeEvent e) {
077: if (e.getPropertyName() != "ancestor") {
078: event = e;
079: }
080: }
081:
082: PropertyChangeEvent getEvent() {
083: PropertyChangeEvent e = event;
084: event = null;
085: return e;
086: }
087: }
088:
089: public JTextComponent_byAuxiliaryComponentTest() {
090: setIgnoreNotImplemented(true);
091: }
092:
093: @Override
094: protected void setUp() throws Exception {
095: super .setUp();
096: UIManager.put("TextCompUISecond",
097: "javax.swing.plaf.basic.BasicTextAreaUI");
098: UIManager.put("TextCompUIFirst",
099: "javax.swing.plaf.basic.TextCompUI");
100: jf = new JFrame();
101: jtComp = new JTextComp();
102: jf.getContentPane().add(jtComp);
103: jf.setLocation(200, 300);
104: jf.setSize(300, 200);
105: jf.pack();
106: }
107:
108: @Override
109: protected void tearDown() throws Exception {
110: jf.dispose();
111: super .tearDown();
112: }
113:
114: public void testGetAccessibleContext() {
115: AccessibleContext ac = jtComp.getAccessibleContext();
116: assertTrue(jtComp.getAccessibleContext() instanceof JTextComponent.AccessibleJTextComponent);
117: assertTrue(jtComp.getAccessibleContext() instanceof JTextComponent.AccessibleJTextComponent);
118: assertEquals(ac, jtComp.getAccessibleContext());
119: }
120:
121: public void testGetActions() {
122: Action actions[] = new DefaultEditorKit().getActions();
123: Action actions1[] = jtComp.getActions();
124: Action actions2[] = new DefaultEditorKit().getActions();
125: assertEquals(actions1.length, actions2.length);
126: assertEquals(53, actions1.length);
127: for (int i = 0; i < actions.length; i++) {
128: assertEquals(actions1[i], actions2[i]);
129: }
130: }
131:
132: public void testUpdateUI() throws Exception {
133: assertTrue(jtComp.getUI() instanceof TextCompUI);
134: TextUI textUI1 = jtComp.getUI();
135: SimplePropertyChangeListener listener = new SimplePropertyChangeListener();
136: jtComp.addPropertyChangeListener(listener);
137: jtComp.UIClassId = "TextCompUISecond";
138: TextUI textUI2 = (TextUI) UIManager.getUI(jtComp);
139: assertNotNull(textUI2);
140: jtComp.updateUI();
141: assertEqualsPropertyChangeEvent("UI", textUI1, jtComp.getUI(),
142: listener.event);
143: assertTrue(jtComp.getUI() instanceof BasicTextAreaUI);
144: }
145:
146: public void testJTextComponent() {
147: jtComp = new JTextComp();
148: assertNotNull(jtComp);
149: assertTrue(jtComp.getUI() instanceof TextCompUI);
150: assertTrue(jtComp.getCaret() instanceof BasicTextUI.BasicCaret);
151: assertEquals("Dot=(0, Forward) Mark=(0, Forward)", jtComp
152: .getCaret().toString());
153: assertTrue(jtComp.getHighlighter() instanceof BasicTextUI.BasicHighlighter);
154: assertEquals(ComponentOrientation.UNKNOWN, jtComp
155: .getComponentOrientation());
156: assertTrue(jtComp.isEditable());
157: assertFalse(jtComp.getDragEnabled());
158: assertEquals('\0', jtComp.getFocusAccelerator());
159: assertEquals("TextCompUI", jtComp.getKeymap().getName());
160: assertEquals(0, jtComp.getKeymap().getBoundActions().length);
161: assertEquals(0, jtComp.getKeymap().getBoundKeyStrokes().length);
162: assertEquals(new InsetsUIResource(0, 0, 0, 0), jtComp
163: .getMargin());
164: }
165:
166: public void testSetGetUITextUI() throws Exception {
167: assertTrue(jtComp.getUI() instanceof TextCompUI);
168: TextUI textUI1 = jtComp.getUI();
169: SimplePropertyChangeListener listener = new SimplePropertyChangeListener();
170: jtComp.addPropertyChangeListener(listener);
171: jtComp.UIClassId = "TextCompUISecond";
172: TextUI textUI2 = (TextUI) UIManager.getUI(jtComp);
173: jtComp.setUI(textUI2);
174: assertEqualsPropertyChangeEvent("UI", textUI1, textUI2,
175: listener.event);
176: assertEquals(textUI2, jtComp.getUI());
177: }
178: }
|