diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2016-08-31 16:03:20 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-09-01 08:50:59 +0000 |
commit | 6d0204cafe8c95acd72728b3d1be616d87a31db1 (patch) | |
tree | ffd603f512c4f516ae02fc124f78f5575ddb4075 /uitest | |
parent | 84fbff9e6645d0133c7b9f214389e215c79c818f (diff) | |
download | vaadin-framework-6d0204cafe8c95acd72728b3d1be616d87a31db1.tar.gz vaadin-framework-6d0204cafe8c95acd72728b3d1be616d87a31db1.zip |
Add simple data change handling for Selects
Change-Id: I16f9577ea4091fb4febe167d76e141b5945f53ab
Diffstat (limited to 'uitest')
5 files changed, 222 insertions, 2 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/data/DummyData.java b/uitest/src/main/java/com/vaadin/tests/data/DummyData.java new file mode 100644 index 0000000000..60b3af7681 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/data/DummyData.java @@ -0,0 +1,117 @@ +package com.vaadin.tests.data; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Stream; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.data.DataCommunicator; +import com.vaadin.server.data.ListDataSource; +import com.vaadin.server.data.Query; +import com.vaadin.shared.data.DataCommunicatorConstants; +import com.vaadin.shared.data.selection.SelectionModel; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.tests.widgetset.TestingWidgetSet; +import com.vaadin.ui.AbstractListing; +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; + +@Widgetset(TestingWidgetSet.NAME) +public class DummyData extends AbstractTestUIWithLog { + + /** + * DataSource that keeps track on how often the data is requested. + */ + private class LoggingDataSource extends ListDataSource<String> { + private int count = 0; + + private LoggingDataSource(Collection<String> collection) { + super(collection); + } + + @Override + public Stream<String> apply(Query query) { + log("Backend request #" + (count++)); + return super.apply(query); + } + } + + /** + * Simplified server only selection model. Selection state passed in data, + * shown as bold text. + */ + private static class DummySelectionModel implements SelectionModel<String> { + private String selected; + private DataCommunicator<String> communicator; + + @Override + public Set<String> getSelectedItems() { + if (selected != null) { + return Collections.singleton(selected); + } + return Collections.emptySet(); + } + + @Override + public void select(String item) { + if (selected != null) { + communicator.refresh(selected); + } + selected = item; + if (selected != null) { + communicator.refresh(selected); + } + } + + @Override + public void deselect(String item) { + if (item == selected) { + select(null); + } + } + + private void setCommunicator(DataCommunicator<String> dataComm) { + communicator = dataComm; + } + } + + public static class DummyComponent + extends AbstractListing<String, DummySelectionModel> { + + private DummyComponent() { + setSelectionModel(new DummySelectionModel()); + addDataGenerator((str, json) -> { + json.put(DataCommunicatorConstants.DATA, str); + if (isSelected(str)) { + json.put(DataCommunicatorConstants.SELECTED, true); + } + }); + getSelectionModel().setCommunicator(getDataCommunicator()); + } + } + + @Override + protected void setup(VaadinRequest request) { + DummyComponent dummy = new DummyComponent(); + List<String> items = new ArrayList<>(); + for (int i = 0; i < 300; ++i) { + items.add("Foo " + i); + } + dummy.setDataSource(new LoggingDataSource(items)); + dummy.select("Foo 200"); + + HorizontalLayout controls = new HorizontalLayout(); + addComponent(controls); + controls.addComponent(new Button("Select Foo 20", e -> { + dummy.select("Foo " + 20); + })); + controls.addComponent(new Button("Reset data source", e -> { + dummy.setDataSource(new LoggingDataSource(items)); + })); + addComponent(dummy); + } +} diff --git a/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java b/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java new file mode 100644 index 0000000000..061b52abf5 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/widgetset/client/data/DummyComponentConnector.java @@ -0,0 +1,43 @@ +package com.vaadin.tests.widgetset.client.data; + +import com.google.gwt.user.client.ui.FlowPanel; +import com.vaadin.client.connectors.AbstractListingConnector; +import com.vaadin.client.data.DataSource; +import com.vaadin.client.ui.VLabel; +import com.vaadin.shared.data.DataCommunicatorConstants; +import com.vaadin.shared.ui.Connect; +import com.vaadin.tests.data.DummyData.DummyComponent; + +import elemental.json.JsonObject; + +@Connect(DummyComponent.class) +public class DummyComponentConnector extends AbstractListingConnector { + + @Override + public FlowPanel getWidget() { + return (FlowPanel) super.getWidget(); + } + + @Override + public void setDataSource(DataSource<JsonObject> dataSource) { + super.setDataSource(dataSource); + + dataSource.addDataChangeHandler(range -> { + assert range.getStart() == 0 && range.getEnd() == dataSource + .size() : "Widget only supports full updates."; + getWidget().clear(); + for (int i = range.getStart(); i < range.getEnd(); ++i) { + VLabel label = new VLabel(); + getWidget().add(label); + JsonObject row = dataSource.getRow(i); + String text = row.getString(DataCommunicatorConstants.DATA); + if (row.hasKey(DataCommunicatorConstants.SELECTED) + && row.getBoolean(DataCommunicatorConstants.SELECTED)) { + text = "<b>" + text + "</b>"; + label.addStyleName("selected"); + } + label.setHTML(text); + } + }); + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/client/GridDetailsClientTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/client/GridDetailsClientTest.java index ed15f13f34..1b00d14050 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/client/GridDetailsClientTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/client/GridDetailsClientTest.java @@ -30,7 +30,7 @@ import org.junit.Test; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebElement; -import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.Range; import com.vaadin.testbench.By; import com.vaadin.testbench.ElementQuery; import com.vaadin.testbench.TestBenchElement; diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/escalator/EscalatorSpacerTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/escalator/EscalatorSpacerTest.java index f389b4b3f3..93931d50ec 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/escalator/EscalatorSpacerTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basicfeatures/escalator/EscalatorSpacerTest.java @@ -33,7 +33,7 @@ import org.openqa.selenium.Keys; import org.openqa.selenium.WebElement; import com.vaadin.client.WidgetUtil; -import com.vaadin.shared.ui.grid.Range; +import com.vaadin.shared.Range; import com.vaadin.testbench.TestBenchElement; import com.vaadin.testbench.elements.NotificationElement; import com.vaadin.testbench.parallel.BrowserUtil; diff --git a/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java b/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java new file mode 100644 index 0000000000..48001ef27f --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java @@ -0,0 +1,60 @@ +package com.vaadin.tests.data; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.openqa.selenium.WebElement; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class DummyDataTest extends SingleBrowserTest { + + @Before + public void setUp() { + setDebug(true); + openTestURL(); + } + + @Test + public void testCorrectRowSelectedOnInit() { + List<WebElement> selected = findElements(By.className("selected")); + assertTrue("Only one should be selected", 1 == selected.size()); + assertEquals("Wrong item selected", "Foo 200", + selected.get(0).getText()); + } + + @Test + public void testServerSelectionUpdatesSelected() { + $(ButtonElement.class).first().click(); + List<WebElement> selected = findElements(By.className("selected")); + assertTrue("Only one should be selected", 1 == selected.size()); + assertEquals("Wrong item selected", "Foo 20", + selected.get(0).getText()); + } + + @Test + public void testDataUpdateDoesNotCauseBackEndRequest() { + assertEquals("Unexpected backend requests", "2. Backend request #1", + getLogRow(0)); + assertEquals("Unexpected backend requests", "1. Backend request #0", + getLogRow(1)); + // Select a row on the server-side, triggers an update + $(ButtonElement.class).first().click(); + assertEquals("No requests should have happened", + "2. Backend request #1", getLogRow(0)); + } + + @Test + public void testDataSourceChangeOnlyOneRequest() { + // Change to a new logging data source + $(ButtonElement.class).get(1).click(); + assertEquals("DataSource change should only cause 1 request", + "3. Backend request #0", getLogRow(0)); + } +} |