From 5cba827316eed64508ffb254edb95f303f44eab9 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 13 Mar 2013 11:14:24 +0200 Subject: Fixed client-server value sync problem in CheckBox (#11028) Change-Id: I1bac8e8a746bcc97b3ce929e76cf3476ad793bf6 --- server/src/com/vaadin/ui/CheckBox.java | 17 +++++ .../tests/components/AbstractTestUIWithLog.java | 36 ++++++++++ .../checkbox/CheckBoxRevertValueChange.html | 82 ++++++++++++++++++++++ .../checkbox/CheckBoxRevertValueChange.java | 65 +++++++++++++++++ 4 files changed, 200 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java create mode 100644 uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.html create mode 100644 uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.java diff --git a/server/src/com/vaadin/ui/CheckBox.java b/server/src/com/vaadin/ui/CheckBox.java index 0ace0a4f26..ac33f5e410 100644 --- a/server/src/com/vaadin/ui/CheckBox.java +++ b/server/src/com/vaadin/ui/CheckBox.java @@ -16,6 +16,8 @@ package com.vaadin.ui; +import org.json.JSONException; + import com.vaadin.data.Property; import com.vaadin.event.FieldEvents.BlurEvent; import com.vaadin.event.FieldEvents.BlurListener; @@ -37,6 +39,21 @@ public class CheckBox extends AbstractField { return; } + /* + * Client side updates the state before sending the event so we need + * to make sure the cached state is updated to match the client. If + * we do not do this, a reverting setValue() call in a listener will + * not cause the new state to be sent to the client. + * + * See #11028, #10030. + */ + try { + getUI().getConnectorTracker().getDiffState(CheckBox.this) + .put("checked", checked); + } catch (JSONException e) { + throw new RuntimeException(e); + } + final Boolean oldValue = getValue(); final Boolean newValue = checked; diff --git a/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java b/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java new file mode 100644 index 0000000000..cace7c3404 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/AbstractTestUIWithLog.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.util.Log; +import com.vaadin.ui.VerticalLayout; + +public abstract class AbstractTestUIWithLog extends AbstractTestUI { + + protected Log log = new Log(5); + + @Override + public void init(VaadinRequest request) { + super.init(request); + ((VerticalLayout) getContent()).addComponent(log, 0); + } + + protected void log(String message) { + log.log(message); + } + +} diff --git a/uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.html b/uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.html new file mode 100644 index 0000000000..ea5849f99f --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.html @@ -0,0 +1,82 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.checkbox.CheckBoxRevertValueChange?restartApplication
mouseClickvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[0]/VCheckBox[0]/domChild[0]10,5
assertTextvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::PID_SLog_row_01. I said no checking!
assertValuevaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[0]/VCheckBox[0]/domChild[0]off
mouseClickvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[0]/VCheckBox[0]/domChild[1]93,10
assertTextvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::PID_SLog_row_02. I said no checking!
assertValuevaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[0]/VCheckBox[0]/domChild[0]off
mouseClickvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[1]/VCheckBox[0]/domChild[0]5,8
assertTextvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::PID_SLog_row_03. I said no unchecking!
assertValuevaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[1]/VCheckBox[0]/domChild[0]on
mouseClickvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[1]/VCheckBox[0]/domChild[1]78,12
assertTextvaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::PID_SLog_row_04. I said no unchecking!
assertValuevaadin=runcomvaadintestscomponentscheckboxCheckBoxRevertValueChange::/VVerticalLayout[0]/Slot[2]/VVerticalLayout[0]/Slot[1]/VCheckBox[0]/domChild[0]on
+ + diff --git a/uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.java b/uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.java new file mode 100644 index 0000000000..5b086fb935 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/checkbox/CheckBoxRevertValueChange.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.checkbox; + +import com.vaadin.annotations.PreserveOnRefresh; +import com.vaadin.data.Property; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.CheckBox; + +@PreserveOnRefresh +public class CheckBoxRevertValueChange extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + final CheckBox alwaysUnchecked = new CheckBox("You may not check me"); + alwaysUnchecked + .addValueChangeListener(new Property.ValueChangeListener() { + public void valueChange(Property.ValueChangeEvent event) { + if (alwaysUnchecked.getValue()) { + log("I said no checking!"); + alwaysUnchecked.setValue(false); + } + } + }); + final CheckBox alwaysChecked = new CheckBox("You may not uncheck me"); + alwaysChecked.setValue(true); + alwaysChecked + .addValueChangeListener(new Property.ValueChangeListener() { + public void valueChange(Property.ValueChangeEvent event) { + if (!alwaysChecked.getValue()) { + log("I said no unchecking!"); + alwaysChecked.setValue(true); + } + } + }); + + addComponent(alwaysUnchecked); + addComponent(alwaysChecked); + } + + @Override + protected String getTestDescription() { + return "Ensure checking of a checkbox can be reverted on the server side without making the client go out of sync"; + } + + @Override + protected Integer getTicketNumber() { + return 11028; + } + +} -- cgit v1.2.3