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: package javax.swing.text;
019:
020: import java.awt.Container;
021: import java.awt.Graphics;
022: import java.awt.Rectangle;
023: import java.awt.Shape;
024: import javax.swing.BasicSwingTestCase;
025: import javax.swing.JTextArea;
026: import javax.swing.SwingConstants;
027: import javax.swing.event.DocumentEvent.EventType;
028: import javax.swing.text.FlowView_FlowStrategyTest.PartFactory;
029: import javax.swing.text.FlowView_FlowStrategyTest.PartView;
030: import javax.swing.text.Position.Bias;
031: import javax.swing.text.ViewTestHelpers.ChildView;
032: import javax.swing.text.ViewTestHelpers.ChildrenFactory;
033:
034: public class ParagraphViewTest extends BasicSwingTestCase {
035: private static final int HEIGHT = 30;
036:
037: private static final int WIDTH = PartView.CHAR_WIDTH * 10;
038:
039: private StyledDocument doc;
040:
041: private Element root;
042:
043: private Element paragraph;
044:
045: private ParagraphView view;
046:
047: @Override
048: protected void setUp() throws Exception {
049: super .setUp();
050: setIgnoreNotImplemented(true);
051: doc = new DefaultStyledDocument();
052: doc.insertString(0, "plainbolditalic\nparagraph2", null);
053: // 0123456789012345
054: MutableAttributeSet attrs = new SimpleAttributeSet();
055: StyleConstants.setBold(attrs, true);
056: doc.setCharacterAttributes(5, 4, attrs, false);
057: attrs = new SimpleAttributeSet();
058: StyleConstants.setItalic(attrs, true);
059: doc.setCharacterAttributes(9, 6, attrs, false);
060: root = doc.getDefaultRootElement();
061: paragraph = root.getElement(0);
062: view = new ParagraphView(paragraph);
063: }
064:
065: public void testParagraphView() {
066: final Marker propertiesSet = new Marker();
067: view = new ParagraphView(paragraph) {
068: @Override
069: protected void setPropertiesFromAttributes() {
070: propertiesSet.setOccurred();
071: super .setPropertiesFromAttributes();
072: };
073: };
074: assertTrue(propertiesSet.isOccurred());
075: assertEquals(0, view.getViewCount());
076: assertEquals(View.Y_AXIS, view.getAxis());
077: assertEquals(View.X_AXIS, view.getFlowAxis());
078: assertNull(view.layoutPool);
079: // Testing insets
080: assertEquals(0, view.getTopInset());
081: assertEquals(0, view.getLeftInset());
082: assertEquals(0, view.getBottomInset());
083: assertEquals(0, view.getRightInset());
084: AttributeSet attrs = getInsetsAttributeSet();
085: doc.setParagraphAttributes(0, 1, attrs, false);
086: view.setPropertiesFromAttributes();
087: assertEquals(9, view.getTopInset());
088: assertEquals(4, view.getLeftInset());
089: assertEquals(1, view.getBottomInset());
090: assertEquals(7, view.getRightInset());
091: }
092:
093: public void testGetAlignment() {
094: loadChilrenAndLayout();
095: final int height = view.getSpan(View.Y_AXIS, 0);
096: assertEquals(0, view.firstLineIndent);
097: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 1e-5f);
098: assertEquals((height / 2) / view.getPreferredSpan(View.Y_AXIS),
099: view.getAlignment(View.Y_AXIS), 1e-5f);
100: // But if height is of type float the assertion fails
101: // assertEquals((height / 2.0) / view.getPreferredSpan(View.Y_AXIS),
102: // view.getAlignment(View.Y_AXIS), 1e-5f);
103: view.layoutChanged(View.X_AXIS);
104: view.layout(WIDTH, HEIGHT);
105: assertEquals(0, view.getOffset(View.X_AXIS, 0));
106: assertEquals(view.firstLineIndent, ((CompositeView) view
107: .getView(0)).getLeftInset());
108: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 1e-5f);
109: assertEquals((height / 2) / view.getPreferredSpan(View.Y_AXIS),
110: view.getAlignment(View.Y_AXIS), 1e-5f);
111: }
112:
113: public void testGetAlignmentNoChildren() {
114: view.loadChildren(null);
115: assertEquals(0.5f, view.getAlignment(View.X_AXIS), 1e-5f);
116: assertEquals(0.5f, view.getAlignment(View.Y_AXIS), 1e-5f);
117: }
118:
119: public void testChangedUpdate() {
120: assertEquals(0, view.firstLineIndent);
121: view.setFirstLineIndent(3.21f);
122: assertEquals(3, view.firstLineIndent);
123: MutableAttributeSet attrs = new SimpleAttributeSet();
124: StyleConstants.setFirstLineIndent(attrs, 5.61f);
125: doc.setParagraphAttributes(0, 1, attrs, false);
126: assertTrue(view.getAttributes().isDefined(
127: StyleConstants.FirstLineIndent));
128: view.loadChildren(null);
129: assertNotNull(view.layoutPool);
130: view.changedUpdate(
131: ((AbstractDocument) doc).new DefaultDocumentEvent(0,
132: paragraph.getEndOffset(), EventType.CHANGE),
133: null, null);
134: assertEquals(5, view.firstLineIndent);
135: }
136:
137: public void testChangedUpdateInvalidate() {
138: loadChilrenAndLayout();
139: assertTrue(view.isAllocationValid());
140: view.changedUpdate(
141: ((AbstractDocument) doc).new DefaultDocumentEvent(0,
142: paragraph.getEndOffset(), EventType.CHANGE),
143: null, null);
144: assertFalse(view.isAllocationValid());
145: }
146:
147: public void testCreateRow() {
148: View row = view.createRow();
149: assertEquals(0, row.getViewCount());
150: assertTrue(row instanceof BoxView);
151: assertEquals("javax.swing.text.ParagraphView$Row", row
152: .getClass().getName());
153: assertEquals(View.X_AXIS, ((BoxView) row).getAxis());
154: CompositeView rowView = (CompositeView) row;
155: assertEquals(0, rowView.getTopInset());
156: assertEquals(0, rowView.getLeftInset());
157: assertEquals(0, rowView.getBottomInset());
158: assertEquals(0, rowView.getRightInset());
159: AttributeSet attrs = getInsetsAttributeSet();
160: doc.setParagraphAttributes(0, 1, attrs, false);
161: view.setPropertiesFromAttributes();
162: rowView = (CompositeView) view.createRow();
163: assertEquals(0, rowView.getTopInset());
164: assertEquals(0, rowView.getLeftInset());
165: assertEquals(0, rowView.getBottomInset());
166: assertEquals(0, rowView.getRightInset());
167: }
168:
169: public void testCreateRowFirstLineIndent() {
170: view.setFirstLineIndent(PartView.CHAR_WIDTH * 3);
171: CompositeView row = (CompositeView) view.createRow();
172: assertEquals(0, row.getTopInset());
173: assertEquals(0, row.getLeftInset());
174: assertEquals(0, row.getBottomInset());
175: assertEquals(0, row.getRightInset());
176: loadChilrenAndLayout();
177: row = (CompositeView) view.getView(0);
178: assertEquals(view.firstLineIndent, row.getLeftInset());
179: assertEquals(0, row.getTopInset());
180: assertEquals(0, row.getBottomInset());
181: assertEquals(0, row.getRightInset());
182: }
183:
184: /*public void testGetFlowStartEmpty() {
185: assertEquals(0, view.getFlowStart(0));
186:
187: try {
188: view.getFlowStart(1);
189:
190: fail("ArrayIndexOutOfBoundsException is expected");
191: } catch (ArrayIndexOutOfBoundsException e) { }
192:
193: try {
194: view.getFlowStart(-1);
195:
196: fail("ArrayIndexOutOfBoundsException is expected");
197: } catch (ArrayIndexOutOfBoundsException e) { }
198: }*/
199: /*public void testGetFlowSpanEmpty() {
200: final boolean[] callSuper = new boolean[] {true};
201:
202: view = new ParagraphView(paragraph) {
203: public View getView(int index) {
204: return callSuper[0] ? super.getView(index) : null;
205: }
206: };
207: assertEquals(view.layoutSpan, view.getFlowSpan(0));
208:
209: try {
210: view.getFlowSpan(1);
211:
212: fail("ArrayIndexOutOfBoundsException is expected");
213: } catch (ArrayIndexOutOfBoundsException e) { }
214:
215: try {
216: view.getFlowSpan(-1);
217:
218: fail("ArrayIndexOutOfBoundsException is expected");
219: } catch (ArrayIndexOutOfBoundsException e) { }
220:
221: callSuper[0] = false;
222:
223: assertEquals(view.layoutSpan, view.getFlowSpan(0));
224: assertEquals(view.layoutSpan, view.getFlowSpan(1));
225: assertEquals(view.layoutSpan, view.getFlowSpan(-1));
226: }*/
227: public void testGetFlowStart() {
228: view.replace(0, view.getViewCount(), new View[] {
229: view.createRow(), view.createRow() });
230: assertEquals(0, view.firstLineIndent);
231: assertEquals(0, view.getFlowStart(0));
232: assertEquals(0, view.getFlowStart(1));
233: view.firstLineIndent = 31;
234: assertEquals(31, view.getFlowStart(0));
235: assertEquals(0, view.getFlowStart(1));
236: ((CompositeView) view.getView(0)).setInsets((short) 7,
237: (short) 9 /*left*/, (short) 21, (short) 11);
238: assertEquals(31 + 9, view.getFlowStart(0));
239: }
240:
241: public void testGetFlowSpan() {
242: view.replace(0, view.getViewCount(), new View[] {
243: view.createRow(), view.createRow() });
244: assertEquals(0, view.firstLineIndent);
245: assertEquals(Short.MAX_VALUE, view.layoutSpan);
246: assertEquals(Short.MAX_VALUE, view.getFlowSpan(0));
247: assertEquals(Short.MAX_VALUE, view.getFlowSpan(1));
248: view.firstLineIndent = 31;
249: assertEquals(Short.MAX_VALUE - 31, view.getFlowSpan(0));
250: assertEquals(Short.MAX_VALUE, view.getFlowSpan(1));
251: view.layoutSpan = 531;
252: assertEquals(500, view.getFlowSpan(0));
253: assertEquals(531, view.getFlowSpan(1));
254: ((CompositeView) view.getView(0)).setInsets((short) 7,
255: (short) 9, (short) 21, (short) 11);
256: assertEquals(500 - 9 - 11, view.getFlowSpan(0));
257: }
258:
259: public void testNextTabStop() {
260: assertNull(view.getTabSet());
261: assertEquals(72f, view.nextTabStop(0, 0), 1e-5f);
262: assertEquals(72f, view.nextTabStop(71.9f, 0), 1e-5f);
263: assertEquals(72f * 2, view.nextTabStop(72f, 0), 1e-5f);
264: }
265:
266: public void testNextTabStopTabSet() {
267: final TabSet tabSet = new TabSet(new TabStop[] {
268: new TabStop(10f), new TabStop(12f), new TabStop(15f) });
269: MutableAttributeSet attrs = new SimpleAttributeSet();
270: StyleConstants.setTabSet(attrs, tabSet);
271: doc.setParagraphAttributes(0, 1, attrs, false);
272: assertSame(tabSet, view.getTabSet());
273: assertEquals(10f, view.nextTabStop(0, 0), 1e-5f);
274: assertEquals(10f, view.nextTabStop(9.99f, 0), 1e-5f);
275: assertEquals(12f, view.nextTabStop(10f, 0), 1e-5f);
276: assertEquals(12f, view.nextTabStop(11.99f, 0), 1e-5f);
277: assertEquals(15f, view.nextTabStop(12f, 0), 1e-5f);
278: assertEquals(15f, view.nextTabStop(14.99f, 0), 1e-5f);
279: if (isHarmony()) {
280: assertEquals(72f, view.nextTabStop(15f, 0), 1e-5f);
281: assertEquals(72f, view.nextTabStop(20f, 0), 1e-5f);
282: assertEquals(72f * 2, view.nextTabStop(72f, 0), 1e-5f);
283: } else {
284: assertEquals(20f, view.nextTabStop(15f, 0), 1e-5f);
285: assertEquals(25f, view.nextTabStop(20f, 0), 1e-5f);
286: }
287: assertEquals(10f, view.nextTabStop(0, 4), 1e-5f);
288: assertEquals(10f, view.nextTabStop(0, 5), 1e-5f);
289: assertEquals(10f, view.nextTabStop(0, 15), 1e-5f);
290: assertTrue(doc.getLength() + 1 < 50);
291: assertEquals(10f, view.nextTabStop(0, 50), 1e-5f);
292: }
293:
294: public void testNextTabStopTabSetTabBase() {
295: final TabSet tabSet = new TabSet(new TabStop[] {
296: new TabStop(10f), new TabStop(12f), new TabStop(15f) });
297: MutableAttributeSet attrs = new SimpleAttributeSet();
298: StyleConstants.setTabSet(attrs, tabSet);
299: doc.setParagraphAttributes(0, 1, attrs, false);
300: view.paint(createTestGraphics(),
301: new Rectangle(11, 21, 431, 527));
302: assertEquals(11, view.getTabBase(), 1e-5f);
303: assertSame(tabSet, view.getTabSet());
304: assertEquals(11 + 10f, view.nextTabStop(0, 0), 1e-5f);
305: assertEquals(11 + 10f, view.nextTabStop(10f, 0), 1e-5f);
306: assertEquals(11 + 10f, view.nextTabStop(20.99f, 0), 1e-5f);
307: assertEquals(11 + 12f, view.nextTabStop(21f, 0), 1e-5f);
308: assertEquals(11 + 15f, view.nextTabStop(23f, 0), 1e-5f);
309: }
310:
311: public void testNextTabStopTabSetAlign()
312: throws BadLocationException {
313: doc.remove(0, doc.getLength());
314: doc.insertString(0, "1\tleft\tcenter\tright\tdecimal 10.124\t"
315: // 01 23456 7890123 456789 012345678901234
316: // 0 1 2 3
317: + "bar\tleft\tnext tab\tnext", null);
318: // 5678 90123 456789012 3456
319: // 4 5
320: paragraph = root.getElement(0);
321: final TabSet tabSet = new TabSet(
322: new TabStop[] {
323: new TabStop(5f, TabStop.ALIGN_LEFT,
324: TabStop.LEAD_NONE),
325: new TabStop(20f, TabStop.ALIGN_CENTER,
326: TabStop.LEAD_DOTS),
327: new TabStop(39f, TabStop.ALIGN_RIGHT,
328: TabStop.LEAD_EQUALS),
329: new TabStop(61f, TabStop.ALIGN_DECIMAL,
330: TabStop.LEAD_HYPHENS),
331: new TabStop(71.9f, TabStop.ALIGN_BAR,
332: TabStop.LEAD_NONE),
333: new TabStop(79.213f, TabStop.ALIGN_LEFT,
334: TabStop.LEAD_UNDERLINE), });
335: MutableAttributeSet attrs = new SimpleAttributeSet();
336: StyleConstants.setTabSet(attrs, tabSet);
337: doc.setParagraphAttributes(0, 1, attrs, false);
338: view = new ParagraphView(paragraph);
339: loadChildren();
340: assertSame(tabSet, view.getTabSet());
341: assertEquals(5f, view.nextTabStop(0, 0), 1e-5f);
342: assertEquals(20f, view.nextTabStop(5f, 0), 1e-5f);
343: assertEquals(39f, view.nextTabStop(20f, 0), 1e-5f);
344: assertEquals(61f, view.nextTabStop(39f, 0), 1e-5f);
345: assertEquals(71.9f, view.nextTabStop(61f, 0), 1e-5f);
346: assertEquals(79.213f, view.nextTabStop(71.9f, 0), 1e-5f);
347: assertEquals(5f, view.nextTabStop(0, 0), 1e-5f);
348: assertEquals(5f, view.nextTabStop(0, 1), 1e-5f);
349: assertEquals(14f, view.nextTabStop(5f, 2), 1e-5f);
350: assertEquals(16f, view.nextTabStop(5f, 3), 1e-5f);
351: assertEquals(20f, view.nextTabStop(5f, 5), 1e-5f);
352: assertEquals(8f, view.nextTabStop(5f, 6), 1e-5f);
353: }
354:
355: public void testBreakView() {
356: loadChilrenAndLayout();
357: assertEquals(2, view.getViewCount());
358: int width = (int) view.getPreferredSpan(View.X_AXIS);
359: int height = (int) view.getPreferredSpan(View.Y_AXIS);
360: assertEquals(64 /*WIDTH*/, width);
361: assertEquals(PartView.CHAR_HEIGHT * 2, height);
362: Shape alloc = new Rectangle(WIDTH, HEIGHT * 2);
363: assertSame(view, view.breakView(View.X_AXIS, width / 2, alloc));
364: assertSame(view, view.breakView(View.X_AXIS, width, alloc));
365: assertSame(view, view.breakView(View.X_AXIS, width + 10, alloc));
366: assertSame(view, view.breakView(View.Y_AXIS,
367: PartView.CHAR_HEIGHT / 2, alloc));
368: assertSame(view, view.breakView(View.Y_AXIS,
369: PartView.CHAR_HEIGHT, alloc));
370: assertSame(view, view.breakView(View.Y_AXIS,
371: PartView.CHAR_HEIGHT + 10, alloc));
372: assertSame(view, view.breakView(View.Y_AXIS, height, alloc));
373: assertSame(view, view
374: .breakView(View.Y_AXIS, height + 10, alloc));
375: }
376:
377: public void testGetBreakWeight() {
378: assertEquals(View.BadBreakWeight, view.getBreakWeight(
379: View.X_AXIS, 1));
380: assertEquals(View.BadBreakWeight, view.getBreakWeight(
381: View.Y_AXIS, 1));
382: assertEquals(View.BadBreakWeight, view.getBreakWeight(
383: View.X_AXIS, 0));
384: assertEquals(View.BadBreakWeight, view.getBreakWeight(
385: View.Y_AXIS, 0));
386: View row = view.createRow();
387: view.append(row);
388: row.append(new PartView(paragraph.getElement(0)));
389: assertEquals(PartView.CHAR_HEIGHT, (int) row
390: .getPreferredSpan(View.Y_AXIS));
391: assertEquals(View.BadBreakWeight, view.getBreakWeight(
392: View.Y_AXIS, PartView.CHAR_HEIGHT));
393: assertEquals(View.BadBreakWeight, view.getBreakWeight(
394: View.Y_AXIS, PartView.CHAR_HEIGHT + 0.01f));
395: assertEquals(View.BadBreakWeight, view.getBreakWeight(
396: View.Y_AXIS, PartView.CHAR_HEIGHT + 1f));
397: view.append(row);
398: assertEquals(View.BadBreakWeight, view.getBreakWeight(
399: View.Y_AXIS, PartView.CHAR_HEIGHT));
400: assertEquals(View.BadBreakWeight, view.getBreakWeight(
401: View.Y_AXIS, PartView.CHAR_HEIGHT + 0.01f));
402: assertEquals(View.BadBreakWeight, view.getBreakWeight(
403: View.Y_AXIS, PartView.CHAR_HEIGHT * 2));
404: assertFalse(view.isAllocationValid());
405: loadChildren();
406: view.layout(WIDTH, 30);
407: assertTrue(view.isAllocationValid());
408: assertEquals(2, view.getViewCount());
409: row = view.getView(0);
410: assertEquals(PartView.CHAR_HEIGHT, (int) row
411: .getPreferredSpan(View.Y_AXIS));
412: assertEquals(View.BadBreakWeight, view.getBreakWeight(
413: View.Y_AXIS, PartView.CHAR_HEIGHT));
414: assertEquals(View.BadBreakWeight, view.getBreakWeight(
415: View.Y_AXIS, PartView.CHAR_HEIGHT + 0.01f));
416: assertEquals(View.BadBreakWeight, view.getBreakWeight(
417: View.Y_AXIS, PartView.CHAR_HEIGHT + 1f));
418: assertEquals(View.BadBreakWeight, view.getBreakWeight(
419: View.Y_AXIS, PartView.CHAR_HEIGHT * 2));
420: assertEquals(View.BadBreakWeight, view.getBreakWeight(
421: View.Y_AXIS, PartView.CHAR_HEIGHT * 2 + 0.01f));
422: Graphics g = createTestGraphics();
423: Rectangle area = new Rectangle(21, 13, WIDTH, HEIGHT);
424: g.setClip(area);
425: view.paint(g, area);
426: assertEquals(PartView.CHAR_HEIGHT, (int) row
427: .getPreferredSpan(View.Y_AXIS));
428: assertEquals(View.BadBreakWeight, view.getBreakWeight(
429: View.Y_AXIS, PartView.CHAR_HEIGHT));
430: assertEquals(View.BadBreakWeight, view.getBreakWeight(
431: View.Y_AXIS, PartView.CHAR_HEIGHT + 0.01f));
432: assertEquals(View.BadBreakWeight, view.getBreakWeight(
433: View.Y_AXIS, PartView.CHAR_HEIGHT + 1f));
434: assertEquals(View.BadBreakWeight, view.getBreakWeight(
435: View.Y_AXIS, PartView.CHAR_HEIGHT * 2));
436: assertEquals(View.BadBreakWeight, view.getBreakWeight(
437: View.Y_AXIS, PartView.CHAR_HEIGHT * 2 + 0.01f));
438: }
439:
440: public void testGetBreakWeightSuper() throws Exception {
441: View row = view.createRow();
442: view.append(row);
443: row.append(new PartView(paragraph.getElement(0)));
444: assertEquals(PartView.CHAR_HEIGHT, (int) row
445: .getPreferredSpan(View.Y_AXIS));
446: assertEquals(View.BadBreakWeight, view.getBreakWeight(
447: View.Y_AXIS, 0, PartView.CHAR_HEIGHT));
448: assertEquals(View.GoodBreakWeight, view.getBreakWeight(
449: View.Y_AXIS, 0, PartView.CHAR_HEIGHT + 0.01f));
450: assertEquals(View.GoodBreakWeight, view.getBreakWeight(
451: View.Y_AXIS, 0, PartView.CHAR_HEIGHT + 1f));
452: view.append(row);
453: assertEquals(View.BadBreakWeight, view.getBreakWeight(
454: View.Y_AXIS, 0, PartView.CHAR_HEIGHT));
455: assertEquals(View.BadBreakWeight, view
456: .getBreakWeight(View.Y_AXIS, PartView.CHAR_HEIGHT,
457: PartView.CHAR_HEIGHT));
458: assertEquals(View.BadBreakWeight, view.getBreakWeight(
459: View.Y_AXIS, 0, PartView.CHAR_HEIGHT * 2));
460: assertEquals(View.GoodBreakWeight, view.getBreakWeight(
461: View.Y_AXIS, 0, PartView.CHAR_HEIGHT * 2 + 0.01f));
462: assertFalse(view.isAllocationValid());
463: loadChilrenAndLayout();
464: assertTrue(view.isAllocationValid());
465: assertEquals(2, view.getViewCount());
466: row = view.getView(0);
467: assertEquals(PartView.CHAR_HEIGHT, (int) row
468: .getPreferredSpan(View.Y_AXIS));
469: assertEquals(View.BadBreakWeight, view.getBreakWeight(
470: View.Y_AXIS, 0, PartView.CHAR_HEIGHT));
471: assertEquals(View.BadBreakWeight, view.getBreakWeight(
472: View.Y_AXIS, 0, PartView.CHAR_HEIGHT + 0.01f));
473: assertEquals(View.BadBreakWeight, view.getBreakWeight(
474: View.Y_AXIS, 0, PartView.CHAR_HEIGHT * 2));
475: assertEquals(View.GoodBreakWeight, view.getBreakWeight(
476: View.Y_AXIS, 0, PartView.CHAR_HEIGHT * 2 + 0.01f));
477: }
478:
479: public void testSetJustification() {
480: view.setJustification(-10);
481: loadChilrenAndLayout();
482: assertEquals(0, view.getOffset(View.X_AXIS, 0));
483: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
484: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
485: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 0));
486: final View row = view.getView(1);
487: assertEquals(0.5f, row.getAlignment(View.X_AXIS), 1e-5);
488: assertEquals(0f, row.getAlignment(View.Y_AXIS), 1e-5);
489: final int len = row.getEndOffset() - row.getStartOffset();
490: assertEquals(PartView.CHAR_HEIGHT, view.getOffset(View.Y_AXIS,
491: 1));
492: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 1));
493: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
494: View.X_AXIS, 1));
495: assertEquals((WIDTH - view.getSpan(View.X_AXIS, 1)) / 2, view
496: .getOffset(View.X_AXIS, 1));
497: }
498:
499: public void testSetJustificationLeft() {
500: view.setJustification(StyleConstants.ALIGN_LEFT);
501: loadChilrenAndLayout();
502: assertEquals(0, view.getOffset(View.X_AXIS, 0));
503: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
504: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
505: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 0));
506: final View row = view.getView(1);
507: assertEquals(0f, row.getAlignment(View.X_AXIS), 1e-5);
508: assertEquals(0f, row.getAlignment(View.Y_AXIS), 1e-5);
509: final int len = row.getEndOffset() - row.getStartOffset();
510: assertEquals(0, view.getOffset(View.X_AXIS, 1));
511: assertEquals(PartView.CHAR_HEIGHT, view.getOffset(View.Y_AXIS,
512: 1));
513: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
514: View.X_AXIS, 1));
515: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 1));
516: }
517:
518: public void testSetJustificationCenter() {
519: view.setJustification(StyleConstants.ALIGN_CENTER);
520: loadChilrenAndLayout();
521: assertEquals(0, view.getOffset(View.X_AXIS, 0));
522: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
523: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
524: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 0));
525: final View row = view.getView(1);
526: assertEquals(0.5f, row.getAlignment(View.X_AXIS), 1e-5);
527: assertEquals(0f, row.getAlignment(View.Y_AXIS), 1e-5);
528: final int len = row.getEndOffset() - row.getStartOffset();
529: assertEquals((WIDTH - view.getSpan(View.X_AXIS, 1)) / 2, view
530: .getOffset(View.X_AXIS, 1));
531: assertEquals(PartView.CHAR_HEIGHT, view.getOffset(View.Y_AXIS,
532: 1));
533: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
534: View.X_AXIS, 1));
535: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 1));
536: }
537:
538: public void testSetJustificationRight() {
539: view.setJustification(StyleConstants.ALIGN_RIGHT);
540: loadChilrenAndLayout();
541: assertEquals(0, view.getOffset(View.X_AXIS, 0));
542: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
543: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
544: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 0));
545: final View row = view.getView(1);
546: assertEquals(1.0f, row.getAlignment(View.X_AXIS), 1e-5);
547: assertEquals(0f, row.getAlignment(View.Y_AXIS), 1e-5);
548: final int len = row.getEndOffset() - row.getStartOffset();
549: assertEquals(WIDTH - view.getSpan(View.X_AXIS, 1), view
550: .getOffset(View.X_AXIS, 1));
551: assertEquals(PartView.CHAR_HEIGHT, view.getOffset(View.Y_AXIS,
552: 1));
553: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
554: View.X_AXIS, 1));
555: assertEquals(PartView.CHAR_HEIGHT, view.getSpan(View.Y_AXIS, 1));
556: }
557:
558: public void testSetLineSpacing() {
559: view.setLineSpacing(2);
560: loadChilrenAndLayout();
561: assertEquals(0, view.getOffset(View.X_AXIS, 0));
562: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
563: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
564: assertEquals(PartView.CHAR_HEIGHT * 3, view.getSpan(
565: View.Y_AXIS, 0));
566: final View row = view.getView(1);
567: final int len = row.getEndOffset() - row.getStartOffset();
568: assertEquals(0, view.getOffset(View.X_AXIS, 1));
569: assertEquals(PartView.CHAR_HEIGHT * 3, view.getOffset(
570: View.Y_AXIS, 1));
571: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
572: View.X_AXIS, 1));
573: assertEquals(PartView.CHAR_HEIGHT * 3, view.getSpan(
574: View.Y_AXIS, 1));
575: assertEquals(PartView.CHAR_HEIGHT, (int) row.getView(0)
576: .getPreferredSpan(View.Y_AXIS));
577: assertEquals(PartView.CHAR_HEIGHT * 3, (int) row
578: .getPreferredSpan(View.Y_AXIS));
579: assertEquals(0, ((CompositeView) row).getTopInset());
580: assertEquals(PartView.CHAR_HEIGHT * 2, ((CompositeView) row)
581: .getBottomInset());
582: }
583:
584: public void testSetLineSpacingFraction() {
585: view.setLineSpacing(1.25f);
586: loadChilrenAndLayout();
587: assertEquals(0, view.getOffset(View.X_AXIS, 0));
588: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
589: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
590: assertEquals((int) (PartView.CHAR_HEIGHT * 2.25f), view
591: .getSpan(View.Y_AXIS, 0));
592: final View row = view.getView(1);
593: final int len = row.getEndOffset() - row.getStartOffset();
594: assertEquals(0, view.getOffset(View.X_AXIS, 1));
595: assertEquals((int) (PartView.CHAR_HEIGHT * 2.25f), view
596: .getOffset(View.Y_AXIS, 1));
597: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
598: View.X_AXIS, 1));
599: assertEquals((int) (PartView.CHAR_HEIGHT * 2.25f), view
600: .getSpan(View.Y_AXIS, 1));
601: assertEquals(PartView.CHAR_HEIGHT, (int) row.getView(0)
602: .getPreferredSpan(View.Y_AXIS));
603: assertEquals((int) (PartView.CHAR_HEIGHT * 2.25f), (int) row
604: .getPreferredSpan(View.Y_AXIS));
605: }
606:
607: public void testSetLineSpacingNegative() {
608: view.setLineSpacing(-1f);
609: loadChilrenAndLayout();
610: assertEquals(0, view.getOffset(View.X_AXIS, 0));
611: assertEquals(0, view.getOffset(View.Y_AXIS, 0));
612: assertEquals(WIDTH, view.getSpan(View.X_AXIS, 0));
613: assertEquals(0, view.getSpan(View.Y_AXIS, 0));
614: final View row = view.getView(1);
615: final int len = row.getEndOffset() - row.getStartOffset();
616: assertEquals(0, view.getOffset(View.X_AXIS, 1));
617: assertEquals(0, view.getOffset(View.Y_AXIS, 1));
618: assertEquals(PartView.CHAR_WIDTH * len, view.getSpan(
619: View.X_AXIS, 1));
620: assertEquals(0, view.getSpan(View.Y_AXIS, 1));
621: assertEquals(PartView.CHAR_HEIGHT, (int) row.getView(0)
622: .getPreferredSpan(View.Y_AXIS));
623: assertEquals(0, (int) row.getPreferredSpan(View.Y_AXIS));
624: }
625:
626: public void testSetFirstLineIndent() {
627: assertEquals(0, view.firstLineIndent);
628: view.setFirstLineIndent(1.2f);
629: assertEquals(1, view.firstLineIndent);
630: view.setFirstLineIndent(1.9f);
631: assertEquals(1, view.firstLineIndent);
632: view.setFirstLineIndent(2f);
633: assertEquals(2, view.firstLineIndent);
634: }
635:
636: public void testSetFirstLineIndentRow() {
637: loadChilrenAndLayout();
638: assertTrue(view.isAllocationValid());
639: assertEquals(0, ((CompositeView) view.getView(0))
640: .getLeftInset());
641: assertEquals(WIDTH / PartView.CHAR_WIDTH, view.getView(0)
642: .getEndOffset());
643: view.setFirstLineIndent(PartView.CHAR_WIDTH * 3);
644: assertTrue(view.isAllocationValid());
645: assertEquals(view.firstLineIndent, ((CompositeView) view
646: .getView(0)).getLeftInset());
647: view.layout(WIDTH, HEIGHT);
648: assertEquals(WIDTH / PartView.CHAR_WIDTH, view.getView(0)
649: .getEndOffset());
650: view.layoutChanged(View.X_AXIS);
651: view.layout(WIDTH, HEIGHT);
652: assertEquals(WIDTH / PartView.CHAR_WIDTH - 3, view.getView(0)
653: .getEndOffset());
654: }
655:
656: public void testSetPropertiesFromAttributes() {
657: final Marker jm = new Marker();
658: final Marker flim = new Marker();
659: final Marker lsm = new Marker();
660: MutableAttributeSet attrs = new SimpleAttributeSet();
661: StyleConstants.setAlignment(attrs, -2);
662: StyleConstants.setFirstLineIndent(attrs, 1.167f);
663: StyleConstants.setLineSpacing(attrs, 1.324f);
664: doc.setParagraphAttributes(0, 1, attrs, false);
665: view = new ParagraphView(paragraph) {
666: @Override
667: protected void setJustification(int j) {
668: jm.setOccurred();
669: jm.setAuxiliary(new Integer(j));
670: };
671:
672: @Override
673: protected void setFirstLineIndent(float fi) {
674: flim.setOccurred();
675: flim.setAuxiliary(new Float(fi));
676: };
677:
678: @Override
679: protected void setLineSpacing(float ls) {
680: lsm.setOccurred();
681: lsm.setAuxiliary(new Float(ls));
682: };
683: };
684: view.setPropertiesFromAttributes();
685: assertTrue(jm.isOccurred());
686: assertEquals(-2, ((Integer) jm.getAuxiliary()).intValue());
687: assertTrue(flim.isOccurred());
688: assertEquals(1.167f,
689: ((Float) flim.getAuxiliary()).floatValue(), 1e-5);
690: assertTrue(lsm.isOccurred());
691: assertEquals(1.324f, ((Float) lsm.getAuxiliary()).floatValue(),
692: 1e-5);
693: }
694:
695: public void testGetLayoutViewCount() {
696: assertNull(view.layoutPool);
697: testExceptionalCase(new NullPointerCase() {
698: @Override
699: public void exceptionalAction() throws Exception {
700: view.getLayoutViewCount();
701: }
702: });
703: view.layoutPool = new BoxView(paragraph, View.Y_AXIS);
704: ((CompositeView) view.layoutPool)
705: .loadChildren(new ChildrenFactory());
706: assertTrue(paragraph.getElementCount() > 0);
707: assertEquals(paragraph.getElementCount(), view
708: .getLayoutViewCount());
709: }
710:
711: public void testGetLayoutView() {
712: assertNull(view.layoutPool);
713: testExceptionalCase(new NullPointerCase() {
714: @Override
715: public void exceptionalAction() throws Exception {
716: view.getLayoutView(0);
717: }
718: });
719: view.layoutPool = new BoxView(paragraph, View.Y_AXIS);
720: ((CompositeView) view.layoutPool)
721: .loadChildren(new ChildrenFactory());
722: assertTrue(paragraph.getElementCount() > 0);
723: assertSame(view.layoutPool.getView(0), view.getLayoutView(0));
724: assertSame(view.layoutPool.getView(1), view.getLayoutView(1));
725: }
726:
727: public void testGetPartialSize() {
728: loadChildren();
729: View child = view.layoutPool.getView(0);
730: assertTrue(child instanceof GlyphView);
731: if (isHarmony()) {
732: assertSame(view, ((GlyphView) child).getTabExpander());
733: } else {
734: assertNull(((GlyphView) child).getTabExpander());
735: }
736: assertTrue(view.layoutPool.getView(0) instanceof TabableView);
737: assertTrue(view.layoutPool.getView(1) instanceof TabableView);
738: GlyphView gv = (GlyphView) child;
739: int start = gv.getStartOffset() + 1;
740: int end = gv.getEndOffset() - 1;
741: assertEquals(gv.getPartialSpan(start, end), view
742: .getPartialSize(start, end), 1e-5f);
743: float size = gv.getPartialSpan(start, end + 1);
744: gv = (GlyphView) view.layoutPool.getView(1);
745: size += gv.getPartialSpan(gv.getStartOffset(), gv
746: .getEndOffset() - 1);
747: assertEquals(size, view.getPartialSize(start,
748: gv.getEndOffset() - 1), 1e-5f);
749: view.layoutPool.replace(0, 2, new View[] {
750: new ChildView(paragraph.getElement(0), 0),
751: new ChildView(paragraph.getElement(1), 1) });
752: assertFalse(view.layoutPool.getView(0) instanceof TabableView);
753: assertFalse(view.layoutPool.getView(1) instanceof TabableView);
754: size = view.layoutPool.getView(0).getPreferredSpan(View.X_AXIS)
755: + view.layoutPool.getView(1).getPreferredSpan(
756: View.X_AXIS);
757: assertEquals(0, view.getPartialSize(start,
758: gv.getEndOffset() - 1), 1e-5f);
759: child = view.layoutPool.getView(0);
760: assertEquals(child.getPreferredSpan(View.X_AXIS), view
761: .getPartialSize(child.getStartOffset(), child
762: .getEndOffset()), 1e-5f);
763: assertEquals(0, view.getPartialSize(child.getStartOffset(), gv
764: .getEndOffset() - 1), 1e-5f);
765: assertEquals(0, view.getPartialSize(child.getStartOffset() + 1,
766: child.getEndOffset()), 1e-5f);
767: child = view.layoutPool.getView(1);
768: assertEquals(child.getPreferredSpan(View.X_AXIS), view
769: .getPartialSize(child.getStartOffset(), child
770: .getEndOffset()), 1e-5f);
771: assertEquals(0, view.getPartialSize(child.getStartOffset(),
772: child.getEndOffset() - 1), 1e-5f);
773: assertEquals(size, view.getPartialSize(view.layoutPool.getView(
774: 0).getStartOffset(), view.layoutPool.getView(1)
775: .getEndOffset()), 1e-5);
776: }
777:
778: public void testFindOffsetToCharactersInString()
779: throws BadLocationException {
780: // 0123456789012345
781: assertEquals("plainbolditalic\n", doc.getText(view
782: .getStartOffset(), view.getEndOffset()));
783: char[] chars = "abcd".toCharArray();
784: int[] offset = new int[] {
785: // 0 1 2 3 4
786: 2, 2, 2, 5, 5,
787: // 5 6 7 8 9
788: 5, 8, 8, 8, 11,
789: // 10 11 12 13 14
790: 11, 11, 14, 14, 14, -1, -1 };
791: for (int i = 0; i < offset.length; i++) {
792: assertEquals("@ " + i, offset[i], view
793: .findOffsetToCharactersInString(chars, i));
794: }
795: assertEquals(-1, view.findOffsetToCharactersInString("e\t.,"
796: .toCharArray(), 0));
797: }
798:
799: public void testGetTabSet() {
800: assertFalse(view.getAttributes().isDefined(
801: StyleConstants.TabSet));
802: assertNull(view.getTabSet());
803: final TabSet tabSet = new TabSet(new TabStop[] {
804: new TabStop(10f), new TabStop(12f), new TabStop(15f) });
805: MutableAttributeSet attrs = new SimpleAttributeSet();
806: StyleConstants.setTabSet(attrs, tabSet);
807: doc.setParagraphAttributes(0, 1, attrs, false);
808: assertTrue(view.getAttributes()
809: .isDefined(StyleConstants.TabSet));
810: assertSame(tabSet, view.getTabSet());
811: }
812:
813: public void testGetTabBase() {
814: assertEquals(0, view.getLeftInset());
815: assertEquals(0, (int) view.getTabBase());
816: AttributeSet attrs = getInsetsAttributeSet();
817: doc.setParagraphAttributes(0, 1, attrs, false);
818: view.setPropertiesFromAttributes();
819: assertEquals(4, view.getLeftInset());
820: assertEquals(0, view.getTabBase(), 1e-5f);
821: view.paint(createTestGraphics(), new Rectangle(7, 10, WIDTH,
822: HEIGHT));
823: assertEquals(7 + 4, (int) view.getTabBase());
824: }
825:
826: // public void testGetClosestPositionTo() {
827: //
828: // }
829: public void testGetNextNorthSouthVisualPositionFrom()
830: throws BadLocationException {
831: final class Params {
832: boolean valid;
833:
834: int pos;
835:
836: Bias b;
837:
838: int dir;
839:
840: int row;
841:
842: int x;
843:
844: public void check(int pos, Bias b, int dir, int row, int x) {
845: assertTrue("valid", valid);
846: assertEquals("offset", pos, this .pos);
847: assertSame("bias", b, this .b);
848: assertEquals("direction", dir, this .dir);
849: assertEquals("row", row, this .row);
850: assertEquals("x", x, this .x);
851: valid = false;
852: }
853: }
854: final Rectangle alloc = new Rectangle(11, 7, WIDTH, HEIGHT);
855: final JTextArea textArea = new JTextArea();
856: final Params params = new Params();
857: view = new ParagraphView(paragraph) {
858: @Override
859: protected int getClosestPositionTo(int pos, Bias b,
860: Shape a, int dir, Bias[] biasRet, int rowIndex,
861: int x) throws BadLocationException {
862: params.valid = true;
863: params.pos = pos;
864: params.b = b;
865: assertSame(alloc, a);
866: params.dir = dir;
867: params.row = rowIndex;
868: params.x = x;
869: return 0;
870: }
871:
872: @Override
873: public Container getContainer() {
874: return textArea;
875: }
876: };
877: loadChilrenAndLayout();
878: Bias[] bias = new Bias[1];
879: view.getNextNorthSouthVisualPositionFrom(0, Bias.Forward,
880: alloc, SwingConstants.SOUTH, bias);
881: params.check(0, Bias.Forward, SwingConstants.SOUTH, 1, 0);
882: view.getNextNorthSouthVisualPositionFrom(1, Bias.Forward,
883: alloc, SwingConstants.SOUTH, bias);
884: params.check(1, Bias.Forward, SwingConstants.SOUTH, 1, 0);
885: view.getNextNorthSouthVisualPositionFrom(1, Bias.Forward,
886: alloc, SwingConstants.NORTH, bias);
887: assertFalse(params.valid);
888: view.getNextNorthSouthVisualPositionFrom(0, Bias.Forward,
889: alloc, SwingConstants.NORTH, bias);
890: assertFalse(params.valid);
891: final int end = view.getEndOffset() - 3;
892: view.getNextNorthSouthVisualPositionFrom(end, Bias.Forward,
893: alloc, SwingConstants.SOUTH, bias);
894: assertFalse(params.valid);
895: view.getNextNorthSouthVisualPositionFrom(end - 1, Bias.Forward,
896: alloc, SwingConstants.SOUTH, bias);
897: assertFalse(params.valid);
898: view.getNextNorthSouthVisualPositionFrom(end - 1, Bias.Forward,
899: alloc, SwingConstants.NORTH, bias);
900: params.check(end - 1, Bias.Forward, SwingConstants.NORTH, 0, 0);
901: view.getNextNorthSouthVisualPositionFrom(end, Bias.Forward,
902: alloc, SwingConstants.NORTH, bias);
903: params.check(end, Bias.Forward, SwingConstants.NORTH, 0, 0);
904: }
905:
906: public void testGetAttributesRow() {
907: final View row = view.createRow();
908: assertNull(row.getAttributes());
909: row.setParent(view);
910: assertSame(view.getAttributes(), row.getAttributes());
911: }
912:
913: private static AttributeSet getInsetsAttributeSet() {
914: MutableAttributeSet attrs = new SimpleAttributeSet();
915: StyleConstants.setSpaceAbove(attrs, 9.53f);
916: StyleConstants.setLeftIndent(attrs, 4.25f);
917: StyleConstants.setSpaceBelow(attrs, 1.41f);
918: StyleConstants.setRightIndent(attrs, 7.12f);
919: return attrs;
920: }
921:
922: private void loadChildren() {
923: view.loadChildren(null);
924: ((CompositeView) view.layoutPool)
925: .loadChildren(new PartFactory());
926: }
927:
928: private void loadChilrenAndLayout() {
929: loadChildren();
930: view.layout(WIDTH, HEIGHT);
931: }
932: }
|