/** * Inserting text with the same attributes into the beginning of the document. * * <p>This test is equivalent to <code>doc.insertString(insertOffset, newLine, null)</code>, where * <code>insertOffset = 0</code>. */ public void testInsertSameAttrsDocStart() throws Exception { insertOffset = 0; // doc.insertString(insertOffset, newLine, null); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[0].setDirection(ElementSpec.JoinPreviousDirection); specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] {0, 6, 6, 10, 10, 16, 16, 17}, new int[] {0, 1}); assertChange(edits.get(1), new int[] {}, new int[] {1, 17}); assertChildren(root.getElement(0), new int[] {0, 1}, new AttributeSet[] {null}); assertChildren( root.getElement(1), new int[] {1, 6, 6, 10, 10, 16, 16, 17}, new AttributeSet[] {null, bold, italic, null}); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("plain", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the start of a paragraph with different attributes. * * <p>This test is equivalent to <code>doc.insertString(insertOffset, newLine, italic)</code>, * where <code>insertOffset = paragraph.getEndOffset()</code>. */ public void testInsertDiffAttrsParStart() throws Exception { insertOffset = paragraph.getEndOffset(); // doc.insertString(insertOffset, newLine, italic); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType), new ElementSpec(italic, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[4].setDirection(ElementSpec.JoinNextDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(3, edits.size()); assertChange(edits.get(0), new int[] {15, 17}, new int[] {15, 16}); assertChange(edits.get(1), new int[] {}, new int[] {16, 17}); assertChange(edits.get(2), new int[] {}, new int[] {16, 17}); assertChildren( root.getElement(0), new int[] {0, 5, 5, 9, 9, 15, 15, 16}, new AttributeSet[] {null, bold, italic, null}); assertChildren(root.getElement(1), new int[] {16, 17}, new AttributeSet[] {italic}); assertChildren(root.getElement(2), new int[] {17, 22}, new AttributeSet[] {null}); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("text\n", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into end of the an element with the same attributes; the following element has * different attributes. * * <p>This test is equivalent to <code>doc.insertString(insertOffset, newLine, bold)</code>, where * 2 is added to default value of <code>insertOffset</code>. */ public void testInsertSameAttrsEnd() throws Exception { insertOffset += 2; // doc.insertString(insertOffset, newLine, bold); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(null, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[0].setDirection(ElementSpec.JoinPreviousDirection); specs[2].setDirection(ElementSpec.JoinFractureDirection); // Spec [0] has wrong attributes (should be bold) but everything works // the way it supposed to. buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] {10, 16, 16, 17}, new int[] {}); assertChange(edits.get(1), new int[] {}, new int[] {10, 17}); assertChildren(root.getElement(0), new int[] {0, 5, 5, 10}, new AttributeSet[] {null, bold}); assertChildren( root.getElement(1), new int[] {10, 16, 16, 17}, new AttributeSet[] {italic, null}); assertEquals("bold\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("italic", getText(doc.getCharacterElement(insertOffset + newLineLen))); }
/** * Inserting text into the end of the document with different attributes. * * <p>This test is equivalent to <code>doc.insertString(insertOffset, newLine, italic)</code>, * where <code>insertOffset = doc.getLength()</code>. */ public void testInsertDiffAttrsDocEnd() throws Exception { insertOffset = doc.getLength(); // doc.insertString(insertOffset, newLine, italic); content.insertString(insertOffset, newLine); event = doc.new DefaultDocumentEvent(insertOffset, newLineLen, EventType.INSERT); ElementSpec[] specs = { new ElementSpec(italic, ElementSpec.ContentType, newLineLen), new ElementSpec(null, ElementSpec.EndTagType), new ElementSpec(null, ElementSpec.StartTagType) }; specs[2].setDirection(ElementSpec.JoinFractureDirection); buf.insert(insertOffset, newLineLen, specs, event); List<?> edits = getEdits(event); assertEquals(2, edits.size()); assertChange(edits.get(0), new int[] {16, 22}, new int[] {16, 20, 20, 21}); assertChange(edits.get(1), new int[] {}, new int[] {21, 22}); assertChildren( root.getElement(1), new int[] {16, 20, 20, 21}, new AttributeSet[] {null, italic}); assertChildren(root.getElement(2), new int[] {21, 22}, new AttributeSet[] {null}); assertEquals("text", getText(doc.getCharacterElement(insertOffset - 1))); assertEquals("\n", getText(doc.getCharacterElement(insertOffset))); assertEquals("\n", getText(doc.getCharacterElement(insertOffset + newLineLen))); }