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.Color;
023: import java.awt.Point;
024: import java.awt.Rectangle;
025: import java.awt.event.InputMethodEvent;
026: import java.awt.font.TextHitInfo;
027: import java.awt.im.InputMethodRequests;
028: import java.text.AttributedCharacterIterator;
029: import java.text.AttributedString;
030: import java.text.AttributedCharacterIterator.Attribute;
031: import java.util.HashMap;
032: import java.util.Map;
033: import javax.swing.JFrame;
034: import javax.swing.JTextArea;
035: import javax.swing.SwingWaitTestCase;
036: import org.apache.harmony.awt.text.ComposedTextParams;
037: import org.apache.harmony.awt.text.PropertyNames;
038:
039: public class JTextComponent_IMLocationTest extends SwingWaitTestCase {
040: JTextArea jta;
041:
042: JFrame jf;
043:
044: InputMethodEvent ime;
045:
046: Map<Attribute, Object> map;
047:
048: AbstractDocument doc;
049:
050: AttributedCharacterIterator iter;
051:
052: AttributedString attrString;
053:
054: static final AttributedCharacterIterator.Attribute SEGMENT_ATTRIBUTE = AttributedCharacterIterator.Attribute.INPUT_METHOD_SEGMENT;
055:
056: static final String initialContent = "IM test";
057:
058: boolean bWasException;
059:
060: InputMethodRequests imr;
061:
062: String message;
063:
064: @Override
065: protected void setUp() throws Exception {
066: super .setUp();
067: map = new HashMap<Attribute, Object>();
068: jf = new JFrame();
069: jta = new JTextArea();
070: jta.setText(initialContent);
071: doc = (AbstractDocument) jta.getDocument();
072: imr = jta.getInputMethodRequests();
073: bWasException = false;
074: message = null;
075: jf.getContentPane().add(jta);
076: jf.setSize(200, 300);
077: jf.setVisible(true);
078: component = jf;
079: }
080:
081: @Override
082: protected void tearDown() throws Exception {
083: jf.dispose();
084: super .tearDown();
085: }
086:
087: private InputMethodEvent getTextEvent(
088: AttributedCharacterIterator text,
089: int committedCharacterCount, TextHitInfo caret,
090: TextHitInfo visiblePosition) {
091: return getEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
092: text, committedCharacterCount, caret, visiblePosition);
093: }
094:
095: private InputMethodEvent getEvent(final int id,
096: final AttributedCharacterIterator text,
097: final int committedCharacterCount, final TextHitInfo caret,
098: final TextHitInfo visiblePosition) {
099: return new InputMethodEvent(jta, id, text,
100: committedCharacterCount, caret, visiblePosition);
101: }
102:
103: private Map<Attribute, Object> putSegmentAttribute(
104: final Map<Attribute, Object> map, final Object value) {
105: map.put(SEGMENT_ATTRIBUTE, value);
106: return map;
107: }
108:
109: private AttributedCharacterIterator getIterator(final String text,
110: final Map<Attribute, Object> map) {
111: attrString = new AttributedString(text, map);
112: return attrString.getIterator();
113: }
114:
115: private void setComposedText() {
116: String content = "12345";
117: iter = getIterator(content, putSegmentAttribute(map, Color.RED));
118: ime = getTextEvent(iter, 0, TextHitInfo.afterOffset(0),
119: TextHitInfo.afterOffset(0));
120: jta.processInputMethodEvent(ime);
121: assertEquals(7, jta.getCaretPosition());
122: checkComposedTextParams(true, 7, 5);
123: assertEquals(initialContent + content, jta.getText());
124: }
125:
126: private void checkComposedTextParams(final boolean shouldBe,
127: final int start, final int length) {
128: if (isHarmony()) {
129: Object value = doc
130: .getProperty(PropertyNames.COMPOSED_TEXT_PROPERTY);
131: if (!shouldBe) {
132: assertNull(value);
133: return;
134: }
135: assertTrue(value instanceof ComposedTextParams);
136: ComposedTextParams params = (ComposedTextParams) value;
137: assertEquals(start, params.getComposedTextStart());
138: assertEquals(length, params.getComposedTextLength());
139: AttributedString text = params.getComposedText();
140: AttributedCharacterIterator iter1 = attrString
141: .getIterator();
142: AttributedCharacterIterator iter2 = text.getIterator();
143: assertEquals(iter1.getAttributes(), iter2.getAttributes());
144: assertEquals(iter1.getRunStart(SEGMENT_ATTRIBUTE), iter2
145: .getRunStart(SEGMENT_ATTRIBUTE));
146: assertEquals(Math.min(iter1.getRunLimit(SEGMENT_ATTRIBUTE),
147: iter2.getEndIndex()), iter2
148: .getRunLimit(SEGMENT_ATTRIBUTE));
149: }
150: }
151:
152: public void testGetLocationOffset() {
153: try {
154: setComposedText();
155: Rectangle rect;
156: Point location = jta.getLocationOnScreen();
157: for (int i = 0; i < 7; i++) {
158: rect = jta.modelToView(i);
159: rect.translate(location.x, location.y);
160: assertNull(imr.getLocationOffset(rect.x, rect.y));
161: }
162: for (int i = 7; i < 13; i++) {
163: rect = jta.modelToView(i);
164: rect.translate(location.x, location.y);
165: assertEquals(TextHitInfo.afterOffset(i - 7), imr
166: .getLocationOffset(rect.x, rect.y));
167: }
168: } catch (BadLocationException e) {
169: }
170: }
171:
172: public void testGetTextLocation() {
173: try {
174: setComposedText();
175: int pos = 7;
176: Rectangle rect = jta.modelToView(pos);
177: Point location = jta.getLocationOnScreen();
178: rect.translate(location.x, location.y);
179: for (int i = 0; i < 10; i++) {
180: assertEquals(rect, imr.getTextLocation(TextHitInfo
181: .beforeOffset(i)));
182: assertEquals(rect, imr.getTextLocation(TextHitInfo
183: .afterOffset(i)));
184: }
185: iter = getIterator("klnoprst", putSegmentAttribute(map,
186: Color.BLACK));
187: ime = getTextEvent(iter, 5, TextHitInfo.afterOffset(0),
188: TextHitInfo.afterOffset(0));
189: jta.processInputMethodEvent(ime);
190: pos = 12;
191: rect = jta.modelToView(pos);
192: rect.translate(location.x, location.y);
193: for (int i = 0; i < 10; i++) {
194: assertEquals(rect, imr.getTextLocation(TextHitInfo
195: .beforeOffset(i)));
196: assertEquals(rect, imr.getTextLocation(TextHitInfo
197: .afterOffset(i)));
198: }
199: } catch (BadLocationException e) {
200: assertFalse("unexpectedException", true);
201: }
202: }
203: }
|