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.event.ActionEvent;
023: import javax.swing.Action;
024: import javax.swing.JEditorPane;
025: import javax.swing.SwingTestCase;
026:
027: public class StyledEditorKit_StyledTextActionTest extends SwingTestCase {
028: StyledEditorKit.StyledTextAction action;
029:
030: boolean bWasException;
031:
032: String message;
033:
034: JEditorPane jep;
035:
036: StyledEditorKit kit;
037:
038: class StyledTextAction extends StyledEditorKit.StyledTextAction {
039: private static final long serialVersionUID = 1L;
040:
041: public StyledTextAction(final String name) {
042: super (name);
043: }
044:
045: public void actionPerformed(final ActionEvent e) {
046: }
047: }
048:
049: @Override
050: protected void setUp() throws Exception {
051: jep = new JEditorPane();
052: setIgnoreNotImplemented(true);
053: action = new StyledTextAction("testName");
054: message = null;
055: bWasException = false;
056: kit = new StyledEditorKit();
057: super .setUp();
058: }
059:
060: @Override
061: protected void tearDown() throws Exception {
062: super .tearDown();
063: }
064:
065: public void testActionPerformed() {
066: }
067:
068: public void testStyledTextAction() {
069: assertEquals("testName", action.getValue(Action.NAME));
070: assertTrue(action.isEnabled());
071: }
072:
073: public void testGetEditor() {
074: jep = new JEditorPane();
075: assertEquals(jep, action.getEditor(new ActionEvent(jep, 1,
076: "...")));
077: //Really this needs to check NullPointerException, but ...
078: }
079:
080: public void testSetParagraphAttributes() {
081: DefaultStyledDocument doc = new DefaultStyledDocument();
082: jep.setEditorKit(kit);
083: jep.setDocument(doc);
084: jep.setText("ABCD\nEFGH\nIJKL\nMNOP\nQRST\n");
085: jep.setCaretPosition(2);
086: SimpleAttributeSet sas = createAttributeSet(2);
087: action.setParagraphAttributes(jep, sas, false);
088: checkParagraphAttributes(doc, -1, 5, sas, 0);
089: jep.setSelectionStart(10);
090: jep.setSelectionEnd(13);
091: SimpleAttributeSet sas2 = createAttributeSet(3);
092: action.setParagraphAttributes(jep, sas2, false);
093: checkParagraphAttributes(doc, 9, 15, sas2, 6);
094: jep.setCaretPosition(18);
095: SimpleAttributeSet sas3 = createAttributeSet(4);
096: action.setParagraphAttributes(jep, sas3, true);
097: for (int i = 0; i < doc.getLength(); i++) {
098: AttributeSet as = doc.getParagraphElement(i)
099: .getAttributes();
100: if (i < 15) {
101: sas.addAttribute(AttributeSet.ResolveAttribute, as
102: .getAttribute(AttributeSet.ResolveAttribute));
103: sas2.addAttribute(AttributeSet.ResolveAttribute, as
104: .getAttribute(AttributeSet.ResolveAttribute));
105: }
106: if (i < 5) {
107: assertEquals(sas, as);
108: } else if (i < 10) {
109: assertNotNull(as
110: .getAttribute(AttributeSet.ResolveAttribute));
111: assertEquals(1, as.getAttributeCount());
112: } else if (i < 15) {
113: assertEquals(sas2, as);
114: } else if (i < 20) {
115: assertEquals(sas3, as);
116: } else {
117: assertNotNull(as
118: .getAttribute(AttributeSet.ResolveAttribute));
119: assertEquals(1, as.getAttributeCount());
120: }
121: }
122: }
123:
124: public void testGetStyledDocument() {
125: jep = new JEditorPane();
126: try {
127: action.getStyledDocument(jep);
128: } catch (IllegalArgumentException e) {
129: bWasException = true;
130: message = e.getMessage();
131: }
132: assertTrue(bWasException);
133: assertTrue(message.endsWith("'StyledDocument'"));
134: StyledEditorKit styledKit = new StyledEditorKit();
135: jep.setEditorKit(styledKit);
136: assertEquals(jep.getDocument(), action.getStyledDocument(jep));
137: bWasException = false;
138: jep.setDocument(new PlainDocument());
139: try {
140: action.getStyledDocument(jep);
141: } catch (IllegalArgumentException e) {
142: bWasException = true;
143: message = e.getMessage();
144: }
145: assertTrue(bWasException);
146: assertTrue(message.endsWith("'StyledDocument'"));
147: }
148:
149: public void testGetStyledEditorKit() {
150: jep = new JEditorPane();
151: try {
152: action.getStyledEditorKit(jep);
153: } catch (IllegalArgumentException e) {
154: bWasException = true;
155: message = e.getMessage();
156: }
157: assertTrue(bWasException);
158: assertTrue(message.endsWith("'StyledEditorKit'"));
159: StyledEditorKit styledKit = new StyledEditorKit();
160: jep.setEditorKit(styledKit);
161: assertEquals(styledKit, action.getStyledEditorKit(jep));
162: jep.setDocument(new PlainDocument());
163: assertEquals(styledKit, action.getStyledEditorKit(jep));
164: }
165:
166: public void testSetCharacterAttributes() {
167: DefaultStyledDocument doc = new DefaultStyledDocument();
168: jep.setEditorKit(kit);
169: jep.setDocument(doc);
170: jep.setText("ABCDEFGHIGKLMNOPQA\n");
171: //no selection
172: AttributeSet attrs = doc.getCharacterElement(4).getAttributes();
173: jep.setCaretPosition(4);
174: SimpleAttributeSet sas = createAttributeSet(1);
175: action.setCharacterAttributes(jep, sas, false);
176: assertEquals(sas, kit.getInputAttributes());
177: assertEquals(attrs, doc.getCharacterElement(4).getAttributes());
178: SimpleAttributeSet sas2 = createAttributeSet(2);
179: action.setCharacterAttributes(jep, sas2, false);
180: assertEquals(sas2, kit.getInputAttributes());
181: assertEquals(attrs, doc.getCharacterElement(4).getAttributes());
182: SimpleAttributeSet sas3 = createAttributeSet(3);
183: action.setCharacterAttributes(jep, sas3, false);
184: assertEquals(sas3, kit.getInputAttributes());
185: assertEquals(attrs, doc.getCharacterElement(4).getAttributes());
186: //there is selection
187: jep.setSelectionStart(3);
188: jep.setSelectionEnd(5);
189: kit.getInputAttributes().removeAttributes(
190: kit.getInputAttributes());
191: attrs = kit.getInputAttributes();
192: SimpleAttributeSet sas4 = createAttributeSet(4);
193: action.setCharacterAttributes(jep, sas4, false);
194: checkAttributes(doc, 3, 6, sas4, true);
195: assertEquals(attrs, kit.getInputAttributes());
196: SimpleAttributeSet sas5 = createAttributeSet(5);
197: SimpleAttributeSet sas6 = new SimpleAttributeSet();
198: sas6.addAttribute("&&", "$$$");
199: action.setCharacterAttributes(jep, sas6, true);
200: assertEquals(attrs, kit.getInputAttributes());
201: action.setCharacterAttributes(jep, sas5, false);
202: sas5.addAttribute("&&", "$$$");
203: checkAttributes(doc, 3, 6, sas5, true);
204: assertEquals(attrs, kit.getInputAttributes());
205: }
206:
207: static final void checkAttributes(final Document doc,
208: final int start, final int end,
209: final MutableAttributeSet set,
210: final boolean ignoreResolveAttribute) {
211: for (int i = 0; i < doc.getLength(); i++) {
212: AttributeSet as = getAttributeSetByOffset(doc, i);
213: if (!ignoreResolveAttribute) {
214: set.addAttribute(AttributeSet.ResolveAttribute, as
215: .getAttribute(AttributeSet.ResolveAttribute));
216: }
217: if (i > start && i < end) {
218: assertEquals(set, as);
219: } else {
220: assertEquals(0, as.getAttributeCount());
221: }
222: }
223: }
224:
225: final void checkParagraphAttributes(final AbstractDocument doc,
226: final int start, final int end,
227: final MutableAttributeSet set, final int from) {
228: for (int i = from; i < doc.getLength(); i++) {
229: AttributeSet as = doc.getParagraphElement(i)
230: .getAttributes();
231: set.addAttribute(AttributeSet.ResolveAttribute, as
232: .getAttribute(AttributeSet.ResolveAttribute));
233: if (i > start && i < end) {
234: assertSame(as, doc.getParagraphElement(i));
235: assertEquals(set.getAttributeCount(), as
236: .getAttributeCount());
237: assertEquals(set, as);
238: } else {
239: assertEquals(1, as.getAttributeCount());
240: }
241: }
242: }
243:
244: final SimpleAttributeSet createAttributeSet(final int i) {
245: SimpleAttributeSet sas = new SimpleAttributeSet();
246: Integer value = new Integer(i);
247: sas.addAttribute(AbstractDocument.ParagraphElementName, value);
248: sas.addAttribute(AbstractDocument.ContentElementName, value);
249: sas.addAttribute(AbstractDocument.SectionElementName, value);
250: sas.addAttribute(StyleConstants.ComponentElementName, value);
251: sas.addAttribute(StyleConstants.IconElementName, value);
252: sas.addAttribute(StyleConstants.IconAttribute, value);
253: sas.addAttribute("##", value);
254: return sas;
255: }
256:
257: static final AttributeSet getAttributeSetByOffset(
258: final Document doc, final int offset) {
259: Element elem = StyledEditorKitTest.getElementByOffset(doc,
260: offset);
261: return (elem == null) ? null : elem.getAttributes();
262: }
263: }
|