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 junit.framework.TestCase;
023:
024: public class JEditorPane_MultithreadedTest extends TestCase {
025: JEditorPane jep;
026:
027: JFrame jf;
028:
029: SetTextMaster masterSetText = new SetTextMaster();
030:
031: MakeSelectionMaster masterMakeSelection = new MakeSelectionMaster();
032:
033: UnsupportedOperationException ex;
034:
035: @Override
036: protected void setUp() throws Exception {
037: super .setUp();
038: ex = null;
039: jep = new JEditorPane();
040: jf = new JFrame();
041: jf.getContentPane().add(jep);
042: jf.setSize(200, 300);
043: jf.pack();
044: }
045:
046: @Override
047: protected void tearDown() throws Exception {
048: jf.dispose();
049: super .tearDown();
050: }
051:
052: final class MakeSelectionMaster implements Runnable {
053: int dot;
054:
055: int mark;
056:
057: public void setDotAndMark(final int d, final int m) {
058: dot = d;
059: mark = m;
060: }
061:
062: public void setDot(final int i) {
063: setDotAndMark(i, i);
064: }
065:
066: public void run() {
067: jep.getCaret().setDot(mark);
068: jep.getCaret().moveDot(dot);
069: }
070: }
071:
072: final class SetTextMaster implements Runnable {
073: String text;
074:
075: public void setText(final String s) {
076: text = s;
077: }
078:
079: public void run() {
080: jep.setText(text);
081: }
082: }
083:
084: public void testReplaceSelection() throws Exception {
085: masterSetText.setText("testReplaceSelection");
086: masterMakeSelection.setDotAndMark(7, 4);
087: SwingUtilities.invokeAndWait(masterSetText);
088: SwingUtilities.invokeAndWait(masterMakeSelection);
089: jep.replaceSelection("XXX");
090: assertEquals("testXXXlaceSelection", jep.getText());
091: masterMakeSelection.setDotAndMark(4, 2);
092: SwingUtilities.invokeAndWait(masterMakeSelection);
093: jep.replaceSelection(null);
094: assertEquals("teXXXlaceSelection", jep.getText());
095: masterMakeSelection.setDotAndMark(2, 0);
096: SwingUtilities.invokeAndWait(masterMakeSelection);
097: jep.replaceSelection(null);
098: assertEquals("XXXlaceSelection", jep.getText());
099: masterMakeSelection.setDotAndMark(3, 3);
100: SwingUtilities.invokeAndWait(masterMakeSelection);
101: jep.replaceSelection("YYY");
102: assertEquals("XXXYYYlaceSelection", jep.getText());
103: }
104:
105: public void testReplaceSelectionNotEditable() throws Exception {
106: SwingUtilities.invokeAndWait(new Runnable() {
107: public void run() {
108: jep.setText("replaceSelectionNotEditable");
109: jep.setEditable(false);
110: jep.setSelectionStart(3);
111: jep.setSelectionEnd(5);
112: }
113: });
114: jep.replaceSelection("AAAA");
115: assertEquals("la", jep.getSelectedText());
116: assertEquals("replaceSelectionNotEditable", jep.getText());
117: }
118:
119: //temporary commented: DefaultStyledDocument not implemented
120: /*public void testReplaceSelectionWithAttributes() throws Exception {
121: final AbstractDocument doc = new DefaultStyledDocument();
122: SimpleAttributeSet as1 = new SimpleAttributeSet();
123: as1.addAttribute("key1", "value1");
124: SimpleAttributeSet as2 = new SimpleAttributeSet();
125: as2.addAttribute("key2", "value2");
126:
127: try {
128: doc.insertString(0, "testReplaceSelection", as1);
129: doc.insertString(4, "INSERT", as2);
130: } catch (final BadLocationException e) {
131: assertFalse("unexpected exception :" + e.getMessage(), true);
132: }
133: SwingUtilities.invokeAndWait(new Runnable() {
134: public void run() {
135: try {
136: jep.setEditorKit(new RTFEditorKit());
137: jep.setDocument(doc);
138: } catch(UnsupportedOperationException e){
139: ex = e;
140: }
141: }
142: });
143: if (ex != null)
144: return;
145: masterMakeSelection.setDotAndMark(7, 6);
146: SwingUtilities.invokeAndWait(masterMakeSelection);
147:
148: jep.replaceSelection("YYY");
149:
150: for (int i = 0; i < doc.getLength(); i++) {
151: AttributeSet as = getAttributeSetByIndex(doc, i);
152: if (i > 3 && i < 12)
153: assertEquals(as2, as);
154: else
155: assertEquals(as1, as);
156: }
157: }*/
158: public void testSetGetTextPlain() throws Exception {
159: jep.setText(JEditorPaneTest.plainString);
160: SwingUtilities.invokeAndWait(new Runnable() {
161: public void run() {
162: assertEquals("plain", JEditorPaneTest.plainString, jep
163: .getText());
164: }
165: });
166: };
167:
168: public void testSetGetTextRtf() throws Exception {
169: SwingUtilities.invokeAndWait(new Runnable() {
170: public void run() {
171: try {
172: jep.setContentType("text/rtf");
173: } catch (UnsupportedOperationException e) {
174: ex = e;
175: }
176: }
177: });
178: if (ex != null) {
179: return;
180: }
181: jep.setText(JEditorPaneTest.rtfString);
182: SwingUtilities.invokeAndWait(new Runnable() {
183: public void run() {
184: assertEquals("blablabla\n", JEditorPaneTest
185: .getDocContent(jep.getDocument()));
186: }
187: });
188: };
189:
190: public void testSetGetTextHTML() throws Exception {
191: SwingUtilities.invokeAndWait(new Runnable() {
192: public void run() {
193: try {
194: jep.setContentType("text/html");
195: } catch (UnsupportedOperationException e) {
196: ex = e;
197: }
198: }
199: });
200: if (ex != null) {
201: return;
202: }
203: jep.setText(JEditorPaneTest.htmlString);
204: SwingUtilities.invokeAndWait(new Runnable() {
205: public void run() {
206: assertEquals(JEditorPaneTest
207: .removeMeta(JEditorPaneTest.htmlString),
208: JEditorPaneTest.removeMeta(jep.getText()
209: .replaceAll("\n", "")));
210: }
211: });
212: };
213: }
|