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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Container;
023: import java.awt.FontMetrics;
024: import java.awt.Rectangle;
025: import javax.swing.event.DocumentEvent;
026: import javax.swing.event.DocumentListener;
027: import javax.swing.text.Position.Bias;
028: import javax.swing.text.PlainViewI18N_LineViewTest.PlainViewI18NWithTextArea;
029: import junit.framework.TestCase;
030:
031: public class PlainViewI18N_BidiTextViewTest extends TestCase implements
032: DocumentListener {
033: private View view;
034:
035: private PlainViewI18N parent;
036:
037: private PlainDocument doc;
038:
039: private Element root;
040:
041: private int startOffset;
042:
043: private int endOffset;
044:
045: private FontMetrics metrics;
046:
047: private final Rectangle shape = new Rectangle(27, 74, 91, 41);
048:
049: private DocumentEvent insertEvent;
050:
051: private DocumentEvent removeEvent;;
052:
053: @Override
054: protected void setUp() throws Exception {
055: super .setUp();
056: doc = new PlainDocument();
057: doc.insertString(0, PlainViewI18N_LineViewTest.defText, null);
058: root = doc.getDefaultRootElement();
059: startOffset = root.getElement(1).getStartOffset() + 1;
060: endOffset = root.getElement(3).getEndOffset() - 1;
061: parent = new PlainViewI18NWithTextArea(root, doc);
062: }
063:
064: public void testGetEndOffset() {
065: view = parent.new LTRTextView(root, startOffset, endOffset);
066: assertEquals(startOffset, view.getStartOffset());
067: assertEquals(endOffset, view.getEndOffset());
068: }
069:
070: public void testGetStartOffset() {
071: view = parent.new RTLTextView(root, startOffset, endOffset);
072: assertEquals(startOffset, view.getStartOffset());
073: assertEquals(endOffset, view.getEndOffset());
074: }
075:
076: private void init() throws BadLocationException {
077: view.setParent(parent);
078: Container container = view.getContainer();
079: metrics = container.getFontMetrics(container.getFont());
080: shape.width = getTextWidth(startOffset, endOffset - startOffset);
081: shape.height = metrics.getHeight();
082: parent.setSize(shape.width, shape.height);
083: }
084:
085: private String getText(int offset, int length)
086: throws BadLocationException {
087: return view.getDocument().getText(offset, length);
088: }
089:
090: private int getTextWidth(int offset, int length)
091: throws BadLocationException {
092: return getTextWidth(getText(offset, length));
093: }
094:
095: private int getTextWidth(String text) {
096: return metrics.stringWidth(text);
097: }
098:
099: private void setOffsets(Element element) {
100: startOffset = element.getStartOffset();
101: endOffset = element.getEndOffset();
102: }
103:
104: public void testModelToViewLTR() throws BadLocationException {
105: final Element line = root.getElement(0);
106: setOffsets(line);
107: view = parent.new LTRTextView(line, startOffset, endOffset);
108: init();
109: assertEquals(new Rectangle(shape.x, shape.y, 1, shape.height),
110: view.modelToView(startOffset, shape, Bias.Forward));
111: assertEquals(new Rectangle(shape.x
112: + getTextWidth(startOffset, 1), shape.y, 1,
113: shape.height), view.modelToView(startOffset + 1, shape,
114: Bias.Forward));
115: assertEquals(new Rectangle(shape.x
116: + getTextWidth(startOffset, endOffset - startOffset),
117: shape.y, 1, shape.height), view.modelToView(endOffset,
118: shape, Bias.Forward));
119: // Invalid values
120: try {
121: view.modelToView(startOffset - 1, shape, Bias.Forward);
122: fail("BadLocationException must be thrown");
123: } catch (BadLocationException e) {
124: }
125: try {
126: view.modelToView(endOffset + 1, shape, Bias.Forward);
127: fail("BadLocationException must be thrown");
128: } catch (BadLocationException e) {
129: }
130: }
131:
132: public void testModelToViewRTL() throws BadLocationException {
133: final Element line = root.getElement(1);
134: setOffsets(line);
135: view = parent.new RTLTextView(line, startOffset, endOffset);
136: init();
137: assertEquals(new Rectangle(
138: shape.x
139: // all RTLtext (both symbols)
140: + getTextWidth(startOffset, endOffset
141: - startOffset - 1), shape.y, 1,
142: shape.height), view.modelToView(startOffset, shape,
143: Bias.Forward));
144: assertEquals(new Rectangle(shape.x
145: // the second symbol in the model
146: + getTextWidth(startOffset + 1, endOffset - startOffset
147: - 2), shape.y, 1, shape.height), view
148: .modelToView(startOffset + 1, shape, Bias.Forward));
149: // no text at all
150: assertEquals(new Rectangle(shape.x, shape.y, 1, shape.height),
151: view.modelToView(endOffset, shape, Bias.Forward));
152: // Invalid values
153: try {
154: view.modelToView(startOffset - 1, shape, Bias.Forward);
155: fail("BadLocationException must be thrown");
156: } catch (BadLocationException e) {
157: }
158: try {
159: view.modelToView(endOffset + 1, shape, Bias.Forward);
160: fail("BadLocationException must be thrown");
161: } catch (BadLocationException e) {
162: }
163: }
164:
165: public static class PreferenceChange {
166: private final View child;
167:
168: private final boolean width;
169:
170: private final boolean height;
171:
172: public PreferenceChange(View child, boolean width,
173: boolean height) {
174: this .child = child;
175: this .width = width;
176: this .height = height;
177: }
178:
179: public void check(View child, boolean width, boolean height) {
180: assertSame("Unexpected child", child, this .child);
181: assertEquals("Width", width, this .width);
182: assertEquals("Height", height, this .height);
183: }
184: }
185:
186: private PreferenceChange preferenceParams;
187:
188: public void testInsertUpdateLTR() throws Exception {
189: final Element line = root.getElement(0);
190: setOffsets(line);
191: view = parent.new LTRTextView(line, startOffset, endOffset) {
192: @Override
193: public void preferenceChanged(View child, boolean w,
194: boolean h) {
195: preferenceParams = new PreferenceChange(child, w, h);
196: }
197: };
198: init();
199: doc.addDocumentListener(this );
200: doc.insertString(startOffset + 1,
201: PlainViewI18N_LineViewTest.LTR, null);
202: view.insertUpdate(insertEvent, shape, null);
203: preferenceParams.check(view, true, false);
204: }
205:
206: public void testInsertUpdateRTL() throws Exception {
207: final Element line = root.getElement(1);
208: setOffsets(line);
209: view = parent.new RTLTextView(line, startOffset, endOffset) {
210: @Override
211: public void preferenceChanged(View child, boolean w,
212: boolean h) {
213: preferenceParams = new PreferenceChange(child, w, h);
214: }
215: };
216: init();
217: doc.addDocumentListener(this );
218: doc.insertString(startOffset + 1,
219: PlainViewI18N_LineViewTest.RTL, null);
220: view.insertUpdate(insertEvent, shape, null);
221: preferenceParams.check(view, true, false);
222: }
223:
224: public void testRemoveUpdateLTR() throws Exception {
225: final Element line = root.getElement(0);
226: setOffsets(line);
227: view = parent.new LTRTextView(line, startOffset, endOffset) {
228: @Override
229: public void preferenceChanged(View child, boolean w,
230: boolean h) {
231: preferenceParams = new PreferenceChange(child, w, h);
232: }
233: };
234: init();
235: doc.addDocumentListener(this );
236: doc.remove(startOffset + 1, 1);
237: view.removeUpdate(removeEvent, shape, null);
238: preferenceParams.check(view, true, false);
239: }
240:
241: public void testRemoveUpdateRTL() throws Exception {
242: final Element line = root.getElement(1);
243: setOffsets(line);
244: view = parent.new RTLTextView(line, startOffset, endOffset) {
245: @Override
246: public void preferenceChanged(View child, boolean w,
247: boolean h) {
248: preferenceParams = new PreferenceChange(child, w, h);
249: }
250: };
251: init();
252: doc.addDocumentListener(this );
253: doc.remove(startOffset + 1, 1);
254: view.removeUpdate(removeEvent, shape, null);
255: preferenceParams.check(view, true, false);
256: }
257:
258: public void changedUpdate(DocumentEvent event) {
259: }
260:
261: public void insertUpdate(DocumentEvent event) {
262: insertEvent = event;
263: }
264:
265: public void removeUpdate(DocumentEvent event) {
266: removeEvent = event;
267: }
268: // public void testBidiTextView() {
269: // }
270: }
|