From ffb4036fe0573c35e759cb89e8b252f5477ecbea Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Mon, 8 Jun 2015 13:44:34 +0300 Subject: [PATCH] Fix Grid drag selection with scrolled page (#17895) Change-Id: I1303781c5a996448e12e86835935702686af3ab7 --- .../selection/MultiSelectionRenderer.java | 26 +-------- .../grid/GridDragSelectionWhileScrolled.java | 51 +++++++++++++++++ .../GridDragSelectionWhileScrolledTest.java | 57 +++++++++++++++++++ 3 files changed, 111 insertions(+), 23 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolled.java create mode 100644 uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolledTest.java diff --git a/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java b/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java index c8a7ceeca3..1e47d3ad6b 100644 --- a/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java +++ b/client/src/com/vaadin/client/widget/grid/selection/MultiSelectionRenderer.java @@ -535,10 +535,8 @@ public class MultiSelectionRenderer extends final int topBorder = getBodyClientTop(); final int bottomBorder = getBodyClientBottom(); - final int scrollCompensation = getScrollCompensation(); - topBound = scrollCompensation + topBorder + SCROLL_AREA_GRADIENT_PX; - bottomBound = scrollCompensation + bottomBorder - - SCROLL_AREA_GRADIENT_PX; + topBound = topBorder + SCROLL_AREA_GRADIENT_PX; + bottomBound = bottomBorder - SCROLL_AREA_GRADIENT_PX; gradientArea = SCROLL_AREA_GRADIENT_PX; // modify bounds if they're too tightly packed @@ -551,17 +549,6 @@ public class MultiSelectionRenderer extends } } - private int getScrollCompensation() { - Element cursor = grid.getElement(); - int scroll = 0; - while (cursor != null) { - scroll -= cursor.getScrollTop(); - cursor = cursor.getParentElement(); - } - - return scroll; - } - public void stop() { if (handlerRegistration != null) { handlerRegistration.removeHandler(); @@ -743,15 +730,8 @@ public class MultiSelectionRenderer extends } /** Get the "top" of an element in relation to "client" coordinates. */ - @SuppressWarnings("static-method") private int getClientTop(final Element e) { - Element cursor = e; - int top = 0; - while (cursor != null) { - top += cursor.getOffsetTop(); - cursor = cursor.getOffsetParent(); - } - return top; + return e.getAbsoluteTop(); } private int getBodyClientBottom() { diff --git a/uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolled.java b/uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolled.java new file mode 100644 index 0000000000..1af1ad02d6 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolled.java @@ -0,0 +1,51 @@ +/* + * 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 com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Grid.SelectionMode; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Layout; +import com.vaadin.ui.VerticalLayout; + +public class GridDragSelectionWhileScrolled extends AbstractTestUI { + + @Override + protected void setup(VaadinRequest request) { + Layout layout = new VerticalLayout(); + + HorizontalLayout spacer = new HorizontalLayout(); + spacer.setHeight("1000px"); + layout.addComponent(spacer); + + PersonTestGrid grid = new PersonTestGrid(100); + grid.setSelectionMode(SelectionMode.MULTI); + layout.addComponent(grid); + + addComponent(layout); + } + + @Override + protected Integer getTicketNumber() { + return 17895; + } + + @Override + protected String getTestDescription() { + return "Drag selecting rows in Grid malfunctions if page is scrolled"; + } +} diff --git a/uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolledTest.java b/uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolledTest.java new file mode 100644 index 0000000000..6a452e1fec --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/grid/GridDragSelectionWhileScrolledTest.java @@ -0,0 +1,57 @@ +/* + * 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 static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.junit.Test; +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.interactions.Actions; + +import com.vaadin.testbench.elements.GridElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class GridDragSelectionWhileScrolledTest extends MultiBrowserTest { + + @Override + protected boolean requireWindowFocusForIE() { + return true; + } + + @Test + public void testDragSelect() throws IOException { + openTestURL(); + + // Scroll grid to view + GridElement grid = $(GridElement.class).first(); + ((JavascriptExecutor) getDriver()).executeScript( + "arguments[0].scrollIntoView(true);", grid); + + // Drag select 2 rows + new Actions(getDriver()).moveToElement(grid.getCell(3, 0), 5, 5) + .clickAndHold().moveToElement(grid.getCell(2, 0), 5, 5) + .release().perform(); + + // Assert only those are selected. + assertTrue("Row 3 should be selected", grid.getRow(3).isSelected()); + assertTrue("Row 2 should be selected", grid.getRow(2).isSelected()); + assertFalse("Row 4 should not be selected", grid.getRow(4).isSelected()); + assertFalse("Row 1 should not be selected", grid.getRow(1).isSelected()); + } +} -- 2.39.5