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: package javax.swing.plaf.basic;
018:
019: import java.awt.IllegalComponentStateException;
020: import java.awt.Point;
021: import java.util.Hashtable;
022:
023: import javax.swing.JButton;
024: import javax.swing.JLabel;
025: import javax.swing.JSlider;
026: import javax.swing.SwingConstants;
027: import javax.swing.SwingTestCase;
028: import javax.swing.UIManager;
029: import javax.swing.UnsupportedLookAndFeelException;
030:
031: public class BasicSliderUITest extends SwingTestCase {
032: private BasicSliderUI sliderUI;
033:
034: private JSlider slider;
035:
036: @Override
037: protected void setUp() throws Exception {
038: super .setUp();
039: try {
040: UIManager.setLookAndFeel(new BasicLookAndFeel() {
041: private static final long serialVersionUID = 1L;
042:
043: @Override
044: public boolean isNativeLookAndFeel() {
045: return true;
046: }
047:
048: @Override
049: public boolean isSupportedLookAndFeel() {
050: return true;
051: }
052:
053: @Override
054: public String getDescription() {
055: return "";
056: }
057:
058: @Override
059: public String getID() {
060: return "";
061: }
062:
063: @Override
064: public String getName() {
065: return "";
066: }
067: });
068: } catch (UnsupportedLookAndFeelException e1) {
069: e1.printStackTrace();
070: }
071: slider = new JSlider();
072: sliderUI = new BasicSliderUI(slider);
073: }
074:
075: @Override
076: protected void tearDown() throws Exception {
077: sliderUI = null;
078: slider = null;
079: super .tearDown();
080: }
081:
082: public void testCreateUI() throws Exception {
083: assertNotNull(BasicSliderUI.createUI(slider));
084: assertNotSame(BasicSliderUI.createUI(slider), BasicSliderUI
085: .createUI(slider));
086: }
087:
088: public void testCreateTrackListener() throws Exception {
089: assertNull(sliderUI.slider);
090: assertNotNull(sliderUI.createTrackListener(slider));
091: assertNull(sliderUI.slider);
092: assertNull(sliderUI.trackListener);
093: assertNotSame(sliderUI.createTrackListener(slider), sliderUI
094: .createTrackListener(new JSlider()));
095: }
096:
097: public void testCreateChangeListener() throws Exception {
098: assertNull(sliderUI.slider);
099: assertNotNull(sliderUI.createChangeListener(slider));
100: assertNull(sliderUI.slider);
101: assertNull(sliderUI.changeListener);
102: assertSame(sliderUI.createChangeListener(slider), sliderUI
103: .createChangeListener(new JSlider()));
104: }
105:
106: public void testCreateChangeListenerNotSame() throws Exception {
107: assertNotSame(sliderUI.createChangeListener(slider),
108: new BasicSliderUI(slider).createChangeListener(slider));
109: }
110:
111: public void testCreateComponentListener() throws Exception {
112: assertNull(sliderUI.slider);
113: assertNotNull(sliderUI.createComponentListener(slider));
114: assertNull(sliderUI.slider);
115: assertNull(sliderUI.componentListener);
116: assertSame(sliderUI.createComponentListener(slider), sliderUI
117: .createComponentListener(new JSlider()));
118: }
119:
120: public void testCreateComponentListenerNotSame() throws Exception {
121: assertNotSame(sliderUI.createComponentListener(slider),
122: new BasicSliderUI(slider)
123: .createComponentListener(slider));
124: }
125:
126: public void testCreateFocusListener() throws Exception {
127: assertNull(sliderUI.slider);
128: assertNotNull(sliderUI.createFocusListener(slider));
129: assertNull(sliderUI.slider);
130: assertNull(sliderUI.focusListener);
131: assertSame(sliderUI.createFocusListener(slider), sliderUI
132: .createFocusListener(new JSlider()));
133: }
134:
135: public void testCreateFocusListenerNotSame() throws Exception {
136: assertNotSame(sliderUI.createFocusListener(slider),
137: new BasicSliderUI(slider).createFocusListener(slider));
138: }
139:
140: public void testCreateScrollListener() throws Exception {
141: assertNull(sliderUI.slider);
142: assertNotNull(sliderUI.createScrollListener(slider));
143: assertNull(sliderUI.slider);
144: assertNull(sliderUI.scrollListener);
145: assertNotSame(sliderUI.createScrollListener(slider), sliderUI
146: .createScrollListener(slider));
147: }
148:
149: // Regression for HARMONY-2878
150: public void testCreateScrollListenerNull() throws Exception {
151: assertNotNull(sliderUI.createScrollListener(null)); // no exception expected
152: }
153:
154: public void testCreatePropertyChangeListener() throws Exception {
155: assertNull(sliderUI.slider);
156: assertNotNull(sliderUI.createPropertyChangeListener(slider));
157: assertNull(sliderUI.slider);
158: assertNull(sliderUI.propertyChangeListener);
159: assertSame(sliderUI.createPropertyChangeListener(slider),
160: sliderUI.createPropertyChangeListener(new JSlider()));
161: }
162:
163: public void testCreatePropertyChangeListenerNotSame()
164: throws Exception {
165: assertNotSame(sliderUI.createPropertyChangeListener(slider),
166: new BasicSliderUI(slider)
167: .createPropertyChangeListener(slider));
168: }
169:
170: public void testGetShadowColor() throws Exception {
171: assertNull(sliderUI.getShadowColor());
172: sliderUI.installUI(slider);
173: assertEquals(UIManager.getColor("Slider.shadow"), sliderUI
174: .getShadowColor());
175: }
176:
177: public void testGetHighlightColor() throws Exception {
178: assertNull(sliderUI.getHighlightColor());
179: sliderUI.installUI(slider);
180: assertEquals(UIManager.getColor("Slider.highlight"), sliderUI
181: .getHighlightColor());
182: }
183:
184: public void testGetFocusColor() throws Exception {
185: assertNull(sliderUI.getFocusColor());
186: sliderUI.installUI(slider);
187: assertEquals(UIManager.getColor("Slider.focus"), sliderUI
188: .getFocusColor());
189: }
190:
191: public void testGetLowestValueLabel() throws Exception {
192: sliderUI.installUI(slider);
193: slider.setLabelTable(slider.createStandardLabels(1));
194: assertEquals("0", ((JLabel) sliderUI.getLowestValueLabel())
195: .getText());
196: slider.setLabelTable(slider.createStandardLabels(2, 57));
197: assertEquals("57", ((JLabel) sliderUI.getLowestValueLabel())
198: .getText());
199: }
200:
201: public void testGetHighestValueLabel() throws Exception {
202: sliderUI.installUI(slider);
203: slider.setLabelTable(slider.createStandardLabels(1));
204: assertEquals("100", ((JLabel) sliderUI.getHighestValueLabel())
205: .getText());
206: slider.setLabelTable(slider.createStandardLabels(2, 56));
207: assertEquals("100", ((JLabel) sliderUI.getHighestValueLabel())
208: .getText());
209: }
210:
211: public void testGetWidthOfHighValueLabel() throws Exception {
212: sliderUI.installUI(slider);
213: Hashtable<Integer, JLabel> t = new Hashtable<Integer, JLabel>();
214: t.put(new Integer("1"), new JLabel("1"));
215: t.put(new Integer("100"), new JLabel("100"));
216: JLabel label = new JLabel("1000000");
217: t.put(new Integer("1000000"), label);
218: slider.setLabelTable(t);
219: assertEquals(label.getWidth(), sliderUI
220: .getWidthOfHighValueLabel());
221: }
222:
223: public void testGetWidthOfLowValueLabel() throws Exception {
224: sliderUI.installUI(slider);
225: Hashtable<Integer, JLabel> t = new Hashtable<Integer, JLabel>();
226: JLabel label = new JLabel("1");
227: t.put(new Integer("1"), label);
228: t.put(new Integer("100"), new JLabel("100"));
229: t.put(new Integer("1000000"), new JLabel("1000000"));
230: slider.setLabelTable(t);
231: assertEquals(label.getWidth(), sliderUI
232: .getWidthOfLowValueLabel());
233: }
234:
235: public void testGetHightOfHighValueLabel() throws Exception {
236: sliderUI.installUI(slider);
237: Hashtable<Integer, JLabel> t = new Hashtable<Integer, JLabel>();
238: t.put(new Integer("1"), new JLabel("1"));
239: t.put(new Integer("100"), new JLabel("100"));
240: JLabel label = new JLabel("1000000");
241: t.put(new Integer("1000000"), label);
242: slider.setLabelTable(t);
243: assertEquals(label.getHeight(), sliderUI
244: .getHeightOfHighValueLabel());
245: }
246:
247: public void testGetHeightOfLowValueLabel() throws Exception {
248: sliderUI.installUI(slider);
249: Hashtable<Integer, JLabel> t = new Hashtable<Integer, JLabel>();
250: JLabel label = new JLabel("1");
251: t.put(new Integer("1"), label);
252: t.put(new Integer("100"), new JLabel("100"));
253: t.put(new Integer("1000000"), new JLabel("1000000"));
254: slider.setLabelTable(t);
255: assertEquals(label.getHeight(), sliderUI
256: .getHeightOfLowValueLabel());
257: }
258:
259: public void testGetWidthOfWidestLabel() throws Exception {
260: sliderUI.installUI(slider);
261: Hashtable<Integer, JLabel> t = new Hashtable<Integer, JLabel>();
262: t.put(new Integer("1"), new JLabel("1"));
263: JLabel label = new JLabel("___________100");
264: t.put(new Integer("100"), label);
265: t.put(new Integer("1000000"), new JLabel("1000000"));
266: slider.setLabelTable(t);
267: assertEquals(label.getWidth(), sliderUI.getWidthOfWidestLabel());
268: }
269:
270: public void testGetHeightOfTallestLabel() throws Exception {
271: sliderUI.installUI(slider);
272: Hashtable<Integer, JLabel> t = new Hashtable<Integer, JLabel>();
273: JLabel label = new JLabel("1");
274: label.setFont(label.getFont().deriveFont(50f));
275: t.put(new Integer("1"), label);
276: t.put(new Integer("100"), new JLabel("100"));
277: t.put(new Integer("1000000"), new JLabel("1000000"));
278: slider.setLabelTable(t);
279: assertEquals(label.getHeight(), sliderUI
280: .getHeightOfTallestLabel());
281: }
282:
283: public void testSetThumbLocation() throws Exception {
284: sliderUI.installUI(slider);
285: sliderUI.setThumbLocation(200, 500);
286: assertEquals(new Point(200, 500), sliderUI.thumbRect
287: .getLocation());
288: sliderUI.setThumbLocation(200, -500);
289: assertEquals(new Point(200, -500), sliderUI.thumbRect
290: .getLocation());
291: sliderUI.setThumbLocation(-200, 500);
292: assertEquals(new Point(-200, 500), sliderUI.thumbRect
293: .getLocation());
294: }
295:
296: // Regression test for HARMONY-2855
297: public void testBasicSliderUI() throws Exception {
298: assertNull(sliderUI.slider);
299: }
300:
301: /**
302: * <code>uninstallUI</code> is called with the same instance of
303: * <code>JSlider</code> to which this UI was installed.
304: */
305: // Regression test for HARMONY-2855
306: public void testUninstallUI01() {
307: sliderUI.installUI(slider);
308: sliderUI.uninstallUI(slider);
309: // No exception is expected
310: }
311:
312: /**
313: * <code>uninstallUI</code> is called before <code>installUI</code>
314: * was called.
315: */
316: // Regression test for HARMONY-2855
317: public void testUninstallUI02() {
318: try {
319: sliderUI.uninstallUI(slider);
320: fail("IllegalComponentStateException is expected");
321: } catch (IllegalComponentStateException e) {
322: // expected
323: }
324: }
325:
326: /**
327: * <code>uninstallUI</code> is called with another instance of
328: * <code>JSlider</code>.
329: */
330: // Regression test for HARMONY-2855
331: public void testUninstallUI03() {
332: try {
333: sliderUI.uninstallUI(new JSlider());
334: fail("IllegalComponentStateException is expected");
335: } catch (IllegalComponentStateException e) {
336: // expected
337: }
338: }
339:
340: /**
341: * <code>uninstallUI</code> is called with instance of another class, i.e.
342: * not <code>JSlider</code> instance.
343: */
344: // Regression test for HARMONY-2855
345: public void testUninstallUI04() {
346: try {
347: sliderUI.uninstallUI(new JButton());
348: fail("IllegalComponentStateException is expected");
349: } catch (IllegalComponentStateException e) {
350: // expected
351: }
352: }
353:
354: /**
355: * Regression test for HARMONY-2591
356: * */
357: public void testActionScrollerEnabled() {
358: BasicSliderUI.ActionScroller m = sliderUI.new ActionScroller(
359: new JSlider(), 3, true);
360: assertTrue(m.isEnabled());
361: }
362:
363: /**
364: * Regression test for HARMONY-4445
365: */
366: public void testMinMaxValue() {
367:
368: slider.setMaximum(Integer.MAX_VALUE);
369: slider.setMinimum(0);
370: slider.setBounds(0, 0, 100, 100);
371:
372: int half = Integer.MAX_VALUE / 2;
373:
374: // UI slightly modified to omit unneeded actions - no functional changes
375: // according to spec
376: BasicSliderUI tested = new BasicSliderUI(slider) {
377: @Override
378: protected void installKeyboardActions(JSlider unneded) {
379: // Empty. In real BasicSliderUI this method installs Keyboard
380: // actions
381: }
382:
383: @Override
384: protected void installDefaults(JSlider unneded) {
385: // Empty. In real BasicSliderUI this method installs defaults
386: // (colors and fonts)
387: }
388:
389: @Override
390: protected void installListeners(JSlider unneded) {
391: // Empty. In real BasicSliderUI this method installs listeners
392: }
393: };
394:
395: tested.installUI(slider);
396: assertEquals(tested.xPositionForValue(half),
397: getCenterHorisontalPosition(tested));
398:
399: slider.setOrientation(SwingConstants.VERTICAL);
400: tested.installUI(slider);
401:
402: assertEquals(tested.yPositionForValue(half),
403: getCenterVerticalPosition(tested));
404:
405: }
406:
407: private int getCenterVerticalPosition(BasicSliderUI ui) {
408: return ui.trackRect.y + (ui.trackRect.height / 2);
409: }
410:
411: private int getCenterHorisontalPosition(BasicSliderUI ui) {
412: return ui.trackRect.x + (ui.trackRect.width / 2);
413: }
414:
415: }
|