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.JFrame;
023: import javax.swing.JTextArea;
024: import javax.swing.SwingTestCase;
025:
026: public class DefaultCaret_BidiTest extends SwingTestCase {
027: AbstractDocument ad;
028:
029: boolean bWasException = false;
030:
031: DefaultCaret dc = null;
032:
033: JFrame jf = null;
034:
035: JTextArea jta = null;
036:
037: String s = null;
038:
039: String sLTR = new String("\u0061");
040:
041: String sRTL = new String("\u05DC");
042:
043: public DefaultCaret_BidiTest(final String name) {
044: super (name);
045: }
046:
047: void dotTest(final int dot, final Position.Bias bias, final String s) {
048: assertEquals(dot, dc.getDot());
049: assertEquals(bias, dc.getDotBias());
050: assertEquals(s, dc.toString());
051: }
052:
053: void insertCase(final int offset, final String ins, final int sDot,
054: final Position.Bias sBias, final int gDot,
055: final Position.Bias gBias) {
056: jta.setText(sLTR + sRTL + sLTR + sRTL + sLTR + sRTL);
057: dc.setDot(sDot, sBias);
058: try {
059: ad.insertString(offset, ins, null);
060: } catch (BadLocationException e) {
061: bWasException = true;
062: s = e.getMessage();
063: }
064: assertFalse("Unexpected exception: " + s, bWasException);
065: assertEquals(gDot, dc.getDot());
066: assertEquals(gBias, dc.getDotBias());
067: }
068:
069: void removeCase(final String sText, final int offset,
070: final int length, final int sDot,
071: final Position.Bias sBias, final int gDot,
072: final Position.Bias gBias) {
073: jta.setText(sText);
074: dc.setDot(sDot, sBias);
075: try {
076: ad.remove(offset, length);
077: } catch (BadLocationException e) {
078: bWasException = true;
079: s = e.getMessage();
080: }
081: assertFalse("Unexpected exception: " + s, bWasException);
082: assertEquals(gDot, dc.getDot());
083: assertEquals(gBias, dc.getDotBias());
084: }
085:
086: void setDotCase(final int dot, final Position.Bias bias,
087: final String s) {
088: dc.setDot(dot, bias);
089: assertEquals(dot, dc.getDot());
090: assertEquals(bias, dc.getDotBias());
091: String tmp = "Dot=(" + dc.getDot() + ", "
092: + dc.getDotBias().toString() + ") " + "Mark=("
093: + dc.getMark() + ", ";
094: assertEquals(tmp + s, dc.toString());
095: }
096:
097: @Override
098: protected void setUp() throws Exception {
099: super .setUp();
100: jta = new JTextArea(sLTR + sRTL + sLTR + sRTL + sLTR + sRTL);
101: dc = new DefaultCaret();
102: jf = new JFrame();
103: bWasException = false;
104: s = null;
105: ad = (AbstractDocument) jta.getDocument();
106: jf.getContentPane().add(jta);
107: jta.getCaret().deinstall(jta);
108: jta.setCaret(dc);
109: jf.setLocation(100, 100);
110: jf.setSize(350, 200);
111: jf.pack();
112: }
113:
114: @Override
115: protected void tearDown() throws Exception {
116: jf.dispose();
117: super .tearDown();
118: }
119:
120: public void testBidiInsert() throws Exception {
121: insertCase(0, sLTR, 2, Position.Bias.Forward, 3,
122: Position.Bias.Forward);
123: insertCase(1, sLTR, 2, Position.Bias.Forward, 3,
124: Position.Bias.Forward);
125: insertCase(2, sLTR, 2, Position.Bias.Forward, 3,
126: Position.Bias.Backward);//b=f
127: insertCase(3, sLTR, 2, Position.Bias.Forward, 2,
128: Position.Bias.Forward);
129: insertCase(0, sRTL, 2, Position.Bias.Forward, 3,
130: Position.Bias.Forward);
131: insertCase(1, sRTL, 2, Position.Bias.Forward, 3,
132: Position.Bias.Forward);
133: insertCase(2, sRTL, 2, Position.Bias.Forward, 3,
134: Position.Bias.Backward);
135: insertCase(3, sRTL, 2, Position.Bias.Forward, 2,
136: Position.Bias.Forward);
137: insertCase(0, sLTR, 2, Position.Bias.Backward, 3,
138: Position.Bias.Backward);
139: insertCase(1, sLTR, 2, Position.Bias.Backward, 3,
140: Position.Bias.Backward);
141: insertCase(2, sLTR, 2, Position.Bias.Backward, 3,
142: Position.Bias.Backward);
143: insertCase(3, sLTR, 2, Position.Bias.Backward, 2,
144: Position.Bias.Backward);
145: insertCase(0, sRTL, 2, Position.Bias.Backward, 3,
146: Position.Bias.Backward);
147: insertCase(1, sRTL, 2, Position.Bias.Backward, 3,
148: Position.Bias.Backward);
149: insertCase(2, sRTL, 2, Position.Bias.Backward, 3,
150: Position.Bias.Backward);
151: insertCase(3, sRTL, 2, Position.Bias.Backward, 2,
152: Position.Bias.Backward);
153: insertCase(1, sLTR, 3, Position.Bias.Forward, 4,
154: Position.Bias.Forward);
155: insertCase(2, sLTR, 3, Position.Bias.Forward, 4,
156: Position.Bias.Forward);
157: insertCase(3, sLTR, 3, Position.Bias.Forward, 4,
158: Position.Bias.Backward);
159: insertCase(4, sLTR, 3, Position.Bias.Forward, 3,
160: Position.Bias.Forward);
161: insertCase(1, sRTL, 3, Position.Bias.Forward, 4,
162: Position.Bias.Forward);
163: insertCase(2, sRTL, 3, Position.Bias.Forward, 4,
164: Position.Bias.Forward);
165: insertCase(3, sRTL, 3, Position.Bias.Forward, 4,
166: Position.Bias.Backward);
167: insertCase(4, sRTL, 3, Position.Bias.Forward, 3,
168: Position.Bias.Forward);
169: insertCase(1, sLTR, 3, Position.Bias.Backward, 4,
170: Position.Bias.Backward);
171: insertCase(2, sLTR, 3, Position.Bias.Backward, 4,
172: Position.Bias.Backward);
173: insertCase(3, sLTR, 3, Position.Bias.Backward, 4,
174: Position.Bias.Backward);
175: insertCase(4, sLTR, 3, Position.Bias.Backward, 3,
176: Position.Bias.Backward);
177: insertCase(1, sRTL, 3, Position.Bias.Backward, 4,
178: Position.Bias.Backward);
179: insertCase(2, sRTL, 3, Position.Bias.Backward, 4,
180: Position.Bias.Backward);
181: insertCase(3, sRTL, 3, Position.Bias.Backward, 4,
182: Position.Bias.Backward);
183: insertCase(4, sRTL, 3, Position.Bias.Backward, 3,
184: Position.Bias.Backward);
185: insertCase(0, sRTL + sRTL, 2, Position.Bias.Backward, 4,
186: Position.Bias.Backward);
187: insertCase(1, sRTL + sRTL, 2, Position.Bias.Backward, 4,
188: Position.Bias.Backward);
189: insertCase(2, sRTL + sRTL, 2, Position.Bias.Backward, 4,
190: Position.Bias.Backward);
191: insertCase(3, sRTL + sRTL, 2, Position.Bias.Backward, 2,
192: Position.Bias.Backward);
193: }
194:
195: public void testBidiRemove() throws Exception {
196: String s1 = sLTR + sRTL + sLTR + sRTL + sLTR + sRTL + sLTR
197: + sRTL + sLTR;
198: String s2 = sRTL + sRTL + sLTR + sLTR + sRTL + sRTL + sLTR
199: + sLTR + sRTL;
200: //s1 = sLTR + sRTL + sLTR + sRTL + sLTR + sRTL + sLTR + sRTL +
201: // sLTR;
202: removeCase(s1, 2, 1, 3, Position.Bias.Forward, 2,
203: Position.Bias.Forward);
204: //removeCase(s1,2,3,3,Position.Bias.Forward,2,Position.Bias.Forward);//b=f
205: removeCase(s1, 2, 1, 3, Position.Bias.Backward, 2,
206: Position.Bias.Backward);
207: removeCase(s1, 2, 3, 3, Position.Bias.Backward, 2,
208: Position.Bias.Backward);
209: //removeCase(s1,1,2,3,Position.Bias.Forward,1,Position.Bias.Backward);//!!!
210: //removeCase(s1,1,3,3,Position.Bias.Forward,1,Position.Bias.Backward);//b=f
211: removeCase(s1, 1, 2, 3, Position.Bias.Backward, 1,
212: Position.Bias.Backward);
213: removeCase(s1, 1, 3, 3, Position.Bias.Backward, 1,
214: Position.Bias.Backward);
215: removeCase(s1, 3, 1, 3, Position.Bias.Forward, 3,
216: Position.Bias.Backward); // b=f
217: removeCase(s1, 3, 2, 3, Position.Bias.Forward, 3,
218: Position.Bias.Backward); // !!!
219: removeCase(s1, 3, 1, 3, Position.Bias.Backward, 3,
220: Position.Bias.Backward);
221: removeCase(s1, 3, 2, 3, Position.Bias.Backward, 3,
222: Position.Bias.Backward);
223: //s2 = sRTL + sRTL + sLTR + sLTR + sRTL + sRTL + sLTR + sLTR +
224: // sRTL;
225: //removeCase(s2,2,1,3,Position.Bias.Forward,2,Position.Bias.Backward);//b=f
226: // begin
227: //removeCase(s2,2,3,3,Position.Bias.Forward,2,Position.Bias.Backward);//
228: // b=f
229: removeCase(s2, 2, 1, 3, Position.Bias.Backward, 2,
230: Position.Bias.Backward);
231: removeCase(s2, 2, 3, 3, Position.Bias.Backward, 2,
232: Position.Bias.Backward);
233: //removeCase(s2,1,2,3,Position.Bias.Forward,1,Position.Bias.Backward);
234: // //b=f
235: //removeCase(s2,1,3,3,Position.Bias.Forward,1,Position.Bias.Backward);
236: // //b=f begin
237: removeCase(s2, 1, 2, 3, Position.Bias.Backward, 1,
238: Position.Bias.Backward);
239: removeCase(s2, 1, 3, 3, Position.Bias.Backward, 1,
240: Position.Bias.Backward);
241: //removeCase(s2,3,1,3,Position.Bias.Forward,3,Position.Bias.Backward);//!!!
242: //removeCase(s2,3,2,3,Position.Bias.Forward,3,Position.Bias.Backward);//b=f
243: removeCase(s2, 3, 1, 3, Position.Bias.Backward, 3,
244: Position.Bias.Backward);
245: removeCase(s2, 3, 2, 3, Position.Bias.Backward, 3,
246: Position.Bias.Backward);
247: }
248:
249: public void testBidiSetDot() throws Exception {
250: setDotCase(0, Position.Bias.Forward, "Forward)");
251: setDotCase(1, Position.Bias.Backward, "Backward)");
252: setDotCase(2, Position.Bias.Forward, "Forward)");
253: setDotCase(3, Position.Bias.Backward, "Backward)");
254: setDotCase(ad.getLength(), Position.Bias.Backward, "Backward)");
255: }
256:
257: public void testBidiMoveDot() throws Exception {
258: jta.setText(sLTR + sRTL + sLTR + sRTL + sLTR);
259: dc.setDot(1, Position.Bias.Backward);
260: dotTest(1, Position.Bias.Backward,
261: "Dot=(1, Backward) Mark=(1, Backward)");
262: dc.moveDot(3);
263: dotTest(3, Position.Bias.Forward,
264: "Dot=(3, Forward) Mark=(1, Backward)");
265: dc.setDot(2, Position.Bias.Forward);
266: dotTest(2, Position.Bias.Forward,
267: "Dot=(2, Forward) Mark=(2, Forward)");
268: dc.moveDot(3);
269: dotTest(3, Position.Bias.Forward,
270: "Dot=(3, Forward) Mark=(2, Forward)");
271: }
272: }
|