]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix declarative support for CustomLayout (#17210)
authorJohannes Dahlström <johannesd@vaadin.com>
Mon, 23 Mar 2015 10:10:07 +0000 (12:10 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 24 Mar 2015 09:04:25 +0000 (09:04 +0000)
CustomLayout now has a public default constructor. If a template is not set
using one of the setters, a warning message is displayed like in the case where
the template file is specified but not found.

Change-Id: I5d56f24fafc5c82e6ab76dec393a0c25bd78aae5

client/src/com/vaadin/client/ui/customlayout/CustomLayoutConnector.java
server/src/com/vaadin/ui/CustomLayout.java
server/tests/src/com/vaadin/tests/server/component/customlayout/CustomLayoutDeclarativeTest.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/customlayout/CustomLayoutWithNullTemplate.java [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/customlayout/CustomLayoutWithNullTemplateTest.java [new file with mode: 0644]

index 80979587b91fb37b1c1cae535d39ffb286f9f562..cde1f1af0fbd5c67ae0393ac6f7868df1cc2dbf5 100644 (file)
@@ -77,22 +77,24 @@ public class CustomLayoutConnector extends AbstractLayoutConnector implements
             // (even though both can never be given at the same time)
             templateContents = getConnection().getResource(
                     "layouts/" + templateName + ".html");
-            if (templateContents == null) {
-                // Template missing -> show debug notice and render components
-                // in order.
-                getWidget()
-                        .getElement()
-                        .setInnerHTML(
-                                "<em>Layout file layouts/"
-                                        + templateName
-                                        + ".html is missing. Components will be drawn for debug purposes.</em>");
-            }
         }
 
         if (templateContents != null) {
             // Template ok -> initialize.
             getWidget().initializeHTML(templateContents,
                     getConnection().getThemeUri());
+        } else {
+            // Template missing -> show debug notice and render components in
+            // order.
+            String warning = templateName != null ? "Layout file layouts/"
+                    + templateName + ".html is missing."
+                    : "Layout file not specified.";
+            getWidget()
+                    .getElement()
+                    .setInnerHTML(
+                            "<em>"
+                                    + warning
+                                    + " Components will be drawn for debug purposes.</em>");
         }
         templateUpdated = true;
     }
index a9c266b0b955354ca5c3c1f553034efc89391975..ceb47e1e7a89cb102d8f65b7f91ea9d49dccdd26 100644 (file)
@@ -23,12 +23,16 @@ import java.io.InputStreamReader;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 
+import org.jsoup.nodes.Element;
+
 import com.vaadin.server.JsonPaintTarget;
 import com.vaadin.server.PaintException;
 import com.vaadin.server.PaintTarget;
 import com.vaadin.shared.ui.customlayout.CustomLayoutState;
+import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * <p>
@@ -71,8 +75,8 @@ public class CustomLayout extends AbstractLayout implements LegacyComponent {
      * {@link #setTemplateName(String)}, that makes layout fetch the template
      * from theme, or {@link #setTemplateContents(String)}.
      */
-    protected CustomLayout() {
-        setWidth(100, UNITS_PERCENTAGE);
+    public CustomLayout() {
+        setWidth(100, Unit.PERCENTAGE);
     }
 
     /**
@@ -305,4 +309,32 @@ public class CustomLayout extends AbstractLayout implements LegacyComponent {
         }
     }
 
+    @Override
+    public void readDesign(Element design, DesignContext designContext) {
+        super.readDesign(design, designContext);
+
+        for (Element child : design.children()) {
+
+            Component childComponent = designContext.readDesign(child);
+
+            if (child.hasAttr(":location")) {
+                addComponent(childComponent, child.attr(":location"));
+            } else {
+                addComponent(childComponent);
+            }
+        }
+    }
+
+    @Override
+    public void writeDesign(Element design, DesignContext designContext) {
+        super.writeDesign(design, designContext);
+
+        for (Entry<String, Component> slot : slots.entrySet()) {
+            Element child = designContext.createElement(slot.getValue());
+            if (slots.size() > 1 || !"".equals(slot.getKey())) {
+                child.attr(":location", slot.getKey());
+            }
+            design.appendChild(child);
+        }
+    }
 }
diff --git a/server/tests/src/com/vaadin/tests/server/component/customlayout/CustomLayoutDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/customlayout/CustomLayoutDeclarativeTest.java
new file mode 100644 (file)
index 0000000..44261a6
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * 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.server.component.customlayout;
+
+import org.junit.Test;
+
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.CustomLayout;
+import com.vaadin.ui.Label;
+
+/**
+ * Tests declarative support for {@link CustomLayout}.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class CustomLayoutDeclarativeTest extends
+        DeclarativeTestBase<CustomLayout> {
+
+    @Test
+    public void testEmpty() {
+        String design = "<v-custom-layout>";
+        CustomLayout expected = new CustomLayout();
+        test(design, expected);
+    }
+
+    @Test
+    public void testWithChildren() {
+        String design = "<v-custom-layout>" + //
+                "<v-button plain-text :location='b'></v-button>" + //
+                "<v-label plain-text :location='l'></v-label>" + //
+                "</v-custom-layout>";
+
+        CustomLayout expected = new CustomLayout();
+        expected.addComponent(new Button(), "b");
+        expected.addComponent(new Label(), "l");
+
+        test(design, expected);
+    }
+
+    @Test
+    public void testWithOneChild() {
+        String design = "<v-custom-layout><v-button plain-text></v-button></v-custom-layout>";
+
+        CustomLayout expected = new CustomLayout();
+        expected.addComponent(new Button());
+
+        test(design, expected);
+    }
+
+    @Test
+    public void testWithTemplate() {
+        String design = "<v-custom-layout template-name='template.html'></v-custom-layout>";
+        CustomLayout expected = new CustomLayout("template.html");
+        test(design, expected);
+    }
+
+    @Test
+    public void testWithDuplicateLocations() {
+        String design = "<v-custom-layout>" + //
+                "<v-button plain-text :location='foo'></v-button>" + //
+                "<v-label plain-text :location='foo'></v-label>" + //
+                "</v-custom-layout>";
+
+        CustomLayout expected = new CustomLayout();
+        expected.addComponent(new Button(), "foo");
+        expected.addComponent(new Label(), "foo");
+
+        testRead(design, expected);
+
+        String written = "<v-custom-layout>" + //
+                "<v-label plain-text :location='foo'></v-label>" + //
+                "</v-custom-layout>";
+
+        testWrite(written, expected);
+    }
+
+    protected void test(String design, CustomLayout expected) {
+        testRead(design, expected);
+        testWrite(design, expected);
+    }
+}
diff --git a/uitest/src/com/vaadin/tests/components/customlayout/CustomLayoutWithNullTemplate.java b/uitest/src/com/vaadin/tests/components/customlayout/CustomLayoutWithNullTemplate.java
new file mode 100644 (file)
index 0000000..de1caf8
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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.customlayout;
+
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.CustomLayout;
+import com.vaadin.ui.Label;
+
+public class CustomLayoutWithNullTemplate extends AbstractTestUI {
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        CustomLayout cl = new CustomLayout();
+        cl.addComponent(new Label("This Label should be visible."), "foo");
+        cl.addComponent(new Button("This Button too."), "bar");
+
+        addComponent(cl);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        return "Verify that a default-constructed CustomLayout renders child components";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 17210;
+    }
+}
diff --git a/uitest/src/com/vaadin/tests/components/customlayout/CustomLayoutWithNullTemplateTest.java b/uitest/src/com/vaadin/tests/components/customlayout/CustomLayoutWithNullTemplateTest.java
new file mode 100644 (file)
index 0000000..668d43f
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * 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.customlayout;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vaadin.testbench.ElementQuery;
+import com.vaadin.testbench.elements.ButtonElement;
+import com.vaadin.testbench.elements.CustomLayoutElement;
+import com.vaadin.testbench.elements.LabelElement;
+import com.vaadin.tests.tb3.SingleBrowserTest;
+
+public class CustomLayoutWithNullTemplateTest extends SingleBrowserTest {
+
+    @Test
+    public void testChildComponents() {
+        openTestURL();
+
+        ElementQuery<CustomLayoutElement> customLayout = $(CustomLayoutElement.class);
+
+        // Verify the Button and Label are rendered inside the CustomLayout.
+        assertTrue("Button was not rendered.",
+                customLayout.$(ButtonElement.class).exists());
+        assertTrue("Label was not rendered.", customLayout
+                .$(LabelElement.class).exists());
+    }
+
+}