From: Sauli Tähkäpää Date: Wed, 25 Feb 2015 14:54:59 +0000 (+0200) Subject: Ensure that slider min is always smaller or equal to max. (#16776) X-Git-Tag: 7.5.0.alpha1~65 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f0a04197be3b536b23889bcce3178962b5e10733;p=vaadin-framework.git Ensure that slider min is always smaller or equal to max. (#16776) - Set slider min to max if max is being set to a smaller value than min, and vice-versa. - Set also an informative message to Slider.ValueOutOfBoundsException. Change-Id: I06b6b05dd0ef2c1fb862cf4acc7df3e235db59ba --- 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 { */ 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 { * 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 { 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 { * @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 }