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.Color;
023: import java.awt.Point;
024: import java.awt.Rectangle;
025: import java.awt.event.FocusEvent;
026: import java.awt.event.FocusListener;
027: import java.awt.event.InputEvent;
028: import java.awt.event.MouseEvent;
029: import java.awt.event.MouseListener;
030: import java.awt.event.MouseMotionListener;
031: import java.util.EventListener;
032: import javax.swing.ExtJTextArea;
033: import javax.swing.JFrame;
034: import javax.swing.JTextArea;
035: import javax.swing.SwingTestCase;
036: import javax.swing.event.CaretEvent;
037: import javax.swing.event.CaretListener;
038: import javax.swing.event.ChangeEvent;
039: import javax.swing.event.ChangeListener;
040: import javax.swing.plaf.TextUI;
041: import javax.swing.text.Position.Bias;
042:
043: public class DefaultCaretTest extends SwingTestCase {
044: JTextArea jta = null;
045:
046: JFrame jf = null;
047:
048: DefaultCaret dc = null;
049:
050: Rectangle r1, r2, r3, r4, r5, r6, r;
051:
052: int tCompListenersCount;
053:
054: //int tmp;
055: Point p;
056:
057: Highlighter.HighlightPainter pnt;
058:
059: AbstractDocument ad;
060:
061: boolean bWasException = false;
062:
063: String s = null;
064:
065: String fireTest = "";
066:
067: String sRTL = new String("\u05DC");
068:
069: String sLTR = new String("\u0061");
070:
071: Color c1 = new Color(234);
072:
073: Color c2 = new Color(235);
074:
075: SimpleNavigationFilter filter;
076:
077: SimpleChangeListener CHL;
078:
079: class SimpleChangeListenerForFire implements ChangeListener {
080: int flag = 0;
081:
082: String s1;
083:
084: SimpleChangeListenerForFire(final String s) {
085: s1 = s;
086: }
087:
088: public void stateChanged(final ChangeEvent e) {
089: flag = 1;
090: fireTest = fireTest + s1;
091: //System.out.println("state changed");
092: }
093: }
094:
095: class SimpleChangeListener implements ChangeListener {
096: int flag = 0;
097:
098: public void stateChanged(final ChangeEvent e) {
099: flag = 1;
100: //System.out.println("state changed");
101: }
102: }
103:
104: class SimpleCaretListener implements CaretListener {
105: public void caretUpdate(final CaretEvent e) {
106: System.out.println("caret update");
107: }
108: }
109:
110: class SimpleNavigationFilter extends NavigationFilter {
111: int flagSetDot = 0;
112:
113: int flagMoveDot = 0;
114:
115: @Override
116: public void setDot(final FilterBypass f, final int i,
117: final Bias b) {
118: flagSetDot = 1;
119: super .setDot(f, i, b);
120: }
121:
122: @Override
123: public void moveDot(final FilterBypass arg0, final int i,
124: final Bias b) {
125: flagMoveDot = 1;
126: super .moveDot(arg0, i, b);
127: }
128: }
129:
130: @Override
131: protected void setUp() throws Exception {
132: super .setUp();
133: jta = new ExtJTextArea(
134: "JTextArea for DefaultCaret Testing\n***\n*%%%**");
135: dc = new DefaultCaret();
136: jf = new JFrame();
137: p = new Point(5, 16);
138: bWasException = false;
139: s = null;
140: r = null;
141: r1 = null;
142: r2 = null;
143: r3 = null;
144: r4 = null;
145: r5 = null;
146: r6 = null;
147: ad = (AbstractDocument) jta.getDocument();
148: jf.getContentPane().add(jta);
149: jta.getCaret().deinstall(jta);
150: tCompListenersCount = jta.getListeners(MouseListener.class).length
151: + jta.getListeners(MouseMotionListener.class).length
152: + jta.getListeners(FocusListener.class).length
153: + jta.getPropertyChangeListeners().length;
154: jta.setCaret(dc);
155: jf.setLocation(100, 100);
156: jf.setSize(350, 200);
157: jf.pack();
158: init();
159: }
160:
161: public void init() {
162: try {
163: r1 = jta.modelToView(6);
164: r2 = jta.modelToView(10);
165: r3 = jta.modelToView(15);
166: r4 = jta.modelToView(2);
167: TextUI textUI = jta.getUI();
168: r5 = textUI.modelToView(jta, 5, Position.Bias.Backward);
169: r6 = textUI.modelToView(jta, 7, Position.Bias.Forward);
170: assertNotNull(r1);
171: assertNotNull(r2);
172: assertNotNull(r3);
173: assertNotNull(r4);
174: } catch (BadLocationException e) {
175: bWasException = true;
176: s = e.getMessage();
177: }
178: assertFalse("Unexpected exception " + s, bWasException);
179: }
180:
181: @Override
182: protected void tearDown() throws Exception {
183: jf.dispose();
184: super .tearDown();
185: }
186:
187: /*
188: * If or not elem in list
189: *
190: */
191: boolean findListener(final EventListener[] list,
192: final EventListener elem) {
193: if (list == null) {
194: return false;
195: }
196: for (int i = 0; i < list.length; i++) {
197: if (list[i] == elem) {
198: return true;
199: }
200: }
201: return false;
202: }
203:
204: /**
205: * Constructor for DefaultCaretTest.
206: *
207: * @param arg0
208: */
209: public DefaultCaretTest(final String arg0) {
210: super (arg0);
211: }
212:
213: public void testDefaultCaret() {
214: assertNotNull(dc);
215: }
216:
217: public void testGetComponent() {
218: assertEquals(jta, dc.getComponent());
219: }
220:
221: /*
222: * Tests that listeners was added to JTextComponent. Doesn't test that
223: * DocumentListener was added to JTextComponent. Doesn't test that Dot and
224: * Mark is 0.
225: */
226: public void testInstall() {
227: int tCompListenersCountCurrent = jta
228: .getListeners(MouseListener.class).length
229: + jta.getListeners(MouseMotionListener.class).length
230: + jta.getListeners(FocusListener.class).length
231: + jta.getPropertyChangeListeners().length;
232: assertEquals(tCompListenersCount,
233: tCompListenersCountCurrent - 4);
234:
235: try { // Regression test for HARMONY-1750
236: new DefaultCaret().install(null);
237: fail("NullPointerException should have been thrown");
238: } catch (NullPointerException e) {
239: // Expected
240: }
241: }
242:
243: /*
244: * Tests that listeners was removed from JTextComponent. Doesn't test that
245: * Document listener was removed.
246: */
247: public void testDeinstall() throws Exception {
248: int i = jta.getListeners(MouseListener.class).length;
249: int j = jta.getListeners(MouseMotionListener.class).length;
250: int k = jta.getListeners(FocusListener.class).length;
251: int l = jta.getPropertyChangeListeners().length;
252: dc.deinstall(jta);
253: assertNull(dc.getComponent());
254: assertEquals(i,
255: jta.getListeners(MouseListener.class).length + 1);
256: assertEquals(j,
257: jta.getListeners(MouseMotionListener.class).length + 1);
258: assertEquals(k,
259: jta.getListeners(FocusListener.class).length + 1);
260: assertEquals(l, jta.getPropertyChangeListeners().length + 1);
261: assertFalse(findListener(jta.getListeners(MouseListener.class),
262: dc));
263: assertFalse(findListener(jta.getListeners(MouseListener.class),
264: dc));
265: assertFalse(findListener(jta.getListeners(FocusListener.class),
266: dc));
267: }
268:
269: /*
270: * Tests methods IsVisible and SetVisible
271: */
272: public void testIsVisible() throws Exception {
273: dc.setVisible(true);
274: assertTrue("dc.isVisible()= false after"
275: + " dc.setVisible(true)", dc.isVisible());
276: dc.setVisible(false);
277: assertFalse("dc.isVisible()= true after"
278: + " dc.setVisible(false)", dc.isVisible());
279: }
280:
281: /*
282: * Tests methods isSelectionVisible and setSelectionVisible
283: */
284: public void testIsSelectionVisible() throws Exception {
285: assertNotNull(dc);
286: dc.setSelectionVisible(true);
287: assertTrue("dc.isSelectionVisible()= false after"
288: + " dc.setSelectionVisible(true)", dc
289: .isSelectionVisible());
290: dc.setSelectionVisible(false);
291: assertFalse("dc.isSelectionVisible()= true after"
292: + " dc.setSelectionVisible(false)", dc
293: .isSelectionVisible());
294: }
295:
296: public void testEquals() {
297: assertFalse(dc.equals(new DefaultCaret()));
298: assertTrue(dc.equals(dc));
299: assertTrue(dc.equals(jta.getCaret()));
300: }
301:
302: /*
303: * Tests addChangeListener,removeChangeListener, getChangeListener
304: */
305: public void testChangeListeners() {
306: try {
307: SimpleChangeListener t1 = new SimpleChangeListener();
308: SimpleChangeListener t2 = new SimpleChangeListener();
309: SimpleChangeListener t3 = new SimpleChangeListener();
310: dc.addChangeListener(t1);
311: assertTrue(findListener(dc.getChangeListeners(), t1));
312: assertTrue(findListener(dc.listenerList
313: .getListeners(ChangeListener.class), t1));
314: dc.addChangeListener(t2);
315: assertTrue(findListener(dc.getChangeListeners(), t1));
316: assertTrue(findListener(dc.listenerList
317: .getListeners(ChangeListener.class), t1));
318: assertTrue(findListener(dc.getChangeListeners(), t2));
319: assertTrue(findListener(dc.listenerList
320: .getListeners(ChangeListener.class), t2));
321: dc.addChangeListener(t3);
322: assertTrue(findListener(dc.getChangeListeners(), t1));
323: assertTrue(findListener(dc.listenerList
324: .getListeners(ChangeListener.class), t1));
325: assertTrue(findListener(dc.getChangeListeners(), t2));
326: assertTrue(findListener(dc.listenerList
327: .getListeners(ChangeListener.class), t2));
328: assertTrue(findListener(dc.getChangeListeners(), t3));
329: assertTrue(findListener(dc.listenerList
330: .getListeners(ChangeListener.class), t3));
331: dc.removeChangeListener(t1);
332: assertFalse(findListener(dc.getChangeListeners(), t1));
333: assertFalse(findListener(dc.listenerList
334: .getListeners(ChangeListener.class), t1));
335: dc.removeChangeListener(t2);
336: assertFalse(findListener(dc.getChangeListeners(), t1));
337: assertFalse(findListener(dc.listenerList
338: .getListeners(ChangeListener.class), t1));
339: assertFalse(findListener(dc.getChangeListeners(), t2));
340: assertFalse(findListener(dc.listenerList
341: .getListeners(ChangeListener.class), t2));
342: dc.removeChangeListener(t3);
343: assertFalse(findListener(dc.getChangeListeners(), t1));
344: assertFalse(findListener(dc.listenerList
345: .getListeners(ChangeListener.class), t1));
346: assertFalse(findListener(dc.getChangeListeners(), t2));
347: assertFalse(findListener(dc.listenerList
348: .getListeners(ChangeListener.class), t2));
349: assertFalse(findListener(dc.getChangeListeners(), t3));
350: assertFalse(findListener(dc.listenerList
351: .getListeners(ChangeListener.class), t3));
352: } catch (NullPointerException e) {
353: bWasException = true;
354: s = e.getMessage();
355: }
356: assertFalse("Unexpected exception: " + s, bWasException);
357: }
358:
359: public void testGetBlinkRate() throws Exception {
360: assertEquals(0, dc.getBlinkRate());
361: dc.setBlinkRate(100);
362: assertEquals(100, dc.getBlinkRate());
363:
364: try { // Regression test for HARMONY-1795
365: dc.setBlinkRate(-1);
366: fail("IllegalArgumentException should have been thrown");
367: } catch (IllegalArgumentException e) {
368: // Expected
369: }
370: }
371:
372: public void testSetDot() throws Exception {
373: dc.setDot(8);
374: dc.moveDot(10);
375: dc.setDot(5);
376: assertEquals(5, dc.getDot());
377: assertEquals(5, dc.getMark());
378: }
379:
380: public void testMoveDot() throws Exception {
381: dc.setDot(4);
382: dc.moveDot(9);
383: assertEquals("tArea", jta.getSelectedText());
384: dc.moveDot(11);
385: assertEquals("tArea f", jta.getSelectedText());
386: }
387:
388: public void testGetDot() throws Exception {
389: jta.setSelectionStart(11);
390: jta.setSelectionEnd(14);
391: assertEquals(14, dc.getDot());
392: assertEquals(11, dc.getMark());
393: }
394:
395: void checkValues(final int dot, final Position.Bias dotBias) {
396: assertEquals(dot, dc.getDot());
397: assertEquals(dot, dc.getMark());
398: assertEquals(dotBias, dc.getDotBias());
399: }
400:
401: /*
402: * Tryes to set caret position by Mouse Events (dot = mark)
403: */
404: public void testPositionCaret() throws Exception {
405: Position.Bias bias[] = new Position.Bias[1];
406: int tmp;
407: jta.setText(sLTR + sRTL + sLTR + sRTL + sLTR + sRTL + sLTR
408: + sRTL);
409: //dc.setDot(8);
410: //dc.moveDot(10);
411: dc.positionCaret(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED,
412: 0, 0, jta.getX() + r1.x, jta.getY() + r1.y, 0, false));
413: tmp = jta.getUI().viewToModel(jta, new Point(r1.x, r1.y), bias);
414: checkValues(tmp, bias[0]);
415: //checkValues(6,6,Position.Bias.Backward);
416: dc.positionCaret(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED,
417: 0, 0, jta.getX() + r5.x, jta.getY() + r5.y, 0, false));
418: tmp = jta.getUI().viewToModel(jta, new Point(r5.x, r5.y), bias);
419: checkValues(tmp, bias[0]);
420: //checkValues(4,4,Position.Bias.Forward);
421: dc.positionCaret(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED,
422: 0, 0, jta.getX() + r6.x, jta.getY() + r6.y, 0, false));
423: tmp = jta.getUI().viewToModel(jta, new Point(r6.x, r6.y), bias);
424: checkValues(tmp, bias[0]);
425: //checkValues(6,6,Position.Bias.Forward);
426: }
427:
428: /*
429: * Tryes to move caret position by Mouse Events (dot = mark)
430: */
431: public void testMoveCaret() throws Exception {
432: dc.positionCaret(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED,
433: 0, 0, jta.getX() + r1.x, jta.getY() + r1.y, 0, false));
434: dc.moveCaret(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED, 0,
435: 0, jta.getX() + r2.x, jta.getY() + r2.y, 0, false));
436: assertEquals(10, dc.getDot());
437: assertEquals(6, dc.getMark());
438: assertEquals(10, jta.getCaretPosition());
439: dc.moveCaret(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED, 0,
440: 0, jta.getX() + r3.x, jta.getY() + r3.y, 0, false));
441: assertEquals(15, dc.getDot());
442: assertEquals(6, dc.getMark());
443: assertEquals(15, jta.getCaretPosition());
444: }
445:
446: /*
447: * Tests setMagicCaretPosition and getMagicCaretPosition
448: */
449: public void testSetMagicCaretPosition() throws Exception {
450: dc.setMagicCaretPosition(p);
451: assertTrue(dc.getMagicCaretPosition().equals(p));
452: }
453:
454: // TODO: may be add other listeners (not ChangeListener)
455: public void testGetListeners() {
456: EventListener[] EvLList = null;
457: SimpleChangeListener ChL1 = new SimpleChangeListener();
458: SimpleChangeListener ChL2 = new SimpleChangeListener();
459: //SimpleCaretListener CL1 = new SimpleCaretListener();
460: //SimpleCaretListener CL2 = new SimpleCaretListener();
461: dc.addChangeListener(ChL1);
462: dc.addChangeListener(ChL2);
463: EvLList = dc.getListeners(ChangeListener.class);
464: assertTrue(findListener(EvLList, ChL1));
465: assertTrue(findListener(EvLList, ChL2));
466: //assertEquals(EvLList.length,2);
467: if ((EvLList != null) & (dc.getChangeListeners() != null)) {
468: assertEquals(EvLList.length, dc.getChangeListeners().length);
469: //dc.addChangeListener(ChL1);
470: //dc.addChangeListener(ChL2);
471: }
472: }
473:
474: /*
475: * To tests adjustVisibility method class ExtJTextArea(ejta) is used. If it
476: * was invoked ejta.scrollRectToVisible then ejta.flag would become 1.
477: */
478: public void testAdjustVisibility() throws Exception {
479: try {
480: r = jta.modelToView(8);
481: if (r == null) {
482: assertNull(r);
483: return;
484: }
485: } catch (BadLocationException e) {
486: bWasException = true;
487: s = e.getMessage();
488: }
489: assertFalse("Unexpected exception: " + s, bWasException);
490: ((ExtJTextArea) jta).flag = 0;
491: dc.adjustVisibility(r1);
492: assertEquals(1, ((ExtJTextArea) jta).flag);
493: }
494:
495: // Regression for HARMONY-2780
496: public void testAdjustVisibilityNull() {
497: new DefaultCaret().adjustVisibility(null);
498: // No exception is expected
499: }
500:
501: public void testFireStateChanged() {
502: SimpleChangeListenerForFire CHL1 = new SimpleChangeListenerForFire(
503: "L1");
504: SimpleChangeListenerForFire CHL2 = new SimpleChangeListenerForFire(
505: "L2");
506: SimpleChangeListenerForFire CHL3 = new SimpleChangeListenerForFire(
507: "L3");
508: dc.addChangeListener(CHL1);
509: dc.addChangeListener(CHL2);
510: dc.addChangeListener(CHL3);
511: assertEquals(0, CHL1.flag);
512: assertEquals(0, CHL2.flag);
513: assertEquals(0, CHL3.flag);
514: assertEquals(fireTest, "");
515: dc.fireStateChanged();
516: assertEquals(1, CHL1.flag);
517: assertEquals(1, CHL2.flag);
518: assertEquals(1, CHL3.flag);
519: assertEquals(fireTest, "L3L2L1");
520: }
521:
522: public void testGetSelectionPainter() throws Exception {
523: Highlighter.Highlight[] h = jta.getHighlighter()
524: .getHighlights();
525: pnt = dc.getSelectionPainter();
526: assertNotNull("DefaultCaret.getSelectionPainter()=null", pnt);
527: if (pnt != null) {
528: assertTrue(pnt instanceof DefaultHighlighter.DefaultHighlightPainter);
529: try {
530: jta.getHighlighter().addHighlight(4, 9, pnt);
531: } catch (BadLocationException e) {
532: bWasException = true;
533: s = e.getMessage();
534: }
535: }
536: assertFalse("Unexpected exception: " + s, bWasException);
537: if (pnt == null) {
538: return;
539: }
540: h = jta.getHighlighter().getHighlights();
541: assertEquals(1, h.length);
542: assertEquals(h[0].getPainter(), dc.getSelectionPainter());
543: assertEquals(4, h[0].getStartOffset());
544: assertEquals(9, h[0].getEndOffset());
545:
546: // Regression for HARMONY-1768
547: DefaultCaret obj = new DefaultCaret() {
548: public Highlighter.HighlightPainter getSelectionPainter() {
549: return super .getSelectionPainter();
550: }
551: };
552: assertNotNull(obj.getSelectionPainter());
553: }
554:
555: public void testFocusGained() throws Exception {
556: jta.setEditable(true);
557: dc.focusGained(new FocusEvent(jta, FocusEvent.FOCUS_GAINED));
558: assertTrue(dc.isVisible());
559: dc.focusLost(new FocusEvent(jta, FocusEvent.FOCUS_LOST));
560: jta.setEditable(false);
561: dc.focusGained(new FocusEvent(jta, FocusEvent.FOCUS_GAINED));
562: assertFalse(dc.isVisible());
563: jta.setEditable(true);
564: jta.setEnabled(false);
565: dc.focusGained(new FocusEvent(jta, FocusEvent.FOCUS_GAINED));
566: assertFalse(dc.isVisible());
567: }
568:
569: public void testFocusLost() throws Exception {
570: dc.focusLost(new FocusEvent(jta, FocusEvent.FOCUS_LOST));
571: assertFalse(dc.isVisible());
572: }
573:
574: public void testMouseDragged() throws Exception {
575: dc.mousePressed(new MouseEvent(jta, MouseEvent.MOUSE_PRESSED,
576: 0, InputEvent.BUTTON1_MASK, jta.getX() + r1.x, jta
577: .getY()
578: + r1.y, 0, false, MouseEvent.BUTTON1));
579: dc.mouseDragged(new MouseEvent(jta, MouseEvent.MOUSE_DRAGGED,
580: 0, InputEvent.BUTTON1_MASK, jta.getX() + r3.x, jta
581: .getY()
582: + r3.y, 0, false, MouseEvent.BUTTON1));
583: assertEquals("rea for D", jta.getSelectedText());
584: /*
585: * dc.mousePressed(new MouseEvent(jta, MouseEvent.MOUSE_PRESSED, 0,
586: * InputEvent.BUTTON1_MASK, jta.getX() + r4.x, jta .getY() + r4.y, 0,
587: * false, MouseEvent.BUTTON1));
588: */
589: dc.mouseDragged(new MouseEvent(jta, MouseEvent.MOUSE_DRAGGED,
590: 0, InputEvent.BUTTON1_MASK, jta.getX() + r4.x, jta
591: .getY()
592: + r4.y, 0, false, MouseEvent.BUTTON1));
593: assertEquals("extA", jta.getSelectedText());
594: /*
595: * dc.mouseDragged(new MouseEvent(jta, MouseEvent.MOUSE_DRAGGED, 0,
596: * InputEvent.BUTTON3_MASK, jta.getX() + r2.x, jta .getY() + r2.y, 0,
597: * false)); try { assertEquals("JTextA", jta.getSelectedText()); } catch
598: * (AssertionFailedError e) { e3 = e; }
599: *
600: * dc.mouseDragged(new MouseEvent(jta, MouseEvent.MOUSE_DRAGGED, 0,
601: * InputEvent.BUTTON1_MASK, jta.getX() + r3.x, jta .getY() + r3.y, 0,
602: * false)); try { assertEquals("JTextArea for D",
603: * jta.getSelectedText()); } catch (AssertionFailedError e) { e4 = e; }
604: */
605: }
606:
607: public void testMouseClicked() throws Exception {
608: dc.mouseClicked(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED,
609: 0, InputEvent.BUTTON1_DOWN_MASK, jta.getX() + r1.x, jta
610: .getY()
611: + r1.y, 2, false, MouseEvent.BUTTON1));
612: assertEquals("JTextArea", jta.getSelectedText());
613: dc.setDot(5);
614: dc.mouseClicked(new MouseEvent(jta, MouseEvent.MOUSE_CLICKED,
615: 0, InputEvent.BUTTON1_DOWN_MASK, jta.getX() + r1.x, jta
616: .getY()
617: + r1.y, 3, false, MouseEvent.BUTTON1));
618: assertEquals("JTextArea for DefaultCaret Testing", jta
619: .getSelectedText());
620: }
621:
622: public void testMousePressed() throws Exception {
623: dc.mousePressed(new MouseEvent(jta, MouseEvent.MOUSE_PRESSED,
624: 0, InputEvent.SHIFT_DOWN_MASK, jta.getX() + r3.x, jta
625: .getY()
626: + r3.y, 0, false, MouseEvent.BUTTON1));
627: assertEquals("JTextArea for D", jta.getSelectedText());
628: }
629:
630: public void testDocInsChange_DotEqMark() throws Exception {
631: try {
632: dc.setDot(5);
633: jta.getDocument().insertString(3, "insert", null);
634: assertFalse("Unexpected exception: " + s, bWasException);
635: assertEquals(11, dc.getDot());
636: dc.setDot(5);
637: jta.getDocument().insertString(2, "ins", null);
638: assertFalse("Unexpected exception: " + s, bWasException);
639: assertEquals(8, dc.getDot());
640: dc.setDot(5);
641: jta.getDocument().insertString(5, "inse", null);
642: assertFalse("Unexpected exception: " + s, bWasException);
643: assertEquals(9, dc.getDot());
644: dc.setDot(5);
645: jta.getDocument().insertString(7, "insert", null);
646: assertEquals(5, dc.getDot());
647: } catch (BadLocationException e) {
648: bWasException = true;
649: s = e.getMessage();
650: }
651: assertFalse("Unexpected exception: " + s, bWasException);
652: }
653:
654: public void testDocInsChange_DotNotEqMark() throws Exception {
655: try {
656: dc.setDot(11);
657: dc.moveDot(6);
658: jta.getDocument().insertString(2, "in", null);
659: assertEquals(8, dc.getDot());
660: assertEquals(13, dc.getMark());
661: dc.setDot(11);
662: dc.moveDot(6);
663: jta.getDocument().insertString(6, "ins", null);
664: assertEquals(9, dc.getDot());
665: assertEquals(14, dc.getMark());
666: dc.setDot(11);
667: dc.moveDot(6);
668: jta.getDocument().insertString(7, "inser", null);
669: assertEquals(6, dc.getDot());
670: assertEquals(16, dc.getMark());
671: dc.setDot(6);
672: dc.moveDot(11);
673: jta.getDocument().insertString(11, "insert", null);
674: assertEquals(17, dc.getDot());
675: assertEquals(6, dc.getMark());
676: } catch (BadLocationException e) {
677: bWasException = true;
678: s = e.getMessage();
679: }
680: assertFalse("Unexpected exception: " + s, bWasException);
681: }
682:
683: public void testDocRemoveChange_DotEqMark() throws Exception {
684: dc.setDot(5);
685: try {
686: jta.getDocument().remove(3, 1);
687: } catch (BadLocationException e) {
688: bWasException = true;
689: s = e.getMessage();
690: }
691: assertEquals(4, dc.getDot());
692: dc.setDot(5);
693: try {
694: jta.getDocument().remove(2, 3);
695: } catch (BadLocationException e) {
696: bWasException = true;
697: s = e.getMessage();
698: }
699: assertEquals(2, dc.getDot());
700: dc.setDot(5);
701: try {
702: jta.getDocument().remove(3, 7);
703: } catch (BadLocationException e) {
704: bWasException = true;
705: s = e.getMessage();
706: }
707: assertEquals(3, dc.getDot());
708: dc.setDot(5);
709: try {
710: jta.getDocument().remove(5, 8);
711: } catch (BadLocationException e) {
712: bWasException = true;
713: s = e.getMessage();
714: }
715: assertEquals(5, dc.getDot());
716: dc.setDot(5);
717: try {
718: jta.getDocument().remove(7, 2);
719: } catch (BadLocationException e) {
720: bWasException = true;
721: s = e.getMessage();
722: }
723: assertEquals(5, dc.getDot());
724: assertFalse("Unexpected exception: " + s, bWasException);
725: }
726:
727: public void testDocRemoveChange_DotNotEqMark() throws Exception {
728: dc.setDot(11);
729: dc.moveDot(6);
730: try {
731: jta.getDocument().remove(3, 2);
732: assertEquals(4, dc.getDot());
733: assertEquals(9, dc.getMark());
734: dc.setDot(11);
735: dc.moveDot(6);
736: jta.getDocument().remove(3, 4);
737: assertEquals(3, dc.getDot());
738: assertEquals(7, dc.getMark());
739: dc.setDot(11);
740: dc.moveDot(6);
741: jta.getDocument().remove(6, 2);
742: assertEquals(6, dc.getDot());
743: assertEquals(9, dc.getMark());
744: dc.setDot(6);
745: dc.moveDot(11);
746: jta.getDocument().remove(10, 7);
747: assertEquals(10, dc.getDot());
748: assertEquals(6, dc.getMark());
749: } catch (BadLocationException e) {
750: bWasException = true;
751: s = e.getMessage();
752: }
753: assertFalse("Unexpected exception: " + s, bWasException);
754: }
755:
756: /*
757: * for 1.5.0
758: *
759: */
760: public void testDocNeverUpdate() throws Exception {
761: dc.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
762: dc.setDot(6);
763: try {
764: jta.getDocument().remove(3, 10);
765: assertEquals(6, dc.getDot());
766: dc.setDot(11);
767: dc.moveDot(6);
768: jta.getDocument().remove(3, 10);
769: assertEquals(6, dc.getDot());
770: assertEquals(11, dc.getMark());
771: dc.setDot(6);
772: jta.getDocument().remove(2, 20);
773: assertEquals(5, dc.getDot());
774: dc.setDot(3);
775: dc.moveDot(1);
776: jta.getDocument().remove(2, 3);
777: assertEquals(1, dc.getDot());
778: assertEquals(2, dc.getMark());
779: } catch (BadLocationException e) {
780: bWasException = true;
781: s = e.getMessage();
782: }
783: assertFalse("Unexpected exception: " + s, bWasException);
784: }
785:
786: /*
787: * for 1.5.0
788: *
789: */
790: public void testConstants() {
791: assertEquals(DefaultCaret.ALWAYS_UPDATE, 2);
792: assertEquals(DefaultCaret.NEVER_UPDATE, 1);
793: assertEquals(DefaultCaret.UPDATE_WHEN_ON_EDT, 0);
794: }
795:
796: /*
797: * for 1.5.0
798: *
799: */
800: public void testSetGetUpdatePolicy() {
801: dc.setUpdatePolicy(0);
802: assertEquals(0, dc.getUpdatePolicy());
803: dc.setUpdatePolicy(1);
804: assertEquals(1, dc.getUpdatePolicy());
805: dc.setUpdatePolicy(2);
806: assertEquals(2, dc.getUpdatePolicy());
807: //According to documentation there it was Exception
808: //but 1.5 never produces it
809: try {
810: dc.setUpdatePolicy(3);
811: } catch (IllegalArgumentException e) {
812: bWasException = true;
813: s = e.getMessage();
814: }
815: if (isHarmony()) {
816: assertTrue("Not thrown Expected exception: " + s,
817: bWasException);
818: }
819: }
820:
821: public void testCaretColor() throws Exception {
822: //TODO Runnable
823: jta.setSelectionColor(c1);
824: jta.setCaretColor(c2);
825: assertEquals(c1, jta.getSelectionColor());
826: assertEquals(c2, jta.getCaretColor());
827: }
828:
829: void checkFlag(final int i1) {
830: assertEquals(i1, CHL.flag);
831: CHL.flag = 0;
832: }
833:
834: public void testInvokeFireStateChanged() throws Exception {
835: CHL = new SimpleChangeListener();
836: dc.addChangeListener(CHL);
837: dc.setDot(5);
838: checkFlag(1);
839: dc.moveDot(8);
840: checkFlag(1);
841: dc.setVisible(true);
842: checkFlag(0);
843: dc.setVisible(false);
844: checkFlag(0);
845: dc.setSelectionVisible(true);
846: checkFlag(0);
847: dc.setSelectionVisible(false);
848: checkFlag(0);
849: jta.setCaretColor(new Color(235));
850: checkFlag(0);
851: jta.setCaretColor(new Color(235));
852: checkFlag(0);
853: jta.setSelectionColor(new Color(235));
854: checkFlag(0);
855: dc.setBlinkRate(100);
856: checkFlag(0);
857: jta.setDocument(new PlainDocument());
858: checkFlag(1);
859: jta.getCaret().deinstall(jta);
860: checkFlag(0);
861: dc.install(new JTextArea("JTextArea"));
862: checkFlag(0);
863: }
864:
865: public void testToString() throws Exception {
866: dc.setDot(4);
867: dc.moveDot(9);
868: assertEquals("Dot=(9, Forward) Mark=(4, Forward)", dc
869: .toString());
870: }
871:
872: //Serialization is not supported now by Swing
873: /*public void testSerialization() throws Exception {
874:
875: dc.setDot(4, Position.Bias.Backward);
876: dc.setBlinkRate(100);
877: dc.setVisible(true);
878: dc.setSelectionVisible(true);
879: dc.setMagicCaretPosition(new Point(200, 300));
880:
881: DefaultCaret dc1 = new DefaultCaret();
882:
883: try {
884: FileOutputStream fo = new FileOutputStream("tmp");
885: ObjectOutputStream so = new ObjectOutputStream(fo);
886: so.writeObject(dc);
887: so.flush();
888: so.close();
889: FileInputStream fi = new FileInputStream("tmp");
890: ObjectInputStream si = new ObjectInputStream(fi);
891: dc1 = (DefaultCaret) si.readObject();
892: si.close();
893: } catch (Exception e) {
894: assertTrue("seralization failed" + e.getMessage(),false);
895: }
896:
897: assertEquals("Dot=(4, Backward) Mark=(4, Backward)", dc1.toString());
898: assertTrue(jta.equals(dc.getComponent()));
899: assertTrue(dc1.isSelectionVisible());
900: assertEquals(100, dc1.getBlinkRate());
901: assertTrue(dc1.getMagicCaretPosition().equals(new Point(200, 300)));
902: assertTrue(dc1.getSelectionPainter() instanceof DefaultHighlighter.DefaultHighlightPainter);
903: assertTrue(dc1.listenerList.toString().equals(
904: dc.listenerList.toString()));
905: assertNull(dc1.changeEvent);
906:
907: } */
908: /*
909: * for 1.4.2
910: */
911: /*
912: * public void testSetGetAsynchronousMovement(){
913: * assertFalse(dc.getAsynchronousMovement());
914: * dc.setAsynchronousMovement(true);
915: * assertTrue(dc.getAsynchronousMovement()); }
916: */
917: public void testNavigationFilter() throws Exception {
918: assertNull(jta.getNavigationFilter());
919: filter = new SimpleNavigationFilter();
920: jta.setNavigationFilter(filter);
921: dc.setDot(3);
922: dc.moveDot(4);
923: assertEquals(1, filter.flagMoveDot);
924: assertEquals(1, filter.flagSetDot);
925: }
926:
927: //TODO
928: public void testIsActive() throws Exception {
929: dc.setVisible(true);
930: assertTrue(dc.isActive());
931: dc.setVisible(false);
932: assertFalse(dc.isActive());
933: }
934:
935: public void test3820() {
936: DefaultCaret dc = new DefaultCaret();
937: assertEquals(0, dc.getChangeListeners().length);
938: dc.addChangeListener(null);
939: assertEquals(0, dc.getChangeListeners().length);
940: }
941:
942: public void test4208() {
943: dc.setSelectionVisible(false);
944: jta.selectAll();
945: assertEquals(0, jta.getHighlighter().getHighlights().length);
946: dc.setSelectionVisible(true);
947: assertEquals(1, jta.getHighlighter().getHighlights().length);
948: }
949: }
|