]> source.dussan.org Git - vaadin-framework.git/commitdiff
Declarative support for Image, Flash, BrowserFrame (#15551,#16327,#15552,#16325)
authorArtur Signell <artur@vaadin.com>
Mon, 9 Feb 2015 18:56:05 +0000 (20:56 +0200)
committerVaadin Code Review <review@vaadin.com>
Tue, 10 Feb 2015 10:28:06 +0000 (10:28 +0000)
Change-Id: Icef0b3c4e652421265714c4fd29dee150bcc8cd0

server/src/com/vaadin/ui/AbstractEmbedded.java
server/src/com/vaadin/ui/Flash.java
server/tests/src/com/vaadin/tests/design/DeclarativeTestBase.java
server/tests/src/com/vaadin/tests/design/EmbeddedsTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/browserframe/BrowserFrameDeclarativeTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/flash/FlashDeclarativeTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/image/ImageDeclarativeTest.java [new file with mode: 0644]

index 66752aa5d7cabf55774f0033d8ee47ab0a2c8fbf..d004ca7635cd3e3e8dab772d4f1566630a2bd2c3 100644 (file)
  */
 package com.vaadin.ui;
 
+import java.util.Collection;
+
+import org.jsoup.nodes.Element;
+
 import com.vaadin.server.Resource;
 import com.vaadin.shared.ui.AbstractEmbeddedState;
+import com.vaadin.ui.declarative.DesignAttributeHandler;
+import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * Abstract base for embedding components.
@@ -81,4 +87,27 @@ public abstract class AbstractEmbedded extends AbstractComponent {
         return getState(false).alternateText;
     }
 
+    @Override
+    public void readDesign(Element design, DesignContext designContext) {
+        super.readDesign(design, designContext);
+        if (design.hasAttr("alt")) {
+            setAlternateText(DesignAttributeHandler.readAttribute("alt",
+                    design.attributes(), String.class));
+        }
+    }
+
+    @Override
+    public void writeDesign(Element design, DesignContext designContext) {
+        super.writeDesign(design, designContext);
+        AbstractEmbedded def = designContext.getDefaultInstance(this);
+        DesignAttributeHandler.writeAttribute("alt", design.attributes(),
+                getAlternateText(), def.getAlternateText(), String.class);
+    }
+
+    @Override
+    protected Collection<String> getCustomAttributes() {
+        Collection<String> c = super.getCustomAttributes();
+        c.add("alternate-text");
+        return c;
+    }
 }
index bbbd4e3285cc105e8bed7113ab83b5a80a61ae94..cd7c00087ee36a2d670ad2f7d06a71d3feb4a467 100644 (file)
  */
 package com.vaadin.ui;
 
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.Map;
+
+import org.jsoup.nodes.Element;
 
 import com.vaadin.server.Resource;
 import com.vaadin.shared.ui.flash.FlashState;
+import com.vaadin.ui.declarative.DesignContext;
 
 /**
  * A component for displaying Adobe® Flash® content.
@@ -88,6 +93,17 @@ public class Flash extends AbstractEmbedded {
         }
     }
 
+    /**
+     * Returns the codebase.
+     * 
+     * @see #setCodebase(String)
+     * @since 7.4
+     * @return Current codebase.
+     */
+    public String getCodebase() {
+        return getState(false).codebase;
+    }
+
     /**
      * This attribute specifies the content type of data expected when
      * downloading the object specified by classid. This attribute is optional
@@ -106,6 +122,17 @@ public class Flash extends AbstractEmbedded {
         }
     }
 
+    /**
+     * Returns the current codetype.
+     * 
+     * @see #setCodetype(String)
+     * @since 7.4
+     * @return Current codetype.
+     */
+    public String getCodetype() {
+        return getState(false).codetype;
+    }
+
     /**
      * This attribute may be used to specify a space-separated list of URIs for
      * archives containing resources relevant to the object, which may include
@@ -126,6 +153,23 @@ public class Flash extends AbstractEmbedded {
         }
     }
 
+    /**
+     * Returns current archive.
+     * 
+     * @see #setArchive(String)
+     * @since 7.4
+     * @return Current archive.
+     */
+    public String getArchive() {
+        return getState(false).archive;
+    }
+
+    /**
+     * Sets standby.
+     * 
+     * @param standby
+     *            Standby string.
+     */
     public void setStandby(String standby) {
         if (standby != getState().standby
                 || (standby != null && !standby.equals(getState().standby))) {
@@ -134,6 +178,16 @@ public class Flash extends AbstractEmbedded {
         }
     }
 
+    /**
+     * Returns standby.
+     * 
+     * @since
+     * @return Standby string.
+     */
+    public String getStandby() {
+        return getState(false).standby;
+    }
+
     /**
      * Sets an object parameter. Parameters are optional information, and they
      * are passed to the instantiated object. Parameters are are stored as name
@@ -179,4 +233,38 @@ public class Flash extends AbstractEmbedded {
         requestRepaint();
     }
 
+    @Override
+    public void writeDesign(Element design, DesignContext designContext) {
+        super.writeDesign(design, designContext);
+        for (String param : getParameterNames()) {
+            design.appendElement("parameter").attr("name", param)
+                    .attr("value", getParameter(param));
+        }
+    }
+
+    /**
+     * Returns an iterable with declared parameter names.
+     * 
+     * @see #setParameter(String, String)
+     * @see #getParameter(String)
+     * @since 7.4
+     * @return An iterable with declared parameter names.
+     */
+    public Iterable<String> getParameterNames() {
+        Map<String, String> map = getState(false).embedParams;
+        if (map == null) {
+            return Collections.emptySet();
+        } else {
+            return Collections.unmodifiableSet(map.keySet());
+        }
+    }
+
+    @Override
+    public void readDesign(Element design, DesignContext designContext) {
+        super.readDesign(design, designContext);
+        for (Element paramElement : design.getElementsByTag("parameter")) {
+            setParameter(paramElement.attr("name"), paramElement.attr("value"));
+        }
+    }
+
 }
index 5bdca6e06aa87379738d4474cd590217f64425f4..cba981c94757a75213032983809410cdb11c3ee1 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Map;
 import org.junit.Assert;
 
 import com.vaadin.ui.Component;
+import com.vaadin.ui.Flash;
 
 public abstract class DeclarativeTestBase<T extends Component> extends
         DeclarativeTestBaseBase<T> {
@@ -84,6 +85,23 @@ public abstract class DeclarativeTestBase<T extends Component> extends
         }
     }
 
+    {
+        comparators.put(Flash.class, new IntrospectorEqualsAsserter<Flash>(
+                Flash.class) {
+            @Override
+            public void assertObjectEquals(Flash o1, Flash o2) {
+                super.assertObjectEquals(o1, o2);
+                assertEquals("parameterNames", o1.getParameterNames(),
+                        o2.getParameterNames());
+                for (String name : o1.getParameterNames()) {
+                    assertEquals("Parameter " + name, o1.getParameter(name),
+                            o2.getParameter(name));
+                }
+            }
+        });
+
+    }
+
     @Override
     protected EqualsAsserter getComparator(Class c) {
         com.vaadin.tests.design.DeclarativeTestBaseBase.EqualsAsserter<?> comp = comparators
diff --git a/server/tests/src/com/vaadin/tests/design/EmbeddedsTest.java b/server/tests/src/com/vaadin/tests/design/EmbeddedsTest.java
new file mode 100644 (file)
index 0000000..4c9e323
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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.design;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import org.junit.Test;
+
+import com.vaadin.server.ExternalResource;
+import com.vaadin.shared.util.SharedUtil;
+import com.vaadin.ui.AbstractEmbedded;
+import com.vaadin.ui.BrowserFrame;
+import com.vaadin.ui.Embedded;
+import com.vaadin.ui.Flash;
+import com.vaadin.ui.Image;
+import com.vaadin.ui.declarative.Design;
+
+/**
+ * Tests declarative support for implementations of {@link AbstractEmbedded} and
+ * {@link Embedded}.
+ * 
+ * @since 7.4
+ * @author Vaadin Ltd
+ */
+public class EmbeddedsTest {
+
+    public static final boolean equals(ExternalResource obj,
+            ExternalResource other) {
+        return SharedUtil.equals(obj.getURL(), other.getURL())
+                && SharedUtil.equals(obj.getMIMEType(), other.getMIMEType());
+    }
+
+    @Test
+    public void testAbstractEmbeddedsToFromDesign() throws Exception {
+        for (AbstractEmbedded ae : new AbstractEmbedded[] { new Image(),
+                new Flash(), new BrowserFrame() }) {
+            ae.setSource(new ExternalResource("http://www.example.org"));
+            ae.setAlternateText("some alternate text");
+            ae.setCaption("some <b>caption</b>");
+            ae.setCaptionAsHtml(true);
+            ae.setDescription("some description");
+            ByteArrayOutputStream bos = new ByteArrayOutputStream();
+            Design.write(ae, bos);
+            AbstractEmbedded result = (AbstractEmbedded) Design
+                    .read(new ByteArrayInputStream(bos.toByteArray()));
+            assertTrue(equals((ExternalResource) ae.getSource(),
+                    (ExternalResource) result.getSource()));
+            assertEquals(ae.getAlternateText(), result.getAlternateText());
+            assertEquals(ae.getCaption(), result.getCaption());
+            assertEquals(ae.isCaptionAsHtml(), result.isCaptionAsHtml());
+            assertEquals(ae.getDescription(), result.getDescription());
+        }
+    }
+
+    @Test
+    public void testFlashToFromDesign() throws Exception {
+        Flash ae = new Flash();
+        ae.setSource(new ExternalResource("http://www.example.org"));
+        ae.setAlternateText("some alternate text");
+        ae.setCaption("some <b>caption</b>");
+        ae.setCaptionAsHtml(true);
+        ae.setDescription("some description");
+        ae.setCodebase("codebase");
+        ae.setArchive("archive");
+        ae.setCodetype("codetype");
+        ae.setParameter("foo", "bar");
+        ae.setParameter("something", "else");
+        ae.setStandby("foobar");
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        Design.write(ae, bos);
+        Flash result = (Flash) Design.read(new ByteArrayInputStream(bos
+                .toByteArray()));
+        assertTrue(equals((ExternalResource) ae.getSource(),
+                (ExternalResource) result.getSource()));
+        assertEquals(ae.getAlternateText(), result.getAlternateText());
+        assertEquals(ae.getCaption(), result.getCaption());
+        assertEquals(ae.isCaptionAsHtml(), result.isCaptionAsHtml());
+        assertEquals(ae.getDescription(), result.getDescription());
+        assertEquals(ae.getCodebase(), result.getCodebase());
+        assertEquals(ae.getArchive(), result.getArchive());
+        assertEquals(ae.getCodetype(), result.getCodetype());
+        assertEquals(ae.getParameter("foo"), result.getParameter("foo"));
+        assertEquals(ae.getParameter("something"),
+                result.getParameter("something"));
+        assertEquals(ae.getStandby(), result.getStandby());
+    }
+
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/browserframe/BrowserFrameDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/browserframe/BrowserFrameDeclarativeTest.java
new file mode 100644 (file)
index 0000000..f4f5056
--- /dev/null
@@ -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.server.component.browserframe;
+
+import org.junit.Test;
+
+import com.vaadin.server.ExternalResource;
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.BrowserFrame;
+
+/**
+ * Tests declarative support for implementations of {@link BrowserFrame}.
+ * 
+ * @since 7.4
+ * @author Vaadin Ltd
+ */
+public class BrowserFrameDeclarativeTest extends
+        DeclarativeTestBase<BrowserFrame> {
+
+    protected String getDesign() {
+        return "<v-browser-frame source='http://foo.bar/some.html' />";
+    }
+
+    protected BrowserFrame getExpectedResult() {
+        BrowserFrame i = new BrowserFrame();
+        i.setSource(new ExternalResource("http://foo.bar/some.html"));
+        return i;
+    };
+
+    @Test
+    public void read() {
+        testRead(getDesign(), getExpectedResult());
+    }
+
+    @Test
+    public void write() {
+        testWrite(getDesign(), getExpectedResult());
+    }
+
+    @Test
+    public void testEmpty() {
+        testRead("<v-browser-frame/>", new BrowserFrame());
+    }
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/flash/FlashDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/flash/FlashDeclarativeTest.java
new file mode 100644 (file)
index 0000000..f16184d
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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.flash;
+
+import org.junit.Test;
+
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.AbstractEmbedded;
+import com.vaadin.ui.Embedded;
+import com.vaadin.ui.Flash;
+
+/**
+ * Tests declarative support for implementations of {@link AbstractEmbedded} and
+ * {@link Embedded}.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class FlashDeclarativeTest extends DeclarativeTestBase<Flash> {
+
+    protected Flash getExpectedResult() {
+        Flash f = new Flash();
+        f.setArchive("arch");
+        f.setCodebase("foo");
+        f.setCodetype("bar");
+        f.setStandby("Please wait");
+        f.setParameter("foo", "bar");
+        f.setParameter("baz", "foo");
+        return f;
+    };
+
+    protected String getDesign() {
+        return "<v-flash standby='Please wait' archive='arch' codebase='foo' codetype='bar' >"
+                + "  <parameter name='baz' value='foo' />\n" //
+                + "  <parameter name='foo' value='bar' />\n" //
+                + "</v-flash>"; //
+    }
+
+    @Test
+    public void read() {
+        testRead(getDesign(), getExpectedResult());
+    }
+
+    @Test
+    public void write() {
+        testWrite(getDesign(), getExpectedResult());
+    }
+
+    @Test
+    public void testEmpty() {
+        testRead("<v-flash />", new Flash());
+    }
+
+}
diff --git a/server/tests/src/com/vaadin/tests/server/component/image/ImageDeclarativeTest.java b/server/tests/src/com/vaadin/tests/server/component/image/ImageDeclarativeTest.java
new file mode 100644 (file)
index 0000000..670b3be
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * 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.image;
+
+import org.junit.Test;
+
+import com.vaadin.server.ExternalResource;
+import com.vaadin.tests.design.DeclarativeTestBase;
+import com.vaadin.ui.Image;
+
+/**
+ * Tests declarative support for implementations of {@link Image}.
+ * 
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ImageDeclarativeTest extends DeclarativeTestBase<Image> {
+
+    protected String getDesign() {
+        return "<v-image source='http://foo.bar/img.png' alt='Some random image from the theme'></v-image>";
+    }
+
+    protected Image getExpectedResult() {
+        Image i = new Image();
+        i.setSource(new ExternalResource("http://foo.bar/img.png"));
+        i.setAlternateText("Some random image from the theme");
+        return i;
+    };
+
+    @Test
+    public void read() {
+        testRead(getDesign(), getExpectedResult());
+    }
+
+    @Test
+    public void write() {
+        testWrite(getDesign(), getExpectedResult());
+    }
+
+    @Test
+    public void testEmpty() {
+        testRead("<v-image />", new Image());
+    }
+
+}