diff options
author | Leif Åstrand <legioth@gmail.com> | 2017-03-01 09:16:25 +0200 |
---|---|---|
committer | Pekka Hyvönen <pekka@vaadin.com> | 2017-03-01 09:16:25 +0200 |
commit | c21c5526b147aad1d77c2a9bc87828fb26798ea4 (patch) | |
tree | 47ca52c8baaaa469d4feea811c75a2cb03b0f158 /uitest | |
parent | abc26cb38d790e9fc0b97720164f855ed7b4dc7b (diff) | |
download | vaadin-framework-c21c5526b147aad1d77c2a9bc87828fb26798ea4.tar.gz vaadin-framework-c21c5526b147aad1d77c2a9bc87828fb26798ea4.zip |
Fix js state partial updates (#8695)
* Fix partial state change handling of JavaScript components
In ace0e324b69753431dcde9949eaa9b0e3e648db9 (Use diffstate for JS connectors)
partial state change handling for JavaScript components was introduced, but the
setNativeState method in JavaScriptConnectorHelper was not adjusted.
By removing the cleanup code from the updateNativeState method it can
be ensured that the non-changed properties are still present on the state object.
* Add test for preserving untouched JS state fields
Diffstat (limited to 'uitest')
3 files changed, 135 insertions, 0 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTracking.java b/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTracking.java new file mode 100644 index 0000000000..0453a67b82 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTracking.java @@ -0,0 +1,66 @@ +/* + * Copyright 2000-2016 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.javascriptcomponent; + +import com.vaadin.annotations.JavaScript; +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.Constants; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.JavaScriptComponentState; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.AbstractJavaScriptComponent; +import com.vaadin.ui.Button; + +@JavaScript("JavaScriptStateTracking.js") +@Widgetset(Constants.DEFAULT_WIDGETSET) +public class JavaScriptStateTracking extends AbstractTestUI { + + public static class StateTrackingComponentState + extends JavaScriptComponentState { + public int counter = 0; + public String field1 = "initial value"; + public String field2; + } + + public static class StateTrackingComponent + extends AbstractJavaScriptComponent { + @Override + protected StateTrackingComponentState getState() { + return (StateTrackingComponentState) super.getState(); + } + } + + @Override + protected void setup(VaadinRequest request) { + StateTrackingComponent stateTrackingComponent = new StateTrackingComponent(); + + Button setField2 = new Button("Set field2", e -> { + stateTrackingComponent.getState().counter++; + stateTrackingComponent.getState().field2 = "updated value " + + stateTrackingComponent.getState().counter; + }); + setField2.setId("setField2"); + + Button clearField1 = new Button("Clear field1", e -> { + stateTrackingComponent.getState().counter++; + stateTrackingComponent.getState().field1 = null; + }); + clearField1.setId("clearField1"); + + addComponents(stateTrackingComponent, setField2, clearField1); + } + +} diff --git a/uitest/src/main/resources/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTracking.js b/uitest/src/main/resources/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTracking.js new file mode 100644 index 0000000000..4c9fca5f0b --- /dev/null +++ b/uitest/src/main/resources/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTracking.js @@ -0,0 +1,13 @@ +window.com_vaadin_tests_components_javascriptcomponent_JavaScriptStateTracking_StateTrackingComponent = function() { + var self = this; + + this.showState = function(state) { + this.getElement().innerHTML = 'counter: <span id="counter">' + state.counter + '</span><br>' + + 'field1: <span id="field1">' + state.field1 + '</span><br>' + + 'field2: <span id="field2">' + state.field2 + '</span>'; + } + + this.onStateChange = function() { + this.showState(this.getState()); + } +}
\ No newline at end of file diff --git a/uitest/src/test/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTrackingTest.java b/uitest/src/test/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTrackingTest.java new file mode 100644 index 0000000000..760ec04af6 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptStateTrackingTest.java @@ -0,0 +1,56 @@ +/* + * Copyright 2000-2016 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.javascriptcomponent; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class JavaScriptStateTrackingTest extends SingleBrowserTest { + @Test + public void testStateTracking() { + openTestURL(); + + // field2 should really be null instead of undefined, but that's a + // separate issue + assertValues(0, "initial value", "undefined"); + + $(ButtonElement.class).id("setField2").click(); + + assertValues(1, "initial value", "updated value 1"); + + $(ButtonElement.class).id("clearField1").click(); + + assertValues(2, "null", "updated value 1"); + + $(ButtonElement.class).id("setField2").click(); + + assertValues(3, "null", "updated value 3"); + } + + private void assertValues(int expectedCounter, String expectedField1, + String expectedField2) { + Assert.assertEquals(String.valueOf(expectedCounter), + findElement(By.id("counter")).getText()); + Assert.assertEquals(String.valueOf(expectedField1), + findElement(By.id("field1")).getText()); + Assert.assertEquals(String.valueOf(expectedField2), + findElement(By.id("field2")).getText()); + } +} |