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 javax.swing.BasicSwingTestCase;
023: import javax.swing.JFrame;
024: import javax.swing.JTextArea;
025: import javax.swing.SwingUtilities;
026: import javax.swing.SwingWaitTestCase;
027:
028: public class DefaultCaret_MultithreadedTest extends BasicSwingTestCase {
029: AbstractDocument ad;
030:
031: boolean bWasException = false;
032:
033: DefaultCaret dc = null;
034:
035: JFrame jf = null;
036:
037: JTextArea jta = null;
038:
039: String s = null;
040:
041: int newDot;
042:
043: public DefaultCaret_MultithreadedTest(final String name) {
044: super (name);
045: }
046:
047: @Override
048: protected void setUp() throws Exception {
049: super .setUp();
050: jta = new JTextArea(
051: "JTextArea for DefaultCaret Testing\n***\n*%%%**");
052: dc = new DefaultCaret();
053: jf = new JFrame();
054: bWasException = false;
055: s = null;
056: ad = (AbstractDocument) jta.getDocument();
057: jf.getContentPane().add(jta);
058: jta.getCaret().deinstall(jta);
059: jta.setCaret(dc);
060: jf.setLocation(100, 100);
061: jf.setSize(350, 200);
062: jf.setVisible(true);
063: }
064:
065: @Override
066: protected void tearDown() throws Exception {
067: jf.dispose();
068: super .tearDown();
069: }
070:
071: Runnable setDot = new Runnable() {
072: public void run() {
073: dc.setDot(newDot);
074: }
075: };
076:
077: public void testAsyncUpdate() throws Exception {
078: waitForIdle();
079: SwingWaitTestCase.isRealized(jf);
080: ad.writeLock();
081: try {
082: ad.insertString(0, "insert", null);
083: } catch (BadLocationException e) {
084: bWasException = true;
085: s = e.getMessage();
086: }
087: ad.writeUnlock();
088: waitForIdle();
089: assertFalse("Unexpected exception: " + s, bWasException);
090: assertEquals(0, dc.getDot());
091: newDot = 4;
092: SwingUtilities.invokeAndWait(setDot);
093: waitForIdle();
094: ad.writeLock();
095: try {
096: ad.remove(0, 3);
097: } catch (BadLocationException e) {
098: bWasException = true;
099: s = e.getMessage();
100: }
101: ad.writeUnlock();
102: waitForIdle();
103: assertEquals(4, dc.getDot());
104: //dc.setAsynchronousMovement(true); //1.4.2
105: if (isHarmony()) {
106: dc.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
107: } else {
108: dc.setAsynchronousMovement(true);
109: }
110: newDot = 0;
111: SwingUtilities.invokeAndWait(setDot);
112: waitForIdle();
113: ad.writeLock();
114: try {
115: ad.insertString(0, "insert", null);
116: } catch (BadLocationException e) {
117: bWasException = true;
118: s = e.getMessage();
119: }
120: ad.writeUnlock();
121: assertEquals(6, dc.getDot());
122: SwingWaitTestCase.isRealized(jf);
123: assertFalse("Unexpected exception: " + s, bWasException);
124: assertEquals(6, dc.getDot());
125: newDot = 4;
126: SwingUtilities.invokeAndWait(setDot);
127: waitForIdle();
128: ad.writeLock();
129: try {
130: ad.remove(0, 3);
131: } catch (BadLocationException e) {
132: bWasException = true;
133: s = e.getMessage();
134: }
135: ad.writeUnlock();
136: waitForIdle();
137: assertFalse("Unexpected exception: " + s, bWasException);
138: assertEquals(1, dc.getDot());
139: }
140: }
|