Browse Source

Fix userOriginated state for CheckBox ValueChangeEvent from the user (#8383)

* Fix userOriginated state for CheckBox ValueChangeEvent from the user
tags/8.0.0.beta2
Artur 7 years ago
parent
commit
bc34610865

+ 20
- 1
server/src/main/java/com/vaadin/ui/CheckBox.java View File

@@ -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;

+ 42
- 0
server/src/test/java/com/vaadin/ui/CheckBoxTest.java View File

@@ -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();

Loading…
Cancel
Save