aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/src/com/vaadin/ui/Slider.java18
-rw-r--r--server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java64
2 files changed, 69 insertions, 13 deletions
diff --git a/server/src/com/vaadin/ui/Slider.java b/server/src/com/vaadin/ui/Slider.java
index 66ed1a48f4..dad4d295bf 100644
--- a/server/src/com/vaadin/ui/Slider.java
+++ b/server/src/com/vaadin/ui/Slider.java
@@ -161,6 +161,11 @@ public class Slider extends AbstractField<Double> {
*/
public void setMax(double max) {
getState().maxValue = max;
+
+ if (getMin() > max) {
+ getState().minValue = max;
+ }
+
if (getValue() > max) {
setValue(max);
}
@@ -179,11 +184,16 @@ public class Slider extends AbstractField<Double> {
* Set the minimum slider value. If the current value of the slider is
* smaller than this, the value is set to the new minimum.
*
- * @param max
+ * @param min
* The new minimum slider value
*/
public void setMin(double min) {
getState().minValue = min;
+
+ if (getMax() < min) {
+ getState().maxValue = min;
+ }
+
if (getValue() < min) {
setValue(min);
}
@@ -260,12 +270,12 @@ public class Slider extends AbstractField<Double> {
newValue = (int) (v * Math.pow(10, resolution));
newValue = newValue / Math.pow(10, resolution);
if (getMin() > newValue || getMax() < newValue) {
- throw new ValueOutOfBoundsException(value);
+ throw new ValueOutOfBoundsException(newValue);
}
} else {
newValue = (int) v;
if (getMin() > newValue || getMax() < newValue) {
- throw new ValueOutOfBoundsException(value);
+ throw new ValueOutOfBoundsException(newValue);
}
}
@@ -313,6 +323,8 @@ public class Slider extends AbstractField<Double> {
* @param valueOutOfBounds
*/
public ValueOutOfBoundsException(Double valueOutOfBounds) {
+ super(String.format("Value %s is out of bounds: [%s, %s]",
+ valueOutOfBounds, getMin(), getMax()));
value = valueOutOfBounds;
}
diff --git a/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java b/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java
index f568da1392..48bba8a853 100644
--- a/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java
+++ b/server/tests/src/com/vaadin/tests/server/component/slider/SliderTest.java
@@ -1,24 +1,68 @@
package com.vaadin.tests.server.component.slider;
-import junit.framework.TestCase;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
import org.junit.Assert;
+import org.junit.Test;
import com.vaadin.ui.Slider;
-import com.vaadin.ui.Slider.ValueOutOfBoundsException;
-public class SliderTest extends TestCase {
+public class SliderTest {
- public void testOutOfBounds() {
+ @Test
+ public void minCannotBeLargerThanMax() {
+ Slider slider = new Slider();
+
+ slider.setMax(100);
+ slider.setMin(101);
+
+ assertThat(slider.getMin(), is(101.0));
+ assertThat(slider.getMax(), is(101.0));
+ }
+
+ @Test
+ public void maxCannotBeSmallerThanMin() {
+ Slider slider = new Slider();
+
+ slider.setMin(50);
+ slider.setMax(10);
+
+ assertThat(slider.getMax(), is(10.0));
+ assertThat(slider.getMin(), is(10.0));
+ }
+
+ @Test
+ public void valueOutOfBoundsExceptionMessageContainsBounds() {
+ Slider slider = new Slider();
+
+ try {
+
+ slider.setValue(-1.0);
+ } catch (Slider.ValueOutOfBoundsException e) {
+ assertThat(e.getMessage(),
+ containsString("Value -1.0 is out of bounds: [0.0, 100.0]"));
+ }
+ }
+
+ @Test
+ public void valueIsSet() {
+ Slider slider = new Slider();
+
+ slider.setValue(5.0);
+
+ assertThat(slider.getValue(), is(5.0));
+ }
+
+ @Test
+ public void valueCannotBeOutOfBounds() {
Slider s = new Slider(0, 10);
- s.setValue(0.0);
- Assert.assertEquals(0.0, s.getValue().doubleValue(), 0.001);
- s.setValue(10.0);
- Assert.assertEquals(10.0, s.getValue().doubleValue(), 0.001);
+
try {
s.setValue(20.0);
- fail("Should throw out of bounds exception");
- } catch (ValueOutOfBoundsException e) {
+ Assert.fail("Should throw out of bounds exception");
+ } catch (Slider.ValueOutOfBoundsException e) {
// TODO: handle exception
}