]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Grid not updating selected item immediately (#10569)
authorTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Thu, 25 Jan 2018 12:36:51 +0000 (14:36 +0200)
committerTeemu Suo-Anttila <tsuoanttila@users.noreply.github.com>
Wed, 31 Jan 2018 12:27:32 +0000 (14:27 +0200)
server/src/main/java/com/vaadin/data/provider/DataCommunicator.java
uitest/src/main/java/com/vaadin/tests/data/DataProviderRefresh.java
uitest/src/test/java/com/vaadin/tests/data/DataProviderRefreshTest.java

index 2819543521071b1a5b4157dc842eff176929b1b2..43dcab027130b67eea47a512de00162a6512297c 100644 (file)
@@ -91,8 +91,7 @@ public class DataCommunicator<T> extends AbstractExtension {
      * that are not in the given collection will be cleaned up and
      * {@link DataGenerator#destroyData(Object)} will be called for them.
      */
-    protected class ActiveDataHandler
-            implements DataGenerator<T> {
+    protected class ActiveDataHandler implements DataGenerator<T> {
 
         /**
          * Set of key strings for currently active data objects
@@ -754,16 +753,14 @@ public class DataCommunicator<T> extends AbstractExtension {
     private void attachDataProviderListener() {
         dataProviderUpdateRegistration = getDataProvider()
                 .addDataProviderListener(event -> {
-                    getUI().access(() -> {
-                        if (event instanceof DataRefreshEvent) {
-                            T item = ((DataRefreshEvent<T>) event).getItem();
-                            getKeyMapper().refresh(item);
-                            generators.forEach(g -> g.refreshData(item));
-                            refresh(item);
-                        } else {
-                            hardReset();
-                        }
-                    });
+                    if (event instanceof DataRefreshEvent) {
+                        T item = ((DataRefreshEvent<T>) event).getItem();
+                        getKeyMapper().refresh(item);
+                        generators.forEach(g -> g.refreshData(item));
+                        getUI().access(() -> refresh(item));
+                    } else {
+                        getUI().access(this::hardReset);
+                    }
                 });
     }
 
index 9f1591407c8e93f31d55b0ab2c8c1547dcbc8f6b..faec5084f744f12b37ce2f7ffc74b743cd09212c 100644 (file)
@@ -3,16 +3,17 @@ package com.vaadin.tests.data;
 import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 import java.util.stream.Stream;
 
 import com.vaadin.data.provider.AbstractDataProvider;
 import com.vaadin.data.provider.Query;
 import com.vaadin.server.VaadinRequest;
-import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.tests.components.AbstractTestUIWithLog;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Grid;
 
-public class DataProviderRefresh extends AbstractTestUI {
+public class DataProviderRefresh extends AbstractTestUIWithLog {
 
     public static class Bean implements Serializable {
 
@@ -96,9 +97,10 @@ public class DataProviderRefresh extends AbstractTestUI {
         }
     }
 
+    private Grid<Bean> grid = new Grid<>();
+
     @Override
     protected void setup(VaadinRequest request) {
-        Grid<Bean> grid = new Grid<>();
         List<Bean> arrayList = new ArrayList<>();
         Bean foo = new Bean("Foo", 10);
         arrayList.add(foo);
@@ -108,9 +110,19 @@ public class DataProviderRefresh extends AbstractTestUI {
         grid.setDataProvider(dataProvider);
         grid.addColumn(Object::toString).setCaption("toString");
         addComponent(grid);
-        addComponent(new Button("Replace item",
-                event -> dataProvider.refreshItem(new Bean("Bar", 10))));
+        addComponent(new Button("Replace item", event -> {
+            dataProvider.refreshItem(new Bean("Bar", 10));
+            logSelectedItem(grid.asSingleSelect().getValue());
+        }));
         addComponent(new Button("Select old", event -> grid.select(foo)));
+        grid.asSingleSelect().addValueChangeListener(e -> {
+            logSelectedItem(e.getValue());
+        });
     }
 
+    private void logSelectedItem(Bean bean) {
+        Optional.ofNullable(bean)
+                .map(b -> "Currently selected: " + b.toString())
+                .ifPresent(this::log);
+    }
 }
index bbb31535a8b3bfe6cfb06c6a0d63282545ad7c39..03e566a7414dcbd715dcd6148cf1569b34f61441 100644 (file)
@@ -2,8 +2,11 @@ package com.vaadin.tests.data;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
+import static org.hamcrest.CoreMatchers.*;
+
 import org.junit.Test;
 
 import com.vaadin.testbench.elements.ButtonElement;
@@ -21,12 +24,14 @@ public class DataProviderRefreshTest extends SingleBrowserTest {
         // Select item before replace
         $(ButtonElement.class).caption("Select old").first().click();
         assertTrue("Row should be selected", grid.getRow(0).isSelected());
+        assertThat(getLogRow(0), containsString("{ Foo, 10 }"));
 
         $(ButtonElement.class).caption("Replace item").first().click();
         assertTrue("Row should still be selected after item replace",
                 grid.getRow(0).isSelected());
         assertEquals("Grid content was not updated.", "{ Bar, 10 }",
                 grid.getCell(0, 0).getText());
+        assertThat(getLogRow(0), containsString("{ Bar, 10 }"));
 
         // Deselect row
         grid.getCell(0, 0).click();