]> source.dussan.org Git - vaadin-framework.git/commitdiff
Toggle column reordering from server side. Tests for reordering. #16643
authorPekka Hyvönen <pekka@vaadin.com>
Tue, 17 Feb 2015 08:54:40 +0000 (10:54 +0200)
committerPekka Hyvönen <pekka@vaadin.com>
Tue, 17 Feb 2015 14:34:28 +0000 (16:34 +0200)
Change-Id: Ib52143ce387f6376878bf3d1c401615a15f1a3cc

server/src/com/vaadin/ui/Grid.java
shared/src/com/vaadin/shared/ui/grid/GridState.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/GridBasicFeatures.java
uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridColumnReorderTest.java [new file with mode: 0644]

index 2bc42676c3a06dd1a3a455d7abf5d0ea191e6dcd..764960606a9ef6f75e7fa2382cbf60d927f472c9 100644 (file)
@@ -3469,6 +3469,31 @@ public class Grid extends AbstractComponent implements SelectionNotifier,
         return columnKeys.get(columnId);
     }
 
+    /**
+     * Returns whether column reordering is allowed. Default value is
+     * <code>false</code>.
+     * 
+     * @since
+     * @return true if reordering is allowed
+     */
+    public boolean isColumnReorderingAllowed() {
+        return getState(false).columnReorderingAllowed;
+    }
+
+    /**
+     * Sets whether or not column reordering is allowed. Default value is
+     * <code>false</code>.
+     * 
+     * @since
+     * @param columnReorderingAllowed
+     *            specifies whether column reordering is allowed
+     */
+    public void setColumnReorderingAllowed(boolean columnReorderingAllowed) {
+        if (isColumnReorderingAllowed() != columnReorderingAllowed) {
+            getState().columnReorderingAllowed = columnReorderingAllowed;
+        }
+    }
+
     @Override
     protected GridState getState() {
         return (GridState) super.getState();
index 7018df1413adae97589aa37e27413285bf47ca6c..ab42a5242421911afd45f7af8da5be667da83556 100644 (file)
@@ -156,4 +156,8 @@ public class GridState extends AbstractComponentState {
     /** The caption for the cancel button in the editor */
     @DelegateToWidget
     public String editorCancelCaption = GridConstants.DEFAULT_CANCEL_CAPTION;
+
+    /** Whether the columns can be reordered */
+    @DelegateToWidget
+    public boolean columnReorderingAllowed;
 }
index e5a46894b88e0ff128d79bf5033ea968bf9a6679..3a6aca11f2a2fe6b82a87b9541b83c97e377bd26 100644 (file)
@@ -508,6 +508,14 @@ public class GridBasicFeatures extends AbstractComponentTest<Grid> {
                         }
                     }
                 });
+        createBooleanAction("Column Reordering Allowed", "State", false,
+                new Command<Grid, Boolean>() {
+
+                    @Override
+                    public void execute(Grid c, Boolean value, Object data) {
+                        c.setColumnReorderingAllowed(value);
+                    }
+                });
     }
 
     protected void createHeaderActions() {
diff --git a/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridColumnReorderTest.java b/uitest/src/com/vaadin/tests/components/grid/basicfeatures/server/GridColumnReorderTest.java
new file mode 100644 (file)
index 0000000..7b62ff8
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * 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.basicfeatures.server;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.interactions.Actions;
+
+import com.vaadin.testbench.TestBenchElement;
+import com.vaadin.tests.components.grid.basicfeatures.GridBasicFeaturesTest;
+
+/**
+ * Tests that Grid columns can be reordered by user with drag and drop #16643.
+ * 
+ * @author Vaadin Ltd
+ */
+public class GridColumnReorderTest extends GridBasicFeaturesTest {
+
+    private static final String[] COLUMN_REORDERING_PATH = { "Component",
+            "State", "Column Reordering Allowed" };
+
+    @Before
+    public void setUp() {
+        setDebug(true);
+    }
+
+    @Test
+    public void testColumnReordering_firstColumnDroppedOnThird_dropOnLeftSide() {
+        // given
+        openTestURL();
+        assertColumnHeaderOrder(0, 1, 2);
+        toggleColumnReordering();
+
+        // when
+        dragDefaultColumnHeader(0, 2, 10);
+
+        // then
+        assertColumnHeaderOrder(1, 0, 2);
+    }
+
+    @Test
+    public void testColumnReordering_firstColumnDroppedOnThird_dropOnRightSide() {
+        // given
+        openTestURL();
+        assertColumnHeaderOrder(0, 1, 2);
+        toggleColumnReordering();
+
+        // when
+        dragDefaultColumnHeader(0, 2, 110);
+
+        // then
+        assertColumnHeaderOrder(1, 2, 0);
+    }
+
+    @Test
+    public void testColumnReordering_notEnabled_noReordering() {
+        // given
+        openTestURL();
+        assertColumnHeaderOrder(0, 1, 2);
+
+        // when
+        dragDefaultColumnHeader(0, 2, 110);
+
+        // then
+        assertColumnHeaderOrder(0, 1, 2);
+    }
+
+    private void toggleColumnReordering() {
+        selectMenuPath(COLUMN_REORDERING_PATH);
+    }
+
+    private void assertColumnHeaderOrder(int... indices) {
+        List<TestBenchElement> headers = getGridHeaderRowCells();
+        for (int i = 0; i < indices.length; i++) {
+            assertColumnHeader("Column " + indices[i], headers.get(i));
+        }
+    }
+
+    private void assertColumnHeader(String expectedHeaderCaption,
+            TestBenchElement testBenchElement) {
+        assertEquals(expectedHeaderCaption.toLowerCase(), testBenchElement
+                .getText().toLowerCase());
+    }
+
+    private WebElement getDefaultColumnHeader(int index) {
+        List<TestBenchElement> headerRowCells = getGridHeaderRowCells();
+        return headerRowCells.get(index);
+    }
+
+    private void dragDefaultColumnHeader(int draggedColumnHeaderIndex,
+            int onTopOfColumnHeaderIndex, int xOffsetFromColumnTopLeftCorner) {
+        new Actions(getDriver())
+                .clickAndHold(getDefaultColumnHeader(draggedColumnHeaderIndex))
+                .moveToElement(
+                        getDefaultColumnHeader(onTopOfColumnHeaderIndex),
+                        xOffsetFromColumnTopLeftCorner, 0).release().perform();
+    }
+}