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 Dennis Ushakov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.beans.PropertyChangeEvent;
023: import java.text.DecimalFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.Arrays;
026: import java.util.Date;
027: import javax.swing.JFormattedTextField.AbstractFormatter;
028: import javax.swing.JSpinner.DateEditor;
029: import javax.swing.JSpinner.DefaultEditor;
030: import javax.swing.JSpinner.ListEditor;
031: import javax.swing.JSpinner.NumberEditor;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034: import javax.swing.plaf.SpinnerUI;
035: import javax.swing.text.DateFormatter;
036: import javax.swing.text.DefaultFormatterFactory;
037: import javax.swing.text.NumberFormatter;
038: import org.apache.harmony.x.swing.StringConstants;
039: import org.apache.harmony.x.swing.Utilities;
040:
041: public class JSpinnerTest extends BasicSwingTestCase {
042: private JSpinner spinner;
043:
044: private SpinnerModel abstractModel;
045:
046: private ChangeController chl;
047:
048: @Override
049: public void setUp() {
050: spinner = new JSpinner();
051: propertyChangeController = new PropertyChangeController();
052: spinner.addPropertyChangeListener(propertyChangeController);
053: chl = new ChangeController();
054: spinner.addChangeListener(chl);
055: abstractModel = new SpinnerModel() {
056: public void addChangeListener(ChangeListener l) {
057: }
058:
059: public Object getNextValue() {
060: return null;
061: }
062:
063: public Object getPreviousValue() {
064: return null;
065: }
066:
067: public Object getValue() {
068: return null;
069: }
070:
071: public void removeChangeListener(ChangeListener l) {
072: }
073:
074: public void setValue(Object value) {
075: }
076: };
077: }
078:
079: @Override
080: public void tearDown() {
081: spinner = null;
082: abstractModel = null;
083: propertyChangeController = null;
084: chl = null;
085: }
086:
087: public void testJSpinner() {
088: assertTrue(spinner.getModel() instanceof SpinnerNumberModel);
089: assertTrue(spinner.getEditor() instanceof JSpinner.NumberEditor);
090: assertTrue(Arrays.asList(spinner.getChangeListeners())
091: .contains(spinner.getEditor()));
092: }
093:
094: public void testSetGetUpdateUI() {
095: assertEquals(spinner.getUIClassID(), "SpinnerUI");
096: SpinnerUI defaultUI = spinner.getUI();
097: assertNotNull(defaultUI);
098: SpinnerUI ui = new SpinnerUI() {
099: };
100: spinner.setUI(ui);
101: assertEquals(ui, spinner.getUI());
102: assertTrue(propertyChangeController.isChanged("UI"));
103: propertyChangeController.reset();
104: spinner.updateUI();
105: assertNotSame(ui, spinner.getUI());
106: assertTrue(propertyChangeController.isChanged("UI"));
107: }
108:
109: public void testSetGetModel() {
110: SpinnerNumberModel newModel = new SpinnerNumberModel(0, -1, 1,
111: 1);
112: spinner.setModel(newModel);
113: assertSame(newModel, spinner.getModel());
114: assertTrue(propertyChangeController
115: .isChanged(StringConstants.MODEL_PROPERTY_CHANGED));
116: assertTrue(propertyChangeController
117: .isChanged(StringConstants.EDITOR_PROPERTY_CHANGED));
118: assertEquals(1, newModel.getChangeListeners().length);
119: spinner.setEditor(new JButton());
120: propertyChangeController.reset();
121: spinner.setModel(new SpinnerNumberModel(0, -2, 2, 1));
122: assertFalse(propertyChangeController
123: .isChanged(StringConstants.EDITOR_PROPERTY_CHANGED));
124: testExceptionalCase(new IllegalArgumentCase() {
125: @Override
126: public void exceptionalAction() throws Exception {
127: spinner.setModel(null);
128: }
129: });
130: if (isHarmony()) {
131: SpinnerNumberModel old = (SpinnerNumberModel) spinner
132: .getModel();
133: spinner.setModel(new SpinnerDateModel());
134: assertEquals(old.getChangeListeners().length, 0);
135: }
136: }
137:
138: public void testSetGetValue() {
139: spinner.setValue(new Integer(10));
140: assertTrue(chl.isChanged());
141: assertEquals(spinner.getModel().getValue(), spinner.getValue());
142: }
143:
144: public void testGetPreviousNextValue() {
145: assertEquals(spinner.getNextValue(), spinner.getModel()
146: .getNextValue());
147: assertEquals(spinner.getPreviousValue(), spinner.getModel()
148: .getPreviousValue());
149: }
150:
151: public void testAddRemoveGetChangeListener() {
152: assertEquals(2, spinner.getChangeListeners().length);
153: assertTrue(Arrays.asList(spinner.getChangeListeners())
154: .contains(chl));
155: ChangeListener listener = new ChangeListener() {
156: public void stateChanged(ChangeEvent e) {
157: }
158: };
159: spinner.addChangeListener(listener);
160: assertTrue(Arrays.asList(spinner.getChangeListeners())
161: .contains(listener));
162: spinner.removeChangeListener(chl);
163: spinner.removeChangeListener(listener);
164: assertEquals(1, spinner.getChangeListeners().length);
165: }
166:
167: public void testFireStateChanged() {
168: spinner.getModel().setValue(new Integer(10));
169: assertTrue(chl.isChanged());
170: }
171:
172: public void testSetGetEditor() throws Exception {
173: JComponent oldEditor = spinner.getEditor();
174: assertNotNull(oldEditor);
175: JComponent editor = new JProgressBar();
176: spinner.setEditor(editor);
177: assertFalse(Arrays.asList(spinner.getChangeListeners())
178: .contains(editor));
179: assertTrue(propertyChangeController
180: .isChanged(StringConstants.EDITOR_PROPERTY_CHANGED));
181: assertSame(editor, spinner.getEditor());
182: editor = new JSpinner.NumberEditor(spinner);
183: spinner.setEditor(editor);
184: assertTrue(Arrays.asList(spinner.getChangeListeners())
185: .contains(editor));
186: assertTrue(propertyChangeController
187: .isChanged(StringConstants.EDITOR_PROPERTY_CHANGED));
188: assertSame(editor, spinner.getEditor());
189: testExceptionalCase(new IllegalArgumentCase() {
190: @Override
191: public void exceptionalAction() throws Exception {
192: spinner.setEditor(null);
193: }
194: });
195: spinner = new JSpinner();
196: spinner.setEditor(oldEditor);
197: assertFalse(Arrays.asList(spinner.getChangeListeners())
198: .contains(oldEditor));
199: spinner = new JSpinner();
200: oldEditor = spinner.getEditor();
201: spinner.setEditor(oldEditor);
202: spinner.setModel(new SpinnerDateModel());
203: assertNotSame(spinner.getEditor(), oldEditor);
204: spinner = new JSpinner();
205: oldEditor = spinner.getEditor();
206: spinner.setEditor(oldEditor);
207: spinner.setModel(new SpinnerDateModel());
208: assertNotSame(spinner.getEditor(), oldEditor);
209: }
210:
211: public void testCreateEditor() {
212: SpinnerModel model = new SpinnerNumberModel();
213: spinner.setModel(model);
214: assertTrue(spinner.createEditor(model) instanceof JSpinner.NumberEditor);
215: model = new SpinnerDateModel();
216: spinner.setModel(model);
217: assertTrue(spinner.createEditor(model) instanceof JSpinner.DateEditor);
218: model = new SpinnerListModel();
219: spinner.setModel(model);
220: assertTrue(spinner.createEditor(model) instanceof JSpinner.ListEditor);
221: assertTrue(spinner.createEditor(abstractModel) instanceof JSpinner.DefaultEditor);
222: }
223:
224: public void testGetAccessibleContext() {
225: }
226:
227: public void testDefaultEditor_DefaultEditor() {
228: DefaultEditor defaultEditor = (DefaultEditor) spinner
229: .createEditor(abstractModel);
230: spinner.setEditor(defaultEditor);
231: assertTrue(Arrays.asList(
232: defaultEditor.getTextField()
233: .getPropertyChangeListeners()).contains(
234: defaultEditor));
235: assertFalse(defaultEditor.getTextField().isEditable());
236: assertSame(defaultEditor.getTextField(), defaultEditor
237: .getComponent(0));
238: JFormattedTextField textField = ((DefaultEditor) spinner
239: .getEditor()).getTextField();
240: assertSame(textField.getActionForKeyStroke(KeyStroke
241: .getKeyStroke("DOWN")), textField
242: .getActionForKeyStroke(KeyStroke.getKeyStroke("UP")));
243: textField
244: .setFormatterFactory(new DefaultFormatterFactory(null));
245: textField.setValue("TEST");
246: assertEquals(new Integer(0), textField.getValue());
247: }
248:
249: public void testDefaultEditor_propertyChange() throws Exception {
250: DefaultEditor defaultEditor = (DefaultEditor) spinner
251: .getEditor();
252: PropertyChangeController pcc = new PropertyChangeController();
253: ChangeController modelController = new ChangeController();
254: defaultEditor.getTextField().addPropertyChangeListener(pcc);
255: spinner.getModel().addChangeListener(modelController);
256: defaultEditor.getTextField().setText("15");
257: defaultEditor.commitEdit();
258: assertTrue(pcc.isChanged());
259: assertTrue(modelController.isChanged());
260: modelController.reset();
261: defaultEditor.getTextField().removePropertyChangeListener(
262: defaultEditor);
263: defaultEditor.getTextField().setText("18");
264: defaultEditor.commitEdit();
265: defaultEditor.getTextField().addPropertyChangeListener(
266: defaultEditor);
267: defaultEditor.propertyChange(new PropertyChangeEvent(
268: defaultEditor.getTextField(), "value", defaultEditor
269: .getTextField().getValue(), new Integer(10)));
270: assertTrue(modelController.isChanged());
271: modelController.reset();
272: defaultEditor.getTextField().removePropertyChangeListener(
273: defaultEditor);
274: defaultEditor.getTextField().setText("58");
275: defaultEditor.commitEdit();
276: defaultEditor.getTextField().addPropertyChangeListener(
277: defaultEditor);
278: defaultEditor.propertyChange(new PropertyChangeEvent(
279: new Integer(10), "value", new Integer(13), new Integer(
280: 10)));
281: assertFalse(modelController.isChanged());
282: }
283:
284: public void testDefaultEditor_stateChange() {
285: DefaultEditor defaultEditor = (DefaultEditor) spinner
286: .getEditor();
287: PropertyChangeController pcc = new PropertyChangeController();
288: defaultEditor.getTextField().addPropertyChangeListener(pcc);
289: spinner.setValue(new Integer(159));
290: assertTrue(pcc.isChanged());
291: }
292:
293: public void testDefaultEditor_LayoutSizes() {
294: DefaultEditor defaultEditor = (DefaultEditor) spinner
295: .createEditor(abstractModel);
296: spinner.setEditor(defaultEditor);
297: assertEquals(defaultEditor.minimumLayoutSize(spinner),
298: Utilities.addInsets(defaultEditor.getTextField()
299: .getMinimumSize(), spinner.getInsets()));
300: assertEquals(defaultEditor.preferredLayoutSize(spinner),
301: Utilities.addInsets(defaultEditor.getTextField()
302: .getPreferredSize(), spinner.getInsets()));
303: }
304:
305: public void testNumberEditor_NumberEditor() {
306: spinner.getModel().setValue(new Integer(5));
307: NumberEditor numEditor = new NumberEditor(spinner);
308: spinner.setEditor(numEditor);
309: assertTrue(numEditor.getTextField().isEditable());
310: assertTrue(numEditor.getTextField().getFormatter() instanceof NumberFormatter);
311: assertEquals(numEditor.getTextField().getValue(),
312: new Integer(5));
313: assertSame(((NumberFormatter) numEditor.getTextField()
314: .getFormatter()).getValueClass(), Integer.class);
315: assertNull(((NumberFormatter) numEditor.getTextField()
316: .getFormatter()).getMinimum());
317: assertNull(((NumberFormatter) numEditor.getTextField()
318: .getFormatter()).getMaximum());
319: assertTrue(numEditor.getFormat().equals(new DecimalFormat()));
320: spinner.setModel(abstractModel);
321: testExceptionalCase(new IllegalArgumentCase() {
322: @Override
323: public void exceptionalAction() throws Exception {
324: new JSpinner.NumberEditor(spinner);
325: }
326: });
327: }
328:
329: public void testNumberEditor_formatter() {
330: NumberEditor numEditor = new NumberEditor(spinner);
331: spinner.setEditor(numEditor);
332: final Integer max1 = new Integer(777);
333: NumberFormatter numberFormatter = ((NumberFormatter) numEditor
334: .getTextField().getFormatter());
335: numberFormatter.setMaximum(max1);
336: assertSame(numberFormatter.getMaximum(), max1);
337: assertSame(numEditor.getModel().getMaximum(), max1);
338: final Integer max2 = new Integer(555);
339: numEditor.getModel().setMaximum(max2);
340: assertSame(numberFormatter.getMaximum(), max2);
341: assertSame(numEditor.getModel().getMaximum(), max2);
342: SpinnerNumberModel old = (SpinnerNumberModel) spinner
343: .getModel();
344: spinner.setModel(abstractModel);
345: final Integer max3 = new Integer(333);
346: old.setMaximum(max3);
347: assertSame(((NumberFormatter) ((NumberEditor) spinner
348: .getEditor()).getTextField().getFormatter())
349: .getMaximum(), max3);
350: }
351:
352: public void testNumberEditor_getModel() {
353: NumberEditor numEditor = new NumberEditor(spinner);
354: spinner.setEditor(numEditor);
355: assertSame(numEditor.getModel(), spinner.getModel());
356: spinner.setModel(abstractModel);
357: testExceptionalCase(new ExceptionalCase(null,
358: ClassCastException.class) {
359: @Override
360: public void exceptionalAction() throws Exception {
361: ((NumberEditor) spinner.getEditor()).getModel();
362: }
363: });
364: }
365:
366: public void testDateEditor_DateEditor() {
367: spinner.setModel(new SpinnerDateModel());
368: DateEditor dateEditor = (DateEditor) spinner.getEditor();
369: spinner.setEditor(dateEditor);
370: assertTrue(dateEditor.getTextField().isEditable());
371: assertTrue(dateEditor.getTextField().getFormatter() instanceof DateFormatter);
372: assertNull(((DateFormatter) dateEditor.getTextField()
373: .getFormatter()).getMinimum());
374: assertNull(((DateFormatter) dateEditor.getTextField()
375: .getFormatter()).getMaximum());
376: assertTrue(dateEditor.getFormat()
377: .equals(new SimpleDateFormat()));
378: spinner.setModel(abstractModel);
379: testExceptionalCase(new IllegalArgumentCase() {
380: @Override
381: public void exceptionalAction() throws Exception {
382: new JSpinner.DateEditor(spinner);
383: }
384: });
385: }
386:
387: public void testDateEditor_formatter() {
388: spinner.setModel(new SpinnerDateModel());
389: DateEditor dateEditor = new DateEditor(spinner);
390: spinner.setEditor(dateEditor);
391: final Date date1 = new Date(777);
392: DateFormatter dateFormatter = ((DateFormatter) dateEditor
393: .getTextField().getFormatter());
394: dateFormatter.setMaximum(date1);
395: assertSame(dateFormatter.getMaximum(), date1);
396: assertSame(dateEditor.getModel().getEnd(), date1);
397: final Date date2 = new Date(555);
398: dateEditor.getModel().setEnd(date2);
399: assertSame(dateFormatter.getMaximum(), date2);
400: assertSame(dateEditor.getModel().getEnd(), date2);
401: SpinnerDateModel old = (SpinnerDateModel) spinner.getModel();
402: spinner.setModel(abstractModel);
403: final Date date3 = new Date(555);
404: old.setEnd(date3);
405: assertEquals(
406: ((DateFormatter) ((DateEditor) spinner.getEditor())
407: .getTextField().getFormatter()).getMaximum(),
408: date3);
409: }
410:
411: public void testListEditor_ListEditor() {
412: Object[] values = { "arrline1", "arrline2", "text",
413: new Integer(33), spinner };
414: spinner.setModel(new SpinnerListModel(values));
415: ListEditor listEditor = new ListEditor(spinner);
416: spinner.setEditor(listEditor);
417: assertTrue(listEditor.getTextField().isEditable());
418: spinner.setModel(abstractModel);
419: testExceptionalCase(new IllegalArgumentCase() {
420: @Override
421: public void exceptionalAction() throws Exception {
422: new JSpinner.ListEditor(spinner);
423: }
424: });
425: }
426:
427: public void testListEditor_formatter() throws Exception {
428: JComponent comp = new JButton();
429: Object[] values = { "arrline1", "arrline2", "text",
430: new Integer(33), comp };
431: spinner.setModel(new SpinnerListModel(values));
432: ListEditor listEditor = new ListEditor(spinner);
433: spinner.setEditor(listEditor);
434: AbstractFormatter formatter = ((ListEditor) spinner.getEditor())
435: .getTextField().getFormatter();
436: assertEquals(formatter.valueToString(null), "");
437: assertEquals(formatter.valueToString(new Integer(33)), "33");
438: assertEquals(formatter.stringToValue("text"), "text");
439: }
440: }
|