001: package org.uispec4j;
002:
003: import org.uispec4j.xml.XmlAssert;
004: import org.uispec4j.utils.Functor;
005: import org.uispec4j.assertion.Assertion;
006:
007: import javax.swing.JLabel;
008: import javax.swing.JSlider;
009: import java.util.Hashtable;
010:
011: public class SliderTest extends UIComponentTestCase {
012: private JSlider jComponent = createThermometer();
013: private Slider thermometer = new Slider(jComponent);
014:
015: public void testGetComponentTypeName() throws Exception {
016: assertEquals("slider", thermometer.getDescriptionTypeName());
017: }
018:
019: public void testGetDescription() throws Exception {
020: XmlAssert.assertEquivalent("<slider name='my thermometer'/>",
021: thermometer.getDescription());
022: }
023:
024: public void testFactory() throws Exception {
025: checkFactory(createThermometer(), Slider.class);
026: }
027:
028: protected UIComponent createComponent() {
029: return thermometer;
030: }
031:
032: public void testLabels() throws Exception {
033: checkAssertionFails(thermometer.labelsEqual(new String[] { "1",
034: "2", "3", "4" }), "4 elements instead of 6\n"
035: + "Expected: [1,2,3,4],\n"
036: + "but was: [-10,0,10,20,30,40]");
037:
038: assertTrue(thermometer.labelsEqual(new String[] { "-10", "0",
039: "10", "20", "30", "40" }));
040:
041: changeLabels();
042:
043: checkAssertionFails(thermometer.labelsEqual(new String[] { "1",
044: "2", "3", "4", "5" }),
045: "Unexpected element 'Very Cold'\n"
046: + "Expected: [1,2,3,4,5],\n"
047: + "but was: [Very Cold,Cold,Cool,Warm,Hot]");
048: checkAssertionFails(thermometer.labelsEqual(new String[] {
049: "Very Cold", "Cold", "Cool", "Hot", "Warm" }),
050: "Unexpected order in the collection\n"
051: + "Expected: [Very Cold,Cold,Cool,Hot,Warm],\n"
052: + "but was: [Very Cold,Cold,Cool,Warm,Hot]");
053: assertTrue(thermometer.labelsEqual(new String[] { "Very Cold",
054: "Cold", "Cool", "Warm", "Hot" }));
055:
056: }
057:
058: public void testPositionCheckBasedOnLabels() throws Exception {
059: changeLabels();
060:
061: jComponent.setValue(10);
062: checkPosition(10, "Cool", "Warm");
063:
064: thermometer.setPosition("Warm");
065: checkPosition(20, "Warm", "Cool");
066:
067: thermometer.setPosition("Cold");
068: checkPosition(0, "Cold", "Hot");
069:
070: checkException(new Functor() {
071: public void run() throws Exception {
072: thermometer.setPosition("unexisting");
073: }
074: }, "No label 'unexisting' has been found");
075: checkPosition(0, "Cold", "Hot");
076: }
077:
078: public void testRelativePosition() throws Exception {
079: jComponent.setValue(16);
080: Assertion positionIsMiddle = thermometer
081: .relativePositionEquals(50);
082: assertTrue(positionIsMiddle);
083:
084: jComponent.setValue(17);
085: checkAssertionFails(positionIsMiddle, "Expected 50 but was 54");
086:
087: thermometer.setPrecision(5);
088: assertTrue(positionIsMiddle);
089: }
090:
091: private void checkPosition(int intValue, String correctLabel,
092: String wrongLabel) throws Exception {
093: assertEquals(intValue, jComponent.getValue());
094: assertTrue(thermometer.positionEquals(correctLabel));
095: checkAssertionFails(thermometer.positionEquals(wrongLabel),
096: "expected:<" + wrongLabel + "> but was:<"
097: + correctLabel + ">");
098: }
099:
100: private static JSlider createThermometer() {
101: JSlider thermometer = new JSlider(JSlider.HORIZONTAL, -10, 40,
102: 0);
103: thermometer.setName("my thermometer");
104: thermometer.setMajorTickSpacing(10);
105: thermometer.setPaintLabels(true);
106: thermometer.setPaintTicks(true);
107: return thermometer;
108: }
109:
110: private void changeLabels() {
111: Hashtable table = new Hashtable();
112: table.put(new Integer(-10), new JLabel("Very Cold"));
113: table.put(new Integer(0), new JLabel("Cold"));
114: table.put(new Integer(10), new JLabel("Cool"));
115: table.put(new Integer(20), new JLabel("Warm"));
116: table.put(new Integer(40), new JLabel("Hot"));
117: jComponent.setLabelTable(table);
118: }
119: }
|