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;
021:
022: import java.awt.event.ActionEvent;
023: import java.beans.PropertyChangeEvent;
024: import java.beans.PropertyChangeListener;
025: import java.text.ParseException;
026: import javax.swing.plaf.UIResource;
027: import javax.swing.text.AbstractDocument;
028: import javax.swing.text.DocumentFilter;
029: import javax.swing.text.NavigationFilter;
030: import javax.swing.text.TextAction;
031:
032: public class JFormattedTextField_AbstractFormatterTest extends
033: SwingTestCase {
034: static class AbstractFormatter extends
035: JFormattedTextField.AbstractFormatter implements Cloneable {
036: private static final long serialVersionUID = 1L;
037:
038: Action[] actions;
039:
040: NavigationFilter navigationFilter;
041:
042: DocumentFilter documentFilter;
043:
044: @Override
045: protected Action[] getActions() {
046: return actions == null ? super .getActions() : actions;
047: }
048:
049: @Override
050: protected DocumentFilter getDocumentFilter() {
051: return documentFilter == null ? super .getDocumentFilter()
052: : documentFilter;
053: }
054:
055: @Override
056: protected NavigationFilter getNavigationFilter() {
057: return navigationFilter == null ? super
058: .getNavigationFilter() : navigationFilter;
059: }
060:
061: final void setActions(final Action[] newActions) {
062: actions = newActions;
063: }
064:
065: final void setDocumentFilter(final DocumentFilter filter) {
066: documentFilter = filter;
067: }
068:
069: final void setNavigationFilter(final NavigationFilter filter) {
070: navigationFilter = filter;
071: }
072:
073: @Override
074: public Object stringToValue(final String string)
075: throws ParseException {
076: return null;
077: }
078:
079: @Override
080: public String valueToString(final Object value)
081: throws ParseException {
082: return null;
083: }
084: }
085:
086: class TextActionImpl extends TextAction {
087: private static final long serialVersionUID = 1L;
088:
089: TextActionImpl(final String name) {
090: super (name);
091: }
092:
093: public void actionPerformed(final ActionEvent e) {
094: }
095: }
096:
097: AbstractFormatter formatter;
098:
099: JFormattedTextField tf;
100:
101: @Override
102: protected void setUp() throws Exception {
103: super .setUp();
104: formatter = new AbstractFormatter();
105: tf = new JFormattedTextField();
106: }
107:
108: class PropertyChangeListenerImpl implements PropertyChangeListener {
109: String name;
110:
111: Object oldValue;
112:
113: Object newValue;
114:
115: String interestingPropertyName;
116:
117: public void propertyChange(final PropertyChangeEvent e) {
118: String propertyName = e.getPropertyName();
119: if (propertyName.equals(interestingPropertyName)) {
120: name = e.getPropertyName();
121: oldValue = e.getOldValue();
122: newValue = e.getNewValue();
123: }
124: }
125:
126: final void setInterestingPropertyName(final String propertyName) {
127: interestingPropertyName = propertyName;
128: }
129: }
130:
131: @Override
132: protected void tearDown() throws Exception {
133: super .tearDown();
134: }
135:
136: public void testClone() {
137: tf.setFormatter(formatter);
138: Object clone = null;
139: try {
140: clone = formatter.clone();
141: } catch (CloneNotSupportedException e) {
142: assertTrue("UnexpectedException: " + e.getMessage(), false);
143: }
144: assertTrue(clone instanceof JFormattedTextField.AbstractFormatter);
145: assertNull(((JFormattedTextField.AbstractFormatter) clone)
146: .getFormattedTextField());
147: }
148:
149: public void testGetActions() {
150: assertNull(formatter.getActions());
151: }
152:
153: final void printActionMap(final ActionMap actionMap) {
154: if (actionMap == null) {
155: return;
156: }
157: for (int i = 0; i < actionMap.size(); i++) {
158: Object key = actionMap.keys()[i];
159: System.out
160: .println(i + " " + key + " " + actionMap.get(key));
161: }
162: }
163:
164: private void checkActionMap(final ActionMap map,
165: final Action[] actions) {
166: if (actions == null) {
167: assertEquals(0, map.size());
168: return;
169: }
170: assertEquals(actions.length, map.size());
171: Object[] keys = map.keys();
172: for (int i = 0; i < keys.length; i++) {
173: String name = (String) keys[i];
174: Action action = map.get(name);
175: boolean contains = false;
176: for (int j = 0; j < actions.length; j++) {
177: Action a = actions[j];
178: if (a.getValue(Action.NAME).equals(name) && action == a) {
179: contains = true;
180: break;
181: }
182: }
183: assertTrue(contains);
184: }
185: }
186:
187: public void testInstallUninstall_Filters() {
188: NavigationFilter navFilter = new NavigationFilter();
189: formatter.setNavigationFilter(navFilter);
190: DocumentFilter docFilter = new DocumentFilter();
191: formatter.setDocumentFilter(docFilter);
192: AbstractDocument doc = (AbstractDocument) tf.getDocument();
193: assertNull(tf.getNavigationFilter());
194: assertNull(doc.getDocumentFilter());
195: formatter.install(tf);
196: assertEquals(navFilter, tf.getNavigationFilter());
197: assertEquals(docFilter, doc.getDocumentFilter());
198: formatter.uninstall();
199: assertNull(tf.getNavigationFilter());
200: assertNull(doc.getDocumentFilter());
201: formatter.install(tf);
202: assertEquals(navFilter, tf.getNavigationFilter());
203: assertEquals(docFilter, doc.getDocumentFilter());
204: formatter.install(null);
205: assertNull(tf.getNavigationFilter());
206: assertNull(doc.getDocumentFilter());
207: }
208:
209: public void testInstallUninstall_Actions() {
210: Action[] actions = new Action[] { new TextActionImpl("1"),
211: new TextActionImpl("2") };
212: formatter.setActions(actions);
213: AbstractDocument doc = (AbstractDocument) tf.getDocument();
214: ActionMap map1 = tf.getActionMap();
215: ActionMap map2 = map1.getParent(); //keymap
216: ActionMap map3 = map2.getParent(); //uiActionMap
217: assertTrue(map3 instanceof UIResource);
218: assertNull(tf.getNavigationFilter());
219: assertNull(doc.getDocumentFilter());
220: formatter.install(tf);
221: ActionMap _map1 = tf.getActionMap();
222: ActionMap _map2 = _map1.getParent(); //keymap
223: ActionMap _map3 = _map2.getParent(); //formatter
224: ActionMap _map4 = _map3.getParent();
225: assertEquals(map1, _map1);
226: assertEquals(map2, _map2);
227: assertEquals(map3, _map4);
228: checkActionMap(_map3, actions);
229: //TODO: Decide if uninstall() & install(null) should reset remove actions
230: //installed by formatter.
231: formatter.install(null);
232: _map1 = tf.getActionMap();
233: _map2 = _map1.getParent();
234: _map3 = _map2.getParent();
235: _map4 = _map3.getParent();
236: assertEquals(map1, _map1);
237: assertEquals(map2, _map2);
238: if (isHarmony()) {
239: assertEquals(map3, _map3);
240: } else {
241: assertEquals(map3, _map4);
242: checkActionMap(_map3, null);
243: }
244: formatter.install(tf);
245: _map1 = tf.getActionMap();
246: _map2 = _map1.getParent();
247: _map3 = _map2.getParent();
248: _map4 = _map3.getParent();
249: assertEquals(map1, _map1);
250: assertEquals(map2, _map2);
251: assertEquals(map3, _map4);
252: checkActionMap(_map3, actions);
253: formatter.uninstall();
254: _map1 = tf.getActionMap();
255: _map2 = _map1.getParent();
256: _map3 = _map2.getParent();
257: _map4 = _map3.getParent();
258: assertEquals(map1, _map1);
259: assertEquals(map2, _map2);
260: if (isHarmony()) {
261: assertEquals(map3, _map3);
262: } else {
263: assertEquals(map3, _map4);
264: checkActionMap(_map3, null);
265: }
266: }
267:
268: public void testSetEditValid() {
269: tf.setFormatter(formatter);
270: PropertyChangeListenerImpl listener = new PropertyChangeListenerImpl();
271: listener.setInterestingPropertyName("editValid");
272: tf.addPropertyChangeListener(listener);
273: formatter.setEditValid(false);
274: formatter.setEditValid(true);
275: assertEquals(Boolean.TRUE, listener.newValue);
276: assertEquals(Boolean.FALSE, listener.oldValue);
277: }
278:
279: public void testGetNavigationFilter() {
280: assertNull(formatter.getNavigationFilter());
281: }
282:
283: public void testGetDocumentFilter() {
284: assertNull(formatter.getDocumentFilter());
285: }
286:
287: public void testGetFormattedTextField() {
288: assertNull(formatter.getFormattedTextField());
289: formatter.install(tf);
290: assertEquals(tf, formatter.getFormattedTextField());
291: formatter.uninstall();
292: //Perhaps, uninstall should reset getFormattedTextField.
293: assertEquals(tf, formatter.getFormattedTextField());
294: formatter.install(null);
295: assertNull(formatter.getFormattedTextField());
296: }
297: }
|