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;
021:
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026: import java.awt.GridLayout;
027: import java.awt.Insets;
028: import java.awt.event.ActionEvent;
029: import java.awt.event.ActionListener;
030: import java.beans.PropertyChangeEvent;
031: import java.beans.PropertyChangeListener; /*
032: import java.io.FileInputStream;
033: import java.io.FileOutputStream;
034: import java.io.ObjectInputStream;
035: import java.io.ObjectOutputStream;
036: */
037: import javax.accessibility.AccessibleContext;
038: import javax.accessibility.AccessibleState;
039: import javax.accessibility.AccessibleStateSet;
040: import javax.swing.event.ChangeEvent;
041: import javax.swing.event.ChangeListener;
042: import javax.swing.text.BadLocationException;
043: import javax.swing.text.DefaultEditorKit;
044: import javax.swing.text.Document;
045: import javax.swing.text.PlainDocument;
046: import javax.swing.text.View;
047:
048: public class JTextFieldTest extends SwingTestCase {
049: String sLTR = "abcd";
050:
051: String sRTL = "\u05dc" + "\u05dc" + "\u05dc" + "\u05dc";
052:
053: String bidiContent = sLTR + sRTL + sLTR + sRTL + sLTR + sRTL;
054:
055: boolean bWasException;
056:
057: String content = "British Air Line Pilots Association "
058: + "British Academy " + "British Airways ";
059:
060: String fireOrder;
061:
062: JFrame jf;
063:
064: ExtJTextField jtf;
065:
066: JTextField jtfBidi;
067:
068: JTextField jtfScroll;
069:
070: SimplePropertyChangeListener listener;
071:
072: String message;
073:
074: class ExtJTextField extends JTextField {
075: private static final long serialVersionUID = 1L;
076:
077: Action configureFrom = null;
078:
079: Action createFrom = null;
080:
081: boolean wasCallInvalidate = false;
082:
083: boolean wasCallRevalidate = false;
084:
085: boolean wasConfigureProperties = false;
086:
087: boolean wasCreateListeners = false;
088:
089: public ExtJTextField(final String s) {
090: super (s);
091: }
092:
093: @Override
094: protected void configurePropertiesFromAction(final Action a) {
095: wasConfigureProperties = true;
096: configureFrom = a;
097: super .configurePropertiesFromAction(a);
098: }
099:
100: @Override
101: protected PropertyChangeListener createActionPropertyChangeListener(
102: final Action a) {
103: wasCreateListeners = true;
104: createFrom = a;
105: return super .createActionPropertyChangeListener(a);
106: }
107:
108: @Override
109: public void invalidate() {
110: wasCallInvalidate = true;
111: super .invalidate();
112: }
113:
114: void resetTestedFields() {
115: wasConfigureProperties = false;
116: wasCreateListeners = false;
117: createFrom = null;
118: configureFrom = null;
119: }
120:
121: @Override
122: public void revalidate() {
123: wasCallRevalidate = true;
124: super .revalidate();
125: }
126: }
127:
128: class SimpleActionListener implements ActionListener {
129: ActionEvent actionEvent = null;
130:
131: String name;
132:
133: SimpleActionListener(final String s) {
134: name = s;
135: }
136:
137: public void actionPerformed(final ActionEvent ae) {
138: actionEvent = ae;
139: fireOrder += name;
140: }
141: }
142:
143: class SimpleChangeListener implements ChangeListener {
144: public void stateChanged(final ChangeEvent arg0) {
145: }
146: }
147:
148: class SimplePropertyChangeListener implements
149: PropertyChangeListener {
150: PropertyChangeEvent event;
151:
152: PropertyChangeEvent getEvent() {
153: PropertyChangeEvent e = event;
154: event = null;
155: return e;
156: }
157:
158: public void propertyChange(final PropertyChangeEvent e) {
159: if (e.getPropertyName() != "ancestor") {
160: event = e;
161: }
162: }
163: }
164:
165: private Dimension getPrefferedSize(final JTextField c) {
166: int widthColumn = c.getColumns() * c.getColumnWidth();
167: Dimension dim = c.getPreferredScrollableViewportSize();
168: int width = Math.max(dim.width, widthColumn);
169: return new Dimension(width, dim.height);
170: }
171:
172: void resetBRM(final BoundedRangeModel brm, final int min,
173: final int value, final int ext, final int max) {
174: brm.setMinimum(min);
175: brm.setValue(value);
176: brm.setExtent(ext);
177: brm.setMaximum(max);
178: }
179:
180: @Override
181: protected void setUp() throws Exception {
182: super .setUp();
183: fireOrder = "";
184: bWasException = false;
185: message = null;
186: listener = new SimplePropertyChangeListener();
187: jf = new JFrame();
188: Container container = jf.getContentPane();
189: container.setLayout(new GridLayout(3, 1, 3, 4));
190: jtf = new ExtJTextField(content);
191: jtf.addPropertyChangeListener(listener);
192: jtfBidi = new JTextField(bidiContent);
193: jtfScroll = new JTextField(content);
194: container.add(jtf);
195: container.add(jtfBidi);
196: container.add(new JScrollPane(jtfScroll));
197: ((JViewport) jtfScroll.getParent())
198: .setScrollMode(JViewport.SIMPLE_SCROLL_MODE);
199: jf.pack();
200: }
201:
202: @Override
203: protected void tearDown() throws Exception {
204: jf.dispose();
205: super .tearDown();
206: }
207:
208: public void testAccessibleJTextField() {
209: AccessibleContext accessible = jtf.getAccessibleContext();
210: assertTrue(accessible instanceof JTextField.AccessibleJTextField);
211: assertEquals(jtf.getAccessibleContext(), accessible);
212: JTextField.AccessibleJTextField access = (JTextField.AccessibleJTextField) accessible;
213: AccessibleStateSet ass = access.getAccessibleStateSet();
214: assertNotSame(ass, access.getAccessibleStateSet());
215: assertTrue(ass.contains(AccessibleState.SINGLE_LINE));
216: assertFalse(ass.contains(AccessibleState.MULTI_LINE));
217: }
218:
219: public void testAddRemoveGetActionListener() {
220: SimpleActionListener listener1 = new SimpleActionListener(
221: "first");
222: SimpleActionListener listener2 = new SimpleActionListener(
223: "second");
224: SimpleActionListener listener3 = new SimpleActionListener(
225: "third");
226: ActionListener[] listeners = jtf.getActionListeners();
227: assertEquals(0, listeners.length);
228: jtf.addActionListener(listener1);
229: listeners = jtf.getActionListeners();
230: assertEquals(1, listeners.length);
231: assertEquals(listener1, listeners[0]);
232: jtf.addActionListener(listener2);
233: listeners = jtf.getActionListeners();
234: assertEquals(2, listeners.length);
235: assertEquals(listener2, listeners[0]);
236: assertEquals(listener1, listeners[1]);
237: jtf.addActionListener(listener3);
238: listeners = jtf.getActionListeners();
239: assertEquals(3, listeners.length);
240: assertEquals(listener3, listeners[0]);
241: assertEquals(listener2, listeners[1]);
242: assertEquals(listener1, listeners[2]);
243: ActionListener listeners1[] = jtf.listenerList
244: .getListeners(ActionListener.class);
245: assertEquals(listener3, listeners1[0]);
246: assertEquals(listener2, listeners1[1]);
247: assertEquals(listener1, listeners1[2]);
248: jtf.addActionListener(listener3);
249: listeners = jtf.getActionListeners();
250: assertEquals(4, listeners.length);
251: assertEquals(listener3, listeners[0]);
252: assertEquals(listener3, listeners[1]);
253: assertEquals(listener2, listeners[2]);
254: assertEquals(listener1, listeners[3]);
255: jtf.removeActionListener(listener2);
256: listeners = jtf.getActionListeners();
257: assertEquals(3, listeners.length);
258: assertEquals(listener3, listeners[0]);
259: assertEquals(listener3, listeners[1]);
260: assertEquals(listener1, listeners[2]);
261: jtf.removeActionListener(listener1);
262: listeners = jtf.getActionListeners();
263: assertEquals(2, listeners.length);
264: assertEquals(listener3, listeners[0]);
265: assertEquals(listener3, listeners[1]);
266: jtf.removeActionListener(listener2);
267: listeners = jtf.getActionListeners();
268: assertEquals(2, listeners.length);
269: assertEquals(listener3, listeners[0]);
270: assertEquals(listener3, listeners[1]);
271: jtf.removeActionListener(listener3);
272: listeners = jtf.getActionListeners();
273: assertEquals(1, listeners.length);
274: assertEquals(listener3, listeners[0]);
275: jtf.removeActionListener(listener3);
276: listeners = jtf.getActionListeners();
277: assertEquals(0, listeners.length);
278: }
279:
280: public void testConfigurePropertiesFromAction() {
281: Action action = new DefaultEditorKit.CutAction();
282: jtf.configurePropertiesFromAction(action);
283: assertTrue(jtf.isEnabled());
284: assertEquals(action.getValue(Action.SHORT_DESCRIPTION), jtf
285: .getToolTipText());
286: action.setEnabled(false);
287: action
288: .putValue(Action.SHORT_DESCRIPTION,
289: "THIS IS TOOLTIPTEXT");
290: jtf.configurePropertiesFromAction(action);
291: assertFalse(jtf.isEnabled());
292: assertEquals(action.getValue(Action.SHORT_DESCRIPTION), jtf
293: .getToolTipText());
294: }
295:
296: public void testCreateActionPropertyChangeListener() {
297: Action action = new DefaultEditorKit.CutAction();
298: PropertyChangeListener listener = jtf
299: .createActionPropertyChangeListener(action);
300: assertNotNull(listener);
301: jtf.setAction(action);
302: action.setEnabled(false);
303: action
304: .putValue(Action.SHORT_DESCRIPTION,
305: "THIS IS TOOLTIPTEXT");
306: assertFalse(jtf.isEnabled());
307: assertEquals("THIS IS TOOLTIPTEXT", jtf.getToolTipText());
308: jtf.setAction(null);
309: action.setEnabled(true);
310: action.putValue(Action.SHORT_DESCRIPTION,
311: "THIS IS CHANGED TOOLTIPTEXT");
312: assertTrue(jtf.isEnabled());
313: assertNull(jtf.getToolTipText());
314: }
315:
316: public void testCreateDefaultModel() {
317: Document doc = jtf.createDefaultModel();
318: Document doc1 = jtf.createDefaultModel();
319: assertTrue(doc instanceof PlainDocument);
320: assertNotSame(jtf.getDocument(), doc);
321: assertNotSame(doc1, doc);
322: }
323:
324: public void testFireActionPerformed() {
325: SimpleActionListener listener1 = new SimpleActionListener(
326: "first");
327: SimpleActionListener listener2 = new SimpleActionListener(
328: "second");
329: SimpleActionListener listener3 = new SimpleActionListener(
330: "third");
331: jtf.addActionListener(listener1);
332: jtf.addActionListener(listener2);
333: jtf.addActionListener(listener3);
334: fireOrder = "";
335: jtf.setAction(new DefaultEditorKit.BeepAction() {
336: private static final long serialVersionUID = 1L;
337:
338: @Override
339: public void actionPerformed(final ActionEvent arg0) {
340: fireOrder += "Action";
341: super .actionPerformed(arg0);
342: }
343: });
344: jtf.fireActionPerformed();
345: assertEquals("Actionthirdsecondfirst", fireOrder);
346: }
347:
348: public void testGetActions() {
349: Action editorKitActions[] = jtf.getUI().getEditorKit(jtf)
350: .getActions();
351: Action jtfActions[] = jtf.getActions();
352: assertEquals(editorKitActions.length + 1, jtfActions.length);
353: for (int i = 0; i < jtfActions.length; i++) {
354: boolean wasEqual = false;
355: for (int j = 0; j < editorKitActions.length; j++) {
356: if (jtfActions[i] == editorKitActions[j]) {
357: wasEqual = true;
358: break;
359: }
360: }
361: if (jtfActions[i].getValue(Action.NAME) == "notify-field-accept") {
362: wasEqual = true;
363: }
364: assertTrue(wasEqual);
365: }
366: }
367:
368: public void testGetHorizontalVisibility() {
369: BoundedRangeModel brm = jtf.getHorizontalVisibility();
370: assertTrue(brm instanceof DefaultBoundedRangeModel);
371: assertEquals(0, brm.getMinimum());
372: int prefWidth = jtf.getPreferredSize().width;
373: Insets insets = jtf.getInsets();
374: int hrzInsets = insets.left + insets.right;
375: int maxValue = prefWidth - hrzInsets;
376: assertEquals(0, brm.getValue());
377: assertEquals(maxValue, brm.getExtent());
378: assertEquals(maxValue, brm.getMaximum());
379: assertFalse(brm.getValueIsAdjusting());
380: DefaultBoundedRangeModel dbrm = (DefaultBoundedRangeModel) brm;
381: assertEquals(1, dbrm.listenerList.getListenerCount());
382: dbrm.fireStateChanged();
383: assertEquals(dbrm, dbrm.changeEvent.getSource());
384: assertEquals(1, dbrm.getChangeListeners().length);
385: }
386:
387: public void testGetPreferredSize() {
388: assertEquals(jtf.getPreferredSize(), jtf
389: .getPreferredScrollableViewportSize());
390: jtf.setColumns(10);
391: assertEquals(getPrefferedSize(jtf), jtf.getPreferredSize());
392: jtf.setColumns(500);
393: assertEquals(getPrefferedSize(jtf), jtf.getPreferredSize());
394: }
395:
396: public void testGetUIClassID() {
397: assertEquals("TextFieldUI", jtf.getUIClassID());
398: assertEquals("TextFieldUI", jtfBidi.getUIClassID());
399: assertEquals("TextFieldUI", jtfScroll.getUIClassID());
400: }
401:
402: public void testIsValidateRoot() {
403: assertTrue(jtf.isValidateRoot());
404: assertTrue(jtfBidi.isValidateRoot());
405: assertFalse(jtfScroll.isValidateRoot());
406: }
407:
408: public void testJTextField() {
409: JTextField tf = new JTextField();
410: assertEquals("", tf.getText());
411: assertTrue(tf.getDocument() instanceof PlainDocument);
412: assertEquals(0, tf.getColumns());
413: }
414:
415: public void testJTextFieldDocumentStringInt() {
416: String str1 = "AAA";
417: String str2 = "testJTextFieldDocumentStringInt()";
418: Document doc = new PlainDocument();
419: try {
420: doc.insertString(0, str2, null);
421: } catch (BadLocationException e) {
422: assertTrue("Unexpected exception: " + e.getMessage(), false);
423: }
424: JTextField tf = new JTextField(doc, str2, 8);
425: assertEquals(str2, tf.getText());
426: assertEquals(doc, tf.getDocument());
427: assertEquals(8, tf.getColumns());
428: doc = new PlainDocument();
429: try {
430: doc.insertString(0, str2, null);
431: } catch (BadLocationException e) {
432: assertTrue("Unexpected exception: " + e.getMessage(), false);
433: }
434: tf = new JTextField(doc, null, 6);
435: assertEquals(str2, tf.getText());
436: tf = new JTextField(doc, "", 6);
437: assertEquals("", tf.getText());
438: try {
439: tf = new JTextField(doc, str1, -1);
440: } catch (IllegalArgumentException e) {
441: bWasException = true;
442: message = e.getMessage();
443: }
444: assertTrue(bWasException);
445: assertEquals("columns less than zero.", message);
446: }
447:
448: public void testJTextFieldInt() {
449: JTextField tf = new JTextField(5);
450: assertEquals("", tf.getText());
451: assertTrue(tf.getDocument() instanceof PlainDocument);
452: assertEquals(5, tf.getColumns());
453: try {
454: tf = new JTextField(-1);
455: } catch (IllegalArgumentException e) {
456: bWasException = true;
457: message = e.getMessage();
458: }
459: assertTrue(bWasException);
460: assertEquals("columns less than zero.", message);
461: }
462:
463: public void testJTextFieldString() {
464: String str1 = "testJTextFieldString()";
465: JTextField tf = new JTextField(str1);
466: assertEquals(str1, tf.getText());
467: assertTrue(tf.getDocument() instanceof PlainDocument);
468: assertEquals(0, tf.getColumns());
469: }
470:
471: public void testJTextFieldStringInt() {
472: String str1 = "testJTextFieldString()";
473: JTextField tf = new JTextField(str1, 5);
474: assertEquals(str1, tf.getText());
475: assertTrue(tf.getDocument() instanceof PlainDocument);
476: assertEquals(5, tf.getColumns());
477: try {
478: tf = new JTextField(-1);
479: } catch (IllegalArgumentException e) {
480: bWasException = true;
481: message = e.getMessage();
482: }
483: assertTrue(bWasException);
484: assertEquals("columns less than zero.", message);
485: }
486:
487: public void testNotifyAction() {
488: assertEquals("notify-field-accept", JTextField.notifyAction);
489: }
490:
491: //implementation dependent
492: /*
493: public void testParamString() {
494: jtf.setActionCommand("ACTION_COMMAND");
495: String str = "," +
496: jtf.getX() + "," +
497: jtf.getY() + "," +
498: jtf.getSize().width + "x" + jtf.getSize().height + "," +
499: "layout=" + jtf.getLayout() + "," ;
500: str =str.replaceFirst("@[^,}]*","");
501: str +=
502: "alignmentX=" + "null"+ "," + //1.4.2
503: "alignmentY=" + "null"+ "," + //1.4.2
504: //"alignmentX=" + "0.0"+ "," + //1.5.0
505: //"alignmentY=" + "0.0"+ "," + //1.5.0
506: "border=" + jtf.getBorder() + "," +
507: "flags=296" + "," +
508: "maximumSize=,minimumSize=,preferredSize=," +
509: "caretColor=" + jtf.getCaretColor() + "," +
510: "disabledTextColor=" + jtf.getDisabledTextColor() + "," +
511: "editable=" + jtf.isEditable() + "," +
512: "margin=" + jtf.getMargin() + "," +
513: "selectedTextColor=" + jtf.getSelectedTextColor() + "," +
514: "selectionColor=" + jtf.getSelectionColor() +"," +
515: "columns=" + jtf.getColumns() + "," +
516: "columnWidth=" + jtf.getColumnWidth() + "," +
517: "command=ACTION_COMMAND," +
518: "horizontalAlignment=LEADING";
519: assertEquals(changeString(str), changeString(jtf.paramString()));
520: } */
521: public void testPostActionEvent() {
522: SimpleActionListener listener1 = new SimpleActionListener(
523: "first");
524: SimpleActionListener listener2 = new SimpleActionListener(
525: "second");
526: SimpleActionListener listener3 = new SimpleActionListener(
527: "third");
528: jtf.addActionListener(listener1);
529: jtf.addActionListener(listener2);
530: jtf.addActionListener(listener3);
531: fireOrder = "";
532: jtf.setAction(new DefaultEditorKit.BeepAction() {
533: private static final long serialVersionUID = 1L;
534:
535: @Override
536: public void actionPerformed(final ActionEvent arg0) {
537: fireOrder += "Action";
538: super .actionPerformed(arg0);
539: }
540: });
541: jtf.postActionEvent();
542: assertEquals("Actionthirdsecondfirst", fireOrder);
543: }
544:
545: void testPropertyChangeEvent(final String name, final int oldValue,
546: final int newValue, final PropertyChangeEvent event) {
547: testPropertyChangeEvent(name, new Integer(oldValue),
548: new Integer(newValue), event);
549: }
550:
551: void testPropertyChangeEvent(final String name,
552: final Object oldValue, final Object newValue,
553: final PropertyChangeEvent event) {
554: assertEquals(name, event.getPropertyName());
555: assertEquals(oldValue, event.getOldValue());
556: assertEquals(newValue, event.getNewValue());
557: }
558:
559: public void testScrollRectToVisible() {
560: }
561:
562: public void testSetActionCommand() {
563: SimpleActionListener listener = new SimpleActionListener("");
564: jtf.addActionListener(listener);
565: jtf.fireActionPerformed();
566: ActionEvent ae = listener.actionEvent;
567: assertEquals(jtf.getText(), ae.getActionCommand());
568: jtf.setText("LALALA");
569: jtf.fireActionPerformed();
570: ActionEvent ae1 = listener.actionEvent;
571: assertEquals(jtf.getText(), ae1.getActionCommand());
572: jtf.setActionCommand("JENJA");
573: jtf.fireActionPerformed();
574: ae = listener.actionEvent;
575: assertEquals("JENJA", ae.getActionCommand());
576: jtf.setActionCommand(null);
577: jtf.fireActionPerformed();
578: ae = listener.actionEvent;
579: assertEquals(jtf.getText(), ae.getActionCommand());
580: jtf.setActionCommand("");
581: jtf.fireActionPerformed();
582: ae = listener.actionEvent;
583: assertEquals("", ae.getActionCommand());
584: }
585:
586: public void testSetDocument() {
587: Document old = jtf.getDocument();
588: Document doc = new PlainDocument();
589: jtf.setDocument(doc);
590: testPropertyChangeEvent("document", old, doc, listener.event);
591: assertEquals(Boolean.TRUE, doc.getProperty("filterNewlines"));
592: }
593:
594: public void testSetFont() {
595: Font oldFont = jtf.getFont();
596: FontMetrics fm = jtf.getFontMetrics(oldFont);
597: assertEquals(fm.charWidth('m'), jtf.getColumnWidth());
598: jtf.wasCallRevalidate = false;
599: Font newFont = new java.awt.Font("SimSun", 0, 12);
600: jtf.setFont(newFont);
601: assertTrue(jtf.wasCallRevalidate);
602: fm = jtf.getFontMetrics(newFont);
603: assertEquals(fm.charWidth('m'), jtf.getColumnWidth());
604: //checks PropertyCchanegEvent
605: PropertyChangeEvent event = listener.event;
606: assertEquals("font", event.getPropertyName());
607: assertEquals(oldFont, event.getOldValue());
608: assertEquals(newFont, event.getNewValue());
609: }
610:
611: public void testSetGetAction() {
612: assertNull(jtf.getAction());
613: Action action = new DefaultEditorKit.CutAction();
614: jtf.setAction(action);
615: assertEquals(action, jtf.getAction());
616: assertTrue(jtf.wasConfigureProperties);
617: assertTrue(jtf.wasCreateListeners);
618: assertEquals(action, jtf.configureFrom);
619: assertEquals(action, jtf.createFrom);
620: assertEquals(1, jtf.getActionListeners().length);
621: assertEquals(action, jtf.getActionListeners()[0]);
622: jtf.resetTestedFields();
623: jtf.setAction(null);
624: assertNull(jtf.getAction());
625: assertTrue(jtf.wasConfigureProperties);
626: assertFalse(jtf.wasCreateListeners);
627: assertNull(jtf.configureFrom);
628: assertNull(jtf.createFrom);
629: assertEquals(0, jtf.getActionListeners().length);
630: jtf.resetTestedFields();
631: jtf.addActionListener(action);
632: jtf.setAction(action);
633: assertEquals(action, jtf.getAction());
634: assertTrue(jtf.wasConfigureProperties);
635: assertTrue(jtf.wasCreateListeners);
636: assertEquals(action, jtf.configureFrom);
637: assertEquals(action, jtf.createFrom);
638: assertEquals(1, jtf.getActionListeners().length);
639: assertEquals(action, jtf.getActionListeners()[0]);
640: }
641:
642: public void testSetGetColumns() {
643: assertEquals(0, jtf.getColumns());
644: jtf.wasCallInvalidate = false;
645: jtf.setColumns(5);
646: assertEquals(5, jtf.getColumns());
647: assertTrue(jtf.wasCallInvalidate);
648: try {
649: jtf.setColumns(-5);
650: } catch (IllegalArgumentException e) {
651: bWasException = true;
652: message = e.getMessage();
653: }
654: assertTrue(bWasException);
655: assertEquals("columns less than zero.", message);
656: }
657:
658: public void testSetGetHorizontalAlignment() {
659: assertEquals(SwingConstants.LEADING, jtf
660: .getHorizontalAlignment());
661: jtf.setHorizontalAlignment(SwingConstants.LEFT);
662: assertEquals(SwingConstants.LEFT, jtf.getHorizontalAlignment());
663: testPropertyChangeEvent("horizontalAlignment",
664: SwingConstants.LEADING, SwingConstants.LEFT,
665: listener.event);
666: jtf.setHorizontalAlignment(SwingConstants.CENTER);
667: assertEquals(SwingConstants.CENTER, jtf
668: .getHorizontalAlignment());
669: testPropertyChangeEvent("horizontalAlignment",
670: SwingConstants.LEFT, SwingConstants.CENTER,
671: listener.event);
672: jtf.setHorizontalAlignment(SwingConstants.RIGHT);
673: assertEquals(SwingConstants.RIGHT, jtf.getHorizontalAlignment());
674: testPropertyChangeEvent("horizontalAlignment",
675: SwingConstants.CENTER, SwingConstants.RIGHT,
676: listener.event);
677: jtf.setHorizontalAlignment(SwingConstants.LEADING);
678: assertEquals(SwingConstants.LEADING, jtf
679: .getHorizontalAlignment());
680: testPropertyChangeEvent("horizontalAlignment",
681: SwingConstants.RIGHT, SwingConstants.LEADING,
682: listener.event);
683: jtf.setHorizontalAlignment(SwingConstants.TRAILING);
684: assertEquals(SwingConstants.TRAILING, jtf
685: .getHorizontalAlignment());
686: testPropertyChangeEvent("horizontalAlignment",
687: SwingConstants.LEADING, SwingConstants.TRAILING,
688: listener.event);
689: try {
690: jtf.setHorizontalAlignment(5000);
691: } catch (IllegalArgumentException e) {
692: bWasException = true;
693: message = e.getMessage();
694: }
695: assertTrue(bWasException);
696: assertEquals("horizontalAlignment", message);
697: }
698:
699: int getInitialScrollOffest(final JTextField c) {
700: int prefWidth = c.getPreferredSize().width;
701: int width = c.getWidth();
702: int diff = prefWidth - width;
703: return (diff >= 0) ? diff + 1 : 0;
704: }
705:
706: int getScrollOffset(final JTextField c, final int scrollOffset) {
707: int prefWidth = c.getPreferredSize().width;
708: int width = c.getWidth();
709: int diff = prefWidth - width;
710: int maxScrollOffset = (diff >= 0) ? diff + 1 : 0;
711: return Math.min(Math.max(0, scrollOffset), maxScrollOffset);
712: }
713:
714: void brmTest(final BoundedRangeModel model, final int min,
715: final int value, final int extent, final int max) {
716: assertEquals(min, model.getMinimum());
717: assertEquals(value, model.getValue());
718: assertEquals(extent, model.getExtent());
719: assertEquals(max, model.getMaximum());
720: }
721:
722: public void testSetGetScrollOffset() {
723: assertEquals(getInitialScrollOffest(jtf), jtf.getScrollOffset());
724: //assertEquals(getInitialScrollOffest(jtfScroll),jtfScroll.getScrollOffset());
725: assertEquals(getInitialScrollOffest(jtfBidi), jtfBidi
726: .getScrollOffset());
727: BoundedRangeModel brm = jtf.getHorizontalVisibility();
728: int brm_min = brm.getMinimum();
729: int brm_extent = brm.getExtent();
730: int brm_max = brm.getMaximum();
731: BoundedRangeModel brmScroll = jtfScroll
732: .getHorizontalVisibility();
733: int brmScroll_min = brmScroll.getMinimum();
734: int brmScroll_extent = brmScroll.getExtent();
735: int brmScroll_max = brmScroll.getMaximum();
736: BoundedRangeModel brmBidi = jtfBidi.getHorizontalVisibility();
737: int brmBidi_min = brmBidi.getMinimum();
738: int brmBidi_extent = brmBidi.getExtent();
739: int brmBidi_max = brmBidi.getMaximum();
740: if (!isHarmony()) {
741: return;
742: }
743: for (int i = -3; i < 500; i++) {
744: jtf.setScrollOffset(i);
745: assertEquals(getScrollOffset(jtf, i), jtf.getScrollOffset());
746: brmTest(brm, brm_min, jtf.getScrollOffset(), brm_extent,
747: brm_max);
748: jtfScroll.setScrollOffset(i);
749: assertEquals(getScrollOffset(jtfScroll, i), jtfScroll
750: .getScrollOffset());
751: brmTest(brmScroll, brmScroll_min, jtfScroll
752: .getScrollOffset(), brmScroll_extent, brmScroll_max);
753: jtfBidi.setScrollOffset(i);
754: assertEquals(getScrollOffset(jtfScroll, i), jtfScroll
755: .getScrollOffset());
756: brmTest(brmBidi, brmBidi_min, jtfBidi.getScrollOffset(),
757: brmBidi_extent, brmBidi_max);
758: }
759: }
760:
761: // Regression for HARMONY-2627
762: public void testGetScrollOffset() {
763: jtf = new ExtJTextField("abc");
764: final int viewWidth = (int) jtf.getUI().getRootView(jtf)
765: .getPreferredSpan(View.X_AXIS);
766:
767: assertEquals(viewWidth + 4, jtf.getPreferredSize().width);
768: assertEquals(0, jtf.getScrollOffset());
769: assertEquals(viewWidth + 4 + 1, getInitialScrollOffest(jtf));
770: }
771:
772: public void testSerialization() {
773: /*
774: JTextField jt = new JTextField(bidiContent);
775: jt.setColumns(8);
776: jt.setFont(new java.awt.Font("SimSun", 0, 12));
777: Action action = new DefaultEditorKit.CopyAction();
778: //jt.setAction(action);
779:
780:
781: JTextField jt1 = new JTextField();
782: try {
783: FileOutputStream fo = new FileOutputStream("tmp");
784: ObjectOutputStream so = new ObjectOutputStream(fo);
785: so.writeObject(jt);
786: so.flush();
787: so.close();
788: FileInputStream fi = new FileInputStream("tmp");
789: ObjectInputStream si = new ObjectInputStream(fi);
790: jt1 = (JTextField) si.readObject();
791: si.close();
792: } catch (Exception e) {
793: assertTrue("seralization failed" + e.getMessage(),false);
794: }
795: assertEquals(bidiContent, jt1.getText());
796: assertEquals(8,jt1.getColumns());
797: assertEquals(new java.awt.Font("SimSun", 0, 12),jt1.getFont());
798: assertNotSame(jt.getHorizontalVisibility(), jt1.getHorizontalVisibility());
799: assertEquals(jt.getScrollOffset(), jt1.getScrollOffset());
800: assertEquals(jt.getColumnWidth(), jt1.getColumnWidth());
801: assertNotSame(jt.getAccessibleContext(), jt1.getAccessibleContext());
802: */
803: }
804: }
|