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 Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing.text.html;
021:
022: import java.awt.event.ActionEvent;
023: import java.io.IOException;
024: import java.io.StringReader;
025: import java.util.Arrays;
026:
027: import javax.swing.AbstractAction;
028: import javax.swing.JEditorPane;
029: import javax.swing.SwingTestCase;
030: import javax.swing.text.BadLocationException;
031: import javax.swing.text.Element;
032: import javax.swing.text.StyleConstants;
033: import javax.swing.text.StyledEditorKit;
034:
035: public class HTMLEditorKit_HTMLTextActionTest extends SwingTestCase {
036: private static final String HTML_TEXT = "<i>Italic text</i>";
037:
038: private static class TestHTMLTextAction extends
039: HTMLEditorKit.HTMLTextAction {
040: public TestHTMLTextAction(final String name) {
041: super (name);
042: }
043:
044: public void actionPerformed(final ActionEvent e) {
045: }
046: }
047:
048: private JEditorPane editorPane;
049: private HTMLEditorKit editorKit;
050: private HTMLDocument document;
051: private HTMLEditorKit.HTMLTextAction action;
052:
053: public HTMLEditorKit_HTMLTextActionTest(final String name) {
054: super (name);
055: }
056:
057: protected void setUp() throws Exception {
058: super .setUp();
059: setIgnoreNotImplemented(true);
060:
061: editorPane = new JEditorPane();
062: editorKit = new HTMLEditorKit();
063: editorPane.setEditorKit(editorKit);
064: document = (HTMLDocument) editorPane.getDocument();
065: action = new TestHTMLTextAction("name");
066: }
067:
068: protected void tearDown() throws Exception {
069: super .tearDown();
070: }
071:
072: public void testHTMLTextAction() {
073: action = new TestHTMLTextAction("actionName");
074: assertEquals("actionName", action.getValue(AbstractAction.NAME));
075: }
076:
077: public void testElementCountToTag() throws Exception {
078: loadHTML();
079: final int offset = editorPane.getDocument().getLength();
080:
081: int bodyCount = action.elementCountToTag(document, offset,
082: HTML.Tag.BODY);
083: assertEquals(1, bodyCount);
084:
085: assertEquals(-1, action.elementCountToTag(document, offset,
086: HTML.Tag.HEAD));
087: }
088:
089: public void testFindElementMatchingTag() throws Exception {
090: loadHTML();
091: final int offset = editorPane.getDocument().getLength();
092: Element[] elems = action.getElementsAt(document, offset);
093:
094: Element bodyElem = action.findElementMatchingTag(document,
095: offset, HTML.Tag.BODY);
096: assertEquals(HTML.Tag.BODY, getHTMLTagByElement(bodyElem));
097: assertTrue(Arrays.asList(elems).contains(bodyElem));
098:
099: assertNull(action.findElementMatchingTag(document, offset,
100: HTML.Tag.HEAD));
101:
102: assertNotNull(action.findElementMatchingTag(document, document
103: .getLength() + 1, HTML.Tag.BODY));
104: }
105:
106: public void testGetElementsAt() throws Exception {
107: loadHTML();
108: final int offset = editorPane.getDocument().getLength();
109:
110: Element[] elems = action.getElementsAt(action
111: .getHTMLDocument(editorPane), offset);
112: assertEquals(editorPane.getDocument().getDefaultRootElement(),
113: elems[0]);
114: assertEquals(0, elems[elems.length - 1].getElementCount());
115:
116: for (int i = 0; i < elems.length; i++) {
117: assertTrue(elems[i].getStartOffset() <= offset
118: && elems[i].getEndOffset() >= offset);
119: }
120:
121: elems = action.getElementsAt(document, document.getLength());
122: assertEquals(editorPane.getDocument().getDefaultRootElement(),
123: elems[0]);
124:
125: elems = action
126: .getElementsAt(document, document.getLength() + 1);
127: assertEquals(editorPane.getDocument().getDefaultRootElement(),
128: elems[0]);
129: }
130:
131: public void testGetHTMLDocument() {
132: assertSame(editorPane.getDocument(), action
133: .getHTMLDocument(editorPane));
134: }
135:
136: public void testGetHTMLEditorKit() {
137: assertSame(editorPane.getEditorKit(), action
138: .getHTMLEditorKit(editorPane));
139:
140: editorPane.setEditorKit(new StyledEditorKit());
141: testExceptionalCase(new IllegalArgumentCase() {
142: public void exceptionalAction() throws Exception {
143: action.getHTMLEditorKit(editorPane);
144: }
145: });
146: }
147:
148: private void loadHTML() throws IOException, BadLocationException {
149: StringReader in = new StringReader(HTML_TEXT);
150: editorKit.read(in, editorPane.getDocument(), 0);
151: }
152:
153: private static HTML.Tag getHTMLTagByElement(final Element elem) {
154: return (HTML.Tag) elem.getAttributes().getAttribute(
155: StyleConstants.NameAttribute);
156: }
157: }
|