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 Sergey Burlak
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.util.Dictionary;
023: import java.util.Enumeration;
024: import java.util.Hashtable;
025:
026: public class JSliderTest extends SwingTestCase {
027: private JSlider slider;
028:
029: @Override
030: protected void setUp() throws Exception {
031: slider = new JSlider();
032: }
033:
034: @Override
035: protected void tearDown() throws Exception {
036: slider = null;
037: }
038:
039: public void testNewJSlider() throws Exception {
040: BoundedRangeModel m = new DefaultBoundedRangeModel(2, 10, 0, 50);
041: m.setValueIsAdjusting(true);
042: slider = new JSlider(m);
043: assertTrue(m == slider.getModel());
044:
045: try { // Regression test for HARMONY-2535
046: new JSlider(2);
047: fail("IllegalArgumentException should have been thrown");
048: } catch (IllegalArgumentException e) {
049: // Expected
050: }
051: }
052:
053: public void testCreateChangeListener() throws Exception {
054: assertNotNull(slider.createChangeListener());
055: assertFalse(slider.createChangeListener() == slider
056: .createChangeListener());
057: }
058:
059: public void testSetGetLabelTable() throws Exception {
060: assertNull(slider.getLabelTable());
061: Dictionary<String, String> labelTable = new Hashtable<String, String>();
062: labelTable.put("a", "b");
063: propertyChangeController = new PropertyChangeController();
064: slider.addPropertyChangeListener(propertyChangeController);
065: slider.setLabelTable(labelTable);
066: assertTrue(propertyChangeController.isChanged("labelTable"));
067: assertEquals(labelTable, slider.getLabelTable());
068: labelTable.put("c", "d");
069: assertEquals(labelTable, slider.getLabelTable());
070: propertyChangeController.reset();
071: slider.setLabelTable(labelTable);
072: assertFalse(propertyChangeController.isChanged("labelTable"));
073: }
074:
075: public void testSetGetInverted() throws Exception {
076: assertFalse(slider.getInverted());
077: propertyChangeController = new PropertyChangeController();
078: slider.addPropertyChangeListener(propertyChangeController);
079: slider.setInverted(true);
080: assertTrue(propertyChangeController.isChanged("inverted"));
081: assertTrue(slider.getInverted());
082: propertyChangeController.reset();
083: slider.setInverted(true);
084: assertFalse(propertyChangeController.isChanged("inverted"));
085: }
086:
087: public void testSetGetMajorTickSpacing() throws Exception {
088: assertEquals(0, slider.getMajorTickSpacing());
089: propertyChangeController = new PropertyChangeController();
090: slider.addPropertyChangeListener(propertyChangeController);
091: slider.setMajorTickSpacing(22);
092: assertTrue(propertyChangeController
093: .isChanged("majorTickSpacing"));
094: assertEquals(22, slider.getMajorTickSpacing());
095: propertyChangeController.reset();
096: slider.setMajorTickSpacing(22);
097: assertFalse(propertyChangeController
098: .isChanged("majorTickSpacing"));
099: }
100:
101: public void testSetGetMinorTickSpacing() throws Exception {
102: assertEquals(0, slider.getMinorTickSpacing());
103: propertyChangeController = new PropertyChangeController();
104: slider.addPropertyChangeListener(propertyChangeController);
105: slider.setMinorTickSpacing(22);
106: assertTrue(propertyChangeController
107: .isChanged("minorTickSpacing"));
108: assertEquals(22, slider.getMinorTickSpacing());
109: propertyChangeController.reset();
110: slider.setMinorTickSpacing(22);
111: assertFalse(propertyChangeController
112: .isChanged("minorTickSpacing"));
113: }
114:
115: public void testSetGetSnapToTicks() throws Exception {
116: assertFalse(slider.getSnapToTicks());
117: propertyChangeController = new PropertyChangeController();
118: slider.addPropertyChangeListener(propertyChangeController);
119: slider.setSnapToTicks(true);
120: assertTrue(propertyChangeController.isChanged("snapToTicks"));
121: assertTrue(slider.getSnapToTicks());
122: propertyChangeController.reset();
123: slider.setSnapToTicks(true);
124: assertFalse(propertyChangeController.isChanged("snapToTicks"));
125: }
126:
127: public void testSetGetPaintTicks() throws Exception {
128: assertFalse(slider.getPaintTicks());
129: propertyChangeController = new PropertyChangeController();
130: slider.addPropertyChangeListener(propertyChangeController);
131: slider.setPaintTicks(true);
132: assertTrue(propertyChangeController.isChanged("paintTicks"));
133: assertTrue(slider.getPaintTicks());
134: propertyChangeController.reset();
135: slider.setPaintTicks(true);
136: assertFalse(propertyChangeController.isChanged("paintTicks"));
137: }
138:
139: public void testSetGetPaintTrack() throws Exception {
140: assertTrue(slider.getPaintTrack());
141: propertyChangeController = new PropertyChangeController();
142: slider.addPropertyChangeListener(propertyChangeController);
143: slider.setPaintTrack(false);
144: assertTrue(propertyChangeController.isChanged("paintTrack"));
145: assertFalse(slider.getPaintTrack());
146: propertyChangeController.reset();
147: slider.setPaintTrack(false);
148: assertFalse(propertyChangeController.isChanged("paintTrack"));
149: }
150:
151: public void testSetGetOrientation() throws Exception {
152: try {
153: slider.setOrientation(20);
154: fail("IllegalArgumentException shall be thrown");
155: } catch (IllegalArgumentException e) {
156: }
157: assertEquals(SwingConstants.HORIZONTAL, slider.getOrientation());
158: propertyChangeController = new PropertyChangeController();
159: slider.addPropertyChangeListener(propertyChangeController);
160: slider.setOrientation(SwingConstants.VERTICAL);
161: assertTrue(propertyChangeController.isChanged("orientation"));
162: assertEquals(SwingConstants.VERTICAL, slider.getOrientation());
163: }
164:
165: public void testSetGetModel() throws Exception {
166: assertNotNull(slider.getModel());
167: propertyChangeController = new PropertyChangeController();
168: slider.addPropertyChangeListener(propertyChangeController);
169: slider.setModel(new DefaultBoundedRangeModel());
170: assertTrue(propertyChangeController.isChanged("model"));
171: }
172:
173: public void testSetPaintLabels() throws Exception {
174: assertFalse(slider.getPaintLabels());
175: propertyChangeController = new PropertyChangeController();
176: slider.addPropertyChangeListener(propertyChangeController);
177: slider.setPaintLabels(true);
178: assertEquals(0, slider.getMajorTickSpacing());
179: assertTrue(propertyChangeController.isChanged("paintLabels"));
180: assertTrue(slider.getPaintLabels());
181: propertyChangeController.reset();
182: slider.setPaintLabels(true);
183: assertFalse(propertyChangeController.isChanged("paintLabels"));
184: }
185:
186: public void testCreateStandardLabels() throws Exception {
187: try {
188: slider.createStandardLabels(-1, 0);
189: fail("IllegalArgumentException shall be thrown");
190: } catch (IllegalArgumentException e) {
191: }
192: try {
193: slider.createStandardLabels(1, -1);
194: fail("IllegalArgumentException shall be thrown");
195: } catch (IllegalArgumentException e) {
196: }
197: try {
198: slider.createStandardLabels(1, 101);
199: fail("IllegalArgumentException shall be thrown");
200: } catch (IllegalArgumentException e) {
201: }
202: Hashtable<?, ?> t = slider.createStandardLabels(2, 0);
203: assertNotNull(t);
204: Enumeration<?> enumeration = t.keys();
205: while (enumeration.hasMoreElements()) {
206: Integer key = (Integer) enumeration.nextElement();
207: String text = ((JLabel) t.get(key)).getText();
208: assertEquals(key.toString(), text);
209: }
210: }
211:
212: public void testGetUI() throws Exception {
213: assertNotNull(slider.getUI());
214: assertTrue(slider.getUI() == slider.getUI());
215: }
216: }
|