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.Point;
023: import java.awt.Rectangle;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.JFrame;
026: import javax.swing.JTextArea;
027: import javax.swing.JTextField;
028: import javax.swing.SwingConstants;
029: import javax.swing.SwingUtilities;
030: import javax.swing.SwingWaitTestCase;
031: import junit.framework.TestCase;
032:
033: public class JTextComponent_MultithreadedTest extends TestCase {
034: JFrame jf;
035:
036: JTextArea jtc;
037:
038: boolean bWasException;
039:
040: String s;
041:
042: String sRTL = "\u05DC";
043:
044: String sLTR = "\u0061";
045:
046: JTextField jep;
047:
048: Rectangle rect;
049:
050: @Override
051: protected void setUp() throws Exception {
052: jf = new JFrame();
053: bWasException = false;
054: s = null;
055: jtc = new JTextArea("just test");
056: jf.getContentPane().add(jtc);
057: jf.setLocation(200, 300);
058: jf.setSize(300, 200);
059: jf.pack();
060: super .setUp();
061: }
062:
063: @Override
064: protected void tearDown() throws Exception {
065: jf.dispose();
066: super .tearDown();
067: }
068:
069: public void testReplaceSelection() throws Exception {
070: Runnable test = new Runnable() {
071: public void run() {
072: jtc.setText("JTextComponent");
073: jtc.select(5, 8);
074: }
075: };
076: SwingUtilities.invokeAndWait(test);
077: jtc.replaceSelection(null);
078: assertEquals("JTextponent", jtc.getText());
079: assertEquals("pon", jtc.getSelectedText());
080: jtc.replaceSelection("XXX");
081: assertEquals("XXX", jtc.getSelectedText());
082: assertEquals("JTextXXXent", jtc.getText());
083: setCaretsAlwaysUpdatePolicy();
084: jtc.replaceSelection("XXX");
085: assertEquals("JTextXXXent", jtc.getText());
086: assertEquals(8, jtc.getCaretPosition());
087: Runnable test1 = new Runnable() {
088: public void run() {
089: jtc.setText("JTextComponent");
090: jtc.setCaretPosition(2);
091: }
092: };
093: SwingUtilities.invokeAndWait(test1);
094: jtc.replaceSelection("XXX");
095: assertNull(jtc.getSelectedText());
096: assertEquals("JTXXXextComponent", jtc.getText());
097: }
098:
099: private void setCaretsAlwaysUpdatePolicy() {
100: DefaultCaret caret = (DefaultCaret) jtc.getCaret();
101: if (BasicSwingTestCase.isHarmony()) {
102: caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
103: } else {
104: caret.setAsynchronousMovement(true);
105: }
106: }
107:
108: public void testModelToView() throws Exception {
109: Runnable test = new Runnable() {
110: public void run() {
111: jtc.setText(sLTR + sRTL + sLTR + sRTL + sLTR);
112: }
113: };
114: SwingUtilities.invokeAndWait(test);
115: Rectangle sample = null;
116: rect = null;
117: try {
118: rect = jtc.modelToView(2);
119: sample = jtc.getUI().modelToView(jtc, 2);
120: assertNotNull(sample);
121: assertNotNull(rect);
122: assertEquals(sample, rect);
123: rect = jtc.modelToView(3);
124: sample = jtc.getUI().modelToView(jtc, 3);
125: } catch (BadLocationException e) {
126: assertFalse("Unexpected exception: " + e.getMessage(), true);
127: }
128: assertNotNull(sample);
129: assertNotNull(rect);
130: assertEquals(sample, rect);
131: }
132:
133: public void testViewToModel() throws Exception {
134: Runnable test = new Runnable() {
135: public void run() {
136: jtc.setText("test View To Model");
137: }
138: };
139: SwingUtilities.invokeAndWait(test);
140: Rectangle r = null;
141: try {
142: r = jtc.modelToView(2);
143: assertNotNull(r);
144: assertEquals(2, jtc.viewToModel(new Point(r.x, r.y)));
145: r = jtc.modelToView(4);
146: assertNotNull(r);
147: assertEquals(4, jtc.viewToModel(new Point(r.x, r.y)));
148: } catch (BadLocationException e) {
149: }
150: }
151:
152: void scrollableIncrementTest(final JTextComponent jtc,
153: final Rectangle rect) {
154: assertEquals(rect.width / 10, jtc.getScrollableUnitIncrement(
155: rect, SwingConstants.HORIZONTAL, -1));
156: assertEquals(rect.width / 10, jtc.getScrollableUnitIncrement(
157: rect, SwingConstants.HORIZONTAL, 0));
158: assertEquals(rect.width / 10, jtc.getScrollableUnitIncrement(
159: rect, SwingConstants.HORIZONTAL, 1));
160: assertEquals(rect.height / 10, jtc.getScrollableUnitIncrement(
161: rect, SwingConstants.VERTICAL, -1));
162: assertEquals(rect.height / 10, jtc.getScrollableUnitIncrement(
163: rect, SwingConstants.VERTICAL, 0));
164: assertEquals(rect.height / 10, jtc.getScrollableUnitIncrement(
165: rect, SwingConstants.VERTICAL, 1));
166: assertEquals(rect.width, jtc.getScrollableBlockIncrement(rect,
167: SwingConstants.HORIZONTAL, -1));
168: assertEquals(rect.width, jtc.getScrollableBlockIncrement(rect,
169: SwingConstants.HORIZONTAL, 0));
170: assertEquals(rect.width, jtc.getScrollableBlockIncrement(rect,
171: SwingConstants.HORIZONTAL, 1));
172: assertEquals(rect.height, jtc.getScrollableBlockIncrement(rect,
173: SwingConstants.VERTICAL, -1));
174: assertEquals(rect.height, jtc.getScrollableBlockIncrement(rect,
175: SwingConstants.VERTICAL, 0));
176: assertEquals(rect.height, jtc.getScrollableBlockIncrement(rect,
177: SwingConstants.VERTICAL, 1));
178: }
179:
180: private String bigString(final String s, final int k) {
181: String str = "";
182: for (int i = 0; i < k; i++) {
183: str += s;
184: }
185: return str;
186: }
187:
188: public void testScrollable() throws Exception {
189: jf.dispose();
190: jf = new JFrame();
191: jep = new JTextField();
192: jf.getContentPane().add(jep);
193: jf.setLocation(200, 300);
194: jf.setSize(500, 500);
195: Runnable test = new Runnable() {
196: public void run() {
197: jep.setText(bigString((bigString("a", 10)
198: + bigString("\u05DC", 10) + "\n"), 10));
199: }
200: };
201: SwingUtilities.invokeAndWait(test);
202: if (!BasicSwingTestCase.isHarmony()) {
203: jf.setVisible(true);
204: SwingWaitTestCase.isRealized(jf);
205: } else {
206: jf.pack();
207: }
208: assertFalse(jep.getScrollableTracksViewportHeight());
209: assertFalse(jep.getScrollableTracksViewportWidth());
210: assertEquals(jep.getPreferredSize(), jep
211: .getPreferredScrollableViewportSize());
212: Rectangle rect = null;
213: try {
214: rect = jep.modelToView(20);
215: assertNotNull(rect);
216: scrollableIncrementTest(jep, rect);
217: rect = jep.modelToView(101);
218: } catch (BadLocationException e) {
219: assertFalse("Unexpected exception :" + e.getMessage(), true);
220: }
221: scrollableIncrementTest(jep, rect);
222: rect.x = rect.x + 2;
223: scrollableIncrementTest(jep, rect);
224: rect.y = rect.y + 14;
225: rect.x = rect.x - 2;
226: scrollableIncrementTest(jep, rect);
227: rect.height = rect.height + 6;
228: scrollableIncrementTest(jep, rect);
229: rect.height = rect.height - 10;
230: scrollableIncrementTest(jep, rect);
231: rect.width = rect.width + 3;
232: scrollableIncrementTest(jep, rect);
233: rect.width = rect.width - 7;
234: scrollableIncrementTest(jep, rect);
235: rect.x = rect.x + 1000;
236: rect.y = rect.y + 4000;
237: rect.height = rect.height + 1013;
238: scrollableIncrementTest(jep, rect);
239: rect.height = rect.height - 3013;
240: scrollableIncrementTest(jep, rect);
241: rect.width = rect.width + 5011;
242: scrollableIncrementTest(jep, rect);
243: rect.width = rect.width - 8011;
244: scrollableIncrementTest(jep, rect);
245: try {
246: jep.getScrollableBlockIncrement(rect, 4, 1);
247: } catch (IllegalArgumentException e) {
248: bWasException = true;
249: s = e.getMessage();
250: }
251: assertTrue(bWasException);
252: assertEquals("Invalid orientation: 4", s);
253: bWasException = false;
254: s = null;
255: try {
256: jep.getScrollableUnitIncrement(rect, 4, 1);
257: } catch (IllegalArgumentException e) {
258: bWasException = true;
259: s = e.getMessage();
260: }
261: assertTrue(bWasException);
262: assertEquals("Invalid orientation: 4", s);
263: }
264: }
|