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.accessibility.AccessibleText;
025: import javax.swing.BasicSwingTestCase;
026: import javax.swing.JFrame;
027: import javax.swing.JTextArea;
028: import javax.swing.JTextField;
029: import javax.swing.SwingUtilities;
030: import junit.framework.TestCase;
031:
032: public class JTextComponent_AccessibleJTextComponent_variousTextTest
033: extends TestCase {
034: JFrame jf;
035:
036: JTextComponent jtc;
037:
038: JTextComponent.AccessibleJTextComponent accessible;
039:
040: boolean bWasException;
041:
042: String str;
043:
044: JTextField jep;
045:
046: Rectangle rect;
047:
048: String text;
049:
050: @Override
051: protected void setUp() throws Exception {
052: super .setUp();
053: jf = new JFrame();
054: bWasException = false;
055: jtc = new JTextArea();
056: text = ("JTextComponent\n"
057: + "cJTextComponent.AccessibleJTextComponent");
058: jtc.setText(text);
059: accessible = (JTextComponent.AccessibleJTextComponent) jtc
060: .getAccessibleContext();
061: jf.getContentPane().add(jtc);
062: jf.setLocation(200, 300);
063: jf.setSize(200, 300);
064: jf.pack();
065: }
066:
067: @Override
068: protected void tearDown() throws Exception {
069: jf.dispose();
070: super .tearDown();
071: }
072:
073: private Rectangle getCharBounds(final JTextComponent component,
074: final int ind) {
075: Rectangle rect1 = null;
076: Rectangle rect2 = null;
077: try {
078: rect1 = component.getUI().modelToView(component, ind,
079: Position.Bias.Forward);
080: } catch (BadLocationException e) {
081: }
082: try {
083: rect2 = component.getUI().modelToView(component, ind + 1,
084: Position.Bias.Backward);
085: } catch (BadLocationException e) {
086: }
087: assertNotNull(rect1);
088: assertNotNull(rect2);
089: rect1.width = Math.abs(rect1.x - rect2.x) + 1;
090: rect1.x = Math.min(rect1.x, rect2.x);
091: return rect1;
092: }
093:
094: public void testGetBeforeAfterAtIndex() throws Exception {
095: Runnable test = new Runnable() {
096: public void run() {
097: jtc.setText("In\n"
098: + " this test. for AccessibleJTextComponent\n"
099: + "LALALA");
100: }
101: };
102: SwingUtilities.invokeAndWait(test);
103: assertEquals("t", accessible.getAtIndex(
104: AccessibleText.CHARACTER, 4));
105: assertEquals("this", accessible.getAtIndex(AccessibleText.WORD,
106: 4));
107: assertEquals(" this test. for AccessibleJTextComponent\n",
108: accessible.getAtIndex(AccessibleText.SENTENCE, 4));
109: assertEquals(" ", accessible.getAtIndex(
110: AccessibleText.CHARACTER, 3));
111: assertEquals(" ", accessible.getAtIndex(AccessibleText.WORD, 3));
112: assertEquals(" this test. for AccessibleJTextComponent\n",
113: accessible.getAtIndex(AccessibleText.SENTENCE, 3));
114: assertEquals(" ", accessible.getAtIndex(
115: AccessibleText.CHARACTER, 8));
116: assertEquals(" ", accessible.getAtIndex(AccessibleText.WORD, 8));
117: assertEquals(" this test. for AccessibleJTextComponent\n",
118: accessible.getAtIndex(AccessibleText.SENTENCE, 8));
119: assertEquals("h", accessible.getAfterIndex(
120: AccessibleText.CHARACTER, 4));
121: assertEquals("LALALA\n", accessible.getAfterIndex(
122: AccessibleText.SENTENCE, 4));
123: assertEquals("t", accessible.getAfterIndex(
124: AccessibleText.CHARACTER, 3));
125: assertEquals("this", accessible.getAfterIndex(
126: AccessibleText.WORD, 3));
127: assertEquals("LALALA\n", accessible.getAfterIndex(
128: AccessibleText.SENTENCE, 3));
129: assertEquals("t", accessible.getAfterIndex(
130: AccessibleText.CHARACTER, 8));
131: assertEquals("test", accessible.getAfterIndex(
132: AccessibleText.WORD, 8));
133: assertEquals("LALALA\n", accessible.getAfterIndex(
134: AccessibleText.SENTENCE, 8));
135: assertNull(accessible.getAfterIndex(AccessibleText.SENTENCE,
136: jtc.getDocument().getLength() - 2));
137: assertEquals(" ", accessible.getBeforeIndex(
138: AccessibleText.CHARACTER, 4));
139: assertEquals(" ", accessible.getBeforeIndex(
140: AccessibleText.WORD, 4));
141: assertEquals("In\n", accessible.getBeforeIndex(
142: AccessibleText.SENTENCE, 4));
143: assertEquals("\n", accessible.getBeforeIndex(
144: AccessibleText.CHARACTER, 3));
145: assertEquals("\n", accessible.getBeforeIndex(
146: AccessibleText.WORD, 3));
147: assertEquals("In\n", accessible.getBeforeIndex(
148: AccessibleText.SENTENCE, 3));
149: assertEquals("s", accessible.getBeforeIndex(
150: AccessibleText.CHARACTER, 8));
151: assertEquals("this", accessible.getBeforeIndex(
152: AccessibleText.WORD, 8));
153: assertEquals("In\n", accessible.getBeforeIndex(
154: AccessibleText.SENTENCE, 8));
155: assertNull(accessible
156: .getBeforeIndex(AccessibleText.SENTENCE, 1));
157: }
158:
159: public void testGetCharacterBounds() throws Exception {
160: Runnable test = new Runnable() {
161: public void run() {
162: jtc.setText("\u05dc" + "\u0061" + "\u05dc" + "\u0061");
163: }
164: };
165: SwingUtilities.invokeAndWait(test);
166: if (BasicSwingTestCase.isHarmony()) {
167: assertEquals(getCharBounds(jtc, 1), accessible
168: .getCharacterBounds(1));
169: assertEquals(getCharBounds(jtc, 2), accessible
170: .getCharacterBounds(2));
171: }
172: }
173:
174: public void testGetIndexAtPoint() throws Exception {
175: Runnable test = new Runnable() {
176: public void run() {
177: //Bidirectional text is not supported
178: //jtc.setText("\u05dc" + "\u0061" + "\u05dc" + "\u0061");
179: jtc.setText("testGetIndexAtPoint");
180: }
181: };
182: SwingUtilities.invokeAndWait(test);
183: rect = null;
184: try {
185: rect = jtc.modelToView(1);
186: } catch (BadLocationException e) {
187: }
188: assertNotNull(rect);
189: assertEquals(1, accessible.getIndexAtPoint(new Point(rect.x,
190: rect.y)));
191: rect = null;
192: try {
193: rect = jtc.modelToView(2);
194: } catch (BadLocationException e) {
195: }
196: assertNotNull(rect);
197: assertEquals(2, accessible.getIndexAtPoint(new Point(rect.x,
198: rect.y)));
199: }
200: }
|