]> source.dussan.org Git - vaadin-framework.git/commitdiff
Tests for #7668 - re-adding a component at an index before its current one does nothing
authorJohannes Dahlström <johannes.dahlstrom@vaadin.com>
Wed, 23 Nov 2011 15:20:09 +0000 (15:20 +0000)
committerJohannes Dahlström <johannes.dahlstrom@vaadin.com>
Wed, 23 Nov 2011 15:20:09 +0000 (15:20 +0000)
svn changeset:22112/svn branch:6.7

tests/server-side/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java [new file with mode: 0644]
tests/server-side/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java [new file with mode: 0644]

diff --git a/tests/server-side/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java b/tests/server-side/com/vaadin/tests/server/component/abstractorderedlayout/AddComponentsTest.java
new file mode 100644 (file)
index 0000000..3df4d5f
--- /dev/null
@@ -0,0 +1,92 @@
+package com.vaadin.tests.server.component.abstractorderedlayout;\r
+\r
+import java.util.Iterator;\r
+import java.util.NoSuchElementException;\r
+\r
+import org.junit.Test;\r
+\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertSame;\r
+import static org.junit.Assert.fail;\r
+\r
+import com.vaadin.ui.Component;\r
+import com.vaadin.ui.AbstractOrderedLayout;\r
+import com.vaadin.ui.HorizontalLayout;\r
+import com.vaadin.ui.Layout;\r
+import com.vaadin.ui.VerticalLayout;\r
+import com.vaadin.ui.Label;\r
+\r
+public class AddComponentsTest {\r
+\r
+    Component[] children = new Component[] {\r
+            new Label("A"), new Label("B"), \r
+            new Label("C"), new Label("D") };\r
+    \r
+    @Test\r
+    public void moveComponentsBetweenLayouts() {\r
+        AbstractOrderedLayout layout1 = new HorizontalLayout();\r
+        AbstractOrderedLayout layout2 = new VerticalLayout();\r
\r
+        layout1.addComponent(children[0]);\r
+        layout1.addComponent(children[1]);\r
+        \r
+        layout2.addComponent(children[2]);\r
+        layout2.addComponent(children[3]);\r
+        \r
+        layout2.addComponent(children[1], 1);\r
+        assertOrder(layout1, new int[] { 0 });\r
+        assertOrder(layout2, new int[] { 2, 1, 3 });\r
+        \r
+        layout1.addComponent(children[3], 0);\r
+        assertOrder(layout1, new int[] { 3, 0 });\r
+        assertOrder(layout2, new int[] { 2, 1 });\r
+    }\r
+    \r
+    @Test\r
+    public void shuffleChildComponents() {\r
+        shuffleChildComponents(new HorizontalLayout());\r
+        shuffleChildComponents(new VerticalLayout());\r
+    }\r
+    \r
+    private void shuffleChildComponents(AbstractOrderedLayout layout) {\r
+        \r
+        for (int i = 0; i < children.length; ++i) {\r
+            layout.addComponent(children[i], i);\r
+        }\r
+        \r
+        assertOrder(layout, new int[] { 0, 1, 2, 3 });\r
+\r
+        // Move C from #2 to #1\r
+        // Exhibits defect #7668\r
+        layout.addComponent(children[2], 1);\r
+        assertOrder(layout, new int[] { 0, 2, 1, 3 });\r
+        \r
+        // Move C from #1 to #4 (which becomes #3 when #1 is erased)\r
+        layout.addComponent(children[2], 4);\r
+        assertOrder(layout, new int[] { 0, 1, 3, 2 });\r
+        \r
+        // Keep everything in place\r
+        layout.addComponent(children[1], 1);\r
+        assertOrder(layout, new int[] { 0, 1, 3, 2 });\r
+        \r
+        // Move D from #2 to #0\r
+        layout.addComponent(children[3], 0);\r
+        assertOrder(layout, new int[] { 3, 0, 1, 2 });\r
+    }\r
+\r
+    /**\r
+     * Asserts that layout has the components in children\r
+     * in the order specified by indices.\r
+     */\r
+    private void assertOrder(Layout layout, int[] indices) {\r
+        Iterator<?> i = layout.getComponentIterator();\r
+        try {\r
+            for(int index : indices) {\r
+                assertSame(children[index], i.next());\r
+            }\r
+            assertFalse("Too many components in layout", i.hasNext());\r
+        } catch(NoSuchElementException e) {\r
+            fail("Too few components in layout");\r
+        }\r
+    }\r
+}\r
diff --git a/tests/server-side/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java b/tests/server-side/com/vaadin/tests/server/component/csslayout/AddComponentsTest.java
new file mode 100644 (file)
index 0000000..6e69dfe
--- /dev/null
@@ -0,0 +1,87 @@
+package com.vaadin.tests.server.component.csslayout;\r
+\r
+import java.util.Iterator;\r
+import java.util.NoSuchElementException;\r
+\r
+import org.junit.Test;\r
+\r
+import static org.junit.Assert.assertSame;\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.fail;\r
+\r
+\r
+import com.vaadin.ui.Component;\r
+import com.vaadin.ui.CssLayout;\r
+import com.vaadin.ui.Label;\r
+import com.vaadin.ui.Layout;\r
+\r
+public class AddComponentsTest {\r
+\r
+    private Component[] children = new Component[] {\r
+            new Label("A"), new Label("B"), \r
+            new Label("C"), new Label("D") };\r
+    \r
+    @Test\r
+    public void moveComponentsBetweenLayouts() {\r
+        CssLayout layout1 = new CssLayout();\r
+        CssLayout layout2 = new CssLayout();\r
+        \r
+        layout1.addComponent(children[0]);\r
+        layout1.addComponent(children[1]);\r
+        \r
+        layout2.addComponent(children[2]);\r
+        layout2.addComponent(children[3]);\r
+        \r
+        layout2.addComponent(children[1], 1);\r
+        assertOrder(layout1, new int[] { 0 });\r
+        assertOrder(layout2, new int[] { 2, 1, 3 });\r
+        \r
+        layout1.addComponent(children[3], 0);\r
+        assertOrder(layout1, new int[] { 3, 0 });\r
+        assertOrder(layout2, new int[] { 2, 1 });\r
+    }\r
+    \r
+    @Test\r
+    public void shuffleChildComponents() {\r
+        CssLayout layout = new CssLayout();\r
+        \r
+        for (int i = 0; i < children.length; ++i) {\r
+            layout.addComponent(children[i], i);\r
+        }\r
+        \r
+        assertOrder(layout, new int[] { 0, 1, 2, 3 });\r
+\r
+        // Move C from #2 to #1\r
+        // Exhibits defect #7668\r
+        layout.addComponent(children[2], 1);\r
+        assertOrder(layout, new int[] { 0, 2, 1, 3 });\r
+        \r
+        // Move C from #1 to #4 (which becomes #3 when #1 is erased)\r
+        layout.addComponent(children[2], 4);\r
+        assertOrder(layout, new int[] { 0, 1, 3, 2 });\r
+        \r
+        // Keep everything in place\r
+        layout.addComponent(children[1], 1);\r
+        assertOrder(layout, new int[] { 0, 1, 3, 2 });\r
+        \r
+        // Move D from #2 to #0\r
+        layout.addComponent(children[3], 0);\r
+        assertOrder(layout, new int[] { 3, 0, 1, 2 });\r
+    }\r
+\r
+    /**\r
+     * Asserts that layout has the components in children\r
+     * in the order specified by indices.\r
+     */\r
+    private void assertOrder(Layout layout, int[] indices) {\r
+        Iterator<?> i = layout.getComponentIterator();\r
+        try {\r
+            for(int index : indices) {\r
+                assertSame(children[index], i.next());\r
+            }\r
+            assertFalse("Too many components in layout", i.hasNext());\r
+        } catch(NoSuchElementException e) {\r
+            fail("Too few components in layout");\r
+        }\r
+    }\r
+}\r