diff options
author | Johannes Tuikkala <johannes@vaadin.com> | 2017-04-20 12:42:52 +0300 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2017-04-20 11:42:52 +0200 |
commit | 779c260b674705a86235f76b5d38685b2062b193 (patch) | |
tree | 43aa4938866146054e615670a736122777ffd869 | |
parent | 2d9fb530cced8a4329a57293d308760b424af067 (diff) | |
download | vaadin-framework-779c260b674705a86235f76b5d38685b2062b193.tar.gz vaadin-framework-779c260b674705a86235f76b5d38685b2062b193.zip |
Fix removing rows over the cached range in Grid
Fixes #8840 for 7.7
3 files changed, 140 insertions, 1 deletions
diff --git a/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java b/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java index 4a9ced7308..049f1c6272 100644 --- a/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java +++ b/client/src/main/java/com/vaadin/client/data/AbstractRemoteDataSource.java @@ -560,7 +560,15 @@ public abstract class AbstractRemoteDataSource<T> implements DataSource<T> { Range remainsBefore = partitions[0]; Range transposedRemainsAfter = partitions[2] .offsetBy(-removedRange.length()); - cached = remainsBefore.combineWith(transposedRemainsAfter); + // #8840 either can be empty if the removed range was over the + // cached range + if (remainsBefore.isEmpty()) { + cached = transposedRemainsAfter; + } else if (transposedRemainsAfter.isEmpty()) { + cached = remainsBefore; + } else { + cached = remainsBefore.combineWith(transposedRemainsAfter); + } } else if (removedRange.getEnd() <= cached.getStart()) { // Removal was before the cache. offset the cache. cached = cached.offsetBy(-removedRange.length()); diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridRemoveCachedRows.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRemoveCachedRows.java new file mode 100644 index 0000000000..fcd33add30 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridRemoveCachedRows.java @@ -0,0 +1,92 @@ +/* + * Copyright 2000-2014 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.grid; + +import java.util.Arrays; +import java.util.List; + +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.grid.HeightMode; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; + +@SuppressWarnings("serial") +public class GridRemoveCachedRows extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + + List<String> columnIds = Arrays.asList("Hello", "this", "are", + "multiple", "columns", "plus", "these", "resemble", "a", + "group", "here", "no", "more"); + + final Grid grid = new Grid(new CustomIndexedContainer()); + grid.setHeightMode(HeightMode.ROW); + grid.setHeightByRows(20); + for (String columnId : columnIds) { + ((CustomIndexedContainer) grid.getContainerDataSource()) + .addContainerProperty(columnId, String.class, ""); + } + + // add a lot of rows to make sure that at least some of them are cached + for (int i = 0; i < 400; i++) { + grid.addRow(columnIds.toArray()); + } + + Button removeRowsButton = new Button("Remove a cached row"); + removeRowsButton.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent event) { + // remove a range of items starting from the visible items and + // ending into the cached items + ((CustomIndexedContainer) grid.getContainerDataSource()) + .removeItemRange(10, 300); + } + }); + + addComponents(grid, removeRowsButton); + } + + @Override + protected String getTestDescription() { + return "Test what happens when item range (starting from visible and ending to cached items) is removed from Grid."; + } + + @Override + protected Integer getTicketNumber() { + return 8840; + } + + /** + * Customised IndexedContainer for removing item range + */ + public class CustomIndexedContainer extends IndexedContainer { + public void removeItemRange(int startIndex, int count) { + Object firstItem = null; + + for (int index = startIndex; index < startIndex + count; index++) { + Object itemId = getIdByIndex(startIndex); + getAllItemIds().remove(itemId); + if (firstItem == null) { + firstItem = itemId; + } + } + fireItemsRemoved(startIndex, firstItem, count); + } + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/GridRemoveCachedRowsTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRemoveCachedRowsTest.java new file mode 100644 index 0000000000..3c0f5b1110 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/GridRemoveCachedRowsTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2014 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.grid; + +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +/** + * Verifies that there's no client side errors when removing a range of rows + * starting from the visible ones and ending into the cached ones. + */ +public class GridRemoveCachedRowsTest extends SingleBrowserTest { + + @Test + public void testNoClientExceptionWhenRemovingARangeOfRows() { + setDebug(true); + openTestURL(); + + // do remove a range of rows + $(ButtonElement.class).first().click(); + + assertNoErrorNotifications(); + } +} |