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.Color;
023: import java.awt.ComponentOrientation;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Insets;
027: import java.awt.Point;
028: import java.awt.Rectangle;
029: import java.awt.event.InputEvent;
030: import java.beans.PropertyChangeEvent;
031: import javax.swing.ActionMap;
032: import javax.swing.InputMap;
033: import javax.swing.JComponent;
034: import javax.swing.JFrame;
035: import javax.swing.JMenuItem;
036: import javax.swing.JTextArea;
037: import javax.swing.JTextField;
038: import javax.swing.KeyStroke;
039: import javax.swing.SwingConstants;
040: import javax.swing.SwingTestCase;
041: import javax.swing.SwingUtilities;
042: import javax.swing.UIManager;
043: import javax.swing.border.Border;
044: import javax.swing.border.TitledBorder;
045: import javax.swing.plaf.ActionMapUIResource;
046: import javax.swing.plaf.BorderUIResource;
047: import javax.swing.plaf.ColorUIResource;
048: import javax.swing.plaf.FontUIResource;
049: import javax.swing.plaf.InputMapUIResource;
050: import javax.swing.plaf.InsetsUIResource;
051: import javax.swing.plaf.TextUI;
052: import javax.swing.plaf.UIResource;
053: import javax.swing.text.AbstractDocument;
054: import javax.swing.text.BadLocationException;
055: import javax.swing.text.Caret;
056: import javax.swing.text.DefaultCaret;
057: import javax.swing.text.DefaultEditorKit;
058: import javax.swing.text.DefaultHighlighter;
059: import javax.swing.text.EditorKit;
060: import javax.swing.text.Element;
061: import javax.swing.text.Highlighter;
062: import javax.swing.text.JTextComponent;
063: import javax.swing.text.Keymap;
064: import javax.swing.text.PlainView;
065: import javax.swing.text.Position;
066: import javax.swing.text.StyledEditorKit;
067: import javax.swing.text.View;
068:
069: import junit.framework.AssertionFailedError;
070:
071: public class BasicTextUITest extends SwingTestCase {
072: MyBasicTextUI basicTextUI;
073:
074: JTextField tf;
075:
076: JExtTextArea jta;
077:
078: JFrame jf;
079:
080: String s;
081:
082: final Dimension PREF_SIZE = new Dimension(184, 20);
083:
084: Position.Bias forward = Position.Bias.Forward;
085:
086: Position.Bias backward = Position.Bias.Backward;
087:
088: private View view;
089:
090: boolean wasCallInvalidate = false;
091:
092: final ColorUIResource RED = new ColorUIResource(255, 0, 0);
093:
094: final ColorUIResource BLUE = new ColorUIResource(0, 0, 255);
095:
096: final ColorUIResource GREEN = new ColorUIResource(0, 255, 255);
097:
098: final ColorUIResource YELLOW = new ColorUIResource(255, 255, 0);
099:
100: final ColorUIResource BLACK = new ColorUIResource(0, 0, 0);
101:
102: final Font FONT = new Font("SimSun", 8, 8);
103:
104: boolean bWasException;
105:
106: class MyBasicTextUI extends BasicTextUI {
107: @Override
108: protected String getPropertyPrefix() {
109: return null;
110: }
111:
112: @Override
113: protected void installDefaults() {
114: super .installDefaults();
115: }
116:
117: @Override
118: protected void installKeyboardActions() {
119: super .installKeyboardActions();
120: }
121:
122: @Override
123: protected void installListeners() {
124: super .installListeners();
125: }
126:
127: @Override
128: public void installUI(final JComponent c) {
129: super .installUI(c);
130: }
131:
132: @Override
133: protected void propertyChange(final PropertyChangeEvent e) {
134: super .propertyChange(e);
135: }
136: }
137:
138: class JExtTextArea extends JTextArea {
139: private static final long serialVersionUID = 1L;
140:
141: public boolean wasCallInvalidate = false;
142:
143: JExtTextArea(final String s) {
144: super (s);
145: }
146:
147: @Override
148: public void invalidate() {
149: wasCallInvalidate = true;
150: super .invalidate();
151: }
152: }
153:
154: @Override
155: protected void setUp() throws Exception {
156: super .setUp();
157: bWasException = false;
158: s = null;
159: basicTextUI = new MyBasicTextUI();
160: tf = new JTextField("JTextField\n JTextField \n JTextField");
161: UIManager
162: .put("TextAreaUI", "javax.swing.plaf.basic.TextAreaUI");
163: jf = new JFrame();
164: jta = new JExtTextArea("");
165: //jta.setText(sLTR + sRTL + sLTR + "\n" + sLTR + sRTL + sLTR + "\n"
166: // + sLTR + sRTL + sLTR + "\n" + "JTextArea \n JTextArea\n"
167: // + "\u05dc");
168: jta.setText("aaa" + "\n" + "bbb" + "\n" + "ccc" + "\n"
169: + "JTextArea \n JTextArea\n" + "e");
170: jf.getContentPane().add(jta);
171: jf.setSize(200, 300);
172: jf.pack();
173: }
174:
175: @Override
176: protected void tearDown() throws Exception {
177: jf.dispose();
178: UIManager.put("TextAreaUI",
179: "javax.swing.plaf.basic.BasicTextAreaUI");
180: super .tearDown();
181: }
182:
183: // public void testBasicTextUI() {
184: // }
185:
186: private void getPos(final BasicTextUI ui, final int start,
187: final Position.Bias bias, final int direction,
188: final int samplePos, final Position.Bias sample,
189: final boolean cond) {
190: int p = 0;
191: Position.Bias b[] = new Position.Bias[1];
192: try {
193: p = ui.getNextVisualPositionFrom(jta, start, bias,
194: direction, b);
195: } catch (BadLocationException e) {
196: }
197: assertEquals(samplePos, p);
198: }
199:
200: public void testGetNextVisualPosition() throws Exception {
201: BasicTextUI ui = (BasicTextUI) jta.getUI();
202: try {
203: ui.getNextVisualPositionFrom(jta, 5, Position.Bias.Forward,
204: SwingConstants.SOUTH_WEST, new Position.Bias[1]);
205: } catch (IllegalArgumentException e) {
206: bWasException = true;
207: s = e.getMessage();
208: } catch (BadLocationException e) {
209: }
210: if (isHarmony()) {
211: assertTrue(bWasException);
212: assertEquals("Invalid direction", s);
213: getPos(ui, 6, forward, SwingConstants.WEST, 5, forward,
214: true);
215: //This tests depends on MagicCaretPosition (timer)
216: //getPos(ui, 6, forward, SwingConstants.NORTH, 0, forward);
217: //getPos(ui, 6, forward, SwingConstants.SOUTH, 8, forward);
218: getPos(ui, 6, forward, SwingConstants.EAST, 7, forward,
219: false);
220: getPos(ui, 6, backward, SwingConstants.WEST, 5, backward,
221: true);
222: //This tests depends on MagicCaretPosition (timer)
223: //getPos(ui, 4, backward, SwingConstants.NORTH, 4, backward); //?? 6
224: //getPos(ui, 36, backward, SwingConstants.SOUTH, 36, backward); //?? 6
225: getPos(ui, 6, backward, SwingConstants.EAST, 7, forward,
226: true);
227: }
228: }
229:
230: public void testGetToolTipTextJTextComponentPoint() {
231: Point p = new Point(0, 0);
232: for (int i = 0; i < 200; i++) {
233: for (int j = 0; j < 200; j++) {
234: p.x = i;
235: p.y = j;
236: assertNull(getBasicTextUI(jta).getToolTipText(tf, p));
237: assertNull(getBasicTextUI(jta).getToolTipText(null, p));
238: assertNull(getBasicTextUI(jta).getToolTipText(jta, p));
239: }
240: }
241: }
242:
243: public void testModelToView() {
244: BasicTextUI ui = (BasicTextUI) jta.getUI();
245: View view = ui.getRootView(jta).getView(0);
246: Rectangle r1 = null, r2 = null;
247: try {
248: int length = jta.getDocument().getLength();
249: for (int i = 0; i < length; i++) {
250: Rectangle visibleRect = ui.getVisibleEditorRect();
251: r1 = ui.modelToView(null, i, forward);
252: r2 = view.modelToView(i, visibleRect, forward)
253: .getBounds();
254: assertEquals(r2, r1);
255: r1 = ui.modelToView(null, i);
256: assertEquals(r2, r1);
257: r1 = ui.modelToView(null, i, backward);
258: r2 = view.modelToView(i, visibleRect, backward)
259: .getBounds();
260: assertEquals(r2, r1);
261: }
262: } catch (BadLocationException e) {
263: }
264: }
265:
266: public void testGetRootView() {
267: BasicTextUI ui = getBasicTextUI(jta);
268: View rootView = ui.getRootView(jta);
269: AbstractDocument doc = (AbstractDocument) jta.getDocument();
270: Element rootElement = jta.getDocument().getDefaultRootElement();
271: assertEquals(1, rootView.getViewCount());
272: View sonRootView = rootView.getView(0);
273: assertNotNull(sonRootView);
274: assertEquals(0, sonRootView.getViewCount());
275: assertEquals(rootElement, rootView.getElement());
276: assertEquals(sonRootView.getElement(), rootElement);
277: assertEquals(rootView.getView(0).getElement(), rootElement);
278: assertEquals(doc, rootView.getDocument());
279: assertEquals(jta, rootView.getContainer());
280: assertEquals(doc.getLength() + 1, rootView.getEndOffset());
281: assertEquals(ui, rootView.getViewFactory());
282: assertNull(rootView.getParent());
283: /*
284: * Next Functionality is not required try { rootView.setParent(new
285: * PlainView(rootElement)); } catch (Error error) { bWasException =
286: * true; s = error.getMessage(); } assertTrue(bWasException);
287: * assertEquals("Can't set parent on root view", s);
288: *
289: * assertEquals(rootView, rootView.createFragment(2, 5));
290: * assertTrue(jta.getAlignmentX() ==
291: * rootView.getAlignment(View.X_AXIS)); assertTrue(jta.getAlignmentY() ==
292: * rootView.getAlignment(View.Y_AXIS));
293: * assertNull(rootView.getAttributes());
294: *
295: * //has not null AttributeSet Dimension dimension =
296: * ui.getMaximumSize(jta);
297: *
298: * //assertTrue(rintSpec(dimension.width) == //
299: * (int)(rootView.getMaximumSpan(View.X_AXIS) / 10));
300: *
301: */
302: }
303:
304: public void testCreateElementintint() {
305: Element elem = jta.getDocument().getDefaultRootElement();
306: assertNull(basicTextUI.create(elem));
307: assertNull(basicTextUI.create(elem, 0, 5));
308: }
309:
310: public void testGetEditorKit() {
311: EditorKit editorKit = basicTextUI.getEditorKit(jta);
312: assertTrue(editorKit instanceof DefaultEditorKit);
313: assertFalse(editorKit instanceof StyledEditorKit);
314: //JEditorPane is not support yet
315: //assertEquals(editorKit, basicTextUI.getEditorKit(new JTextPane()));
316: //assertEquals(editorKit, basicTextUI.getEditorKit(new JEditorPane()));
317: assertEquals(editorKit, basicTextUI
318: .getEditorKit(new JTextField()));
319: MyBasicTextUI basicTextUI2 = new MyBasicTextUI();
320: assertEquals(editorKit, basicTextUI2
321: .getEditorKit(new JTextArea()));
322: }
323:
324: private Point rectToPoint(final Point p, final Rectangle r) {
325: p.x = r.x;
326: p.y = r.y;
327: return p;
328: }
329:
330: public void testViewToModel() {
331: BasicTextUI ui = (BasicTextUI) jta.getUI();
332: Position.Bias b[] = new Position.Bias[1];
333: Rectangle r1 = null;
334: Rectangle r2 = null;
335: Point p = new Point(0, 0);
336: try {
337: r1 = ui.modelToView(null, 6, forward);
338: } catch (BadLocationException e) {
339: }
340: try {
341: r2 = ui.modelToView(null, 6, backward);
342: } catch (BadLocationException e) {
343: }
344: assertNotNull(r1);
345: assertNotNull(r2);
346: assertEquals(6, ui.viewToModel(null, rectToPoint(p, r1)));
347: assertEquals(6, ui.viewToModel(null, rectToPoint(p, r2)));
348: assertEquals(6, ui.viewToModel(null, rectToPoint(p, r1), b));
349: assertEquals(6, ui.viewToModel(null, rectToPoint(p, r2), b));
350: }
351:
352: private BasicTextUI getBasicTextUI(final JTextComponent c) {
353: return (BasicTextUI) c.getUI();
354: }
355:
356: public void testGetPreferredSizeJComponent() throws Exception {
357: tf = new JTextField("JTextField\n JTextField \n JTextField");
358: View view = tf.getUI().getRootView(tf);
359: Insets insets = tf.getInsets();
360: int prefX = (int) view.getPreferredSpan(View.X_AXIS);
361: int prefY = (int) view.getPreferredSpan(View.Y_AXIS);
362: int hrz = insets.left + insets.right;
363: int vrt = insets.top + insets.bottom;
364: int uiPrefWidth = prefX + hrz;
365: int uiPrefHeight = prefY + vrt;
366: assertEquals(new Dimension(uiPrefWidth, uiPrefHeight),
367: getBasicTextUI(tf).getPreferredSize(tf));
368: }
369:
370: public void testGetMinimumSizeJComponent() {
371: tf = new JTextField("JTextField\n JTextField \n JTextField");
372: View view = tf.getUI().getRootView(tf);
373: Insets insets = tf.getInsets();
374: int minX = (int) view.getMinimumSpan(View.X_AXIS);
375: int minY = (int) view.getMinimumSpan(View.Y_AXIS);
376: int hrz = insets.left + insets.right;
377: int vrt = insets.top + insets.bottom;
378: int uiMinWidth = minX + hrz;
379: int uiMinHeight = minY + vrt;
380: assertEquals(new Dimension(uiMinWidth, uiMinHeight),
381: getBasicTextUI(tf).getMinimumSize(tf));
382: }
383:
384: public void testGetMaximumSizeJComponent() {
385: assertEquals(
386: new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE),
387: ((BasicTextUI) jta.getUI()).getMaximumSize(jta));
388: }
389:
390: public void testSetView() throws Exception {
391: jta.wasCallInvalidate = false;
392: view = new PlainView(jta.getDocument().getDefaultRootElement());
393: ((BasicTextUI) jta.getUI()).setView(view);
394: wasCallInvalidate = jta.wasCallInvalidate;
395: assertTrue(wasCallInvalidate);
396: assertEquals(view, ((BasicTextUI) jta.getUI()).getRootView(jta)
397: .getView(0));
398: }
399:
400: private String getKeymapName(final JTextComponent c) {
401: BasicTextUI ui = (BasicTextUI) jta.getUI();
402: String className = ui.getClass().getName();
403: int start = className.lastIndexOf(".");
404: int end = className.length();
405: String keymapName = className.substring(start + 1, end);
406: return keymapName;
407: }
408:
409: public void testCreateKeymap() {
410: JTextComponent.removeKeymap("BasicTextAreaUI");
411: TextUI ui = jta.getUI();
412: Keymap keymap = ((BasicTextUI) ui).createKeymap();
413: assertEquals(getKeymapName(jta), keymap.getName());
414: assertTrue(keymap.getDefaultAction() instanceof DefaultEditorKit.DefaultKeyTypedAction);
415: assertEquals(0, keymap.getBoundActions().length);
416: assertEquals(0, keymap.getBoundKeyStrokes().length);
417: assertEquals(JTextComponent.DEFAULT_KEYMAP, keymap
418: .getResolveParent().getName());
419: }
420:
421: public void testDamageRangeJTextComponentintint() {
422: }
423:
424: public void testGetComponent() {
425: assertEquals(jta, ((BasicTextUI) jta.getUI()).getComponent());
426: }
427:
428: public void testCreateHighlighter() {
429: Highlighter highlighter = basicTextUI.createHighlighter();
430: assertTrue(highlighter instanceof BasicTextUI.BasicHighlighter);
431: }
432:
433: public void testCreateCaret() {
434: Caret caret = basicTextUI.createCaret();
435: assertTrue(caret instanceof BasicTextUI.BasicCaret);
436: }
437:
438: private String findAndRemoveSubstring(final String str,
439: final String subStr) {
440: int index = str.indexOf(subStr);
441: assertTrue(index >= 0);
442: return str.replaceFirst(subStr, "");
443: }
444:
445: public void testUninstallUI() throws Exception {
446: TextUI ui = jta.getUI();
447: assertTrue(ui instanceof TextAreaUI);
448: TextAreaUI.callOrder = "";
449: jta.setVisible(false);
450: jta.getUI().uninstallUI(jta);
451: String tmp = TextAreaUI.callOrder;
452: tmp = findAndRemoveSubstring(tmp, "uninstallUI::");
453: tmp = findAndRemoveSubstring(tmp, "uninstallDefaults::");
454: tmp = findAndRemoveSubstring(tmp, "uninstallKeyboardActions::");
455: tmp = findAndRemoveSubstring(tmp, "uninstallListeners::");
456: assertEquals("", tmp);
457:
458: // regression for HARMONY-2521
459: new javax.swing.JTextPane().updateUI();
460: }
461:
462: public void testInstallUI() throws Exception {
463: Caret caret = jta.getCaret();
464: Highlighter highlighter = jta.getHighlighter();
465: String prefix = ((BasicTextUI) tf.getUI()).getPropertyPrefix();
466: (jta.getUI()).uninstallUI(jta);
467: TextUI ui = jta.getUI();
468: assertTrue(ui instanceof TextAreaUI);
469: TextAreaUI.callOrder = "";
470: (jta.getUI()).installUI(jta);
471: String tmp = TextAreaUI.callOrder;
472: tmp = findAndRemoveSubstring(tmp, "installUI::");
473: tmp = findAndRemoveSubstring(tmp, "installDefaults::");
474: tmp = findAndRemoveSubstring(tmp, "installKeyboardActions::");
475: tmp = findAndRemoveSubstring(tmp, "installListeners::");
476: tmp = findAndRemoveSubstring(tmp, "modelChanged::");
477: tmp = findAndRemoveSubstring(tmp, "createCaret::");
478: tmp = findAndRemoveSubstring(tmp, "createHighlighter::");
479: tmp = tmp.replaceAll("create::", "");
480: assertEquals("", tmp);
481: assertNotSame(caret, jta.getCaret());
482: assertTrue(jta.getCaret() instanceof UIResource);
483: assertNotSame(highlighter, jta.getHighlighter());
484: assertTrue(jta.getHighlighter() instanceof UIResource);
485: assertTrue(jta.getTransferHandler() instanceof UIResource);
486: assertTrue(jta.isOpaque());
487: int caretBlinkRate = ((Integer) getProperty(prefix,
488: "caretBlinkRate")).intValue();
489: assertEquals(caretBlinkRate, caret.getBlinkRate());
490: assertEquals(jta.getDocument().getDefaultRootElement(), jta
491: .getUI().getRootView(jta).getElement());
492: }
493:
494: // Regression test for HARMONY-1779
495: public void testInstallUINull() {
496: basicTextUI = new MyBasicTextUI();
497: try {
498: basicTextUI.installUI(null);
499: fail("Error is expected (\"TextUI needs JTextComponent\")");
500: } catch (AssertionFailedError e) {
501: // Let JUnit handle its exceptions
502: throw e;
503: } catch (Error e) {
504: // expected
505: }
506: }
507:
508: // Regression test for HARMONY-1779
509: public void testInstallUINonTextComponent() {
510: basicTextUI = new MyBasicTextUI();
511: try {
512: basicTextUI.installUI(new JMenuItem());
513: fail("Error is expected (\"TextUI needs JTextComponent\")");
514: } catch (AssertionFailedError e) {
515: // Let JUnit handle its exceptions
516: throw e;
517: } catch (Error e) {
518: // expected
519: }
520: }
521:
522: public void testGetKeymapName() {
523: assertEquals("BasicTextUITest$MyBasicTextUI", basicTextUI
524: .getKeymapName());
525: }
526:
527: public void testPropertyChange() throws Exception {
528: TextAreaUI ui = (TextAreaUI) jta.getUI();
529: ui.propertyChangeFlag = false;
530: ui.eventName = null;
531: jta.setCaretColor(null);
532: assertTrue(ui.propertyChangeFlag);
533: assertEquals("caretColor", ui.eventName);
534: }
535:
536: public void testGetVisibleEditorRect() throws Exception {
537: BasicTextUI ui = (BasicTextUI) jta.getUI();
538: jta.setSize(3, 0);
539: assertNull(ui.getVisibleEditorRect());
540: jta.setBounds(0, 0, 234, 553);
541: assertEquals(jta.getBounds(), ui.getVisibleEditorRect());
542: }
543:
544: public void testUninstallListeners() {
545: }
546:
547: public void testUninstallKeyboardActions() {
548: }
549:
550: public void testUninstallDefaults() throws Exception {
551: tf.setCaretColor(null);
552: tf.setSelectionColor(null);
553: tf.setSelectedTextColor(null);
554: tf.setDisabledTextColor(null);
555: tf.setFont(null);
556: tf.setBackground(null);
557: tf.setForeground(null);
558: tf.setBorder(BorderUIResource.getEtchedBorderUIResource());
559: tf.setMargin(null);
560: tf.setActionMap(null);
561: ((BasicTextUI) tf.getUI()).uninstallDefaults();
562: assertNull(tf.getForeground());
563: assertNull(tf.getBackground());
564: assertNull(tf.getFont());
565: assertNull(tf.getCaretColor());
566: assertNull(tf.getSelectionColor());
567: assertNull(tf.getSelectedTextColor());
568: assertNull(tf.getDisabledTextColor());
569: //?????
570: assertNull(tf.getBorder());
571: assertNull(tf.getMargin());
572: tf.setCaretColor(RED);
573: tf.setSelectionColor(GREEN);
574: tf.setSelectedTextColor(BLUE);
575: tf.setDisabledTextColor(YELLOW);
576: tf.setFont(new FontUIResource("SimSun", 8, 8));
577: tf.setBackground(BLACK);
578: tf.setForeground(YELLOW);
579: tf.setBorder(new BorderUIResource.LineBorderUIResource(
580: Color.RED));
581: tf.setMargin(new InsetsUIResource(2, 4, 6, 3));
582: ((BasicTextUI) tf.getUI()).uninstallDefaults();
583: //TODO specification antogonism
584: //assertEquals(YELLOW,tf.getForeground()); //???
585: //assertEquals(BLACK,tf.getBackground()); //???
586: //assertEquals(new FontUIResource("SimSun", 8, 8),tf.getFont()); //???
587: assertNull(tf.getCaretColor());
588: assertNull(tf.getSelectionColor());
589: assertNull(tf.getSelectedTextColor());
590: assertNull(tf.getDisabledTextColor());
591: //?????
592: assertNull(tf.getBorder());
593: assertNull(tf.getMargin());
594: tf.setCaretColor(Color.RED);
595: tf.setSelectionColor(Color.GREEN);
596: tf.setSelectedTextColor(Color.BLUE);
597: tf.setDisabledTextColor(Color.YELLOW);
598: tf.setFont(FONT);
599: tf.setBackground(Color.BLACK);
600: tf.setForeground(Color.YELLOW);
601: tf.setBorder(new BasicBorders.ButtonBorder(null, null, null,
602: null));
603: tf.setMargin(new Insets(2, 4, 6, 3));
604: ((BasicTextUI) tf.getUI()).uninstallDefaults();
605: assertEquals(Color.YELLOW, tf.getForeground());
606: assertEquals(Color.BLACK, tf.getBackground());
607: assertEquals(FONT, tf.getFont());
608: assertEquals(Color.RED, tf.getCaretColor()); //!!!!! ???
609: assertEquals(Color.GREEN, tf.getSelectionColor());
610: assertEquals(Color.BLUE, tf.getSelectedTextColor());
611: assertEquals(Color.YELLOW, tf.getDisabledTextColor());
612: assertNull(tf.getBorder());//????
613: assertEquals(new Insets(2, 4, 6, 3), tf.getMargin());
614: }
615:
616: public void testModelChanged() throws Exception {
617: TextAreaUI ui = (TextAreaUI) jta.getUI();
618: ui.flagCreate = false;
619: ui.modelChanged();
620: assertTrue(ui.flagCreate);
621: }
622:
623: public void testInstallListeners() throws Exception {
624: }
625:
626: private InputMap getInputMap(final int generation) {
627: InputMap im = jta.getInputMap();
628: for (int i = 0; i < generation; i++) {
629: im = im.getParent();
630: }
631: return im;
632: }
633:
634: private ActionMap getActionMap(final int generation) {
635: ActionMap am = jta.getActionMap();
636: for (int i = 0; i < generation; i++) {
637: am = am.getParent();
638: }
639: return am;
640: }
641:
642: public void testInstallKeyboardActions() throws Exception {
643: ((BasicTextUI) jta.getUI()).uninstallKeyboardActions();
644: assertEquals(0, getInputMap(0).size());
645: assertFalse(getInputMap(0) instanceof InputMapUIResource);
646: assertEquals(0, getInputMap(1).size());
647: assertTrue(getInputMap(1) instanceof InputMapUIResource);
648: //assertEquals(55, jta.getInputMap().getParent().getParent().size());1.5.0
649: assertTrue(getInputMap(2) instanceof InputMapUIResource);
650: assertNull(getInputMap(3));
651: assertEquals(0, getActionMap(0).size());
652: assertFalse(getActionMap(0) instanceof ActionMapUIResource);
653: assertNull(getActionMap(1));
654: assertNull(jta.getKeymap());
655: ((BasicTextUI) jta.getUI()).installKeyboardActions();
656: assertNotNull(jta.getKeymap());
657: assertEquals(getKeymapName(jta), jta.getKeymap().getName());
658: assertEquals(0, getInputMap(0).size());
659: assertEquals(0, getInputMap(1).size());
660: assertEquals(0, getInputMap(2).size());
661: //assertEquals(55, jta.getInputMap().getParent().getParent().getParent()
662: // .size()); //1.5.0
663: assertNull(getInputMap(4));
664: assertEquals(0, getActionMap(0).size());
665: assertEquals(1, getActionMap(1).size());
666: //Note
667: //assertEquals(2,jta.getActionMap().getParent().getParent().size());
668: assertEquals(56, getActionMap(3).size());
669: assertNull(getActionMap(4));
670: assertFalse(getInputMap(0) instanceof InputMapUIResource);
671: assertFalse(getInputMap(1) instanceof InputMapUIResource);
672: assertTrue(getInputMap(2) instanceof InputMapUIResource);
673: assertTrue(getInputMap(3) instanceof InputMapUIResource);
674: assertFalse(getActionMap(0) instanceof ActionMapUIResource);
675: assertFalse(getActionMap(1) instanceof ActionMapUIResource);
676: assertTrue(getActionMap(2) instanceof ActionMapUIResource);
677: assertTrue(getActionMap(3) instanceof ActionMapUIResource);
678: }
679:
680: private Object getProperty(final String prefix, final String s) {
681: return UIManager.getLookAndFeelDefaults().get(prefix + "." + s);
682: }
683:
684: private void setProperies(final JTextComponent c,
685: final Color caretColor, final Color selectionColor,
686: final Color selectedTextColor,
687: final Color disabledTextColor, final Font font,
688: final Color background, final Color foreground,
689: final Insets margin, final Border border,
690: final int caretBlinkRate) {
691: c.setCaretColor(caretColor);
692: c.setSelectionColor(selectionColor);
693: c.setSelectedTextColor(selectedTextColor);
694: c.setDisabledTextColor(disabledTextColor);
695: c.setFont(font);
696: c.setBackground(background);
697: c.setForeground(foreground);
698: c.setBorder(border);
699: c.setMargin(margin);
700: Caret caret = tf.getCaret();
701: if (caret != null) {
702: caret.setBlinkRate(caretBlinkRate);
703: }
704: }
705:
706: private void checkProperies(final JTextComponent c,
707: final Color caretColor, final Color selectionColor,
708: final Color selectedTextColor,
709: final Color disabledTextColor, final Font font,
710: final Color background, final Color foreground,
711: final Insets margin, final Border border) {
712: assertEquals(caretColor, c.getCaretColor());
713: assertEquals(selectionColor, c.getSelectionColor());
714: assertEquals(selectedTextColor, c.getSelectedTextColor());
715: assertEquals(disabledTextColor, c.getDisabledTextColor());
716: assertEquals(font, c.getFont());
717: assertEquals(background, c.getBackground());
718: assertEquals(foreground, c.getForeground());
719: assertEquals(border, c.getBorder());
720: assertEquals(margin, c.getMargin());
721: }
722:
723: public void testInstallDefaults() throws Exception {
724: String prefix = ((BasicTextUI) tf.getUI()).getPropertyPrefix();
725: setProperies(tf, null, null, null, null, null, null, null,
726: null, null, 0);
727: tf.setActionMap(null);
728: ((BasicTextUI) tf.getUI()).installDefaults();
729: Color foreground = (Color) getProperty(prefix, "foreground");
730: Color background = (Color) getProperty(prefix, "background");
731: Font font = (Font) getProperty(prefix, "font");
732: Color caretForeground = (Color) getProperty(prefix,
733: "caretForeground");
734: Color selectionBackground = (Color) getProperty(prefix,
735: "selectionBackground");
736: Color selectionForeground = (Color) getProperty(prefix,
737: "selectionForeground");
738: Color inactiveForeground = (Color) getProperty(prefix,
739: "inactiveForeground");
740: Insets margin = (Insets) getProperty(prefix, "margin");
741: Border border = (Border) getProperty(prefix, "border");
742: checkProperies(tf, caretForeground, selectionBackground,
743: selectionForeground, inactiveForeground, font,
744: background, foreground, margin, border);
745: setProperies(tf, RED, GREEN, BLUE, YELLOW, new FontUIResource(
746: "SimSun", 8, 8), BLACK, YELLOW, new InsetsUIResource(2,
747: 4, 6, 3), new BorderUIResource.LineBorderUIResource(
748: Color.RED), 56);
749: ((BasicTextUI) tf.getUI()).installDefaults();
750: checkProperies(tf, caretForeground, selectionBackground,
751: selectionForeground, inactiveForeground, font,
752: background, foreground, margin, border);
753: Border newBorder = new TitledBorder("KK");
754: setProperies(tf, Color.RED, Color.GREEN, Color.BLUE,
755: Color.YELLOW, FONT, Color.BLACK, Color.YELLOW,
756: new Insets(2, 4, 6, 3), newBorder, 0);
757: ((BasicTextUI) tf.getUI()).installDefaults();
758: checkProperies(tf, Color.RED, Color.GREEN, Color.BLUE,
759: Color.YELLOW, FONT, Color.BLACK, Color.YELLOW,
760: new Insets(2, 4, 6, 3), newBorder);
761: }
762:
763: public void testBasicCaret() {
764: Caret caret = new BasicTextUI.BasicCaret();
765: assertTrue(caret instanceof DefaultCaret);
766: assertTrue(caret instanceof UIResource);
767: }
768:
769: public void testBasicHighlighter() {
770: Highlighter highlighter = new BasicTextUI.BasicHighlighter();
771: assertTrue(highlighter instanceof DefaultHighlighter);
772: assertTrue(highlighter instanceof UIResource);
773: }
774:
775: public void testI18nProperty() throws Exception {
776: JTextArea ta = new JTextArea("aaaa");
777: TextAreaUI ui = (TextAreaUI) ta.getUI();
778: ui.flagModelChanged = false;
779: ta.setText("aaaa" + "\u05dc");
780: assertTrue(ui.flagModelChanged);
781: ui.flagModelChanged = false;
782: ta.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
783: assertTrue(ui.flagModelChanged);
784: }
785:
786: public void testCallOrder() throws Exception {
787: TextAreaUI.callOrder = "";
788: jta.getUI().uninstallUI(jta);
789: assertEquals(
790: "uninstallUI::uninstallDefaults::uninstallKeyboard"
791: + "Actions::uninstallListeners::",
792: TextAreaUI.callOrder);
793: TextAreaUI.callOrder = "";
794: jta.getUI().installUI(jta);
795: assertEquals(
796: "installUI::installDefaults::createCaret::createHighli"
797: + "ghter::modelChanged::create::installListeners::installKeyboardActions::",
798: TextAreaUI.callOrder);
799: }
800:
801: public void testFocusAccelerator() throws Exception {
802: jta.setFocusAccelerator('a');
803: InputMap im = SwingUtilities.getUIInputMap(jta,
804: JComponent.WHEN_IN_FOCUSED_WINDOW);
805: ActionMap am = jta.getActionMap().getParent().getParent();
806: assertNotNull(am);
807: assertNotNull(im);
808: assertEquals(1, im.size());
809: assertTrue(am.size() > 0);
810: assertEquals(im.keys()[0], KeyStroke.getKeyStroke('A',
811: InputEvent.ALT_DOWN_MASK));
812: Object actionName = im.get(im.keys()[0]);
813: assertNotNull(am.get(actionName));
814: }
815: }
|