diff options
author | Artur <artur@vaadin.com> | 2017-01-30 23:34:06 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-01-30 23:34:06 +0200 |
commit | bc34610865f7ae71286d19a6a6a4b00bce3ec2ce (patch) | |
tree | 558c8cc473de8c7a990beeef0bf5a4ca5a58328a | |
parent | 2eb8004a1bea6909ce4c59ef2dd9ebba7bb86b96 (diff) | |
download | vaadin-framework-bc34610865f7ae71286d19a6a6a4b00bce3ec2ce.tar.gz vaadin-framework-bc34610865f7ae71286d19a6a6a4b00bce3ec2ce.zip |
Fix userOriginated state for CheckBox ValueChangeEvent from the user (#8383)
* Fix userOriginated state for CheckBox ValueChangeEvent from the user
-rw-r--r-- | server/src/main/java/com/vaadin/ui/CheckBox.java | 21 | ||||
-rw-r--r-- | server/src/test/java/com/vaadin/ui/CheckBoxTest.java | 42 |
2 files changed, 62 insertions, 1 deletions
diff --git a/server/src/main/java/com/vaadin/ui/CheckBox.java b/server/src/main/java/com/vaadin/ui/CheckBox.java index 8675332eb5..84f816741d 100644 --- a/server/src/main/java/com/vaadin/ui/CheckBox.java +++ b/server/src/main/java/com/vaadin/ui/CheckBox.java @@ -60,7 +60,7 @@ public class CheckBox extends AbstractField<Boolean> if (!newValue.equals(oldValue)) { // The event is only sent if the switch state is changed - setValue(newValue); + setValue(newValue, true); } }; @@ -118,6 +118,25 @@ public class CheckBox extends AbstractField<Boolean> super.setValue(value); } + /** + * Sets the value of this CheckBox. If the new value is not equal to + * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws + * {@code NullPointerException} if the value is null. + * + * @param value + * the new value, not {@code null} + * @param userOriginated + * {@code true} if this event originates from the client, + * {@code false} otherwise. + * @throws NullPointerException + * if {@code value} is {@code null} + */ + @Override + protected boolean setValue(Boolean value, boolean userOriginated) { + Objects.requireNonNull(value, "CheckBox value must not be null"); + return super.setValue(value, userOriginated); + } + @Override public Boolean getEmptyValue() { return false; diff --git a/server/src/test/java/com/vaadin/ui/CheckBoxTest.java b/server/src/test/java/com/vaadin/ui/CheckBoxTest.java index ee0ed943eb..eb96656389 100644 --- a/server/src/test/java/com/vaadin/ui/CheckBoxTest.java +++ b/server/src/test/java/com/vaadin/ui/CheckBoxTest.java @@ -15,9 +15,15 @@ */ package com.vaadin.ui; +import java.util.concurrent.atomic.AtomicBoolean; + import org.junit.Assert; import org.junit.Test; +import com.vaadin.shared.MouseEventDetails; +import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc; +import com.vaadin.tests.util.MockUI; + public class CheckBoxTest { @Test public void initiallyFalse() { @@ -34,6 +40,42 @@ public class CheckBoxTest { Assert.assertFalse(cb.getValue()); } + @Test + public void setValueChangeFromClientIsUserOriginated() { + UI ui = new MockUI(); + CheckBox cb = new CheckBox(); + ui.setContent(cb); + AtomicBoolean userOriginated = new AtomicBoolean(false); + cb.addValueChangeListener(e -> { + userOriginated.set(e.isUserOriginated()); + }); + ComponentTest.syncToClient(cb); + ComponentTest.getRpcProxy(cb, CheckBoxServerRpc.class).setChecked(true, + new MouseEventDetails()); + Assert.assertTrue(userOriginated.get()); + userOriginated.set(false); + ComponentTest.syncToClient(cb); + ComponentTest.getRpcProxy(cb, CheckBoxServerRpc.class).setChecked(false, + new MouseEventDetails()); + Assert.assertTrue(userOriginated.get()); + } + + @Test + public void setValueChangeFromServerIsNotUserOriginated() { + UI ui = new MockUI(); + CheckBox cb = new CheckBox(); + ui.setContent(cb); + AtomicBoolean userOriginated = new AtomicBoolean(true); + cb.addValueChangeListener(e -> { + userOriginated.set(e.isUserOriginated()); + }); + cb.setValue(true); + Assert.assertFalse(userOriginated.get()); + userOriginated.set(true); + cb.setValue(false); + Assert.assertFalse(userOriginated.get()); + } + @Test(expected = NullPointerException.class) public void setValue_nullValue_throwsNPE() { CheckBox cb = new CheckBox(); |