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.Color;
023: import java.awt.event.ActionEvent;
024: import javax.swing.Action;
025: import javax.swing.JEditorPane;
026: import javax.swing.SwingTestCase;
027:
028: public class StyledEditorKit_ActionsTest extends SwingTestCase {
029: JEditorPane jep;
030:
031: int dot;
032:
033: StyledEditorKit kit;
034:
035: StyledDocument doc;
036:
037: @Override
038: protected void setUp() throws Exception {
039: super .setUp();
040: setIgnoreNotImplemented(true);
041: jep = new JEditorPane();
042: kit = new StyledEditorKit();
043: jep.setEditorKit(kit);
044: doc = (StyledDocument) jep.getDocument();
045: jep.setText("ABCD\nEFGH\n" + "IGKL\nMNOP\n" + "QRST\nUVWX\n");
046: dot = 18;
047: jep.setCaretPosition(dot);
048: }
049:
050: @Override
051: protected void tearDown() throws Exception {
052: super .tearDown();
053: }
054:
055: private String toString(final int i) {
056: return (new Integer(i)).toString();
057: }
058:
059: private void checkAttribute(final int offset, final Object key,
060: final Object value) {
061: Object realValue = ((AbstractDocument) jep.getDocument())
062: .getParagraphElement(offset).getAttributes()
063: .getAttribute(key);
064: assertEquals(value, realValue);
065: }
066:
067: private void checkAttribute(final int offset, final Object key,
068: final int value) {
069: checkAttribute(offset, key, new Integer(value));
070: }
071:
072: private void checkName(final Action action, final String name) {
073: assertEquals(name, action.getValue(Action.NAME));
074: }
075:
076: private void assertTrue(final Object obj) {
077: assertTrue(((Boolean) obj).booleanValue());
078: }
079:
080: private void assertFalse(final Object obj) {
081: assertFalse(((Boolean) obj).booleanValue());
082: }
083:
084: private void checkProperty(final int start, final int end,
085: final Object attribute, final Object value,
086: final Object alternative) {
087: for (int i = 0; i < doc.getLength(); i++) {
088: Object property = StyledEditorKit_StyledTextActionTest
089: .getAttributeSetByOffset(doc, i).getAttribute(
090: attribute);
091: if (i >= start && i <= end) {
092: assertEquals(value, property);
093: } else {
094: assertEquals(alternative, property);
095: }
096: }
097: }
098:
099: private void checkToggleAction(final Action action,
100: final String name, final Object attribute) {
101: checkName(action, name);
102: ActionEvent ae = new ActionEvent(jep,
103: ActionEvent.ACTION_PERFORMED, null);
104: action.actionPerformed(ae);
105: assertTrue(kit.getInputAttributes().getAttribute(attribute));
106: action.actionPerformed(ae);
107: assertFalse(kit.getInputAttributes().getAttribute(attribute));
108: action.actionPerformed(ae);
109: assertTrue(kit.getInputAttributes().getAttribute(attribute));
110: ae = new ActionEvent(jep, ActionEvent.ACTION_PERFORMED, "true");
111: action.actionPerformed(ae);
112: assertFalse(kit.getInputAttributes().getAttribute(attribute));
113: jep.setSelectionStart(2);
114: jep.setSelectionEnd(5);
115: action.actionPerformed(ae);
116: checkProperty(3, 5, attribute, Boolean.TRUE, null);
117: assertTrue(kit.getInputAttributes().getAttribute(attribute));
118: action.actionPerformed(null);
119: }
120:
121: private void checkNonToggleAction(final Action action,
122: final String name, final Object attribute,
123: final Object value, final Object defaultValue,
124: final Object alternative, final String param1,
125: final String param2) {
126: checkName(action, name);
127: ActionEvent ae1 = new ActionEvent(jep,
128: ActionEvent.ACTION_PERFORMED, param1);
129: action.actionPerformed(ae1);
130: checkProperty(100, 100, attribute, alternative, alternative);
131: assertEquals(defaultValue, kit.getInputAttributes()
132: .getAttribute(attribute));
133: ActionEvent ae2 = new ActionEvent(jep,
134: ActionEvent.ACTION_PERFORMED, param2);
135: action.actionPerformed(ae2);
136: checkProperty(100, 100, attribute, alternative, alternative);
137: assertEquals(value, kit.getInputAttributes().getAttribute(
138: attribute));
139: jep.setSelectionStart(2);
140: jep.setSelectionEnd(5);
141: action.actionPerformed(ae1);
142: assertEquals(defaultValue, kit.getInputAttributes()
143: .getAttribute(attribute));
144: checkProperty(3, 5, attribute, defaultValue, alternative);
145: action.actionPerformed(ae2);
146: assertEquals(value, kit.getInputAttributes().getAttribute(
147: attribute));
148: checkProperty(3, 5, attribute, value, alternative);
149: action.actionPerformed(null);
150: }
151:
152: public void testAlignmentAction() {
153: Action action = new StyledEditorKit.AlignmentAction(null,
154: StyleConstants.ALIGN_JUSTIFIED);
155: checkName(action, null);
156: action = new StyledEditorKit.AlignmentAction("alignmentName",
157: StyleConstants.ALIGN_JUSTIFIED);
158: checkName(action, "alignmentName");
159: ActionEvent ae = new ActionEvent(jep,
160: ActionEvent.ACTION_PERFORMED,
161: toString(StyleConstants.ALIGN_CENTER));
162: action.actionPerformed(ae);
163: checkAttribute(dot, StyleConstants.Alignment,
164: StyleConstants.ALIGN_CENTER);
165: ae = new ActionEvent(jep, ActionEvent.ACTION_PERFORMED, null);
166: action.actionPerformed(ae);
167: checkAttribute(dot, StyleConstants.Alignment,
168: StyleConstants.ALIGN_JUSTIFIED);
169: ae = new ActionEvent(jep, ActionEvent.ACTION_PERFORMED, "aa");
170: action.actionPerformed(ae);
171: checkAttribute(dot, StyleConstants.Alignment,
172: StyleConstants.ALIGN_JUSTIFIED);
173: ae = new ActionEvent(jep, ActionEvent.ACTION_PERFORMED, "");
174: action.actionPerformed(ae);
175: checkAttribute(dot, StyleConstants.Alignment,
176: StyleConstants.ALIGN_JUSTIFIED);
177: action.actionPerformed(null);
178: }
179:
180: public void testBoldAction() {
181: Action action = new StyledEditorKit.BoldAction();
182: checkToggleAction(action, "font-bold", StyleConstants.Bold);
183: }
184:
185: public void testFontFamilyAction() {
186: String defaultValue = "SansSerif";
187: Object alternative = null;
188: String value = "Monospaced";
189: Action action = new StyledEditorKit.FontFamilyAction(null,
190: defaultValue);
191: checkName(action, null);
192: action = new StyledEditorKit.FontFamilyAction(
193: "familyActionName", defaultValue);
194: Object attribute = StyleConstants.FontFamily;
195: checkNonToggleAction(action, "familyActionName", attribute,
196: value, defaultValue, alternative, null, value);
197: }
198:
199: public void testFontSizeAction() {
200: Integer defaultValue = new Integer(48);
201: Object alternative = null;
202: Integer value = new Integer(16);
203: Action action = new StyledEditorKit.FontFamilyAction(null, "48");
204: checkName(action, null);
205: action = new StyledEditorKit.FontSizeAction("sizeActionName",
206: 48);
207: Object attribute = StyleConstants.FontSize;
208: checkNonToggleAction(action, "sizeActionName", attribute,
209: value, defaultValue, alternative, "", "16");
210: }
211:
212: public void testForegroundAction() {
213: Color defaultValue = Color.RED;
214: Object alternative = null;
215: Object value = Color.decode("425");
216: Action action = new StyledEditorKit.ForegroundAction(null,
217: defaultValue);
218: checkName(action, null);
219: action = new StyledEditorKit.ForegroundAction("foregroundName",
220: defaultValue);
221: Object attribute = StyleConstants.Foreground;
222: checkNonToggleAction(action, "foregroundName", attribute,
223: value, defaultValue, alternative, "", "425");
224: }
225:
226: public void testItalicAction() {
227: Action action = new StyledEditorKit.ItalicAction();
228: checkToggleAction(action, "font-italic", StyleConstants.Italic);
229: }
230:
231: public void testUnderlineAction() {
232: Action action = new StyledEditorKit.UnderlineAction();
233: checkToggleAction(action, "font-underline",
234: StyleConstants.Underline);
235: }
236: }
|