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.util.Enumeration;
023:
024: import javax.swing.Action;
025: import javax.swing.JEditorPane;
026: import javax.swing.SwingTestCase;
027: import javax.swing.event.ChangeListener;
028:
029: public class StyledEditorKitTest extends SwingTestCase {
030: StyledEditorKit kit;
031:
032: class SimpleElement implements Element {
033: String name;
034:
035: AttributeSet set = new SimpleAttributeSet();
036:
037: public SimpleElement(final String s) {
038: name = s;
039: }
040:
041: public AttributeSet getAttributes() {
042: return set;
043: }
044:
045: public Document getDocument() {
046: return new PlainDocument();
047: }
048:
049: public Element getElement(final int arg0) {
050: return null;
051: }
052:
053: public int getElementCount() {
054: return 0;
055: }
056:
057: public int getElementIndex(final int arg0) {
058: return 0;
059: }
060:
061: public int getEndOffset() {
062: return 0;
063: }
064:
065: public String getName() {
066: return name;
067: }
068:
069: public Element getParentElement() {
070: return null;
071: }
072:
073: public int getStartOffset() {
074: return 0;
075: }
076:
077: public boolean isLeaf() {
078: return false;
079: }
080:
081: final void setAttributeSet(final AttributeSet as) {
082: set = as;
083: }
084: }
085:
086: @Override
087: protected void setUp() throws Exception {
088: super .setUp();
089: setIgnoreNotImplemented(true);
090: kit = new StyledEditorKit();
091: }
092:
093: @Override
094: protected void tearDown() throws Exception {
095: super .tearDown();
096: }
097:
098: public void testStyledEditorKit() {
099: }
100:
101: public void testClone() {
102: Object kit1 = kit.clone();
103: assertNotSame(kit1, kit);
104: assertTrue(kit1 instanceof StyledEditorKit);
105: }
106:
107: public void testGetActions() {
108: Action[] actions = kit.getActions();
109: Action[] super Actions = new DefaultEditorKit().getActions();
110: Action[] newActions = new Action[] {
111: new StyledEditorKit.FontSizeAction("font-size-48", 48),
112: new StyledEditorKit.FontSizeAction("font-size-36", 36),
113: new StyledEditorKit.FontSizeAction("font-size-24", 24),
114: new StyledEditorKit.FontSizeAction("font-size-18", 18),
115: new StyledEditorKit.FontSizeAction("font-size-16", 16),
116: new StyledEditorKit.FontSizeAction("font-size-14", 14),
117: new StyledEditorKit.FontSizeAction("font-size-12", 12),
118: new StyledEditorKit.FontSizeAction("font-size-10", 10),
119: new StyledEditorKit.FontSizeAction("font-size-8", 8),
120: new StyledEditorKit.FontFamilyAction(
121: "font-family-SansSerif", "SansSerif"),
122: new StyledEditorKit.FontFamilyAction(
123: "font-family-Serif", "Serif"),
124: new StyledEditorKit.FontFamilyAction(
125: "font-family-Monospaced", "Monospaced"),
126: new StyledEditorKit.BoldAction(),
127: new StyledEditorKit.UnderlineAction(),
128: new StyledEditorKit.ItalicAction(),
129: new StyledEditorKit.AlignmentAction("right-justify",
130: StyleConstants.ALIGN_RIGHT),
131: new StyledEditorKit.AlignmentAction("left-justify",
132: StyleConstants.ALIGN_LEFT),
133: new StyledEditorKit.AlignmentAction("center-justify",
134: StyleConstants.ALIGN_CENTER)
135: //,
136: //new StyledEditorKit.StyledInsertBreakAction("insert-break")
137: };
138: //There are some problems
139: //1) no Foreground action in StyledEditorKit.getActions
140: //2) DefaultEditorKit.InsertBreakAction is replaced by
141: // StyledEditorKit.StyledInsertBreakAction
142: for (int i = 0; i < super Actions.length; i++) {
143: boolean was = false;
144: if (super Actions[i]
145: .getClass()
146: .getName()
147: .equals(
148: "javax.swing.text.DefaultEditorKit$InsertBreakAction")) {
149: continue;
150: }
151: for (int j = 0; j < actions.length; j++) {
152: if (super Actions[i] == actions[j]) {
153: was = true;
154: break;
155: }
156: }
157: assertTrue("action missed: " + super Actions[i], was);
158: }
159: for (int i = 0; i < newActions.length; i++) {
160: boolean was = false;
161: for (int j = 0; j < actions.length; j++) {
162: if (newActions[i].getClass().getName().equals(
163: actions[j].getClass().getName())
164: && newActions[i].getValue(Action.NAME).equals(
165: actions[j].getValue(Action.NAME))) {
166: was = true;
167: break;
168: }
169: }
170: assertTrue("action missed: " + super Actions[i], was);
171: }
172: }
173:
174: public void testCreateDefaultDocument() {
175: Document doc1 = kit.createDefaultDocument();
176: Document doc2 = kit.createDefaultDocument();
177: String className = "javax.swing.text.DefaultStyledDocument";
178: assertEquals(className, doc1.getClass().getName());
179: assertEquals(className, doc2.getClass().getName());
180: assertNotSame(doc1, doc2);
181: }
182:
183: final SimpleAttributeSet createAttributeSet() {
184: SimpleAttributeSet sas = new SimpleAttributeSet();
185: sas.addAttribute(AbstractDocument.ParagraphElementName, "p");
186: sas.addAttribute(AbstractDocument.ContentElementName, "c");
187: sas.addAttribute(AbstractDocument.SectionElementName, "s");
188: sas.addAttribute(StyleConstants.ComponentElementName, "1");
189: sas.addAttribute(StyleConstants.IconElementName, "2");
190: sas.addAttribute(StyleConstants.IconElementName, "3");
191: sas.addAttribute(StyleConstants.IconAttribute, "4");
192: sas.addAttribute(AbstractDocument.ElementNameAttribute, "7");
193: sas.addAttribute("##", "5");
194: return sas;
195: }
196:
197: static final void removeObsolete(final MutableAttributeSet as) {
198: as.removeAttribute(StyleConstants.IconAttribute);
199: as.removeAttribute(StyleConstants.ComponentAttribute);
200: as.removeAttribute(AbstractDocument.ElementNameAttribute);
201: }
202:
203: public void testCreateInputAttributes() {
204: SimpleAttributeSet sas = createAttributeSet();
205: SimpleElement element = new SimpleElement("");
206: element.setAttributeSet(sas);
207: SimpleAttributeSet set = new SimpleAttributeSet();
208: set.addAttribute(StyleConstants.ComponentAttribute, "3");
209: set.addAttribute(AbstractDocument.ContentElementName, "~");
210: set.addAttribute("^^$$$", "%%%%");
211: kit.createInputAttributes(element, set);
212: removeObsolete(sas);
213: assertEquals(sas, set);
214: }
215:
216: public void testDeinstallJEditorPane() {
217: }
218:
219: public void testGetCharacterAttributeRun() {
220: SimpleAttributeSet set = createAttributeSet();
221: JEditorPane jep = new JEditorPane();
222: DefaultStyledDocument doc = new DefaultStyledDocument();
223: jep.setEditorKit(kit);
224: jep.setDocument(doc);
225: jep.setText("ABCDEDFGHIGKLMN\n");
226: doc.setCharacterAttributes(3, 8, set, true);
227: int length = doc.getLength();
228: removeObsolete(set);
229: for (int i = 0; i < length; i++) {
230: jep.setCaretPosition(i);
231: assertEquals(getElementByOffset(doc, i), kit
232: .getCharacterAttributeRun());
233: }
234: }
235:
236: public void testGetInputAttributes() {
237: SimpleAttributeSet set = createAttributeSet();
238: JEditorPane jep = new JEditorPane();
239: DefaultStyledDocument doc = new DefaultStyledDocument();
240: jep.setEditorKit(kit);
241: jep.setDocument(doc);
242: jep.setText("ABCDEDFGHIGKLMN\n");
243: doc.setCharacterAttributes(3, 8, set, true);
244: int length = doc.getLength();
245: removeObsolete(set);
246: for (int i = 0; i < length; i++) {
247: jep.setCaretPosition(i);
248: if (i > 3 && i < 12) {
249: assertEquals(set, kit.getInputAttributes());
250: } else {
251: assertEquals(new SimpleAttributeSet(), kit
252: .getInputAttributes());
253: }
254: }
255: }
256:
257: public void testGetInputAttributesNoComponent() throws Exception {
258: final AttributeSet as = kit.getInputAttributes();
259: assertNotNull(as);
260: assertEquals(0, as.getAttributeCount());
261: }
262:
263: static final Element getElementByOffset(final Document doc,
264: final int offset) {
265: int pos = (offset == 0) ? 0 : offset - 1;
266: Element elem = doc.getDefaultRootElement();
267: while (elem.getElementCount() > 0) {
268: elem = elem.getElement(elem.getElementIndex(pos));
269: }
270: return elem;
271: }
272:
273: public void testGetViewFactory() {
274: ViewFactory factory = kit.getViewFactory();
275: Element element = new SimpleElement(
276: AbstractDocument.ContentElementName);
277: assertEquals("javax.swing.text.LabelView", factory.create(
278: element).getClass().getName());
279: element = new SimpleElement(
280: AbstractDocument.ParagraphElementName);
281: assertEquals("javax.swing.text.ParagraphView", factory.create(
282: element).getClass().getName());
283: element = new SimpleElement(AbstractDocument.SectionElementName);
284: View v = factory.create(element);
285: assertEquals("javax.swing.text.BoxView", v.getClass().getName());
286: assertEquals(View.Y_AXIS, ((BoxView) v).getAxis());
287: element = new SimpleElement(StyleConstants.ComponentElementName);
288: assertEquals("javax.swing.text.ComponentView", factory.create(
289: element).getClass().getName());
290: element = new SimpleElement(StyleConstants.IconElementName);
291: assertEquals("javax.swing.text.IconView", factory.create(
292: element).getClass().getName());
293: element = new SimpleElement("something");
294: assertEquals("javax.swing.text.LabelView", factory.create(
295: element).getClass().getName());
296: ViewFactory factory1 = kit.getViewFactory();
297: assertEquals(factory, factory1);
298: }
299:
300: public void testInstallJEditorPane() {
301: }
302:
303: /**
304: * Regression test for HARMONY-2594
305: * */
306: public void testcreateInputAttributes() {
307: MyStyledEditorKit msek = new MyStyledEditorKit();
308: MutableAttributeSet set = new Style() {
309: public void removeChangeListener(ChangeListener p0) {
310: return;
311: }
312:
313: public void addChangeListener(ChangeListener p0) {
314: return;
315: }
316:
317: public String getName() {
318: return "AA";
319: }
320:
321: public void setResolveParent(AttributeSet p0) {
322: return;
323: }
324:
325: public void removeAttributes(AttributeSet p0) {
326: return;
327: }
328:
329: public void removeAttributes(Enumeration p0) {
330: return;
331: }
332:
333: public void removeAttribute(Object p0) {
334: return;
335: }
336:
337: public void addAttributes(AttributeSet p0) {
338: return;
339: }
340:
341: public void addAttribute(Object p0, Object p1) {
342: return;
343: }
344:
345: public AttributeSet getResolveParent() {
346: return null;
347: }
348:
349: public boolean containsAttributes(AttributeSet p0) {
350: return false;
351: }
352:
353: public boolean containsAttribute(Object p0, Object p1) {
354: return false;
355: }
356:
357: public Enumeration getAttributeNames() {
358: return null;
359: }
360:
361: public Object getAttribute(Object p0) {
362: return null;
363: }
364:
365: public AttributeSet copyAttributes() {
366: return null;
367: }
368:
369: public boolean isEqual(AttributeSet p0) {
370: return false;
371: }
372:
373: public boolean isDefined(Object p0) {
374: return false;
375: }
376:
377: public int getAttributeCount() {
378: return 0;
379: }
380: };
381: try {
382: msek.createInputAttributes(null, set);
383: fail("NPE not thrown when Element is null!");
384: } catch (NullPointerException npe) {
385: // expected
386: }
387: }
388:
389: /**
390: * Regression test for HARMONY-2594
391: * */
392: public void testCreateInputAttributes2() {
393: MyStyledEditorKit msek = new MyStyledEditorKit();
394: try {
395: msek.createInputAttributes(new SimpleElement(""), null);
396: fail("NPE not thrown when MutableAttributeSet is null!");
397: } catch (NullPointerException npe) {
398: // expected
399: }
400: }
401:
402: class MyStyledEditorKit extends StyledEditorKit {
403: public MyStyledEditorKit() {
404: super ();
405: }
406:
407: public void createInputAttributes(Element element,
408: MutableAttributeSet set) {
409: super.createInputAttributes(element, set);
410: }
411: }
412:
413: }
|